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 |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame^] | 4 | from unicodedata import normalize |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 5 | from test import support |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 6 | |
| 7 | filenames = [ |
Martin v. Löwis | 45bb87b | 2002-10-07 17:27:15 +0000 | [diff] [blame] | 8 | 'abc', |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 9 | 'ascii', |
| 10 | 'Gr\xfc\xdf-Gott', |
| 11 | '\u0393\u03b5\u03b9\u03ac-\u03c3\u03b1\u03c2', |
| 12 | '\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435', |
| 13 | '\u306b\u307d\u3093', |
| 14 | '\u05d4\u05e9\u05e7\u05e6\u05e5\u05e1', |
| 15 | '\u66e8\u66e9\u66eb', |
| 16 | '\u66e8\u05e9\u3093\u0434\u0393\xdf', |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame^] | 17 | # Specific code points: fn, NFC(fn) and NFKC(fn) all differents |
| 18 | '\u2000\u2000\u2000A', |
| 19 | '\u2001\u2001\u2001A', |
| 20 | '\u2003\u2003\u2003A', # == NFC('\u2001\u2001\u2001A') |
| 21 | '\u0020\u0020\u0020A', # '\u0020' == ' ' == NFKC('\u2000') |
| 22 | # '\u0020' == NFKC('\u2001') == NFKC('\u2003') |
| 23 | '\u1fee\u1ffd', |
| 24 | # Specific code points: NFC(fn), NFD(fn), NFKC(fn) and NFKD(fn) all differents |
| 25 | '\u0385\u03d3\u03d4', |
| 26 | '\u00a8\u0301\u03d2\u0301\u03d2\u0308', # == NFD('\u0385\u03d3\u03d4') |
| 27 | '\u0020\u0308\u0301\u038e\u03ab', # == NFKC('\u0385\u03d3\u03d4') |
| 28 | '\u1e9b\u1fc1\u1fcd\u1fce\u1fcf\u1fdd\u1fde\u1fdf\u1fed', |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 29 | ] |
| 30 | |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame^] | 31 | |
Tim Peters | 1ee401f | 2002-10-05 17:54:56 +0000 | [diff] [blame] | 32 | # Destroy directory dirname and all files under it, to one level. |
| 33 | def deltree(dirname): |
| 34 | # Don't hide legitimate errors: if one of these suckers exists, it's |
| 35 | # an error if we can't remove it. |
| 36 | if os.path.exists(dirname): |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 37 | # 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] | 38 | for fname in os.listdir(str(dirname)): |
Tim Peters | 1ee401f | 2002-10-05 17:54:56 +0000 | [diff] [blame] | 39 | os.unlink(os.path.join(dirname, fname)) |
| 40 | os.rmdir(dirname) |
| 41 | |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame^] | 42 | |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 43 | class UnicodeFileTests(unittest.TestCase): |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame^] | 44 | files = set(filenames) |
| 45 | normal_form = None |
Tim Peters | 1ee401f | 2002-10-05 17:54:56 +0000 | [diff] [blame] | 46 | |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 47 | def setUp(self): |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 48 | try: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 49 | os.mkdir(support.TESTFN) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 50 | except OSError: |
| 51 | pass |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame^] | 52 | files = set() |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 53 | for name in self.files: |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame^] | 54 | name = os.path.join(support.TESTFN, self.norm(name)) |
Florent Xicluna | 683a7e7 | 2010-03-03 00:06:37 +0000 | [diff] [blame] | 55 | try: |
| 56 | f = open(name, 'wb') |
| 57 | except UnicodeEncodeError: |
| 58 | if not os.path.supports_unicode_filenames: |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame^] | 59 | self.skipTest("only NT+ and systems with Unicode-friendly" |
| 60 | "filesystem encoding") |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 61 | f.write((name+'\n').encode("utf-8")) |
| 62 | f.close() |
| 63 | os.stat(name) |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame^] | 64 | files.add(name) |
| 65 | self.files = files |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 66 | |
| 67 | def tearDown(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 68 | deltree(support.TESTFN) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 69 | |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame^] | 70 | def norm(self, s): |
| 71 | if self.normal_form: |
| 72 | return normalize(self.normal_form, s) |
| 73 | return s |
| 74 | |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 75 | def _apply_failure(self, fn, filename, expected_exception, |
| 76 | check_fn_in_exception = True): |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame^] | 77 | with self.assertRaises(expected_exception) as c: |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 78 | fn(filename) |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame^] | 79 | exc_filename = c.exception.filename |
| 80 | # the "filename" exception attribute may be encoded |
| 81 | if isinstance(exc_filename, bytes): |
| 82 | filename = filename.encode(sys.getfilesystemencoding()) |
| 83 | if check_fn_in_exception: |
| 84 | self.assertEqual(exc_filename, filename, "Function '%s(%r) failed " |
| 85 | "with bad filename in the exception: %r" % |
| 86 | (fn.__name__, filename, exc_filename)) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 87 | |
| 88 | def test_failures(self): |
| 89 | # Pass non-existing Unicode filenames all over the place. |
| 90 | for name in self.files: |
| 91 | name = "not_" + name |
| 92 | self._apply_failure(open, name, IOError) |
| 93 | self._apply_failure(os.stat, name, OSError) |
| 94 | self._apply_failure(os.chdir, name, OSError) |
| 95 | self._apply_failure(os.rmdir, name, OSError) |
| 96 | self._apply_failure(os.remove, name, OSError) |
| 97 | # listdir may append a wildcard to the filename, so dont check |
| 98 | self._apply_failure(os.listdir, name, OSError, False) |
| 99 | |
| 100 | def test_open(self): |
| 101 | for name in self.files: |
Guido van Rossum | c12a813 | 2007-10-26 04:29:23 +0000 | [diff] [blame] | 102 | f = open(name, 'wb') |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 103 | f.write((name+'\n').encode("utf-8")) |
| 104 | f.close() |
| 105 | os.stat(name) |
| 106 | |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame^] | 107 | def test_normalize(self): |
| 108 | files = set(self.files) |
| 109 | others = set() |
| 110 | for nf in set(['NFC', 'NFD', 'NFKC', 'NFKD']): |
| 111 | others |= set(normalize(nf, file) for file in files) |
| 112 | others -= files |
| 113 | if sys.platform == 'darwin': |
| 114 | files = set(normalize('NFD', file) for file in files) |
| 115 | for name in others: |
| 116 | if sys.platform == 'darwin' and normalize('NFD', name) in files: |
| 117 | # Mac OS X decomposes Unicode names, using Normal Form D. |
| 118 | # http://developer.apple.com/mac/library/qa/qa2001/qa1173.html |
| 119 | os.stat(name) |
| 120 | continue |
| 121 | self._apply_failure(open, name, IOError) |
| 122 | self._apply_failure(os.stat, name, OSError) |
| 123 | self._apply_failure(os.chdir, name, OSError) |
| 124 | self._apply_failure(os.rmdir, name, OSError) |
| 125 | self._apply_failure(os.remove, name, OSError) |
| 126 | # listdir may append a wildcard to the filename, so dont check |
| 127 | self._apply_failure(os.listdir, name, OSError, False) |
| 128 | |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 129 | def test_listdir(self): |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame^] | 130 | sf0 = set(self.files) |
| 131 | f1 = os.listdir(support.TESTFN.encode(sys.getfilesystemencoding())) |
| 132 | f2 = os.listdir(support.TESTFN) |
| 133 | if sys.platform == 'darwin': |
| 134 | # Mac OS X returns canonically decomposed Unicode (Normal Form D) |
| 135 | # http://developer.apple.com/mac/library/qa/qa2001/qa1173.html |
| 136 | sf0 = set(normalize('NFD', f) for f in self.files) |
| 137 | f2 = [normalize('NFD', f) for f in f2] |
| 138 | sf2 = set(os.path.join(support.TESTFN, f) for f in f2) |
| 139 | self.assertEqual(sf0, sf2) |
| 140 | self.assertEqual(len(f1), len(f2)) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 141 | |
| 142 | def test_rename(self): |
| 143 | for name in self.files: |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame^] | 144 | os.rename(name, "tmp") |
| 145 | os.rename("tmp", name) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 146 | |
| 147 | def test_directory(self): |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame^] | 148 | dirname = os.path.join(support.TESTFN, 'Gr\xfc\xdf-\u66e8\u66e9\u66eb') |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 149 | filename = '\xdf-\u66e8\u66e9\u66eb' |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 150 | oldwd = os.getcwd() |
| 151 | os.mkdir(dirname) |
| 152 | os.chdir(dirname) |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame^] | 153 | try: |
| 154 | with open(filename, 'wb') as f: |
| 155 | f.write((filename + '\n').encode("utf-8")) |
| 156 | os.access(filename,os.R_OK) |
| 157 | os.remove(filename) |
| 158 | finally: |
| 159 | os.chdir(oldwd) |
| 160 | os.rmdir(dirname) |
| 161 | |
| 162 | |
| 163 | class UnicodeNFCFileTests(UnicodeFileTests): |
| 164 | normal_form = 'NFC' |
| 165 | |
| 166 | |
| 167 | class UnicodeNFDFileTests(UnicodeFileTests): |
| 168 | normal_form = 'NFD' |
| 169 | |
| 170 | |
| 171 | class UnicodeNFKCFileTests(UnicodeFileTests): |
| 172 | normal_form = 'NFKC' |
| 173 | |
| 174 | |
| 175 | class UnicodeNFKDFileTests(UnicodeFileTests): |
| 176 | normal_form = 'NFKD' |
| 177 | |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 178 | |
| 179 | def test_main(): |
Tim Peters | 1ee401f | 2002-10-05 17:54:56 +0000 | [diff] [blame] | 180 | try: |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame^] | 181 | support.run_unittest( |
| 182 | UnicodeFileTests, |
| 183 | UnicodeNFCFileTests, |
| 184 | UnicodeNFDFileTests, |
| 185 | UnicodeNFKCFileTests, |
| 186 | UnicodeNFKDFileTests, |
| 187 | ) |
Tim Peters | 1ee401f | 2002-10-05 17:54:56 +0000 | [diff] [blame] | 188 | finally: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 189 | deltree(support.TESTFN) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 190 | |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame^] | 191 | |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 192 | if __name__ == "__main__": |
| 193 | test_main() |