Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_import.py @ 58925:eecf1bef052d 2.6
Merged revisions 78207 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line
#7930: fix stripid
........
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Tue, 16 Feb 2010 23:29:44 +0000 |
| parents | f20705875e80 |
| children | 55e2368f72f2 |
| 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 |
|
55330
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
3 import stat |
|
15057
bf27caba3d0e
Adding Jeremy's new test_import (SF patch 101709).
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4 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
|
5 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
|
6 import sys |
|
23851
d6ae544acd24
Fix SF bug # 561858 Assertion with very long lists
Neal Norwitz <nnorwitz@gmail.com>
parents:
23584
diff
changeset
|
7 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
|
8 import warnings |
|
50618
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
9 import marshal |
|
55330
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
10 from test.test_support import (unlink, TESTFN, unload, run_unittest, |
|
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
11 check_warnings, TestFailed) |
|
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
|
12 |
|
17707
fe4c1034b480
SF bug #422177: Results from .pyc differs from .py
Tim Peters <tim.peters@gmail.com>
parents:
17095
diff
changeset
|
13 |
| 32884 | 14 def remove_files(name): |
| 15 for f in (name + os.extsep + "py", | |
| 16 name + os.extsep + "pyc", | |
| 17 name + os.extsep + "pyo", | |
| 18 name + os.extsep + "pyw", | |
| 19 name + "$py.class"): | |
| 20 if os.path.exists(f): | |
| 21 os.remove(f) | |
| 22 | |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
23 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
24 class ImportTest(unittest.TestCase): |
|
40144
6609f020649f
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
39943
diff
changeset
|
25 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
26 def testCaseSensitivity(self): |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
27 # 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
|
28 # far, we know for sure that "random" exists. |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
29 try: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
30 import RAnDoM |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
31 except ImportError: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
32 pass |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
33 else: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
34 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
|
35 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
36 def testDoubleConst(self): |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
37 # 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
|
38 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
|
39 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
40 def testImport(self): |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
41 def test_with_extension(ext): |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
42 # ext normally ".py"; perhaps ".pyw" |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
43 source = TESTFN + ext |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
44 pyo = TESTFN + os.extsep + "pyo" |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
45 if sys.platform.startswith('java'): |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
46 pyc = TESTFN + "$py.class" |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
47 else: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
48 pyc = TESTFN + os.extsep + "pyc" |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
49 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
50 f = open(source, "w") |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
51 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
|
52 a = random.randrange(1000) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
53 b = random.randrange(1000) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
54 print >> f, "a =", a |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
55 print >> f, "b =", b |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
56 f.close() |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
57 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
58 try: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
59 try: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
60 mod = __import__(TESTFN) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
61 except ImportError, err: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
62 self.fail("import from %s failed: %s" % (ext, err)) |
|
40144
6609f020649f
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
39943
diff
changeset
|
63 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
64 self.assertEquals(mod.a, a, |
|
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 self.assertEquals(mod.b, b, |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
67 "module loaded (%s) but contents invalid" % mod) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
68 finally: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
69 os.unlink(source) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
70 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
71 try: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
72 try: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
73 reload(mod) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
74 except ImportError, err: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
75 self.fail("import from .pyc/.pyo failed: %s" % err) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
76 finally: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
77 try: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
78 os.unlink(pyc) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
79 except OSError: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
80 pass |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
81 try: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
82 os.unlink(pyo) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
83 except OSError: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
84 pass |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
85 del sys.modules[TESTFN] |
|
15057
bf27caba3d0e
Adding Jeremy's new test_import (SF patch 101709).
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
86 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
87 sys.path.insert(0, os.curdir) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
88 try: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
89 test_with_extension(os.extsep + "py") |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
90 if sys.platform.startswith("win"): |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
91 for ext in ".PY", ".Py", ".pY", ".pyw", ".PYW", ".pYw": |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
92 test_with_extension(ext) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
93 finally: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
94 del sys.path[0] |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
95 |
|
55330
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
96 if os.name == 'posix': |
|
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
97 def test_execute_bit_not_copied(self): |
|
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
98 # Issue 6070: under posix .pyc files got their execute bit set if |
|
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
99 # the .py file had the execute bit set, but they aren't executable. |
|
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
100 oldmask = os.umask(022) |
|
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
101 sys.path.insert(0, os.curdir) |
|
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
102 try: |
|
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
103 fname = TESTFN + os.extsep + "py" |
|
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
104 f = open(fname, 'w').close() |
|
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
105 os.chmod(fname, (stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH | |
|
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
106 stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)) |
|
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
107 __import__(TESTFN) |
|
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
108 fn = fname + 'c' |
|
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
109 if not os.path.exists(fn): |
|
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
110 fn = fname + 'o' |
|
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
111 if not os.path.exists(fn): raise TestFailed("__import__ did " |
|
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
112 "not result in creation of either a .pyc or .pyo file") |
|
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
113 s = os.stat(fn) |
|
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
114 self.assertEquals(stat.S_IMODE(s.st_mode), |
|
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
115 stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) |
|
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
116 finally: |
|
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
117 os.umask(oldmask) |
|
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
118 remove_files(TESTFN) |
|
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
119 if TESTFN in sys.modules: del sys.modules[TESTFN] |
|
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
120 del sys.path[0] |
|
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
121 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
122 def testImpModule(self): |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
123 # 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
|
124 import imp |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
125 x = imp.find_module("os") |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
126 os = imp.load_module("os", *x) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
127 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
128 def test_module_with_large_stack(self, module='longlist'): |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
129 # 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
|
130 filename = module + os.extsep + 'py' |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
131 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
132 # create a file with a list of 65000 elements |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
133 f = open(filename, 'w+') |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
134 f.write('d = [\n') |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
135 for i in range(65000): |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
136 f.write('"",\n') |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
137 f.write(']') |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
138 f.close() |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
139 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
140 # 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
|
141 f = open(filename, 'r') |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
142 py_compile.compile(filename) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
143 f.close() |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
144 os.unlink(filename) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
145 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
146 # need to be able to load from current dir |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
147 sys.path.append('') |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
148 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
149 # this used to crash |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
150 exec 'import ' + module |
|
15057
bf27caba3d0e
Adding Jeremy's new test_import (SF patch 101709).
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
151 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
152 # cleanup |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
153 del sys.path[-1] |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
154 for ext in 'pyc', 'pyo': |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
155 fname = module + os.extsep + ext |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
156 if os.path.exists(fname): |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
157 os.unlink(fname) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
158 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
159 def test_failing_import_sticks(self): |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
160 source = TESTFN + os.extsep + "py" |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
161 f = open(source, "w") |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
162 print >> f, "a = 1/0" |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
163 f.close() |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
164 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
165 # 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
|
166 # we try. |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
167 sys.path.insert(0, os.curdir) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
168 try: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
169 for i in 1, 2, 3: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
170 try: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
171 mod = __import__(TESTFN) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
172 except ZeroDivisionError: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
173 if TESTFN in sys.modules: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
174 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
|
175 else: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
176 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
|
177 finally: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
178 sys.path.pop(0) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
179 remove_files(TESTFN) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
180 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
181 def test_failing_reload(self): |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
182 # 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
|
183 source = TESTFN + os.extsep + "py" |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
184 f = open(source, "w") |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
185 print >> f, "a = 1" |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
186 print >> f, "b = 2" |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
187 f.close() |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
188 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
189 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
|
190 try: |
|
7fa1f87ca6b2
Derived from SF patch #446899 Permit import of .pyw under Windows, from
Tim Peters <tim.peters@gmail.com>
parents:
18930
diff
changeset
|
191 mod = __import__(TESTFN) |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
192 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
|
193 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
|
194 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
|
195 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
196 # 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
|
197 # 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
|
198 # 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
|
199 # compiled file too. |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
200 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
|
201 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
202 # Now damage the module. |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
203 f = open(source, "w") |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
204 print >> f, "a = 10" |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
205 print >> f, "b = 20//0" |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
206 f.close() |
|
23851
d6ae544acd24
Fix SF bug # 561858 Assertion with very long lists
Neal Norwitz <nnorwitz@gmail.com>
parents:
23584
diff
changeset
|
207 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
208 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
|
209 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
210 # 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
|
211 mod = sys.modules.get(TESTFN) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
212 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
|
213 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
214 # 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
|
215 # stick. |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
216 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
|
217 self.assertEquals(mod.b, 2, "module has wrong attribute values") |
| 32884 | 218 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
219 finally: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
220 sys.path.pop(0) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
221 remove_files(TESTFN) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
222 if TESTFN in sys.modules: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
223 del sys.modules[TESTFN] |
|
41133
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
41128
diff
changeset
|
224 |
|
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
|
225 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
|
226 # 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
|
227 # when faced with self-recursive reload()ing. |
|
41133
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
41128
diff
changeset
|
228 |
|
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
|
229 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
|
230 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
|
231 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
|
232 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
|
233 sys.path.pop(0) |
| 32884 | 234 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
235 def test_import_name_binding(self): |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
236 # 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
|
237 import test as x |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
238 import test.test_support |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
239 self.assert_(x is test, x.__name__) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
240 self.assert_(hasattr(test.test_support, "__file__")) |
| 32884 | 241 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
242 # 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
|
243 import test.test_support as y |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
244 self.assert_(y is test.test_support, y.__name__) |
| 32884 | 245 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
246 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
|
247 with warnings.catch_warnings(): |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
248 # 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
|
249 # 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
|
250 warnings.simplefilter('error', ImportWarning) |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
251 self.assertRaises(ImportWarning, __import__, "site-packages") |
|
35742
1c4d192f1568
Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
32884
diff
changeset
|
252 |
|
44614
dc9d95b717c5
Fixed #1776. __import__() no longer imports modules by file name
Christian Heimes <christian@cheimes.de>
parents:
43736
diff
changeset
|
253 def test_importbyfilename(self): |
|
dc9d95b717c5
Fixed #1776. __import__() no longer imports modules by file name
Christian Heimes <christian@cheimes.de>
parents:
43736
diff
changeset
|
254 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
|
255 try: |
|
dc9d95b717c5
Fixed #1776. __import__() no longer imports modules by file name
Christian Heimes <christian@cheimes.de>
parents:
43736
diff
changeset
|
256 __import__(path) |
|
dc9d95b717c5
Fixed #1776. __import__() no longer imports modules by file name
Christian Heimes <christian@cheimes.de>
parents:
43736
diff
changeset
|
257 except ImportError, err: |
|
dc9d95b717c5
Fixed #1776. __import__() no longer imports modules by file name
Christian Heimes <christian@cheimes.de>
parents:
43736
diff
changeset
|
258 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
|
259 err.args[0]) |
|
dc9d95b717c5
Fixed #1776. __import__() no longer imports modules by file name
Christian Heimes <christian@cheimes.de>
parents:
43736
diff
changeset
|
260 else: |
|
dc9d95b717c5
Fixed #1776. __import__() no longer imports modules by file name
Christian Heimes <christian@cheimes.de>
parents:
43736
diff
changeset
|
261 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
|
262 |
|
55330
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
263 |
|
50618
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
264 class TestPycRewriting(unittest.TestCase): |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
265 # 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
|
266 # 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
|
267 # and the .pyc file are renamed). |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
268 |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
269 module_name = "unlikely_module_name" |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
270 module_source = """ |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
271 import sys |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
272 code_filename = sys._getframe().f_code.co_filename |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
273 module_filename = __file__ |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
274 constant = 1 |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
275 def func(): |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
276 pass |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
277 func_filename = func.func_code.co_filename |
|
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 dir_name = os.path.abspath(TESTFN) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
280 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
|
281 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
|
282 |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
283 def setUp(self): |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
284 self.sys_path = sys.path[:] |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
285 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
|
286 os.mkdir(self.dir_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
287 with open(self.file_name, "w") as f: |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
288 f.write(self.module_source) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
289 sys.path.insert(0, self.dir_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
290 |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
291 def tearDown(self): |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
292 sys.path[:] = self.sys_path |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
293 if self.orig_module is not None: |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
294 sys.modules[self.module_name] = self.orig_module |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
295 else: |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
296 del sys.modules[self.module_name] |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
297 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
|
298 if os.path.exists(file_name): |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
299 os.remove(file_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
300 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
|
301 shutil.rmtree(self.dir_name) |
|
50618
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
302 |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
303 def import_module(self): |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
304 ns = globals() |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
305 __import__(self.module_name, ns, ns) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
306 return sys.modules[self.module_name] |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
307 |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
308 def test_basics(self): |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
309 mod = self.import_module() |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
310 self.assertEqual(mod.module_filename, self.file_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
311 self.assertEqual(mod.code_filename, self.file_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
312 self.assertEqual(mod.func_filename, self.file_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
313 del sys.modules[self.module_name] |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
314 mod = self.import_module() |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
315 self.assertEqual(mod.module_filename, self.compiled_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
316 self.assertEqual(mod.code_filename, self.file_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
317 self.assertEqual(mod.func_filename, self.file_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
318 |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
319 def test_incorrect_code_name(self): |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
320 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
|
321 mod = self.import_module() |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
322 self.assertEqual(mod.module_filename, self.compiled_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
323 self.assertEqual(mod.code_filename, self.file_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
324 self.assertEqual(mod.func_filename, self.file_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
325 |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
326 def test_module_without_source(self): |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
327 target = "another_module.py" |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
328 py_compile.compile(self.file_name, dfile=target) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
329 os.remove(self.file_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
330 mod = self.import_module() |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
331 self.assertEqual(mod.module_filename, self.compiled_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
332 self.assertEqual(mod.code_filename, target) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
333 self.assertEqual(mod.func_filename, target) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
334 |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
335 def test_foreign_code(self): |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
336 py_compile.compile(self.file_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
337 with open(self.compiled_name, "rb") as f: |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
338 header = f.read(8) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
339 code = marshal.load(f) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
340 constants = list(code.co_consts) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
341 foreign_code = test_main.func_code |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
342 pos = constants.index(1) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
343 constants[pos] = foreign_code |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
344 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
|
345 code.co_flags, code.co_code, tuple(constants), |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
346 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
|
347 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
|
348 code.co_freevars, code.co_cellvars) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
349 with open(self.compiled_name, "wb") as f: |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
350 f.write(header) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
351 marshal.dump(code, f) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
352 mod = self.import_module() |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
353 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
|
354 |
|
43736
e8d003b3870f
Removed non ASCII text from test as requested by Guido. Sorry :/
Christian Heimes <christian@cheimes.de>
parents:
43735
diff
changeset
|
355 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
|
356 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
|
357 |
|
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
|
358 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
|
359 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
|
360 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
|
361 |
|
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
|
362 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
|
363 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
|
364 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
|
365 |
|
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
|
366 # 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
|
367 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
|
368 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
|
369 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
|
370 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
|
371 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
|
372 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
|
373 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
|
374 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
|
375 |
|
45905
2a01075cd902
Issue #2400: Allow relative imports to "import *".
Martin v. Löwis <martin@v.loewis.de>
parents:
44614
diff
changeset
|
376 class RelativeImport(unittest.TestCase): |
|
2a01075cd902
Issue #2400: Allow relative imports to "import *".
Martin v. Löwis <martin@v.loewis.de>
parents:
44614
diff
changeset
|
377 def tearDown(self): |
|
2a01075cd902
Issue #2400: Allow relative imports to "import *".
Martin v. Löwis <martin@v.loewis.de>
parents:
44614
diff
changeset
|
378 try: |
|
2a01075cd902
Issue #2400: Allow relative imports to "import *".
Martin v. Löwis <martin@v.loewis.de>
parents:
44614
diff
changeset
|
379 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
|
380 except: |
|
2a01075cd902
Issue #2400: Allow relative imports to "import *".
Martin v. Löwis <martin@v.loewis.de>
parents:
44614
diff
changeset
|
381 pass |
|
2a01075cd902
Issue #2400: Allow relative imports to "import *".
Martin v. Löwis <martin@v.loewis.de>
parents:
44614
diff
changeset
|
382 |
|
2a01075cd902
Issue #2400: Allow relative imports to "import *".
Martin v. Löwis <martin@v.loewis.de>
parents:
44614
diff
changeset
|
383 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
|
384 # 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
|
385 from . import relimport |
|
45905
2a01075cd902
Issue #2400: Allow relative imports to "import *".
Martin v. Löwis <martin@v.loewis.de>
parents:
44614
diff
changeset
|
386 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
|
387 |
|
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
|
388 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
|
389 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
|
390 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
|
391 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
|
392 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
|
393 # 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
|
394 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
|
395 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
|
396 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
|
397 # 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
|
398 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
|
399 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
|
400 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
|
401 # 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
|
402 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
|
403 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
|
404 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
|
405 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
|
406 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
|
407 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
|
408 # 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
|
409 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
|
410 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
|
411 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
|
412 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
|
413 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
|
414 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
|
415 # 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
|
416 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
|
417 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
|
418 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
|
419 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
420 def test_main(verbose=None): |
|
50618
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
421 run_unittest(ImportTest, TestPycRewriting, PathsTests, RelativeImport) |
|
38018
b5e7d5b3c442
- Add new Warning class, ImportWarning
Thomas Wouters <thomas@python.org>
parents:
37408
diff
changeset
|
422 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
423 if __name__ == '__main__': |
|
46565
0e1185bfa313
Allow test_import to work when it is invoked directly
Benjamin Peterson <benjamin@python.org>
parents:
45912
diff
changeset
|
424 # 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
|
425 from test.test_import import test_main |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
426 test_main() |
