@@ -115,12 +115,12 @@ def test_recursive_import(self, monkeypatch, tmpdir):
115
115
})
116
116
""" ))
117
117
pkgdir .join ('submod.py' ).write (py .code .Source ("""
118
- import recmodule
118
+ import recmodule
119
119
class someclass: pass
120
120
print (recmodule.__dict__)
121
121
""" ))
122
122
monkeypatch .syspath_prepend (tmpdir )
123
- import recmodule
123
+ import recmodule
124
124
assert isinstance (recmodule , apipkg .ApiModule )
125
125
assert recmodule .some .__name__ == "someclass"
126
126
@@ -134,7 +134,20 @@ def test_module_alias_import(self, monkeypatch, tmpdir):
134
134
""" ))
135
135
monkeypatch .syspath_prepend (tmpdir )
136
136
import aliasimport
137
- assert aliasimport .some is py .std .os .path
137
+ for k , v in py .std .os .path .__dict__ .items ():
138
+ assert getattr (aliasimport .some , k ) == v
139
+
140
+ def test_from_module_alias_import (self , monkeypatch , tmpdir ):
141
+ pkgdir = tmpdir .mkdir ("fromaliasimport" )
142
+ pkgdir .join ('__init__.py' ).write (py .code .Source ("""
143
+ import apipkg
144
+ apipkg.initpkg(__name__, exportdefs={
145
+ 'some': 'os.path',
146
+ })
147
+ """ ))
148
+ monkeypatch .syspath_prepend (tmpdir )
149
+ from fromaliasimport .some import join
150
+ assert join is py .std .os .path .join
138
151
139
152
def xtest_nested_absolute_imports ():
140
153
import email
@@ -211,14 +224,22 @@ def test_initpkg_transfers_attrs(monkeypatch):
211
224
assert newmod .__loader__ == mod .__loader__
212
225
assert newmod .__doc__ == mod .__doc__
213
226
214
- def test_initpkg_not_overwrite_exportdefs (monkeypatch ):
227
+ def test_initpkg_nodoc (monkeypatch ):
215
228
mod = type (sys )('hello' )
216
- mod .__doc__ = "this is the documentation "
229
+ mod .__file__ = "hello.py "
217
230
monkeypatch .setitem (sys .modules , 'hello' , mod )
218
- apipkg .initpkg ('hello' , {"__doc__" : "sys:__doc__" })
231
+ apipkg .initpkg ('hello' , {})
219
232
newmod = sys .modules ['hello' ]
220
- assert newmod != mod
221
- assert newmod .__doc__ == sys .__doc__
233
+ assert not newmod .__doc__
234
+
235
+ def test_initpkg_overwrite_doc (monkeypatch ):
236
+ hello = type (sys )('hello' )
237
+ hello .__doc__ = "this is the documentation"
238
+ monkeypatch .setitem (sys .modules , 'hello' , hello )
239
+ apipkg .initpkg ('hello' , {"__doc__" : "sys:__doc__" })
240
+ newhello = sys .modules ['hello' ]
241
+ assert newhello != hello
242
+ assert newhello .__doc__ == sys .__doc__
222
243
223
244
def test_initpkg_not_transfers_not_existing_attrs (monkeypatch ):
224
245
mod = type (sys )('hello' )
@@ -276,7 +297,7 @@ def test_onfirstaccess(tmpdir, monkeypatch):
276
297
""" ))
277
298
pkgdir .join ('submod.py' ).write (py .code .Source ("""
278
299
l = []
279
- def init():
300
+ def init():
280
301
l.append(1)
281
302
""" ))
282
303
monkeypatch .syspath_prepend (tmpdir )
@@ -298,9 +319,9 @@ def test_onfirstaccess_setsnewattr(tmpdir, monkeypatch, mode):
298
319
)
299
320
""" ))
300
321
pkgdir .join ('submod.py' ).write (py .code .Source ("""
301
- def init():
322
+ def init():
302
323
import %s as pkg
303
- pkg.newattr = 42
324
+ pkg.newattr = 42
304
325
""" % pkgname ))
305
326
monkeypatch .syspath_prepend (tmpdir )
306
327
mod = __import__ (pkgname )
@@ -380,3 +401,76 @@ def test_extra_attributes(tmpdir, monkeypatch):
380
401
monkeypatch .syspath_prepend (tmpdir )
381
402
import extra_attributes
382
403
assert extra_attributes .foo == 'bar'
404
+
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
+
411
+ def test_aliasmodule_repr ():
412
+ am = apipkg .AliasModule ("mymod" , "sys" )
413
+ r = repr (am )
414
+ assert "<AliasModule 'mymod' for 'sys'>" == r
415
+ am .version
416
+ assert repr (am ) == r
417
+
418
+ def test_aliasmodule_proxy_methods (tmpdir , monkeypatch ):
419
+ pkgdir = tmpdir
420
+ pkgdir .join ('aliasmodule_proxy.py' ).write (py .code .Source ("""
421
+ def doit():
422
+ return 42
423
+ """ ))
424
+
425
+ pkgdir .join ('my_aliasmodule_proxy.py' ).write (py .code .Source ("""
426
+ import apipkg
427
+ apipkg.initpkg(__name__, dict(proxy='aliasmodule_proxy'))
428
+
429
+ def doit():
430
+ return 42
431
+ """ ))
432
+
433
+ monkeypatch .syspath_prepend (tmpdir )
434
+ import aliasmodule_proxy as orig
435
+ from my_aliasmodule_proxy import proxy
436
+
437
+ doit = proxy .doit
438
+ assert doit is orig .doit
439
+
440
+ del proxy .doit
441
+ py .test .raises (AttributeError , "orig.doit" )
442
+
443
+ proxy .doit = doit
444
+ assert orig .doit is doit
445
+
446
+ def test_aliasmodule_nested_import_with_from (tmpdir , monkeypatch ):
447
+ import os
448
+ pkgdir = tmpdir .mkdir ("api1" )
449
+ pkgdir .ensure ("__init__.py" ).write (py .std .textwrap .dedent ("""
450
+ import apipkg
451
+ apipkg.initpkg(__name__, {
452
+ 'os2': 'api2',
453
+ 'os2.path': 'api2.path2',
454
+ })
455
+ """ ))
456
+ tmpdir .join ("api2.py" ).write (py .std .textwrap .dedent ("""
457
+ import os, sys
458
+ from os import path
459
+ sys.modules['api2.path2'] = path
460
+ x = 3
461
+ """ ))
462
+ monkeypatch .syspath_prepend (tmpdir )
463
+ from api1 import os2
464
+ from api1 .os2 .path import abspath
465
+ assert abspath == os .path .abspath
466
+ # check that api1.os2 mirrors os.*
467
+ assert os2 .x == 3
468
+ import api1
469
+ assert 'os2.path' not in api1 .__dict__
470
+
471
+
472
+ def test_initpkg_without_old_module ():
473
+ apipkg .initpkg ("initpkg_without_old_module" ,
474
+ dict (modules = "sys:modules" ))
475
+ from initpkg_without_old_module import modules
476
+ assert modules is sys .modules
0 commit comments