annotate Lib/test/test_imp.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 2f2c7eb27437
children 2370ae9ee052 0c70c24750fd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28857
68c6186dc1a1 Rewrote. As reported on c.l.py, when the test suite is run via
Tim Peters <tim.peters@gmail.com>
parents: 27923
diff changeset
1 import imp
52480
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
2 import os
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
3 import os.path
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
4 import shutil
49007
45c43808979e PyTokenizer_FindEncoding() always failed because it set the tokenizer state
Brett Cannon <bcannon@gmail.com>
parents: 47201
diff changeset
5 import sys
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 34188
diff changeset
6 import unittest
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 45471
diff changeset
7 from test import support
65368
9712586fa42f Issue #9308: Removed redundant coding cookies. Added tests for
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65023
diff changeset
8 import importlib
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 34188
diff changeset
9
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 34188
diff changeset
10 class LockTests(unittest.TestCase):
27853
5620b0786271 Cleanup from patch #683257:
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
11
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 34188
diff changeset
12 """Very basic test of import lock functions."""
27853
5620b0786271 Cleanup from patch #683257:
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
13
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 34188
diff changeset
14 def verify_lock_state(self, expected):
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52480
diff changeset
15 self.assertEqual(imp.lock_held(), expected,
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 34188
diff changeset
16 "expected imp.lock_held() to be %r" % expected)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 34188
diff changeset
17 def testLock(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 34188
diff changeset
18 LOOPS = 50
27853
5620b0786271 Cleanup from patch #683257:
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
19
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 34188
diff changeset
20 # The import lock may already be held, e.g. if the test suite is run
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 34188
diff changeset
21 # via "import test.autotest".
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 34188
diff changeset
22 lock_held_at_start = imp.lock_held()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 34188
diff changeset
23 self.verify_lock_state(lock_held_at_start)
28857
68c6186dc1a1 Rewrote. As reported on c.l.py, when the test suite is run via
Tim Peters <tim.peters@gmail.com>
parents: 27923
diff changeset
24
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 34188
diff changeset
25 for i in range(LOOPS):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 34188
diff changeset
26 imp.acquire_lock()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 34188
diff changeset
27 self.verify_lock_state(True)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 34188
diff changeset
28
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 34188
diff changeset
29 for i in range(LOOPS):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 34188
diff changeset
30 imp.release_lock()
27853
5620b0786271 Cleanup from patch #683257:
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
31
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 34188
diff changeset
32 # The original state should be restored now.
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 34188
diff changeset
33 self.verify_lock_state(lock_held_at_start)
28857
68c6186dc1a1 Rewrote. As reported on c.l.py, when the test suite is run via
Tim Peters <tim.peters@gmail.com>
parents: 27923
diff changeset
34
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 34188
diff changeset
35 if not lock_held_at_start:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 34188
diff changeset
36 try:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 34188
diff changeset
37 imp.release_lock()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 34188
diff changeset
38 except RuntimeError:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 34188
diff changeset
39 pass
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 34188
diff changeset
40 else:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 34188
diff changeset
41 self.fail("release_lock() without lock should raise "
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 34188
diff changeset
42 "RuntimeError")
27853
5620b0786271 Cleanup from patch #683257:
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
43
43605
a2bb76d11d66 Patch 1267 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
44 class ImportTests(unittest.TestCase):
65368
9712586fa42f Issue #9308: Removed redundant coding cookies. Added tests for
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65023
diff changeset
45 def setUp(self):
9712586fa42f Issue #9308: Removed redundant coding cookies. Added tests for
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65023
diff changeset
46 mod = importlib.import_module('test.encoded_modules')
9712586fa42f Issue #9308: Removed redundant coding cookies. Added tests for
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65023
diff changeset
47 self.test_strings = mod.test_strings
9712586fa42f Issue #9308: Removed redundant coding cookies. Added tests for
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65023
diff changeset
48 self.test_path = mod.__path__
9712586fa42f Issue #9308: Removed redundant coding cookies. Added tests for
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65023
diff changeset
49
9712586fa42f Issue #9308: Removed redundant coding cookies. Added tests for
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65023
diff changeset
50 def test_import_encoded_module(self):
9712586fa42f Issue #9308: Removed redundant coding cookies. Added tests for
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65023
diff changeset
51 for modname, encoding, teststr in self.test_strings:
9712586fa42f Issue #9308: Removed redundant coding cookies. Added tests for
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65023
diff changeset
52 mod = importlib.import_module('test.encoded_modules.'
9712586fa42f Issue #9308: Removed redundant coding cookies. Added tests for
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65023
diff changeset
53 'module_' + modname)
9712586fa42f Issue #9308: Removed redundant coding cookies. Added tests for
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65023
diff changeset
54 self.assertEqual(teststr, mod.test)
43605
a2bb76d11d66 Patch 1267 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
55
a2bb76d11d66 Patch 1267 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
56 def test_find_module_encoding(self):
65368
9712586fa42f Issue #9308: Removed redundant coding cookies. Added tests for
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65023
diff changeset
57 for mod, encoding, _ in self.test_strings:
65741
c85f5339068a Move test_imp over to file context managers.
Brett Cannon <bcannon@gmail.com>
parents: 65368
diff changeset
58 with imp.find_module('module_' + mod, self.test_path)[0] as fd:
c85f5339068a Move test_imp over to file context managers.
Brett Cannon <bcannon@gmail.com>
parents: 65368
diff changeset
59 self.assertEqual(fd.encoding, encoding)
43605
a2bb76d11d66 Patch 1267 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
60
43632
0067a6ab4854 Issue 1267, continued.
Guido van Rossum <guido@python.org>
parents: 43605
diff changeset
61 def test_issue1267(self):
65368
9712586fa42f Issue #9308: Removed redundant coding cookies. Added tests for
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65023
diff changeset
62 for mod, encoding, _ in self.test_strings:
9712586fa42f Issue #9308: Removed redundant coding cookies. Added tests for
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65023
diff changeset
63 fp, filename, info = imp.find_module('module_' + mod,
9712586fa42f Issue #9308: Removed redundant coding cookies. Added tests for
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65023
diff changeset
64 self.test_path)
65741
c85f5339068a Move test_imp over to file context managers.
Brett Cannon <bcannon@gmail.com>
parents: 65368
diff changeset
65 with fp:
c85f5339068a Move test_imp over to file context managers.
Brett Cannon <bcannon@gmail.com>
parents: 65368
diff changeset
66 self.assertNotEqual(fp, None)
c85f5339068a Move test_imp over to file context managers.
Brett Cannon <bcannon@gmail.com>
parents: 65368
diff changeset
67 self.assertEqual(fp.encoding, encoding)
c85f5339068a Move test_imp over to file context managers.
Brett Cannon <bcannon@gmail.com>
parents: 65368
diff changeset
68 self.assertEqual(fp.tell(), 0)
c85f5339068a Move test_imp over to file context managers.
Brett Cannon <bcannon@gmail.com>
parents: 65368
diff changeset
69 self.assertEqual(fp.readline(), '# test %s encoding\n'
c85f5339068a Move test_imp over to file context managers.
Brett Cannon <bcannon@gmail.com>
parents: 65368
diff changeset
70 % encoding)
43632
0067a6ab4854 Issue 1267, continued.
Guido van Rossum <guido@python.org>
parents: 43605
diff changeset
71
0067a6ab4854 Issue 1267, continued.
Guido van Rossum <guido@python.org>
parents: 43605
diff changeset
72 fp, filename, info = imp.find_module("tokenize")
65741
c85f5339068a Move test_imp over to file context managers.
Brett Cannon <bcannon@gmail.com>
parents: 65368
diff changeset
73 with fp:
c85f5339068a Move test_imp over to file context managers.
Brett Cannon <bcannon@gmail.com>
parents: 65368
diff changeset
74 self.assertNotEqual(fp, None)
c85f5339068a Move test_imp over to file context managers.
Brett Cannon <bcannon@gmail.com>
parents: 65368
diff changeset
75 self.assertEqual(fp.encoding, "utf-8")
c85f5339068a Move test_imp over to file context managers.
Brett Cannon <bcannon@gmail.com>
parents: 65368
diff changeset
76 self.assertEqual(fp.tell(), 0)
c85f5339068a Move test_imp over to file context managers.
Brett Cannon <bcannon@gmail.com>
parents: 65368
diff changeset
77 self.assertEqual(fp.readline(),
c85f5339068a Move test_imp over to file context managers.
Brett Cannon <bcannon@gmail.com>
parents: 65368
diff changeset
78 '"""Tokenization help for Python programs.\n')
43632
0067a6ab4854 Issue 1267, continued.
Guido van Rossum <guido@python.org>
parents: 43605
diff changeset
79
49007
45c43808979e PyTokenizer_FindEncoding() always failed because it set the tokenizer state
Brett Cannon <bcannon@gmail.com>
parents: 47201
diff changeset
80 def test_issue3594(self):
45c43808979e PyTokenizer_FindEncoding() always failed because it set the tokenizer state
Brett Cannon <bcannon@gmail.com>
parents: 47201
diff changeset
81 temp_mod_name = 'test_imp_helper'
45c43808979e PyTokenizer_FindEncoding() always failed because it set the tokenizer state
Brett Cannon <bcannon@gmail.com>
parents: 47201
diff changeset
82 sys.path.insert(0, '.')
45c43808979e PyTokenizer_FindEncoding() always failed because it set the tokenizer state
Brett Cannon <bcannon@gmail.com>
parents: 47201
diff changeset
83 try:
45c43808979e PyTokenizer_FindEncoding() always failed because it set the tokenizer state
Brett Cannon <bcannon@gmail.com>
parents: 47201
diff changeset
84 with open(temp_mod_name + '.py', 'w') as file:
45c43808979e PyTokenizer_FindEncoding() always failed because it set the tokenizer state
Brett Cannon <bcannon@gmail.com>
parents: 47201
diff changeset
85 file.write("# coding: cp1252\nu = 'test.test_imp'\n")
45c43808979e PyTokenizer_FindEncoding() always failed because it set the tokenizer state
Brett Cannon <bcannon@gmail.com>
parents: 47201
diff changeset
86 file, filename, info = imp.find_module(temp_mod_name)
45c43808979e PyTokenizer_FindEncoding() always failed because it set the tokenizer state
Brett Cannon <bcannon@gmail.com>
parents: 47201
diff changeset
87 file.close()
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59764
diff changeset
88 self.assertEqual(file.encoding, 'cp1252')
49007
45c43808979e PyTokenizer_FindEncoding() always failed because it set the tokenizer state
Brett Cannon <bcannon@gmail.com>
parents: 47201
diff changeset
89 finally:
45c43808979e PyTokenizer_FindEncoding() always failed because it set the tokenizer state
Brett Cannon <bcannon@gmail.com>
parents: 47201
diff changeset
90 del sys.path[0]
45c43808979e PyTokenizer_FindEncoding() always failed because it set the tokenizer state
Brett Cannon <bcannon@gmail.com>
parents: 47201
diff changeset
91 support.unlink(temp_mod_name + '.py')
45c43808979e PyTokenizer_FindEncoding() always failed because it set the tokenizer state
Brett Cannon <bcannon@gmail.com>
parents: 47201
diff changeset
92 support.unlink(temp_mod_name + '.pyc')
45c43808979e PyTokenizer_FindEncoding() always failed because it set the tokenizer state
Brett Cannon <bcannon@gmail.com>
parents: 47201
diff changeset
93 support.unlink(temp_mod_name + '.pyo')
45c43808979e PyTokenizer_FindEncoding() always failed because it set the tokenizer state
Brett Cannon <bcannon@gmail.com>
parents: 47201
diff changeset
94
52480
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
95 def test_issue5604(self):
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
96 # Test cannot cover imp.load_compiled function.
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
97 # Martin von Loewis note what shared library cannot have non-ascii
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
98 # character because init_xxx function cannot be compiled
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
99 # and issue never happens for dynamic modules.
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
100 # But sources modified to follow generic way for processing pathes.
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
101
59390
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
102 # the return encoding could be uppercase or None
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
103 fs_encoding = sys.getfilesystemencoding()
52480
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
104
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
105 # covers utf-8 and Windows ANSI code pages
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
106 # one non-space symbol from every page
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
107 # (http://en.wikipedia.org/wiki/Code_page)
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
108 known_locales = {
59390
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
109 'utf-8' : b'\xc3\xa4',
52480
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
110 'cp1250' : b'\x8C',
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
111 'cp1251' : b'\xc0',
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
112 'cp1252' : b'\xc0',
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
113 'cp1253' : b'\xc1',
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
114 'cp1254' : b'\xc0',
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
115 'cp1255' : b'\xe0',
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
116 'cp1256' : b'\xe0',
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
117 'cp1257' : b'\xc0',
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
118 'cp1258' : b'\xc0',
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
119 }
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
120
59764
f77f8501da7f Merged revisions 79144 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59390
diff changeset
121 if sys.platform == 'darwin':
f77f8501da7f Merged revisions 79144 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59390
diff changeset
122 self.assertEqual(fs_encoding, 'utf-8')
f77f8501da7f Merged revisions 79144 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59390
diff changeset
123 # Mac OS X uses the Normal Form D decomposition
f77f8501da7f Merged revisions 79144 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59390
diff changeset
124 # http://developer.apple.com/mac/library/qa/qa2001/qa1173.html
f77f8501da7f Merged revisions 79144 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59390
diff changeset
125 special_char = b'a\xcc\x88'
f77f8501da7f Merged revisions 79144 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59390
diff changeset
126 else:
f77f8501da7f Merged revisions 79144 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59390
diff changeset
127 special_char = known_locales.get(fs_encoding)
f77f8501da7f Merged revisions 79144 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59390
diff changeset
128
59390
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
129 if not special_char:
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
130 self.skipTest("can't run this test with %s as filesystem encoding"
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
131 % fs_encoding)
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
132 decoded_char = special_char.decode(fs_encoding)
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
133 temp_mod_name = 'test_imp_helper_' + decoded_char
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
134 test_package_name = 'test_imp_helper_package_' + decoded_char
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
135 init_file_name = os.path.join(test_package_name, '__init__.py')
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
136 try:
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
137 # if the curdir is not in sys.path the test fails when run with
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
138 # ./python ./Lib/test/regrtest.py test_imp
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
139 sys.path.insert(0, os.curdir)
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
140 with open(temp_mod_name + '.py', 'w') as file:
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
141 file.write('a = 1\n')
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
142 file, filename, info = imp.find_module(temp_mod_name)
65741
c85f5339068a Move test_imp over to file context managers.
Brett Cannon <bcannon@gmail.com>
parents: 65368
diff changeset
143 with file:
c85f5339068a Move test_imp over to file context managers.
Brett Cannon <bcannon@gmail.com>
parents: 65368
diff changeset
144 self.assertIsNotNone(file)
c85f5339068a Move test_imp over to file context managers.
Brett Cannon <bcannon@gmail.com>
parents: 65368
diff changeset
145 self.assertTrue(filename[:-3].endswith(temp_mod_name))
c85f5339068a Move test_imp over to file context managers.
Brett Cannon <bcannon@gmail.com>
parents: 65368
diff changeset
146 self.assertEqual(info[0], '.py')
c85f5339068a Move test_imp over to file context managers.
Brett Cannon <bcannon@gmail.com>
parents: 65368
diff changeset
147 self.assertEqual(info[1], 'U')
c85f5339068a Move test_imp over to file context managers.
Brett Cannon <bcannon@gmail.com>
parents: 65368
diff changeset
148 self.assertEqual(info[2], imp.PY_SOURCE)
52480
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
149
65741
c85f5339068a Move test_imp over to file context managers.
Brett Cannon <bcannon@gmail.com>
parents: 65368
diff changeset
150 mod = imp.load_module(temp_mod_name, file, filename, info)
c85f5339068a Move test_imp over to file context managers.
Brett Cannon <bcannon@gmail.com>
parents: 65368
diff changeset
151 self.assertEqual(mod.a, 1)
52480
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
152
59390
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
153 mod = imp.load_source(temp_mod_name, temp_mod_name + '.py')
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
154 self.assertEqual(mod.a, 1)
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
155
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
156 mod = imp.load_compiled(
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
157 temp_mod_name, imp.cache_from_source(temp_mod_name + '.py'))
59390
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
158 self.assertEqual(mod.a, 1)
52480
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
159
59390
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
160 if not os.path.exists(test_package_name):
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
161 os.mkdir(test_package_name)
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
162 with open(init_file_name, 'w') as file:
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
163 file.write('b = 2\n')
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
164 package = imp.load_package(test_package_name, test_package_name)
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
165 self.assertEqual(package.b, 2)
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
166 finally:
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
167 del sys.path[0]
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
168 for ext in ('.py', '.pyc', '.pyo'):
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
169 support.unlink(temp_mod_name + ext)
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
170 support.unlink(init_file_name + ext)
aa48526451d1 Merged revisions 78689,78696-78697,78708,78711 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
171 support.rmtree(test_package_name)
52480
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
172
69529
fa5e348889c2 Issue #9319: Fix a crash on parsing a Python source code without encoding
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
173 def test_issue9319(self):
69556
2f2c7eb27437 Issue #11919: try to fix test_imp failure on some buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 69530
diff changeset
174 path = os.path.dirname(__file__)
69530
701069f9888c Issue #9319: Fix the unit test
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69529
diff changeset
175 self.assertRaises(SyntaxError,
69556
2f2c7eb27437 Issue #11919: try to fix test_imp failure on some buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 69530
diff changeset
176 imp.find_module, "badsyntax_pep3120", [path])
69529
fa5e348889c2 Issue #9319: Fix a crash on parsing a Python source code without encoding
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
177
52480
80271ce09a66 Issue #5604: non-ASCII characters in module name passed to
Guido van Rossum <guido@python.org>
parents: 49106
diff changeset
178
56592
098ba3a63730 Note that a number of the changes listed below were not applicable to the Py3k branch, and hence the corresponding
Nick Coghlan <ncoghlan@gmail.com>
parents: 55593
diff changeset
179 class ReloadTests(unittest.TestCase):
098ba3a63730 Note that a number of the changes listed below were not applicable to the Py3k branch, and hence the corresponding
Nick Coghlan <ncoghlan@gmail.com>
parents: 55593
diff changeset
180
098ba3a63730 Note that a number of the changes listed below were not applicable to the Py3k branch, and hence the corresponding
Nick Coghlan <ncoghlan@gmail.com>
parents: 55593
diff changeset
181 """Very basic tests to make sure that imp.reload() operates just like
098ba3a63730 Note that a number of the changes listed below were not applicable to the Py3k branch, and hence the corresponding
Nick Coghlan <ncoghlan@gmail.com>
parents: 55593
diff changeset
182 reload()."""
098ba3a63730 Note that a number of the changes listed below were not applicable to the Py3k branch, and hence the corresponding
Nick Coghlan <ncoghlan@gmail.com>
parents: 55593
diff changeset
183
098ba3a63730 Note that a number of the changes listed below were not applicable to the Py3k branch, and hence the corresponding
Nick Coghlan <ncoghlan@gmail.com>
parents: 55593
diff changeset
184 def test_source(self):
59763
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59762
diff changeset
185 # XXX (ncoghlan): It would be nice to use test.support.CleanImport
56592
098ba3a63730 Note that a number of the changes listed below were not applicable to the Py3k branch, and hence the corresponding
Nick Coghlan <ncoghlan@gmail.com>
parents: 55593
diff changeset
186 # here, but that breaks because the os module registers some
098ba3a63730 Note that a number of the changes listed below were not applicable to the Py3k branch, and hence the corresponding
Nick Coghlan <ncoghlan@gmail.com>
parents: 55593
diff changeset
187 # handlers in copy_reg on import. Since CleanImport doesn't
098ba3a63730 Note that a number of the changes listed below were not applicable to the Py3k branch, and hence the corresponding
Nick Coghlan <ncoghlan@gmail.com>
parents: 55593
diff changeset
188 # revert that registration, the module is left in a broken
098ba3a63730 Note that a number of the changes listed below were not applicable to the Py3k branch, and hence the corresponding
Nick Coghlan <ncoghlan@gmail.com>
parents: 55593
diff changeset
189 # state after reversion. Reinitialising the module contents
098ba3a63730 Note that a number of the changes listed below were not applicable to the Py3k branch, and hence the corresponding
Nick Coghlan <ncoghlan@gmail.com>
parents: 55593
diff changeset
190 # and just reverting os.environ to its previous state is an OK
098ba3a63730 Note that a number of the changes listed below were not applicable to the Py3k branch, and hence the corresponding
Nick Coghlan <ncoghlan@gmail.com>
parents: 55593
diff changeset
191 # workaround
098ba3a63730 Note that a number of the changes listed below were not applicable to the Py3k branch, and hence the corresponding
Nick Coghlan <ncoghlan@gmail.com>
parents: 55593
diff changeset
192 with support.EnvironmentVarGuard():
098ba3a63730 Note that a number of the changes listed below were not applicable to the Py3k branch, and hence the corresponding
Nick Coghlan <ncoghlan@gmail.com>
parents: 55593
diff changeset
193 import os
098ba3a63730 Note that a number of the changes listed below were not applicable to the Py3k branch, and hence the corresponding
Nick Coghlan <ncoghlan@gmail.com>
parents: 55593
diff changeset
194 imp.reload(os)
098ba3a63730 Note that a number of the changes listed below were not applicable to the Py3k branch, and hence the corresponding
Nick Coghlan <ncoghlan@gmail.com>
parents: 55593
diff changeset
195
098ba3a63730 Note that a number of the changes listed below were not applicable to the Py3k branch, and hence the corresponding
Nick Coghlan <ncoghlan@gmail.com>
parents: 55593
diff changeset
196 def test_extension(self):
098ba3a63730 Note that a number of the changes listed below were not applicable to the Py3k branch, and hence the corresponding
Nick Coghlan <ncoghlan@gmail.com>
parents: 55593
diff changeset
197 with support.CleanImport('time'):
098ba3a63730 Note that a number of the changes listed below were not applicable to the Py3k branch, and hence the corresponding
Nick Coghlan <ncoghlan@gmail.com>
parents: 55593
diff changeset
198 import time
098ba3a63730 Note that a number of the changes listed below were not applicable to the Py3k branch, and hence the corresponding
Nick Coghlan <ncoghlan@gmail.com>
parents: 55593
diff changeset
199 imp.reload(time)
098ba3a63730 Note that a number of the changes listed below were not applicable to the Py3k branch, and hence the corresponding
Nick Coghlan <ncoghlan@gmail.com>
parents: 55593
diff changeset
200
098ba3a63730 Note that a number of the changes listed below were not applicable to the Py3k branch, and hence the corresponding
Nick Coghlan <ncoghlan@gmail.com>
parents: 55593
diff changeset
201 def test_builtin(self):
098ba3a63730 Note that a number of the changes listed below were not applicable to the Py3k branch, and hence the corresponding
Nick Coghlan <ncoghlan@gmail.com>
parents: 55593
diff changeset
202 with support.CleanImport('marshal'):
098ba3a63730 Note that a number of the changes listed below were not applicable to the Py3k branch, and hence the corresponding
Nick Coghlan <ncoghlan@gmail.com>
parents: 55593
diff changeset
203 import marshal
098ba3a63730 Note that a number of the changes listed below were not applicable to the Py3k branch, and hence the corresponding
Nick Coghlan <ncoghlan@gmail.com>
parents: 55593
diff changeset
204 imp.reload(marshal)
44567
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 43632
diff changeset
205
43632
0067a6ab4854 Issue 1267, continued.
Guido van Rossum <guido@python.org>
parents: 43605
diff changeset
206
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
207 class PEP3147Tests(unittest.TestCase):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
208 """Tests of PEP 3147."""
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
209
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
210 tag = imp.get_tag()
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
211
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
212 def test_cache_from_source(self):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
213 # Given the path to a .py file, return the path to its PEP 3147
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
214 # defined .pyc file (i.e. under __pycache__).
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
215 self.assertEqual(
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
216 imp.cache_from_source('/foo/bar/baz/qux.py', True),
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
217 '/foo/bar/baz/__pycache__/qux.{}.pyc'.format(self.tag))
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
218
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
219 def test_cache_from_source_optimized(self):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
220 # Given the path to a .py file, return the path to its PEP 3147
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
221 # defined .pyo file (i.e. under __pycache__).
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
222 self.assertEqual(
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
223 imp.cache_from_source('/foo/bar/baz/qux.py', False),
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
224 '/foo/bar/baz/__pycache__/qux.{}.pyo'.format(self.tag))
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
225
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
226 def test_cache_from_source_cwd(self):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
227 self.assertEqual(imp.cache_from_source('foo.py', True),
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
228 os.sep.join(('__pycache__',
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
229 'foo.{}.pyc'.format(self.tag))))
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
230
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
231 def test_cache_from_source_override(self):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
232 # When debug_override is not None, it can be any true-ish or false-ish
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
233 # value.
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
234 self.assertEqual(
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
235 imp.cache_from_source('/foo/bar/baz.py', []),
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
236 '/foo/bar/__pycache__/baz.{}.pyo'.format(self.tag))
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
237 self.assertEqual(
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
238 imp.cache_from_source('/foo/bar/baz.py', [17]),
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
239 '/foo/bar/__pycache__/baz.{}.pyc'.format(self.tag))
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
240 # However if the bool-ishness can't be determined, the exception
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
241 # propagates.
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
242 class Bearish:
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
243 def __bool__(self): raise RuntimeError
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
244 self.assertRaises(
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
245 RuntimeError,
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
246 imp.cache_from_source, '/foo/bar/baz.py', Bearish())
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
247
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
248 @unittest.skipIf(os.altsep is None,
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
249 'test meaningful only where os.altsep is defined')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
250 def test_altsep_cache_from_source(self):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
251 # Windows path and PEP 3147.
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
252 self.assertEqual(
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
253 imp.cache_from_source('\\foo\\bar\\baz\\qux.py', True),
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
254 '\\foo\\bar\\baz\\__pycache__\\qux.{}.pyc'.format(self.tag))
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
255
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
256 @unittest.skipIf(os.altsep is None,
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
257 'test meaningful only where os.altsep is defined')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
258 def test_altsep_and_sep_cache_from_source(self):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
259 # Windows path and PEP 3147 where altsep is right of sep.
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
260 self.assertEqual(
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
261 imp.cache_from_source('\\foo\\bar/baz\\qux.py', True),
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
262 '\\foo\\bar/baz\\__pycache__\\qux.{}.pyc'.format(self.tag))
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
263
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
264 @unittest.skipIf(os.altsep is None,
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
265 'test meaningful only where os.altsep is defined')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
266 def test_sep_altsep_and_sep_cache_from_source(self):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
267 # Windows path and PEP 3147 where sep is right of altsep.
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
268 self.assertEqual(
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
269 imp.cache_from_source('\\foo\\bar\\baz/qux.py', True),
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
270 '\\foo\\bar\\baz/__pycache__/qux.{}.pyc'.format(self.tag))
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
271
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
272 def test_source_from_cache(self):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
273 # Given the path to a PEP 3147 defined .pyc file, return the path to
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
274 # its source. This tests the good path.
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
275 self.assertEqual(imp.source_from_cache(
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
276 '/foo/bar/baz/__pycache__/qux.{}.pyc'.format(self.tag)),
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
277 '/foo/bar/baz/qux.py')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
278
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
279 def test_source_from_cache_bad_path(self):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
280 # When the path to a pyc file is not in PEP 3147 format, a ValueError
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
281 # is raised.
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
282 self.assertRaises(
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
283 ValueError, imp.source_from_cache, '/foo/bar/bazqux.pyc')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
284
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
285 def test_source_from_cache_no_slash(self):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
286 # No slashes at all in path -> ValueError
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
287 self.assertRaises(
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
288 ValueError, imp.source_from_cache, 'foo.cpython-32.pyc')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
289
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
290 def test_source_from_cache_too_few_dots(self):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
291 # Too few dots in final path component -> ValueError
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
292 self.assertRaises(
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
293 ValueError, imp.source_from_cache, '__pycache__/foo.pyc')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
294
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
295 def test_source_from_cache_too_many_dots(self):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
296 # Too many dots in final path component -> ValueError
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
297 self.assertRaises(
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
298 ValueError, imp.source_from_cache,
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
299 '__pycache__/foo.cpython-32.foo.pyc')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
300
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
301 def test_source_from_cache_no__pycache__(self):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
302 # Another problem with the path -> ValueError
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
303 self.assertRaises(
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
304 ValueError, imp.source_from_cache,
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
305 '/foo/bar/foo.cpython-32.foo.pyc')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
306
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
307 def test_package___file__(self):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
308 # Test that a package's __file__ points to the right source directory.
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
309 os.mkdir('pep3147')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
310 sys.path.insert(0, os.curdir)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
311 def cleanup():
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
312 if sys.path[0] == os.curdir:
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
313 del sys.path[0]
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
314 shutil.rmtree('pep3147')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
315 self.addCleanup(cleanup)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
316 # Touch the __init__.py file.
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
317 with open('pep3147/__init__.py', 'w'):
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
318 pass
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
319 m = __import__('pep3147')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
320 # Ensure we load the pyc file.
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
321 support.forget('pep3147')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
322 m = __import__('pep3147')
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
323 self.assertEqual(m.__file__,
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
324 os.sep.join(('.', 'pep3147', '__init__.py')))
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
325
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
326
63945
03e884fac4db Issue #9425: NullImporter constructor is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents: 62805
diff changeset
327 class NullImporterTests(unittest.TestCase):
63960
291c9afab12c Fix a typo: TESTFN_UNENCODEABLE => TESTFN_UNENCODABLE
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63945
diff changeset
328 @unittest.skipIf(support.TESTFN_UNENCODABLE is None,
63945
03e884fac4db Issue #9425: NullImporter constructor is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents: 62805
diff changeset
329 "Need an undecodeable filename")
03e884fac4db Issue #9425: NullImporter constructor is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents: 62805
diff changeset
330 def test_unencodeable(self):
63960
291c9afab12c Fix a typo: TESTFN_UNENCODEABLE => TESTFN_UNENCODABLE
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63945
diff changeset
331 name = support.TESTFN_UNENCODABLE
63945
03e884fac4db Issue #9425: NullImporter constructor is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents: 62805
diff changeset
332 os.mkdir(name)
03e884fac4db Issue #9425: NullImporter constructor is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents: 62805
diff changeset
333 try:
03e884fac4db Issue #9425: NullImporter constructor is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents: 62805
diff changeset
334 self.assertRaises(ImportError, imp.NullImporter, name)
03e884fac4db Issue #9425: NullImporter constructor is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents: 62805
diff changeset
335 finally:
03e884fac4db Issue #9425: NullImporter constructor is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents: 62805
diff changeset
336 os.rmdir(name)
44567
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 43632
diff changeset
337
43632
0067a6ab4854 Issue 1267, continued.
Guido van Rossum <guido@python.org>
parents: 43605
diff changeset
338
27912
c5ebacbba0be Actually run these tests from regrtest.py.
Neal Norwitz <nnorwitz@gmail.com>
parents: 27853
diff changeset
339 def test_main():
49106
7087ea828dda Issue #3806: LockTests in test_imp should be skipped when thread is not available.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49007
diff changeset
340 tests = [
7087ea828dda Issue #3806: LockTests in test_imp should be skipped when thread is not available.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49007
diff changeset
341 ImportTests,
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
342 PEP3147Tests,
56592
098ba3a63730 Note that a number of the changes listed below were not applicable to the Py3k branch, and hence the corresponding
Nick Coghlan <ncoghlan@gmail.com>
parents: 55593
diff changeset
343 ReloadTests,
63945
03e884fac4db Issue #9425: NullImporter constructor is fully unicode compliant
Victor Stinner <victor.stinner@haypocalc.com>
parents: 62805
diff changeset
344 NullImporterTests,
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59763
diff changeset
345 ]
49106
7087ea828dda Issue #3806: LockTests in test_imp should be skipped when thread is not available.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49007
diff changeset
346 try:
7087ea828dda Issue #3806: LockTests in test_imp should be skipped when thread is not available.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49007
diff changeset
347 import _thread
7087ea828dda Issue #3806: LockTests in test_imp should be skipped when thread is not available.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49007
diff changeset
348 except ImportError:
7087ea828dda Issue #3806: LockTests in test_imp should be skipped when thread is not available.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49007
diff changeset
349 pass
7087ea828dda Issue #3806: LockTests in test_imp should be skipped when thread is not available.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49007
diff changeset
350 else:
7087ea828dda Issue #3806: LockTests in test_imp should be skipped when thread is not available.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49007
diff changeset
351 tests.append(LockTests)
7087ea828dda Issue #3806: LockTests in test_imp should be skipped when thread is not available.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49007
diff changeset
352 support.run_unittest(*tests)
27912
c5ebacbba0be Actually run these tests from regrtest.py.
Neal Norwitz <nnorwitz@gmail.com>
parents: 27853
diff changeset
353
27853
5620b0786271 Cleanup from patch #683257:
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
354 if __name__ == "__main__":
27912
c5ebacbba0be Actually run these tests from regrtest.py.
Neal Norwitz <nnorwitz@gmail.com>
parents: 27853
diff changeset
355 test_main()