Author: Manuel Jacob <m...@manueljacob.de> Branch: py3.3 Changeset: r79230:6a89b5539f61 Date: 2015-08-25 23:41 +0200 http://bitbucket.org/pypy/pypy/changeset/6a89b5539f61/
Log: Add more tests for zip-importing an archive whose filename contain non-ASCII characters, and fix bugs. diff --git a/pypy/module/imp/importing.py b/pypy/module/imp/importing.py --- a/pypy/module/imp/importing.py +++ b/pypy/module/imp/importing.py @@ -350,7 +350,7 @@ start = len(filename) - 4 stop = len(filename) - 1 if not 0 <= start <= stop or filename[start:stop].lower() != ".py": - return space.wrap(filename) + return space.wrap_fsdecoded(filename) py = make_source_pathname(filename) if py is None: py = filename[:-1] @@ -360,8 +360,8 @@ pass else: if stat.S_ISREG(st.st_mode): - return space.wrap(py) - return space.wrap(filename) + return space.wrap_fsdecoded(py) + return space.wrap_fsdecoded(filename) def update_code_filenames(space, code_w, pathname, oldname=None): assert isinstance(code_w, PyCode) diff --git a/pypy/module/zipimport/interp_zipimport.py b/pypy/module/zipimport/interp_zipimport.py --- a/pypy/module/zipimport/interp_zipimport.py +++ b/pypy/module/zipimport/interp_zipimport.py @@ -146,7 +146,7 @@ def import_py_file(self, space, modname, filename, buf, pkgpath): w = space.wrap - w_mod = w(Module(space, w(modname))) + w_mod = w(Module(space, space.wrap_fsdecoded(modname))) real_name = self.filename + os.path.sep + self.corr_zname(filename) space.setattr(w_mod, w('__loader__'), space.wrap(self)) importing._prepare_module(space, w_mod, real_name, pkgpath) @@ -313,8 +313,8 @@ space, co_filename, source) return space.wrap(code_w) raise oefmt(get_error(space), - "Cannot find source or code for %s in %s", - filename, self.name) + "Cannot find source or code for %s in %R", + filename, space.wrap_fsdecoded(self.name)) @unwrap_spec(fullname='str0') def get_source(self, space, fullname): @@ -334,17 +334,19 @@ # We have the module, but no source. return space.w_None raise oefmt(get_error(space), - "Cannot find source for %s in %s", filename, self.name) + "Cannot find source for %s in %R", filename, + space.wrap_fsdecoded(self.name)) @unwrap_spec(fullname='str0') def get_filename(self, space, fullname): filename = self.make_filename(fullname) for _, is_package, ext in ENUMERATE_EXTS: if self.have_modulefile(space, filename + ext): - return space.wrap(self.filename + os.path.sep + - self.corr_zname(filename + ext)) + return space.wrap_fsdecoded(self.filename + os.path.sep + + self.corr_zname(filename + ext)) raise oefmt(get_error(space), - "Cannot find module %s in %s", filename, self.name) + "Cannot find module %s in %R", filename, + space.wrap_fsdecoded(self.name)) @unwrap_spec(fullname='str0') def is_package(self, space, fullname): @@ -353,11 +355,12 @@ if self.have_modulefile(space, filename + ext): return space.wrap(is_package) raise oefmt(get_error(space), - "Cannot find module %s in %s", filename, self.name) + "Cannot find module %s in %R", filename, + space.wrap_fsdecoded(self.name)) def getarchive(self, space): space = self.space - return space.wrap(self.filename) + return space.wrap_fsdecoded(self.filename) def descr_new_zipimporter(space, w_type, w_name): name = space.fsencode_w(w_name) @@ -378,8 +381,8 @@ ok = True break if not ok: - raise oefmt(get_error(space), "Did not find %s to be a valid zippath", - name) + raise oefmt(get_error(space), "Did not find %R to be a valid zippath", + w_name) try: w_result = zip_cache.get(filename) if w_result is None: @@ -391,7 +394,8 @@ try: zip_file = RZipFile(filename, 'r') except (BadZipfile, OSError): - raise oefmt(get_error(space), "%s seems not to be a zipfile", filename) + raise oefmt(get_error(space), "%R seems not to be a zipfile", + space.wrap_fsdecoded(filename)) except RZlibError, e: # in this case, CPython raises the direct exception coming # from the zlib module: let's to the same diff --git a/pypy/module/zipimport/test/test_zipimport.py b/pypy/module/zipimport/test/test_zipimport.py --- a/pypy/module/zipimport/test/test_zipimport.py +++ b/pypy/module/zipimport/test/test_zipimport.py @@ -1,3 +1,4 @@ +# -*- encoding: utf-8 -*- import inspect import os import time @@ -346,11 +347,48 @@ import zipimport assert sys.path_hooks.count(zipimport.zipimporter) == 1 - def test_unicode_filename(self): + def w__make_unicode_filename(self): + import os + head, tail = os.path.split(self.zipfile) + self.zipfile = head + os.path.sep + tail[:4] + '_ä' + tail[4:] + + def test_unicode_filename_notfound(self): import zipimport - raises(zipimport.ZipImportError, + raises(zipimport.ZipImportError, zipimport.zipimporter, 'caf\xe9') + def test_unicode_filename_invalid_zippath(self): + import zipimport + import os + self._make_unicode_filename() + os.mkdir(self.zipfile) + raises(zipimport.ZipImportError, + zipimport.zipimporter, self.zipfile) + + def test_unicode_filename_invalid_zip(self): + import zipimport + self._make_unicode_filename() + open(self.zipfile, 'wb').write(b'invalid zip') + raises(zipimport.ZipImportError, + zipimport.zipimporter, self.zipfile) + + def test_unicode_filename_existing(self): + import zipimport + self._make_unicode_filename() + self.writefile('ä.py', '3') + z = zipimport.zipimporter(self.zipfile) + assert not z.is_package('ä') + raises(ImportError, z.is_package, 'xx') + assert z.get_source('ä') == '3' + raises(ImportError, z.get_source, 'xx') + assert z.get_code('ä') + raises(ImportError, z.get_code, 'xx') + mod = z.load_module('ä') + assert z.get_filename('ä') == mod.__file__ + raises(ImportError, z.load_module, 'xx') + raises(ImportError, z.get_filename, 'xx') + assert z.archive == self.zipfile + def test_co_filename(self): self.writefile('mymodule.py', """ def get_co_filename(): _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit