This commit was manufactured by cvs2svn to create tag 'r234c1'.
[python/dscho.git] / Lib / test / test_unicode_file.py
blobb8b1848f9c65f8741e725729dd2e1137ace73455
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.
4 import os, glob
6 from test.test_support import verify, TestSkipped, TESTFN_UNICODE
7 from test.test_support import TESTFN_ENCODING
8 try:
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"
23 f.close()
25 # Test stat and chmod
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)
33 # Test rename
34 try:
35 os.unlink(TESTFN_ENCODED + ".new")
36 except os.error:
37 pass
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()"
65 f.close()
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"
71 # test os.open
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"
76 os.close(f)
77 os.unlink(TESTFN_UNICODE)
79 # Test directories etc
80 cwd = os.getcwd()
81 abs_encoded = os.path.abspath(TESTFN_ENCODED) + ".dir"
82 abs_unicode = os.path.abspath(TESTFN_UNICODE) + ".dir"
83 os.mkdir(abs_encoded)
84 try:
85 os.chdir(abs_encoded)
86 os.chdir(abs_unicode)
87 finally:
88 os.chdir(cwd)
89 os.rmdir(abs_unicode)
90 os.mkdir(abs_unicode)
91 try:
92 os.chdir(abs_encoded)
93 os.chdir(abs_unicode)
94 finally:
95 os.chdir(cwd)
96 os.rmdir(abs_encoded)
97 print "All the Unicode tests appeared to work"