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