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
.test_support
import verify
, TestSkipped
, TESTFN_UNICODE
7 from test
.test_support
import TESTFN_ENCODING
9 TESTFN_ENCODED
= TESTFN_UNICODE
.encode(TESTFN_ENCODING
)
10 except (UnicodeError, TypeError):
11 # Either the file system encoding is None, or the file name
12 # cannot be encoded in the file system encoding.
13 raise TestSkipped("No Unicode filesystem semantics on this platform.")
15 # Check with creation as Unicode string.
16 f
= open(TESTFN_UNICODE
, 'wb')
17 if not os
.path
.isfile(TESTFN_UNICODE
):
18 print "File doesn't exist after creating it"
20 if not os
.path
.isfile(TESTFN_ENCODED
):
21 print "File doesn't exist (encoded string) after creating it"
26 if os
.stat(TESTFN_ENCODED
) != os
.stat(TESTFN_UNICODE
):
27 print "os.stat() did not agree on the 2 filenames"
28 if os
.lstat(TESTFN_ENCODED
) != os
.lstat(TESTFN_UNICODE
):
29 print "os.lstat() did not agree on the 2 filenames"
30 os
.chmod(TESTFN_ENCODED
, 0777)
31 os
.chmod(TESTFN_UNICODE
, 0777)
35 os
.unlink(TESTFN_ENCODED
+ ".new")
38 os
.rename(TESTFN_ENCODED
, TESTFN_ENCODED
+ ".new")
39 os
.rename(TESTFN_UNICODE
+".new", TESTFN_ENCODED
)
41 os
.unlink(TESTFN_ENCODED
)
42 if os
.path
.isfile(TESTFN_ENCODED
) or \
43 os
.path
.isfile(TESTFN_UNICODE
):
44 print "File exists after deleting it"
46 # Check with creation as encoded string.
47 f
= open(TESTFN_ENCODED
, 'wb')
48 if not os
.path
.isfile(TESTFN_UNICODE
) or \
49 not os
.path
.isfile(TESTFN_ENCODED
):
50 print "File doesn't exist after creating it"
52 path
, base
= os
.path
.split(os
.path
.abspath(TESTFN_ENCODED
))
53 # Until PEP 277 is adopted, this test is not portable
54 # if base not in os.listdir(path):
55 # print "Filename did not appear in os.listdir()"
56 # path, base = os.path.split(os.path.abspath(TESTFN_UNICODE))
57 # if base not in os.listdir(path):
58 # print "Unicode filename did not appear in os.listdir()"
60 if os
.path
.abspath(TESTFN_ENCODED
) != os
.path
.abspath(glob
.glob(TESTFN_ENCODED
)[0]):
61 print "Filename did not appear in glob.glob()"
62 if os
.path
.abspath(TESTFN_UNICODE
) != os
.path
.abspath(glob
.glob(TESTFN_UNICODE
)[0]):
63 print "Unicode filename did not appear in glob.glob()"
66 os
.unlink(TESTFN_UNICODE
)
67 if os
.path
.isfile(TESTFN_ENCODED
) or \
68 os
.path
.isfile(TESTFN_UNICODE
):
69 print "File exists after deleting it"
72 f
= os
.open(TESTFN_ENCODED
, os
.O_CREAT
)
73 if not os
.path
.isfile(TESTFN_UNICODE
) or \
74 not os
.path
.isfile(TESTFN_ENCODED
):
75 print "File doesn't exist after creating it"
77 os
.unlink(TESTFN_UNICODE
)
79 # Test directories etc
81 abs_encoded
= os
.path
.abspath(TESTFN_ENCODED
) + ".dir"
82 abs_unicode
= os
.path
.abspath(TESTFN_UNICODE
) + ".dir"
97 print "All the Unicode tests appeared to work"