mirror of
https://github.com/python/cpython.git
synced 2025-12-15 21:44:50 +00:00
Fix issue 3221 by emitting a RuntimeWarning instead of raising SystemError when the parent module can't be found during an absolute import (likely due to non-PEP 361 aware code which sets a module level __package__ attribute)
This commit is contained in:
parent
12c8660cc6
commit
b028f50911
3 changed files with 61 additions and 6 deletions
|
|
@ -1,5 +1,3 @@
|
|||
from test.test_support import TESTFN, run_unittest, catch_warning
|
||||
|
||||
import unittest
|
||||
import os
|
||||
import random
|
||||
|
|
@ -7,7 +5,7 @@ import shutil
|
|||
import sys
|
||||
import py_compile
|
||||
import warnings
|
||||
from test.test_support import unlink, TESTFN, unload
|
||||
from test.test_support import unlink, TESTFN, unload, run_unittest, catch_warning
|
||||
|
||||
|
||||
def remove_files(name):
|
||||
|
|
@ -266,6 +264,38 @@ class RelativeImport(unittest.TestCase):
|
|||
from . import relimport
|
||||
self.assertTrue(hasattr(relimport, "RelativeImport"))
|
||||
|
||||
def test_issue3221(self):
|
||||
def check_absolute():
|
||||
exec "from os import path" in ns
|
||||
def check_relative():
|
||||
exec "from . import relimport" in ns
|
||||
# Check both OK with __package__ and __name__ correct
|
||||
ns = dict(__package__='test', __name__='test.notarealmodule')
|
||||
check_absolute()
|
||||
check_relative()
|
||||
# Check both OK with only __name__ wrong
|
||||
ns = dict(__package__='test', __name__='notarealpkg.notarealmodule')
|
||||
check_absolute()
|
||||
check_relative()
|
||||
# Check relative fails with only __package__ wrong
|
||||
ns = dict(__package__='foo', __name__='test.notarealmodule')
|
||||
with catch_warning() as w:
|
||||
check_absolute()
|
||||
self.assert_('foo' in str(w.message))
|
||||
self.assertEqual(w.category, RuntimeWarning)
|
||||
self.assertRaises(SystemError, check_relative)
|
||||
# Check relative fails with __package__ and __name__ wrong
|
||||
ns = dict(__package__='foo', __name__='notarealpkg.notarealmodule')
|
||||
with catch_warning() as w:
|
||||
check_absolute()
|
||||
self.assert_('foo' in str(w.message))
|
||||
self.assertEqual(w.category, RuntimeWarning)
|
||||
self.assertRaises(SystemError, check_relative)
|
||||
# Check both fail with package set to a non-string
|
||||
ns = dict(__package__=object())
|
||||
self.assertRaises(ValueError, check_absolute)
|
||||
self.assertRaises(ValueError, check_relative)
|
||||
|
||||
def test_main(verbose=None):
|
||||
run_unittest(ImportTest, PathsTests, RelativeImport)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue