Skip to content

Commit c81efb9

Browse files
Merge pull request #7 from GlenWalker/master
Preserve __package__ attribute from original module
2 parents 9c7a93c + b7439bf commit c81efb9

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

src/apipkg/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ def initpkg(pkgname, exportdefs, attr=None, eager=False):
5151
d['__loader__'] = oldmod.__loader__
5252
if hasattr(oldmod, '__path__'):
5353
d['__path__'] = [_py_abspath(p) for p in oldmod.__path__]
54+
if hasattr(oldmod, '__package__'):
55+
d['__package__'] = oldmod.__package__
5456
if '__doc__' not in exportdefs and getattr(oldmod, '__doc__', None):
5557
d['__doc__'] = oldmod.__doc__
5658
d.update(attr)

test_apipkg.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ def test_initpkg_transfers_attrs(monkeypatch):
227227
mod.__version__ = 10
228228
mod.__file__ = "hello.py"
229229
mod.__loader__ = "loader"
230+
mod.__package__ = "package"
230231
mod.__doc__ = "this is the documentation"
231232
monkeypatch.setitem(sys.modules, 'hello', mod)
232233
apipkg.initpkg('hello', {})
@@ -235,6 +236,7 @@ def test_initpkg_transfers_attrs(monkeypatch):
235236
assert newmod.__file__ == py.path.local(mod.__file__)
236237
assert newmod.__version__ == mod.__version__
237238
assert newmod.__loader__ == mod.__loader__
239+
assert newmod.__package__ == mod.__package__
238240
assert newmod.__doc__ == mod.__doc__
239241

240242

@@ -261,12 +263,14 @@ def test_initpkg_not_transfers_not_existing_attrs(monkeypatch):
261263
mod = ModuleType('hello')
262264
mod.__file__ = "hello.py"
263265
assert not hasattr(mod, '__path__')
266+
assert not hasattr(mod, '__package__') or mod.__package__ is None
264267
monkeypatch.setitem(sys.modules, 'hello', mod)
265268
apipkg.initpkg('hello', {})
266269
newmod = sys.modules['hello']
267270
assert newmod != mod
268271
assert newmod.__file__ == py.path.local(mod.__file__)
269272
assert not hasattr(newmod, '__path__')
273+
assert not hasattr(newmod, '__package__') or mod.__package__ is None
270274

271275

272276
def test_initpkg_not_changing_jython_paths(monkeypatch):

0 commit comments

Comments
 (0)