summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_import.py')
-rw-r--r--Lib/test/test_import.py23
1 files changed, 20 insertions, 3 deletions
diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py
index 81e221dd735..aa65c7a419b 100644
--- a/Lib/test/test_import.py
+++ b/Lib/test/test_import.py
@@ -1,5 +1,3 @@
-from test.support import TESTFN, run_unittest, catch_warning
-
import unittest
import os
import random
@@ -8,7 +6,7 @@ import sys
import py_compile
import warnings
import imp
-from test.support import unlink, TESTFN, unload
+from test.support import unlink, TESTFN, unload, run_unittest, catch_warning
def remove_files(name):
@@ -267,6 +265,25 @@ class RelativeImport(unittest.TestCase):
from . import relimport
self.assertTrue(hasattr(relimport, "RelativeImport"))
+ def test_issue3221(self):
+ def check_relative():
+ exec("from . import relimport", ns)
+ # Check both OK with __package__ and __name__ correct
+ ns = dict(__package__='test', __name__='test.notarealmodule')
+ check_relative()
+ # Check both OK with only __name__ wrong
+ ns = dict(__package__='test', __name__='notarealpkg.notarealmodule')
+ check_relative()
+ # Check relative fails with only __package__ wrong
+ ns = dict(__package__='foo', __name__='test.notarealmodule')
+ self.assertRaises(SystemError, check_relative)
+ # Check relative fails with __package__ and __name__ wrong
+ ns = dict(__package__='foo', __name__='notarealpkg.notarealmodule')
+ self.assertRaises(SystemError, check_relative)
+ # Check both fail with package set to a non-string
+ ns = dict(__package__=object())
+ self.assertRaises(ValueError, check_relative)
+
def test_main(verbose=None):
run_unittest(ImportTest, PathsTests, RelativeImport)