Skip to content

Commit dd8d3c1

Browse files
authored
Merge pull request RustPython#2419 from ishigoya/master
remaining test_unicode* from v3.8.7
2 parents 7cdafe7 + dbc3d64 commit dd8d3c1

File tree

4 files changed

+728
-0
lines changed

4 files changed

+728
-0
lines changed

Lib/test/test_unicode_file.py

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Test some Unicode file name semantics
2+
# We don't test many operations on files other than
3+
# that their names can be used with Unicode characters.
4+
import os, glob, time, shutil
5+
import unicodedata
6+
7+
import unittest
8+
from test.support import (run_unittest, rmtree, change_cwd,
9+
TESTFN_ENCODING, TESTFN_UNICODE, TESTFN_UNENCODABLE, create_empty_file)
10+
11+
if not os.path.supports_unicode_filenames:
12+
try:
13+
TESTFN_UNICODE.encode(TESTFN_ENCODING)
14+
except (UnicodeError, TypeError):
15+
# Either the file system encoding is None, or the file name
16+
# cannot be encoded in the file system encoding.
17+
raise unittest.SkipTest("No Unicode filesystem semantics on this platform.")
18+
19+
def remove_if_exists(filename):
20+
if os.path.exists(filename):
21+
os.unlink(filename)
22+
23+
class TestUnicodeFiles(unittest.TestCase):
24+
# The 'do_' functions are the actual tests. They generally assume the
25+
# file already exists etc.
26+
27+
# Do all the tests we can given only a single filename. The file should
28+
# exist.
29+
def _do_single(self, filename):
30+
self.assertTrue(os.path.exists(filename))
31+
self.assertTrue(os.path.isfile(filename))
32+
self.assertTrue(os.access(filename, os.R_OK))
33+
self.assertTrue(os.path.exists(os.path.abspath(filename)))
34+
self.assertTrue(os.path.isfile(os.path.abspath(filename)))
35+
self.assertTrue(os.access(os.path.abspath(filename), os.R_OK))
36+
os.chmod(filename, 0o777)
37+
os.utime(filename, None)
38+
os.utime(filename, (time.time(), time.time()))
39+
# Copy/rename etc tests using the same filename
40+
self._do_copyish(filename, filename)
41+
# Filename should appear in glob output
42+
self.assertTrue(
43+
os.path.abspath(filename)==os.path.abspath(glob.glob(glob.escape(filename))[0]))
44+
# basename should appear in listdir.
45+
path, base = os.path.split(os.path.abspath(filename))
46+
file_list = os.listdir(path)
47+
# Normalize the unicode strings, as round-tripping the name via the OS
48+
# may return a different (but equivalent) value.
49+
base = unicodedata.normalize("NFD", base)
50+
file_list = [unicodedata.normalize("NFD", f) for f in file_list]
51+
52+
self.assertIn(base, file_list)
53+
54+
# Tests that copy, move, etc one file to another.
55+
def _do_copyish(self, filename1, filename2):
56+
# Should be able to rename the file using either name.
57+
self.assertTrue(os.path.isfile(filename1)) # must exist.
58+
os.rename(filename1, filename2 + ".new")
59+
self.assertFalse(os.path.isfile(filename2))
60+
self.assertTrue(os.path.isfile(filename1 + '.new'))
61+
os.rename(filename1 + ".new", filename2)
62+
self.assertFalse(os.path.isfile(filename1 + '.new'))
63+
self.assertTrue(os.path.isfile(filename2))
64+
65+
shutil.copy(filename1, filename2 + ".new")
66+
os.unlink(filename1 + ".new") # remove using equiv name.
67+
# And a couple of moves, one using each name.
68+
shutil.move(filename1, filename2 + ".new")
69+
self.assertFalse(os.path.exists(filename2))
70+
self.assertTrue(os.path.exists(filename1 + '.new'))
71+
shutil.move(filename1 + ".new", filename2)
72+
self.assertFalse(os.path.exists(filename2 + '.new'))
73+
self.assertTrue(os.path.exists(filename1))
74+
# Note - due to the implementation of shutil.move,
75+
# it tries a rename first. This only fails on Windows when on
76+
# different file systems - and this test can't ensure that.
77+
# So we test the shutil.copy2 function, which is the thing most
78+
# likely to fail.
79+
shutil.copy2(filename1, filename2 + ".new")
80+
self.assertTrue(os.path.isfile(filename1 + '.new'))
81+
os.unlink(filename1 + ".new")
82+
self.assertFalse(os.path.exists(filename2 + '.new'))
83+
84+
def _do_directory(self, make_name, chdir_name):
85+
if os.path.isdir(make_name):
86+
rmtree(make_name)
87+
os.mkdir(make_name)
88+
try:
89+
with change_cwd(chdir_name):
90+
cwd_result = os.getcwd()
91+
name_result = make_name
92+
93+
cwd_result = unicodedata.normalize("NFD", cwd_result)
94+
name_result = unicodedata.normalize("NFD", name_result)
95+
96+
self.assertEqual(os.path.basename(cwd_result),name_result)
97+
finally:
98+
os.rmdir(make_name)
99+
100+
# The '_test' functions 'entry points with params' - ie, what the
101+
# top-level 'test' functions would be if they could take params
102+
def _test_single(self, filename):
103+
remove_if_exists(filename)
104+
create_empty_file(filename)
105+
try:
106+
self._do_single(filename)
107+
finally:
108+
os.unlink(filename)
109+
self.assertTrue(not os.path.exists(filename))
110+
# and again with os.open.
111+
f = os.open(filename, os.O_CREAT)
112+
os.close(f)
113+
try:
114+
self._do_single(filename)
115+
finally:
116+
os.unlink(filename)
117+
118+
# The 'test' functions are unittest entry points, and simply call our
119+
# _test functions with each of the filename combinations we wish to test
120+
@unittest.skip("TODO: RUSTPYTHON")
121+
def test_single_files(self):
122+
self._test_single(TESTFN_UNICODE)
123+
if TESTFN_UNENCODABLE is not None:
124+
self._test_single(TESTFN_UNENCODABLE)
125+
126+
def test_directories(self):
127+
# For all 'equivalent' combinations:
128+
# Make dir with encoded, chdir with unicode, checkdir with encoded
129+
# (or unicode/encoded/unicode, etc
130+
ext = ".dir"
131+
self._do_directory(TESTFN_UNICODE+ext, TESTFN_UNICODE+ext)
132+
# Our directory name that can't use a non-unicode name.
133+
if TESTFN_UNENCODABLE is not None:
134+
self._do_directory(TESTFN_UNENCODABLE+ext,
135+
TESTFN_UNENCODABLE+ext)
136+
137+
def test_main():
138+
run_unittest(__name__)
139+
140+
if __name__ == "__main__":
141+
test_main()
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
# Test the Unicode versions of normal file functions
2+
# open, os.open, os.stat. os.listdir, os.rename, os.remove, os.mkdir, os.chdir, os.rmdir
3+
import os
4+
import sys
5+
import unittest
6+
import warnings
7+
from unicodedata import normalize
8+
from test import support
9+
10+
filenames = [
11+
'1_abc',
12+
'2_ascii',
13+
'3_Gr\xfc\xdf-Gott',
14+
'4_\u0393\u03b5\u03b9\u03ac-\u03c3\u03b1\u03c2',
15+
'5_\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435',
16+
'6_\u306b\u307d\u3093',
17+
'7_\u05d4\u05e9\u05e7\u05e6\u05e5\u05e1',
18+
'8_\u66e8\u66e9\u66eb',
19+
'9_\u66e8\u05e9\u3093\u0434\u0393\xdf',
20+
# Specific code points: fn, NFC(fn) and NFKC(fn) all different
21+
'10_\u1fee\u1ffd',
22+
]
23+
24+
# Mac OS X decomposes Unicode names, using Normal Form D.
25+
# http://developer.apple.com/mac/library/qa/qa2001/qa1173.html
26+
# "However, most volume formats do not follow the exact specification for
27+
# these normal forms. For example, HFS Plus uses a variant of Normal Form D
28+
# in which U+2000 through U+2FFF, U+F900 through U+FAFF, and U+2F800 through
29+
# U+2FAFF are not decomposed."
30+
if sys.platform != 'darwin':
31+
filenames.extend([
32+
# Specific code points: NFC(fn), NFD(fn), NFKC(fn) and NFKD(fn) all different
33+
'11_\u0385\u03d3\u03d4',
34+
'12_\u00a8\u0301\u03d2\u0301\u03d2\u0308', # == NFD('\u0385\u03d3\u03d4')
35+
'13_\u0020\u0308\u0301\u038e\u03ab', # == NFKC('\u0385\u03d3\u03d4')
36+
'14_\u1e9b\u1fc1\u1fcd\u1fce\u1fcf\u1fdd\u1fde\u1fdf\u1fed',
37+
38+
# Specific code points: fn, NFC(fn) and NFKC(fn) all different
39+
'15_\u1fee\u1ffd\ufad1',
40+
'16_\u2000\u2000\u2000A',
41+
'17_\u2001\u2001\u2001A',
42+
'18_\u2003\u2003\u2003A', # == NFC('\u2001\u2001\u2001A')
43+
'19_\u0020\u0020\u0020A', # '\u0020' == ' ' == NFKC('\u2000') ==
44+
# NFKC('\u2001') == NFKC('\u2003')
45+
])
46+
47+
48+
# Is it Unicode-friendly?
49+
if not os.path.supports_unicode_filenames:
50+
fsencoding = sys.getfilesystemencoding()
51+
try:
52+
for name in filenames:
53+
name.encode(fsencoding)
54+
except UnicodeEncodeError:
55+
raise unittest.SkipTest("only NT+ and systems with "
56+
"Unicode-friendly filesystem encoding")
57+
58+
59+
class UnicodeFileTests(unittest.TestCase):
60+
files = set(filenames)
61+
normal_form = None
62+
63+
def setUp(self):
64+
try:
65+
os.mkdir(support.TESTFN)
66+
except FileExistsError:
67+
pass
68+
self.addCleanup(support.rmtree, support.TESTFN)
69+
70+
files = set()
71+
for name in self.files:
72+
name = os.path.join(support.TESTFN, self.norm(name))
73+
with open(name, 'wb') as f:
74+
f.write((name+'\n').encode("utf-8"))
75+
os.stat(name)
76+
files.add(name)
77+
self.files = files
78+
79+
def norm(self, s):
80+
if self.normal_form:
81+
return normalize(self.normal_form, s)
82+
return s
83+
84+
def _apply_failure(self, fn, filename,
85+
expected_exception=FileNotFoundError,
86+
check_filename=True):
87+
with self.assertRaises(expected_exception) as c:
88+
fn(filename)
89+
exc_filename = c.exception.filename
90+
if check_filename:
91+
self.assertEqual(exc_filename, filename, "Function '%s(%a) failed "
92+
"with bad filename in the exception: %a" %
93+
(fn.__name__, filename, exc_filename))
94+
95+
@unittest.skip("TODO: RUSTPYTHON")
96+
def test_failures(self):
97+
# Pass non-existing Unicode filenames all over the place.
98+
for name in self.files:
99+
name = "not_" + name
100+
self._apply_failure(open, name)
101+
self._apply_failure(os.stat, name)
102+
self._apply_failure(os.chdir, name)
103+
self._apply_failure(os.rmdir, name)
104+
self._apply_failure(os.remove, name)
105+
self._apply_failure(os.listdir, name)
106+
107+
if sys.platform == 'win32':
108+
# Windows is lunatic. Issue #13366.
109+
_listdir_failure = NotADirectoryError, FileNotFoundError
110+
else:
111+
_listdir_failure = NotADirectoryError
112+
113+
@unittest.skip("TODO: RUSTPYTHON")
114+
def test_open(self):
115+
for name in self.files:
116+
f = open(name, 'wb')
117+
f.write((name+'\n').encode("utf-8"))
118+
f.close()
119+
os.stat(name)
120+
self._apply_failure(os.listdir, name, self._listdir_failure)
121+
122+
# Skip the test on darwin, because darwin does normalize the filename to
123+
# NFD (a variant of Unicode NFD form). Normalize the filename to NFC, NFKC,
124+
# NFKD in Python is useless, because darwin will normalize it later and so
125+
# open(), os.stat(), etc. don't raise any exception.
126+
@unittest.skip("TODO: RUSTPYTHON")
127+
@unittest.skipIf(sys.platform == 'darwin', 'irrelevant test on Mac OS X')
128+
def test_normalize(self):
129+
files = set(self.files)
130+
others = set()
131+
for nf in set(['NFC', 'NFD', 'NFKC', 'NFKD']):
132+
others |= set(normalize(nf, file) for file in files)
133+
others -= files
134+
for name in others:
135+
self._apply_failure(open, name)
136+
self._apply_failure(os.stat, name)
137+
self._apply_failure(os.chdir, name)
138+
self._apply_failure(os.rmdir, name)
139+
self._apply_failure(os.remove, name)
140+
self._apply_failure(os.listdir, name)
141+
142+
# Skip the test on darwin, because darwin uses a normalization different
143+
# than Python NFD normalization: filenames are different even if we use
144+
# Python NFD normalization.
145+
@unittest.skipIf(sys.platform == 'darwin', 'irrelevant test on Mac OS X')
146+
def test_listdir(self):
147+
sf0 = set(self.files)
148+
with warnings.catch_warnings():
149+
warnings.simplefilter("ignore", DeprecationWarning)
150+
f1 = os.listdir(support.TESTFN.encode(sys.getfilesystemencoding()))
151+
f2 = os.listdir(support.TESTFN)
152+
sf2 = set(os.path.join(support.TESTFN, f) for f in f2)
153+
self.assertEqual(sf0, sf2, "%a != %a" % (sf0, sf2))
154+
self.assertEqual(len(f1), len(f2))
155+
156+
def test_rename(self):
157+
for name in self.files:
158+
os.rename(name, "tmp")
159+
os.rename("tmp", name)
160+
161+
def test_directory(self):
162+
dirname = os.path.join(support.TESTFN, 'Gr\xfc\xdf-\u66e8\u66e9\u66eb')
163+
filename = '\xdf-\u66e8\u66e9\u66eb'
164+
with support.temp_cwd(dirname):
165+
with open(filename, 'wb') as f:
166+
f.write((filename + '\n').encode("utf-8"))
167+
os.access(filename,os.R_OK)
168+
os.remove(filename)
169+
170+
171+
class UnicodeNFCFileTests(UnicodeFileTests):
172+
normal_form = 'NFC'
173+
174+
175+
class UnicodeNFDFileTests(UnicodeFileTests):
176+
normal_form = 'NFD'
177+
178+
179+
class UnicodeNFKCFileTests(UnicodeFileTests):
180+
normal_form = 'NFKC'
181+
182+
183+
class UnicodeNFKDFileTests(UnicodeFileTests):
184+
normal_form = 'NFKD'
185+
186+
187+
def test_main():
188+
support.run_unittest(
189+
UnicodeFileTests,
190+
UnicodeNFCFileTests,
191+
UnicodeNFDFileTests,
192+
UnicodeNFKCFileTests,
193+
UnicodeNFKDFileTests,
194+
)
195+
196+
197+
if __name__ == "__main__":
198+
test_main()

Lib/test/test_unicode_identifiers.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import unittest
2+
3+
class PEP3131Test(unittest.TestCase):
4+
5+
@unittest.skip("TODO: RUSTPYTHON")
6+
def test_valid(self):
7+
class T:
8+
ä = 1
9+
µ = 2 # this is a compatibility character
10+
= 3
11+
x󠄀 = 4
12+
self.assertEqual(getattr(T, "\xe4"), 1)
13+
self.assertEqual(getattr(T, "\u03bc"), 2)
14+
self.assertEqual(getattr(T, '\u87d2'), 3)
15+
self.assertEqual(getattr(T, 'x\U000E0100'), 4)
16+
17+
# TODO: RUSTPYTHON
18+
@unittest.expectedFailure
19+
def test_non_bmp_normalized(self):
20+
𝔘𝔫𝔦𝔠𝔬𝔡𝔢 = 1
21+
self.assertIn("Unicode", dir())
22+
23+
@unittest.skip("TODO: RUSTPYTHON")
24+
def test_invalid(self):
25+
try:
26+
from test import badsyntax_3131
27+
except SyntaxError as s:
28+
self.assertEqual(str(s),
29+
"invalid character in identifier (badsyntax_3131.py, line 2)")
30+
else:
31+
self.fail("expected exception didn't occur")
32+
33+
if __name__ == "__main__":
34+
unittest.main()

0 commit comments

Comments
 (0)