Skip to content

Commit a4ef203

Browse files
committed
small internal refinement to AliasModules - only useful when directly calling
AliasModule(...) for now.
1 parent 7604716 commit a4ef203

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

apipkg.py

Lines changed: 10 additions & 4 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.dev5'
12+
__version__ = '1.2.dev6'
1313

1414
def initpkg(pkgname, exportdefs, attr=dict()):
1515
""" initialize given package from the export definitions. """
@@ -136,18 +136,24 @@ def __dict__(self):
136136
__dict__ = property(__dict__)
137137

138138

139-
def AliasModule(modname, modpath):
139+
def AliasModule(modname, modpath, attrname=None):
140140
mod = []
141141

142142
def getmod():
143143
if not mod:
144-
mod.append(importobj(modpath, None))
144+
x = importobj(modpath, None)
145+
if attrname is not None:
146+
x = getattr(x, attrname)
147+
mod.append(x)
145148
return mod[0]
146149

147150
class AliasModule(ModuleType):
148151

149152
def __repr__(self):
150-
return '<AliasModule %r for %r>' % (modname, modpath)
153+
x = modpath
154+
if attrname:
155+
x += "." + attrname
156+
return '<AliasModule %r for %r>' % (modname, x)
151157

152158
def __getattribute__(self, name):
153159
return getattr(getmod(), name)

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.dev5',
22+
version='1.2.dev6',
2323
url='http://bitbucket.org/hpk42/apipkg',
2424
license='MIT License',
2525
platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],

test_apipkg.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,12 @@ def test_extra_attributes(tmpdir, monkeypatch):
402402
import extra_attributes
403403
assert extra_attributes.foo == 'bar'
404404

405+
def test_aliasmodule_aliases_an_attribute():
406+
am = apipkg.AliasModule("mymod", "pprint", 'PrettyPrinter')
407+
r = repr(am)
408+
assert "<AliasModule 'mymod' for 'pprint.PrettyPrinter'>" == r
409+
assert am.format
410+
405411
def test_aliasmodule_repr():
406412
am = apipkg.AliasModule("mymod", "sys")
407413
r = repr(am)

0 commit comments

Comments
 (0)