码迷,mamicode.com
首页 > 编程语言 > 详细

python linecache source code

时间:2016-01-31 13:26:05      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/ch3cooh/p/python-linecache-source.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!