annotate Lib/test/test_import.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents c79d0a7ac79d
children 4d480a2a6296 2b62348a1c03
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
59661
58e3bc21c2dd Add tests for overriding and shadowing __import__; these are a useful tripwire for an incoming JIT optimization.
Collin Winter <collinw@gmail.com>
parents: 59660
diff changeset
1 import builtins
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
2 import imp
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
3 from importlib.test.import_ import test_relative_imports
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
4 from importlib.test.import_ import util as importlib_util
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
5 import marshal
15057
bf27caba3d0e Adding Jeremy's new test_import (SF patch 101709).
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
6 import os
72670
7697223df6df Issue #11956: Skip test_import.test_unwritable_directory on FreeBSD when run as
Charles-François Natali <neologix@free.fr>
parents: 72453
diff changeset
7 import platform
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
8 import py_compile
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
9 import random
55331
c7042127196a Merged revisions 74058 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 54731
diff changeset
10 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
11 import sys
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
12 import unittest
66911
7c3715b10cdd Merged revisions 87251 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
13 import textwrap
74603
37ac757a9145 Port remaining test fixes, and fix test_importlib too.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74602
diff changeset
14 import errno
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
15
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
16 from test.support import (
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
17 EnvironmentVarGuard, TESTFN, check_warnings, forget, is_jython,
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
18 make_legacy_pyc, rmtree, run_unittest, swap_attr, swap_item, temp_umask,
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
19 unlink, unload)
66911
7c3715b10cdd Merged revisions 87251 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
20 from test import script_helper
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
21
17707
fe4c1034b480 SF bug #422177: Results from .pyc differs from .py
Tim Peters <tim.peters@gmail.com>
parents: 17095
diff changeset
22
32884
88a1d86d442e New tests:
Tim Peters <tim.peters@gmail.com>
parents: 29066
diff changeset
23 def remove_files(name):
42622
4d2aa4bfb2d6 Remove RISCOS support
Skip Montanaro <skip@pobox.com>
parents: 41969
diff changeset
24 for f in (name + ".py",
4d2aa4bfb2d6 Remove RISCOS support
Skip Montanaro <skip@pobox.com>
parents: 41969
diff changeset
25 name + ".pyc",
4d2aa4bfb2d6 Remove RISCOS support
Skip Montanaro <skip@pobox.com>
parents: 41969
diff changeset
26 name + ".pyo",
4d2aa4bfb2d6 Remove RISCOS support
Skip Montanaro <skip@pobox.com>
parents: 41969
diff changeset
27 name + ".pyw",
32884
88a1d86d442e New tests:
Tim Peters <tim.peters@gmail.com>
parents: 29066
diff changeset
28 name + "$py.class"):
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
29 unlink(f)
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
30 rmtree('__pycache__')
32884
88a1d86d442e New tests:
Tim Peters <tim.peters@gmail.com>
parents: 29066
diff changeset
31
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
32
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
33 class ImportTests(unittest.TestCase):
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
34
68812
c527ce78ba0c Try to make test_import a bit more robust
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
35 def setUp(self):
c527ce78ba0c Try to make test_import a bit more robust
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
36 remove_files(TESTFN)
c527ce78ba0c Try to make test_import a bit more robust
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
37
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
38 def tearDown(self):
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
39 unload(TESTFN)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
40
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
41 setUp = tearDown
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
42
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
43 def test_case_sensitivity(self):
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
44 # Brief digression to test that import is case-sensitive: if we got
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
45 # this far, we know for sure that "random" exists.
65777
ce53fea7f957 condense
Benjamin Peterson <benjamin@python.org>
parents: 65776
diff changeset
46 with self.assertRaises(ImportError):
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
47 import RAnDoM
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
48
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
49 def test_double_const(self):
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
50 # Another brief digression to test the accuracy of manifest float
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
51 # constants.
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
52 from test import double_const # don't blink -- that *was* the test
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
53
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
54 def test_import(self):
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
55 def test_with_extension(ext):
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
56 # The extension is normally ".py", perhaps ".pyw".
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
57 source = TESTFN + ext
42622
4d2aa4bfb2d6 Remove RISCOS support
Skip Montanaro <skip@pobox.com>
parents: 41969
diff changeset
58 pyo = TESTFN + ".pyo"
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
59 if is_jython:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
60 pyc = TESTFN + "$py.class"
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
61 else:
42622
4d2aa4bfb2d6 Remove RISCOS support
Skip Montanaro <skip@pobox.com>
parents: 41969
diff changeset
62 pyc = TESTFN + ".pyc"
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
63
54731
032c2e5e0614 make file closing more robust
Benjamin Peterson <benjamin@python.org>
parents: 50997
diff changeset
64 with open(source, "w") as f:
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
65 print("# This tests Python's ability to import a",
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
66 ext, "file.", file=f)
54731
032c2e5e0614 make file closing more robust
Benjamin Peterson <benjamin@python.org>
parents: 50997
diff changeset
67 a = random.randrange(1000)
032c2e5e0614 make file closing more robust
Benjamin Peterson <benjamin@python.org>
parents: 50997
diff changeset
68 b = random.randrange(1000)
032c2e5e0614 make file closing more robust
Benjamin Peterson <benjamin@python.org>
parents: 50997
diff changeset
69 print("a =", a, file=f)
032c2e5e0614 make file closing more robust
Benjamin Peterson <benjamin@python.org>
parents: 50997
diff changeset
70 print("b =", b, file=f)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
71
43880
4a6a7aea57f4 Correct a failing test when test_import is run after test_coding:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43760
diff changeset
72 if TESTFN in sys.modules:
4a6a7aea57f4 Correct a failing test when test_import is run after test_coding:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 43760
diff changeset
73 del sys.modules[TESTFN]
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
74 try:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
75 try:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
76 mod = __import__(TESTFN)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
77 except ImportError as err:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
78 self.fail("import from %s failed: %s" % (ext, err))
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
79
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
80 self.assertEqual(mod.a, a,
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
81 "module loaded (%s) but contents invalid" % mod)
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
82 self.assertEqual(mod.b, b,
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
83 "module loaded (%s) but contents invalid" % mod)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
84 finally:
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
85 forget(TESTFN)
41969
9d02a4adf574 Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
86 unlink(source)
9d02a4adf574 Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
87 unlink(pyc)
9d02a4adf574 Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
88 unlink(pyo)
15057
bf27caba3d0e Adding Jeremy's new test_import (SF patch 101709).
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
89
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
90 sys.path.insert(0, os.curdir)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
91 try:
42622
4d2aa4bfb2d6 Remove RISCOS support
Skip Montanaro <skip@pobox.com>
parents: 41969
diff changeset
92 test_with_extension(".py")
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
93 if sys.platform.startswith("win"):
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
94 for ext in [".PY", ".Py", ".pY", ".pyw", ".PYW", ".pYw"]:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
95 test_with_extension(ext)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
96 finally:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
97 del sys.path[0]
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
98
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
99 @unittest.skipUnless(os.name == 'posix',
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
100 "test meaningful only on posix systems")
55331
c7042127196a Merged revisions 74058 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 54731
diff changeset
101 def test_execute_bit_not_copied(self):
c7042127196a Merged revisions 74058 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 54731
diff changeset
102 # Issue 6070: under posix .pyc files got their execute bit set if
c7042127196a Merged revisions 74058 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 54731
diff changeset
103 # the .py file had the execute bit set, but they aren't executable.
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
104 with temp_umask(0o022):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
105 sys.path.insert(0, os.curdir)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
106 try:
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
107 fname = TESTFN + os.extsep + "py"
65775
42ad4ae6d8ee remove useless assignment
Benjamin Peterson <benjamin@python.org>
parents: 65298
diff changeset
108 open(fname, 'w').close()
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
109 os.chmod(fname, (stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH |
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
110 stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH))
74112
f4afc6858ed2 Issue #13645: fix test_import failure when run immediately after test_coding.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73460
diff changeset
111 fn = imp.cache_from_source(fname)
f4afc6858ed2 Issue #13645: fix test_import failure when run immediately after test_coding.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73460
diff changeset
112 unlink(fn)
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
113 __import__(TESTFN)
59671
80dc4d131d85 Merged revisions 79030-79032 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59661
diff changeset
114 if not os.path.exists(fn):
80dc4d131d85 Merged revisions 79030-79032 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59661
diff changeset
115 self.fail("__import__ did not result in creation of "
80dc4d131d85 Merged revisions 79030-79032 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59661
diff changeset
116 "either a .pyc or .pyo file")
73460
f0b1d8e4de4f test_import: test_execute_bit_not_copied() was actually a no-op: fix it.
Charles-François Natali <neologix@free.fr>
parents: 72680
diff changeset
117 s = os.stat(fn)
f0b1d8e4de4f test_import: test_execute_bit_not_copied() was actually a no-op: fix it.
Charles-François Natali <neologix@free.fr>
parents: 72680
diff changeset
118 self.assertEqual(stat.S_IMODE(s.st_mode),
f0b1d8e4de4f test_import: test_execute_bit_not_copied() was actually a no-op: fix it.
Charles-François Natali <neologix@free.fr>
parents: 72680
diff changeset
119 stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
120 finally:
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
121 del sys.path[0]
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
122 remove_files(TESTFN)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
123 unload(TESTFN)
55331
c7042127196a Merged revisions 74058 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 54731
diff changeset
124
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
125 def test_imp_module(self):
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
126 # Verify that the imp module can correctly load and find .py files
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
127 # XXX (ncoghlan): It would be nice to use support.CleanImport
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
128 # here, but that breaks because the os module registers some
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
129 # handlers in copy_reg on import. Since CleanImport doesn't
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
130 # revert that registration, the module is left in a broken
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
131 # state after reversion. Reinitialising the module contents
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
132 # and just reverting os.environ to its previous state is an OK
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
133 # workaround
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
134 orig_path = os.path
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
135 orig_getenv = os.getenv
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
136 with EnvironmentVarGuard():
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
137 x = imp.find_module("os")
65778
5b627035a922 close open file from find_module()
Benjamin Peterson <benjamin@python.org>
parents: 65777
diff changeset
138 self.addCleanup(x[0].close)
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
139 new_os = imp.load_module("os", *x)
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
140 self.assertIs(os, new_os)
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
141 self.assertIs(orig_path, new_os.path)
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
142 self.assertIsNot(orig_getenv, new_os.getenv)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
143
72453
125887a41a6f Issue #7732: Don't open a directory as a file anymore while importing a
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68812
diff changeset
144 def test_bug7732(self):
125887a41a6f Issue #7732: Don't open a directory as a file anymore while importing a
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68812
diff changeset
145 source = TESTFN + '.py'
125887a41a6f Issue #7732: Don't open a directory as a file anymore while importing a
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68812
diff changeset
146 os.mkdir(source)
125887a41a6f Issue #7732: Don't open a directory as a file anymore while importing a
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68812
diff changeset
147 try:
125887a41a6f Issue #7732: Don't open a directory as a file anymore while importing a
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68812
diff changeset
148 self.assertRaisesRegex(ImportError, '^No module',
125887a41a6f Issue #7732: Don't open a directory as a file anymore while importing a
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68812
diff changeset
149 imp.find_module, TESTFN, ["."])
125887a41a6f Issue #7732: Don't open a directory as a file anymore while importing a
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68812
diff changeset
150 finally:
125887a41a6f Issue #7732: Don't open a directory as a file anymore while importing a
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68812
diff changeset
151 os.rmdir(source)
125887a41a6f Issue #7732: Don't open a directory as a file anymore while importing a
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68812
diff changeset
152
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
153 def test_module_with_large_stack(self, module='longlist'):
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
154 # Regression test for http://bugs.python.org/issue561858.
42622
4d2aa4bfb2d6 Remove RISCOS support
Skip Montanaro <skip@pobox.com>
parents: 41969
diff changeset
155 filename = module + '.py'
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
156
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
157 # Create a file with a list of 65000 elements.
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
158 with open(filename, 'w') as f:
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
159 f.write('d = [\n')
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
160 for i in range(65000):
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
161 f.write('"",\n')
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
162 f.write(']')
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
163
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
164 try:
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
165 # Compile & remove .py file; we only need .pyc (or .pyo).
62706
d831fc0c2634 Make test_import a little bit more robust for cleaning up after itself in the
Brett Cannon <bcannon@gmail.com>
parents: 62570
diff changeset
166 # Bytecode must be relocated from the PEP 3147 bytecode-only location.
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
167 py_compile.compile(filename)
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
168 finally:
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
169 unlink(filename)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
170
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
171 # Need to be able to load from current dir.
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
172 sys.path.append('')
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
173
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
174 try:
62706
d831fc0c2634 Make test_import a little bit more robust for cleaning up after itself in the
Brett Cannon <bcannon@gmail.com>
parents: 62570
diff changeset
175 make_legacy_pyc(filename)
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
176 # This used to crash.
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
177 exec('import ' + module)
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
178 finally:
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
179 # Cleanup.
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
180 del sys.path[-1]
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
181 unlink(filename + 'c')
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
182 unlink(filename + 'o')
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
183
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
184 def test_failing_import_sticks(self):
42622
4d2aa4bfb2d6 Remove RISCOS support
Skip Montanaro <skip@pobox.com>
parents: 41969
diff changeset
185 source = TESTFN + ".py"
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
186 with open(source, "w") as f:
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
187 print("a = 1/0", file=f)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
188
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
189 # New in 2.4, we shouldn't be able to import that no matter how often
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
190 # we try.
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
191 sys.path.insert(0, os.curdir)
41969
9d02a4adf574 Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
192 if TESTFN in sys.modules:
9d02a4adf574 Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
193 del sys.modules[TESTFN]
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
194 try:
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
195 for i in [1, 2, 3]:
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
196 self.assertRaises(ZeroDivisionError, __import__, TESTFN)
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
197 self.assertNotIn(TESTFN, sys.modules,
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
198 "damaged module in sys.modules on %i try" % i)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
199 finally:
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
200 del sys.path[0]
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
201 remove_files(TESTFN)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
202
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
203 def test_import_name_binding(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
204 # import x.y.z binds x in the current namespace
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
205 import test as x
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46664
diff changeset
206 import test.support
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55331
diff changeset
207 self.assertTrue(x is test, x.__name__)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55331
diff changeset
208 self.assertTrue(hasattr(test.support, "__file__"))
32884
88a1d86d442e New tests:
Tim Peters <tim.peters@gmail.com>
parents: 29066
diff changeset
209
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
210 # import x.y.z as w binds z as w
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46664
diff changeset
211 import test.support as y
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55331
diff changeset
212 self.assertTrue(y is test.support, y.__name__)
32884
88a1d86d442e New tests:
Tim Peters <tim.peters@gmail.com>
parents: 29066
diff changeset
213
44567
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
214 def test_failing_reload(self):
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
215 # A failing reload should leave the module object in sys.modules.
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
216 source = TESTFN + os.extsep + "py"
44567
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
217 with open(source, "w") as f:
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
218 f.write("a = 1\nb=2\n")
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
219
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
220 sys.path.insert(0, os.curdir)
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
221 try:
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
222 mod = __import__(TESTFN)
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
223 self.assertIn(TESTFN, sys.modules)
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
224 self.assertEqual(mod.a, 1, "module has wrong attribute values")
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
225 self.assertEqual(mod.b, 2, "module has wrong attribute values")
44567
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
226
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
227 # On WinXP, just replacing the .py file wasn't enough to
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
228 # convince reload() to reparse it. Maybe the timestamp didn't
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
229 # move enough. We force it to get reparsed by removing the
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
230 # compiled file too.
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
231 remove_files(TESTFN)
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
232
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
233 # Now damage the module.
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
234 with open(source, "w") as f:
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
235 f.write("a = 10\nb=20//0\n")
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
236
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
237 self.assertRaises(ZeroDivisionError, imp.reload, mod)
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
238 # But we still expect the module to be in sys.modules.
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
239 mod = sys.modules.get(TESTFN)
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
240 self.assertIsNot(mod, None, "expected module to be in sys.modules")
44567
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
241
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
242 # We should have replaced a w/ 10, but the old b value should
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
243 # stick.
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
244 self.assertEqual(mod.a, 10, "module has wrong attribute values")
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
245 self.assertEqual(mod.b, 2, "module has wrong attribute values")
44567
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
246
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
247 finally:
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
248 del sys.path[0]
44567
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
249 remove_files(TESTFN)
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
250 unload(TESTFN)
44567
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44360
diff changeset
251
44583
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
252 def test_file_to_source(self):
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
253 # check if __file__ points to the source file where available
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
254 source = TESTFN + ".py"
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
255 with open(source, "w") as f:
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
256 f.write("test = None\n")
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
257
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
258 sys.path.insert(0, os.curdir)
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
259 try:
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
260 mod = __import__(TESTFN)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55331
diff changeset
261 self.assertTrue(mod.__file__.endswith('.py'))
44583
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
262 os.remove(source)
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
263 del sys.modules[TESTFN]
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
264 make_legacy_pyc(source)
44583
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
265 mod = __import__(TESTFN)
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
266 base, ext = os.path.splitext(mod.__file__)
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
267 self.assertIn(ext, ('.pyc', '.pyo'))
44583
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
268 finally:
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
269 del sys.path[0]
44583
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
270 remove_files(TESTFN)
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
271 if TESTFN in sys.modules:
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
272 del sys.modules[TESTFN]
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
273
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
274 def test_import_name_binding(self):
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
275 # import x.y.z binds x in the current namespace.
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
276 import test as x
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
277 import test.support
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
278 self.assertIs(x, test, x.__name__)
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
279 self.assertTrue(hasattr(test.support, "__file__"))
44583
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
280
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
281 # import x.y.z as w binds z as w.
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
282 import test.support as y
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
283 self.assertIs(y, test.support, y.__name__)
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
284
59671
80dc4d131d85 Merged revisions 79030-79032 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59661
diff changeset
285 def test_import_initless_directory_warning(self):
80dc4d131d85 Merged revisions 79030-79032 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59661
diff changeset
286 with check_warnings(('', ImportWarning)):
80dc4d131d85 Merged revisions 79030-79032 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59661
diff changeset
287 # Just a random non-package directory we always expect to be
80dc4d131d85 Merged revisions 79030-79032 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59661
diff changeset
288 # somewhere in sys.path...
80dc4d131d85 Merged revisions 79030-79032 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59661
diff changeset
289 self.assertRaises(ImportError, __import__, "site-packages")
80dc4d131d85 Merged revisions 79030-79032 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59661
diff changeset
290
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
291 def test_import_by_filename(self):
44617
a29adc3b0f26 Merged revisions 59864-59882 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44583
diff changeset
292 path = os.path.abspath(TESTFN)
63791
7608532f933a Issue #9425: skip tests if a filename is not encodable
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63163
diff changeset
293 encoding = sys.getfilesystemencoding()
44617
a29adc3b0f26 Merged revisions 59864-59882 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44583
diff changeset
294 try:
63791
7608532f933a Issue #9425: skip tests if a filename is not encodable
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63163
diff changeset
295 path.encode(encoding)
7608532f933a Issue #9425: skip tests if a filename is not encodable
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63163
diff changeset
296 except UnicodeEncodeError:
7608532f933a Issue #9425: skip tests if a filename is not encodable
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63163
diff changeset
297 self.skipTest('path is not encodable to {}'.format(encoding))
59671
80dc4d131d85 Merged revisions 79030-79032 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59661
diff changeset
298 with self.assertRaises(ImportError) as c:
44617
a29adc3b0f26 Merged revisions 59864-59882 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44583
diff changeset
299 __import__(path)
59671
80dc4d131d85 Merged revisions 79030-79032 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59661
diff changeset
300 self.assertEqual("Import by filename is not supported.",
80dc4d131d85 Merged revisions 79030-79032 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59661
diff changeset
301 c.exception.args[0])
44617
a29adc3b0f26 Merged revisions 59864-59882 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44583
diff changeset
302
66911
7c3715b10cdd Merged revisions 87251 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
303 def test_import_in_del_does_not_crash(self):
7c3715b10cdd Merged revisions 87251 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
304 # Issue 4236
7c3715b10cdd Merged revisions 87251 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
305 testfn = script_helper.make_script('', TESTFN, textwrap.dedent("""\
7c3715b10cdd Merged revisions 87251 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
306 import sys
7c3715b10cdd Merged revisions 87251 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
307 class C:
7c3715b10cdd Merged revisions 87251 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
308 def __del__(self):
7c3715b10cdd Merged revisions 87251 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
309 import imp
7c3715b10cdd Merged revisions 87251 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
310 sys.argv.insert(0, C())
7c3715b10cdd Merged revisions 87251 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
311 """))
7c3715b10cdd Merged revisions 87251 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
312 script_helper.assert_python_ok(testfn)
7c3715b10cdd Merged revisions 87251 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 66340
diff changeset
313
74596
a2f3d6986bfa 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: 74112
diff changeset
314 def test_timestamp_overflow(self):
a2f3d6986bfa 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: 74112
diff changeset
315 # A modification timestamp larger than 2**32 should not be a problem
a2f3d6986bfa 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: 74112
diff changeset
316 # when importing a module (issue #11235).
74602
365d0e387179 Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents: 74596
diff changeset
317 sys.path.insert(0, os.curdir)
365d0e387179 Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents: 74596
diff changeset
318 try:
365d0e387179 Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents: 74596
diff changeset
319 source = TESTFN + ".py"
365d0e387179 Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents: 74596
diff changeset
320 compiled = imp.cache_from_source(source)
365d0e387179 Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents: 74596
diff changeset
321 with open(source, 'w') as f:
365d0e387179 Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents: 74596
diff changeset
322 pass
365d0e387179 Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents: 74596
diff changeset
323 try:
74611
c79d0a7ac79d Port import fixes from 2.7.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74603
diff changeset
324 os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5))
74602
365d0e387179 Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents: 74596
diff changeset
325 except OverflowError:
365d0e387179 Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents: 74596
diff changeset
326 self.skipTest("cannot set modification time to large integer")
74603
37ac757a9145 Port remaining test fixes, and fix test_importlib too.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74602
diff changeset
327 except OSError as e:
37ac757a9145 Port remaining test fixes, and fix test_importlib too.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74602
diff changeset
328 if e.errno != getattr(errno, 'EOVERFLOW', None):
37ac757a9145 Port remaining test fixes, and fix test_importlib too.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74602
diff changeset
329 raise
37ac757a9145 Port remaining test fixes, and fix test_importlib too.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74602
diff changeset
330 self.skipTest("cannot set modification time to large integer ({})".format(e))
74602
365d0e387179 Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents: 74596
diff changeset
331 __import__(TESTFN)
365d0e387179 Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents: 74596
diff changeset
332 # The pyc file was created.
365d0e387179 Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents: 74596
diff changeset
333 os.stat(compiled)
365d0e387179 Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents: 74596
diff changeset
334 finally:
365d0e387179 Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents: 74596
diff changeset
335 del sys.path[0]
365d0e387179 Make test work under 32-bit systems, and when invoked through Lib/test/regrtest.py
Antoine Pitrou <solipsis@pitrou.net>
parents: 74596
diff changeset
336 remove_files(TESTFN)
74596
a2f3d6986bfa 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: 74112
diff changeset
337
55331
c7042127196a Merged revisions 74058 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 54731
diff changeset
338
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
339 class PycRewritingTests(unittest.TestCase):
50620
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
340 # Test that the `co_filename` attribute on code objects always points
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
341 # to the right file, even when various things happen (e.g. both the .py
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
342 # and the .pyc file are renamed).
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
343
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
344 module_name = "unlikely_module_name"
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
345 module_source = """
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
346 import sys
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
347 code_filename = sys._getframe().f_code.co_filename
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
348 module_filename = __file__
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
349 constant = 1
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
350 def func():
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
351 pass
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
352 func_filename = func.__code__.co_filename
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
353 """
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
354 dir_name = os.path.abspath(TESTFN)
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
355 file_name = os.path.join(dir_name, module_name) + os.extsep + "py"
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
356 compiled_name = imp.cache_from_source(file_name)
50620
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
357
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
358 def setUp(self):
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
359 self.sys_path = sys.path[:]
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
360 self.orig_module = sys.modules.pop(self.module_name, None)
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
361 os.mkdir(self.dir_name)
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
362 with open(self.file_name, "w") as f:
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
363 f.write(self.module_source)
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
364 sys.path.insert(0, self.dir_name)
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
365
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
366 def tearDown(self):
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
367 sys.path[:] = self.sys_path
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
368 if self.orig_module is not None:
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
369 sys.modules[self.module_name] = self.orig_module
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
370 else:
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
371 unload(self.module_name)
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
372 unlink(self.file_name)
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
373 unlink(self.compiled_name)
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
374 rmtree(self.dir_name)
50620
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
375
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
376 def import_module(self):
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
377 ns = globals()
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
378 __import__(self.module_name, ns, ns)
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
379 return sys.modules[self.module_name]
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
380
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
381 def test_basics(self):
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
382 mod = self.import_module()
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
383 self.assertEqual(mod.module_filename, self.file_name)
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
384 self.assertEqual(mod.code_filename, self.file_name)
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
385 self.assertEqual(mod.func_filename, self.file_name)
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
386 del sys.modules[self.module_name]
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
387 mod = self.import_module()
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
388 self.assertEqual(mod.module_filename, self.file_name)
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
389 self.assertEqual(mod.code_filename, self.file_name)
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
390 self.assertEqual(mod.func_filename, self.file_name)
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
391
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
392 def test_incorrect_code_name(self):
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
393 py_compile.compile(self.file_name, dfile="another_module.py")
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
394 mod = self.import_module()
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
395 self.assertEqual(mod.module_filename, self.file_name)
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
396 self.assertEqual(mod.code_filename, self.file_name)
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
397 self.assertEqual(mod.func_filename, self.file_name)
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
398
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
399 def test_module_without_source(self):
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
400 target = "another_module.py"
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
401 py_compile.compile(self.file_name, dfile=target)
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
402 os.remove(self.file_name)
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
403 pyc_file = make_legacy_pyc(self.file_name)
50620
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
404 mod = self.import_module()
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
405 self.assertEqual(mod.module_filename, pyc_file)
50620
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
406 self.assertEqual(mod.code_filename, target)
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
407 self.assertEqual(mod.func_filename, target)
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
408
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
409 def test_foreign_code(self):
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
410 py_compile.compile(self.file_name)
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
411 with open(self.compiled_name, "rb") as f:
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
412 header = f.read(8)
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
413 code = marshal.load(f)
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
414 constants = list(code.co_consts)
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
415 foreign_code = test_main.__code__
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
416 pos = constants.index(1)
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
417 constants[pos] = foreign_code
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
418 code = type(code)(code.co_argcount, code.co_kwonlyargcount,
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
419 code.co_nlocals, code.co_stacksize,
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
420 code.co_flags, code.co_code, tuple(constants),
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
421 code.co_names, code.co_varnames, code.co_filename,
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
422 code.co_name, code.co_firstlineno, code.co_lnotab,
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
423 code.co_freevars, code.co_cellvars)
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
424 with open(self.compiled_name, "wb") as f:
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
425 f.write(header)
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
426 marshal.dump(code, f)
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
427 mod = self.import_module()
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
428 self.assertEqual(mod.constant.co_filename, foreign_code.co_filename)
e987e0df8f48 Merged revisions 68363 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 49533
diff changeset
429
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
430
43747
e5eb93a0a20e Removed non ASCII text from test as requested by Guido.
Christian Heimes <christian@cheimes.de>
parents: 43733
diff changeset
431 class PathsTests(unittest.TestCase):
e5eb93a0a20e Removed non ASCII text from test as requested by Guido.
Christian Heimes <christian@cheimes.de>
parents: 43733
diff changeset
432 SAMPLES = ('test', 'test\u00e4\u00f6\u00fc\u00df', 'test\u00e9\u00e8',
e5eb93a0a20e Removed non ASCII text from test as requested by Guido.
Christian Heimes <christian@cheimes.de>
parents: 43733
diff changeset
433 'test\u00b0\u00b3\u00b2')
43732
fd685d7e859a Fixed unit tests for os.environ. Some of the tests didn't test at all because os.environ was empty.
Christian Heimes <christian@cheimes.de>
parents: 42629
diff changeset
434 path = TESTFN
fd685d7e859a Fixed unit tests for os.environ. Some of the tests didn't test at all because os.environ was empty.
Christian Heimes <christian@cheimes.de>
parents: 42629
diff changeset
435
fd685d7e859a Fixed unit tests for os.environ. Some of the tests didn't test at all because os.environ was empty.
Christian Heimes <christian@cheimes.de>
parents: 42629
diff changeset
436 def setUp(self):
fd685d7e859a Fixed unit tests for os.environ. Some of the tests didn't test at all because os.environ was empty.
Christian Heimes <christian@cheimes.de>
parents: 42629
diff changeset
437 os.mkdir(self.path)
fd685d7e859a Fixed unit tests for os.environ. Some of the tests didn't test at all because os.environ was empty.
Christian Heimes <christian@cheimes.de>
parents: 42629
diff changeset
438 self.syspath = sys.path[:]
fd685d7e859a Fixed unit tests for os.environ. Some of the tests didn't test at all because os.environ was empty.
Christian Heimes <christian@cheimes.de>
parents: 42629
diff changeset
439
fd685d7e859a Fixed unit tests for os.environ. Some of the tests didn't test at all because os.environ was empty.
Christian Heimes <christian@cheimes.de>
parents: 42629
diff changeset
440 def tearDown(self):
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
441 rmtree(self.path)
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
442 sys.path[:] = self.syspath
43732
fd685d7e859a Fixed unit tests for os.environ. Some of the tests didn't test at all because os.environ was empty.
Christian Heimes <christian@cheimes.de>
parents: 42629
diff changeset
443
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
444 # Regression test for http://bugs.python.org/issue1293.
43733
f87dc6a04cea Added unit test for bug http://bugs.python.org/issue1293
Christian Heimes <christian@cheimes.de>
parents: 43732
diff changeset
445 def test_trailing_slash(self):
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
446 with open(os.path.join(self.path, 'test_trailing_slash.py'), 'w') as f:
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
447 f.write("testdata = 'test_trailing_slash'")
43733
f87dc6a04cea Added unit test for bug http://bugs.python.org/issue1293
Christian Heimes <christian@cheimes.de>
parents: 43732
diff changeset
448 sys.path.append(self.path+'/')
f87dc6a04cea Added unit test for bug http://bugs.python.org/issue1293
Christian Heimes <christian@cheimes.de>
parents: 43732
diff changeset
449 mod = __import__("test_trailing_slash")
f87dc6a04cea Added unit test for bug http://bugs.python.org/issue1293
Christian Heimes <christian@cheimes.de>
parents: 43732
diff changeset
450 self.assertEqual(mod.testdata, 'test_trailing_slash')
f87dc6a04cea Added unit test for bug http://bugs.python.org/issue1293
Christian Heimes <christian@cheimes.de>
parents: 43732
diff changeset
451 unload("test_trailing_slash")
f87dc6a04cea Added unit test for bug http://bugs.python.org/issue1293
Christian Heimes <christian@cheimes.de>
parents: 43732
diff changeset
452
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
453 # Regression test for http://bugs.python.org/issue3677.
50997
048eb5a8343b Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50620
diff changeset
454 def _test_UNC_path(self):
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
455 with open(os.path.join(self.path, 'test_trailing_slash.py'), 'w') as f:
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
456 f.write("testdata = 'test_trailing_slash'")
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
457 # Create the UNC path, like \\myhost\c$\foo\bar.
50997
048eb5a8343b Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50620
diff changeset
458 path = os.path.abspath(self.path)
048eb5a8343b Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50620
diff changeset
459 import socket
048eb5a8343b Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50620
diff changeset
460 hn = socket.gethostname()
048eb5a8343b Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50620
diff changeset
461 drive = path[0]
048eb5a8343b Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50620
diff changeset
462 unc = "\\\\%s\\%s$"%(hn, drive)
048eb5a8343b Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50620
diff changeset
463 unc += path[2:]
048eb5a8343b Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50620
diff changeset
464 sys.path.append(path)
048eb5a8343b Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50620
diff changeset
465 mod = __import__("test_trailing_slash")
048eb5a8343b Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50620
diff changeset
466 self.assertEqual(mod.testdata, 'test_trailing_slash')
048eb5a8343b Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50620
diff changeset
467 unload("test_trailing_slash")
048eb5a8343b Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50620
diff changeset
468
048eb5a8343b Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50620
diff changeset
469 if sys.platform == "win32":
048eb5a8343b Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50620
diff changeset
470 test_UNC_path = _test_UNC_path
048eb5a8343b Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50620
diff changeset
471
048eb5a8343b Add a test for UNC import paths, see issue 3677
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50620
diff changeset
472
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
473 class RelativeImportTests(unittest.TestCase):
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
474
45931
1657f0acca78 Merged revisions 61538-61540,61556,61559-61560,61563,61565,61571,61575-61576,61580-61582,61586,61591,61593,61595,61605-61606,61613-61616,61618,61621-61623,61625,61627,61631-61634 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44634
diff changeset
475 def tearDown(self):
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
476 unload("test.relimport")
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
477 setUp = tearDown
45931
1657f0acca78 Merged revisions 61538-61540,61556,61559-61560,61563,61565,61571,61575-61576,61580-61582,61586,61591,61593,61595,61605-61606,61613-61616,61618,61621-61623,61625,61627,61631-61634 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44634
diff changeset
478
1657f0acca78 Merged revisions 61538-61540,61556,61559-61560,61563,61565,61571,61575-61576,61580-61582,61586,61591,61593,61595,61605-61606,61613-61616,61618,61621-61623,61625,61627,61631-61634 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44634
diff changeset
479 def test_relimport_star(self):
1657f0acca78 Merged revisions 61538-61540,61556,61559-61560,61563,61565,61571,61575-61576,61580-61582,61586,61591,61593,61595,61605-61606,61613-61616,61618,61621-61623,61625,61627,61631-61634 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44634
diff changeset
480 # This will import * from .test_import.
1657f0acca78 Merged revisions 61538-61540,61556,61559-61560,61563,61565,61571,61575-61576,61580-61582,61586,61591,61593,61595,61605-61606,61613-61616,61618,61621-61623,61625,61627,61631-61634 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44634
diff changeset
481 from . import relimport
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
482 self.assertTrue(hasattr(relimport, "RelativeImportTests"))
45931
1657f0acca78 Merged revisions 61538-61540,61556,61559-61560,61563,61565,61571,61575-61576,61580-61582,61586,61591,61593,61595,61605-61606,61613-61616,61618,61621-61623,61625,61627,61631-61634 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44634
diff changeset
483
48499
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
484 def test_issue3221(self):
49533
ca72271c624c forward port r66386
Benjamin Peterson <benjamin@python.org>
parents: 49100
diff changeset
485 # Note for mergers: the 'absolute' tests from the 2.x branch
ca72271c624c forward port r66386
Benjamin Peterson <benjamin@python.org>
parents: 49100
diff changeset
486 # are missing in Py3k because implicit relative imports are
ca72271c624c forward port r66386
Benjamin Peterson <benjamin@python.org>
parents: 49100
diff changeset
487 # a thing of the past
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
488 #
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
489 # Regression test for http://bugs.python.org/issue3221.
48499
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
490 def check_relative():
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
491 exec("from . import relimport", ns)
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
492
49533
ca72271c624c forward port r66386
Benjamin Peterson <benjamin@python.org>
parents: 49100
diff changeset
493 # Check relative import OK with __package__ and __name__ correct
48499
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
494 ns = dict(__package__='test', __name__='test.notarealmodule')
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
495 check_relative()
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
496
49533
ca72271c624c forward port r66386
Benjamin Peterson <benjamin@python.org>
parents: 49100
diff changeset
497 # Check relative import OK with only __name__ wrong
48499
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
498 ns = dict(__package__='test', __name__='notarealpkg.notarealmodule')
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
499 check_relative()
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
500
49533
ca72271c624c forward port r66386
Benjamin Peterson <benjamin@python.org>
parents: 49100
diff changeset
501 # Check relative import fails with only __package__ wrong
48499
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
502 ns = dict(__package__='foo', __name__='test.notarealmodule')
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
503 self.assertRaises(SystemError, check_relative)
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
504
49533
ca72271c624c forward port r66386
Benjamin Peterson <benjamin@python.org>
parents: 49100
diff changeset
505 # Check relative import fails with __package__ and __name__ wrong
48499
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
506 ns = dict(__package__='foo', __name__='notarealpkg.notarealmodule')
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
507 self.assertRaises(SystemError, check_relative)
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
508
49533
ca72271c624c forward port r66386
Benjamin Peterson <benjamin@python.org>
parents: 49100
diff changeset
509 # Check relative import fails with package set to a non-string
48499
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
510 ns = dict(__package__=object())
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
511 self.assertRaises(ValueError, check_relative)
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
512
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
513 def test_absolute_import_without_future(self):
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
514 # If explicit relative import syntax is used, then do not try
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
515 # to perform an absolute import in the face of failure.
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
516 # Issue #7902.
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
517 with self.assertRaises(ImportError):
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
518 from .os import sep
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
519 self.fail("explicit relative import triggered an "
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
520 "implicit absolute import")
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
521
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
522
59661
58e3bc21c2dd Add tests for overriding and shadowing __import__; these are a useful tripwire for an incoming JIT optimization.
Collin Winter <collinw@gmail.com>
parents: 59660
diff changeset
523 class OverridingImportBuiltinTests(unittest.TestCase):
58e3bc21c2dd Add tests for overriding and shadowing __import__; these are a useful tripwire for an incoming JIT optimization.
Collin Winter <collinw@gmail.com>
parents: 59660
diff changeset
524 def test_override_builtin(self):
58e3bc21c2dd Add tests for overriding and shadowing __import__; these are a useful tripwire for an incoming JIT optimization.
Collin Winter <collinw@gmail.com>
parents: 59660
diff changeset
525 # Test that overriding builtins.__import__ can bypass sys.modules.
58e3bc21c2dd Add tests for overriding and shadowing __import__; these are a useful tripwire for an incoming JIT optimization.
Collin Winter <collinw@gmail.com>
parents: 59660
diff changeset
526 import os
58e3bc21c2dd Add tests for overriding and shadowing __import__; these are a useful tripwire for an incoming JIT optimization.
Collin Winter <collinw@gmail.com>
parents: 59660
diff changeset
527
58e3bc21c2dd Add tests for overriding and shadowing __import__; these are a useful tripwire for an incoming JIT optimization.
Collin Winter <collinw@gmail.com>
parents: 59660
diff changeset
528 def foo():
58e3bc21c2dd Add tests for overriding and shadowing __import__; these are a useful tripwire for an incoming JIT optimization.
Collin Winter <collinw@gmail.com>
parents: 59660
diff changeset
529 import os
58e3bc21c2dd Add tests for overriding and shadowing __import__; these are a useful tripwire for an incoming JIT optimization.
Collin Winter <collinw@gmail.com>
parents: 59660
diff changeset
530 return os
58e3bc21c2dd Add tests for overriding and shadowing __import__; these are a useful tripwire for an incoming JIT optimization.
Collin Winter <collinw@gmail.com>
parents: 59660
diff changeset
531 self.assertEqual(foo(), os) # Quick sanity check.
58e3bc21c2dd Add tests for overriding and shadowing __import__; these are a useful tripwire for an incoming JIT optimization.
Collin Winter <collinw@gmail.com>
parents: 59660
diff changeset
532
58e3bc21c2dd Add tests for overriding and shadowing __import__; these are a useful tripwire for an incoming JIT optimization.
Collin Winter <collinw@gmail.com>
parents: 59660
diff changeset
533 with swap_attr(builtins, "__import__", lambda *x: 5):
58e3bc21c2dd Add tests for overriding and shadowing __import__; these are a useful tripwire for an incoming JIT optimization.
Collin Winter <collinw@gmail.com>
parents: 59660
diff changeset
534 self.assertEqual(foo(), 5)
58e3bc21c2dd Add tests for overriding and shadowing __import__; these are a useful tripwire for an incoming JIT optimization.
Collin Winter <collinw@gmail.com>
parents: 59660
diff changeset
535
58e3bc21c2dd Add tests for overriding and shadowing __import__; these are a useful tripwire for an incoming JIT optimization.
Collin Winter <collinw@gmail.com>
parents: 59660
diff changeset
536 # Test what happens when we shadow __import__ in globals(); this
58e3bc21c2dd Add tests for overriding and shadowing __import__; these are a useful tripwire for an incoming JIT optimization.
Collin Winter <collinw@gmail.com>
parents: 59660
diff changeset
537 # currently does not impact the import process, but if this changes,
58e3bc21c2dd Add tests for overriding and shadowing __import__; these are a useful tripwire for an incoming JIT optimization.
Collin Winter <collinw@gmail.com>
parents: 59660
diff changeset
538 # other code will need to change, so keep this test as a tripwire.
58e3bc21c2dd Add tests for overriding and shadowing __import__; these are a useful tripwire for an incoming JIT optimization.
Collin Winter <collinw@gmail.com>
parents: 59660
diff changeset
539 with swap_item(globals(), "__import__", lambda *x: 5):
58e3bc21c2dd Add tests for overriding and shadowing __import__; these are a useful tripwire for an incoming JIT optimization.
Collin Winter <collinw@gmail.com>
parents: 59660
diff changeset
540 self.assertEqual(foo(), os)
58e3bc21c2dd Add tests for overriding and shadowing __import__; these are a useful tripwire for an incoming JIT optimization.
Collin Winter <collinw@gmail.com>
parents: 59660
diff changeset
541
58e3bc21c2dd Add tests for overriding and shadowing __import__; these are a useful tripwire for an incoming JIT optimization.
Collin Winter <collinw@gmail.com>
parents: 59660
diff changeset
542
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
543 class PycacheTests(unittest.TestCase):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
544 # Test the various PEP 3147 related behaviors.
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
545
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
546 tag = imp.get_tag()
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
547
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
548 def _clean(self):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
549 forget(TESTFN)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
550 rmtree('__pycache__')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
551 unlink(self.source)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
552
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
553 def setUp(self):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
554 self.source = TESTFN + '.py'
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
555 self._clean()
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
556 with open(self.source, 'w') as fp:
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
557 print('# This is a test file written by test_import.py', file=fp)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
558 sys.path.insert(0, os.curdir)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
559
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
560 def tearDown(self):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
561 assert sys.path[0] == os.curdir, 'Unexpected sys.path[0]'
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
562 del sys.path[0]
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
563 self._clean()
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
564
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
565 def test_import_pyc_path(self):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
566 self.assertFalse(os.path.exists('__pycache__'))
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
567 __import__(TESTFN)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
568 self.assertTrue(os.path.exists('__pycache__'))
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
569 self.assertTrue(os.path.exists(os.path.join(
65295
dc200cf2f565 #9964: fix running test_import under -O or -OO.
Georg Brandl <georg@python.org>
parents: 64069
diff changeset
570 '__pycache__', '{}.{}.py{}'.format(
65298
4a8e0d89755b Better check for "any optimize option given".
Georg Brandl <georg@python.org>
parents: 65295
diff changeset
571 TESTFN, self.tag, __debug__ and 'c' or 'o'))))
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
572
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
573 @unittest.skipUnless(os.name == 'posix',
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
574 "test meaningful only on posix systems")
72680
7a2127ca6c8a os.geteuid() may not be available...
Charles-François Natali <neologix@free.fr>
parents: 72674
diff changeset
575 @unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0,
72674
cbda512c6d7f Issue #11956: Always skip test_import.test_unwritable_directory when run as
Charles-François Natali <neologix@free.fr>
parents: 72670
diff changeset
576 "due to varying filesystem permission semantics (issue #11956)")
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
577 def test_unwritable_directory(self):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
578 # When the umask causes the new __pycache__ directory to be
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
579 # unwritable, the import still succeeds but no .pyc file is written.
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
580 with temp_umask(0o222):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
581 __import__(TESTFN)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
582 self.assertTrue(os.path.exists('__pycache__'))
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
583 self.assertFalse(os.path.exists(os.path.join(
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
584 '__pycache__', '{}.{}.pyc'.format(TESTFN, self.tag))))
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
585
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
586 def test_missing_source(self):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
587 # With PEP 3147 cache layout, removing the source but leaving the pyc
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
588 # file does not satisfy the import.
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
589 __import__(TESTFN)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
590 pyc_file = imp.cache_from_source(self.source)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
591 self.assertTrue(os.path.exists(pyc_file))
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
592 os.remove(self.source)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
593 forget(TESTFN)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
594 self.assertRaises(ImportError, __import__, TESTFN)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
595
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
596 def test_missing_source_legacy(self):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
597 # Like test_missing_source() except that for backward compatibility,
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
598 # when the pyc file lives where the py file would have been (and named
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
599 # without the tag), it is importable. The __file__ of the imported
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
600 # module is the pyc location.
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
601 __import__(TESTFN)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
602 # pyc_file gets removed in _clean() via tearDown().
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
603 pyc_file = make_legacy_pyc(self.source)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
604 os.remove(self.source)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
605 unload(TESTFN)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
606 m = __import__(TESTFN)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
607 self.assertEqual(m.__file__,
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
608 os.path.join(os.curdir, os.path.relpath(pyc_file)))
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
609
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
610 def test___cached__(self):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
611 # Modules now also have an __cached__ that points to the pyc file.
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
612 m = __import__(TESTFN)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
613 pyc_file = imp.cache_from_source(TESTFN + '.py')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
614 self.assertEqual(m.__cached__, os.path.join(os.curdir, pyc_file))
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
615
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
616 def test___cached___legacy_pyc(self):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
617 # Like test___cached__() except that for backward compatibility,
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
618 # when the pyc file lives where the py file would have been (and named
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
619 # without the tag), it is importable. The __cached__ of the imported
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
620 # module is the pyc location.
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
621 __import__(TESTFN)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
622 # pyc_file gets removed in _clean() via tearDown().
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
623 pyc_file = make_legacy_pyc(self.source)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
624 os.remove(self.source)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
625 unload(TESTFN)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
626 m = __import__(TESTFN)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
627 self.assertEqual(m.__cached__,
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
628 os.path.join(os.curdir, os.path.relpath(pyc_file)))
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
629
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
630 def test_package___cached__(self):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
631 # Like test___cached__ but for packages.
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
632 def cleanup():
64067
d52f9c8fe5d6 Use test.support and unittest features. Fix duplicated test (bad merge in r79033). Fix comment for issue #7902.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63791
diff changeset
633 rmtree('pep3147')
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
634 os.mkdir('pep3147')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
635 self.addCleanup(cleanup)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
636 # Touch the __init__.py
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
637 with open(os.path.join('pep3147', '__init__.py'), 'w'):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
638 pass
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
639 with open(os.path.join('pep3147', 'foo.py'), 'w'):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
640 pass
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
641 unload('pep3147.foo')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
642 unload('pep3147')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
643 m = __import__('pep3147.foo')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
644 init_pyc = imp.cache_from_source(
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
645 os.path.join('pep3147', '__init__.py'))
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
646 self.assertEqual(m.__cached__, os.path.join(os.curdir, init_pyc))
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
647 foo_pyc = imp.cache_from_source(os.path.join('pep3147', 'foo.py'))
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
648 self.assertEqual(sys.modules['pep3147.foo'].__cached__,
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
649 os.path.join(os.curdir, foo_pyc))
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
650
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
651 def test_package___cached___from_pyc(self):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
652 # Like test___cached__ but ensuring __cached__ when imported from a
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
653 # PEP 3147 pyc file.
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
654 def cleanup():
64067
d52f9c8fe5d6 Use test.support and unittest features. Fix duplicated test (bad merge in r79033). Fix comment for issue #7902.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63791
diff changeset
655 rmtree('pep3147')
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
656 os.mkdir('pep3147')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
657 self.addCleanup(cleanup)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
658 unload('pep3147.foo')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
659 unload('pep3147')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
660 # Touch the __init__.py
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
661 with open(os.path.join('pep3147', '__init__.py'), 'w'):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
662 pass
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
663 with open(os.path.join('pep3147', 'foo.py'), 'w'):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
664 pass
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
665 m = __import__('pep3147.foo')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
666 unload('pep3147.foo')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
667 unload('pep3147')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
668 m = __import__('pep3147.foo')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
669 init_pyc = imp.cache_from_source(
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
670 os.path.join('pep3147', '__init__.py'))
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
671 self.assertEqual(m.__cached__, os.path.join(os.curdir, init_pyc))
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
672 foo_pyc = imp.cache_from_source(os.path.join('pep3147', 'foo.py'))
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
673 self.assertEqual(sys.modules['pep3147.foo'].__cached__,
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
674 os.path.join(os.curdir, foo_pyc))
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
675
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
676
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
677 class RelativeImportFromImportlibTests(test_relative_imports.RelativeImports):
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
678
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
679 def setUp(self):
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
680 self._importlib_util_flag = importlib_util.using___import__
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
681 importlib_util.using___import__ = True
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
682
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
683 def tearDown(self):
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
684 importlib_util.using___import__ = self._importlib_util_flag
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
685
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
686
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
687 def test_main(verbose=None):
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59671
diff changeset
688 run_unittest(ImportTests, PycacheTests,
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
689 PycRewritingTests, PathsTests, RelativeImportTests,
63162
f523b39852e7 Explicitly test relative imports by reusing importlib tests.
Brett Cannon <bcannon@gmail.com>
parents: 62706
diff changeset
690 OverridingImportBuiltinTests,
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
691 RelativeImportFromImportlibTests)
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
692
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37408
diff changeset
693
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
694 if __name__ == '__main__':
64085
4df0d2a13aa1 Merged revisions 81214,82302,82465,83090-83091,84097,84099 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 55593
diff changeset
695 # Test needs to be a package, so we can do relative imports.
46664
031dfe455585 Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45931
diff changeset
696 from test.test_import import test_main
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
697 test_main()