[pypy-svn] r70138 - in pypy/branch/import-builtin/pypy/module/imp: . test
afa at codespeak.net
afa at codespeak.net
Tue Dec 15 19:18:24 CET 2009
Author: afa
Date: Tue Dec 15 19:18:24 2009
New Revision: 70138
Modified:
pypy/branch/import-builtin/pypy/module/imp/importing.py
pypy/branch/import-builtin/pypy/module/imp/interp_imp.py
pypy/branch/import-builtin/pypy/module/imp/test/test_import.py
Log:
- change imp.find_module, so that it finds lone .pyc files even when the normal import does not.
- A failing reload() should keep the old module in sys.modules
Modified: pypy/branch/import-builtin/pypy/module/imp/importing.py
==============================================================================
--- pypy/branch/import-builtin/pypy/module/imp/importing.py (original)
+++ pypy/branch/import-builtin/pypy/module/imp/importing.py Tue Dec 15 19:18:24 2009
@@ -25,7 +25,7 @@
# PY_CODERESOURCE = 8
IMP_HOOK = 9
-def find_modtype(space, filepart):
+def find_modtype(space, filepart, force_lonepycfiles=False):
"""Check which kind of module to import for the given filepart,
which is a path without extension. Returns PY_SOURCE, PY_COMPILED or
SEARCH_ERROR.
@@ -38,7 +38,9 @@
# The .py file does not exist. By default on PyPy, lonepycfiles
# is False: if a .py file does not exist, we don't even try to
# look for a lone .pyc file.
- if not space.config.objspace.lonepycfiles:
+ # The "imp" module does not respect this, and is allowed to find
+ # lone .pyc files.
+ if not space.config.objspace.lonepycfiles and not force_lonepycfiles:
return SEARCH_ERROR, None, None
# check the .pyc file
@@ -258,7 +260,8 @@
def fromLoader(w_loader):
return FindInfo(IMP_HOOK, '', None, w_loader=w_loader)
-def find_module(space, modulename, w_modulename, partname, w_path, use_loader=True):
+def find_module(space, modulename, w_modulename, partname, w_path,
+ use_loader=True, force_lonepycfiles=False):
# Examin importhooks (PEP302) before doing the import
if use_loader:
w_loader = find_in_meta_path(space, w_modulename, w_path)
@@ -289,14 +292,16 @@
filepart = os.path.join(path, partname)
if os.path.isdir(filepart) and case_ok(filepart):
initfile = os.path.join(filepart, '__init__')
- modtype, _, _ = find_modtype(space, initfile)
+ modtype, _, _ = find_modtype(space, initfile,
+ force_lonepycfiles)
if modtype in (PY_SOURCE, PY_COMPILED):
return FindInfo(PKG_DIRECTORY, filepart, None)
else:
msg = "Not importing directory " +\
"'%s' missing __init__.py" % (filepart,)
space.warn(msg, space.w_ImportWarning)
- modtype, suffix, filemode = find_modtype(space, filepart)
+ modtype, suffix, filemode = find_modtype(space, filepart,
+ force_lonepycfiles)
try:
if modtype in (PY_SOURCE, PY_COMPILED):
filename = filepart + suffix
@@ -442,6 +447,11 @@
try:
return load_module(space, w_modulename, find_info, reuse=True)
+ except:
+ # load_module probably removed name from modules because of
+ # the error. Put back the original module object.
+ space.sys.setmodule(w_module)
+ raise
finally:
if find_info.stream:
find_info.stream.close()
Modified: pypy/branch/import-builtin/pypy/module/imp/interp_imp.py
==============================================================================
--- pypy/branch/import-builtin/pypy/module/imp/interp_imp.py (original)
+++ pypy/branch/import-builtin/pypy/module/imp/interp_imp.py Tue Dec 15 19:18:24 2009
@@ -36,7 +36,9 @@
w_path = None
find_info = importing.find_module(
- space, name, w_name, name, w_path, use_loader=False)
+ space, name, w_name, name, w_path,
+ use_loader=False,
+ force_lonepycfiles=True)
if not find_info:
raise OperationError(
space.w_ImportError,
Modified: pypy/branch/import-builtin/pypy/module/imp/test/test_import.py
==============================================================================
--- pypy/branch/import-builtin/pypy/module/imp/test/test_import.py (original)
+++ pypy/branch/import-builtin/pypy/module/imp/test/test_import.py Tue Dec 15 19:18:24 2009
@@ -412,6 +412,21 @@
import os
os.unlink(test_reload.__file__)
+ def test_reload_failing(self):
+ import test_reload
+ import time
+ time.sleep(1)
+ f = open(test_reload.__file__, "w")
+ f.write("a = 10 // 0\n")
+ f.close()
+
+ # A failing reload should leave the previous module in sys.modules
+ raises(ZeroDivisionError, reload, test_reload)
+ import os, sys
+ assert 'test_reload' in sys.modules
+ assert test_reload.test
+ os.unlink(test_reload.__file__)
+
def test_reload_submodule(self):
import pkg.a
reload(pkg.a)
@@ -857,22 +872,19 @@
sys.meta_path.append(i)
sys.path_hooks.append(ImpWrapper)
sys.path_importer_cache.clear()
- mnames = ("colorsys", "urlparse", "distutils.core", "compiler.misc")
- for mname in mnames:
- parent = mname.split(".")[0]
- for n in sys.modules.keys():
- if n.startswith(parent):
- del sys.modules[n]
- for mname in mnames:
- m = __import__(mname, globals(), locals(), ["__dummy__"])
- m.__loader__ # to make sure we actually handled the import
- # Delete urllib from modules because urlparse was imported above.
- # Without this hack, test_socket_ssl fails if run in this order:
- # regrtest.py test_codecmaps_tw test_importhooks test_socket_ssl
try:
- del sys.modules['urllib']
- except KeyError:
- pass
+ mnames = ("colorsys", "urlparse", "distutils.core", "compiler.misc")
+ for mname in mnames:
+ parent = mname.split(".")[0]
+ for n in sys.modules.keys():
+ if n.startswith(parent):
+ del sys.modules[n]
+ for mname in mnames:
+ m = __import__(mname, globals(), locals(), ["__dummy__"])
+ m.__loader__ # to make sure we actually handled the import
+ finally:
+ sys.meta_path.pop(0)
+ sys.path_hooks.pop(0)
class AppTestNoPycFile(object):
usepycfiles = False
@@ -891,6 +903,9 @@
_teardown(cls.space, cls.saved_modules)
def test_import_possibly_from_pyc(self):
+ import sys
+ assert sys.meta_path == []
+ assert sys.path_hooks == []
from compiled import x
if self.usepycfiles:
assert x.__file__.endswith('x.pyc')
More information about the Pypy-commit
mailing list