linecache module load and cache file contents in detail

  • 2020-06-23 01:02:02
  • OfStack

linecache module

I came into contact with the linecache module because when I read the SOURCE code of attrs two days ago, I saw that the internal code referred to this module to simulate a fake file. I read the source code of this module with a face of doubt and found that it was just that. The code was not much.

The linecache module can read a file and cache its contents for multiple later reads. This module was originally designed to read the source code of the Python module, so when a file name is not under the specified path, the module will try to read the file by searching the path (search path).

interface

The argument of the linecache module with SUCCession17EN__ really only provides the getline/clearcache/checkcache3 interfaces, but there is more to it than that. I'll describe each of these interfaces individually.

linecache.getline(filename, lineno, module_globals=None)

Get some 1 row of the specified file, filename specified filename, lineno specifies the line number, module_globals context is used to specify the module I don't know how to call, can call context, finally actually present at the meeting to a parameter linecache. updatecache (), is used to try to use __loader__ load file, 1 cases will not use the last one parameter, can be ignored. The function returns an empty string when the line number is less than 1 or greater than the file's maximum line number.

linecache.clearcache()

Clear all caches, notice all caches.

linecache.checkcache(filename=None)

This function checks the cache, removes the original cache if the file size or modification time changes, and keeps the cache unchanged if the file is lazily loaded. Check all files in the cache when filename is None.

The following is the interface not written into the argument of the module ___.

linecache.lazycache(filename, module_globals)

Using lazy loading for the specified file, enabling lazy loading successfully loads the file content into memory only when the interface to get the content is actually called. Using this avoids redundant files IO. The return value is 1 Boolean, True if the lazy load succeeds, False if the file content has actually been loaded into memory, or False if the load fails.

The module_globals parameter to this function is required and is essentially the context in which the module to load is passed. If you load the linecache module, pass in linecache. ___, 62en__, or vars(linecache) (these are the only two options available, though you can build a dictionary and pass it in). The function then gets the get_source function of ___ 65en__, depending on the context, and saves it to the cache. This function was added after Python3.5.

linecache.updatecache(filename, module_globals=None)

This function is the core of the entire module and is used to update the file cache and return the file contents. Any error in the middle of the function will return an empty list.

For normal files, the tokenize.open () function is used internally to open the file, detect the encoding of the file, and open the file with the detected encoding. The default is ES78en-8 if the encoding is missing. If the file cannot be opened with the given path, try to load it using the path specified by sys.path. If \n is not in the last line of file content, \n is automatically added to the last character.

For lazily loaded files, the get_source function saved during lazy loading is called to get the file contents.

Note: linecache USES readlines1 to load all file contents once a file has been opened, so it can cause problems if files are too many or too large, so use with caution.

linecache.getlines(filename, module_globals=None)

Gets all the contents of the file. If the file has not been loaded or is lazy, linecache.updatecache () is called to load the file contents, and MemoryError clears the cache if it appears. linecache.getline() actually calls this function internally.

linecache.cache

This is a dictionary, and all the file caches are stored in it. The Key of the dictionary is the filename passed in as you read it, and Value is the tuple with the file size, modification time, content, and name, and the get_source function with each successive 109EN__ if the file is lazy loaded.

conclusion

The Python standard library has a lot of basic modules built into it that you wouldn't normally notice, but there are always other pieces of code that rely on this infrastructure, like pdb and traceback in the standard library. There is actually a lot of code can be used, usually read more source code, there will be a surprise.

That's the end of this article on linecache module loading and caching file content in detail, and I hope you found it helpful. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: