Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions mypyc/doc/str_operations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Construction
* String literal
* ``str(x: int)``
* ``str(x: object)``
* ``repr(x: int)``
* ``repr(x: object)``

Operators
---------
Expand Down
38 changes: 19 additions & 19 deletions mypyc/primitives/int_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
9 changes: 9 additions & 0 deletions mypyc/primitives/str_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="+",
Expand Down
26 changes: 24 additions & 2 deletions mypyc/test-data/run-strings.test
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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)
Expand Down