Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 1 | # Test the Unicode versions of normal file functions |
| 2 | # open, os.open, os.stat. os.listdir, os.rename, os.remove, os.mkdir, os.chdir, os.rmdir |
Mark Hammond | bb4a47c | 2003-07-16 03:46:38 +0000 | [diff] [blame] | 3 | import sys, os, unittest |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 4 | from test import test_support |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 5 | if not os.path.supports_unicode_filenames: |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 6 | raise test_support.TestSkipped("test works only on NT+") |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 7 | |
| 8 | filenames = [ |
Martin v. Löwis | 45bb87b | 2002-10-07 17:27:15 +0000 | [diff] [blame] | 9 | 'abc', |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 10 | 'ascii', |
| 11 | 'Gr\xfc\xdf-Gott', |
| 12 | '\u0393\u03b5\u03b9\u03ac-\u03c3\u03b1\u03c2', |
| 13 | '\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435', |
| 14 | '\u306b\u307d\u3093', |
| 15 | '\u05d4\u05e9\u05e7\u05e6\u05e5\u05e1', |
| 16 | '\u66e8\u66e9\u66eb', |
| 17 | '\u66e8\u05e9\u3093\u0434\u0393\xdf', |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 18 | ] |
| 19 | |
Tim Peters | 1ee401f | 2002-10-05 17:54:56 +0000 | [diff] [blame] | 20 | # Destroy directory dirname and all files under it, to one level. |
| 21 | def deltree(dirname): |
| 22 | # Don't hide legitimate errors: if one of these suckers exists, it's |
| 23 | # an error if we can't remove it. |
| 24 | if os.path.exists(dirname): |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 25 | # must pass unicode to os.listdir() so we get back unicode results. |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 26 | for fname in os.listdir(str(dirname)): |
Tim Peters | 1ee401f | 2002-10-05 17:54:56 +0000 | [diff] [blame] | 27 | os.unlink(os.path.join(dirname, fname)) |
| 28 | os.rmdir(dirname) |
| 29 | |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 30 | class UnicodeFileTests(unittest.TestCase): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 31 | files = [os.path.join(test_support.TESTFN, f) for f in filenames] |
Tim Peters | 1ee401f | 2002-10-05 17:54:56 +0000 | [diff] [blame] | 32 | |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 33 | def setUp(self): |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 34 | try: |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 35 | os.mkdir(test_support.TESTFN) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 36 | except OSError: |
| 37 | pass |
| 38 | for name in self.files: |
Guido van Rossum | c12a813 | 2007-10-26 04:29:23 +0000 | [diff] [blame^] | 39 | f = open(name, 'wb') |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 40 | f.write((name+'\n').encode("utf-8")) |
| 41 | f.close() |
| 42 | os.stat(name) |
| 43 | |
| 44 | def tearDown(self): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 45 | deltree(test_support.TESTFN) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 46 | |
| 47 | def _apply_failure(self, fn, filename, expected_exception, |
| 48 | check_fn_in_exception = True): |
| 49 | try: |
| 50 | fn(filename) |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 51 | raise test_support.TestFailed("Expected to fail calling '%s(%r)'" |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 52 | % (fn.__name__, filename)) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 53 | except expected_exception as details: |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 54 | if check_fn_in_exception and details.filename != filename: |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 55 | raise test_support.TestFailed("Function '%s(%r) failed with " |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 56 | "bad filename in the exception: %r" |
| 57 | % (fn.__name__, filename, |
| 58 | details.filename)) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 59 | |
| 60 | def test_failures(self): |
| 61 | # Pass non-existing Unicode filenames all over the place. |
| 62 | for name in self.files: |
| 63 | name = "not_" + name |
| 64 | self._apply_failure(open, name, IOError) |
| 65 | self._apply_failure(os.stat, name, OSError) |
| 66 | self._apply_failure(os.chdir, name, OSError) |
| 67 | self._apply_failure(os.rmdir, name, OSError) |
| 68 | self._apply_failure(os.remove, name, OSError) |
| 69 | # listdir may append a wildcard to the filename, so dont check |
| 70 | self._apply_failure(os.listdir, name, OSError, False) |
| 71 | |
| 72 | def test_open(self): |
| 73 | for name in self.files: |
Guido van Rossum | c12a813 | 2007-10-26 04:29:23 +0000 | [diff] [blame^] | 74 | f = open(name, 'wb') |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 75 | f.write((name+'\n').encode("utf-8")) |
| 76 | f.close() |
| 77 | os.stat(name) |
| 78 | |
| 79 | def test_listdir(self): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 80 | f1 = os.listdir(test_support.TESTFN) |
Martin v. Löwis | d6eb352 | 2004-11-07 20:01:56 +0000 | [diff] [blame] | 81 | # Printing f1 is not appropriate, as specific filenames |
| 82 | # returned depend on the local encoding |
Guido van Rossum | c12a813 | 2007-10-26 04:29:23 +0000 | [diff] [blame^] | 83 | f2 = os.listdir(str(test_support.TESTFN.encode("utf-8"), |
Mark Hammond | bb4a47c | 2003-07-16 03:46:38 +0000 | [diff] [blame] | 84 | sys.getfilesystemencoding())) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 85 | f2.sort() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 86 | print(f2) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 87 | |
| 88 | def test_rename(self): |
| 89 | for name in self.files: |
| 90 | os.rename(name,"tmp") |
| 91 | os.rename("tmp",name) |
| 92 | |
| 93 | def test_directory(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 94 | dirname = os.path.join(test_support.TESTFN,'Gr\xfc\xdf-\u66e8\u66e9\u66eb') |
| 95 | filename = '\xdf-\u66e8\u66e9\u66eb' |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 96 | oldwd = os.getcwd() |
| 97 | os.mkdir(dirname) |
| 98 | os.chdir(dirname) |
Guido van Rossum | c12a813 | 2007-10-26 04:29:23 +0000 | [diff] [blame^] | 99 | f = open(filename, 'wb') |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 100 | f.write((filename + '\n').encode("utf-8")) |
| 101 | f.close() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 102 | print(repr(filename)) |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 103 | os.access(filename,os.R_OK) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 104 | os.remove(filename) |
| 105 | os.chdir(oldwd) |
| 106 | os.rmdir(dirname) |
| 107 | |
| 108 | def test_main(): |
Tim Peters | 1ee401f | 2002-10-05 17:54:56 +0000 | [diff] [blame] | 109 | try: |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 110 | test_support.run_unittest(UnicodeFileTests) |
Tim Peters | 1ee401f | 2002-10-05 17:54:56 +0000 | [diff] [blame] | 111 | finally: |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 112 | deltree(test_support.TESTFN) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 113 | |
| 114 | if __name__ == "__main__": |
| 115 | test_main() |