[3.6] bpo-30876: Relative import from unloaded package now reimports … · python/cpython@28343e3

@@ -24,8 +24,9 @@

2424

EnvironmentVarGuard, TESTFN, check_warnings, forget, is_jython,

2525

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

2626

unlink, unload, create_empty_file, cpython_only, TESTFN_UNENCODABLE,

27-

temp_dir)

27+

temp_dir, DirsOnSysPath)

2828

from test.support import script_helper

29+

from test.test_importlib.util import uncache

293030313132

skip_if_dont_write_bytecode = unittest.skipIf(

@@ -640,11 +641,11 @@ def check_relative():

640641641642

# Check relative import fails with only __package__ wrong

642643

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

643-

self.assertRaises(SystemError, check_relative)

644+

self.assertRaises(ModuleNotFoundError, check_relative)

644645645646

# Check relative import fails with __package__ and __name__ wrong

646647

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

647-

self.assertRaises(SystemError, check_relative)

648+

self.assertRaises(ModuleNotFoundError, check_relative)

648649649650

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

650651

ns = dict(__package__=object())

@@ -659,6 +660,20 @@ def test_absolute_import_without_future(self):

659660

self.fail("explicit relative import triggered an "

660661

"implicit absolute import")

661662663+

def test_import_from_non_package(self):

664+

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

665+

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

666+

with self.assertRaises(ImportError):

667+

import submodule1

668+

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

669+

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

670+671+

def test_import_from_unloaded_package(self):

672+

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

673+

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

674+

import package2.submodule1

675+

package2.submodule1.submodule2

676+662677663678

class OverridingImportBuiltinTests(unittest.TestCase):

664679

def test_override_builtin(self):