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.support import os_helper
9 from test import support
10
11
12 filenames = [
13 '1_abc',
14 '2_ascii',
15 '3_Gr\xfc\xdf-Gott',
16 '4_\u0393\u03b5\u03b9\u03ac-\u03c3\u03b1\u03c2',
17 '5_\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435',
18 '6_\u306b\u307d\u3093',
19 '7_\u05d4\u05e9\u05e7\u05e6\u05e5\u05e1',
20 '8_\u66e8\u66e9\u66eb',
21 '9_\u66e8\u05e9\u3093\u0434\u0393\xdf',
22 # Specific code points: fn, NFC(fn) and NFKC(fn) all different
23 '10_\u1fee\u1ffd',
24 ]
25
26 # Mac OS X decomposes Unicode names, using Normal Form D.
27 # http://developer.apple.com/mac/library/qa/qa2001/qa1173.html
28 # "However, most volume formats do not follow the exact specification for
29 # these normal forms. For example, HFS Plus uses a variant of Normal Form D
30 # in which U+2000 through U+2FFF, U+F900 through U+FAFF, and U+2F800 through
31 # U+2FAFF are not decomposed."
32 if sys.platform != 'darwin':
33 filenames.extend([
34 # Specific code points: NFC(fn), NFD(fn), NFKC(fn) and NFKD(fn) all different
35 '11_\u0385\u03d3\u03d4',
36 '12_\u00a8\u0301\u03d2\u0301\u03d2\u0308', # == NFD('\u0385\u03d3\u03d4')
37 '13_\u0020\u0308\u0301\u038e\u03ab', # == NFKC('\u0385\u03d3\u03d4')
38 '14_\u1e9b\u1fc1\u1fcd\u1fce\u1fcf\u1fdd\u1fde\u1fdf\u1fed',
39
40 # Specific code points: fn, NFC(fn) and NFKC(fn) all different
41 '15_\u1fee\u1ffd\ufad1',
42 '16_\u2000\u2000\u2000A',
43 '17_\u2001\u2001\u2001A',
44 '18_\u2003\u2003\u2003A', # == NFC('\u2001\u2001\u2001A')
45 '19_\u0020\u0020\u0020A', # '\u0020' == ' ' == NFKC('\u2000') ==
46 # NFKC('\u2001') == NFKC('\u2003')
47 ])
48
49
50 # Is it Unicode-friendly?
51 if not os.path.supports_unicode_filenames:
52 fsencoding = sys.getfilesystemencoding()
53 try:
54 for name in filenames:
55 name.encode(fsencoding)
56 except UnicodeEncodeError:
57 raise unittest.SkipTest("only NT+ and systems with "
58 "Unicode-friendly filesystem encoding")
59
60
61 class ESC[4;38;5;81mUnicodeFileTests(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
62 files = set(filenames)
63 normal_form = None
64
65 def setUp(self):
66 try:
67 os.mkdir(os_helper.TESTFN)
68 except FileExistsError:
69 pass
70 self.addCleanup(os_helper.rmtree, os_helper.TESTFN)
71
72 files = set()
73 for name in self.files:
74 name = os.path.join(os_helper.TESTFN, self.norm(name))
75 with open(name, 'wb') as f:
76 f.write((name+'\n').encode("utf-8"))
77 os.stat(name)
78 files.add(name)
79 self.files = files
80
81 def norm(self, s):
82 if self.normal_form:
83 return normalize(self.normal_form, s)
84 return s
85
86 def _apply_failure(self, fn, filename,
87 expected_exception=FileNotFoundError,
88 check_filename=True):
89 with self.assertRaises(expected_exception) as c:
90 fn(filename)
91 exc_filename = c.exception.filename
92 if check_filename:
93 self.assertEqual(exc_filename, filename, "Function '%s(%a) failed "
94 "with bad filename in the exception: %a" %
95 (fn.__name__, filename, exc_filename))
96
97 def test_failures(self):
98 # Pass non-existing Unicode filenames all over the place.
99 for name in self.files:
100 name = "not_" + name
101 self._apply_failure(open, name)
102 self._apply_failure(os.stat, name)
103 self._apply_failure(os.chdir, name)
104 self._apply_failure(os.rmdir, name)
105 self._apply_failure(os.remove, name)
106 self._apply_failure(os.listdir, name)
107
108 if sys.platform == 'win32':
109 # Windows is lunatic. Issue #13366.
110 _listdir_failure = NotADirectoryError, FileNotFoundError
111 else:
112 _listdir_failure = NotADirectoryError
113
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.skipIf(sys.platform == 'darwin', 'irrelevant test on Mac OS X')
127 @unittest.skipIf(
128 support.is_emscripten or support.is_wasi,
129 "test fails on Emscripten/WASI when host platform is macOS."
130 )
131 def test_normalize(self):
132 files = set(self.files)
133 others = set()
134 for nf in set(['NFC', 'NFD', 'NFKC', 'NFKD']):
135 others |= set(normalize(nf, file) for file in files)
136 others -= files
137 for name in others:
138 self._apply_failure(open, name)
139 self._apply_failure(os.stat, name)
140 self._apply_failure(os.chdir, name)
141 self._apply_failure(os.rmdir, name)
142 self._apply_failure(os.remove, name)
143 self._apply_failure(os.listdir, name)
144
145 # Skip the test on darwin, because darwin uses a normalization different
146 # than Python NFD normalization: filenames are different even if we use
147 # Python NFD normalization.
148 @unittest.skipIf(sys.platform == 'darwin', 'irrelevant test on Mac OS X')
149 def test_listdir(self):
150 sf0 = set(self.files)
151 with warnings.catch_warnings():
152 warnings.simplefilter("ignore", DeprecationWarning)
153 f1 = os.listdir(os_helper.TESTFN.encode(
154 sys.getfilesystemencoding()))
155 f2 = os.listdir(os_helper.TESTFN)
156 sf2 = set(os.path.join(os_helper.TESTFN, f) for f in f2)
157 self.assertEqual(sf0, sf2, "%a != %a" % (sf0, sf2))
158 self.assertEqual(len(f1), len(f2))
159
160 def test_rename(self):
161 for name in self.files:
162 os.rename(name, "tmp")
163 os.rename("tmp", name)
164
165 def test_directory(self):
166 dirname = os.path.join(os_helper.TESTFN,
167 'Gr\xfc\xdf-\u66e8\u66e9\u66eb')
168 filename = '\xdf-\u66e8\u66e9\u66eb'
169 with os_helper.temp_cwd(dirname):
170 with open(filename, 'wb') as f:
171 f.write((filename + '\n').encode("utf-8"))
172 os.access(filename,os.R_OK)
173 os.remove(filename)
174
175
176 class ESC[4;38;5;81mUnicodeNFCFileTests(ESC[4;38;5;149mUnicodeFileTests):
177 normal_form = 'NFC'
178
179
180 class ESC[4;38;5;81mUnicodeNFDFileTests(ESC[4;38;5;149mUnicodeFileTests):
181 normal_form = 'NFD'
182
183
184 class ESC[4;38;5;81mUnicodeNFKCFileTests(ESC[4;38;5;149mUnicodeFileTests):
185 normal_form = 'NFKC'
186
187
188 class ESC[4;38;5;81mUnicodeNFKDFileTests(ESC[4;38;5;149mUnicodeFileTests):
189 normal_form = 'NFKD'
190
191
192 if __name__ == "__main__":
193 unittest.main()