bpo-30876: Relative import from unloaded package now reimports the pa… · python/cpython@8a9cd20

@@ -23,8 +23,9 @@

2323

EnvironmentVarGuard, TESTFN, check_warnings, forget, is_jython,

2424

make_legacy_pyc, rmtree, run_unittest, swap_attr, swap_item, temp_umask,

2525

unlink, unload, create_empty_file, cpython_only, TESTFN_UNENCODABLE,

26-

temp_dir)

26+

temp_dir, DirsOnSysPath)

2727

from test.support import script_helper

28+

from test.test_importlib.util import uncache

282929303031

skip_if_dont_write_bytecode = unittest.skipIf(

@@ -670,11 +671,11 @@ def check_relative():

670671671672

# Check relative import fails with only __package__ wrong

672673

ns = dict(__package__='foo', __name__='test.notarealmodule')

673-

self.assertRaises(SystemError, check_relative)

674+

self.assertRaises(ModuleNotFoundError, check_relative)

674675675676

# Check relative import fails with __package__ and __name__ wrong

676677

ns = dict(__package__='foo', __name__='notarealpkg.notarealmodule')

677-

self.assertRaises(SystemError, check_relative)

678+

self.assertRaises(ModuleNotFoundError, check_relative)

678679679680

# Check relative import fails with package set to a non-string

680681

ns = dict(__package__=object())

@@ -689,6 +690,20 @@ def test_absolute_import_without_future(self):

689690

self.fail("explicit relative import triggered an "

690691

"implicit absolute import")

691692693+

def test_import_from_non_package(self):

694+

path = os.path.join(os.path.dirname(__file__), 'data', 'package2')

695+

with uncache('submodule1', 'submodule2'), DirsOnSysPath(path):

696+

with self.assertRaises(ImportError):

697+

import submodule1

698+

self.assertNotIn('submodule1', sys.modules)

699+

self.assertNotIn('submodule2', sys.modules)

700+701+

def test_import_from_unloaded_package(self):

702+

with uncache('package2', 'package2.submodule1', 'package2.submodule2'), \

703+

DirsOnSysPath(os.path.join(os.path.dirname(__file__), 'data')):

704+

import package2.submodule1

705+

package2.submodule1.submodule2

706+692707693708

class OverridingImportBuiltinTests(unittest.TestCase):

694709

def test_override_builtin(self):