blob: 4b16cbb383d217da6e756b0ca46b402b51f0fd9d [file] [log] [blame]
Mark Hammond7995eb22002-10-03 23:14:10 +00001# 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
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003import os
4import sys
5import unittest
6import warnings
Florent Xiclunaeb136182010-03-21 18:49:50 +00007from unicodedata import normalize
Benjamin Petersonee8712c2008-05-20 21:35:26 +00008from test import support
Mark Hammond7995eb22002-10-03 23:14:10 +00009
10filenames = [
Victor Stinner7362c4f2010-10-28 11:20:31 +000011 '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',
Florent Xiclunaeb136182010-03-21 18:49:50 +000020 # Specific code points: fn, NFC(fn) and NFKC(fn) all differents
Victor Stinner7362c4f2010-10-28 11:20:31 +000021 '10_\u1fee\u1ffd',
Mark Hammond7995eb22002-10-03 23:14:10 +000022 ]
23
Florent Xiclunafd1b0932010-03-28 00:25:02 +000024# 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."
30if sys.platform != 'darwin':
31 filenames.extend([
Victor Stinnerfc6f5a42010-10-28 22:57:03 +000032 # Specific code points: NFC(fn), NFD(fn), NFKC(fn) and NFKD(fn) all differents
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
Florent Xiclunafd1b0932010-03-28 00:25:02 +000038 # Specific code points: fn, NFC(fn) and NFKC(fn) all differents
Victor Stinner7362c4f2010-10-28 11:20:31 +000039 '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') ==
Florent Xicluna65eb4532011-11-03 23:11:14 +010044 # NFKC('\u2001') == NFKC('\u2003')
45 ])
Florent Xiclunaeb136182010-03-21 18:49:50 +000046
Florent Xicluna87082ee2010-08-09 17:18:05 +000047
48# Is it Unicode-friendly?
49if not os.path.supports_unicode_filenames:
Victor Stinner94908bb2010-08-18 21:23:25 +000050 fsencoding = sys.getfilesystemencoding()
Florent Xicluna87082ee2010-08-09 17:18:05 +000051 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
Tim Peters1ee401f2002-10-05 17:54:56 +000059# Destroy directory dirname and all files under it, to one level.
60def deltree(dirname):
61 # Don't hide legitimate errors: if one of these suckers exists, it's
62 # an error if we can't remove it.
63 if os.path.exists(dirname):
Mark Hammond8696ebc2002-10-08 02:44:31 +000064 # must pass unicode to os.listdir() so we get back unicode results.
Guido van Rossumef87d6e2007-05-02 19:09:54 +000065 for fname in os.listdir(str(dirname)):
Tim Peters1ee401f2002-10-05 17:54:56 +000066 os.unlink(os.path.join(dirname, fname))
67 os.rmdir(dirname)
68
Florent Xiclunaeb136182010-03-21 18:49:50 +000069
Mark Hammond7995eb22002-10-03 23:14:10 +000070class UnicodeFileTests(unittest.TestCase):
Florent Xiclunaeb136182010-03-21 18:49:50 +000071 files = set(filenames)
72 normal_form = None
Tim Peters1ee401f2002-10-05 17:54:56 +000073
Mark Hammond7995eb22002-10-03 23:14:10 +000074 def setUp(self):
Mark Hammond7995eb22002-10-03 23:14:10 +000075 try:
Benjamin Petersonee8712c2008-05-20 21:35:26 +000076 os.mkdir(support.TESTFN)
Florent Xicluna65eb4532011-11-03 23:11:14 +010077 except FileExistsError:
Mark Hammond7995eb22002-10-03 23:14:10 +000078 pass
Florent Xiclunaeb136182010-03-21 18:49:50 +000079 files = set()
Mark Hammond7995eb22002-10-03 23:14:10 +000080 for name in self.files:
Florent Xiclunaeb136182010-03-21 18:49:50 +000081 name = os.path.join(support.TESTFN, self.norm(name))
Florent Xicluna87082ee2010-08-09 17:18:05 +000082 with open(name, 'wb') as f:
83 f.write((name+'\n').encode("utf-8"))
Mark Hammond7995eb22002-10-03 23:14:10 +000084 os.stat(name)
Florent Xiclunaeb136182010-03-21 18:49:50 +000085 files.add(name)
86 self.files = files
Mark Hammond7995eb22002-10-03 23:14:10 +000087
88 def tearDown(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000089 deltree(support.TESTFN)
Mark Hammond7995eb22002-10-03 23:14:10 +000090
Florent Xiclunaeb136182010-03-21 18:49:50 +000091 def norm(self, s):
92 if self.normal_form:
93 return normalize(self.normal_form, s)
94 return s
95
Florent Xicluna65eb4532011-11-03 23:11:14 +010096 def _apply_failure(self, fn, filename,
97 expected_exception=FileNotFoundError,
98 check_filename=True):
Florent Xiclunaeb136182010-03-21 18:49:50 +000099 with self.assertRaises(expected_exception) as c:
Mark Hammond7995eb22002-10-03 23:14:10 +0000100 fn(filename)
Florent Xiclunaeb136182010-03-21 18:49:50 +0000101 exc_filename = c.exception.filename
Florent Xiclunae0912db2011-11-07 21:56:17 +0100102 # listdir may append a wildcard to the filename
103 if fn is os.listdir and sys.platform == 'win32':
104 exc_filename, _, wildcard = exc_filename.rpartition(os.sep)
Florent Xiclunabe908402011-11-07 23:24:08 +0100105 self.assertEqual(wildcard, '*.*')
Florent Xicluna65eb4532011-11-03 23:11:14 +0100106 if check_filename:
Victor Stinner74ad7542010-10-28 11:09:09 +0000107 self.assertEqual(exc_filename, filename, "Function '%s(%a) failed "
Victor Stinner7dae81b2010-10-28 11:11:24 +0000108 "with bad filename in the exception: %a" %
Florent Xiclunaeb136182010-03-21 18:49:50 +0000109 (fn.__name__, filename, exc_filename))
Mark Hammond7995eb22002-10-03 23:14:10 +0000110
111 def test_failures(self):
112 # Pass non-existing Unicode filenames all over the place.
113 for name in self.files:
114 name = "not_" + name
Florent Xicluna65eb4532011-11-03 23:11:14 +0100115 self._apply_failure(open, name)
116 self._apply_failure(os.stat, name)
117 self._apply_failure(os.chdir, name)
118 self._apply_failure(os.rmdir, name)
119 self._apply_failure(os.remove, name)
Florent Xiclunae0912db2011-11-07 21:56:17 +0100120 self._apply_failure(os.listdir, name)
121
122 if sys.platform == 'win32':
Florent Xiclunabe908402011-11-07 23:24:08 +0100123 # Windows is lunatic. Issue #13366.
124 _listdir_failure = NotADirectoryError, FileNotFoundError
Florent Xiclunae0912db2011-11-07 21:56:17 +0100125 else:
126 _listdir_failure = NotADirectoryError
Mark Hammond7995eb22002-10-03 23:14:10 +0000127
128 def test_open(self):
129 for name in self.files:
Guido van Rossumc12a8132007-10-26 04:29:23 +0000130 f = open(name, 'wb')
Mark Hammond7995eb22002-10-03 23:14:10 +0000131 f.write((name+'\n').encode("utf-8"))
132 f.close()
133 os.stat(name)
Florent Xiclunae0912db2011-11-07 21:56:17 +0100134 self._apply_failure(os.listdir, name, self._listdir_failure)
Mark Hammond7995eb22002-10-03 23:14:10 +0000135
Victor Stinnerbfd7b262010-10-28 23:14:45 +0000136 # Skip the test on darwin, because darwin does normalize the filename to
137 # NFD (a variant of Unicode NFD form). Normalize the filename to NFC, NFKC,
138 # NFKD in Python is useless, because darwin will normalize it later and so
139 # open(), os.stat(), etc. don't raise any exception.
Florent Xicluna65eb4532011-11-03 23:11:14 +0100140 @unittest.skipIf(sys.platform == 'darwin', 'irrelevant test on Mac OS X')
Florent Xiclunaeb136182010-03-21 18:49:50 +0000141 def test_normalize(self):
142 files = set(self.files)
143 others = set()
144 for nf in set(['NFC', 'NFD', 'NFKC', 'NFKD']):
145 others |= set(normalize(nf, file) for file in files)
146 others -= files
Florent Xiclunaeb136182010-03-21 18:49:50 +0000147 for name in others:
Florent Xicluna65eb4532011-11-03 23:11:14 +0100148 self._apply_failure(open, name)
149 self._apply_failure(os.stat, name)
150 self._apply_failure(os.chdir, name)
151 self._apply_failure(os.rmdir, name)
152 self._apply_failure(os.remove, name)
Florent Xiclunae0912db2011-11-07 21:56:17 +0100153 self._apply_failure(os.listdir, name)
Florent Xiclunaeb136182010-03-21 18:49:50 +0000154
Victor Stinnerbfd7b262010-10-28 23:14:45 +0000155 # Skip the test on darwin, because darwin uses a normalization different
156 # than Python NFD normalization: filenames are different even if we use
157 # Python NFD normalization.
Florent Xicluna65eb4532011-11-03 23:11:14 +0100158 @unittest.skipIf(sys.platform == 'darwin', 'irrelevant test on Mac OS X')
Mark Hammond7995eb22002-10-03 23:14:10 +0000159 def test_listdir(self):
Florent Xiclunaeb136182010-03-21 18:49:50 +0000160 sf0 = set(self.files)
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100161 with warnings.catch_warnings():
162 warnings.simplefilter("ignore", DeprecationWarning)
163 f1 = os.listdir(support.TESTFN.encode(sys.getfilesystemencoding()))
Florent Xiclunaeb136182010-03-21 18:49:50 +0000164 f2 = os.listdir(support.TESTFN)
Florent Xiclunaeb136182010-03-21 18:49:50 +0000165 sf2 = set(os.path.join(support.TESTFN, f) for f in f2)
Victor Stinner2ebe6972010-10-24 21:05:03 +0000166 self.assertEqual(sf0, sf2, "%a != %a" % (sf0, sf2))
Florent Xiclunaeb136182010-03-21 18:49:50 +0000167 self.assertEqual(len(f1), len(f2))
Mark Hammond7995eb22002-10-03 23:14:10 +0000168
169 def test_rename(self):
170 for name in self.files:
Florent Xiclunaeb136182010-03-21 18:49:50 +0000171 os.rename(name, "tmp")
172 os.rename("tmp", name)
Mark Hammond7995eb22002-10-03 23:14:10 +0000173
174 def test_directory(self):
Florent Xiclunaeb136182010-03-21 18:49:50 +0000175 dirname = os.path.join(support.TESTFN, 'Gr\xfc\xdf-\u66e8\u66e9\u66eb')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000176 filename = '\xdf-\u66e8\u66e9\u66eb'
Mark Hammond7995eb22002-10-03 23:14:10 +0000177 oldwd = os.getcwd()
178 os.mkdir(dirname)
179 os.chdir(dirname)
Florent Xiclunaeb136182010-03-21 18:49:50 +0000180 try:
181 with open(filename, 'wb') as f:
182 f.write((filename + '\n').encode("utf-8"))
183 os.access(filename,os.R_OK)
184 os.remove(filename)
185 finally:
186 os.chdir(oldwd)
187 os.rmdir(dirname)
188
189
190class UnicodeNFCFileTests(UnicodeFileTests):
191 normal_form = 'NFC'
192
193
194class UnicodeNFDFileTests(UnicodeFileTests):
195 normal_form = 'NFD'
196
197
198class UnicodeNFKCFileTests(UnicodeFileTests):
199 normal_form = 'NFKC'
200
201
202class UnicodeNFKDFileTests(UnicodeFileTests):
203 normal_form = 'NFKD'
204
Mark Hammond7995eb22002-10-03 23:14:10 +0000205
206def test_main():
Tim Peters1ee401f2002-10-05 17:54:56 +0000207 try:
Florent Xiclunaeb136182010-03-21 18:49:50 +0000208 support.run_unittest(
209 UnicodeFileTests,
210 UnicodeNFCFileTests,
211 UnicodeNFDFileTests,
212 UnicodeNFKCFileTests,
213 UnicodeNFKDFileTests,
214 )
Tim Peters1ee401f2002-10-05 17:54:56 +0000215 finally:
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000216 deltree(support.TESTFN)
Mark Hammond7995eb22002-10-03 23:14:10 +0000217
Florent Xiclunaeb136182010-03-21 18:49:50 +0000218
Mark Hammond7995eb22002-10-03 23:14:10 +0000219if __name__ == "__main__":
220 test_main()