annotate Lib/linecache.py @ 48416:fb02fedfe647

Merged revisions 64677 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r64677 | brett.cannon | 2008-07-02 14:52:42 -0700 (Wed, 02 Jul 2008) | 2 lines Revert r64673 and instead just change the file encoding. ........
author Brett Cannon <bcannon@gmail.com>
date Wed, 02 Jul 2008 22:06:12 +0000
parents 11407c1f68ca
children 252927a2f32d 3e1c449d11e0
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
41665
a3248fefc0dc Support PEP-263-style coding declarations.
Guido van Rossum <guido@python.org>
parents: 40948
diff changeset
10 import re
566
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
29680
4e1a47f5204c Whitespace normalization.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23625
diff changeset
12 __all__ = ["getline", "clearcache", "checkcache"]
16198
32755b456369 a few more modules get __all__
Skip Montanaro <skip@pobox.com>
parents: 15543
diff changeset
13
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
14 def getline(filename, lineno, module_globals=None):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
15 lines = getlines(filename, module_globals)
11706
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
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
34 def getlines(filename, module_globals=None):
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
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 23029
diff changeset
38 if filename in cache:
11706
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:
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
41 return updatecache(filename, module_globals)
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
33839
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 29680
diff changeset
44 def checkcache(filename=None):
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
33839
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 29680
diff changeset
48 if filename is None:
40948
3093ea9b4024 Fix another loop over a dict that may change... :-(
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
49 filenames = list(cache.keys())
33839
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 29680
diff changeset
50 else:
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 29680
diff changeset
51 if filename in cache:
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 29680
diff changeset
52 filenames = [filename]
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 29680
diff changeset
53 else:
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 29680
diff changeset
54 return
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 29680
diff changeset
55
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 29680
diff changeset
56 for filename in filenames:
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
57 size, mtime, lines, fullname = cache[filename]
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
58 if mtime is None:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
59 continue # no-op for files loaded via a __loader__
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
60 try:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
61 stat = os.stat(fullname)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
62 except os.error:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
63 del cache[filename]
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
64 continue
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 23620
diff changeset
65 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
66 del cache[filename]
566
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
69 def updatecache(filename, module_globals=None):
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
70 """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
71 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
72 and return an empty list."""
566
5a1f9ade1512 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 23029
diff changeset
74 if filename in cache:
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
75 del cache[filename]
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
76 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
77 return []
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
78
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
79 fullname = filename
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
80 try:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
81 stat = os.stat(fullname)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
82 except os.error as msg:
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
83 basename = os.path.split(filename)[1]
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
84
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
85 # Try for a __loader__, if available
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
86 if module_globals and '__loader__' in module_globals:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
87 name = module_globals.get('__name__')
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
88 loader = module_globals['__loader__']
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
89 get_source = getattr(loader, 'get_source', None)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
90
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
91 if name and get_source:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
92 if basename.startswith(name.split('.')[-1]+'.'):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
93 try:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
94 data = get_source(name)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
95 except (ImportError, IOError):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
96 pass
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
97 else:
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
98 if data is None:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
99 # No luck, the PEP302 loader cannot find the source
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
100 # for this module.
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
101 return []
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
102 cache[filename] = (
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
103 len(data), None,
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
104 [line+'\n' for line in data.splitlines()], fullname
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
105 )
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
106 return cache[filename][2]
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
107
17977
2a94bf235daa Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents: 16198
diff changeset
108 # Try looking through the module search path.
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 33839
diff changeset
109
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
110 for dirname in sys.path:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
111 try:
17977
2a94bf235daa Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents: 16198
diff changeset
112 fullname = os.path.join(dirname, basename)
2a94bf235daa Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents: 16198
diff changeset
113 except (TypeError, AttributeError):
2a94bf235daa Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents: 16198
diff changeset
114 # 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
115 pass
17977
2a94bf235daa Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents: 16198
diff changeset
116 else:
2a94bf235daa Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents: 16198
diff changeset
117 try:
2a94bf235daa Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents: 16198
diff changeset
118 stat = os.stat(fullname)
2a94bf235daa Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents: 16198
diff changeset
119 break
2a94bf235daa Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents: 16198
diff changeset
120 except os.error:
2a94bf235daa Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents: 16198
diff changeset
121 pass
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
122 else:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
123 # No luck
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
124 ## 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
125 return []
42173
91ade67ab230 Be even more lenient when catching errors from readlines().
Guido van Rossum <guido@python.org>
parents: 41811
diff changeset
126 ## print("Refreshing cache for %s..." % fullname)
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
127 try:
23029
8291473fde3e Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents: 17977
diff changeset
128 fp = open(fullname, 'rU')
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
129 lines = fp.readlines()
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
130 fp.close()
42173
91ade67ab230 Be even more lenient when catching errors from readlines().
Guido van Rossum <guido@python.org>
parents: 41811
diff changeset
131 except Exception as msg:
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
132 ## 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
133 return []
41665
a3248fefc0dc Support PEP-263-style coding declarations.
Guido van Rossum <guido@python.org>
parents: 40948
diff changeset
134 coding = "utf-8"
a3248fefc0dc Support PEP-263-style coding declarations.
Guido van Rossum <guido@python.org>
parents: 40948
diff changeset
135 for line in lines[:2]:
a3248fefc0dc Support PEP-263-style coding declarations.
Guido van Rossum <guido@python.org>
parents: 40948
diff changeset
136 m = re.search(r"coding[:=]\s*([-\w.]+)", line)
a3248fefc0dc Support PEP-263-style coding declarations.
Guido van Rossum <guido@python.org>
parents: 40948
diff changeset
137 if m:
a3248fefc0dc Support PEP-263-style coding declarations.
Guido van Rossum <guido@python.org>
parents: 40948
diff changeset
138 coding = m.group(1)
a3248fefc0dc Support PEP-263-style coding declarations.
Guido van Rossum <guido@python.org>
parents: 40948
diff changeset
139 break
a3248fefc0dc Support PEP-263-style coding declarations.
Guido van Rossum <guido@python.org>
parents: 40948
diff changeset
140 try:
41811
d07d34b53db5 linecache.py was still struggling with unicode vs. non-unicode.
Guido van Rossum <guido@python.org>
parents: 41665
diff changeset
141 lines = [line if isinstance(line, str) else str(line, coding)
d07d34b53db5 linecache.py was still struggling with unicode vs. non-unicode.
Guido van Rossum <guido@python.org>
parents: 41665
diff changeset
142 for line in lines]
d07d34b53db5 linecache.py was still struggling with unicode vs. non-unicode.
Guido van Rossum <guido@python.org>
parents: 41665
diff changeset
143 except:
41665
a3248fefc0dc Support PEP-263-style coding declarations.
Guido van Rossum <guido@python.org>
parents: 40948
diff changeset
144 pass # Hope for the best
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 23620
diff changeset
145 size, mtime = stat.st_size, stat.st_mtime
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 2954
diff changeset
146 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
147 return lines