File tree Expand file tree Collapse file tree 2 files changed +25
-7
lines changed Expand file tree Collapse file tree 2 files changed +25
-7
lines changed Original file line number Diff line number Diff line change @@ -191,18 +191,22 @@ def getmod():
191191 mod .append (x )
192192 return mod [0 ]
193193
194+ x = modpath + ("." + attrname if attrname else "" )
195+ repr_result = "<AliasModule {!r} for {!r}>" .format (modname , x )
196+
194197 class AliasModule (ModuleType ):
195198 def __repr__ (self ):
196- x = modpath
197- if attrname :
198- x += "." + attrname
199- return "<AliasModule {!r} for {!r}>" .format (modname , x )
199+ return repr_result
200200
201201 def __getattribute__ (self , name ):
202202 try :
203203 return getattr (getmod (), name )
204204 except ImportError :
205- return None
205+ if modpath == "pytest" and attrname is None :
206+ # hack for pylibs py.test
207+ return None
208+ else :
209+ raise
206210
207211 def __setattr__ (self , name , value ):
208212 setattr (getmod (), name , value )
Original file line number Diff line number Diff line change @@ -551,11 +551,25 @@ def test_aliasmodule_aliases_an_attribute():
551551 assert not hasattr (am , "lqkje" )
552552
553553
554- def test_aliasmodule_aliases_unimportable ():
554+ def test_aliasmodule_aliases_unimportable_fails ():
555555 am = apipkg .AliasModule ("mymod" , "qlwkejqlwe" , "main" )
556556 r = repr (am )
557557 assert "<AliasModule 'mymod' for 'qlwkejqlwe.main'>" == r
558- assert am .qwe is None
558+ # this would pass starting with apipkg 1.3 to work around a pytest bug
559+ with pytest .raises (ImportError ):
560+ am .qwe is None
561+
562+
563+ def test_aliasmodule_pytest_autoreturn_none_for_hack (monkeypatch ):
564+ def error (* k ):
565+ raise ImportError (k )
566+
567+ monkeypatch .setattr (apipkg , "importobj" , error )
568+ # apipkg 1.3 added this hack
569+ am = apipkg .AliasModule ("mymod" , "pytest" )
570+ r = repr (am )
571+ assert "<AliasModule 'mymod' for 'pytest'>" == r
572+ assert am .test is None
559573
560574
561575def test_aliasmodule_unicode ():
You can’t perform that action at this time.
0 commit comments