~ubuntu-branches/ubuntu/trusty/python3.4/trusty-proposed

« back to all changes in this revision

Viewing changes to Lib/test/test_pep277.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-11-25 09:44:27 UTC
  • Revision ID: package-import@ubuntu.com-20131125094427-lzxj8ap5w01lmo7f
Tags: upstream-3.4~b1
ImportĀ upstreamĀ versionĀ 3.4~b1

Show diffs side-by-side

added added

removed removed

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