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 |
| 18 | u'\u2000\u2000\u2000A', |
| 19 | u'\u2001\u2001\u2001A', |
| 20 | u'\u2003\u2003\u2003A', # == NFC(u'\u2001\u2001\u2001A') |
| 21 | u'\u0020\u0020\u0020A', # u'\u0020' == u' ' == NFKC(u'\u2000') |
| 22 | # u'\u0020' == NFKC(u'\u2001') == NFKC(u'\u2003') |
| 23 | u'\u1fee\u1ffd', |
| 24 | # Specific code points: NFC(fn), NFD(fn), NFKC(fn) and NFKD(fn) all differents |
| 25 | u'\u0385\u03d3\u03d4', |
| 26 | u'\u00a8\u0301\u03d2\u0301\u03d2\u0308', # == NFD(u'\u0385\u03d3\u03d4') |
| 27 | u'\u0020\u0308\u0301\u038e\u03ab', # == NFKC(u'\u0385\u03d3\u03d4') |
| 28 | u'\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 | 77a8849 | 2010-03-21 18:00:38 +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. |
| 38 | for fname in os.listdir(unicode(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 | 77a8849 | 2010-03-21 18:00:38 +0000 | [diff] [blame] | 42 | |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 43 | class UnicodeFileTests(unittest.TestCase): |
Florent Xicluna | 77a8849 | 2010-03-21 18:00:38 +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: |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 49 | os.mkdir(test_support.TESTFN) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 50 | except OSError: |
| 51 | pass |
Florent Xicluna | 77a8849 | 2010-03-21 18:00:38 +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 | 77a8849 | 2010-03-21 18:00:38 +0000 | [diff] [blame] | 54 | name = os.path.join(test_support.TESTFN, self.norm(name)) |
Florent Xicluna | c0a9d41 | 2010-03-02 22:34:11 +0000 | [diff] [blame] | 55 | try: |
| 56 | f = open(name, 'w') |
| 57 | except UnicodeEncodeError: |
| 58 | if not os.path.supports_unicode_filenames: |
Florent Xicluna | 77a8849 | 2010-03-21 18:00:38 +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 | 77a8849 | 2010-03-21 18:00:38 +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): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 68 | deltree(test_support.TESTFN) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 69 | |
Florent Xicluna | 77a8849 | 2010-03-21 18:00:38 +0000 | [diff] [blame] | 70 | def norm(self, s): |
| 71 | if self.normal_form and isinstance(s, unicode): |
| 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 | 77a8849 | 2010-03-21 18:00:38 +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 | 77a8849 | 2010-03-21 18:00:38 +0000 | [diff] [blame] | 79 | exc_filename = c.exception.filename |
| 80 | # the "filename" exception attribute may be encoded |
| 81 | if isinstance(exc_filename, str): |
| 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: |
| 102 | f = open(name, 'w') |
| 103 | f.write((name+'\n').encode("utf-8")) |
| 104 | f.close() |
| 105 | os.stat(name) |
| 106 | |
Florent Xicluna | 77a8849 | 2010-03-21 18:00:38 +0000 | [diff] [blame] | 107 | def test_normalize(self): |
| 108 | files = set(f for f in self.files if isinstance(f, unicode)) |
| 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 | 77a8849 | 2010-03-21 18:00:38 +0000 | [diff] [blame] | 130 | sf0 = set(self.files) |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 131 | f1 = os.listdir(test_support.TESTFN) |
Mark Hammond | bb4a47c | 2003-07-16 03:46:38 +0000 | [diff] [blame] | 132 | f2 = os.listdir(unicode(test_support.TESTFN, |
| 133 | sys.getfilesystemencoding())) |
Florent Xicluna | 77a8849 | 2010-03-21 18:00:38 +0000 | [diff] [blame] | 134 | if sys.platform == 'darwin': |
| 135 | # Mac OS X returns canonically decomposed Unicode (Normal Form D) |
| 136 | # http://developer.apple.com/mac/library/qa/qa2001/qa1173.html |
| 137 | sf0 = set(normalize('NFD', unicode(f)) for f in self.files) |
| 138 | f2 = [normalize('NFD', unicode(f)) for f in f2] |
| 139 | sf2 = set(os.path.join(unicode(test_support.TESTFN), f) for f in f2) |
| 140 | self.assertEqual(sf0, sf2) |
| 141 | self.assertEqual(len(f1), len(f2)) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 142 | |
| 143 | def test_rename(self): |
| 144 | for name in self.files: |
Florent Xicluna | 77a8849 | 2010-03-21 18:00:38 +0000 | [diff] [blame] | 145 | os.rename(name, "tmp") |
| 146 | os.rename("tmp", name) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 147 | |
| 148 | def test_directory(self): |
Florent Xicluna | 77a8849 | 2010-03-21 18:00:38 +0000 | [diff] [blame] | 149 | dirname = os.path.join(test_support.TESTFN, |
| 150 | u'Gr\xfc\xdf-\u66e8\u66e9\u66eb') |
Martin v. Löwis | 45bb87b | 2002-10-07 17:27:15 +0000 | [diff] [blame] | 151 | filename = u'\xdf-\u66e8\u66e9\u66eb' |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 152 | oldwd = os.getcwd() |
| 153 | os.mkdir(dirname) |
| 154 | os.chdir(dirname) |
Florent Xicluna | 77a8849 | 2010-03-21 18:00:38 +0000 | [diff] [blame] | 155 | try: |
| 156 | with open(filename, 'w') as f: |
| 157 | f.write((filename + '\n').encode("utf-8")) |
| 158 | os.access(filename,os.R_OK) |
| 159 | os.remove(filename) |
| 160 | finally: |
| 161 | os.chdir(oldwd) |
| 162 | os.rmdir(dirname) |
| 163 | |
| 164 | |
| 165 | class UnicodeNFCFileTests(UnicodeFileTests): |
| 166 | normal_form = 'NFC' |
| 167 | |
| 168 | |
| 169 | class UnicodeNFDFileTests(UnicodeFileTests): |
| 170 | normal_form = 'NFD' |
| 171 | |
| 172 | |
| 173 | class UnicodeNFKCFileTests(UnicodeFileTests): |
| 174 | normal_form = 'NFKC' |
| 175 | |
| 176 | |
| 177 | class UnicodeNFKDFileTests(UnicodeFileTests): |
| 178 | normal_form = 'NFKD' |
| 179 | |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 180 | |
| 181 | def test_main(): |
Tim Peters | 1ee401f | 2002-10-05 17:54:56 +0000 | [diff] [blame] | 182 | try: |
Florent Xicluna | 77a8849 | 2010-03-21 18:00:38 +0000 | [diff] [blame] | 183 | test_support.run_unittest( |
| 184 | UnicodeFileTests, |
| 185 | UnicodeNFCFileTests, |
| 186 | UnicodeNFDFileTests, |
| 187 | UnicodeNFKCFileTests, |
| 188 | UnicodeNFKDFileTests, |
| 189 | ) |
Tim Peters | 1ee401f | 2002-10-05 17:54:56 +0000 | [diff] [blame] | 190 | finally: |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 191 | deltree(test_support.TESTFN) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 192 | |
Florent Xicluna | 77a8849 | 2010-03-21 18:00:38 +0000 | [diff] [blame] | 193 | |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 194 | if __name__ == "__main__": |
| 195 | test_main() |