FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Merged revisions 67750-67751 via svnmerge from · python/cpython@f088e5e · GitHub

Repository navigation

Commit f088e5e

Browse files
committed
Merged revisions 67750-67751 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r67750 | nick.coghlan | 2008-12-14 20:54:50 +1000 (Sun, 14 Dec 2008) | 1 line Fix several issues relating to access to source code inside zipfiles. Initial work by Alexander Belopolsky. See Misc/NEWS in this checkin for details. ........ r67751 | nick.coghlan | 2008-12-14 21:09:40 +1000 (Sun, 14 Dec 2008) | 1 line Add file that was missed from r67750 ........
1 parent 80a0c7f commit f088e5e

11 files changed

Lines changed: 360 additions & 48 deletions

File tree

‎Lib/bdb.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def format_stack_entry(self, frame_lineno, lprefix=': '):
346346
rv = frame.f_locals['__return__']
347347
s = s + '->'
348348
s = s + reprlib.repr(rv)
349-
line = linecache.getline(filename, lineno)
349+
line = linecache.getline(filename, lineno, frame.f_globals)
350350
if line: s = s + lprefix + line.strip()
351351
return s
352352

@@ -588,7 +588,7 @@ def user_line(self, frame):
588588
name = frame.f_code.co_name
589589
if not name: name = '???'
590590
fn = self.canonic(frame.f_code.co_filename)
591-
line = linecache.getline(fn, frame.f_lineno)
591+
line = linecache.getline(fn, frame.f_lineno, frame.f_globals)
592592
print('+++', fn, frame.f_lineno, name, ':', line.strip())
593593
def user_return(self, frame, retval):
594594
print('+++ return', retval)

‎Lib/linecache.py‎

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,20 @@ def updatecache(filename, module_globals=None):
8989
get_source = getattr(loader, 'get_source', None)
9090

9191
if name and get_source:
92-
if basename.startswith(name.split('.')[-1]+'.'):
93-
try:
94-
data = get_source(name)
95-
except (ImportError, IOError):
96-
pass
97-
else:
98-
if data is None:
99-
# No luck, the PEP302 loader cannot find the source
100-
# for this module.
101-
return []
102-
cache[filename] = (
103-
len(data), None,
104-
[line+'\n' for line in data.splitlines()], fullname
105-
)
106-
return cache[filename][2]
92+
try:
93+
data = get_source(name)
94+
except (ImportError, IOError):
95+
pass
96+
else:
97+
if data is None:
98+
# No luck, the PEP302 loader cannot find the source
99+
# for this module.
100+
return []
101+
cache[filename] = (
102+
len(data), None,
103+
[line+'\n' for line in data.splitlines()], fullname
104+
)
105+
return cache[filename][2]
107106

108107
# Try looking through the module search path.
109108

‎Lib/pdb.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ def checkline(self, filename, lineno):
438438
Return `lineno` if it is, 0 if not (e.g. a docstring, comment, blank
439439
line or EOF). Warning: testing is not comprehensive.
440440
"""
441-
line = linecache.getline(filename, lineno)
441+
line = linecache.getline(filename, lineno, self.curframe.f_globals)
442442
if not line:
443443
print('End of file', file=self.stdout)
444444
return 0
@@ -768,7 +768,7 @@ def do_list(self, arg):
768768
breaklist = self.get_file_breaks(filename)
769769
try:
770770
for lineno in range(first, last+1):
771-
line = linecache.getline(filename, lineno)
771+
line = linecache.getline(filename, lineno, self.curframe.f_globals)
772772
if not line:
773773
print('[EOF]', file=self.stdout)
774774
break

‎Lib/runpy.py‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,14 @@ def _run_module_code(code, init_globals=None,
6565

6666
# This helper is needed due to a missing component in the PEP 302
6767
# loader protocol (specifically, "get_filename" is non-standard)
68+
# Since we can't introduce new features in maintenance releases,
69+
# support was added to zipimporter under the name '_get_filename'
6870
def _get_filename(loader, mod_name):
69-
try:
70-
get_filename = loader.get_filename
71-
except AttributeError:
72-
return None
73-
else:
74-
return get_filename(mod_name)
71+
for attr in ("get_filename", "_get_filename"):
72+
meth = getattr(loader, attr, None)
73+
if meth is not None:
74+
return meth(mod_name)
75+
return None
7576

7677
# Helper to get the loader, code and filename for a module
7778
def _get_module_details(mod_name):

‎Lib/test/test_cmd_line_script.py‎

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,36 +75,66 @@ def _compile_test_script(script_name):
7575
compiled_name = script_name + 'o'
7676
return compiled_name
7777

78-
def _make_test_zip(zip_dir, zip_basename, script_name):
78+
def _make_test_zip(zip_dir, zip_basename, script_name, name_in_zip=None):
7979
zip_filename = zip_basename+os.path.extsep+"zip"
8080
zip_name = os.path.join(zip_dir, zip_filename)
8181
zip_file = zipfile.ZipFile(zip_name, 'w')
82-
zip_file.write(script_name, os.path.basename(script_name))
82+
if name_in_zip is None:
83+
name_in_zip = os.path.basename(script_name)
84+
zip_file.write(script_name, name_in_zip)
8385
zip_file.close()
84-
# if verbose:
86+
#if verbose:
8587
# zip_file = zipfile.ZipFile(zip_name, 'r')
8688
# print("Contents of %r:" % zip_name)
8789
# zip_file.printdir()
8890
# zip_file.close()
89-
return zip_name
91+
return zip_name, os.path.join(zip_name, name_in_zip)
9092

9193
def _make_test_pkg(pkg_dir):
9294
os.mkdir(pkg_dir)
9395
_make_test_script(pkg_dir, '__init__', '')
9496

97+
def _make_test_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
98+
source=test_source, depth=1):
99+
init_name = _make_test_script(zip_dir, '__init__', '')
100+
init_basename = os.path.basename(init_name)
101+
script_name = _make_test_script(zip_dir, script_basename, source)
102+
pkg_names = [os.sep.join([pkg_name]*i) for i in range(1, depth+1)]
103+
script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name))
104+
zip_filename = zip_basename+os.extsep+'zip'
105+
zip_name = os.path.join(zip_dir, zip_filename)
106+
zip_file = zipfile.ZipFile(zip_name, 'w')
107+
for name in pkg_names:
108+
init_name_in_zip = os.path.join(name, init_basename)
109+
zip_file.write(init_name, init_name_in_zip)
110+
zip_file.write(script_name, script_name_in_zip)
111+
zip_file.close()
112+
os.unlink(init_name)
113+
os.unlink(script_name)
114+
#if verbose:
115+
# zip_file = zipfile.ZipFile(zip_name, 'r')
116+
# print 'Contents of %r:' % zip_name
117+
# zip_file.printdir()
118+
# zip_file.close()
119+
return zip_name, os.path.join(zip_name, script_name_in_zip)
120+
95121
# There's no easy way to pass the script directory in to get
96122
# -m to work (avoiding that is the whole point of making
97123
# directories and zipfiles executable!)
98124
# So we fake it for testing purposes with a custom launch script
99125
launch_source = """\
100126
import sys, os.path, runpy
101-
sys.path[0:0] = os.path.dirname(__file__)
127+
sys.path.insert(0, %s)
102128
runpy._run_module_as_main(%r)
103129
"""
104130

105-
def _make_launch_script(script_dir, script_basename, module_name):
106-
return _make_test_script(script_dir, script_basename,
107-
launch_source % module_name)
131+
def _make_launch_script(script_dir, script_basename, module_name, path=None):
132+
if path is None:
133+
path = "os.path.dirname(__file__)"
134+
else:
135+
path = repr(path)
136+
source = launch_source % (path, module_name)
137+
return _make_test_script(script_dir, script_basename, source)
108138

109139
class CmdLineTest(unittest.TestCase):
110140
def _check_script(self, script_name, expected_file,
@@ -155,24 +185,35 @@ def test_directory_compiled(self):
155185
def test_zipfile(self):
156186
with temp_dir() as script_dir:
157187
script_name = _make_test_script(script_dir, '__main__')
158-
zip_name = _make_test_zip(script_dir, 'test_zip', script_name)
159-
self._check_script(zip_name, None, zip_name, '')
188+
zip_name, run_name = _make_test_zip(script_dir, 'test_zip', script_name)
189+
self._check_script(zip_name, run_name, zip_name, '')
160190

161191
def test_zipfile_compiled(self):
162192
with temp_dir() as script_dir:
163193
script_name = _make_test_script(script_dir, '__main__')
164194
compiled_name = _compile_test_script(script_name)
165-
zip_name = _make_test_zip(script_dir, 'test_zip', compiled_name)
166-
self._check_script(zip_name, None, zip_name, '')
195+
zip_name, run_name = _make_test_zip(script_dir, 'test_zip', compiled_name)
196+
self._check_script(zip_name, run_name, zip_name, '')
167197

168198
def test_module_in_package(self):
169199
with temp_dir() as script_dir:
170200
pkg_dir = os.path.join(script_dir, 'test_pkg')
171201
_make_test_pkg(pkg_dir)
172202
script_name = _make_test_script(pkg_dir, 'script')
173203
launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.script')
174-
self._check_script(launch_name, script_name,
175-
script_name, 'test_pkg')
204+
self._check_script(launch_name, script_name, script_name, 'test_pkg')
205+
206+
def test_module_in_package_in_zipfile(self):
207+
with temp_dir() as script_dir:
208+
zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script')
209+
launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.script', zip_name)
210+
self._check_script(launch_name, run_name, run_name, 'test_pkg')
211+
212+
def test_module_in_subpackage_in_zipfile(self):
213+
with temp_dir() as script_dir:
214+
zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script', depth=2)
215+
launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.test_pkg.script', zip_name)
216+
self._check_script(launch_name, run_name, run_name, 'test_pkg.test_pkg')
176217

177218

178219
def test_main():

‎Lib/test/test_doctest.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import doctest
77
import warnings
88

9+
# NOTE: There are some additional tests relating to interaction with
10+
# zipimport in the test_zipimport_support test module.
11+
912
######################################################################
1013
## Sample Objects (used by test cases)
1114
######################################################################
@@ -369,7 +372,7 @@ def test_DocTestFinder(): r"""
369372
>>> tests = finder.find(sample_func)
370373
371374
>>> print(tests) # doctest: +ELLIPSIS
372-
[<DocTest sample_func from ...:13 (1 example)>]
375+
[<DocTest sample_func from ...:16 (1 example)>]
373376
374377
The exact name depends on how test_doctest was invoked, so allow for
375378
leading path components.

‎Lib/test/test_inspect.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
# getclasstree, getargspec, getargvalues, formatargspec, formatargvalues,
1919
# currentframe, stack, trace, isdatadescriptor
2020

21+
# NOTE: There are some additional tests relating to interaction with
22+
# zipimport in the test_zipimport_support test module.
23+
2124
modfile = mod.__file__
2225
if modfile.endswith(('c', 'o')):
2326
modfile = modfile[:-1]

‎Lib/test/test_zipimport.py‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,24 @@ def testZipImporterMethods(self):
211211
zi = zipimport.zipimporter(TEMP_ZIP)
212212
self.assertEquals(zi.archive, TEMP_ZIP)
213213
self.assertEquals(zi.is_package(TESTPACK), True)
214-
zi.load_module(TESTPACK)
214+
mod = zi.load_module(TESTPACK)
215+
self.assertEquals(zi._get_filename(TESTPACK), mod.__file__)
215216

216217
self.assertEquals(zi.is_package(packdir + '__init__'), False)
217218
self.assertEquals(zi.is_package(packdir + TESTPACK2), True)
218219
self.assertEquals(zi.is_package(packdir2 + TESTMOD), False)
219220

220-
mod_name = packdir2 + TESTMOD
221-
mod = __import__(module_path_to_dotted_name(mod_name))
221+
mod_path = packdir2 + TESTMOD
222+
mod_name = module_path_to_dotted_name(mod_path)
223+
pkg = __import__(mod_name)
224+
mod = sys.modules[mod_name]
222225
self.assertEquals(zi.get_source(TESTPACK), None)
223-
self.assertEquals(zi.get_source(mod_name), None)
226+
self.assertEquals(zi.get_source(mod_path), None)
227+
self.assertEquals(zi._get_filename(mod_path), mod.__file__)
228+
# To pass in the module name instead of the path, we must use the right importer
229+
loader = mod.__loader__
230+
self.assertEquals(loader.get_source(mod_name), None)
231+
self.assertEquals(loader._get_filename(mod_name), mod.__file__)
224232

225233
# test prefix and archivepath members
226234
zi2 = zipimport.zipimporter(TEMP_ZIP + os.sep + TESTPACK)
@@ -248,15 +256,23 @@ def testZipImporterMethodsInSubDirectory(self):
248256
self.assertEquals(zi.archive, TEMP_ZIP)
249257
self.assertEquals(zi.prefix, packdir)
250258
self.assertEquals(zi.is_package(TESTPACK2), True)
251-
zi.load_module(TESTPACK2)
259+
mod = zi.load_module(TESTPACK2)
260+
self.assertEquals(zi._get_filename(TESTPACK2), mod.__file__)
252261

253262
self.assertEquals(zi.is_package(TESTPACK2 + os.sep + '__init__'), False)
254263
self.assertEquals(zi.is_package(TESTPACK2 + os.sep + TESTMOD), False)
255264

256-
mod_name = TESTPACK2 + os.sep + TESTMOD
257-
mod = __import__(module_path_to_dotted_name(mod_name))
265+
mod_path = TESTPACK2 + os.sep + TESTMOD
266+
mod_name = module_path_to_dotted_name(mod_path)
267+
pkg = __import__(mod_name)
268+
mod = sys.modules[mod_name]
258269
self.assertEquals(zi.get_source(TESTPACK2), None)
259-
self.assertEquals(zi.get_source(mod_name), None)
270+
self.assertEquals(zi.get_source(mod_path), None)
271+
self.assertEquals(zi._get_filename(mod_path), mod.__file__)
272+
# To pass in the module name instead of the path, we must use the right importer
273+
loader = mod.__loader__
274+
self.assertEquals(loader.get_source(mod_name), None)
275+
self.assertEquals(loader._get_filename(mod_name), mod.__file__)
260276
finally:
261277
z.close()
262278
os.remove(TEMP_ZIP)

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL