4
def coerce_char_default(char c):
6
Default char -> int coercion
8
>>> coerce_char_default(ord('A')) == ord('A')
14
def coerce_uchar_default(unsigned char c):
16
Default char -> int coercion
18
>>> coerce_uchar_default(ord('A')) == ord('A')
24
@cython.test_assert_path_exists("//CoerceIntToBytesNode")
25
@cython.test_fail_if_path_exists("//CoerceToPyTypeNode")
26
def coerce_char_bytes_cast(char c):
28
Explicit char -> bytes coercion
30
>>> coerce_char_bytes_cast(ord('A')) == 'A'.encode('ASCII')
36
@cython.test_assert_path_exists("//CoerceIntToBytesNode")
37
@cython.test_fail_if_path_exists("//CoerceToPyTypeNode")
38
def coerce_uchar_bytes_cast(unsigned char c):
40
Explicit uchar -> bytes coercion
42
>>> coerce_uchar_bytes_cast(ord('A')) == 'A'.encode('ASCII')
44
>>> b = coerce_uchar_bytes_cast(ord('\\xff'))
45
>>> b == '\\xff' or b == '\\xff'.encode('ISO-8859-1') # Py2 or Py3
51
@cython.test_assert_path_exists("//CoerceIntToBytesNode")
52
@cython.test_fail_if_path_exists("//CoerceToPyTypeNode")
53
def coerce_int_bytes_cast(int c):
55
Explicit int -> bytes coercion
57
>>> coerce_int_bytes_cast(ord('A')) == 'A'.encode('ASCII')
59
>>> coerce_int_bytes_cast(ord('A') + 0x100)
60
Traceback (most recent call last):
61
OverflowError: value too large to pack into a byte
62
>>> coerce_int_bytes_cast(ord('A') - 0x100)
63
Traceback (most recent call last):
64
OverflowError: value too large to pack into a byte
69
@cython.test_assert_path_exists("//CoerceIntToBytesNode")
70
@cython.test_fail_if_path_exists("//CoerceToPyTypeNode")
71
def coerce_uint_bytes_cast(unsigned int c):
73
Explicit uint -> bytes coercion
75
>>> coerce_uint_bytes_cast(ord('A')) == 'A'.encode('ASCII')
77
>>> b = coerce_uint_bytes_cast(ord('\\xff'))
78
>>> b == '\\xff' or b == '\\xff'.encode('ISO-8859-1') # Py2 or Py3
81
>>> coerce_uint_bytes_cast(ord('A') + 0x100)
82
Traceback (most recent call last):
83
OverflowError: value too large to pack into a byte
88
@cython.test_assert_path_exists("//CoerceIntToBytesNode")
89
@cython.test_fail_if_path_exists("//CoerceToPyTypeNode")
90
def coerce_char_bytes_assign(char c):
92
Implicit char -> bytes coercion in assignments
94
>>> coerce_char_bytes_assign(ord('A')) == 'A'.encode('ASCII')
101
@cython.test_assert_path_exists("//CoerceIntToBytesNode")
102
@cython.test_fail_if_path_exists("//CoerceToPyTypeNode")
103
def coerce_uchar_bytes_assign(unsigned char c):
105
Implicit uchar -> bytes coercion in assignments
107
>>> coerce_uchar_bytes_assign(ord('A')) == 'A'.encode('ASCII')
109
>>> b = coerce_uchar_bytes_assign(ord('\\xff'))
110
>>> b == '\\xff' or b == '\\xff'.encode('ISO-8859-1') # Py2 or Py3
117
@cython.test_assert_path_exists("//CoerceIntToBytesNode")
118
@cython.test_fail_if_path_exists("//CoerceToPyTypeNode")
119
def coerce_int_bytes_assign(int c):
121
Implicit int -> bytes coercion in assignments
123
>>> coerce_int_bytes_assign(ord('A')) == 'A'.encode('ASCII')
126
>>> coerce_int_bytes_assign(ord('A') + 0x100)
127
Traceback (most recent call last):
128
OverflowError: value too large to pack into a byte
129
>>> coerce_int_bytes_assign(ord('A') - 0x100)
130
Traceback (most recent call last):
131
OverflowError: value too large to pack into a byte
137
@cython.test_assert_path_exists("//CoerceIntToBytesNode")
138
@cython.test_fail_if_path_exists("//CoerceToPyTypeNode")
139
def coerce_uint_bytes_assign(unsigned int c):
141
Implicit uint -> bytes coercion in assignments
143
>>> coerce_uint_bytes_assign(ord('A')) == 'A'.encode('ASCII')
145
>>> b = coerce_uint_bytes_assign(ord('\\xff'))
146
>>> b == '\\xff' or b == '\\xff'.encode('ISO-8859-1') # Py2 or Py3
149
>>> coerce_uint_bytes_assign(ord('A') + 0x100)
150
Traceback (most recent call last):
151
OverflowError: value too large to pack into a byte
157
def inplace_ops_use_arithmetic():
159
>>> print(inplace_ops_use_arithmetic().decode('ascii'))
171
@cython.test_fail_if_path_exists('//CoerceFromPyTypeNode')
172
def indexing_to_char(bytes s):
176
>>> indexing_to_char('abc'.encode('ascii'))
179
cdef unsigned char c = s[1]