blob: 8b5757cc692429141716d511b85d5206692d8bbe [file] [log] [blame]
Mark Hammondef8b6542001-05-13 08:04:26 +00001# 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.
4import os
5
6from test_support import verify, TestSkipped, TESTFN_UNICODE
7try:
8 from test_support import TESTFN_ENCODING
Martin v. Löwis7c82a3e02001-09-05 17:09:48 +00009 oldlocale = None
Mark Hammondef8b6542001-05-13 08:04:26 +000010except ImportError:
Martin v. Löwis7c82a3e02001-09-05 17:09:48 +000011 import locale
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
16 # available, either.
17 oldlocale = locale.setlocale(locale.LC_CTYPE)
18 try:
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.")
Mark Hammondef8b6542001-05-13 08:04:26 +000023
24TESTFN_ENCODED = TESTFN_UNICODE.encode(TESTFN_ENCODING)
25
26# Check with creation as Unicode string.
27f = open(TESTFN_UNICODE, 'wb')
28if not os.path.isfile(TESTFN_UNICODE):
29 print "File doesn't exist after creating it"
30
31if not os.path.isfile(TESTFN_ENCODED):
32 print "File doesn't exist (encoded string) after creating it"
33
34f.close()
35
36# Test stat and chmod
37if os.stat(TESTFN_ENCODED) != os.stat(TESTFN_UNICODE):
38 print "os.stat() did not agree on the 2 filenames"
39os.chmod(TESTFN_ENCODED, 0777)
40os.chmod(TESTFN_UNICODE, 0777)
41
42# Test rename
43os.rename(TESTFN_ENCODED, TESTFN_ENCODED + ".new")
44os.rename(TESTFN_UNICODE+".new", TESTFN_ENCODED)
45
46os.unlink(TESTFN_ENCODED)
47if os.path.isfile(TESTFN_ENCODED) or \
48 os.path.isfile(TESTFN_UNICODE):
49 print "File exists after deleting it"
50
51# Check with creation as encoded string.
52f = open(TESTFN_ENCODED, 'wb')
53if not os.path.isfile(TESTFN_UNICODE) or \
54 not os.path.isfile(TESTFN_ENCODED):
55 print "File doesn't exist after creating it"
56
57path, base = os.path.split(os.path.abspath(TESTFN_ENCODED))
58if base not in os.listdir(path):
59 print "Filename did not appear in os.listdir()"
60
61f.close()
62os.unlink(TESTFN_UNICODE)
63if os.path.isfile(TESTFN_ENCODED) or \
64 os.path.isfile(TESTFN_UNICODE):
65 print "File exists after deleting it"
66
67# test os.open
68f = os.open(TESTFN_ENCODED, os.O_CREAT)
69if not os.path.isfile(TESTFN_UNICODE) or \
70 not os.path.isfile(TESTFN_ENCODED):
71 print "File doesn't exist after creating it"
72os.close(f)
73os.unlink(TESTFN_UNICODE)
74
75# Test directories etc
76cwd = os.getcwd()
77abs_encoded = os.path.abspath(TESTFN_ENCODED) + ".dir"
78abs_unicode = os.path.abspath(TESTFN_UNICODE) + ".dir"
79os.mkdir(abs_encoded)
80try:
81 os.chdir(abs_encoded)
82 os.chdir(abs_unicode)
83finally:
84 os.chdir(cwd)
85 os.rmdir(abs_unicode)
86os.mkdir(abs_unicode)
87try:
88 os.chdir(abs_encoded)
89 os.chdir(abs_unicode)
90finally:
91 os.chdir(cwd)
92 os.rmdir(abs_encoded)
93print "All the Unicode tests appeared to work"
Martin v. Löwis7c82a3e02001-09-05 17:09:48 +000094if oldlocale:
95 locale.setlocale(locale.LC_CTYPE, oldlocale)