Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_import.py @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | bd74e866f9ca |
| children | 6768b2350c57 |
| rev | line source |
|---|---|
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
1 import unittest |
|
15057
bf27caba3d0e
Adding Jeremy's new test_import (SF patch 101709).
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2 import os |
|
bf27caba3d0e
Adding Jeremy's new test_import (SF patch 101709).
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3 import random |
|
43735
bd2f8bb11bcc
Backport of import tests for bug http://bugs.python.org/issue1293 and bug http://bugs.python.org/issue1342
Christian Heimes <christian@cheimes.de>
parents:
42584
diff
changeset
|
4 import shutil |
|
15317
13975aec5152
Insert the current directory to the front of sys.path -- and remove it
Guido van Rossum <guido@python.org>
parents:
15057
diff
changeset
|
5 import sys |
|
23851
d6ae544acd24
Fix SF bug # 561858 Assertion with very long lists
Neal Norwitz <nnorwitz@gmail.com>
parents:
23584
diff
changeset
|
6 import py_compile |
|
40455
480789b3294d
Add test.test_support.guard_warnings_filter . This function returns a context
Brett Cannon <bcannon@gmail.com>
parents:
40144
diff
changeset
|
7 import warnings |
|
50618
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
8 import marshal |
|
49146
f28b999a69f8
Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents:
49099
diff
changeset
|
9 from test.test_support import unlink, TESTFN, unload, run_unittest, check_warnings |
|
15317
13975aec5152
Insert the current directory to the front of sys.path -- and remove it
Guido van Rossum <guido@python.org>
parents:
15057
diff
changeset
|
10 |
|
17707
fe4c1034b480
SF bug #422177: Results from .pyc differs from .py
Tim Peters <tim.peters@gmail.com>
parents:
17095
diff
changeset
|
11 |
| 32884 | 12 def remove_files(name): |
| 13 for f in (name + os.extsep + "py", | |
| 14 name + os.extsep + "pyc", | |
| 15 name + os.extsep + "pyo", | |
| 16 name + os.extsep + "pyw", | |
| 17 name + "$py.class"): | |
| 18 if os.path.exists(f): | |
| 19 os.remove(f) | |
| 20 | |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
21 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
22 class ImportTest(unittest.TestCase): |
|
40144
6609f020649f
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
39943
diff
changeset
|
23 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
24 def testCaseSensitivity(self): |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
25 # Brief digression to test that import is case-sensitive: if we got this |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
26 # far, we know for sure that "random" exists. |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
27 try: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
28 import RAnDoM |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
29 except ImportError: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
30 pass |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
31 else: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
32 self.fail("import of RAnDoM should have failed (case mismatch)") |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
33 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
34 def testDoubleConst(self): |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
35 # Another brief digression to test the accuracy of manifest float constants. |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
36 from test import double_const # don't blink -- that *was* the test |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
37 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
38 def testImport(self): |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
39 def test_with_extension(ext): |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
40 # ext normally ".py"; perhaps ".pyw" |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
41 source = TESTFN + ext |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
42 pyo = TESTFN + os.extsep + "pyo" |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
43 if sys.platform.startswith('java'): |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
44 pyc = TESTFN + "$py.class" |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
45 else: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
46 pyc = TESTFN + os.extsep + "pyc" |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
47 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
48 f = open(source, "w") |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
49 print >> f, "# This tests Python's ability to import a", ext, "file." |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
50 a = random.randrange(1000) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
51 b = random.randrange(1000) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
52 print >> f, "a =", a |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
53 print >> f, "b =", b |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
54 f.close() |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
55 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
56 try: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
57 try: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
58 mod = __import__(TESTFN) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
59 except ImportError, err: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
60 self.fail("import from %s failed: %s" % (ext, err)) |
|
40144
6609f020649f
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
39943
diff
changeset
|
61 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
62 self.assertEquals(mod.a, a, |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
63 "module loaded (%s) but contents invalid" % mod) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
64 self.assertEquals(mod.b, b, |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
65 "module loaded (%s) but contents invalid" % mod) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
66 finally: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
67 os.unlink(source) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
68 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
69 try: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
70 try: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
71 reload(mod) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
72 except ImportError, err: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
73 self.fail("import from .pyc/.pyo failed: %s" % err) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
74 finally: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
75 try: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
76 os.unlink(pyc) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
77 except OSError: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
78 pass |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
79 try: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
80 os.unlink(pyo) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
81 except OSError: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
82 pass |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
83 del sys.modules[TESTFN] |
|
15057
bf27caba3d0e
Adding Jeremy's new test_import (SF patch 101709).
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
84 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
85 sys.path.insert(0, os.curdir) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
86 try: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
87 test_with_extension(os.extsep + "py") |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
88 if sys.platform.startswith("win"): |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
89 for ext in ".PY", ".Py", ".pY", ".pyw", ".PYW", ".pYw": |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
90 test_with_extension(ext) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
91 finally: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
92 del sys.path[0] |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
93 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
94 def testImpModule(self): |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
95 # Verify that the imp module can correctly load and find .py files |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
96 import imp |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
97 x = imp.find_module("os") |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
98 os = imp.load_module("os", *x) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
99 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
100 def test_module_with_large_stack(self, module='longlist'): |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
101 # create module w/list of 65000 elements to test bug #561858 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
102 filename = module + os.extsep + 'py' |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
103 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
104 # create a file with a list of 65000 elements |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
105 f = open(filename, 'w+') |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
106 f.write('d = [\n') |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
107 for i in range(65000): |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
108 f.write('"",\n') |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
109 f.write(']') |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
110 f.close() |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
111 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
112 # compile & remove .py file, we only need .pyc (or .pyo) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
113 f = open(filename, 'r') |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
114 py_compile.compile(filename) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
115 f.close() |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
116 os.unlink(filename) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
117 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
118 # need to be able to load from current dir |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
119 sys.path.append('') |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
120 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
121 # this used to crash |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
122 exec 'import ' + module |
|
15057
bf27caba3d0e
Adding Jeremy's new test_import (SF patch 101709).
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
123 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
124 # cleanup |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
125 del sys.path[-1] |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
126 for ext in 'pyc', 'pyo': |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
127 fname = module + os.extsep + ext |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
128 if os.path.exists(fname): |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
129 os.unlink(fname) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
130 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
131 def test_failing_import_sticks(self): |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
132 source = TESTFN + os.extsep + "py" |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
133 f = open(source, "w") |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
134 print >> f, "a = 1/0" |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
135 f.close() |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
136 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
137 # New in 2.4, we shouldn't be able to import that no matter how often |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
138 # we try. |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
139 sys.path.insert(0, os.curdir) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
140 try: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
141 for i in 1, 2, 3: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
142 try: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
143 mod = __import__(TESTFN) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
144 except ZeroDivisionError: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
145 if TESTFN in sys.modules: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
146 self.fail("damaged module in sys.modules on %i. try" % i) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
147 else: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
148 self.fail("was able to import a damaged module on %i. try" % i) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
149 finally: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
150 sys.path.pop(0) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
151 remove_files(TESTFN) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
152 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
153 def test_failing_reload(self): |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
154 # A failing reload should leave the module object in sys.modules. |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
155 source = TESTFN + os.extsep + "py" |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
156 f = open(source, "w") |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
157 print >> f, "a = 1" |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
158 print >> f, "b = 2" |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
159 f.close() |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
160 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
161 sys.path.insert(0, os.curdir) |
|
18992
7fa1f87ca6b2
Derived from SF patch #446899 Permit import of .pyw under Windows, from
Tim Peters <tim.peters@gmail.com>
parents:
18930
diff
changeset
|
162 try: |
|
7fa1f87ca6b2
Derived from SF patch #446899 Permit import of .pyw under Windows, from
Tim Peters <tim.peters@gmail.com>
parents:
18930
diff
changeset
|
163 mod = __import__(TESTFN) |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
164 self.assert_(TESTFN in sys.modules, "expected module in sys.modules") |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
165 self.assertEquals(mod.a, 1, "module has wrong attribute values") |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
166 self.assertEquals(mod.b, 2, "module has wrong attribute values") |
|
15057
bf27caba3d0e
Adding Jeremy's new test_import (SF patch 101709).
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
167 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
168 # On WinXP, just replacing the .py file wasn't enough to |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
169 # convince reload() to reparse it. Maybe the timestamp didn't |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
170 # move enough. We force it to get reparsed by removing the |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
171 # compiled file too. |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
172 remove_files(TESTFN) |
|
23584
c6af574bda29
Verify that the imp can find and load .py files.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20654
diff
changeset
|
173 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
174 # Now damage the module. |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
175 f = open(source, "w") |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
176 print >> f, "a = 10" |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
177 print >> f, "b = 20//0" |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
178 f.close() |
|
23851
d6ae544acd24
Fix SF bug # 561858 Assertion with very long lists
Neal Norwitz <nnorwitz@gmail.com>
parents:
23584
diff
changeset
|
179 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
180 self.assertRaises(ZeroDivisionError, reload, mod) |
|
23851
d6ae544acd24
Fix SF bug # 561858 Assertion with very long lists
Neal Norwitz <nnorwitz@gmail.com>
parents:
23584
diff
changeset
|
181 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
182 # But we still expect the module to be in sys.modules. |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
183 mod = sys.modules.get(TESTFN) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
184 self.failIf(mod is None, "expected module to still be in sys.modules") |
|
23851
d6ae544acd24
Fix SF bug # 561858 Assertion with very long lists
Neal Norwitz <nnorwitz@gmail.com>
parents:
23584
diff
changeset
|
185 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
186 # We should have replaced a w/ 10, but the old b value should |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
187 # stick. |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
188 self.assertEquals(mod.a, 10, "module has wrong attribute values") |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
189 self.assertEquals(mod.b, 2, "module has wrong attribute values") |
| 32884 | 190 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
191 finally: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
192 sys.path.pop(0) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
193 remove_files(TESTFN) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
194 if TESTFN in sys.modules: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
195 del sys.modules[TESTFN] |
|
41133
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
41128
diff
changeset
|
196 |
|
41128
45ce388ec59a
Bug #742342: make Python stop segfaulting on infinitely-recursive reload()s. Fixed by patch #922167.
Collin Winter <collinw@gmail.com>
parents:
40455
diff
changeset
|
197 def test_infinite_reload(self): |
|
45ce388ec59a
Bug #742342: make Python stop segfaulting on infinitely-recursive reload()s. Fixed by patch #922167.
Collin Winter <collinw@gmail.com>
parents:
40455
diff
changeset
|
198 # Bug #742342 reports that Python segfaults (infinite recursion in C) |
|
45ce388ec59a
Bug #742342: make Python stop segfaulting on infinitely-recursive reload()s. Fixed by patch #922167.
Collin Winter <collinw@gmail.com>
parents:
40455
diff
changeset
|
199 # when faced with self-recursive reload()ing. |
|
41133
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
41128
diff
changeset
|
200 |
|
41128
45ce388ec59a
Bug #742342: make Python stop segfaulting on infinitely-recursive reload()s. Fixed by patch #922167.
Collin Winter <collinw@gmail.com>
parents:
40455
diff
changeset
|
201 sys.path.insert(0, os.path.dirname(__file__)) |
|
45ce388ec59a
Bug #742342: make Python stop segfaulting on infinitely-recursive reload()s. Fixed by patch #922167.
Collin Winter <collinw@gmail.com>
parents:
40455
diff
changeset
|
202 try: |
|
45ce388ec59a
Bug #742342: make Python stop segfaulting on infinitely-recursive reload()s. Fixed by patch #922167.
Collin Winter <collinw@gmail.com>
parents:
40455
diff
changeset
|
203 import infinite_reload |
|
45ce388ec59a
Bug #742342: make Python stop segfaulting on infinitely-recursive reload()s. Fixed by patch #922167.
Collin Winter <collinw@gmail.com>
parents:
40455
diff
changeset
|
204 finally: |
|
45ce388ec59a
Bug #742342: make Python stop segfaulting on infinitely-recursive reload()s. Fixed by patch #922167.
Collin Winter <collinw@gmail.com>
parents:
40455
diff
changeset
|
205 sys.path.pop(0) |
| 32884 | 206 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
207 def test_import_name_binding(self): |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
208 # import x.y.z binds x in the current namespace |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
209 import test as x |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
210 import test.test_support |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
211 self.assert_(x is test, x.__name__) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
212 self.assert_(hasattr(test.test_support, "__file__")) |
| 32884 | 213 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
214 # import x.y.z as w binds z as w |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
215 import test.test_support as y |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
216 self.assert_(y is test.test_support, y.__name__) |
| 32884 | 217 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
218 def test_import_initless_directory_warning(self): |
|
49099
ff96a313cd1c
warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents:
47944
diff
changeset
|
219 with warnings.catch_warnings(): |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
220 # Just a random non-package directory we always expect to be |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
221 # somewhere in sys.path... |
|
40455
480789b3294d
Add test.test_support.guard_warnings_filter . This function returns a context
Brett Cannon <bcannon@gmail.com>
parents:
40144
diff
changeset
|
222 warnings.simplefilter('error', ImportWarning) |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
223 self.assertRaises(ImportWarning, __import__, "site-packages") |
|
35742
1c4d192f1568
Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
32884
diff
changeset
|
224 |
|
44614
dc9d95b717c5
Fixed #1776. __import__() no longer imports modules by file name
Christian Heimes <christian@cheimes.de>
parents:
43736
diff
changeset
|
225 def test_importbyfilename(self): |
|
dc9d95b717c5
Fixed #1776. __import__() no longer imports modules by file name
Christian Heimes <christian@cheimes.de>
parents:
43736
diff
changeset
|
226 path = os.path.abspath(TESTFN) |
|
dc9d95b717c5
Fixed #1776. __import__() no longer imports modules by file name
Christian Heimes <christian@cheimes.de>
parents:
43736
diff
changeset
|
227 try: |
|
dc9d95b717c5
Fixed #1776. __import__() no longer imports modules by file name
Christian Heimes <christian@cheimes.de>
parents:
43736
diff
changeset
|
228 __import__(path) |
|
dc9d95b717c5
Fixed #1776. __import__() no longer imports modules by file name
Christian Heimes <christian@cheimes.de>
parents:
43736
diff
changeset
|
229 except ImportError, err: |
|
dc9d95b717c5
Fixed #1776. __import__() no longer imports modules by file name
Christian Heimes <christian@cheimes.de>
parents:
43736
diff
changeset
|
230 self.assertEqual("Import by filename is not supported.", |
|
dc9d95b717c5
Fixed #1776. __import__() no longer imports modules by file name
Christian Heimes <christian@cheimes.de>
parents:
43736
diff
changeset
|
231 err.args[0]) |
|
dc9d95b717c5
Fixed #1776. __import__() no longer imports modules by file name
Christian Heimes <christian@cheimes.de>
parents:
43736
diff
changeset
|
232 else: |
|
dc9d95b717c5
Fixed #1776. __import__() no longer imports modules by file name
Christian Heimes <christian@cheimes.de>
parents:
43736
diff
changeset
|
233 self.fail("import by path didn't raise an exception") |
|
dc9d95b717c5
Fixed #1776. __import__() no longer imports modules by file name
Christian Heimes <christian@cheimes.de>
parents:
43736
diff
changeset
|
234 |
|
50618
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
235 class TestPycRewriting(unittest.TestCase): |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
236 # Test that the `co_filename` attribute on code objects always points |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
237 # to the right file, even when various things happen (e.g. both the .py |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
238 # and the .pyc file are renamed). |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
239 |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
240 module_name = "unlikely_module_name" |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
241 module_source = """ |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
242 import sys |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
243 code_filename = sys._getframe().f_code.co_filename |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
244 module_filename = __file__ |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
245 constant = 1 |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
246 def func(): |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
247 pass |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
248 func_filename = func.func_code.co_filename |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
249 """ |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
250 dir_name = os.path.abspath(TESTFN) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
251 file_name = os.path.join(dir_name, module_name) + os.extsep + "py" |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
252 compiled_name = file_name + ("c" if __debug__ else "o") |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
253 |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
254 def setUp(self): |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
255 self.sys_path = sys.path[:] |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
256 self.orig_module = sys.modules.pop(self.module_name, None) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
257 os.mkdir(self.dir_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
258 with open(self.file_name, "w") as f: |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
259 f.write(self.module_source) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
260 sys.path.insert(0, self.dir_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
261 |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
262 def tearDown(self): |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
263 sys.path[:] = self.sys_path |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
264 if self.orig_module is not None: |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
265 sys.modules[self.module_name] = self.orig_module |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
266 else: |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
267 del sys.modules[self.module_name] |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
268 for file_name in self.file_name, self.compiled_name: |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
269 if os.path.exists(file_name): |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
270 os.remove(file_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
271 if os.path.exists(self.dir_name): |
|
50786
2d453b221bec
Merged revisions 68292,68344,68361,68378,68424,68426,68429-68430,68450,68457,68480-68481,68493,68495,68499,68501,68512,68514-68515 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50618
diff
changeset
|
272 shutil.rmtree(self.dir_name) |
|
50618
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
273 |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
274 def import_module(self): |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
275 ns = globals() |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
276 __import__(self.module_name, ns, ns) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
277 return sys.modules[self.module_name] |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
278 |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
279 def test_basics(self): |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
280 mod = self.import_module() |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
281 self.assertEqual(mod.module_filename, self.file_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
282 self.assertEqual(mod.code_filename, self.file_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
283 self.assertEqual(mod.func_filename, self.file_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
284 del sys.modules[self.module_name] |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
285 mod = self.import_module() |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
286 self.assertEqual(mod.module_filename, self.compiled_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
287 self.assertEqual(mod.code_filename, self.file_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
288 self.assertEqual(mod.func_filename, self.file_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
289 |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
290 def test_incorrect_code_name(self): |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
291 py_compile.compile(self.file_name, dfile="another_module.py") |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
292 mod = self.import_module() |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
293 self.assertEqual(mod.module_filename, self.compiled_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
294 self.assertEqual(mod.code_filename, self.file_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
295 self.assertEqual(mod.func_filename, self.file_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
296 |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
297 def test_module_without_source(self): |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
298 target = "another_module.py" |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
299 py_compile.compile(self.file_name, dfile=target) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
300 os.remove(self.file_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
301 mod = self.import_module() |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
302 self.assertEqual(mod.module_filename, self.compiled_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
303 self.assertEqual(mod.code_filename, target) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
304 self.assertEqual(mod.func_filename, target) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
305 |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
306 def test_foreign_code(self): |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
307 py_compile.compile(self.file_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
308 with open(self.compiled_name, "rb") as f: |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
309 header = f.read(8) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
310 code = marshal.load(f) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
311 constants = list(code.co_consts) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
312 foreign_code = test_main.func_code |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
313 pos = constants.index(1) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
314 constants[pos] = foreign_code |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
315 code = type(code)(code.co_argcount, code.co_nlocals, code.co_stacksize, |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
316 code.co_flags, code.co_code, tuple(constants), |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
317 code.co_names, code.co_varnames, code.co_filename, |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
318 code.co_name, code.co_firstlineno, code.co_lnotab, |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
319 code.co_freevars, code.co_cellvars) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
320 with open(self.compiled_name, "wb") as f: |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
321 f.write(header) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
322 marshal.dump(code, f) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
323 mod = self.import_module() |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
324 self.assertEqual(mod.constant.co_filename, foreign_code.co_filename) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
325 |
|
43736
e8d003b3870f
Removed non ASCII text from test as requested by Guido. Sorry :/
Christian Heimes <christian@cheimes.de>
parents:
43735
diff
changeset
|
326 class PathsTests(unittest.TestCase): |
|
43735
bd2f8bb11bcc
Backport of import tests for bug http://bugs.python.org/issue1293 and bug http://bugs.python.org/issue1342
Christian Heimes <christian@cheimes.de>
parents:
42584
diff
changeset
|
327 path = TESTFN |
|
bd2f8bb11bcc
Backport of import tests for bug http://bugs.python.org/issue1293 and bug http://bugs.python.org/issue1342
Christian Heimes <christian@cheimes.de>
parents:
42584
diff
changeset
|
328 |
|
bd2f8bb11bcc
Backport of import tests for bug http://bugs.python.org/issue1293 and bug http://bugs.python.org/issue1342
Christian Heimes <christian@cheimes.de>
parents:
42584
diff
changeset
|
329 def setUp(self): |
|
bd2f8bb11bcc
Backport of import tests for bug http://bugs.python.org/issue1293 and bug http://bugs.python.org/issue1342
Christian Heimes <christian@cheimes.de>
parents:
42584
diff
changeset
|
330 os.mkdir(self.path) |
|
bd2f8bb11bcc
Backport of import tests for bug http://bugs.python.org/issue1293 and bug http://bugs.python.org/issue1342
Christian Heimes <christian@cheimes.de>
parents:
42584
diff
changeset
|
331 self.syspath = sys.path[:] |
|
bd2f8bb11bcc
Backport of import tests for bug http://bugs.python.org/issue1293 and bug http://bugs.python.org/issue1342
Christian Heimes <christian@cheimes.de>
parents:
42584
diff
changeset
|
332 |
|
bd2f8bb11bcc
Backport of import tests for bug http://bugs.python.org/issue1293 and bug http://bugs.python.org/issue1342
Christian Heimes <christian@cheimes.de>
parents:
42584
diff
changeset
|
333 def tearDown(self): |
|
bd2f8bb11bcc
Backport of import tests for bug http://bugs.python.org/issue1293 and bug http://bugs.python.org/issue1342
Christian Heimes <christian@cheimes.de>
parents:
42584
diff
changeset
|
334 shutil.rmtree(self.path) |
|
bd2f8bb11bcc
Backport of import tests for bug http://bugs.python.org/issue1293 and bug http://bugs.python.org/issue1342
Christian Heimes <christian@cheimes.de>
parents:
42584
diff
changeset
|
335 sys.path = self.syspath |
|
bd2f8bb11bcc
Backport of import tests for bug http://bugs.python.org/issue1293 and bug http://bugs.python.org/issue1342
Christian Heimes <christian@cheimes.de>
parents:
42584
diff
changeset
|
336 |
|
bd2f8bb11bcc
Backport of import tests for bug http://bugs.python.org/issue1293 and bug http://bugs.python.org/issue1342
Christian Heimes <christian@cheimes.de>
parents:
42584
diff
changeset
|
337 # http://bugs.python.org/issue1293 |
|
bd2f8bb11bcc
Backport of import tests for bug http://bugs.python.org/issue1293 and bug http://bugs.python.org/issue1342
Christian Heimes <christian@cheimes.de>
parents:
42584
diff
changeset
|
338 def test_trailing_slash(self): |
|
bd2f8bb11bcc
Backport of import tests for bug http://bugs.python.org/issue1293 and bug http://bugs.python.org/issue1342
Christian Heimes <christian@cheimes.de>
parents:
42584
diff
changeset
|
339 f = open(os.path.join(self.path, 'test_trailing_slash.py'), 'w') |
|
bd2f8bb11bcc
Backport of import tests for bug http://bugs.python.org/issue1293 and bug http://bugs.python.org/issue1342
Christian Heimes <christian@cheimes.de>
parents:
42584
diff
changeset
|
340 f.write("testdata = 'test_trailing_slash'") |
|
bd2f8bb11bcc
Backport of import tests for bug http://bugs.python.org/issue1293 and bug http://bugs.python.org/issue1342
Christian Heimes <christian@cheimes.de>
parents:
42584
diff
changeset
|
341 f.close() |
|
bd2f8bb11bcc
Backport of import tests for bug http://bugs.python.org/issue1293 and bug http://bugs.python.org/issue1342
Christian Heimes <christian@cheimes.de>
parents:
42584
diff
changeset
|
342 sys.path.append(self.path+'/') |
|
bd2f8bb11bcc
Backport of import tests for bug http://bugs.python.org/issue1293 and bug http://bugs.python.org/issue1342
Christian Heimes <christian@cheimes.de>
parents:
42584
diff
changeset
|
343 mod = __import__("test_trailing_slash") |
|
bd2f8bb11bcc
Backport of import tests for bug http://bugs.python.org/issue1293 and bug http://bugs.python.org/issue1342
Christian Heimes <christian@cheimes.de>
parents:
42584
diff
changeset
|
344 self.assertEqual(mod.testdata, 'test_trailing_slash') |
|
bd2f8bb11bcc
Backport of import tests for bug http://bugs.python.org/issue1293 and bug http://bugs.python.org/issue1342
Christian Heimes <christian@cheimes.de>
parents:
42584
diff
changeset
|
345 unload("test_trailing_slash") |
|
bd2f8bb11bcc
Backport of import tests for bug http://bugs.python.org/issue1293 and bug http://bugs.python.org/issue1342
Christian Heimes <christian@cheimes.de>
parents:
42584
diff
changeset
|
346 |
|
50998
bd74e866f9ca
Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50786
diff
changeset
|
347 # http://bugs.python.org/issue3677 |
|
bd74e866f9ca
Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50786
diff
changeset
|
348 def _test_UNC_path(self): |
|
bd74e866f9ca
Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50786
diff
changeset
|
349 f = open(os.path.join(self.path, 'test_trailing_slash.py'), 'w') |
|
bd74e866f9ca
Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50786
diff
changeset
|
350 f.write("testdata = 'test_trailing_slash'") |
|
bd74e866f9ca
Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50786
diff
changeset
|
351 f.close() |
|
bd74e866f9ca
Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50786
diff
changeset
|
352 #create the UNC path, like \\myhost\c$\foo\bar |
|
bd74e866f9ca
Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50786
diff
changeset
|
353 path = os.path.abspath(self.path) |
|
bd74e866f9ca
Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50786
diff
changeset
|
354 import socket |
|
bd74e866f9ca
Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50786
diff
changeset
|
355 hn = socket.gethostname() |
|
bd74e866f9ca
Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50786
diff
changeset
|
356 drive = path[0] |
|
bd74e866f9ca
Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50786
diff
changeset
|
357 unc = "\\\\%s\\%s$"%(hn, drive) |
|
bd74e866f9ca
Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50786
diff
changeset
|
358 unc += path[2:] |
|
bd74e866f9ca
Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50786
diff
changeset
|
359 sys.path.append(path) |
|
bd74e866f9ca
Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50786
diff
changeset
|
360 mod = __import__("test_trailing_slash") |
|
bd74e866f9ca
Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50786
diff
changeset
|
361 self.assertEqual(mod.testdata, 'test_trailing_slash') |
|
bd74e866f9ca
Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50786
diff
changeset
|
362 unload("test_trailing_slash") |
|
bd74e866f9ca
Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50786
diff
changeset
|
363 |
|
bd74e866f9ca
Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50786
diff
changeset
|
364 if sys.platform == "win32": |
|
bd74e866f9ca
Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50786
diff
changeset
|
365 test_UNC_path = _test_UNC_path |
|
bd74e866f9ca
Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50786
diff
changeset
|
366 |
|
bd74e866f9ca
Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50786
diff
changeset
|
367 |
|
45905
2a01075cd902
Issue #2400: Allow relative imports to "import *".
Martin v. Löwis <martin@v.loewis.de>
parents:
44614
diff
changeset
|
368 class RelativeImport(unittest.TestCase): |
|
2a01075cd902
Issue #2400: Allow relative imports to "import *".
Martin v. Löwis <martin@v.loewis.de>
parents:
44614
diff
changeset
|
369 def tearDown(self): |
|
2a01075cd902
Issue #2400: Allow relative imports to "import *".
Martin v. Löwis <martin@v.loewis.de>
parents:
44614
diff
changeset
|
370 try: |
|
2a01075cd902
Issue #2400: Allow relative imports to "import *".
Martin v. Löwis <martin@v.loewis.de>
parents:
44614
diff
changeset
|
371 del sys.modules["test.relimport"] |
|
2a01075cd902
Issue #2400: Allow relative imports to "import *".
Martin v. Löwis <martin@v.loewis.de>
parents:
44614
diff
changeset
|
372 except: |
|
2a01075cd902
Issue #2400: Allow relative imports to "import *".
Martin v. Löwis <martin@v.loewis.de>
parents:
44614
diff
changeset
|
373 pass |
|
2a01075cd902
Issue #2400: Allow relative imports to "import *".
Martin v. Löwis <martin@v.loewis.de>
parents:
44614
diff
changeset
|
374 |
|
2a01075cd902
Issue #2400: Allow relative imports to "import *".
Martin v. Löwis <martin@v.loewis.de>
parents:
44614
diff
changeset
|
375 def test_relimport_star(self): |
|
2a01075cd902
Issue #2400: Allow relative imports to "import *".
Martin v. Löwis <martin@v.loewis.de>
parents:
44614
diff
changeset
|
376 # This will import * from .test_import. |
|
45912
422b49dfd15b
Import relimport using a relative import.
Martin v. Löwis <martin@v.loewis.de>
parents:
45905
diff
changeset
|
377 from . import relimport |
|
45905
2a01075cd902
Issue #2400: Allow relative imports to "import *".
Martin v. Löwis <martin@v.loewis.de>
parents:
44614
diff
changeset
|
378 self.assertTrue(hasattr(relimport, "RelativeImport")) |
|
2a01075cd902
Issue #2400: Allow relative imports to "import *".
Martin v. Löwis <martin@v.loewis.de>
parents:
44614
diff
changeset
|
379 |
|
47944
b90752c5e8c1
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)
Nick Coghlan <ncoghlan@gmail.com>
parents:
46565
diff
changeset
|
380 def test_issue3221(self): |
|
b90752c5e8c1
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)
Nick Coghlan <ncoghlan@gmail.com>
parents:
46565
diff
changeset
|
381 def check_absolute(): |
|
b90752c5e8c1
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)
Nick Coghlan <ncoghlan@gmail.com>
parents:
46565
diff
changeset
|
382 exec "from os import path" in ns |
|
b90752c5e8c1
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)
Nick Coghlan <ncoghlan@gmail.com>
parents:
46565
diff
changeset
|
383 def check_relative(): |
|
b90752c5e8c1
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)
Nick Coghlan <ncoghlan@gmail.com>
parents:
46565
diff
changeset
|
384 exec "from . import relimport" in ns |
|
b90752c5e8c1
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)
Nick Coghlan <ncoghlan@gmail.com>
parents:
46565
diff
changeset
|
385 # Check both OK with __package__ and __name__ correct |
|
b90752c5e8c1
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)
Nick Coghlan <ncoghlan@gmail.com>
parents:
46565
diff
changeset
|
386 ns = dict(__package__='test', __name__='test.notarealmodule') |
|
b90752c5e8c1
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)
Nick Coghlan <ncoghlan@gmail.com>
parents:
46565
diff
changeset
|
387 check_absolute() |
|
b90752c5e8c1
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)
Nick Coghlan <ncoghlan@gmail.com>
parents:
46565
diff
changeset
|
388 check_relative() |
|
b90752c5e8c1
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)
Nick Coghlan <ncoghlan@gmail.com>
parents:
46565
diff
changeset
|
389 # Check both OK with only __name__ wrong |
|
b90752c5e8c1
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)
Nick Coghlan <ncoghlan@gmail.com>
parents:
46565
diff
changeset
|
390 ns = dict(__package__='test', __name__='notarealpkg.notarealmodule') |
|
b90752c5e8c1
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)
Nick Coghlan <ncoghlan@gmail.com>
parents:
46565
diff
changeset
|
391 check_absolute() |
|
b90752c5e8c1
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)
Nick Coghlan <ncoghlan@gmail.com>
parents:
46565
diff
changeset
|
392 check_relative() |
|
b90752c5e8c1
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)
Nick Coghlan <ncoghlan@gmail.com>
parents:
46565
diff
changeset
|
393 # Check relative fails with only __package__ wrong |
|
b90752c5e8c1
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)
Nick Coghlan <ncoghlan@gmail.com>
parents:
46565
diff
changeset
|
394 ns = dict(__package__='foo', __name__='test.notarealmodule') |
|
49146
f28b999a69f8
Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents:
49099
diff
changeset
|
395 with check_warnings() as w: |
|
47944
b90752c5e8c1
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)
Nick Coghlan <ncoghlan@gmail.com>
parents:
46565
diff
changeset
|
396 check_absolute() |
|
49146
f28b999a69f8
Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents:
49099
diff
changeset
|
397 self.assert_('foo' in str(w.message)) |
|
f28b999a69f8
Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents:
49099
diff
changeset
|
398 self.assertEqual(w.category, RuntimeWarning) |
|
47944
b90752c5e8c1
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)
Nick Coghlan <ncoghlan@gmail.com>
parents:
46565
diff
changeset
|
399 self.assertRaises(SystemError, check_relative) |
|
b90752c5e8c1
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)
Nick Coghlan <ncoghlan@gmail.com>
parents:
46565
diff
changeset
|
400 # Check relative fails with __package__ and __name__ wrong |
|
b90752c5e8c1
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)
Nick Coghlan <ncoghlan@gmail.com>
parents:
46565
diff
changeset
|
401 ns = dict(__package__='foo', __name__='notarealpkg.notarealmodule') |
|
49146
f28b999a69f8
Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents:
49099
diff
changeset
|
402 with check_warnings() as w: |
|
47944
b90752c5e8c1
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)
Nick Coghlan <ncoghlan@gmail.com>
parents:
46565
diff
changeset
|
403 check_absolute() |
|
49146
f28b999a69f8
Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents:
49099
diff
changeset
|
404 self.assert_('foo' in str(w.message)) |
|
f28b999a69f8
Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents:
49099
diff
changeset
|
405 self.assertEqual(w.category, RuntimeWarning) |
|
47944
b90752c5e8c1
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)
Nick Coghlan <ncoghlan@gmail.com>
parents:
46565
diff
changeset
|
406 self.assertRaises(SystemError, check_relative) |
|
b90752c5e8c1
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)
Nick Coghlan <ncoghlan@gmail.com>
parents:
46565
diff
changeset
|
407 # Check both fail with package set to a non-string |
|
b90752c5e8c1
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)
Nick Coghlan <ncoghlan@gmail.com>
parents:
46565
diff
changeset
|
408 ns = dict(__package__=object()) |
|
b90752c5e8c1
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)
Nick Coghlan <ncoghlan@gmail.com>
parents:
46565
diff
changeset
|
409 self.assertRaises(ValueError, check_absolute) |
|
b90752c5e8c1
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)
Nick Coghlan <ncoghlan@gmail.com>
parents:
46565
diff
changeset
|
410 self.assertRaises(ValueError, check_relative) |
|
b90752c5e8c1
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)
Nick Coghlan <ncoghlan@gmail.com>
parents:
46565
diff
changeset
|
411 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
412 def test_main(verbose=None): |
|
50618
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
413 run_unittest(ImportTest, TestPycRewriting, PathsTests, RelativeImport) |
|
38018
b5e7d5b3c442
- Add new Warning class, ImportWarning
Thomas Wouters <thomas@python.org>
parents:
37408
diff
changeset
|
414 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
415 if __name__ == '__main__': |
|
46565
0e1185bfa313
Allow test_import to work when it is invoked directly
Benjamin Peterson <benjamin@python.org>
parents:
45912
diff
changeset
|
416 # test needs to be a package, so we can do relative import |
|
0e1185bfa313
Allow test_import to work when it is invoked directly
Benjamin Peterson <benjamin@python.org>
parents:
45912
diff
changeset
|
417 from test.test_import import test_main |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
418 test_main() |
