1997-09-06 15:42:57 -03:00
|
|
|
# Test packages (dotted-name import)
|
|
|
|
|
2007-08-24 16:13:58 -03:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
import textwrap
|
|
|
|
import unittest
|
|
|
|
from test import test_support
|
1997-09-06 15:42:57 -03:00
|
|
|
|
|
|
|
|
2007-08-24 16:13:58 -03:00
|
|
|
# Helpers to create and destroy hierarchies.
|
1997-09-06 15:42:57 -03:00
|
|
|
|
|
|
|
def cleanout(root):
|
|
|
|
names = os.listdir(root)
|
|
|
|
for name in names:
|
1998-03-26 15:42:58 -04:00
|
|
|
fullname = os.path.join(root, name)
|
|
|
|
if os.path.isdir(fullname) and not os.path.islink(fullname):
|
|
|
|
cleanout(fullname)
|
|
|
|
else:
|
|
|
|
os.remove(fullname)
|
2007-08-24 16:13:58 -03:00
|
|
|
os.rmdir(root)
|
1997-09-06 15:42:57 -03:00
|
|
|
|
2000-09-01 03:53:52 -03:00
|
|
|
def fixdir(lst):
|
2007-08-24 16:13:58 -03:00
|
|
|
if "__builtins__" in lst:
|
|
|
|
lst.remove("__builtins__")
|
2000-09-01 03:53:52 -03:00
|
|
|
return lst
|
|
|
|
|
1997-09-06 15:42:57 -03:00
|
|
|
|
2007-08-24 16:13:58 -03:00
|
|
|
# XXX Things to test
|
|
|
|
#
|
|
|
|
# import package without __init__
|
|
|
|
# import package with __init__
|
|
|
|
# __init__ importing submodule
|
|
|
|
# __init__ importing global module
|
|
|
|
# __init__ defining variables
|
|
|
|
# submodule importing other submodule
|
|
|
|
# submodule importing global module
|
|
|
|
# submodule import submodule via global name
|
|
|
|
# from package import submodule
|
|
|
|
# from package import subpackage
|
|
|
|
# from package import variable (defined in __init__)
|
|
|
|
# from package import * (defined in __init__)
|
|
|
|
|
|
|
|
|
|
|
|
class Test(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.root = None
|
2008-04-18 20:31:33 -03:00
|
|
|
self.pkgname = None
|
2007-08-24 16:13:58 -03:00
|
|
|
self.syspath = list(sys.path)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
sys.path[:] = self.syspath
|
|
|
|
cleanout(self.root)
|
|
|
|
|
2008-04-18 20:31:33 -03:00
|
|
|
# delete all modules concerning the tested hiearchy
|
|
|
|
if self.pkgname:
|
|
|
|
modules = [name for name in sys.modules
|
|
|
|
if self.pkgname in name.split('.')]
|
|
|
|
for name in modules:
|
|
|
|
del sys.modules[name]
|
|
|
|
|
2007-08-24 16:13:58 -03:00
|
|
|
def run_code(self, code):
|
|
|
|
exec(textwrap.dedent(code), globals(), {"self": self})
|
|
|
|
|
|
|
|
def mkhier(self, descr):
|
|
|
|
root = tempfile.mkdtemp()
|
1998-03-26 15:42:58 -04:00
|
|
|
sys.path.insert(0, root)
|
2007-08-24 16:13:58 -03:00
|
|
|
if not os.path.isdir(root):
|
|
|
|
os.mkdir(root)
|
|
|
|
for name, contents in descr:
|
|
|
|
comps = name.split()
|
|
|
|
fullname = root
|
|
|
|
for c in comps:
|
|
|
|
fullname = os.path.join(fullname, c)
|
|
|
|
if contents is None:
|
|
|
|
os.mkdir(fullname)
|
|
|
|
else:
|
|
|
|
f = open(fullname, "w")
|
|
|
|
f.write(contents)
|
|
|
|
if contents and contents[-1] != '\n':
|
|
|
|
f.write('\n')
|
|
|
|
f.close()
|
|
|
|
self.root = root
|
2008-04-18 20:31:33 -03:00
|
|
|
# package name is the name of the first item
|
|
|
|
self.pkgname = descr[0][0]
|
2007-08-24 16:13:58 -03:00
|
|
|
|
|
|
|
def test_1(self):
|
|
|
|
hier = [("t1", None), ("t1 __init__"+os.extsep+"py", "")]
|
|
|
|
self.mkhier(hier)
|
|
|
|
import t1
|
|
|
|
|
|
|
|
def test_2(self):
|
|
|
|
hier = [
|
|
|
|
("t2", None),
|
|
|
|
("t2 __init__"+os.extsep+"py", "'doc for t2'"),
|
|
|
|
("t2 sub", None),
|
|
|
|
("t2 sub __init__"+os.extsep+"py", ""),
|
|
|
|
("t2 sub subsub", None),
|
|
|
|
("t2 sub subsub __init__"+os.extsep+"py", "spam = 1"),
|
|
|
|
]
|
|
|
|
self.mkhier(hier)
|
|
|
|
|
|
|
|
import t2
|
|
|
|
self.assertEqual(t2.__doc__, "doc for t2")
|
|
|
|
|
|
|
|
import t2.sub
|
|
|
|
import t2.sub.subsub
|
|
|
|
self.assertEqual(t2.__name__, "t2")
|
|
|
|
self.assertEqual(t2.sub.__name__, "t2.sub")
|
|
|
|
self.assertEqual(t2.sub.subsub.__name__, "t2.sub.subsub")
|
|
|
|
|
|
|
|
# This exec crap is needed because Py3k forbids 'import *' outside
|
|
|
|
# of module-scope and __import__() is insufficient for what we need.
|
|
|
|
s = """
|
|
|
|
import t2
|
|
|
|
from t2 import *
|
|
|
|
self.assertEqual(dir(), ['self', 'sub', 't2'])
|
|
|
|
"""
|
|
|
|
self.run_code(s)
|
|
|
|
|
|
|
|
from t2 import sub
|
|
|
|
from t2.sub import subsub
|
|
|
|
from t2.sub.subsub import spam
|
|
|
|
self.assertEqual(sub.__name__, "t2.sub")
|
|
|
|
self.assertEqual(subsub.__name__, "t2.sub.subsub")
|
|
|
|
self.assertEqual(sub.subsub.__name__, "t2.sub.subsub")
|
|
|
|
for name in ['spam', 'sub', 'subsub', 't2']:
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(locals()["name"], "Failed to import %s" % name)
|
2007-08-24 16:13:58 -03:00
|
|
|
|
|
|
|
import t2.sub
|
|
|
|
import t2.sub.subsub
|
|
|
|
self.assertEqual(t2.__name__, "t2")
|
|
|
|
self.assertEqual(t2.sub.__name__, "t2.sub")
|
|
|
|
self.assertEqual(t2.sub.subsub.__name__, "t2.sub.subsub")
|
|
|
|
|
|
|
|
s = """
|
|
|
|
from t2 import *
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(dir(), ['self', 'sub'])
|
2007-08-24 16:13:58 -03:00
|
|
|
"""
|
|
|
|
self.run_code(s)
|
|
|
|
|
|
|
|
def test_3(self):
|
|
|
|
hier = [
|
|
|
|
("t3", None),
|
|
|
|
("t3 __init__"+os.extsep+"py", ""),
|
|
|
|
("t3 sub", None),
|
|
|
|
("t3 sub __init__"+os.extsep+"py", ""),
|
|
|
|
("t3 sub subsub", None),
|
|
|
|
("t3 sub subsub __init__"+os.extsep+"py", "spam = 1"),
|
|
|
|
]
|
|
|
|
self.mkhier(hier)
|
|
|
|
|
|
|
|
import t3.sub.subsub
|
|
|
|
self.assertEqual(t3.__name__, "t3")
|
|
|
|
self.assertEqual(t3.sub.__name__, "t3.sub")
|
|
|
|
self.assertEqual(t3.sub.subsub.__name__, "t3.sub.subsub")
|
|
|
|
|
|
|
|
def test_4(self):
|
|
|
|
hier = [
|
|
|
|
("t4.py", "raise RuntimeError('Shouldnt load t4.py')"),
|
|
|
|
("t4", None),
|
|
|
|
("t4 __init__"+os.extsep+"py", ""),
|
|
|
|
("t4 sub.py", "raise RuntimeError('Shouldnt load sub.py')"),
|
|
|
|
("t4 sub", None),
|
|
|
|
("t4 sub __init__"+os.extsep+"py", ""),
|
|
|
|
("t4 sub subsub"+os.extsep+"py",
|
|
|
|
"raise RuntimeError('Shouldnt load subsub.py')"),
|
|
|
|
("t4 sub subsub", None),
|
|
|
|
("t4 sub subsub __init__"+os.extsep+"py", "spam = 1"),
|
|
|
|
]
|
|
|
|
self.mkhier(hier)
|
|
|
|
|
|
|
|
s = """
|
|
|
|
from t4.sub.subsub import *
|
|
|
|
self.assertEqual(spam, 1)
|
|
|
|
"""
|
|
|
|
self.run_code(s)
|
|
|
|
|
|
|
|
def test_5(self):
|
|
|
|
hier = [
|
|
|
|
("t5", None),
|
|
|
|
("t5 __init__"+os.extsep+"py", "import t5.foo"),
|
|
|
|
("t5 string"+os.extsep+"py", "spam = 1"),
|
|
|
|
("t5 foo"+os.extsep+"py",
|
|
|
|
"from . import string; assert string.spam == 1"),
|
|
|
|
]
|
|
|
|
self.mkhier(hier)
|
|
|
|
|
|
|
|
import t5
|
|
|
|
s = """
|
|
|
|
from t5 import *
|
|
|
|
self.assertEqual(dir(), ['foo', 'self', 'string', 't5'])
|
|
|
|
"""
|
|
|
|
self.run_code(s)
|
|
|
|
|
|
|
|
import t5
|
|
|
|
self.assertEqual(fixdir(dir(t5)),
|
|
|
|
['__doc__', '__file__', '__name__',
|
2007-12-03 08:55:17 -04:00
|
|
|
'__package__', '__path__', 'foo', 'string', 't5'])
|
2007-08-24 16:13:58 -03:00
|
|
|
self.assertEqual(fixdir(dir(t5.foo)),
|
2007-12-03 08:55:17 -04:00
|
|
|
['__doc__', '__file__', '__name__', '__package__',
|
|
|
|
'string'])
|
2007-08-24 16:13:58 -03:00
|
|
|
self.assertEqual(fixdir(dir(t5.string)),
|
2007-12-03 08:55:17 -04:00
|
|
|
['__doc__', '__file__', '__name__','__package__',
|
|
|
|
'spam'])
|
2007-08-24 16:13:58 -03:00
|
|
|
|
|
|
|
def test_6(self):
|
|
|
|
hier = [
|
|
|
|
("t6", None),
|
|
|
|
("t6 __init__"+os.extsep+"py",
|
|
|
|
"__all__ = ['spam', 'ham', 'eggs']"),
|
|
|
|
("t6 spam"+os.extsep+"py", ""),
|
|
|
|
("t6 ham"+os.extsep+"py", ""),
|
|
|
|
("t6 eggs"+os.extsep+"py", ""),
|
|
|
|
]
|
|
|
|
self.mkhier(hier)
|
|
|
|
|
|
|
|
import t6
|
|
|
|
self.assertEqual(fixdir(dir(t6)),
|
|
|
|
['__all__', '__doc__', '__file__',
|
2007-12-03 08:55:17 -04:00
|
|
|
'__name__', '__package__', '__path__'])
|
2007-08-24 16:13:58 -03:00
|
|
|
s = """
|
|
|
|
import t6
|
|
|
|
from t6 import *
|
|
|
|
self.assertEqual(fixdir(dir(t6)),
|
|
|
|
['__all__', '__doc__', '__file__',
|
2007-12-03 08:55:17 -04:00
|
|
|
'__name__', '__package__', '__path__',
|
|
|
|
'eggs', 'ham', 'spam'])
|
2007-08-24 16:13:58 -03:00
|
|
|
self.assertEqual(dir(), ['eggs', 'ham', 'self', 'spam', 't6'])
|
|
|
|
"""
|
|
|
|
self.run_code(s)
|
|
|
|
|
|
|
|
def test_7(self):
|
|
|
|
hier = [
|
|
|
|
("t7", None),
|
2008-04-18 20:31:33 -03:00
|
|
|
("t7"+os.extsep+"py", ""),
|
2007-08-24 16:13:58 -03:00
|
|
|
("t7 __init__"+os.extsep+"py", ""),
|
|
|
|
("t7 sub"+os.extsep+"py",
|
|
|
|
"raise RuntimeError('Shouldnt load sub.py')"),
|
|
|
|
("t7 sub", None),
|
|
|
|
("t7 sub __init__"+os.extsep+"py", ""),
|
|
|
|
("t7 sub "+os.extsep+"py",
|
|
|
|
"raise RuntimeError('Shouldnt load subsub.py')"),
|
|
|
|
("t7 sub subsub", None),
|
|
|
|
("t7 sub subsub __init__"+os.extsep+"py",
|
|
|
|
"spam = 1"),
|
|
|
|
]
|
|
|
|
self.mkhier(hier)
|
|
|
|
|
|
|
|
|
|
|
|
t7, sub, subsub = None, None, None
|
|
|
|
import t7 as tas
|
|
|
|
self.assertEqual(fixdir(dir(tas)),
|
2007-12-03 08:55:17 -04:00
|
|
|
['__doc__', '__file__', '__name__',
|
|
|
|
'__package__', '__path__'])
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertFalse(t7)
|
2007-08-24 16:13:58 -03:00
|
|
|
from t7 import sub as subpar
|
|
|
|
self.assertEqual(fixdir(dir(subpar)),
|
2007-12-03 08:55:17 -04:00
|
|
|
['__doc__', '__file__', '__name__',
|
|
|
|
'__package__', '__path__'])
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertFalse(t7)
|
|
|
|
self.assertFalse(sub)
|
2007-08-24 16:13:58 -03:00
|
|
|
from t7.sub import subsub as subsubsub
|
|
|
|
self.assertEqual(fixdir(dir(subsubsub)),
|
2007-12-03 08:55:17 -04:00
|
|
|
['__doc__', '__file__', '__name__',
|
|
|
|
'__package__', '__path__', 'spam'])
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertFalse(t7)
|
|
|
|
self.assertFalse(sub)
|
|
|
|
self.assertFalse(subsub)
|
2007-08-24 16:13:58 -03:00
|
|
|
from t7.sub.subsub import spam as ham
|
|
|
|
self.assertEqual(ham, 1)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertFalse(t7)
|
|
|
|
self.assertFalse(sub)
|
|
|
|
self.assertFalse(subsub)
|
2007-08-24 16:13:58 -03:00
|
|
|
|
|
|
|
|
|
|
|
def test_main():
|
|
|
|
test_support.run_unittest(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|