blob: 6d891e541eecfbdff951db2b2f88d3ee0d677aae [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
Mark Hammondbb4a47c2003-07-16 03:46:38 +00003import sys, os, unittest
Florent Xiclunaeb136182010-03-21 18:49:50 +00004from unicodedata import normalize
Benjamin Petersonee8712c2008-05-20 21:35:26 +00005from test import support
Mark Hammond7995eb22002-10-03 23:14:10 +00006
7filenames = [
Victor Stinner7362c4f2010-10-28 11:20:31 +00008 '1_abc',
9 '2_ascii',
10 '3_Gr\xfc\xdf-Gott',
11 '4_\u0393\u03b5\u03b9\u03ac-\u03c3\u03b1\u03c2',
12 '5_\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435',
13 '6_\u306b\u307d\u3093',
14 '7_\u05d4\u05e9\u05e7\u05e6\u05e5\u05e1',
15 '8_\u66e8\u66e9\u66eb',
16 '9_\u66e8\u05e9\u3093\u0434\u0393\xdf',
Florent Xiclunaeb136182010-03-21 18:49:50 +000017 # Specific code points: fn, NFC(fn) and NFKC(fn) all differents
Victor Stinner7362c4f2010-10-28 11:20:31 +000018 '10_\u1fee\u1ffd',
Mark Hammond7995eb22002-10-03 23:14:10 +000019 ]
20
Florent Xiclunafd1b0932010-03-28 00:25:02 +000021# Mac OS X decomposes Unicode names, using Normal Form D.
22# http://developer.apple.com/mac/library/qa/qa2001/qa1173.html
23# "However, most volume formats do not follow the exact specification for
24# these normal forms. For example, HFS Plus uses a variant of Normal Form D
25# in which U+2000 through U+2FFF, U+F900 through U+FAFF, and U+2F800 through
26# U+2FAFF are not decomposed."
27if sys.platform != 'darwin':
28 filenames.extend([
Victor Stinnerfc6f5a42010-10-28 22:57:03 +000029 # Specific code points: NFC(fn), NFD(fn), NFKC(fn) and NFKD(fn) all differents
30 '11_\u0385\u03d3\u03d4',
31 '12_\u00a8\u0301\u03d2\u0301\u03d2\u0308', # == NFD('\u0385\u03d3\u03d4')
32 '13_\u0020\u0308\u0301\u038e\u03ab', # == NFKC('\u0385\u03d3\u03d4')
33 '14_\u1e9b\u1fc1\u1fcd\u1fce\u1fcf\u1fdd\u1fde\u1fdf\u1fed',
34
Florent Xiclunafd1b0932010-03-28 00:25:02 +000035 # Specific code points: fn, NFC(fn) and NFKC(fn) all differents
Victor Stinner7362c4f2010-10-28 11:20:31 +000036 '15_\u1fee\u1ffd\ufad1',
37 '16_\u2000\u2000\u2000A',
38 '17_\u2001\u2001\u2001A',
39 '18_\u2003\u2003\u2003A', # == NFC('\u2001\u2001\u2001A')
40 '19_\u0020\u0020\u0020A', # '\u0020' == ' ' == NFKC('\u2000') ==
Florent Xiclunafd1b0932010-03-28 00:25:02 +000041 # NFKC('\u2001') == NFKC('\u2003')
42])
Florent Xiclunaeb136182010-03-21 18:49:50 +000043
Florent Xicluna87082ee2010-08-09 17:18:05 +000044
45# Is it Unicode-friendly?
46if not os.path.supports_unicode_filenames:
Victor Stinner94908bb2010-08-18 21:23:25 +000047 fsencoding = sys.getfilesystemencoding()
Florent Xicluna87082ee2010-08-09 17:18:05 +000048 try:
49 for name in filenames:
50 name.encode(fsencoding)
51 except UnicodeEncodeError:
52 raise unittest.SkipTest("only NT+ and systems with "
53 "Unicode-friendly filesystem encoding")
54
55
Tim Peters1ee401f2002-10-05 17:54:56 +000056# Destroy directory dirname and all files under it, to one level.
57def deltree(dirname):
58 # Don't hide legitimate errors: if one of these suckers exists, it's
59 # an error if we can't remove it.
60 if os.path.exists(dirname):
Mark Hammond8696ebc2002-10-08 02:44:31 +000061 # must pass unicode to os.listdir() so we get back unicode results.
Guido van Rossumef87d6e2007-05-02 19:09:54 +000062 for fname in os.listdir(str(dirname)):
Tim Peters1ee401f2002-10-05 17:54:56 +000063 os.unlink(os.path.join(dirname, fname))
64 os.rmdir(dirname)
65
Florent Xiclunaeb136182010-03-21 18:49:50 +000066
Mark Hammond7995eb22002-10-03 23:14:10 +000067class UnicodeFileTests(unittest.TestCase):
Florent Xiclunaeb136182010-03-21 18:49:50 +000068 files = set(filenames)
69 normal_form = None
Tim Peters1ee401f2002-10-05 17:54:56 +000070
Mark Hammond7995eb22002-10-03 23:14:10 +000071 def setUp(self):
Mark Hammond7995eb22002-10-03 23:14:10 +000072 try:
Benjamin Petersonee8712c2008-05-20 21:35:26 +000073 os.mkdir(support.TESTFN)
Mark Hammond7995eb22002-10-03 23:14:10 +000074 except OSError:
75 pass
Florent Xiclunaeb136182010-03-21 18:49:50 +000076 files = set()
Mark Hammond7995eb22002-10-03 23:14:10 +000077 for name in self.files:
Florent Xiclunaeb136182010-03-21 18:49:50 +000078 name = os.path.join(support.TESTFN, self.norm(name))
Florent Xicluna87082ee2010-08-09 17:18:05 +000079 with open(name, 'wb') as f:
80 f.write((name+'\n').encode("utf-8"))
Mark Hammond7995eb22002-10-03 23:14:10 +000081 os.stat(name)
Florent Xiclunaeb136182010-03-21 18:49:50 +000082 files.add(name)
83 self.files = files
Mark Hammond7995eb22002-10-03 23:14:10 +000084
85 def tearDown(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000086 deltree(support.TESTFN)
Mark Hammond7995eb22002-10-03 23:14:10 +000087
Florent Xiclunaeb136182010-03-21 18:49:50 +000088 def norm(self, s):
89 if self.normal_form:
90 return normalize(self.normal_form, s)
91 return s
92
Mark Hammond7995eb22002-10-03 23:14:10 +000093 def _apply_failure(self, fn, filename, expected_exception,
94 check_fn_in_exception = True):
Florent Xiclunaeb136182010-03-21 18:49:50 +000095 with self.assertRaises(expected_exception) as c:
Mark Hammond7995eb22002-10-03 23:14:10 +000096 fn(filename)
Florent Xiclunaeb136182010-03-21 18:49:50 +000097 exc_filename = c.exception.filename
98 # the "filename" exception attribute may be encoded
99 if isinstance(exc_filename, bytes):
100 filename = filename.encode(sys.getfilesystemencoding())
101 if check_fn_in_exception:
Victor Stinner74ad7542010-10-28 11:09:09 +0000102 self.assertEqual(exc_filename, filename, "Function '%s(%a) failed "
Victor Stinner7dae81b2010-10-28 11:11:24 +0000103 "with bad filename in the exception: %a" %
Florent Xiclunaeb136182010-03-21 18:49:50 +0000104 (fn.__name__, filename, exc_filename))
Mark Hammond7995eb22002-10-03 23:14:10 +0000105
106 def test_failures(self):
107 # Pass non-existing Unicode filenames all over the place.
108 for name in self.files:
109 name = "not_" + name
110 self._apply_failure(open, name, IOError)
111 self._apply_failure(os.stat, name, OSError)
112 self._apply_failure(os.chdir, name, OSError)
113 self._apply_failure(os.rmdir, name, OSError)
114 self._apply_failure(os.remove, name, OSError)
115 # listdir may append a wildcard to the filename, so dont check
116 self._apply_failure(os.listdir, name, OSError, False)
117
118 def test_open(self):
119 for name in self.files:
Guido van Rossumc12a8132007-10-26 04:29:23 +0000120 f = open(name, 'wb')
Mark Hammond7995eb22002-10-03 23:14:10 +0000121 f.write((name+'\n').encode("utf-8"))
122 f.close()
123 os.stat(name)
124
Victor Stinnerbfd7b262010-10-28 23:14:45 +0000125 # Skip the test on darwin, because darwin does normalize the filename to
126 # NFD (a variant of Unicode NFD form). Normalize the filename to NFC, NFKC,
127 # NFKD in Python is useless, because darwin will normalize it later and so
128 # open(), os.stat(), etc. don't raise any exception.
129 @unittest.skipIf(sys.platform == 'darwin', 'irrevelant test on Mac OS X')
Florent Xiclunaeb136182010-03-21 18:49:50 +0000130 def test_normalize(self):
131 files = set(self.files)
132 others = set()
133 for nf in set(['NFC', 'NFD', 'NFKC', 'NFKD']):
134 others |= set(normalize(nf, file) for file in files)
135 others -= files
Florent Xiclunaeb136182010-03-21 18:49:50 +0000136 for name in others:
Florent Xiclunaeb136182010-03-21 18:49:50 +0000137 self._apply_failure(open, name, IOError)
138 self._apply_failure(os.stat, name, OSError)
139 self._apply_failure(os.chdir, name, OSError)
140 self._apply_failure(os.rmdir, name, OSError)
141 self._apply_failure(os.remove, name, OSError)
142 # listdir may append a wildcard to the filename, so dont check
143 self._apply_failure(os.listdir, name, OSError, False)
144
Victor Stinnerbfd7b262010-10-28 23:14:45 +0000145 # 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', 'irrevelant test on Mac OS X')
Mark Hammond7995eb22002-10-03 23:14:10 +0000149 def test_listdir(self):
Florent Xiclunaeb136182010-03-21 18:49:50 +0000150 sf0 = set(self.files)
151 f1 = os.listdir(support.TESTFN.encode(sys.getfilesystemencoding()))
152 f2 = os.listdir(support.TESTFN)
Florent Xiclunaeb136182010-03-21 18:49:50 +0000153 sf2 = set(os.path.join(support.TESTFN, f) for f in f2)
Victor Stinner2ebe6972010-10-24 21:05:03 +0000154 self.assertEqual(sf0, sf2, "%a != %a" % (sf0, sf2))
Florent Xiclunaeb136182010-03-21 18:49:50 +0000155 self.assertEqual(len(f1), len(f2))
Mark Hammond7995eb22002-10-03 23:14:10 +0000156
157 def test_rename(self):
158 for name in self.files:
Florent Xiclunaeb136182010-03-21 18:49:50 +0000159 os.rename(name, "tmp")
160 os.rename("tmp", name)
Mark Hammond7995eb22002-10-03 23:14:10 +0000161
162 def test_directory(self):
Florent Xiclunaeb136182010-03-21 18:49:50 +0000163 dirname = os.path.join(support.TESTFN, 'Gr\xfc\xdf-\u66e8\u66e9\u66eb')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000164 filename = '\xdf-\u66e8\u66e9\u66eb'
Mark Hammond7995eb22002-10-03 23:14:10 +0000165 oldwd = os.getcwd()
166 os.mkdir(dirname)
167 os.chdir(dirname)
Florent Xiclunaeb136182010-03-21 18:49:50 +0000168 try:
169 with open(filename, 'wb') as f:
170 f.write((filename + '\n').encode("utf-8"))
171 os.access(filename,os.R_OK)
172 os.remove(filename)
173 finally:
174 os.chdir(oldwd)
175 os.rmdir(dirname)
176
177
178class UnicodeNFCFileTests(UnicodeFileTests):
179 normal_form = 'NFC'
180
181
182class UnicodeNFDFileTests(UnicodeFileTests):
183 normal_form = 'NFD'
184
185
186class UnicodeNFKCFileTests(UnicodeFileTests):
187 normal_form = 'NFKC'
188
189
190class UnicodeNFKDFileTests(UnicodeFileTests):
191 normal_form = 'NFKD'
192
Mark Hammond7995eb22002-10-03 23:14:10 +0000193
194def test_main():
Tim Peters1ee401f2002-10-05 17:54:56 +0000195 try:
Florent Xiclunaeb136182010-03-21 18:49:50 +0000196 support.run_unittest(
197 UnicodeFileTests,
198 UnicodeNFCFileTests,
199 UnicodeNFDFileTests,
200 UnicodeNFKCFileTests,
201 UnicodeNFKDFileTests,
202 )
Tim Peters1ee401f2002-10-05 17:54:56 +0000203 finally:
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000204 deltree(support.TESTFN)
Mark Hammond7995eb22002-10-03 23:14:10 +0000205
Florent Xiclunaeb136182010-03-21 18:49:50 +0000206
Mark Hammond7995eb22002-10-03 23:14:10 +0000207if __name__ == "__main__":
208 test_main()