test_unicode_file_functions.py 6.84 KB
Newer Older
1 2
# Test the Unicode versions of normal file functions
# open, os.open, os.stat. os.listdir, os.rename, os.remove, os.mkdir, os.chdir, os.rmdir
3 4 5 6
import os
import sys
import unittest
import warnings
7
from unicodedata import normalize
8
from test import support
9 10

filenames = [
11 12 13 14 15 16 17 18 19
    '1_abc',
    '2_ascii',
    '3_Gr\xfc\xdf-Gott',
    '4_\u0393\u03b5\u03b9\u03ac-\u03c3\u03b1\u03c2',
    '5_\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435',
    '6_\u306b\u307d\u3093',
    '7_\u05d4\u05e9\u05e7\u05e6\u05e5\u05e1',
    '8_\u66e8\u66e9\u66eb',
    '9_\u66e8\u05e9\u3093\u0434\u0393\xdf',
20
    # Specific code points: fn, NFC(fn) and NFKC(fn) all differents
21
    '10_\u1fee\u1ffd',
22 23
    ]

24 25 26 27 28 29 30 31
# Mac OS X decomposes Unicode names, using Normal Form D.
# http://developer.apple.com/mac/library/qa/qa2001/qa1173.html
# "However, most volume formats do not follow the exact specification for
# these normal forms.  For example, HFS Plus uses a variant of Normal Form D
# in which U+2000 through U+2FFF, U+F900 through U+FAFF, and U+2F800 through
# U+2FAFF are not decomposed."
if sys.platform != 'darwin':
    filenames.extend([
32 33 34 35 36 37
        # Specific code points: NFC(fn), NFD(fn), NFKC(fn) and NFKD(fn) all differents
        '11_\u0385\u03d3\u03d4',
        '12_\u00a8\u0301\u03d2\u0301\u03d2\u0308', # == NFD('\u0385\u03d3\u03d4')
        '13_\u0020\u0308\u0301\u038e\u03ab',       # == NFKC('\u0385\u03d3\u03d4')
        '14_\u1e9b\u1fc1\u1fcd\u1fce\u1fcf\u1fdd\u1fde\u1fdf\u1fed',

38
        # Specific code points: fn, NFC(fn) and NFKC(fn) all differents
39 40 41 42 43
        '15_\u1fee\u1ffd\ufad1',
        '16_\u2000\u2000\u2000A',
        '17_\u2001\u2001\u2001A',
        '18_\u2003\u2003\u2003A',  # == NFC('\u2001\u2001\u2001A')
        '19_\u0020\u0020\u0020A',  # '\u0020' == ' ' == NFKC('\u2000') ==
44 45
                                   #  NFKC('\u2001') == NFKC('\u2003')
    ])
46

47 48 49

# Is it Unicode-friendly?
if not os.path.supports_unicode_filenames:
50
    fsencoding = sys.getfilesystemencoding()
51 52 53 54 55 56 57 58
    try:
        for name in filenames:
            name.encode(fsencoding)
    except UnicodeEncodeError:
        raise unittest.SkipTest("only NT+ and systems with "
                                "Unicode-friendly filesystem encoding")


59
class UnicodeFileTests(unittest.TestCase):
60 61
    files = set(filenames)
    normal_form = None
62

63 64
    def setUp(self):
        try:
65
            os.mkdir(support.TESTFN)
66
        except FileExistsError:
67
            pass
68 69
        self.addCleanup(support.rmtree, support.TESTFN)

70
        files = set()
71
        for name in self.files:
72
            name = os.path.join(support.TESTFN, self.norm(name))
73 74
            with open(name, 'wb') as f:
                f.write((name+'\n').encode("utf-8"))
75
            os.stat(name)
76 77
            files.add(name)
        self.files = files
78

79 80 81 82 83
    def norm(self, s):
        if self.normal_form:
            return normalize(self.normal_form, s)
        return s

84 85 86
    def _apply_failure(self, fn, filename,
                       expected_exception=FileNotFoundError,
                       check_filename=True):
87
        with self.assertRaises(expected_exception) as c:
88
            fn(filename)
89
        exc_filename = c.exception.filename
90
        if check_filename:
91
            self.assertEqual(exc_filename, filename, "Function '%s(%a) failed "
92
                             "with bad filename in the exception: %a" %
93
                             (fn.__name__, filename, exc_filename))
94 95 96 97 98

    def test_failures(self):
        # Pass non-existing Unicode filenames all over the place.
        for name in self.files:
            name = "not_" + name
99 100 101 102 103
            self._apply_failure(open, name)
            self._apply_failure(os.stat, name)
            self._apply_failure(os.chdir, name)
            self._apply_failure(os.rmdir, name)
            self._apply_failure(os.remove, name)
104 105 106
            self._apply_failure(os.listdir, name)

    if sys.platform == 'win32':
107 108
        # Windows is lunatic. Issue #13366.
        _listdir_failure = NotADirectoryError, FileNotFoundError
109 110
    else:
        _listdir_failure = NotADirectoryError
111 112 113

    def test_open(self):
        for name in self.files:
114
            f = open(name, 'wb')
115 116 117
            f.write((name+'\n').encode("utf-8"))
            f.close()
            os.stat(name)
118
            self._apply_failure(os.listdir, name, self._listdir_failure)
119

120 121 122 123
    # Skip the test on darwin, because darwin does normalize the filename to
    # NFD (a variant of Unicode NFD form). Normalize the filename to NFC, NFKC,
    # NFKD in Python is useless, because darwin will normalize it later and so
    # open(), os.stat(), etc. don't raise any exception.
124
    @unittest.skipIf(sys.platform == 'darwin', 'irrelevant test on Mac OS X')
125 126 127 128 129 130 131
    def test_normalize(self):
        files = set(self.files)
        others = set()
        for nf in set(['NFC', 'NFD', 'NFKC', 'NFKD']):
            others |= set(normalize(nf, file) for file in files)
        others -= files
        for name in others:
132 133 134 135 136
            self._apply_failure(open, name)
            self._apply_failure(os.stat, name)
            self._apply_failure(os.chdir, name)
            self._apply_failure(os.rmdir, name)
            self._apply_failure(os.remove, name)
137
            self._apply_failure(os.listdir, name)
138

139 140 141
    # Skip the test on darwin, because darwin uses a normalization different
    # than Python NFD normalization: filenames are different even if we use
    # Python NFD normalization.
142
    @unittest.skipIf(sys.platform == 'darwin', 'irrelevant test on Mac OS X')
143
    def test_listdir(self):
144
        sf0 = set(self.files)
145 146 147
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", DeprecationWarning)
            f1 = os.listdir(support.TESTFN.encode(sys.getfilesystemencoding()))
148 149
        f2 = os.listdir(support.TESTFN)
        sf2 = set(os.path.join(support.TESTFN, f) for f in f2)
150
        self.assertEqual(sf0, sf2, "%a != %a" % (sf0, sf2))
151
        self.assertEqual(len(f1), len(f2))
152 153 154

    def test_rename(self):
        for name in self.files:
155 156
            os.rename(name, "tmp")
            os.rename("tmp", name)
157 158

    def test_directory(self):
159
        dirname = os.path.join(support.TESTFN, 'Gr\xfc\xdf-\u66e8\u66e9\u66eb')
160
        filename = '\xdf-\u66e8\u66e9\u66eb'
161
        with support.temp_cwd(dirname):
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
            with open(filename, 'wb') as f:
                f.write((filename + '\n').encode("utf-8"))
            os.access(filename,os.R_OK)
            os.remove(filename)


class UnicodeNFCFileTests(UnicodeFileTests):
    normal_form = 'NFC'


class UnicodeNFDFileTests(UnicodeFileTests):
    normal_form = 'NFD'


class UnicodeNFKCFileTests(UnicodeFileTests):
    normal_form = 'NFKC'


class UnicodeNFKDFileTests(UnicodeFileTests):
    normal_form = 'NFKD'

183 184

def test_main():
185 186 187 188 189 190 191
    support.run_unittest(
        UnicodeFileTests,
        UnicodeNFCFileTests,
        UnicodeNFDFileTests,
        UnicodeNFKCFileTests,
        UnicodeNFKDFileTests,
    )
192

193

194 195
if __name__ == "__main__":
    test_main()