20
>>> float_call_conjugate()
27
def float_call_conjugate():
29
>>> float_call_conjugate()
32
x = float(1.5).conjugate()
49
>>> for exp in (14, 15, 16, 30, 31, 32, 52, 53, 54, 60, 61, 62, 63, 64):
50
... for sign in (1, 0, -1):
51
... value = (sign or 1) * 2**exp + sign
52
... float_value = from_int(value)
53
... assert float_value == float(value), "expected %s2**%s+%s == %r, got %r, difference %r" % (
54
... '-' if sign < 0 else '', exp, sign, float(value), float_value, float_value - float(value))
59
@cython.test_assert_path_exists(
60
"//CoerceToPyTypeNode",
61
"//CoerceToPyTypeNode//PythonCapiCallNode",
63
def from_bytes(s: bytes):
65
>>> from_bytes(b"123")
67
>>> from_bytes(b"123.25")
69
>>> from_bytes(b"98_5_6.2_1")
71
>>> from_bytes(b"12_4_131_123123_1893798127398123_19238712_128937198237.8222113_519879812387")
72
1.2413112312318938e+47
73
>>> from_bytes(b"123E100")
75
>>> from_bytes(b"12__._3") # doctest: +ELLIPSIS
76
Traceback (most recent call last):
77
ValueError: ...12__._3...
78
>>> from_bytes(b"_12.3") # doctest: +ELLIPSIS
79
Traceback (most recent call last):
80
ValueError: ..._12.3...
81
>>> from_bytes(b"12.3_") # doctest: +ELLIPSIS
82
Traceback (most recent call last):
83
ValueError: ...12.3_...
84
>>> from_bytes(b"na_n") # doctest: +ELLIPSIS
85
Traceback (most recent call last):
86
ValueError: ...na_n...
87
>>> from_bytes(b"_" * 10000) # doctest: +ELLIPSIS
88
Traceback (most recent call last):
89
ValueError: ...____...
90
>>> from_bytes(None) # doctest: +ELLIPSIS
91
Traceback (most recent call last):
97
@cython.test_assert_path_exists(
98
"//CoerceToPyTypeNode",
99
"//CoerceToPyTypeNode//PythonCapiCallNode",
101
def from_bytes_literals():
103
>>> from_bytes_literals()
104
(123.0, 123.23, 123.76, 1e+100)
106
return float(b"123"), float(b"123.23"), float(b"12_3.7_6"), float(b"1e100")
109
@cython.test_assert_path_exists(
110
"//CoerceToPyTypeNode",
111
"//CoerceToPyTypeNode//PythonCapiCallNode",
113
def from_bytearray(s: bytearray):
115
>>> from_bytearray(bytearray(b"123"))
117
>>> from_bytearray(bytearray(b"123.25"))
119
>>> from_bytearray(bytearray(b"98_5_6.2_1"))
121
>>> from_bytearray(bytearray(b"12_4_131_123123_1893798127398123_19238712_128937198237.8222113_519879812387"))
122
1.2413112312318938e+47
123
>>> from_bytearray(bytearray(b"123E100"))
125
>>> from_bytearray(bytearray(b"12__._3")) # doctest: +ELLIPSIS
126
Traceback (most recent call last):
127
ValueError: ...12__._3...
128
>>> from_bytearray(bytearray(b"_12.3")) # doctest: +ELLIPSIS
129
Traceback (most recent call last):
130
ValueError: ..._12.3...
131
>>> from_bytearray(bytearray(b"12.3_")) # doctest: +ELLIPSIS
132
Traceback (most recent call last):
133
ValueError: ...12.3_...
134
>>> from_bytearray(bytearray(b"in_f")) # doctest: +ELLIPSIS
135
Traceback (most recent call last):
136
ValueError: ...in_f...
137
>>> from_bytearray(None) # doctest: +ELLIPSIS
138
Traceback (most recent call last):
144
@cython.test_assert_path_exists(
145
"//CoerceToPyTypeNode",
146
"//CoerceToPyTypeNode//PythonCapiCallNode",
148
def from_str(s: 'str'):
152
>>> from_str("123.25")
154
>>> from_str("3_21.2_5")
156
>>> from_str("12_4_131_123123_1893798127398123_19238712_128937198237.8222113_519879812387")
157
1.2413112312318938e+47
158
>>> from_str("123E100")
160
>>> from_str("12__._3") # doctest: +ELLIPSIS
161
Traceback (most recent call last):
162
ValueError: ...12__._3...
163
>>> from_str("_12.3") # doctest: +ELLIPSIS
164
Traceback (most recent call last):
165
ValueError: ..._12.3...
166
>>> from_str("12.3_") # doctest: +ELLIPSIS
167
Traceback (most recent call last):
168
ValueError: ...12.3_...
169
>>> from_str("n_an") # doctest: +ELLIPSIS
170
Traceback (most recent call last):
171
ValueError: ...n_an...
172
>>> from_str(None) # doctest: +ELLIPSIS
173
Traceback (most recent call last):
179
@cython.test_assert_path_exists(
180
"//CoerceToPyTypeNode",
181
"//CoerceToPyTypeNode//PythonCapiCallNode",
183
def from_str_literals():
185
>>> from_str_literals()
186
(123.0, 123.23, 124.23, 1e+100)
188
return float("123"), float("123.23"), float("1_2_4.2_3"), float("1e100")
191
@cython.test_assert_path_exists(
192
"//CoerceToPyTypeNode",
193
"//CoerceToPyTypeNode//PythonCapiCallNode",
195
def from_unicode(s: 'unicode'):
197
>>> from_unicode(u"123")
199
>>> from_unicode(u"123.25")
201
>>> from_unicode(u"12_4.8_5")
203
>>> from_unicode(u"12_4_131_123123_1893798127398123_19238712_128937198237.8222113_519879812387")
204
1.2413112312318938e+47
205
>>> from_unicode(u"123E100")
207
>>> from_unicode("೬")
209
>>> from_unicode(u"123.23\\N{PUNCTUATION SPACE}")
211
>>> from_unicode(u"\\N{PUNCTUATION SPACE} 123.23 \\N{PUNCTUATION SPACE}")
213
>>> from_unicode(u"\\N{PUNCTUATION SPACE} 12_3.2_3 \\N{PUNCTUATION SPACE}")
215
>>> from_unicode(u"\\N{PUNCTUATION SPACE} " * 25 + u"123.54 " + u"\\N{PUNCTUATION SPACE} " * 22) # >= 40 chars
217
>>> from_unicode(u"\\N{PUNCTUATION SPACE} " * 25 + u"1_23.5_4 " + u"\\N{PUNCTUATION SPACE} " * 22)
219
>>> from_unicode(u"\\N{PUNCTUATION SPACE} " + u"123.54 " * 2 + u"\\N{PUNCTUATION SPACE}") # doctest: +ELLIPSIS
220
Traceback (most recent call last):
221
ValueError: ...123.54 123.54...
222
>>> from_unicode(u"\\N{PUNCTUATION SPACE} " * 25 + u"123.54 " * 2 + u"\\N{PUNCTUATION SPACE} " * 22) # doctest: +ELLIPSIS
223
Traceback (most recent call last):
224
ValueError: ...123.54 123.54...
225
>>> from_unicode(u"_12__._3") # doctest: +ELLIPSIS
226
Traceback (most recent call last):
227
ValueError: ..._12__._3...
228
>>> from_unicode(u"_12.3") # doctest: +ELLIPSIS
229
Traceback (most recent call last):
230
ValueError: ..._12.3...
231
>>> from_unicode(u"12.3_") # doctest: +ELLIPSIS
232
Traceback (most recent call last):
233
ValueError: ...12.3_...
234
>>> from_unicode(u"i_nf") # doctest: +ELLIPSIS
235
Traceback (most recent call last):
236
ValueError: ...i_nf...
237
>>> from_unicode(None) # doctest: +ELLIPSIS
238
Traceback (most recent call last):
244
@cython.test_assert_path_exists(
245
"//CoerceToPyTypeNode",
246
"//CoerceToPyTypeNode//PythonCapiCallNode",
248
def from_unicode_literals():
250
>>> from_unicode_literals()
251
(123.0, 123.23, 123.45, 1e+100, 123.23)
253
return float(u"123"), float(u"123.23"), float(u"12_3.4_5"), float(u"1e100"), float(u"123.23\N{PUNCTUATION SPACE}")
256
def catch_valueerror(val):
258
>>> catch_valueerror("foo")
260
>>> catch_valueerror(u"foo")
262
>>> catch_valueerror(b"foo")
264
>>> catch_valueerror(bytearray(b"foo"))
266
>>> catch_valueerror("-1")