python linecache source code

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python linecache source code相关的知识,希望对你有一定的参考价值。

The source code of linecache.py from python 2.0 is here.

from stat import *

Get file information

>>> filestat = os.stat(README.TXT)
>>> filestat
nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=2623L, st_atime=1450607830L, st_mtime=1398844440L, st_ctime=1398844440L)
>>> filestat.st_mtime
1398844440.0
>>> import stat
>>> from stat import *
>>> filestat[ST_MTIME]
1398844440L

Notice filesta.st_mtime is a float number while filestat[ST_MTIME] is a long number which is strange.

Each cache is a entry is a dictionary with key=filename value=(size, mtime, lines, fullnames) (line 91)

Cache is only updated when a cache miss occurs. (line 39)

Function checkcache() is NOT called automatically and it seems that the user should call this function manually.

Source Code

 1 """Cache lines from files.
 2 
 3 This is intended to read lines from modules imported -- hence if a filename
 4 is not found, it will look down the module search path for a file by
 5 that name.
 6 """
 7 
 8 import sys
 9 import os
10 from stat import *
11 
12 def getline(filename, lineno):
13     lines = getlines(filename)
14     if 1 <= lineno <= len(lines):
15         return lines[lineno-1]
16     else:
17         return ‘‘
18 
19 
20 # The cache
21 
22 cache = {} # The cache
23 
24 
25 def clearcache():
26     """Clear the cache entirely."""
27 
28     global cache
29     cache = {}
30 
31 
32 def getlines(filename):
33     """Get the lines for a file from the cache.
34     Update the cache if it doesn‘t contain an entry for this file already."""
35 
36     if cache.has_key(filename):
37         return cache[filename][2]
38     else:
39         return updatecache(filename)
40 
41 
42 def checkcache():
43     """Discard cache entries that are out of date.
44     (This is not checked upon each call!)"""
45 
46     for filename in cache.keys():
47         size, mtime, lines, fullname = cache[filename]
48         try:
49             stat = os.stat(fullname)
50         except os.error:
51             del cache[filename]
52             continue
53         if size <> stat[ST_SIZE] or mtime <> stat[ST_MTIME]:
54             del cache[filename]
55 
56 
57 def updatecache(filename):
58     """Update a cache entry and return its list of lines.
59     If something‘s wrong, print a message, discard the cache entry,
60     and return an empty list."""
61 
62     if cache.has_key(filename):
63         del cache[filename]
64     if not filename or filename[0] + filename[-1] == <>:
65         return []
66     fullname = filename
67     try:
68         stat = os.stat(fullname)
69     except os.error, msg:
70         # Try looking through the module search path
71         basename = os.path.split(filename)[1]
72         for dirname in sys.path:
73             fullname = os.path.join(dirname, basename)
74             try:
75                 stat = os.stat(fullname)
76                 break
77             except os.error:
78                 pass
79         else:
80             # No luck
81 ##          print ‘*** Cannot stat‘, filename, ‘:‘, msg
82             return []
83     try:
84         fp = open(fullname, r)
85         lines = fp.readlines()
86         fp.close()
87     except IOError, msg:
88 ##      print ‘*** Cannot open‘, fullname, ‘:‘, msg
89         return []
90     size, mtime = stat[ST_SIZE], stat[ST_MTIME]
91     cache[filename] = size, mtime, lines, fullname
92     return lines

 

以上是关于python linecache source code的主要内容,如果未能解决你的问题,请参考以下文章

python linecache模块 读取文件行使用注意事项

python linecache模块 读取文件行使用注意事项

Python linecache模块读取文件

linecache

part12:Python 文件I/O(pathlib模块:PurePathPath,os.path,fnmatch,open,with,linecache,os模块操作文件和目录,tempfile(

安装 debugger-linecache (1.1.1) 时出错,Bundler 无法继续