diff --git a/mypyc/doc/str_operations.rst b/mypyc/doc/str_operations.rst index 11828a4d128a..4a7aff00f2ad 100644 --- a/mypyc/doc/str_operations.rst +++ b/mypyc/doc/str_operations.rst @@ -12,6 +12,8 @@ Construction * String literal * ``str(x: int)`` * ``str(x: object)`` +* ``repr(x: int)`` +* ``repr(x: object)`` Operators --------- diff --git a/mypyc/primitives/int_ops.py b/mypyc/primitives/int_ops.py index 657578d20046..9b8b48da602d 100644 --- a/mypyc/primitives/int_ops.py +++ b/mypyc/primitives/int_ops.py @@ -77,25 +77,25 @@ error_kind=ERR_MAGIC, ) -# str(int) -int_to_str_op = function_op( - name="builtins.str", - arg_types=[int_rprimitive], - return_type=str_rprimitive, - c_function_name="CPyTagged_Str", - error_kind=ERR_MAGIC, - priority=2, -) - -# We need a specialization for str on bools also since the int one is wrong... -function_op( - name="builtins.str", - arg_types=[bool_rprimitive], - return_type=str_rprimitive, - c_function_name="CPyBool_Str", - error_kind=ERR_MAGIC, - priority=3, -) +for name in ("builtins.str", "builtins.repr"): + # str(int) and repr(int) + int_to_str_op = function_op( + name=name, + arg_types=[int_rprimitive], + return_type=str_rprimitive, + c_function_name="CPyTagged_Str", + error_kind=ERR_MAGIC, + priority=2, + ) + # We need a specialization for str on bools also since the int one is wrong... + function_op( + name=name, + arg_types=[bool_rprimitive], + return_type=str_rprimitive, + c_function_name="CPyBool_Str", + error_kind=ERR_MAGIC, + priority=3, + ) def int_binary_primitive( diff --git a/mypyc/primitives/str_ops.py b/mypyc/primitives/str_ops.py index 75d47b0f0e7a..ded339b9672c 100644 --- a/mypyc/primitives/str_ops.py +++ b/mypyc/primitives/str_ops.py @@ -38,6 +38,15 @@ error_kind=ERR_MAGIC, ) +# repr(obj) +function_op( + name="builtins.repr", + arg_types=[object_rprimitive], + return_type=str_rprimitive, + c_function_name="PyObject_Repr", + error_kind=ERR_MAGIC, +) + # str1 + str2 binary_op( name="+", diff --git a/mypyc/test-data/run-strings.test b/mypyc/test-data/run-strings.test index 07122c2707ac..9183b45b036a 100644 --- a/mypyc/test-data/run-strings.test +++ b/mypyc/test-data/run-strings.test @@ -2,6 +2,11 @@ [case testStrBasics] from typing import Tuple +class A: + def __str__(self) -> str: + return "A-str" + def __repr__(self) -> str: + return "A-repr" def f() -> str: return 'some string' def g() -> str: @@ -10,6 +15,14 @@ def tostr(x: int) -> str: return str(x) def booltostr(x: bool) -> str: return str(x) +def clstostr(x: A) -> str: + return str(x) +def torepr(x: int) -> str: + return repr(x) +def booltorepr(x: bool) -> str: + return repr(x) +def clstorepr(x: A) -> str: + return repr(x) def concat(x: str, y: str) -> str: return x + y def eq(x: str) -> int: @@ -29,8 +42,9 @@ def remove_prefix_suffix(x: str, y: str) -> Tuple[str, str]: [file driver.py] from native import ( - f, g, tostr, booltostr, concat, eq, match, match_tuple, - match_tuple_literal_args, remove_prefix_suffix + f, g, A, tostr, booltostr, clstostr, concat, eq, match, match_tuple, + match_tuple_literal_args, remove_prefix_suffix, + torepr, booltorepr, clstorepr ) import sys from testutil import assertRaises @@ -42,12 +56,20 @@ assert tostr(57) == '57' assert concat('foo', 'bar') == 'foobar' assert booltostr(True) == 'True' assert booltostr(False) == 'False' +assert clstostr(A()) == "A-str" assert eq('foo') == 0 assert eq('zar') == 1 assert eq('bar') == 2 +assert torepr(57) == '57' +assert booltorepr(True) == 'True' +assert booltorepr(False) == 'False' +assert clstorepr(A()) == "A-repr" + assert int(tostr(0)) == 0 assert int(tostr(20)) == 20 +assert int(torepr(0)) == 0 +assert int(torepr(20)) == 20 assert match('', '') == (True, True) assert match('abc', '') == (True, True) assert match('abc', 'a') == (True, False)