Debian Codegraph @

pypy/interpreter/test/test_fsencode.py

raw symbols SCIP
1 import sys
2 from pypy.interpreter.error import OperationError
3
4 class BaseFSEncodeTest:
5
6 def setup_class(cls):
7 space = cls.space
8 cls.testfn_unencodable = get_unencodable()
9 cls.w_testfn_unencodable = space.wrap(cls.testfn_unencodable)
10 cls.special_char = get_special_char()
11 cls.w_special_char = space.wrap(cls.special_char)
12
13 def get_unencodable():
14 """Copy of the stdlib's support.TESTFN_UNENCODABLE:
15
16 A filename (py3k str type) that should *not* be able to be encoded
17 by the filesystem encoding (in strict mode). It can be None if we
18 cannot generate such filename.
19 """
20 testfn_unencodable = None
21 testfn = u'test_tmp'
22
23 if sys.platform == 'win32':
24 testfn_unencodable = testfn + u"-\u5171\u0141\u2661\u0363\uDC80"
25 elif sys.platform != 'darwin':
26 try:
27 '\xff'.decode(sys.getfilesystemencoding())
28 except UnicodeDecodeError:
29 testfn_unencodable = testfn + u'-\udcff'
30 return testfn_unencodable
31
32 def get_special_char():
33 """Copy of the stdlib's test_imp.test_issue5604 special_char:
34
35 A non-ascii filename (py3k str type) that *should* be able to be
36 encoded by the filesystem encoding (in strict mode). It can be None
37 if we cannot generate such filename.
38 """
39 fsenc = sys.getfilesystemencoding()
40 # covers utf-8 and Windows ANSI code pages one non-space symbol from
41 # every page (http://en.wikipedia.org/wiki/Code_page)
42 known_locales = {
43 'utf-8' : b'\xc3\xa4',
44 'cp1250' : b'\x8C',
45 'cp1251' : b'\xc0',
46 'cp1252' : b'\xc0',
47 'cp1253' : b'\xc1',
48 'cp1254' : b'\xc0',
49 'cp1255' : b'\xe0',
50 'cp1256' : b'\xe0',
51 'cp1257' : b'\xc0',
52 'cp1258' : b'\xc0',
53 }
54
55 if sys.platform == 'darwin':
56 # Mac OS X uses the Normal Form D decomposition
57 # http://developer.apple.com/mac/library/qa/qa2001/qa1173.html
58 special_char = b'a\xcc\x88'
59 else:
60 special_char = known_locales.get(fsenc)
61
62 if special_char:
63 return special_char.decode(fsenc)
64
65 class TestFSEncode(BaseFSEncodeTest):
66 def test_fsencode_fsdecode(self):
67 space = self.space
68 strs = [u"/home/bar/baz", u"c:\\"]
69 if self.special_char:
70 strs.append(self.special_char)
71 for st in strs:
72 # check roundtrip
73 w_st = space.newtext(st.encode('utf8'), len(st))
74 w_enc = space.fsencode(w_st)
75 w_st2 = space.fsdecode(w_enc)
76 assert space.eq_w(w_st, w_st2)
77 assert space.fsdecode_w(w_enc) == st
78
79 assert space.fsencode_w(w_enc) == space.bytes_w(w_enc)
80 assert space.eq_w(space.newfilename(space.bytes_w(w_enc)), w_st2)
81
82 def test_null_byte(self):
83 space = self.space
84 uni = u'abc\x00def'
85 w_u = space.newtext(uni.encode('utf8'), len(uni))
86 # this can behave in two different ways depending on how
87 # much initialized the space is: space.fsencode() can raise
88 # ValueError directly, or return a wrapped bytes with the 0
89 # embedded---and then space.fsencode_w() should raise ValueError.
90 space.raises_w(space.w_ValueError, space.fsencode_w, w_u)
91
92 def test_interface_from___pypy__(self):
93 space = self.space
94 strs = [u"/home/bar/baz", u"c:\\"]
95 if self.special_char:
96 strs.append(self.special_char)
97 for st in strs:
98 w_st = space.newtext(st.encode('utf8'), len(st))
99 w_enc = space.fsencode(w_st)
100 space.appexec([w_st, w_enc], """(u, s):
101 import __pypy__
102 assert __pypy__.fsencode(u) == s
103 assert __pypy__.fsdecode(s) == u
104 """)