1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# -*- coding: utf-8 -*-
__doc__ = u"""
>>> len(u)
15
"""
cimport cython
_bytes = bytes
cdef unicode text = u'abcäöüöéèâÁÀABC'
u = text
def default():
"""
>>> default() == 'abcdefg'.encode()
True
"""
return u'abcdefg'.encode()
def encode_non_constant(encoding):
"""
>>> isinstance(encode_non_constant('utf8'), _bytes)
True
>>> encode_non_constant('utf8') == u.encode('UTF-8')
True
"""
return text.encode(encoding)
@cython.test_assert_path_exists('//PythonCapiFunctionNode[@cname = "PyUnicode_AsUTF8String"]')
def utf8():
"""
>>> isinstance(utf8(), _bytes)
True
>>> utf8() == u.encode('UTF-8')
True
"""
return text.encode(u'UTF-8')
@cython.test_assert_path_exists('//PythonCapiFunctionNode[@cname = "PyUnicode_AsUTF8String"]')
def utf8_strict():
"""
>>> isinstance(utf8_strict(), _bytes)
True
>>> utf8_strict() == u.encode('UTF-8', 'strict')
True
"""
return text.encode(u'UTF-8', u'strict')
@cython.test_assert_path_exists('//PythonCapiFunctionNode[@cname = "PyUnicode_AsUTF8String"]')
def utf8_str_strict():
"""
>>> isinstance(utf8_str_strict(), _bytes)
True
>>> utf8_str_strict() == u.encode('UTF-8', 'strict')
True
"""
return text.encode('UTF-8', 'strict')
@cython.test_assert_path_exists('//PythonCapiFunctionNode[@cname = "PyUnicode_AsUTF8String"]')
def utf8_bytes_strict():
"""
>>> isinstance(utf8_bytes_strict(), _bytes)
True
>>> utf8_bytes_strict() == u.encode('UTF-8', 'strict')
True
"""
return text.encode(b'UTF-8', b'strict')
@cython.test_assert_path_exists('//PythonCapiFunctionNode[@cname = "PyUnicode_AsEncodedString"]')
def ascii_replace():
"""
>>> isinstance(ascii_replace(), _bytes)
True
>>> ascii_replace() == u.encode('ASCII', 'replace')
True
"""
return text.encode(u'ASCII', u'replace')
def cp850_strict():
"""
>>> isinstance(cp850_strict(), _bytes)
True
>>> cp850_strict() == u.encode('cp850', 'strict')
True
"""
return text.encode(u'cp850', u'strict')
@cython.test_assert_path_exists('//PythonCapiFunctionNode[@cname = "PyUnicode_AsLatin1String"]')
def latin1():
"""
>>> isinstance(latin1(), _bytes)
True
>>> latin1() == u.encode('latin-1')
True
"""
return text.encode(u'latin-1')
@cython.test_fail_if_path_exists('//PythonCapiFunctionNode', '//SimpleCallNode')
def latin1_constant():
"""
>>> isinstance(latin1_constant(), _bytes)
True
>>> latin1_constant() == latin1()
True
"""
return u'abcäöüöéèâÁÀABC'.encode('latin1')
|