1 # Test some Unicode file name semantics
2 # We dont test many operations on files other than
3 # that their names can be used with Unicode characters.
6 from test_support
import verify
, TestSkipped
, TESTFN_UNICODE
8 from test_support
import TESTFN_ENCODING
12 # try to run the test in an UTF-8 locale. If this locale is not
13 # available, avoid running the test since the locale's encoding
14 # might not support TESTFN_UNICODE. Likewise, if the system does
15 # not support locale.CODESET, Unicode file semantics is not
17 oldlocale
= locale
.setlocale(locale
.LC_CTYPE
)
19 locale
.setlocale(locale
.LC_CTYPE
,"en_US.UTF-8")
20 TESTFN_ENCODING
= locale
.nl_langinfo(locale
.CODESET
)
21 except (locale
.Error
, AttributeError):
22 raise TestSkipped("No Unicode filesystem semantics on this platform.")
24 TESTFN_ENCODED
= TESTFN_UNICODE
.encode(TESTFN_ENCODING
)
26 # Check with creation as Unicode string.
27 f
= open(TESTFN_UNICODE
, 'wb')
28 if not os
.path
.isfile(TESTFN_UNICODE
):
29 print "File doesn't exist after creating it"
31 if not os
.path
.isfile(TESTFN_ENCODED
):
32 print "File doesn't exist (encoded string) after creating it"
37 if os
.stat(TESTFN_ENCODED
) != os
.stat(TESTFN_UNICODE
):
38 print "os.stat() did not agree on the 2 filenames"
39 os
.chmod(TESTFN_ENCODED
, 0777)
40 os
.chmod(TESTFN_UNICODE
, 0777)
43 os
.rename(TESTFN_ENCODED
, TESTFN_ENCODED
+ ".new")
44 os
.rename(TESTFN_UNICODE
+".new", TESTFN_ENCODED
)
46 os
.unlink(TESTFN_ENCODED
)
47 if os
.path
.isfile(TESTFN_ENCODED
) or \
48 os
.path
.isfile(TESTFN_UNICODE
):
49 print "File exists after deleting it"
51 # Check with creation as encoded string.
52 f
= open(TESTFN_ENCODED
, 'wb')
53 if not os
.path
.isfile(TESTFN_UNICODE
) or \
54 not os
.path
.isfile(TESTFN_ENCODED
):
55 print "File doesn't exist after creating it"
57 path
, base
= os
.path
.split(os
.path
.abspath(TESTFN_ENCODED
))
58 if base
not in os
.listdir(path
):
59 print "Filename did not appear in os.listdir()"
62 os
.unlink(TESTFN_UNICODE
)
63 if os
.path
.isfile(TESTFN_ENCODED
) or \
64 os
.path
.isfile(TESTFN_UNICODE
):
65 print "File exists after deleting it"
68 f
= os
.open(TESTFN_ENCODED
, os
.O_CREAT
)
69 if not os
.path
.isfile(TESTFN_UNICODE
) or \
70 not os
.path
.isfile(TESTFN_ENCODED
):
71 print "File doesn't exist after creating it"
73 os
.unlink(TESTFN_UNICODE
)
75 # Test directories etc
77 abs_encoded
= os
.path
.abspath(TESTFN_ENCODED
) + ".dir"
78 abs_unicode
= os
.path
.abspath(TESTFN_UNICODE
) + ".dir"
93 print "All the Unicode tests appeared to work"
95 locale
.setlocale(locale
.LC_CTYPE
, oldlocale
)