Skip to content

Commit 16f134e

Browse files
authored
1 parent b1be379 commit 16f134e

File tree

4 files changed

+54
-21
lines changed

4 files changed

+54
-21
lines changed

mypyc/doc/str_operations.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Construction
1212
* String literal
1313
* ``str(x: int)``
1414
* ``str(x: object)``
15+
* ``repr(x: int)``
16+
* ``repr(x: object)``
1517

1618
Operators
1719
---------

mypyc/primitives/int_ops.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -77,25 +77,25 @@
7777
error_kind=ERR_MAGIC,
7878
)
7979

80-
# str(int)
81-
int_to_str_op = function_op(
82-
name="builtins.str",
83-
arg_types=[int_rprimitive],
84-
return_type=str_rprimitive,
85-
c_function_name="CPyTagged_Str",
86-
error_kind=ERR_MAGIC,
87-
priority=2,
88-
)
89-
90-
# We need a specialization for str on bools also since the int one is wrong...
91-
function_op(
92-
name="builtins.str",
93-
arg_types=[bool_rprimitive],
94-
return_type=str_rprimitive,
95-
c_function_name="CPyBool_Str",
96-
error_kind=ERR_MAGIC,
97-
priority=3,
98-
)
80+
for name in ("builtins.str", "builtins.repr"):
81+
# str(int) and repr(int)
82+
int_to_str_op = function_op(
83+
name=name,
84+
arg_types=[int_rprimitive],
85+
return_type=str_rprimitive,
86+
c_function_name="CPyTagged_Str",
87+
error_kind=ERR_MAGIC,
88+
priority=2,
89+
)
90+
# We need a specialization for str on bools also since the int one is wrong...
91+
function_op(
92+
name=name,
93+
arg_types=[bool_rprimitive],
94+
return_type=str_rprimitive,
95+
c_function_name="CPyBool_Str",
96+
error_kind=ERR_MAGIC,
97+
priority=3,
98+
)
9999

100100

101101
def int_binary_primitive(

mypyc/primitives/str_ops.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@
3838
error_kind=ERR_MAGIC,
3939
)
4040

41+
# repr(obj)
42+
function_op(
43+
name="builtins.repr",
44+
arg_types=[object_rprimitive],
45+
return_type=str_rprimitive,
46+
c_function_name="PyObject_Repr",
47+
error_kind=ERR_MAGIC,
48+
)
49+
4150
# str1 + str2
4251
binary_op(
4352
name="+",

mypyc/test-data/run-strings.test

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
[case testStrBasics]
44
from typing import Tuple
5+
class A:
6+
def __str__(self) -> str:
7+
return "A-str"
8+
def __repr__(self) -> str:
9+
return "A-repr"
510
def f() -> str:
611
return 'some string'
712
def g() -> str:
@@ -10,6 +15,14 @@ def tostr(x: int) -> str:
1015
return str(x)
1116
def booltostr(x: bool) -> str:
1217
return str(x)
18+
def clstostr(x: A) -> str:
19+
return str(x)
20+
def torepr(x: int) -> str:
21+
return repr(x)
22+
def booltorepr(x: bool) -> str:
23+
return repr(x)
24+
def clstorepr(x: A) -> str:
25+
return repr(x)
1326
def concat(x: str, y: str) -> str:
1427
return x + y
1528
def eq(x: str) -> int:
@@ -29,8 +42,9 @@ def remove_prefix_suffix(x: str, y: str) -> Tuple[str, str]:
2942

3043
[file driver.py]
3144
from native import (
32-
f, g, tostr, booltostr, concat, eq, match, match_tuple,
33-
match_tuple_literal_args, remove_prefix_suffix
45+
f, g, A, tostr, booltostr, clstostr, concat, eq, match, match_tuple,
46+
match_tuple_literal_args, remove_prefix_suffix,
47+
torepr, booltorepr, clstorepr
3448
)
3549
import sys
3650
from testutil import assertRaises
@@ -42,12 +56,20 @@ assert tostr(57) == '57'
4256
assert concat('foo', 'bar') == 'foobar'
4357
assert booltostr(True) == 'True'
4458
assert booltostr(False) == 'False'
59+
assert clstostr(A()) == "A-str"
4560
assert eq('foo') == 0
4661
assert eq('zar') == 1
4762
assert eq('bar') == 2
4863

64+
assert torepr(57) == '57'
65+
assert booltorepr(True) == 'True'
66+
assert booltorepr(False) == 'False'
67+
assert clstorepr(A()) == "A-repr"
68+
4969
assert int(tostr(0)) == 0
5070
assert int(tostr(20)) == 20
71+
assert int(torepr(0)) == 0
72+
assert int(torepr(20)) == 20
5173
assert match('', '') == (True, True)
5274
assert match('abc', '') == (True, True)
5375
assert match('abc', 'a') == (True, False)

0 commit comments

Comments
 (0)