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 |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 18 | '\u1fee\u1ffd', |
| 19 | # Specific code points: NFC(fn), NFD(fn), NFKC(fn) and NFKD(fn) all differents |
| 20 | '\u0385\u03d3\u03d4', |
| 21 | '\u00a8\u0301\u03d2\u0301\u03d2\u0308', # == NFD('\u0385\u03d3\u03d4') |
| 22 | '\u0020\u0308\u0301\u038e\u03ab', # == NFKC('\u0385\u03d3\u03d4') |
| 23 | '\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 | fd1b093 | 2010-03-28 00:25:02 +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 | '\u1fee\u1ffd\ufad1', |
| 36 | '\u2000\u2000\u2000A', |
| 37 | '\u2001\u2001\u2001A', |
| 38 | '\u2003\u2003\u2003A', # == NFC('\u2001\u2001\u2001A') |
| 39 | '\u0020\u0020\u0020A', # '\u0020' == ' ' == NFKC('\u2000') == |
| 40 | # NFKC('\u2001') == NFKC('\u2003') |
| 41 | ]) |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 42 | |
Florent Xicluna | 87082ee | 2010-08-09 17:18:05 +0000 | [diff] [blame] | 43 | |
| 44 | # Is it Unicode-friendly? |
| 45 | if not os.path.supports_unicode_filenames: |
Victor Stinner | 94908bb | 2010-08-18 21:23:25 +0000 | [diff] [blame] | 46 | fsencoding = sys.getfilesystemencoding() |
Florent Xicluna | 87082ee | 2010-08-09 17:18:05 +0000 | [diff] [blame] | 47 | try: |
| 48 | for name in filenames: |
| 49 | name.encode(fsencoding) |
| 50 | except UnicodeEncodeError: |
| 51 | raise unittest.SkipTest("only NT+ and systems with " |
| 52 | "Unicode-friendly filesystem encoding") |
| 53 | |
| 54 | |
Tim Peters | 1ee401f | 2002-10-05 17:54:56 +0000 | [diff] [blame] | 55 | # Destroy directory dirname and all files under it, to one level. |
| 56 | def deltree(dirname): |
| 57 | # Don't hide legitimate errors: if one of these suckers exists, it's |
| 58 | # an error if we can't remove it. |
| 59 | if os.path.exists(dirname): |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 60 | # 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] | 61 | for fname in os.listdir(str(dirname)): |
Tim Peters | 1ee401f | 2002-10-05 17:54:56 +0000 | [diff] [blame] | 62 | os.unlink(os.path.join(dirname, fname)) |
| 63 | os.rmdir(dirname) |
| 64 | |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 65 | |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 66 | class UnicodeFileTests(unittest.TestCase): |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 67 | files = set(filenames) |
| 68 | normal_form = None |
Tim Peters | 1ee401f | 2002-10-05 17:54:56 +0000 | [diff] [blame] | 69 | |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 70 | def setUp(self): |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 71 | try: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 72 | os.mkdir(support.TESTFN) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 73 | except OSError: |
| 74 | pass |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 75 | files = set() |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 76 | for name in self.files: |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 77 | name = os.path.join(support.TESTFN, self.norm(name)) |
Florent Xicluna | 87082ee | 2010-08-09 17:18:05 +0000 | [diff] [blame] | 78 | with open(name, 'wb') as f: |
| 79 | f.write((name+'\n').encode("utf-8")) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 80 | os.stat(name) |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 81 | files.add(name) |
| 82 | self.files = files |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 83 | |
| 84 | def tearDown(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 85 | deltree(support.TESTFN) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 86 | |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 87 | def norm(self, s): |
| 88 | if self.normal_form: |
| 89 | return normalize(self.normal_form, s) |
| 90 | return s |
| 91 | |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 92 | def _apply_failure(self, fn, filename, expected_exception, |
| 93 | check_fn_in_exception = True): |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 94 | with self.assertRaises(expected_exception) as c: |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 95 | fn(filename) |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 96 | exc_filename = c.exception.filename |
| 97 | # the "filename" exception attribute may be encoded |
| 98 | if isinstance(exc_filename, bytes): |
| 99 | filename = filename.encode(sys.getfilesystemencoding()) |
| 100 | if check_fn_in_exception: |
| 101 | self.assertEqual(exc_filename, filename, "Function '%s(%r) failed " |
| 102 | "with bad filename in the exception: %r" % |
| 103 | (fn.__name__, filename, exc_filename)) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 104 | |
| 105 | def test_failures(self): |
| 106 | # Pass non-existing Unicode filenames all over the place. |
| 107 | for name in self.files: |
| 108 | name = "not_" + name |
| 109 | self._apply_failure(open, name, IOError) |
| 110 | self._apply_failure(os.stat, name, OSError) |
| 111 | self._apply_failure(os.chdir, name, OSError) |
| 112 | self._apply_failure(os.rmdir, name, OSError) |
| 113 | self._apply_failure(os.remove, name, OSError) |
| 114 | # listdir may append a wildcard to the filename, so dont check |
| 115 | self._apply_failure(os.listdir, name, OSError, False) |
| 116 | |
| 117 | def test_open(self): |
| 118 | for name in self.files: |
Guido van Rossum | c12a813 | 2007-10-26 04:29:23 +0000 | [diff] [blame] | 119 | f = open(name, 'wb') |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 120 | f.write((name+'\n').encode("utf-8")) |
| 121 | f.close() |
| 122 | os.stat(name) |
| 123 | |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 124 | def test_normalize(self): |
| 125 | files = set(self.files) |
| 126 | others = set() |
| 127 | for nf in set(['NFC', 'NFD', 'NFKC', 'NFKD']): |
| 128 | others |= set(normalize(nf, file) for file in files) |
| 129 | others -= files |
| 130 | if sys.platform == 'darwin': |
| 131 | files = set(normalize('NFD', file) for file in files) |
| 132 | for name in others: |
| 133 | if sys.platform == 'darwin' and normalize('NFD', name) in files: |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 134 | # Mac OS X decomposes Unicode names. See comment above. |
Victor Stinner | 5c1808a | 2010-08-19 17:35:00 +0000 | [diff] [blame] | 135 | try: |
| 136 | os.stat(name) |
| 137 | except OSError as err: |
| 138 | raise AssertionError("File %a doesn't exist" % name) |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 139 | continue |
| 140 | self._apply_failure(open, name, IOError) |
| 141 | self._apply_failure(os.stat, name, OSError) |
| 142 | self._apply_failure(os.chdir, name, OSError) |
| 143 | self._apply_failure(os.rmdir, name, OSError) |
| 144 | self._apply_failure(os.remove, name, OSError) |
| 145 | # listdir may append a wildcard to the filename, so dont check |
| 146 | self._apply_failure(os.listdir, name, OSError, False) |
| 147 | |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 148 | def test_listdir(self): |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 149 | sf0 = set(self.files) |
| 150 | f1 = os.listdir(support.TESTFN.encode(sys.getfilesystemencoding())) |
| 151 | f2 = os.listdir(support.TESTFN) |
| 152 | if sys.platform == 'darwin': |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 153 | # Mac OS X decomposes Unicode names. See comment above. |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 154 | sf0 = set(normalize('NFD', f) for f in self.files) |
| 155 | f2 = [normalize('NFD', f) for f in f2] |
| 156 | sf2 = set(os.path.join(support.TESTFN, f) for f in f2) |
Victor Stinner | 2ebe697 | 2010-10-24 21:05:03 +0000 | [diff] [blame^] | 157 | self.assertEqual(sf0, sf2, "%a != %a" % (sf0, sf2)) |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 158 | self.assertEqual(len(f1), len(f2)) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 159 | |
| 160 | def test_rename(self): |
| 161 | for name in self.files: |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 162 | os.rename(name, "tmp") |
| 163 | os.rename("tmp", name) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 164 | |
| 165 | def test_directory(self): |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 166 | 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] | 167 | filename = '\xdf-\u66e8\u66e9\u66eb' |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 168 | oldwd = os.getcwd() |
| 169 | os.mkdir(dirname) |
| 170 | os.chdir(dirname) |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 171 | try: |
| 172 | with open(filename, 'wb') as f: |
| 173 | f.write((filename + '\n').encode("utf-8")) |
| 174 | os.access(filename,os.R_OK) |
| 175 | os.remove(filename) |
| 176 | finally: |
| 177 | os.chdir(oldwd) |
| 178 | os.rmdir(dirname) |
| 179 | |
| 180 | |
| 181 | class UnicodeNFCFileTests(UnicodeFileTests): |
| 182 | normal_form = 'NFC' |
| 183 | |
| 184 | |
| 185 | class UnicodeNFDFileTests(UnicodeFileTests): |
| 186 | normal_form = 'NFD' |
| 187 | |
| 188 | |
| 189 | class UnicodeNFKCFileTests(UnicodeFileTests): |
| 190 | normal_form = 'NFKC' |
| 191 | |
| 192 | |
| 193 | class UnicodeNFKDFileTests(UnicodeFileTests): |
| 194 | normal_form = 'NFKD' |
| 195 | |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 196 | |
| 197 | def test_main(): |
Tim Peters | 1ee401f | 2002-10-05 17:54:56 +0000 | [diff] [blame] | 198 | try: |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 199 | support.run_unittest( |
| 200 | UnicodeFileTests, |
| 201 | UnicodeNFCFileTests, |
| 202 | UnicodeNFDFileTests, |
| 203 | UnicodeNFKCFileTests, |
| 204 | UnicodeNFKDFileTests, |
| 205 | ) |
Tim Peters | 1ee401f | 2002-10-05 17:54:56 +0000 | [diff] [blame] | 206 | finally: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 207 | deltree(support.TESTFN) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 208 | |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 209 | |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 210 | if __name__ == "__main__": |
| 211 | test_main() |