Skip to content

Commit fdcdc16

Browse files
authored
Merge pull request #48 from RustPython/master
Sync Fork from Upstream Repo
2 parents 1276581 + a8b9113 commit fdcdc16

File tree

9 files changed

+3299
-2
lines changed

9 files changed

+3299
-2
lines changed

Lib/test/test_importlib/test_locks.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def test_deadlock(self):
9696
self.assertGreaterEqual(nb_deadlocks, 1)
9797
self.assertEqual(results.count((True, True)), len(results) - nb_deadlocks)
9898

99+
@unittest.skip("TODO: RUSTPYTHON, flaky test")
99100
def test_no_deadlock(self):
100101
results = self.run_deadlock_avoidance_test(False)
101102
self.assertEqual(results.count((True, False)), 0)

Lib/test/test_threading.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,11 @@ class PyRLockTests(lock_tests.RLockTests):
13291329
class CRLockTests(lock_tests.RLockTests):
13301330
locktype = staticmethod(threading._CRLock)
13311331

1332+
# TODO: RUSTPYTHON
1333+
@unittest.skip("TODO: RUSTPYTHON, flaky test")
1334+
def test_different_thread(self):
1335+
super().test_different_thread()
1336+
13321337
# TODO: RUSTPYTHON
13331338
@unittest.expectedFailure
13341339
def test_release_save_unacquired(self):

Lib/test/test_utf8_mode.py

Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
"""
2+
Test the implementation of the PEP 540: the UTF-8 Mode.
3+
"""
4+
5+
import locale
6+
import sys
7+
import textwrap
8+
import unittest
9+
from test import support
10+
from test.support.script_helper import assert_python_ok, assert_python_failure
11+
12+
13+
MS_WINDOWS = (sys.platform == 'win32')
14+
POSIX_LOCALES = ('C', 'POSIX')
15+
VXWORKS = (sys.platform == "vxworks")
16+
17+
class UTF8ModeTests(unittest.TestCase):
18+
DEFAULT_ENV = {
19+
'PYTHONUTF8': '',
20+
'PYTHONLEGACYWINDOWSFSENCODING': '',
21+
'PYTHONCOERCECLOCALE': '0',
22+
}
23+
24+
def posix_locale(self):
25+
loc = locale.setlocale(locale.LC_CTYPE, None)
26+
return (loc in POSIX_LOCALES)
27+
28+
def get_output(self, *args, failure=False, **kw):
29+
kw = dict(self.DEFAULT_ENV, **kw)
30+
if failure:
31+
out = assert_python_failure(*args, **kw)
32+
out = out[2]
33+
else:
34+
out = assert_python_ok(*args, **kw)
35+
out = out[1]
36+
return out.decode().rstrip("\n\r")
37+
38+
@unittest.skipIf(MS_WINDOWS, 'Windows has no POSIX locale')
39+
def test_posix_locale(self):
40+
code = 'import sys; print(sys.flags.utf8_mode)'
41+
42+
for loc in POSIX_LOCALES:
43+
with self.subTest(LC_ALL=loc):
44+
out = self.get_output('-c', code, LC_ALL=loc)
45+
self.assertEqual(out, '1')
46+
47+
# TODO: RUSTPYTHON
48+
@unittest.expectedFailure
49+
def test_xoption(self):
50+
code = 'import sys; print(sys.flags.utf8_mode)'
51+
52+
out = self.get_output('-X', 'utf8', '-c', code)
53+
self.assertEqual(out, '1')
54+
55+
# undocumented but accepted syntax: -X utf8=1
56+
out = self.get_output('-X', 'utf8=1', '-c', code)
57+
self.assertEqual(out, '1')
58+
59+
out = self.get_output('-X', 'utf8=0', '-c', code)
60+
self.assertEqual(out, '0')
61+
62+
if MS_WINDOWS:
63+
# PYTHONLEGACYWINDOWSFSENCODING disables the UTF-8 Mode
64+
# and has the priority over -X utf8
65+
out = self.get_output('-X', 'utf8', '-c', code,
66+
PYTHONLEGACYWINDOWSFSENCODING='1')
67+
self.assertEqual(out, '0')
68+
69+
# TODO: RUSTPYTHON
70+
@unittest.expectedFailure
71+
def test_env_var(self):
72+
code = 'import sys; print(sys.flags.utf8_mode)'
73+
74+
out = self.get_output('-c', code, PYTHONUTF8='1')
75+
self.assertEqual(out, '1')
76+
77+
out = self.get_output('-c', code, PYTHONUTF8='0')
78+
self.assertEqual(out, '0')
79+
80+
# -X utf8 has the priority over PYTHONUTF8
81+
out = self.get_output('-X', 'utf8=0', '-c', code, PYTHONUTF8='1')
82+
self.assertEqual(out, '0')
83+
84+
if MS_WINDOWS:
85+
# PYTHONLEGACYWINDOWSFSENCODING disables the UTF-8 mode
86+
# and has the priority over PYTHONUTF8
87+
out = self.get_output('-X', 'utf8', '-c', code, PYTHONUTF8='1',
88+
PYTHONLEGACYWINDOWSFSENCODING='1')
89+
self.assertEqual(out, '0')
90+
91+
# Cannot test with the POSIX locale, since the POSIX locale enables
92+
# the UTF-8 mode
93+
if not self.posix_locale():
94+
# PYTHONUTF8 should be ignored if -E is used
95+
out = self.get_output('-E', '-c', code, PYTHONUTF8='1')
96+
self.assertEqual(out, '0')
97+
98+
# invalid mode
99+
out = self.get_output('-c', code, PYTHONUTF8='xxx', failure=True)
100+
self.assertIn('invalid PYTHONUTF8 environment variable value',
101+
out.rstrip())
102+
103+
def test_filesystemencoding(self):
104+
code = textwrap.dedent('''
105+
import sys
106+
print("{}/{}".format(sys.getfilesystemencoding(),
107+
sys.getfilesystemencodeerrors()))
108+
''')
109+
110+
if MS_WINDOWS:
111+
expected = 'utf-8/surrogatepass'
112+
else:
113+
expected = 'utf-8/surrogateescape'
114+
115+
out = self.get_output('-X', 'utf8', '-c', code)
116+
self.assertEqual(out, expected)
117+
118+
if MS_WINDOWS:
119+
# PYTHONLEGACYWINDOWSFSENCODING disables the UTF-8 mode
120+
# and has the priority over -X utf8 and PYTHONUTF8
121+
out = self.get_output('-X', 'utf8', '-c', code,
122+
PYTHONUTF8='strict',
123+
PYTHONLEGACYWINDOWSFSENCODING='1')
124+
self.assertEqual(out, 'mbcs/replace')
125+
126+
# TODO: RUSTPYTHON
127+
if MS_WINDOWS:
128+
test_filesystemencoding = unittest.expectedFailure(test_filesystemencoding)
129+
130+
# TODO: RUSTPYTHON
131+
@unittest.expectedFailure
132+
def test_stdio(self):
133+
code = textwrap.dedent('''
134+
import sys
135+
print(f"stdin: {sys.stdin.encoding}/{sys.stdin.errors}")
136+
print(f"stdout: {sys.stdout.encoding}/{sys.stdout.errors}")
137+
print(f"stderr: {sys.stderr.encoding}/{sys.stderr.errors}")
138+
''')
139+
140+
out = self.get_output('-X', 'utf8', '-c', code,
141+
PYTHONIOENCODING='')
142+
self.assertEqual(out.splitlines(),
143+
['stdin: utf-8/surrogateescape',
144+
'stdout: utf-8/surrogateescape',
145+
'stderr: utf-8/backslashreplace'])
146+
147+
# PYTHONIOENCODING has the priority over PYTHONUTF8
148+
out = self.get_output('-X', 'utf8', '-c', code,
149+
PYTHONIOENCODING="latin1")
150+
self.assertEqual(out.splitlines(),
151+
['stdin: iso8859-1/strict',
152+
'stdout: iso8859-1/strict',
153+
'stderr: iso8859-1/backslashreplace'])
154+
155+
out = self.get_output('-X', 'utf8', '-c', code,
156+
PYTHONIOENCODING=":namereplace")
157+
self.assertEqual(out.splitlines(),
158+
['stdin: utf-8/namereplace',
159+
'stdout: utf-8/namereplace',
160+
'stderr: utf-8/backslashreplace'])
161+
162+
# TODO: RUSTPYTHON
163+
@unittest.expectedFailure
164+
def test_io(self):
165+
code = textwrap.dedent('''
166+
import sys
167+
filename = sys.argv[1]
168+
with open(filename) as fp:
169+
print(f"{fp.encoding}/{fp.errors}")
170+
''')
171+
filename = __file__
172+
173+
out = self.get_output('-c', code, filename, PYTHONUTF8='1')
174+
self.assertEqual(out, 'UTF-8/strict')
175+
176+
def _check_io_encoding(self, module, encoding=None, errors=None):
177+
filename = __file__
178+
179+
# Encoding explicitly set
180+
args = []
181+
if encoding:
182+
args.append(f'encoding={encoding!r}')
183+
if errors:
184+
args.append(f'errors={errors!r}')
185+
code = textwrap.dedent('''
186+
import sys
187+
from %s import open
188+
filename = sys.argv[1]
189+
with open(filename, %s) as fp:
190+
print(f"{fp.encoding}/{fp.errors}")
191+
''') % (module, ', '.join(args))
192+
out = self.get_output('-c', code, filename,
193+
PYTHONUTF8='1')
194+
195+
if not encoding:
196+
encoding = 'UTF-8'
197+
if not errors:
198+
errors = 'strict'
199+
self.assertEqual(out, f'{encoding}/{errors}')
200+
201+
def check_io_encoding(self, module):
202+
self._check_io_encoding(module, encoding="latin1")
203+
self._check_io_encoding(module, errors="namereplace")
204+
self._check_io_encoding(module,
205+
encoding="latin1", errors="namereplace")
206+
207+
# TODO: RUSTPYTHON
208+
@unittest.expectedFailure
209+
def test_io_encoding(self):
210+
self.check_io_encoding('io')
211+
212+
def test_pyio_encoding(self):
213+
self.check_io_encoding('_pyio')
214+
215+
def test_locale_getpreferredencoding(self):
216+
code = 'import locale; print(locale.getpreferredencoding(False), locale.getpreferredencoding(True))'
217+
out = self.get_output('-X', 'utf8', '-c', code)
218+
self.assertEqual(out, 'UTF-8 UTF-8')
219+
220+
for loc in POSIX_LOCALES:
221+
with self.subTest(LC_ALL=loc):
222+
out = self.get_output('-X', 'utf8', '-c', code, LC_ALL=loc)
223+
self.assertEqual(out, 'UTF-8 UTF-8')
224+
225+
# TODO: RUSTPYTHON
226+
@unittest.expectedFailure
227+
@unittest.skipIf(MS_WINDOWS, 'test specific to Unix')
228+
def test_cmd_line(self):
229+
arg = 'h\xe9\u20ac'.encode('utf-8')
230+
arg_utf8 = arg.decode('utf-8')
231+
arg_ascii = arg.decode('ascii', 'surrogateescape')
232+
code = 'import locale, sys; print("%s:%s" % (locale.getpreferredencoding(), ascii(sys.argv[1:])))'
233+
234+
def check(utf8_opt, expected, **kw):
235+
out = self.get_output('-X', utf8_opt, '-c', code, arg, **kw)
236+
args = out.partition(':')[2].rstrip()
237+
self.assertEqual(args, ascii(expected), out)
238+
239+
check('utf8', [arg_utf8])
240+
for loc in POSIX_LOCALES:
241+
with self.subTest(LC_ALL=loc):
242+
check('utf8', [arg_utf8], LC_ALL=loc)
243+
244+
if sys.platform == 'darwin' or support.is_android or VXWORKS:
245+
c_arg = arg_utf8
246+
elif sys.platform.startswith("aix"):
247+
c_arg = arg.decode('iso-8859-1')
248+
else:
249+
c_arg = arg_ascii
250+
for loc in POSIX_LOCALES:
251+
with self.subTest(LC_ALL=loc):
252+
check('utf8=0', [c_arg], LC_ALL=loc)
253+
254+
def test_optim_level(self):
255+
# CPython: check that Py_Main() doesn't increment Py_OptimizeFlag
256+
# twice when -X utf8 requires to parse the configuration twice (when
257+
# the encoding changes after reading the configuration, the
258+
# configuration is read again with the new encoding).
259+
code = 'import sys; print(sys.flags.optimize)'
260+
out = self.get_output('-X', 'utf8', '-O', '-c', code)
261+
self.assertEqual(out, '1')
262+
out = self.get_output('-X', 'utf8', '-OO', '-c', code)
263+
self.assertEqual(out, '2')
264+
265+
code = 'import sys; print(sys.flags.ignore_environment)'
266+
out = self.get_output('-X', 'utf8', '-E', '-c', code)
267+
self.assertEqual(out, '1')
268+
269+
270+
if __name__ == "__main__":
271+
unittest.main()

Lib/test/test_utf8source.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# This file is marked as binary in the CVS, to prevent MacCVS from recoding it.
2+
3+
import unittest
4+
5+
class PEP3120Test(unittest.TestCase):
6+
7+
def test_pep3120(self):
8+
self.assertEqual(
9+
"Питон".encode("utf-8"),
10+
b'\xd0\x9f\xd0\xb8\xd1\x82\xd0\xbe\xd0\xbd'
11+
)
12+
self.assertEqual(
13+
"\П".encode("utf-8"),
14+
b'\\\xd0\x9f'
15+
)
16+
17+
# TODO: RUSTPYTHON
18+
@unittest.expectedFailure # "badsyntax_pep3120.py" may make the WASM CI fail
19+
def test_badsyntax(self):
20+
try:
21+
import test.badsyntax_pep3120
22+
except SyntaxError as msg:
23+
msg = str(msg).lower()
24+
self.assertTrue('utf-8' in msg)
25+
else:
26+
self.fail("expected exception didn't occur")
27+
28+
29+
class BuiltinCompileTests(unittest.TestCase):
30+
31+
# TODO: RUSTPYTHON
32+
@unittest.expectedFailure
33+
# Issue 3574.
34+
def test_latin1(self):
35+
# Allow compile() to read Latin-1 source.
36+
source_code = '# coding: Latin-1\nu = "Ç"\n'.encode("Latin-1")
37+
try:
38+
code = compile(source_code, '<dummy>', 'exec')
39+
except SyntaxError:
40+
self.fail("compile() cannot handle Latin-1 source")
41+
ns = {}
42+
exec(code, ns)
43+
self.assertEqual('Ç', ns['u'])
44+
45+
46+
if __name__ == "__main__":
47+
unittest.main()

0 commit comments

Comments
 (0)