Skip to content

Commit 7604716

Browse files
committed
test and refine for specifying dotted alias import names
1 parent 905b75e commit 7604716

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

apipkg.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import sys
1010
from types import ModuleType
1111

12-
__version__ = "1.2.dev1"
12+
__version__ = '1.2.dev5'
1313

1414
def initpkg(pkgname, exportdefs, attr=dict()):
1515
""" initialize given package from the export definitions. """
@@ -81,7 +81,8 @@ def __init__(self, name, importspec, implprefix=None, attr=None):
8181
subname = '%s.%s'%(self.__name__, name)
8282
apimod = AliasModule(subname, modpath)
8383
sys.modules[subname] = apimod
84-
setattr(self, name, apimod)
84+
if '.' not in name:
85+
setattr(self, name, apimod)
8586
else:
8687
self.__map__[name] = (modpath, attrname)
8788

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def main():
1919
description=
2020
'apipkg: namespace control and lazy-import mechanism',
2121
long_description = open('README.txt').read(),
22-
version='1.2.dev4',
22+
version='1.2.dev5',
2323
url='http://bitbucket.org/hpk42/apipkg',
2424
license='MIT License',
2525
platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],

test_apipkg.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,31 @@ def doit():
437437
proxy.doit = doit
438438
assert orig.doit is doit
439439

440+
def test_aliasmodule_nested_import_with_from(tmpdir, monkeypatch):
441+
import os
442+
pkgdir = tmpdir.mkdir("api1")
443+
pkgdir.ensure("__init__.py").write(py.std.textwrap.dedent("""
444+
import apipkg
445+
apipkg.initpkg(__name__, {
446+
'os2': 'api2',
447+
'os2.path': 'api2.path2',
448+
})
449+
"""))
450+
tmpdir.join("api2.py").write(py.std.textwrap.dedent("""
451+
import os, sys
452+
from os import path
453+
sys.modules['api2.path2'] = path
454+
x = 3
455+
"""))
456+
monkeypatch.syspath_prepend(tmpdir)
457+
from api1 import os2
458+
from api1.os2.path import abspath
459+
assert abspath == os.path.abspath
460+
# check that api1.os2 mirrors os.*
461+
assert os2.x == 3
462+
import api1
463+
assert 'os2.path' not in api1.__dict__
464+
440465

441466
def test_initpkg_without_old_module():
442467
apipkg.initpkg("initpkg_without_old_module",

0 commit comments

Comments
 (0)