Skip to content

Commit 88c1b85

Browse files
[mypyc] Add LoadAddress op for PyFloat_Type & PyTuple_Type (#13078) (#13150)
- Fixes mypyc/mypyc#924 - Fixes mypyc/mypyc#926 - Fixes mypyc/mypyc#935 This is a follow-up of commit 7811f08. Co-authored-by: Richard Si <[email protected]>
1 parent d06dcf0 commit 88c1b85

File tree

5 files changed

+64
-11
lines changed

5 files changed

+64
-11
lines changed

mypyc/primitives/float_ops.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22

33
from mypyc.ir.ops import ERR_MAGIC
44
from mypyc.ir.rtypes import (
5-
str_rprimitive, float_rprimitive
5+
str_rprimitive, float_rprimitive, object_rprimitive
66
)
77
from mypyc.primitives.registry import (
8-
function_op
8+
load_address_op, function_op
99
)
1010

11+
# Get the 'builtins.float' type object.
12+
load_address_op(
13+
name='builtins.float',
14+
type=object_rprimitive,
15+
src='PyFloat_Type')
16+
1117
# float(str)
1218
function_op(
1319
name='builtins.float',

mypyc/primitives/tuple_ops.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@
99
tuple_rprimitive, int_rprimitive, list_rprimitive, object_rprimitive,
1010
c_pyssize_t_rprimitive, bit_rprimitive
1111
)
12-
from mypyc.primitives.registry import method_op, function_op, custom_op
12+
from mypyc.primitives.registry import load_address_op, method_op, function_op, custom_op
1313

14+
# Get the 'builtins.tuple' type object.
15+
load_address_op(
16+
name='builtins.tuple',
17+
type=object_rprimitive,
18+
src='PyTuple_Type')
1419

1520
# tuple[index] (for an int index)
1621
tuple_get_item_op = method_op(

mypyc/test-data/irbuild-dunders.test

+4-7
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,13 @@ L0:
175175
def f(c):
176176
c :: __main__.C
177177
r0, r1 :: int
178-
r2, r3, r4 :: object
179-
r5 :: str
180-
r6, r7 :: object
178+
r2, r3, r4, r5 :: object
181179
L0:
182180
r0 = c.__neg__()
183181
r1 = c.__invert__()
184182
r2 = load_address PyLong_Type
185183
r3 = PyObject_CallFunctionObjArgs(r2, c, 0)
186-
r4 = builtins :: module
187-
r5 = 'float'
188-
r6 = CPyObject_GetAttr(r4, r5)
189-
r7 = PyObject_CallFunctionObjArgs(r6, c, 0)
184+
r4 = load_address PyFloat_Type
185+
r5 = PyObject_CallFunctionObjArgs(r4, c, 0)
190186
return 1
187+

mypyc/test-data/run-python37.test

+3-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class Person4:
7070

7171
@dataclass
7272
class Person5:
73+
weight: float
7374
friends: Set[str] = field(default_factory=set)
7475
parents: FrozenSet[str] = frozenset()
7576

@@ -122,7 +123,8 @@ assert i8 > i9
122123

123124
assert Person1.__annotations__ == {'age': int, 'name': str}
124125
assert Person2.__annotations__ == {'age': int, 'name': str}
125-
assert Person5.__annotations__ == {'friends': set, 'parents': frozenset}
126+
assert Person5.__annotations__ == {'weight': float, 'friends': set,
127+
'parents': frozenset}
126128

127129
[file driver.py]
128130
import sys

mypyc/test-data/run-tuples.test

+43
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,49 @@ class Sub(NT):
9595
pass
9696
assert f(Sub(3, 2)) == 3
9797

98+
-- Ref: https://github.com/mypyc/mypyc/issues/924
99+
[case testNamedTupleClassSyntax]
100+
from typing import Dict, List, NamedTuple, Optional, Tuple, Union
101+
102+
class ClassIR: pass
103+
104+
class FuncIR: pass
105+
106+
StealsDescription = Union[bool, List[bool]]
107+
108+
class Record(NamedTuple):
109+
st_mtime: float
110+
st_size: int
111+
is_borrowed: bool
112+
hash: str
113+
python_path: Tuple[str, ...]
114+
type: 'ClassIR'
115+
method: FuncIR
116+
shadow_method: Optional[FuncIR]
117+
classes: Dict[str, 'ClassIR']
118+
steals: StealsDescription
119+
ordering: Optional[List[int]]
120+
extra_int_constants: List[Tuple[int]]
121+
122+
[file driver.py]
123+
from typing import Optional
124+
from native import ClassIR, FuncIR, Record
125+
126+
assert Record.__annotations__ == {
127+
'st_mtime': float,
128+
'st_size': int,
129+
'is_borrowed': bool,
130+
'hash': str,
131+
'python_path': tuple,
132+
'type': ClassIR,
133+
'method': FuncIR,
134+
'shadow_method': type,
135+
'classes': dict,
136+
'steals': type,
137+
'ordering': type,
138+
'extra_int_constants': list,
139+
}, Record.__annotations__
140+
98141
[case testTupleOps]
99142
from typing import Tuple, List, Any, Optional
100143
from typing_extensions import Final

0 commit comments

Comments
 (0)