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 sys
, os
, unittest
4 from unicodedata
import normalize
5 from test
import test_support
11 u
'\u0393\u03b5\u03b9\u03ac-\u03c3\u03b1\u03c2',
12 u
'\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435',
13 u
'\u306b\u307d\u3093',
14 u
'\u05d4\u05e9\u05e7\u05e6\u05e5\u05e1',
15 u
'\u66e8\u66e9\u66eb',
16 u
'\u66e8\u05e9\u3093\u0434\u0393\xdf',
17 # Specific code points: fn, NFC(fn) and NFKC(fn) all differents
19 # Specific code points: NFC(fn), NFD(fn), NFKC(fn) and NFKD(fn) all differents
20 u
'\u0385\u03d3\u03d4',
21 u
'\u00a8\u0301\u03d2\u0301\u03d2\u0308', # == NFD(u'\u0385\u03d3\u03d4')
22 u
'\u0020\u0308\u0301\u038e\u03ab', # == NFKC(u'\u0385\u03d3\u03d4')
23 u
'\u1e9b\u1fc1\u1fcd\u1fce\u1fcf\u1fdd\u1fde\u1fdf\u1fed',
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':
34 # Specific code points: fn, NFC(fn) and NFKC(fn) all differents
35 u
'\u1fee\u1ffd\ufad1',
36 u
'\u2000\u2000\u2000A',
37 u
'\u2001\u2001\u2001A',
38 u
'\u2003\u2003\u2003A', # == NFC(u'\u2001\u2001\u2001A')
39 u
'\u0020\u0020\u0020A', # u'\u0020' == u' ' == NFKC(u'\u2000') ==
40 # NFKC(u'\u2001') == NFKC(u'\u2003')
44 # Is it Unicode-friendly?
45 if not os
.path
.supports_unicode_filenames
:
46 fsencoding
= sys
.getfilesystemencoding() or sys
.getdefaultencoding()
48 for name
in filenames
:
49 name
.encode(fsencoding
)
50 except UnicodeEncodeError:
51 raise unittest
.SkipTest("only NT+ and systems with "
52 "Unicode-friendly filesystem encoding")
55 # Destroy directory dirname and all files under it, to one level.
57 # Don't hide legitimate errors: if one of these suckers exists, it's
58 # an error if we can't remove it.
59 if os
.path
.exists(dirname
):
60 # must pass unicode to os.listdir() so we get back unicode results.
61 for fname
in os
.listdir(unicode(dirname
)):
62 os
.unlink(os
.path
.join(dirname
, fname
))
66 class UnicodeFileTests(unittest
.TestCase
):
67 files
= set(filenames
)
72 os
.mkdir(test_support
.TESTFN
)
76 for name
in self
.files
:
77 name
= os
.path
.join(test_support
.TESTFN
, self
.norm(name
))
78 with
open(name
, 'w') as f
:
79 f
.write((name
+'\n').encode("utf-8"))
85 deltree(test_support
.TESTFN
)
88 if self
.normal_form
and isinstance(s
, unicode):
89 return normalize(self
.normal_form
, s
)
92 def _apply_failure(self
, fn
, filename
, expected_exception
,
93 check_fn_in_exception
= True):
94 with self
.assertRaises(expected_exception
) as c
:
96 exc_filename
= c
.exception
.filename
97 # the "filename" exception attribute may be encoded
98 if isinstance(exc_filename
, str):
99 filename
= filename
.encode(sys
.getfilesystemencoding())
100 if check_fn_in_exception
:
101 self
.assertEqual(exc_filename
, filename
, "Function '%s(%r) failed "
102 "with bad filename in the exception: %r" %
103 (fn
.__name
__, filename
, exc_filename
))
105 def test_failures(self
):
106 # Pass non-existing Unicode filenames all over the place.
107 for name
in self
.files
:
109 self
._apply
_failure
(open, name
, IOError)
110 self
._apply
_failure
(os
.stat
, name
, OSError)
111 self
._apply
_failure
(os
.chdir
, name
, OSError)
112 self
._apply
_failure
(os
.rmdir
, name
, OSError)
113 self
._apply
_failure
(os
.remove
, name
, OSError)
114 # listdir may append a wildcard to the filename, so dont check
115 self
._apply
_failure
(os
.listdir
, name
, OSError, False)
118 for name
in self
.files
:
120 f
.write((name
+'\n').encode("utf-8"))
124 def test_normalize(self
):
125 files
= set(f
for f
in self
.files
if isinstance(f
, unicode))
127 for nf
in set(['NFC', 'NFD', 'NFKC', 'NFKD']):
128 others |
= set(normalize(nf
, file) for file in files
)
130 if sys
.platform
== 'darwin':
131 files
= set(normalize('NFD', file) for file in files
)
133 if sys
.platform
== 'darwin' and normalize('NFD', name
) in files
:
134 # Mac OS X decomposes Unicode names. See comment above.
137 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)
145 def test_listdir(self
):
146 sf0
= set(self
.files
)
147 f1
= os
.listdir(test_support
.TESTFN
)
148 f2
= os
.listdir(unicode(test_support
.TESTFN
,
149 sys
.getfilesystemencoding()))
150 if sys
.platform
== 'darwin':
151 # Mac OS X decomposes Unicode names. See comment above.
152 sf0
= set(normalize('NFD', unicode(f
)) for f
in self
.files
)
153 f2
= [normalize('NFD', unicode(f
)) for f
in f2
]
154 sf2
= set(os
.path
.join(unicode(test_support
.TESTFN
), f
) for f
in f2
)
155 self
.assertEqual(sf0
, sf2
)
156 self
.assertEqual(len(f1
), len(f2
))
158 def test_rename(self
):
159 for name
in self
.files
:
160 os
.rename(name
, "tmp")
161 os
.rename("tmp", name
)
163 def test_directory(self
):
164 dirname
= os
.path
.join(test_support
.TESTFN
,
165 u
'Gr\xfc\xdf-\u66e8\u66e9\u66eb')
166 filename
= u
'\xdf-\u66e8\u66e9\u66eb'
171 with
open(filename
, 'w') as f
:
172 f
.write((filename
+ '\n').encode("utf-8"))
173 os
.access(filename
,os
.R_OK
)
180 class UnicodeNFCFileTests(UnicodeFileTests
):
184 class UnicodeNFDFileTests(UnicodeFileTests
):
188 class UnicodeNFKCFileTests(UnicodeFileTests
):
192 class UnicodeNFKDFileTests(UnicodeFileTests
):
198 test_support
.run_unittest(
202 UnicodeNFKCFileTests
,
203 UnicodeNFKDFileTests
,
206 deltree(test_support
.TESTFN
)
209 if __name__
== "__main__":