Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 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 |
| 5 | |
| 6 | from test_support import verify, TestSkipped, TESTFN_UNICODE |
| 7 | try: |
| 8 | from test_support import TESTFN_ENCODING |
Martin v. Löwis | 7c82a3e0 | 2001-09-05 17:09:48 +0000 | [diff] [blame] | 9 | oldlocale = None |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 10 | except ImportError: |
Martin v. Löwis | 7c82a3e0 | 2001-09-05 17:09:48 +0000 | [diff] [blame] | 11 | 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 Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 23 | |
| 24 | TESTFN_ENCODED = TESTFN_UNICODE.encode(TESTFN_ENCODING) |
| 25 | |
| 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" |
| 30 | |
| 31 | if not os.path.isfile(TESTFN_ENCODED): |
| 32 | print "File doesn't exist (encoded string) after creating it" |
| 33 | |
| 34 | f.close() |
| 35 | |
| 36 | # Test stat and chmod |
| 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) |
| 41 | |
| 42 | # Test rename |
| 43 | os.rename(TESTFN_ENCODED, TESTFN_ENCODED + ".new") |
| 44 | os.rename(TESTFN_UNICODE+".new", TESTFN_ENCODED) |
| 45 | |
| 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" |
| 50 | |
| 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" |
| 56 | |
| 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()" |
| 60 | |
| 61 | f.close() |
| 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" |
| 66 | |
| 67 | # test os.open |
| 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" |
| 72 | os.close(f) |
| 73 | os.unlink(TESTFN_UNICODE) |
| 74 | |
| 75 | # Test directories etc |
| 76 | cwd = os.getcwd() |
| 77 | abs_encoded = os.path.abspath(TESTFN_ENCODED) + ".dir" |
| 78 | abs_unicode = os.path.abspath(TESTFN_UNICODE) + ".dir" |
| 79 | os.mkdir(abs_encoded) |
| 80 | try: |
| 81 | os.chdir(abs_encoded) |
| 82 | os.chdir(abs_unicode) |
| 83 | finally: |
| 84 | os.chdir(cwd) |
| 85 | os.rmdir(abs_unicode) |
| 86 | os.mkdir(abs_unicode) |
| 87 | try: |
| 88 | os.chdir(abs_encoded) |
| 89 | os.chdir(abs_unicode) |
| 90 | finally: |
| 91 | os.chdir(cwd) |
| 92 | os.rmdir(abs_encoded) |
| 93 | print "All the Unicode tests appeared to work" |
Martin v. Löwis | 7c82a3e0 | 2001-09-05 17:09:48 +0000 | [diff] [blame] | 94 | if oldlocale: |
| 95 | locale.setlocale(locale.LC_CTYPE, oldlocale) |