diff --git a/mypyc/primitives/float_ops.py b/mypyc/primitives/float_ops.py new file mode 100644 index 0000000000000..17bbdfbfe69e8 --- /dev/null +++ b/mypyc/primitives/float_ops.py @@ -0,0 +1,17 @@ +"""Primitive float ops.""" + +from mypyc.ir.ops import ERR_MAGIC +from mypyc.ir.rtypes import ( + str_rprimitive, float_rprimitive +) +from mypyc.primitives.registry import ( + c_function_op +) + +# float(str) +c_function_op( + name='builtins.float', + arg_types=[str_rprimitive], + return_type=float_rprimitive, + c_function_name='PyFloat_FromString', + error_kind=ERR_MAGIC) diff --git a/mypyc/primitives/registry.py b/mypyc/primitives/registry.py index 454e7f1f6db4c..1503341ecb865 100644 --- a/mypyc/primitives/registry.py +++ b/mypyc/primitives/registry.py @@ -345,3 +345,4 @@ def load_address_op(name: str, import mypyc.primitives.dict_ops # noqa import mypyc.primitives.tuple_ops # noqa import mypyc.primitives.misc_ops # noqa +import mypyc.primitives.float_ops # noqa diff --git a/mypyc/test-data/run-floats.test b/mypyc/test-data/run-floats.test new file mode 100644 index 0000000000000..e2ab4b2288610 --- /dev/null +++ b/mypyc/test-data/run-floats.test @@ -0,0 +1,12 @@ +[case testStrToFloat] +def str_to_float(x: str) -> float: + return float(x) + +[file driver.py] +from native import str_to_float + +assert str_to_float("1") == 1.0 +assert str_to_float("1.234567") == 1.234567 +assert str_to_float("44324") == 44324.0 +assert str_to_float("23.4") == 23.4 +assert str_to_float("-43.44e-4") == -43.44e-4 diff --git a/mypyc/test/test_run.py b/mypyc/test/test_run.py index 82a288e0d293b..938bdeb7c9950 100644 --- a/mypyc/test/test_run.py +++ b/mypyc/test/test_run.py @@ -33,6 +33,7 @@ 'run-misc.test', 'run-functions.test', 'run-integers.test', + 'run-floats.test', 'run-bools.test', 'run-strings.test', 'run-tuples.test',