Mercurial > cpython-lru_cache-weakref
annotate Lib/linecache.py @ 51749:1408feec0084 3.0
Merged revisions 69798 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
................
r69798 | benjamin.peterson | 2009-02-19 22:02:38 -0600 (Thu, 19 Feb 2009) | 13 lines
Merged revisions 69769,69776 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r69769 | georg.brandl | 2009-02-19 02:30:06 -0600 (Thu, 19 Feb 2009) | 1 line
#5310, #3558: fix operator precedence table.
........
r69776 | georg.brandl | 2009-02-19 10:34:51 -0600 (Thu, 19 Feb 2009) | 2 lines
#5317: update IronPython URL.
........
................
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Fri, 20 Feb 2009 04:05:32 +0000 |
| parents | 252927a2f32d |
| children |
| 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 | 7 |
|
722
57e94eba2484
Search through the module search path.
Guido van Rossum <guido@python.org>
parents:
566
diff
changeset
|
8 import sys |
| 566 | 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 | 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 | 20 |
| 21 | |
| 22 # The cache | |
| 23 | |
| 24 cache = {} # The cache | |
| 25 | |
| 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 | 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 | 32 |
| 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 | 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 | 42 |
| 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 | 67 |
| 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 | 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: |
|
50164
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46747
diff
changeset
|
92 try: |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46747
diff
changeset
|
93 data = get_source(name) |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46747
diff
changeset
|
94 except (ImportError, IOError): |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46747
diff
changeset
|
95 pass |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46747
diff
changeset
|
96 else: |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46747
diff
changeset
|
97 if data is None: |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46747
diff
changeset
|
98 # No luck, the PEP302 loader cannot find the source |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46747
diff
changeset
|
99 # for this module. |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46747
diff
changeset
|
100 return [] |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46747
diff
changeset
|
101 cache[filename] = ( |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46747
diff
changeset
|
102 len(data), None, |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46747
diff
changeset
|
103 [line+'\n' for line in data.splitlines()], fullname |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46747
diff
changeset
|
104 ) |
|
252927a2f32d
Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46747
diff
changeset
|
105 return cache[filename][2] |
|
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
|
106 |
|
17977
2a94bf235daa
Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents:
16198
diff
changeset
|
107 # 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
|
108 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
109 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
|
110 try: |
|
17977
2a94bf235daa
Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents:
16198
diff
changeset
|
111 fullname = os.path.join(dirname, basename) |
|
2a94bf235daa
Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents:
16198
diff
changeset
|
112 except (TypeError, AttributeError): |
|
2a94bf235daa
Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents:
16198
diff
changeset
|
113 # 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
|
114 pass |
|
17977
2a94bf235daa
Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents:
16198
diff
changeset
|
115 else: |
|
2a94bf235daa
Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents:
16198
diff
changeset
|
116 try: |
|
2a94bf235daa
Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents:
16198
diff
changeset
|
117 stat = os.stat(fullname) |
|
2a94bf235daa
Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents:
16198
diff
changeset
|
118 break |
|
2a94bf235daa
Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents:
16198
diff
changeset
|
119 except os.error: |
|
2a94bf235daa
Patch from Gordon McMillan.
Tim Peters <tim.peters@gmail.com>
parents:
16198
diff
changeset
|
120 pass |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
121 else: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
122 # No luck |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
123 ## 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
|
124 return [] |
|
42173
91ade67ab230
Be even more lenient when catching errors from readlines().
Guido van Rossum <guido@python.org>
parents:
41811
diff
changeset
|
125 ## 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
|
126 try: |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
17977
diff
changeset
|
127 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
|
128 lines = fp.readlines() |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
2954
diff
changeset
|
129 fp.close() |
|
42173
91ade67ab230
Be even more lenient when catching errors from readlines().
Guido van Rossum <guido@python.org>
parents:
41811
diff
changeset
|
130 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
|
131 ## 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
|
132 return [] |
|
41665
a3248fefc0dc
Support PEP-263-style coding declarations.
Guido van Rossum <guido@python.org>
parents:
40948
diff
changeset
|
133 coding = "utf-8" |
|
a3248fefc0dc
Support PEP-263-style coding declarations.
Guido van Rossum <guido@python.org>
parents:
40948
diff
changeset
|
134 for line in lines[:2]: |
|
a3248fefc0dc
Support PEP-263-style coding declarations.
Guido van Rossum <guido@python.org>
parents:
40948
diff
changeset
|
135 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
|
136 if m: |
|
a3248fefc0dc
Support PEP-263-style coding declarations.
Guido van Rossum <guido@python.org>
parents:
40948
diff
changeset
|
137 coding = m.group(1) |
|
a3248fefc0dc
Support PEP-263-style coding declarations.
Guido van Rossum <guido@python.org>
parents:
40948
diff
changeset
|
138 break |
|
a3248fefc0dc
Support PEP-263-style coding declarations.
Guido van Rossum <guido@python.org>
parents:
40948
diff
changeset
|
139 try: |
|
41811
d07d34b53db5
linecache.py was still struggling with unicode vs. non-unicode.
Guido van Rossum <guido@python.org>
parents:
41665
diff
changeset
|
140 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
|
141 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
|
142 except: |
|
41665
a3248fefc0dc
Support PEP-263-style coding declarations.
Guido van Rossum <guido@python.org>
parents:
40948
diff
changeset
|
143 pass # Hope for the best |
|
23625
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
144 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
|
145 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
|
146 return lines |
