Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_import.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | 7eac87fa7a06 |
| children | 321414874b26 |
| rev | line source |
|---|---|
|
74601
dd1b4fc2d736
Skip test failure under OpenIndiana
Antoine Pitrou <solipsis@pitrou.net>
parents:
74600
diff
changeset
|
1 import errno |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
2 import imp |
|
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
3 import marshal |
|
15057
bf27caba3d0e
Adding Jeremy's new test_import (SF patch 101709).
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4 import os |
|
23851
d6ae544acd24
Fix SF bug # 561858 Assertion with very long lists
Neal Norwitz <nnorwitz@gmail.com>
parents:
23584
diff
changeset
|
5 import py_compile |
|
15057
bf27caba3d0e
Adding Jeremy's new test_import (SF patch 101709).
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
6 import random |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
7 import stat |
|
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
|
8 import sys |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
9 import unittest |
|
75493
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
10 import textwrap |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
11 import shutil |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
12 |
|
64070
e4432efc88b9
Merged revisions 84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents:
61773
diff
changeset
|
13 from test.test_support import (unlink, TESTFN, unload, run_unittest, rmtree, |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
14 is_jython, check_warnings, EnvironmentVarGuard) |
|
75500
f8efcca7e90f
Moved symlink support into its own module. Ported can_symlink from Python 3.2, skipping symlink test when it cannot be invoked (such as when the symlink privilege is not present).
Jason R. Coombs <jaraco@jaraco.com>
parents:
75495
diff
changeset
|
15 from test import symlink_support |
|
66912
4ee4086dd1b7
Merged revisions 87251 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
64072
diff
changeset
|
16 from test import script_helper |
|
17707
fe4c1034b480
SF bug #422177: Results from .pyc differs from .py
Tim Peters <tim.peters@gmail.com>
parents:
17095
diff
changeset
|
17 |
| 32884 | 18 def remove_files(name): |
| 19 for f in (name + os.extsep + "py", | |
| 20 name + os.extsep + "pyc", | |
| 21 name + os.extsep + "pyo", | |
| 22 name + os.extsep + "pyw", | |
| 23 name + "$py.class"): | |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
24 unlink(f) |
| 32884 | 25 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
26 |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
27 class ImportTests(unittest.TestCase): |
|
40144
6609f020649f
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
39943
diff
changeset
|
28 |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
29 def tearDown(self): |
|
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
30 unload(TESTFN) |
|
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
31 setUp = tearDown |
|
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
32 |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
33 def test_case_sensitivity(self): |
|
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
34 # Brief digression to test that import is case-sensitive: if we got |
|
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
35 # this far, we know for sure that "random" exists. |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
36 try: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
37 import RAnDoM |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
38 except ImportError: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
39 pass |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
40 else: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
41 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
|
42 |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
43 def test_double_const(self): |
|
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
44 # Another brief digression to test the accuracy of manifest float |
|
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
45 # constants. |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
46 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
|
47 |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
48 def test_import(self): |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
49 def test_with_extension(ext): |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
50 # The extension is normally ".py", perhaps ".pyw". |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
51 source = TESTFN + ext |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
52 pyo = TESTFN + os.extsep + "pyo" |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
53 if is_jython: |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
54 pyc = TESTFN + "$py.class" |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
55 else: |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
56 pyc = TESTFN + os.extsep + "pyc" |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
57 |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
58 with open(source, "w") as f: |
|
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
59 print >> f, ("# This tests Python's ability to import a", ext, |
|
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
60 "file.") |
|
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
61 a = random.randrange(1000) |
|
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
62 b = random.randrange(1000) |
|
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
63 print >> f, "a =", a |
|
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
64 print >> f, "b =", b |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
65 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
66 try: |
|
63556
dba99828d91c
Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
61774
diff
changeset
|
67 mod = __import__(TESTFN) |
|
dba99828d91c
Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
61774
diff
changeset
|
68 except ImportError, err: |
|
dba99828d91c
Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
61774
diff
changeset
|
69 self.fail("import from %s failed: %s" % (ext, err)) |
|
dba99828d91c
Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
61774
diff
changeset
|
70 else: |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
71 self.assertEqual(mod.a, a, |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
72 "module loaded (%s) but contents invalid" % mod) |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
73 self.assertEqual(mod.b, b, |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
74 "module loaded (%s) but contents invalid" % mod) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
75 finally: |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
76 unlink(source) |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
77 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
78 try: |
|
63556
dba99828d91c
Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
61774
diff
changeset
|
79 imp.reload(mod) |
|
dba99828d91c
Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
61774
diff
changeset
|
80 except ImportError, err: |
|
dba99828d91c
Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
61774
diff
changeset
|
81 self.fail("import from .pyc/.pyo failed: %s" % err) |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
82 finally: |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
83 unlink(pyc) |
|
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
84 unlink(pyo) |
|
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
85 unload(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"): |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
91 for ext in [".PY", ".Py", ".pY", ".pyw", ".PYW", ".pYw"]: |
|
39943
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] |
|
15057
bf27caba3d0e
Adding Jeremy's new test_import (SF patch 101709).
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
95 |
|
55132
e9fc8947c49a
Issue 6070: when creating a compiled file, after copying the mode bits, on
R. David Murray <rdmurray@bitdance.com>
parents:
55002
diff
changeset
|
96 @unittest.skipUnless(os.name == 'posix', "test meaningful only on posix systems") |
|
e9fc8947c49a
Issue 6070: when creating a compiled file, after copying the mode bits, on
R. David Murray <rdmurray@bitdance.com>
parents:
55002
diff
changeset
|
97 def test_execute_bit_not_copied(self): |
|
e9fc8947c49a
Issue 6070: when creating a compiled file, after copying the mode bits, on
R. David Murray <rdmurray@bitdance.com>
parents:
55002
diff
changeset
|
98 # Issue 6070: under posix .pyc files got their execute bit set if |
|
e9fc8947c49a
Issue 6070: when creating a compiled file, after copying the mode bits, on
R. David Murray <rdmurray@bitdance.com>
parents:
55002
diff
changeset
|
99 # the .py file had the execute bit set, but they aren't executable. |
|
55167
4f1b2a457e83
Specify umask in execute bit test to get consistent results
R. David Murray <rdmurray@bitdance.com>
parents:
55162
diff
changeset
|
100 oldmask = os.umask(022) |
|
55168
0f065b48acf9
Curdir needs to be in the path for the test to work on all buildbots.
R. David Murray <rdmurray@bitdance.com>
parents:
55167
diff
changeset
|
101 sys.path.insert(0, os.curdir) |
|
55132
e9fc8947c49a
Issue 6070: when creating a compiled file, after copying the mode bits, on
R. David Murray <rdmurray@bitdance.com>
parents:
55002
diff
changeset
|
102 try: |
|
e9fc8947c49a
Issue 6070: when creating a compiled file, after copying the mode bits, on
R. David Murray <rdmurray@bitdance.com>
parents:
55002
diff
changeset
|
103 fname = TESTFN + os.extsep + "py" |
|
e9fc8947c49a
Issue 6070: when creating a compiled file, after copying the mode bits, on
R. David Murray <rdmurray@bitdance.com>
parents:
55002
diff
changeset
|
104 f = open(fname, 'w').close() |
|
e9fc8947c49a
Issue 6070: when creating a compiled file, after copying the mode bits, on
R. David Murray <rdmurray@bitdance.com>
parents:
55002
diff
changeset
|
105 os.chmod(fname, (stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH | |
|
e9fc8947c49a
Issue 6070: when creating a compiled file, after copying the mode bits, on
R. David Murray <rdmurray@bitdance.com>
parents:
55002
diff
changeset
|
106 stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)) |
|
e9fc8947c49a
Issue 6070: when creating a compiled file, after copying the mode bits, on
R. David Murray <rdmurray@bitdance.com>
parents:
55002
diff
changeset
|
107 __import__(TESTFN) |
|
55162
468a40adf56f
Make test work with -O.
R. David Murray <rdmurray@bitdance.com>
parents:
55161
diff
changeset
|
108 fn = fname + 'c' |
|
468a40adf56f
Make test work with -O.
R. David Murray <rdmurray@bitdance.com>
parents:
55161
diff
changeset
|
109 if not os.path.exists(fn): |
|
468a40adf56f
Make test work with -O.
R. David Murray <rdmurray@bitdance.com>
parents:
55161
diff
changeset
|
110 fn = fname + 'o' |
|
55330
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): |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
112 self.fail("__import__ did not result in creation of " |
|
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
113 "either a .pyc or .pyo file") |
|
55162
468a40adf56f
Make test work with -O.
R. David Murray <rdmurray@bitdance.com>
parents:
55161
diff
changeset
|
114 s = os.stat(fn) |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
115 self.assertEqual(stat.S_IMODE(s.st_mode), |
|
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
116 stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) |
|
55132
e9fc8947c49a
Issue 6070: when creating a compiled file, after copying the mode bits, on
R. David Murray <rdmurray@bitdance.com>
parents:
55002
diff
changeset
|
117 finally: |
|
55167
4f1b2a457e83
Specify umask in execute bit test to get consistent results
R. David Murray <rdmurray@bitdance.com>
parents:
55162
diff
changeset
|
118 os.umask(oldmask) |
|
55132
e9fc8947c49a
Issue 6070: when creating a compiled file, after copying the mode bits, on
R. David Murray <rdmurray@bitdance.com>
parents:
55002
diff
changeset
|
119 remove_files(TESTFN) |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
120 unload(TESTFN) |
|
55168
0f065b48acf9
Curdir needs to be in the path for the test to work on all buildbots.
R. David Murray <rdmurray@bitdance.com>
parents:
55167
diff
changeset
|
121 del sys.path[0] |
|
55132
e9fc8947c49a
Issue 6070: when creating a compiled file, after copying the mode bits, on
R. David Murray <rdmurray@bitdance.com>
parents:
55002
diff
changeset
|
122 |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
123 def test_imp_module(self): |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
124 # Verify that the imp module can correctly load and find .py files |
|
55330
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
125 |
|
56580
5e7c5c94c572
Using CleanImport to revert a reload of the os module doesn't work due to function registrations in copy_reg. The perils of reloading modules even for tests...
Nick Coghlan <ncoghlan@gmail.com>
parents:
56567
diff
changeset
|
126 # XXX (ncoghlan): It would be nice to use test_support.CleanImport |
|
5e7c5c94c572
Using CleanImport to revert a reload of the os module doesn't work due to function registrations in copy_reg. The perils of reloading modules even for tests...
Nick Coghlan <ncoghlan@gmail.com>
parents:
56567
diff
changeset
|
127 # here, but that breaks because the os module registers some |
|
5e7c5c94c572
Using CleanImport to revert a reload of the os module doesn't work due to function registrations in copy_reg. The perils of reloading modules even for tests...
Nick Coghlan <ncoghlan@gmail.com>
parents:
56567
diff
changeset
|
128 # handlers in copy_reg on import. Since CleanImport doesn't |
|
5e7c5c94c572
Using CleanImport to revert a reload of the os module doesn't work due to function registrations in copy_reg. The perils of reloading modules even for tests...
Nick Coghlan <ncoghlan@gmail.com>
parents:
56567
diff
changeset
|
129 # revert that registration, the module is left in a broken |
|
5e7c5c94c572
Using CleanImport to revert a reload of the os module doesn't work due to function registrations in copy_reg. The perils of reloading modules even for tests...
Nick Coghlan <ncoghlan@gmail.com>
parents:
56567
diff
changeset
|
130 # state after reversion. Reinitialising the module contents |
|
5e7c5c94c572
Using CleanImport to revert a reload of the os module doesn't work due to function registrations in copy_reg. The perils of reloading modules even for tests...
Nick Coghlan <ncoghlan@gmail.com>
parents:
56567
diff
changeset
|
131 # and just reverting os.environ to its previous state is an OK |
|
5e7c5c94c572
Using CleanImport to revert a reload of the os module doesn't work due to function registrations in copy_reg. The perils of reloading modules even for tests...
Nick Coghlan <ncoghlan@gmail.com>
parents:
56567
diff
changeset
|
132 # workaround |
|
5e7c5c94c572
Using CleanImport to revert a reload of the os module doesn't work due to function registrations in copy_reg. The perils of reloading modules even for tests...
Nick Coghlan <ncoghlan@gmail.com>
parents:
56567
diff
changeset
|
133 orig_path = os.path |
|
5e7c5c94c572
Using CleanImport to revert a reload of the os module doesn't work due to function registrations in copy_reg. The perils of reloading modules even for tests...
Nick Coghlan <ncoghlan@gmail.com>
parents:
56567
diff
changeset
|
134 orig_getenv = os.getenv |
|
5e7c5c94c572
Using CleanImport to revert a reload of the os module doesn't work due to function registrations in copy_reg. The perils of reloading modules even for tests...
Nick Coghlan <ncoghlan@gmail.com>
parents:
56567
diff
changeset
|
135 with EnvironmentVarGuard(): |
|
56567
993207dce395
Avoid replacing existing modules and sys.path in import tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55168
diff
changeset
|
136 x = imp.find_module("os") |
|
56580
5e7c5c94c572
Using CleanImport to revert a reload of the os module doesn't work due to function registrations in copy_reg. The perils of reloading modules even for tests...
Nick Coghlan <ncoghlan@gmail.com>
parents:
56567
diff
changeset
|
137 new_os = imp.load_module("os", *x) |
|
5e7c5c94c572
Using CleanImport to revert a reload of the os module doesn't work due to function registrations in copy_reg. The perils of reloading modules even for tests...
Nick Coghlan <ncoghlan@gmail.com>
parents:
56567
diff
changeset
|
138 self.assertIs(os, new_os) |
|
5e7c5c94c572
Using CleanImport to revert a reload of the os module doesn't work due to function registrations in copy_reg. The perils of reloading modules even for tests...
Nick Coghlan <ncoghlan@gmail.com>
parents:
56567
diff
changeset
|
139 self.assertIs(orig_path, new_os.path) |
|
5e7c5c94c572
Using CleanImport to revert a reload of the os module doesn't work due to function registrations in copy_reg. The perils of reloading modules even for tests...
Nick Coghlan <ncoghlan@gmail.com>
parents:
56567
diff
changeset
|
140 self.assertIsNot(orig_getenv, new_os.getenv) |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
141 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
142 def test_module_with_large_stack(self, module='longlist'): |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
143 # Regression test for http://bugs.python.org/issue561858. |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
144 filename = module + os.extsep + 'py' |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
145 |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
146 # Create a file with a list of 65000 elements. |
|
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
147 with open(filename, 'w+') as f: |
|
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
148 f.write('d = [\n') |
|
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
149 for i in range(65000): |
|
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
150 f.write('"",\n') |
|
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
151 f.write(']') |
|
15057
bf27caba3d0e
Adding Jeremy's new test_import (SF patch 101709).
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
152 |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
153 # Compile & remove .py file, we only need .pyc (or .pyo). |
|
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
154 with open(filename, 'r') as f: |
|
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
155 py_compile.compile(filename) |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
156 unlink(filename) |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
157 |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
158 # Need to be able to load from current dir. |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
159 sys.path.append('') |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
160 |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
161 # This used to crash. |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
162 exec 'import ' + module |
|
15057
bf27caba3d0e
Adding Jeremy's new test_import (SF patch 101709).
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
163 |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
164 # Cleanup. |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
165 del sys.path[-1] |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
166 unlink(filename + 'c') |
|
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
167 unlink(filename + 'o') |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
168 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
169 def test_failing_import_sticks(self): |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
170 source = TESTFN + os.extsep + "py" |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
171 with open(source, "w") as f: |
|
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
172 print >> f, "a = 1 // 0" |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
173 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
174 # 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
|
175 # we try. |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
176 sys.path.insert(0, os.curdir) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
177 try: |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
178 for i in [1, 2, 3]: |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
179 self.assertRaises(ZeroDivisionError, __import__, TESTFN) |
|
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
180 self.assertNotIn(TESTFN, sys.modules, |
|
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
181 "damaged module in sys.modules on %i try" % i) |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
182 finally: |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
183 del sys.path[0] |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
184 remove_files(TESTFN) |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
185 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
186 def test_failing_reload(self): |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
187 # 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
|
188 source = TESTFN + os.extsep + "py" |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
189 with open(source, "w") as f: |
|
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
190 print >> f, "a = 1" |
|
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
191 print >> f, "b = 2" |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
192 |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
193 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
|
194 try: |
|
7fa1f87ca6b2
Derived from SF patch #446899 Permit import of .pyw under Windows, from
Tim Peters <tim.peters@gmail.com>
parents:
18930
diff
changeset
|
195 mod = __import__(TESTFN) |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58198
diff
changeset
|
196 self.assertIn(TESTFN, sys.modules) |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
197 self.assertEqual(mod.a, 1, "module has wrong attribute values") |
|
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
198 self.assertEqual(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
|
199 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
200 # 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
|
201 # 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
|
202 # 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
|
203 # compiled file too. |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
204 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
|
205 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
206 # Now damage the module. |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
207 with open(source, "w") as f: |
|
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
208 print >> f, "a = 10" |
|
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
209 print >> f, "b = 20//0" |
|
23851
d6ae544acd24
Fix SF bug # 561858 Assertion with very long lists
Neal Norwitz <nnorwitz@gmail.com>
parents:
23584
diff
changeset
|
210 |
|
63556
dba99828d91c
Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
61774
diff
changeset
|
211 self.assertRaises(ZeroDivisionError, imp.reload, mod) |
|
15057
bf27caba3d0e
Adding Jeremy's new test_import (SF patch 101709).
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
212 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
213 # 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
|
214 mod = sys.modules.get(TESTFN) |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
215 self.assertIsNot(mod, None, "expected module to be in sys.modules") |
|
23851
d6ae544acd24
Fix SF bug # 561858 Assertion with very long lists
Neal Norwitz <nnorwitz@gmail.com>
parents:
23584
diff
changeset
|
216 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
217 # 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
|
218 # stick. |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
219 self.assertEqual(mod.a, 10, "module has wrong attribute values") |
|
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
220 self.assertEqual(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
|
221 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
222 finally: |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
223 del sys.path[0] |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
224 remove_files(TESTFN) |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
225 unload(TESTFN) |
|
41133
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
41128
diff
changeset
|
226 |
|
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
|
227 def test_infinite_reload(self): |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
228 # http://bugs.python.org/issue742342 reports that Python segfaults |
|
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
229 # (infinite recursion in C) when faced with self-recursive reload()ing. |
|
41133
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
41128
diff
changeset
|
230 |
|
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
|
231 sys.path.insert(0, os.path.dirname(__file__)) |
|
18992
7fa1f87ca6b2
Derived from SF patch #446899 Permit import of .pyw under Windows, from
Tim Peters <tim.peters@gmail.com>
parents:
18930
diff
changeset
|
232 try: |
|
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
|
233 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
|
234 finally: |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
235 del sys.path[0] |
| 32884 | 236 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
237 def test_import_name_binding(self): |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
238 # import x.y.z binds x in the current namespace. |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
239 import test as x |
|
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
240 import test.test_support |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
241 self.assertIs(x, test, x.__name__) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50998
diff
changeset
|
242 self.assertTrue(hasattr(test.test_support, "__file__")) |
| 32884 | 243 |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
244 # import x.y.z as w binds z as w. |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
245 import test.test_support as y |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
246 self.assertIs(y, test.test_support, y.__name__) |
|
18992
7fa1f87ca6b2
Derived from SF patch #446899 Permit import of .pyw under Windows, from
Tim Peters <tim.peters@gmail.com>
parents:
18930
diff
changeset
|
247 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
248 def test_import_initless_directory_warning(self): |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
249 with check_warnings(('', ImportWarning)): |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
250 # 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
|
251 # somewhere in sys.path... |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
252 self.assertRaises(ImportError, __import__, "site-packages") |
|
23584
c6af574bda29
Verify that the imp can find and load .py files.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20654
diff
changeset
|
253 |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
254 def test_import_by_filename(self): |
|
44614
dc9d95b717c5
Fixed #1776. __import__() no longer imports modules by file name
Christian Heimes <christian@cheimes.de>
parents:
43736
diff
changeset
|
255 path = os.path.abspath(TESTFN) |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
256 with self.assertRaises(ImportError) as c: |
|
44614
dc9d95b717c5
Fixed #1776. __import__() no longer imports modules by file name
Christian Heimes <christian@cheimes.de>
parents:
43736
diff
changeset
|
257 __import__(path) |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
258 self.assertEqual("Import by filename is not supported.", |
|
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
259 c.exception.args[0]) |
|
44614
dc9d95b717c5
Fixed #1776. __import__() no longer imports modules by file name
Christian Heimes <christian@cheimes.de>
parents:
43736
diff
changeset
|
260 |
|
66912
4ee4086dd1b7
Merged revisions 87251 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
64072
diff
changeset
|
261 def test_import_in_del_does_not_crash(self): |
|
4ee4086dd1b7
Merged revisions 87251 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
64072
diff
changeset
|
262 # Issue 4236 |
|
4ee4086dd1b7
Merged revisions 87251 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
64072
diff
changeset
|
263 testfn = script_helper.make_script('', TESTFN, textwrap.dedent("""\ |
|
4ee4086dd1b7
Merged revisions 87251 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
64072
diff
changeset
|
264 import sys |
|
4ee4086dd1b7
Merged revisions 87251 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
64072
diff
changeset
|
265 class C: |
|
4ee4086dd1b7
Merged revisions 87251 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
64072
diff
changeset
|
266 def __del__(self): |
|
4ee4086dd1b7
Merged revisions 87251 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
64072
diff
changeset
|
267 import imp |
|
4ee4086dd1b7
Merged revisions 87251 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
64072
diff
changeset
|
268 sys.argv.insert(0, C()) |
|
4ee4086dd1b7
Merged revisions 87251 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
64072
diff
changeset
|
269 """)) |
|
74114
ea72a1fc4b92
Fix test_import failure when run multiple times.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73550
diff
changeset
|
270 try: |
|
ea72a1fc4b92
Fix test_import failure when run multiple times.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73550
diff
changeset
|
271 script_helper.assert_python_ok(testfn) |
|
ea72a1fc4b92
Fix test_import failure when run multiple times.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73550
diff
changeset
|
272 finally: |
|
ea72a1fc4b92
Fix test_import failure when run multiple times.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73550
diff
changeset
|
273 unlink(testfn) |
|
23851
d6ae544acd24
Fix SF bug # 561858 Assertion with very long lists
Neal Norwitz <nnorwitz@gmail.com>
parents:
23584
diff
changeset
|
274 |
|
72456
0f5b64630fda
Issue #7732: Fix a crash on importing a module if a directory has the same name
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68256
diff
changeset
|
275 def test_bug7732(self): |
|
0f5b64630fda
Issue #7732: Fix a crash on importing a module if a directory has the same name
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68256
diff
changeset
|
276 source = TESTFN + '.py' |
|
0f5b64630fda
Issue #7732: Fix a crash on importing a module if a directory has the same name
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68256
diff
changeset
|
277 os.mkdir(source) |
|
0f5b64630fda
Issue #7732: Fix a crash on importing a module if a directory has the same name
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68256
diff
changeset
|
278 try: |
|
73550
555871844962
Issue #7732: Try to fix the a failing test on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents:
72456
diff
changeset
|
279 self.assertRaises((ImportError, IOError), |
|
555871844962
Issue #7732: Try to fix the a failing test on Windows
Victor Stinner <victor.stinner@haypocalc.com>
parents:
72456
diff
changeset
|
280 imp.find_module, TESTFN, ["."]) |
|
72456
0f5b64630fda
Issue #7732: Fix a crash on importing a module if a directory has the same name
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68256
diff
changeset
|
281 finally: |
|
0f5b64630fda
Issue #7732: Fix a crash on importing a module if a directory has the same name
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68256
diff
changeset
|
282 os.rmdir(source) |
|
0f5b64630fda
Issue #7732: Fix a crash on importing a module if a directory has the same name
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68256
diff
changeset
|
283 |
|
74598
0499eed74126
Issue #11235: Fix OverflowError when trying to import a source file whose modification time doesn't fit in a 32-bit timestamp.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74114
diff
changeset
|
284 def test_timestamp_overflow(self): |
|
0499eed74126
Issue #11235: Fix OverflowError when trying to import a source file whose modification time doesn't fit in a 32-bit timestamp.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74114
diff
changeset
|
285 # A modification timestamp larger than 2**32 should not be a problem |
|
0499eed74126
Issue #11235: Fix OverflowError when trying to import a source file whose modification time doesn't fit in a 32-bit timestamp.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74114
diff
changeset
|
286 # when importing a module (issue #11235). |
|
74600
5f7ad3c26677
Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents:
74598
diff
changeset
|
287 sys.path.insert(0, os.curdir) |
|
5f7ad3c26677
Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents:
74598
diff
changeset
|
288 try: |
|
5f7ad3c26677
Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents:
74598
diff
changeset
|
289 source = TESTFN + ".py" |
|
5f7ad3c26677
Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents:
74598
diff
changeset
|
290 compiled = source + ('c' if __debug__ else 'o') |
|
5f7ad3c26677
Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents:
74598
diff
changeset
|
291 with open(source, 'w') as f: |
|
5f7ad3c26677
Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents:
74598
diff
changeset
|
292 pass |
|
5f7ad3c26677
Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents:
74598
diff
changeset
|
293 try: |
|
74610
e8a525de2bac
Remove debug output, fix assert (hopefully) and exercise signedness issues a bit more.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74601
diff
changeset
|
294 os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5)) |
|
74600
5f7ad3c26677
Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents:
74598
diff
changeset
|
295 except OverflowError: |
|
5f7ad3c26677
Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents:
74598
diff
changeset
|
296 self.skipTest("cannot set modification time to large integer") |
|
74601
dd1b4fc2d736
Skip test failure under OpenIndiana
Antoine Pitrou <solipsis@pitrou.net>
parents:
74600
diff
changeset
|
297 except OSError as e: |
|
dd1b4fc2d736
Skip test failure under OpenIndiana
Antoine Pitrou <solipsis@pitrou.net>
parents:
74600
diff
changeset
|
298 if e.errno != getattr(errno, 'EOVERFLOW', None): |
|
dd1b4fc2d736
Skip test failure under OpenIndiana
Antoine Pitrou <solipsis@pitrou.net>
parents:
74600
diff
changeset
|
299 raise |
|
dd1b4fc2d736
Skip test failure under OpenIndiana
Antoine Pitrou <solipsis@pitrou.net>
parents:
74600
diff
changeset
|
300 self.skipTest("cannot set modification time to large integer ({})".format(e)) |
|
74600
5f7ad3c26677
Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents:
74598
diff
changeset
|
301 __import__(TESTFN) |
|
5f7ad3c26677
Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents:
74598
diff
changeset
|
302 # The pyc file was created. |
|
5f7ad3c26677
Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents:
74598
diff
changeset
|
303 os.stat(compiled) |
|
5f7ad3c26677
Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents:
74598
diff
changeset
|
304 finally: |
|
5f7ad3c26677
Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents:
74598
diff
changeset
|
305 del sys.path[0] |
|
5f7ad3c26677
Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents:
74598
diff
changeset
|
306 remove_files(TESTFN) |
|
74598
0499eed74126
Issue #11235: Fix OverflowError when trying to import a source file whose modification time doesn't fit in a 32-bit timestamp.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74114
diff
changeset
|
307 |
|
55330
f20705875e80
Merged revisions 73870,73879,73899-73900,73905-73906 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
50786
diff
changeset
|
308 |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
309 class PycRewritingTests(unittest.TestCase): |
|
50618
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
310 # 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
|
311 # 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
|
312 # and the .pyc file are renamed). |
|
23851
d6ae544acd24
Fix SF bug # 561858 Assertion with very long lists
Neal Norwitz <nnorwitz@gmail.com>
parents:
23584
diff
changeset
|
313 |
|
50618
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
314 module_name = "unlikely_module_name" |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
315 module_source = """ |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
316 import sys |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
317 code_filename = sys._getframe().f_code.co_filename |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
318 module_filename = __file__ |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
319 constant = 1 |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
320 def func(): |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
321 pass |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
322 func_filename = func.func_code.co_filename |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
323 """ |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
324 dir_name = os.path.abspath(TESTFN) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
325 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
|
326 compiled_name = file_name + ("c" if __debug__ else "o") |
|
23851
d6ae544acd24
Fix SF bug # 561858 Assertion with very long lists
Neal Norwitz <nnorwitz@gmail.com>
parents:
23584
diff
changeset
|
327 |
|
50618
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
328 def setUp(self): |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
329 self.sys_path = sys.path[:] |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
330 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
|
331 os.mkdir(self.dir_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
332 with open(self.file_name, "w") as f: |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
333 f.write(self.module_source) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
334 sys.path.insert(0, self.dir_name) |
|
23851
d6ae544acd24
Fix SF bug # 561858 Assertion with very long lists
Neal Norwitz <nnorwitz@gmail.com>
parents:
23584
diff
changeset
|
335 |
|
50618
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
336 def tearDown(self): |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
337 sys.path[:] = self.sys_path |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
338 if self.orig_module is not None: |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
339 sys.modules[self.module_name] = self.orig_module |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
340 else: |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
341 unload(self.module_name) |
|
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
342 unlink(self.file_name) |
|
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
343 unlink(self.compiled_name) |
|
64070
e4432efc88b9
Merged revisions 84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents:
61773
diff
changeset
|
344 rmtree(self.dir_name) |
|
23851
d6ae544acd24
Fix SF bug # 561858 Assertion with very long lists
Neal Norwitz <nnorwitz@gmail.com>
parents:
23584
diff
changeset
|
345 |
|
50618
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
346 def import_module(self): |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
347 ns = globals() |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
348 __import__(self.module_name, ns, ns) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
349 return sys.modules[self.module_name] |
|
23851
d6ae544acd24
Fix SF bug # 561858 Assertion with very long lists
Neal Norwitz <nnorwitz@gmail.com>
parents:
23584
diff
changeset
|
350 |
|
50618
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
351 def test_basics(self): |
|
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.module_filename, self.file_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
354 self.assertEqual(mod.code_filename, self.file_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
355 self.assertEqual(mod.func_filename, self.file_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
356 del sys.modules[self.module_name] |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
357 mod = self.import_module() |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
358 self.assertEqual(mod.module_filename, self.compiled_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
359 self.assertEqual(mod.code_filename, self.file_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
360 self.assertEqual(mod.func_filename, self.file_name) |
|
23851
d6ae544acd24
Fix SF bug # 561858 Assertion with very long lists
Neal Norwitz <nnorwitz@gmail.com>
parents:
23584
diff
changeset
|
361 |
|
50618
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
362 def test_incorrect_code_name(self): |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
363 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
|
364 mod = self.import_module() |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
365 self.assertEqual(mod.module_filename, self.compiled_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
366 self.assertEqual(mod.code_filename, self.file_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
367 self.assertEqual(mod.func_filename, self.file_name) |
| 32884 | 368 |
|
50618
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
369 def test_module_without_source(self): |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
370 target = "another_module.py" |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
371 py_compile.compile(self.file_name, dfile=target) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
372 os.remove(self.file_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
373 mod = self.import_module() |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
374 self.assertEqual(mod.module_filename, self.compiled_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
375 self.assertEqual(mod.code_filename, target) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
376 self.assertEqual(mod.func_filename, target) |
| 32884 | 377 |
|
50618
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
378 def test_foreign_code(self): |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
379 py_compile.compile(self.file_name) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
380 with open(self.compiled_name, "rb") as f: |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
381 header = f.read(8) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
382 code = marshal.load(f) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
383 constants = list(code.co_consts) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
384 foreign_code = test_main.func_code |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
385 pos = constants.index(1) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
386 constants[pos] = foreign_code |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
387 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
|
388 code.co_flags, code.co_code, tuple(constants), |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
389 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
|
390 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
|
391 code.co_freevars, code.co_cellvars) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
392 with open(self.compiled_name, "wb") as f: |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
393 f.write(header) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
394 marshal.dump(code, f) |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
395 mod = self.import_module() |
|
617199769bd3
Merged revisions 68360 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49146
diff
changeset
|
396 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
|
397 |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
398 |
|
43736
e8d003b3870f
Removed non ASCII text from test as requested by Guido. Sorry :/
Christian Heimes <christian@cheimes.de>
parents:
43735
diff
changeset
|
399 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
|
400 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
|
401 |
|
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
|
402 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
|
403 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
|
404 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
|
405 |
|
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
|
406 def tearDown(self): |
|
64070
e4432efc88b9
Merged revisions 84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents:
61773
diff
changeset
|
407 rmtree(self.path) |
|
56567
993207dce395
Avoid replacing existing modules and sys.path in import tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55168
diff
changeset
|
408 sys.path[:] = self.syspath |
| 32884 | 409 |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
410 # Regression test for http://bugs.python.org/issue1293. |
|
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
|
411 def test_trailing_slash(self): |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
412 with open(os.path.join(self.path, 'test_trailing_slash.py'), 'w') as f: |
|
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
413 f.write("testdata = 'test_trailing_slash'") |
|
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
|
414 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
|
415 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
|
416 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
|
417 unload("test_trailing_slash") |
| 32884 | 418 |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
419 # Regression test for http://bugs.python.org/issue3677. |
|
50998
bd74e866f9ca
Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50786
diff
changeset
|
420 def _test_UNC_path(self): |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
421 with open(os.path.join(self.path, 'test_trailing_slash.py'), 'w') as f: |
|
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
422 f.write("testdata = 'test_trailing_slash'") |
|
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
423 # Create the UNC path, like \\myhost\c$\foo\bar. |
|
50998
bd74e866f9ca
Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50786
diff
changeset
|
424 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
|
425 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
|
426 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
|
427 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
|
428 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
|
429 unc += path[2:] |
|
78083
7eac87fa7a06
Issue #15338: skip test_UNC_path when the current user doesn't have enough permissions to access the path.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75500
diff
changeset
|
430 try: |
|
7eac87fa7a06
Issue #15338: skip test_UNC_path when the current user doesn't have enough permissions to access the path.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75500
diff
changeset
|
431 os.listdir(unc) |
|
7eac87fa7a06
Issue #15338: skip test_UNC_path when the current user doesn't have enough permissions to access the path.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75500
diff
changeset
|
432 except OSError as e: |
|
7eac87fa7a06
Issue #15338: skip test_UNC_path when the current user doesn't have enough permissions to access the path.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75500
diff
changeset
|
433 if e.errno in (errno.EPERM, errno.EACCES): |
|
7eac87fa7a06
Issue #15338: skip test_UNC_path when the current user doesn't have enough permissions to access the path.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75500
diff
changeset
|
434 # See issue #15338 |
|
7eac87fa7a06
Issue #15338: skip test_UNC_path when the current user doesn't have enough permissions to access the path.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75500
diff
changeset
|
435 self.skipTest("cannot access administrative share %r" % (unc,)) |
|
7eac87fa7a06
Issue #15338: skip test_UNC_path when the current user doesn't have enough permissions to access the path.
Antoine Pitrou <solipsis@pitrou.net>
parents:
75500
diff
changeset
|
436 raise |
|
50998
bd74e866f9ca
Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50786
diff
changeset
|
437 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
|
438 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
|
439 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
|
440 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
|
441 |
|
bd74e866f9ca
Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50786
diff
changeset
|
442 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
|
443 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
|
444 |
|
bd74e866f9ca
Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
50786
diff
changeset
|
445 |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
446 class RelativeImportTests(unittest.TestCase): |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
447 |
|
45905
2a01075cd902
Issue #2400: Allow relative imports to "import *".
Martin v. Löwis <martin@v.loewis.de>
parents:
44614
diff
changeset
|
448 def tearDown(self): |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
449 unload("test.relimport") |
|
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
450 setUp = tearDown |
|
45905
2a01075cd902
Issue #2400: Allow relative imports to "import *".
Martin v. Löwis <martin@v.loewis.de>
parents:
44614
diff
changeset
|
451 |
|
2a01075cd902
Issue #2400: Allow relative imports to "import *".
Martin v. Löwis <martin@v.loewis.de>
parents:
44614
diff
changeset
|
452 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
|
453 # 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
|
454 from . import relimport |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
455 self.assertTrue(hasattr(relimport, "RelativeImportTests")) |
| 32884 | 456 |
|
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
|
457 def test_issue3221(self): |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
458 # Regression test for http://bugs.python.org/issue3221. |
|
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
|
459 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
|
460 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
|
461 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
|
462 exec "from . import relimport" in ns |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
463 |
|
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
|
464 # 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
|
465 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
|
466 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
|
467 check_relative() |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
468 |
|
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
|
469 # 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
|
470 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
|
471 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
|
472 check_relative() |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
473 |
|
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
|
474 # 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
|
475 ns = dict(__package__='foo', __name__='test.notarealmodule') |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
476 with check_warnings(('.+foo', 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
|
477 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
|
478 self.assertRaises(SystemError, check_relative) |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
479 |
|
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
|
480 # 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
|
481 ns = dict(__package__='foo', __name__='notarealpkg.notarealmodule') |
|
59668
9b4625700251
Cleanup in test_import and test_coding.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59659
diff
changeset
|
482 with check_warnings(('.+foo', 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
|
483 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
|
484 self.assertRaises(SystemError, check_relative) |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
485 |
|
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
|
486 # 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
|
487 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
|
488 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
|
489 self.assertRaises(ValueError, check_relative) |
| 32884 | 490 |
|
61774
55e2368f72f2
Merged revisions 81380 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
55330
diff
changeset
|
491 def test_absolute_import_without_future(self): |
|
64071
907f73e227a6
Merged revisions 84100 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents:
63556
diff
changeset
|
492 # If explicit relative import syntax is used, then do not try |
|
907f73e227a6
Merged revisions 84100 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents:
63556
diff
changeset
|
493 # to perform an absolute import in the face of failure. |
|
61774
55e2368f72f2
Merged revisions 81380 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
55330
diff
changeset
|
494 # Issue #7902. |
|
64070
e4432efc88b9
Merged revisions 84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents:
61773
diff
changeset
|
495 with self.assertRaises(ImportError): |
|
61774
55e2368f72f2
Merged revisions 81380 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
55330
diff
changeset
|
496 from .os import sep |
|
55e2368f72f2
Merged revisions 81380 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
55330
diff
changeset
|
497 self.fail("explicit relative import triggered an " |
|
64071
907f73e227a6
Merged revisions 84100 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents:
63556
diff
changeset
|
498 "implicit absolute import") |
| 32884 | 499 |
|
35742
1c4d192f1568
Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
32884
diff
changeset
|
500 |
|
75493
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
501 class TestSymbolicallyLinkedPackage(unittest.TestCase): |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
502 package_name = 'sample' |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
503 |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
504 def setUp(self): |
|
75495
c3bd31d3c7f5
Improve the test case to avoid spurious errors about already existing symlinks.
Jason R. Coombs <jaraco@jaraco.com>
parents:
75493
diff
changeset
|
505 if os.path.exists(self.tagged): |
|
c3bd31d3c7f5
Improve the test case to avoid spurious errors about already existing symlinks.
Jason R. Coombs <jaraco@jaraco.com>
parents:
75493
diff
changeset
|
506 shutil.rmtree(self.tagged) |
|
c3bd31d3c7f5
Improve the test case to avoid spurious errors about already existing symlinks.
Jason R. Coombs <jaraco@jaraco.com>
parents:
75493
diff
changeset
|
507 if os.path.exists(self.package_name): |
|
75500
f8efcca7e90f
Moved symlink support into its own module. Ported can_symlink from Python 3.2, skipping symlink test when it cannot be invoked (such as when the symlink privilege is not present).
Jason R. Coombs <jaraco@jaraco.com>
parents:
75495
diff
changeset
|
508 symlink_support.remove_symlink(self.package_name) |
|
75493
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
509 self.orig_sys_path = sys.path[:] |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
510 |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
511 # create a sample package; imagine you have a package with a tag and |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
512 # you want to symbolically link it from its untagged name. |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
513 os.mkdir(self.tagged) |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
514 init_file = os.path.join(self.tagged, '__init__.py') |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
515 open(init_file, 'w').close() |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
516 assert os.path.exists(init_file) |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
517 |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
518 # now create a symlink to the tagged package |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
519 # sample -> sample-tagged |
|
75500
f8efcca7e90f
Moved symlink support into its own module. Ported can_symlink from Python 3.2, skipping symlink test when it cannot be invoked (such as when the symlink privilege is not present).
Jason R. Coombs <jaraco@jaraco.com>
parents:
75495
diff
changeset
|
520 symlink_support.symlink(self.tagged, self.package_name) |
|
75493
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
521 |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
522 assert os.path.isdir(self.package_name) |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
523 assert os.path.isfile(os.path.join(self.package_name, '__init__.py')) |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
524 |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
525 @property |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
526 def tagged(self): |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
527 return self.package_name + '-tagged' |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
528 |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
529 # regression test for issue6727 |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
530 @unittest.skipUnless( |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
531 not hasattr(sys, 'getwindowsversion') |
|
75495
c3bd31d3c7f5
Improve the test case to avoid spurious errors about already existing symlinks.
Jason R. Coombs <jaraco@jaraco.com>
parents:
75493
diff
changeset
|
532 or sys.getwindowsversion() >= (6, 0), |
|
75493
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
533 "Windows Vista or later required") |
|
75500
f8efcca7e90f
Moved symlink support into its own module. Ported can_symlink from Python 3.2, skipping symlink test when it cannot be invoked (such as when the symlink privilege is not present).
Jason R. Coombs <jaraco@jaraco.com>
parents:
75495
diff
changeset
|
534 @symlink_support.skip_unless_symlink |
|
75493
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
535 def test_symlinked_dir_importable(self): |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
536 # make sure sample can only be imported from the current directory. |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
537 sys.path[:] = ['.'] |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
538 |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
539 # and try to import the package |
|
75495
c3bd31d3c7f5
Improve the test case to avoid spurious errors about already existing symlinks.
Jason R. Coombs <jaraco@jaraco.com>
parents:
75493
diff
changeset
|
540 __import__(self.package_name) |
|
75493
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
541 |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
542 def tearDown(self): |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
543 # now cleanup |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
544 if os.path.exists(self.package_name): |
|
75500
f8efcca7e90f
Moved symlink support into its own module. Ported can_symlink from Python 3.2, skipping symlink test when it cannot be invoked (such as when the symlink privilege is not present).
Jason R. Coombs <jaraco@jaraco.com>
parents:
75495
diff
changeset
|
545 symlink_support.remove_symlink(self.package_name) |
|
75493
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
546 if os.path.exists(self.tagged): |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
547 shutil.rmtree(self.tagged) |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
548 sys.path[:] = self.orig_sys_path |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
549 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
550 def test_main(verbose=None): |
|
75493
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
551 run_unittest(ImportTests, PycRewritingTests, PathsTests, |
|
92f4d4eebed1
Adding regression test for issue6727
Jason R. Coombs <jaraco@jaraco.com>
parents:
74610
diff
changeset
|
552 RelativeImportTests, TestSymbolicallyLinkedPackage) |
|
38018
b5e7d5b3c442
- Add new Warning class, ImportWarning
Thomas Wouters <thomas@python.org>
parents:
37408
diff
changeset
|
553 |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
554 if __name__ == '__main__': |
|
59657
2064bf62a39f
Style cleanup in test_import.
Collin Winter <collinw@gmail.com>
parents:
58669
diff
changeset
|
555 # Test needs to be a package, so we can do relative imports. |
|
46565
0e1185bfa313
Allow test_import to work when it is invoked directly
Benjamin Peterson <benjamin@python.org>
parents:
45912
diff
changeset
|
556 from test.test_import import test_main |
|
39943
d743381131d6
Convert test_import to unittest.
Georg Brandl <georg@python.org>
parents:
38018
diff
changeset
|
557 test_main() |
