annotate Lib/linecache.py @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents 2a94bf235daa
children 8291473fde3e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
1 """Cache lines from files.
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
2
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
3 This is intended to read lines from modules imported -- hence if a filename
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
4 is not found, it will look down the module search path for a file by
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
5 that name.
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
6 """
566
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
722
57e94eba2484 Search through the module search path.
Guido van Rossum <guido@python.org>
parents: 566
diff changeset
8 import sys
566
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 import os
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 from stat import *
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
16198
32755b456369 a few more modules get __all__
Skip Montanaro <skip@pobox.com>
parents: 15543
diff changeset
12 __all__ = ["getline","clearcache","checkcache"]
32755b456369 a few more modules get __all__
Skip Montanaro <skip@pobox.com>
parents: 15543
diff changeset
13
566
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 def getline(filename, lineno):
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
15 lines = getlines(filename)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
16 if 1 <= lineno <= len(lines):
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
17 return lines[lineno-1]
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
18 else:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
19 return ''
566
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
20
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 # The cache
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 cache = {} # The cache
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
27 def clearcache():
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
28 """Clear the cache entirely."""
566
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
30 global cache
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
31 cache = {}
566
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 def getlines(filename):
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
35 """Get the lines for a file from the cache.
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
36 Update the cache if it doesn't contain an entry for this file already."""
566
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
38 if cache.has_key(filename):
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
39 return cache[filename][2]
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
40 else:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
41 return updatecache(filename)
566
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 def checkcache():
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
45 """Discard cache entries that are out of date.
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
46 (This is not checked upon each call!)"""
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
47
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
48 for filename in cache.keys():
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
49 size, mtime, lines, fullname = cache[filename]
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
50 try:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
51 stat = os.stat(fullname)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
52 except os.error:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
53 del cache[filename]
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
54 continue
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 11706
diff changeset
55 if size != stat[ST_SIZE] or mtime != stat[ST_MTIME]:
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
56 del cache[filename]
566
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
59 def updatecache(filename):
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
60 """Update a cache entry and return its list of lines.
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
61 If something's wrong, print a message, discard the cache entry,
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
62 and return an empty list."""
566
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
64 if cache.has_key(filename):
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
65 del cache[filename]
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
66 if not filename or filename[0] + filename[-1] == '<>':
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
67 return []
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
68 fullname = filename
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
69 try:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
70 stat = os.stat(fullname)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
71 except os.error, msg:
17977
2a94bf235daa Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents: 16198
diff changeset
72 # Try looking through the module search path.
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
73 basename = os.path.split(filename)[1]
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
74 for dirname in sys.path:
17977
2a94bf235daa Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents: 16198
diff changeset
75 # When using imputil, sys.path may contain things other than
2a94bf235daa Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents: 16198
diff changeset
76 # strings; ignore them when it happens.
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
77 try:
17977
2a94bf235daa Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents: 16198
diff changeset
78 fullname = os.path.join(dirname, basename)
2a94bf235daa Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents: 16198
diff changeset
79 except (TypeError, AttributeError):
2a94bf235daa Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents: 16198
diff changeset
80 # Not sufficiently string-like to do anything useful with.
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
81 pass
17977
2a94bf235daa Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents: 16198
diff changeset
82 else:
2a94bf235daa Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents: 16198
diff changeset
83 try:
2a94bf235daa Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents: 16198
diff changeset
84 stat = os.stat(fullname)
2a94bf235daa Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents: 16198
diff changeset
85 break
2a94bf235daa Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents: 16198
diff changeset
86 except os.error:
2a94bf235daa Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents: 16198
diff changeset
87 pass
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
88 else:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
89 # No luck
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
90 ## print '*** Cannot stat', filename, ':', msg
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
91 return []
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
92 try:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
93 fp = open(fullname, 'r')
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
94 lines = fp.readlines()
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
95 fp.close()
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
96 except IOError, msg:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
97 ## print '*** Cannot open', fullname, ':', msg
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
98 return []
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
99 size, mtime = stat[ST_SIZE], stat[ST_MTIME]
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
100 cache[filename] = size, mtime, lines, fullname
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
101 return lines