Skip to content

Commit

Permalink
[mypyc] Add LoadAddress op for PyFloat_Type & PyTuple_Type (#13078) (#…
Browse files Browse the repository at this point in the history
…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 <63936253+ichard26@users.noreply.github.com>
  • Loading branch information
hauntsaninja and ichard26 committed Jul 16, 2022
1 parent d06dcf0 commit 88c1b85
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 11 deletions.
10 changes: 8 additions & 2 deletions mypyc/primitives/float_ops.py
Expand Up @@ -2,12 +2,18 @@

from mypyc.ir.ops import ERR_MAGIC
from mypyc.ir.rtypes import (
str_rprimitive, float_rprimitive
str_rprimitive, float_rprimitive, object_rprimitive
)
from mypyc.primitives.registry import (
function_op
load_address_op, function_op
)

# Get the 'builtins.float' type object.
load_address_op(
name='builtins.float',
type=object_rprimitive,
src='PyFloat_Type')

# float(str)
function_op(
name='builtins.float',
Expand Down
7 changes: 6 additions & 1 deletion mypyc/primitives/tuple_ops.py
Expand Up @@ -9,8 +9,13 @@
tuple_rprimitive, int_rprimitive, list_rprimitive, object_rprimitive,
c_pyssize_t_rprimitive, bit_rprimitive
)
from mypyc.primitives.registry import method_op, function_op, custom_op
from mypyc.primitives.registry import load_address_op, method_op, function_op, custom_op

# Get the 'builtins.tuple' type object.
load_address_op(
name='builtins.tuple',
type=object_rprimitive,
src='PyTuple_Type')

# tuple[index] (for an int index)
tuple_get_item_op = method_op(
Expand Down
11 changes: 4 additions & 7 deletions mypyc/test-data/irbuild-dunders.test
Expand Up @@ -175,16 +175,13 @@ L0:
def f(c):
c :: __main__.C
r0, r1 :: int
r2, r3, r4 :: object
r5 :: str
r6, r7 :: object
r2, r3, r4, r5 :: object
L0:
r0 = c.__neg__()
r1 = c.__invert__()
r2 = load_address PyLong_Type
r3 = PyObject_CallFunctionObjArgs(r2, c, 0)
r4 = builtins :: module
r5 = 'float'
r6 = CPyObject_GetAttr(r4, r5)
r7 = PyObject_CallFunctionObjArgs(r6, c, 0)
r4 = load_address PyFloat_Type
r5 = PyObject_CallFunctionObjArgs(r4, c, 0)
return 1

4 changes: 3 additions & 1 deletion mypyc/test-data/run-python37.test
Expand Up @@ -70,6 +70,7 @@ class Person4:

@dataclass
class Person5:
weight: float
friends: Set[str] = field(default_factory=set)
parents: FrozenSet[str] = frozenset()

Expand Down Expand Up @@ -122,7 +123,8 @@ assert i8 > i9

assert Person1.__annotations__ == {'age': int, 'name': str}
assert Person2.__annotations__ == {'age': int, 'name': str}
assert Person5.__annotations__ == {'friends': set, 'parents': frozenset}
assert Person5.__annotations__ == {'weight': float, 'friends': set,
'parents': frozenset}

[file driver.py]
import sys
Expand Down
43 changes: 43 additions & 0 deletions mypyc/test-data/run-tuples.test
Expand Up @@ -95,6 +95,49 @@ class Sub(NT):
pass
assert f(Sub(3, 2)) == 3

-- Ref: https://github.com/mypyc/mypyc/issues/924
[case testNamedTupleClassSyntax]
from typing import Dict, List, NamedTuple, Optional, Tuple, Union

class ClassIR: pass

class FuncIR: pass

StealsDescription = Union[bool, List[bool]]

class Record(NamedTuple):
st_mtime: float
st_size: int
is_borrowed: bool
hash: str
python_path: Tuple[str, ...]
type: 'ClassIR'
method: FuncIR
shadow_method: Optional[FuncIR]
classes: Dict[str, 'ClassIR']
steals: StealsDescription
ordering: Optional[List[int]]
extra_int_constants: List[Tuple[int]]

[file driver.py]
from typing import Optional
from native import ClassIR, FuncIR, Record

assert Record.__annotations__ == {
'st_mtime': float,
'st_size': int,
'is_borrowed': bool,
'hash': str,
'python_path': tuple,
'type': ClassIR,
'method': FuncIR,
'shadow_method': type,
'classes': dict,
'steals': type,
'ordering': type,
'extra_int_constants': list,
}, Record.__annotations__

[case testTupleOps]
from typing import Tuple, List, Any, Optional
from typing_extensions import Final
Expand Down

0 comments on commit 88c1b85

Please sign in to comment.