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 test
import test_support
5 if not os
.path
.supports_unicode_filenames
:
6 raise unittest
.SkipTest
, "test works only on NT+"
12 u
'\u0393\u03b5\u03b9\u03ac-\u03c3\u03b1\u03c2',
13 u
'\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435',
14 u
'\u306b\u307d\u3093',
15 u
'\u05d4\u05e9\u05e7\u05e6\u05e5\u05e1',
16 u
'\u66e8\u66e9\u66eb',
17 u
'\u66e8\u05e9\u3093\u0434\u0393\xdf',
20 # Destroy directory dirname and all files under it, to one level.
22 # Don't hide legitimate errors: if one of these suckers exists, it's
23 # an error if we can't remove it.
24 if os
.path
.exists(dirname
):
25 # must pass unicode to os.listdir() so we get back unicode results.
26 for fname
in os
.listdir(unicode(dirname
)):
27 os
.unlink(os
.path
.join(dirname
, fname
))
30 class UnicodeFileTests(unittest
.TestCase
):
31 files
= [os
.path
.join(test_support
.TESTFN
, f
) for f
in filenames
]
35 os
.mkdir(test_support
.TESTFN
)
38 for name
in self
.files
:
40 f
.write((name
+'\n').encode("utf-8"))
45 deltree(test_support
.TESTFN
)
47 def _apply_failure(self
, fn
, filename
, expected_exception
,
48 check_fn_in_exception
= True):
51 raise test_support
.TestFailed("Expected to fail calling '%s(%r)'"
52 % (fn
.__name
__, filename
))
53 except expected_exception
, details
:
54 if check_fn_in_exception
and details
.filename
!= filename
:
55 raise test_support
.TestFailed("Function '%s(%r) failed with "
56 "bad filename in the exception: %r"
57 % (fn
.__name
__, filename
,
60 def test_failures(self
):
61 # Pass non-existing Unicode filenames all over the place.
62 for name
in self
.files
:
64 self
._apply
_failure
(open, name
, IOError)
65 self
._apply
_failure
(os
.stat
, name
, OSError)
66 self
._apply
_failure
(os
.chdir
, name
, OSError)
67 self
._apply
_failure
(os
.rmdir
, name
, OSError)
68 self
._apply
_failure
(os
.remove
, name
, OSError)
69 # listdir may append a wildcard to the filename, so dont check
70 self
._apply
_failure
(os
.listdir
, name
, OSError, False)
73 for name
in self
.files
:
75 f
.write((name
+'\n').encode("utf-8"))
79 def test_listdir(self
):
80 f1
= os
.listdir(test_support
.TESTFN
)
81 f2
= os
.listdir(unicode(test_support
.TESTFN
,
82 sys
.getfilesystemencoding()))
83 sf2
= set(u
"\\".join((unicode(test_support
.TESTFN
), f
))
85 self
.assertEqual(len(f1
), len(self
.files
))
86 self
.assertEqual(sf2
, set(self
.files
))
88 def test_rename(self
):
89 for name
in self
.files
:
93 def test_directory(self
):
94 dirname
= os
.path
.join(test_support
.TESTFN
,u
'Gr\xfc\xdf-\u66e8\u66e9\u66eb')
95 filename
= u
'\xdf-\u66e8\u66e9\u66eb'
99 f
= open(filename
, 'w')
100 f
.write((filename
+ '\n').encode("utf-8"))
102 os
.access(filename
,os
.R_OK
)
109 test_support
.run_unittest(UnicodeFileTests
)
111 deltree(test_support
.TESTFN
)
113 if __name__
== "__main__":