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 = [ |
Victor Stinner | 7362c4f | 2010-10-28 11:20:31 +0000 | [diff] [blame] | 8 | '1_abc', |
| 9 | '2_ascii', |
| 10 | '3_Gr\xfc\xdf-Gott', |
| 11 | '4_\u0393\u03b5\u03b9\u03ac-\u03c3\u03b1\u03c2', |
| 12 | '5_\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435', |
| 13 | '6_\u306b\u307d\u3093', |
| 14 | '7_\u05d4\u05e9\u05e7\u05e6\u05e5\u05e1', |
| 15 | '8_\u66e8\u66e9\u66eb', |
| 16 | '9_\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 |
Victor Stinner | 7362c4f | 2010-10-28 11:20:31 +0000 | [diff] [blame] | 18 | '10_\u1fee\u1ffd', |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 19 | ] |
| 20 | |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 21 | # Mac OS X decomposes Unicode names, using Normal Form D. |
| 22 | # http://developer.apple.com/mac/library/qa/qa2001/qa1173.html |
| 23 | # "However, most volume formats do not follow the exact specification for |
| 24 | # these normal forms. For example, HFS Plus uses a variant of Normal Form D |
| 25 | # in which U+2000 through U+2FFF, U+F900 through U+FAFF, and U+2F800 through |
| 26 | # U+2FAFF are not decomposed." |
| 27 | if sys.platform != 'darwin': |
| 28 | filenames.extend([ |
Victor Stinner | fc6f5a4 | 2010-10-28 22:57:03 +0000 | [diff] [blame] | 29 | # Specific code points: NFC(fn), NFD(fn), NFKC(fn) and NFKD(fn) all differents |
| 30 | '11_\u0385\u03d3\u03d4', |
| 31 | '12_\u00a8\u0301\u03d2\u0301\u03d2\u0308', # == NFD('\u0385\u03d3\u03d4') |
| 32 | '13_\u0020\u0308\u0301\u038e\u03ab', # == NFKC('\u0385\u03d3\u03d4') |
| 33 | '14_\u1e9b\u1fc1\u1fcd\u1fce\u1fcf\u1fdd\u1fde\u1fdf\u1fed', |
| 34 | |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 35 | # Specific code points: fn, NFC(fn) and NFKC(fn) all differents |
Victor Stinner | 7362c4f | 2010-10-28 11:20:31 +0000 | [diff] [blame] | 36 | '15_\u1fee\u1ffd\ufad1', |
| 37 | '16_\u2000\u2000\u2000A', |
| 38 | '17_\u2001\u2001\u2001A', |
| 39 | '18_\u2003\u2003\u2003A', # == NFC('\u2001\u2001\u2001A') |
| 40 | '19_\u0020\u0020\u0020A', # '\u0020' == ' ' == NFKC('\u2000') == |
Florent Xicluna | 65eb453 | 2011-11-03 23:11:14 +0100 | [diff] [blame] | 41 | # NFKC('\u2001') == NFKC('\u2003') |
| 42 | ]) |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 43 | |
Florent Xicluna | 87082ee | 2010-08-09 17:18:05 +0000 | [diff] [blame] | 44 | |
| 45 | # Is it Unicode-friendly? |
| 46 | if not os.path.supports_unicode_filenames: |
Victor Stinner | 94908bb | 2010-08-18 21:23:25 +0000 | [diff] [blame] | 47 | fsencoding = sys.getfilesystemencoding() |
Florent Xicluna | 87082ee | 2010-08-09 17:18:05 +0000 | [diff] [blame] | 48 | try: |
| 49 | for name in filenames: |
| 50 | name.encode(fsencoding) |
| 51 | except UnicodeEncodeError: |
| 52 | raise unittest.SkipTest("only NT+ and systems with " |
| 53 | "Unicode-friendly filesystem encoding") |
| 54 | |
| 55 | |
Tim Peters | 1ee401f | 2002-10-05 17:54:56 +0000 | [diff] [blame] | 56 | # Destroy directory dirname and all files under it, to one level. |
| 57 | def deltree(dirname): |
| 58 | # Don't hide legitimate errors: if one of these suckers exists, it's |
| 59 | # an error if we can't remove it. |
| 60 | if os.path.exists(dirname): |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 61 | # 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] | 62 | for fname in os.listdir(str(dirname)): |
Tim Peters | 1ee401f | 2002-10-05 17:54:56 +0000 | [diff] [blame] | 63 | os.unlink(os.path.join(dirname, fname)) |
| 64 | os.rmdir(dirname) |
| 65 | |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 66 | |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 67 | class UnicodeFileTests(unittest.TestCase): |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 68 | files = set(filenames) |
| 69 | normal_form = None |
Tim Peters | 1ee401f | 2002-10-05 17:54:56 +0000 | [diff] [blame] | 70 | |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 71 | def setUp(self): |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 72 | try: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 73 | os.mkdir(support.TESTFN) |
Florent Xicluna | 65eb453 | 2011-11-03 23:11:14 +0100 | [diff] [blame] | 74 | except FileExistsError: |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 75 | pass |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 76 | files = set() |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 77 | for name in self.files: |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 78 | name = os.path.join(support.TESTFN, self.norm(name)) |
Florent Xicluna | 87082ee | 2010-08-09 17:18:05 +0000 | [diff] [blame] | 79 | with open(name, 'wb') as f: |
| 80 | f.write((name+'\n').encode("utf-8")) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 81 | os.stat(name) |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 82 | files.add(name) |
| 83 | self.files = files |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 84 | |
| 85 | def tearDown(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 86 | deltree(support.TESTFN) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 87 | |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 88 | def norm(self, s): |
| 89 | if self.normal_form: |
| 90 | return normalize(self.normal_form, s) |
| 91 | return s |
| 92 | |
Florent Xicluna | 65eb453 | 2011-11-03 23:11:14 +0100 | [diff] [blame] | 93 | def _apply_failure(self, fn, filename, |
| 94 | expected_exception=FileNotFoundError, |
| 95 | check_filename=True): |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 96 | with self.assertRaises(expected_exception) as c: |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 97 | fn(filename) |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 98 | exc_filename = c.exception.filename |
Florent Xicluna | e0912db | 2011-11-07 21:56:17 +0100 | [diff] [blame] | 99 | # listdir may append a wildcard to the filename |
| 100 | if fn is os.listdir and sys.platform == 'win32': |
| 101 | exc_filename, _, wildcard = exc_filename.rpartition(os.sep) |
Florent Xicluna | be90840 | 2011-11-07 23:24:08 +0100 | [diff] [blame] | 102 | self.assertEqual(wildcard, '*.*') |
Florent Xicluna | 65eb453 | 2011-11-03 23:11:14 +0100 | [diff] [blame] | 103 | if check_filename: |
Victor Stinner | 74ad754 | 2010-10-28 11:09:09 +0000 | [diff] [blame] | 104 | self.assertEqual(exc_filename, filename, "Function '%s(%a) failed " |
Victor Stinner | 7dae81b | 2010-10-28 11:11:24 +0000 | [diff] [blame] | 105 | "with bad filename in the exception: %a" % |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 106 | (fn.__name__, filename, exc_filename)) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 107 | |
| 108 | def test_failures(self): |
| 109 | # Pass non-existing Unicode filenames all over the place. |
| 110 | for name in self.files: |
| 111 | name = "not_" + name |
Florent Xicluna | 65eb453 | 2011-11-03 23:11:14 +0100 | [diff] [blame] | 112 | self._apply_failure(open, name) |
| 113 | self._apply_failure(os.stat, name) |
| 114 | self._apply_failure(os.chdir, name) |
| 115 | self._apply_failure(os.rmdir, name) |
| 116 | self._apply_failure(os.remove, name) |
Florent Xicluna | e0912db | 2011-11-07 21:56:17 +0100 | [diff] [blame] | 117 | self._apply_failure(os.listdir, name) |
| 118 | |
| 119 | if sys.platform == 'win32': |
Florent Xicluna | be90840 | 2011-11-07 23:24:08 +0100 | [diff] [blame] | 120 | # Windows is lunatic. Issue #13366. |
| 121 | _listdir_failure = NotADirectoryError, FileNotFoundError |
Florent Xicluna | e0912db | 2011-11-07 21:56:17 +0100 | [diff] [blame] | 122 | else: |
| 123 | _listdir_failure = NotADirectoryError |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 124 | |
| 125 | def test_open(self): |
| 126 | for name in self.files: |
Guido van Rossum | c12a813 | 2007-10-26 04:29:23 +0000 | [diff] [blame] | 127 | f = open(name, 'wb') |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 128 | f.write((name+'\n').encode("utf-8")) |
| 129 | f.close() |
| 130 | os.stat(name) |
Florent Xicluna | e0912db | 2011-11-07 21:56:17 +0100 | [diff] [blame] | 131 | self._apply_failure(os.listdir, name, self._listdir_failure) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 132 | |
Victor Stinner | bfd7b26 | 2010-10-28 23:14:45 +0000 | [diff] [blame] | 133 | # Skip the test on darwin, because darwin does normalize the filename to |
| 134 | # NFD (a variant of Unicode NFD form). Normalize the filename to NFC, NFKC, |
| 135 | # NFKD in Python is useless, because darwin will normalize it later and so |
| 136 | # open(), os.stat(), etc. don't raise any exception. |
Florent Xicluna | 65eb453 | 2011-11-03 23:11:14 +0100 | [diff] [blame] | 137 | @unittest.skipIf(sys.platform == 'darwin', 'irrelevant test on Mac OS X') |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 138 | def test_normalize(self): |
| 139 | files = set(self.files) |
| 140 | others = set() |
| 141 | for nf in set(['NFC', 'NFD', 'NFKC', 'NFKD']): |
| 142 | others |= set(normalize(nf, file) for file in files) |
| 143 | others -= files |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 144 | for name in others: |
Florent Xicluna | 65eb453 | 2011-11-03 23:11:14 +0100 | [diff] [blame] | 145 | self._apply_failure(open, name) |
| 146 | self._apply_failure(os.stat, name) |
| 147 | self._apply_failure(os.chdir, name) |
| 148 | self._apply_failure(os.rmdir, name) |
| 149 | self._apply_failure(os.remove, name) |
Florent Xicluna | e0912db | 2011-11-07 21:56:17 +0100 | [diff] [blame] | 150 | self._apply_failure(os.listdir, name) |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 151 | |
Victor Stinner | bfd7b26 | 2010-10-28 23:14:45 +0000 | [diff] [blame] | 152 | # Skip the test on darwin, because darwin uses a normalization different |
| 153 | # than Python NFD normalization: filenames are different even if we use |
| 154 | # Python NFD normalization. |
Florent Xicluna | 65eb453 | 2011-11-03 23:11:14 +0100 | [diff] [blame] | 155 | @unittest.skipIf(sys.platform == 'darwin', 'irrelevant test on Mac OS X') |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 156 | def test_listdir(self): |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 157 | sf0 = set(self.files) |
| 158 | f1 = os.listdir(support.TESTFN.encode(sys.getfilesystemencoding())) |
| 159 | f2 = os.listdir(support.TESTFN) |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 160 | sf2 = set(os.path.join(support.TESTFN, f) for f in f2) |
Victor Stinner | 2ebe697 | 2010-10-24 21:05:03 +0000 | [diff] [blame] | 161 | self.assertEqual(sf0, sf2, "%a != %a" % (sf0, sf2)) |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 162 | self.assertEqual(len(f1), len(f2)) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 163 | |
| 164 | def test_rename(self): |
| 165 | for name in self.files: |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 166 | os.rename(name, "tmp") |
| 167 | os.rename("tmp", name) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 168 | |
| 169 | def test_directory(self): |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 170 | 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] | 171 | filename = '\xdf-\u66e8\u66e9\u66eb' |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 172 | oldwd = os.getcwd() |
| 173 | os.mkdir(dirname) |
| 174 | os.chdir(dirname) |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 175 | try: |
| 176 | with open(filename, 'wb') as f: |
| 177 | f.write((filename + '\n').encode("utf-8")) |
| 178 | os.access(filename,os.R_OK) |
| 179 | os.remove(filename) |
| 180 | finally: |
| 181 | os.chdir(oldwd) |
| 182 | os.rmdir(dirname) |
| 183 | |
| 184 | |
| 185 | class UnicodeNFCFileTests(UnicodeFileTests): |
| 186 | normal_form = 'NFC' |
| 187 | |
| 188 | |
| 189 | class UnicodeNFDFileTests(UnicodeFileTests): |
| 190 | normal_form = 'NFD' |
| 191 | |
| 192 | |
| 193 | class UnicodeNFKCFileTests(UnicodeFileTests): |
| 194 | normal_form = 'NFKC' |
| 195 | |
| 196 | |
| 197 | class UnicodeNFKDFileTests(UnicodeFileTests): |
| 198 | normal_form = 'NFKD' |
| 199 | |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 200 | |
| 201 | def test_main(): |
Tim Peters | 1ee401f | 2002-10-05 17:54:56 +0000 | [diff] [blame] | 202 | try: |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 203 | support.run_unittest( |
| 204 | UnicodeFileTests, |
| 205 | UnicodeNFCFileTests, |
| 206 | UnicodeNFDFileTests, |
| 207 | UnicodeNFKCFileTests, |
| 208 | UnicodeNFKDFileTests, |
| 209 | ) |
Tim Peters | 1ee401f | 2002-10-05 17:54:56 +0000 | [diff] [blame] | 210 | finally: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 211 | deltree(support.TESTFN) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 212 | |
Florent Xicluna | eb13618 | 2010-03-21 18:49:50 +0000 | [diff] [blame] | 213 | |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 214 | if __name__ == "__main__": |
| 215 | test_main() |