Skip to content

Commit 4d840c3

Browse files
create a eagerloading option and support bpython
1 parent 9a3fac9 commit 4d840c3

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
- introduce apipkg.distribution_version(name) as helper to
1111
obtain the current version number of a package from install metadata
1212
its used by default with the package name
13+
- add an eagerloading option and eagerload automatically
14+
if bpython is used
1315

1416
1.2
1517
----------------------------------------

apipkg.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def distribution_version(name):
3838
__version__ = distribution_version(__name__)
3939

4040

41-
def initpkg(pkgname, exportdefs, attr=dict()):
41+
def initpkg(pkgname, exportdefs, attr=dict(), eager=False):
4242
""" initialize given package from the export definitions. """
4343
oldmod = sys.modules.get(pkgname)
4444
d = {}
@@ -59,6 +59,11 @@ def initpkg(pkgname, exportdefs, attr=dict()):
5959
oldmod.__dict__.update(d)
6060
mod = ApiModule(pkgname, exportdefs, implprefix=pkgname, attr=d)
6161
sys.modules[pkgname] = mod
62+
# eaerload in bypthon to avoid their monkeypatching breaking packages
63+
if 'bpython' in sys.modules or eager:
64+
for module in sys.modules.values():
65+
if isinstance(module, ApiModule):
66+
module.__dict__
6267

6368

6469
def importobj(modpath, attrname):

test_apipkg.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,3 +531,15 @@ def test_initpkg_without_old_module():
531531
def test_get_distribution_version():
532532
assert apipkg.distribution_version('setuptools') is not None
533533
assert apipkg.distribution_version('email') is None
534+
535+
536+
def test_eagerload_on_bython(monkeypatch):
537+
monkeypatch.delitem(sys.modules, 'bpython', raising=False)
538+
apipkg.initpkg(
539+
'apipkg.testmodule.example.lazy',
540+
{'test': 'apipkg.does_not_exist'})
541+
monkeypatch.setitem(sys.modules, 'bpython', True)
542+
with pytest.raises(ImportError):
543+
apipkg.initpkg(
544+
'apipkg.testmodule.example.lazy',
545+
{'test': 'apipkg.does_not_exist'})

0 commit comments

Comments
 (0)