88import contextlib
99import os
1010import sys
11+ import unittest .mock
1112
1213
1314class TestHook :
@@ -683,16 +684,16 @@ def test_import_module():
683684 importlib .import_module ("test.audit_test_data.submodule2" ) # absolute import
684685 importlib .import_module ("_testcapi" ) # extension module
685686
686- actual = [a [ 0 ] for e , a in hook .seen if e == "import" ]
687+ actual = [a for e , a in hook .seen if e == "import" ]
687688 assertSequenceEqual (
688689 [
689- "email" ,
690- "pythoninfo" ,
691- "test.audit_test_data.submodule" ,
692- "test.audit_test_data" ,
693- "test.audit_test_data.submodule2" ,
694- "_testcapi" ,
695- "_testcapi" ,
690+ ( "email" , None , sys . path , sys . meta_path , sys . path_hooks ) ,
691+ ( "pythoninfo" , None , sys . path , sys . meta_path , sys . path_hooks ) ,
692+ ( "test.audit_test_data.submodule" , None , sys . path , sys . meta_path , sys . path_hooks ) ,
693+ ( "test.audit_test_data" , None , sys . path , sys . meta_path , sys . path_hooks ) ,
694+ ( "test.audit_test_data.submodule2" , None , sys . path , sys . meta_path , sys . path_hooks ) ,
695+ ( "_testcapi" , None , sys . path , sys . meta_path , sys . path_hooks ) ,
696+ ( "_testcapi" , unittest . mock . ANY , None , None , None )
696697 ],
697698 actual ,
698699 )
@@ -704,47 +705,52 @@ def test_builtin__import__():
704705 __import__ ("importlib" )
705706 __import__ ("email" )
706707 __import__ ("pythoninfo" )
707- __import__ ("test. audit_test_data.submodule" , fromlist = [ "audit_test_data" ] )
708+ __import__ ("audit_test_data.submodule" , level = 1 , globals = { "__package__" : "test" } )
708709 __import__ ("test.audit_test_data.submodule2" )
709710 __import__ ("_testcapi" )
710711
711- actual = [a [ 0 ] for e , a in hook .seen if e == "import" ]
712+ actual = [a for e , a in hook .seen if e == "import" ]
712713 assertSequenceEqual (
713714 [
714- "email" ,
715- "pythoninfo" ,
716- "test.audit_test_data.submodule" ,
717- "test.audit_test_data" ,
718- "test.audit_test_data.submodule2" ,
719- "_testcapi" ,
720- "_testcapi" ,
715+ ( "email" , None , sys . path , sys . meta_path , sys . path_hooks ) ,
716+ ( "pythoninfo" , None , sys . path , sys . meta_path , sys . path_hooks ) ,
717+ ( "test.audit_test_data.submodule" , None , sys . path , sys . meta_path , sys . path_hooks ) ,
718+ ( "test.audit_test_data" , None , sys . path , sys . meta_path , sys . path_hooks ) ,
719+ ( "test.audit_test_data.submodule2" , None , sys . path , sys . meta_path , sys . path_hooks ) ,
720+ ( "_testcapi" , None , sys . path , sys . meta_path , sys . path_hooks ) ,
721+ ( "_testcapi" , unittest . mock . ANY , None , None , None )
721722 ],
722723 actual ,
723724 )
724725
725726def test_import_statement ():
726727 import importlib # noqa: F401
728+ # Set __package__ so relative imports work
729+ old_package = globals ().get ("__package__" , None )
730+ globals ()["__package__" ] = "test"
727731
728732 with TestHook () as hook :
729733 import importlib # noqa: F401
730734 import email # noqa: F401
731735 import pythoninfo # noqa: F401
732- from test .audit_test_data import submodule # noqa: F401
736+ from .audit_test_data import submodule # noqa: F401
733737 import test .audit_test_data .submodule2 # noqa: F401
734738 import _testcapi # noqa: F401
735739
736- actual = [a [0 ] for e , a in hook .seen if e == "import" ]
740+ globals ()["__package__" ] = old_package
741+
742+ actual = [a for e , a in hook .seen if e == "import" ]
737743 # Import statement ordering is different because the package is
738744 # loaded first and then the submodule
739745 assertSequenceEqual (
740746 [
741- "email" ,
742- "pythoninfo" ,
743- "test.audit_test_data" ,
744- "test.audit_test_data.submodule" ,
745- "test.audit_test_data.submodule2" ,
746- "_testcapi" ,
747- "_testcapi" ,
747+ ( "email" , None , sys . path , sys . meta_path , sys . path_hooks ) ,
748+ ( "pythoninfo" , None , sys . path , sys . meta_path , sys . path_hooks ) ,
749+ ( "test.audit_test_data" , None , sys . path , sys . meta_path , sys . path_hooks ) ,
750+ ( "test.audit_test_data.submodule" , None , sys . path , sys . meta_path , sys . path_hooks ) ,
751+ ( "test.audit_test_data.submodule2" , None , sys . path , sys . meta_path , sys . path_hooks ) ,
752+ ( "_testcapi" , None , sys . path , sys . meta_path , sys . path_hooks ) ,
753+ ( "_testcapi" , unittest . mock . ANY , None , None , None )
748754 ],
749755 actual ,
750756 )
0 commit comments