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