Skip to content

Commit

Permalink
Fix "float(None)" after optimising the "float('...')" parsing case..
Browse files Browse the repository at this point in the history
  • Loading branch information
scoder committed Nov 20, 2020
1 parent ba19517 commit c59a1dc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
8 changes: 7 additions & 1 deletion Cython/Compiler/Optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2629,6 +2629,7 @@ def _handle_simple_function_float(self, node, function, pos_args):
return ExprNodes.TypecastNode(
node.pos, operand=func_arg, type=node.type)

arg = None
if func_arg.type is Builtin.bytes_type:
cfunc_name = "__Pyx_PyBytes_AsDouble"
utility_code_name = 'pybytes_as_double'
Expand All @@ -2642,13 +2643,18 @@ def _handle_simple_function_float(self, node, function, pos_args):
cfunc_name = "__Pyx_PyString_AsDouble"
utility_code_name = 'pystring_as_double'
else:
arg = func_arg # no need for an additional None check
cfunc_name = "__Pyx_PyObject_AsDouble"
utility_code_name = 'pyobject_as_double'

if arg is None:
arg = func_arg.as_none_safe_node(
"float() argument must be a string or a number, not 'NoneType'")

return ExprNodes.PythonCapiCallNode(
node.pos, cfunc_name,
self.PyObject_AsDouble_func_type,
args = pos_args,
args = [arg],
is_temp = node.is_temp,
utility_code = load_c_utility(utility_code_name),
py_name = "float")
Expand Down
12 changes: 12 additions & 0 deletions tests/run/builtin_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ def from_bytes(s: bytes):
1.2413112312318938e+47
>>> from_bytes(b"123E100")
1.23e+102
>>> from_bytes(None) # doctest: +ELLIPSIS
Traceback (most recent call last):
TypeError...
"""
return float(s)

Expand Down Expand Up @@ -92,6 +95,9 @@ def from_bytearray(s: bytearray):
1.2413112312318938e+47
>>> from_bytearray(bytearray(b"123E100"))
1.23e+102
>>> from_bytearray(None) # doctest: +ELLIPSIS
Traceback (most recent call last):
TypeError...
"""
return float(s)

Expand All @@ -112,6 +118,9 @@ def from_str(s: 'str'):
1.2413112312318938e+47
>>> from_str("123E100")
1.23e+102
>>> from_str(None) # doctest: +ELLIPSIS
Traceback (most recent call last):
TypeError...
"""
return float(s)

Expand Down Expand Up @@ -146,6 +155,9 @@ def from_unicode(s: 'unicode'):
1.23e+102
>>> from_unicode(u"123.23\\N{PUNCTUATION SPACE}")
123.23
>>> from_unicode(None) # doctest: +ELLIPSIS
Traceback (most recent call last):
TypeError...
"""
return float(s)

Expand Down

0 comments on commit c59a1dc

Please sign in to comment.