Skip to content

Commit 0406c4c

Browse files
committed
[mypyc] Add LoadAddress op for PyFloat_Type & PyTuple_Type
- Fixes mypyc/mypyc#924 - Fixes mypyc/mypyc#926 - Fixes mypyc/mypyc#935 This is a follow-up of commit 7811f08.
1 parent 86aefb1 commit 0406c4c

File tree

5 files changed

+88
-11
lines changed

5 files changed

+88
-11
lines changed

mypyc/primitives/float_ops.py

Lines changed: 8 additions & 2 deletions
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

Lines changed: 6 additions & 1 deletion
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

Lines changed: 4 additions & 7 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,73 @@ 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 FileData(NamedTuple):
103+
st_mtime: float
104+
st_size: int
105+
hash: str
106+
107+
class SearchPaths(NamedTuple):
108+
python_path: Tuple[str, ...]
109+
mypy_path: Tuple[str, ...]
110+
package_path: Tuple[str, ...]
111+
typeshed_path: Tuple[str, ...]
112+
113+
class ClassIR: pass
114+
115+
class FuncIR: pass
116+
117+
class VTableMethod(NamedTuple):
118+
cls: 'ClassIR'
119+
name: str
120+
method: FuncIR
121+
shadow_method: Optional[FuncIR]
122+
123+
class DeserMaps(NamedTuple):
124+
classes: Dict[str, 'ClassIR']
125+
functions: Dict[str, 'FuncIR']
126+
127+
StealsDescription = Union[bool, List[bool]]
128+
129+
class RType: pass
130+
131+
class CFunctionDescription(NamedTuple):
132+
name: str
133+
arg_types: List[RType]
134+
return_type: RType
135+
var_arg_type: Optional[RType]
136+
truncated_type: Optional[RType]
137+
c_function_name: str
138+
error_kind: int
139+
steals: StealsDescription
140+
is_borrowed: bool
141+
ordering: Optional[List[int]]
142+
extra_int_constants: List[Tuple[int, RType]]
143+
priority: int
144+
145+
class IntComparisonOpDescription(NamedTuple):
146+
binary_op_variant: int
147+
c_func_description: CFunctionDescription
148+
c_func_negated: bool
149+
c_func_swap_operands: bool
150+
151+
class LoadAddressDescription(NamedTuple):
152+
name: str
153+
type: RType
154+
src: str
155+
156+
[file driver.py]
157+
from native import FileData, SearchPaths, DeserMaps, LoadAddressDescription, RType
158+
159+
assert FileData.__annotations__ == {'st_mtime': float, 'st_size': int, 'hash': str}
160+
assert SearchPaths.__annotations__ == {'python_path': tuple, 'mypy_path': tuple,
161+
'package_path': tuple, 'typeshed_path': tuple}
162+
assert DeserMaps.__annotations__ == {'classes': dict, 'functions': dict}
163+
assert LoadAddressDescription.__annotations__ == {'name': str, 'type': RType, 'src': str}
164+
98165
[case testTupleOps]
99166
from typing import Tuple, List, Any, Optional
100167
from typing_extensions import Final

0 commit comments

Comments
 (0)