[pypy-svn] r61162 - in pypy/trunk/pypy/module/__builtin__: . test
antocuni at codespeak.net
antocuni at codespeak.net
Tue Jan 20 16:03:37 CET 2009
Author: antocuni
Date: Tue Jan 20 16:03:37 2009
New Revision: 61162
Modified:
pypy/trunk/pypy/module/__builtin__/importing.py
pypy/trunk/pypy/module/__builtin__/test/test_import.py
Log:
add a new test for imports, and simplify part of the importing logic.
Hopefully, this should not be less correct than the previous one :-)
Modified: pypy/trunk/pypy/module/__builtin__/importing.py
==============================================================================
--- pypy/trunk/pypy/module/__builtin__/importing.py (original)
+++ pypy/trunk/pypy/module/__builtin__/importing.py Tue Jan 20 16:03:37 2009
@@ -143,7 +143,6 @@
space.wrap("Empty module name"))
w = space.wrap
-
ctxt_name = None
if w_globals is not None and not space.is_w(w_globals, space.w_None):
ctxt_w_name = try_getitem(space, w_globals, w('__name__'))
@@ -156,7 +155,7 @@
raise
else:
ctxt_w_path = None
-
+
rel_modulename = None
if ctxt_name is not None:
if level == 0:
@@ -164,22 +163,18 @@
rel_modulename = modulename
else:
ctxt_name_prefix_parts = ctxt_name.split('.')
- if ctxt_w_path is None: # context is a plain module
- if level < 0:
- ctxt_name_prefix_parts = ctxt_name_prefix_parts[:-1]
- else:
- cnpp = ctxt_name_prefix_parts
- ctxt_name_prefix_parts = [ ctxt_name_prefix_parts[i]
- for i in range(len(cnpp)-level) ]
- if ctxt_name_prefix_parts:
- rel_modulename = '.'.join(ctxt_name_prefix_parts)
- if modulename:
- rel_modulename += '.' + modulename
- else: # context is a package module
- rel_modulename = ctxt_name
+ if level > 0:
+ n = len(ctxt_name_prefix_parts)-level+1
+ assert n>=0
+ ctxt_name_prefix_parts = ctxt_name_prefix_parts[:n]
+ if ctxt_w_path is None: # plain module
+ ctxt_name_prefix_parts.pop()
+ if ctxt_name_prefix_parts:
+ rel_modulename = '.'.join(ctxt_name_prefix_parts)
if modulename:
rel_modulename += '.' + modulename
baselevel = len(ctxt_name_prefix_parts)
+
if rel_modulename is not None:
w_mod = check_sys_modules(space, w(rel_modulename))
if (w_mod is None or
Modified: pypy/trunk/pypy/module/__builtin__/test/test_import.py
==============================================================================
--- pypy/trunk/pypy/module/__builtin__/test/test_import.py (original)
+++ pypy/trunk/pypy/module/__builtin__/test/test_import.py Tue Jan 20 16:03:37 2009
@@ -327,6 +327,15 @@
res = __import__('', mydict, mydict, ('bar',), 1)
assert res is foo
+ def test_relative_import_pkg(self):
+ import sys
+ import imp
+ pkg = imp.new_module('pkg')
+ sys.modules['pkg'] = pkg
+ mydict = {'__name__': 'pkg.foo', '__path__': '/some/path'}
+ res = __import__('', mydict, None, ['bar'], 2)
+ assert res is pkg
+
def test_universal_newlines(self):
import pkg_univnewlines
assert pkg_univnewlines.a == 5
More information about the Pypy-commit
mailing list