Skip to content

Commit ce2a216

Browse files
authored
[mypyc] New style new_tuple_op (python#9298)
This PR supports new-style new_tuple_op, which completes the support of all tuple ops.
1 parent 0dfac58 commit ce2a216

File tree

7 files changed

+35
-28
lines changed

7 files changed

+35
-28
lines changed

mypyc/irbuild/builder.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ def compare_tagged(self, lhs: Value, rhs: Value, op: str, line: int) -> Value:
250250
def builtin_len(self, val: Value, line: int) -> Value:
251251
return self.builder.builtin_len(val, line)
252252

253+
def new_tuple(self, items: List[Value], line: int) -> Value:
254+
return self.builder.new_tuple(items, line)
255+
253256
@property
254257
def environment(self) -> Environment:
255258
return self.builder.environment

mypyc/irbuild/classdef.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
not_implemented_op
2323
)
2424
from mypyc.primitives.dict_ops import dict_set_item_op, dict_new_op
25-
from mypyc.primitives.tuple_ops import new_tuple_op
2625
from mypyc.common import SELF_NAME
2726
from mypyc.irbuild.util import (
2827
is_dataclass_decorator, get_func_def, is_dataclass, is_constant, add_self_to_env
@@ -165,7 +164,7 @@ def allocate_class(builder: IRBuilder, cdef: ClassDef) -> Value:
165164
base_exprs = cdef.base_type_exprs + cdef.removed_base_type_exprs
166165
if base_exprs:
167166
bases = [builder.accept(x) for x in base_exprs]
168-
tp_bases = builder.primitive_op(new_tuple_op, bases, cdef.line)
167+
tp_bases = builder.new_tuple(bases, cdef.line)
169168
else:
170169
tp_bases = builder.add(LoadErrorValue(object_rprimitive, is_borrowed=True))
171170
modname = builder.load_static_unicode(builder.module_name)
@@ -219,7 +218,7 @@ def populate_non_ext_bases(builder: IRBuilder, cdef: ClassDef) -> Value:
219218

220219
base = builder.load_global_str(cls.name, cdef.line)
221220
bases.append(base)
222-
return builder.primitive_op(new_tuple_op, bases, cdef.line)
221+
return builder.new_tuple(bases, cdef.line)
223222

224223

225224
def find_non_ext_metaclass(builder: IRBuilder, cdef: ClassDef, bases: Value) -> Value:
@@ -465,9 +464,8 @@ def create_mypyc_attrs_tuple(builder: IRBuilder, ir: ClassIR, line: int) -> Valu
465464
attrs = [name for ancestor in ir.mro for name in ancestor.attributes]
466465
if ir.inherits_python:
467466
attrs.append('__dict__')
468-
return builder.primitive_op(new_tuple_op,
469-
[builder.load_static_unicode(attr) for attr in attrs],
470-
line)
467+
items = [builder.load_static_unicode(attr) for attr in attrs]
468+
return builder.new_tuple(items, line)
471469

472470

473471
def finish_non_ext_dict(builder: IRBuilder, non_ext: NonExtClassInfo, line: int) -> None:

mypyc/irbuild/ll_builder.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def py_call(self,
267267

268268
if len(star_arg_values) == 0:
269269
# We can directly construct a tuple if there are no star args.
270-
pos_args_tuple = self.primitive_op(new_tuple_op, pos_arg_values, line)
270+
pos_args_tuple = self.new_tuple(pos_arg_values, line)
271271
else:
272272
# Otherwise we construct a list and call extend it with the star args, since tuples
273273
# don't have an extend method.
@@ -338,7 +338,8 @@ def native_args_to_positional(self,
338338
for lst, arg in zip(formal_to_actual, sig.args):
339339
output_arg = None
340340
if arg.kind == ARG_STAR:
341-
output_arg = self.primitive_op(new_tuple_op, [args[i] for i in lst], line)
341+
items = [args[i] for i in lst]
342+
output_arg = self.new_tuple(items, line)
342343
elif arg.kind == ARG_STAR2:
343344
dict_entries = [(self.load_static_unicode(cast(str, arg_names[i])), args[i])
344345
for i in lst]
@@ -844,6 +845,10 @@ def builtin_len(self, val: Value, line: int) -> Value:
844845
else:
845846
return self.call_c(generic_len_op, [val], line)
846847

848+
def new_tuple(self, items: List[Value], line: int) -> Value:
849+
load_size_op = self.add(LoadInt(len(items), -1, c_pyssize_t_rprimitive))
850+
return self.call_c(new_tuple_op, [load_size_op] + items, line)
851+
847852
# Internal helpers
848853

849854
def decompose_union_helper(self,

mypyc/primitives/tuple_ops.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
"""
66

77
from mypyc.ir.ops import ERR_MAGIC
8-
from mypyc.ir.rtypes import tuple_rprimitive, int_rprimitive, list_rprimitive, object_rprimitive
8+
from mypyc.ir.rtypes import (
9+
tuple_rprimitive, int_rprimitive, list_rprimitive, object_rprimitive, c_pyssize_t_rprimitive
10+
)
911
from mypyc.primitives.registry import (
10-
c_method_op, custom_op, simple_emit, c_function_op
12+
c_method_op, c_function_op, c_custom_op
1113
)
1214

1315

@@ -20,14 +22,12 @@
2022
error_kind=ERR_MAGIC)
2123

2224
# Construct a boxed tuple from items: (item1, item2, ...)
23-
new_tuple_op = custom_op(
24-
arg_types=[object_rprimitive],
25-
result_type=tuple_rprimitive,
26-
is_var_arg=True,
25+
new_tuple_op = c_custom_op(
26+
arg_types=[c_pyssize_t_rprimitive],
27+
return_type=tuple_rprimitive,
28+
c_function_name='PyTuple_Pack',
2729
error_kind=ERR_MAGIC,
28-
steals=False,
29-
format_str='{dest} = ({comma_args}) :: tuple',
30-
emit=simple_emit('{dest} = PyTuple_Pack({num_args}{comma_if_args}{comma_args});'))
30+
var_arg_type=object_rprimitive)
3131

3232
# Construct tuple from a list.
3333
list_tuple_op = c_function_op(

mypyc/test-data/irbuild-basic.test

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,7 +1258,7 @@ def call_python_function_with_keyword_arg(x):
12581258
L0:
12591259
r0 = load_address PyLong_Type
12601260
r1 = unicode_3 :: static ('base')
1261-
r2 = (x) :: tuple
1261+
r2 = PyTuple_Pack(1, x)
12621262
r3 = box(short_int, 4)
12631263
r4 = CPyDict_Build(1, r1, r3)
12641264
r5 = py_call_with_kwargs(r0, r2, r4)
@@ -1287,15 +1287,15 @@ L0:
12871287
r1 = CPyObject_GetAttr(xs, r0)
12881288
r2 = unicode_5 :: static ('x')
12891289
r3 = box(short_int, 0)
1290-
r4 = (r3) :: tuple
1290+
r4 = PyTuple_Pack(1, r3)
12911291
r5 = box(int, first)
12921292
r6 = CPyDict_Build(1, r2, r5)
12931293
r7 = py_call_with_kwargs(r1, r4, r6)
12941294
r8 = unicode_4 :: static ('insert')
12951295
r9 = CPyObject_GetAttr(xs, r8)
12961296
r10 = unicode_5 :: static ('x')
12971297
r11 = unicode_6 :: static ('i')
1298-
r12 = () :: tuple
1298+
r12 = PyTuple_Pack(0)
12991299
r13 = box(int, second)
13001300
r14 = box(short_int, 2)
13011301
r15 = CPyDict_Build(2, r10, r13, r11, r14)
@@ -1828,7 +1828,7 @@ L0:
18281828
r7 = __main__.globals :: static
18291829
r8 = unicode_6 :: static ('f')
18301830
r9 = CPyDict_GetItem(r7, r8)
1831-
r10 = () :: tuple
1831+
r10 = PyTuple_Pack(0)
18321832
r11 = PyDict_New()
18331833
r12 = CPyDict_UpdateInDisplay(r11, r6)
18341834
r13 = py_call_with_kwargs(r9, r10, r11)
@@ -1840,7 +1840,8 @@ def h():
18401840
r2, r3 :: object
18411841
r4, r5 :: dict
18421842
r6 :: str
1843-
r7, r8 :: object
1843+
r7 :: object
1844+
r8 :: object
18441845
r9 :: tuple
18451846
r10 :: dict
18461847
r11 :: int32
@@ -1856,7 +1857,7 @@ L0:
18561857
r6 = unicode_6 :: static ('f')
18571858
r7 = CPyDict_GetItem(r5, r6)
18581859
r8 = box(short_int, 2)
1859-
r9 = (r8) :: tuple
1860+
r9 = PyTuple_Pack(1, r8)
18601861
r10 = PyDict_New()
18611862
r11 = CPyDict_UpdateInDisplay(r10, r4)
18621863
r12 = py_call_with_kwargs(r7, r9, r10)

mypyc/test-data/irbuild-classes.test

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ L6:
410410
r42 = pytype_from_template(r41, r39, r40)
411411
r43 = C_trait_vtable_setup()
412412
r44 = unicode_8 :: static ('__mypyc_attrs__')
413-
r45 = () :: tuple
413+
r45 = PyTuple_Pack(0)
414414
r46 = PyObject_SetAttr(r42, r44, r45)
415415
__main__.C = r42 :: type
416416
r47 = __main__.globals :: static
@@ -421,7 +421,7 @@ L6:
421421
r52 = __main__.S_template :: type
422422
r53 = pytype_from_template(r52, r50, r51)
423423
r54 = unicode_8 :: static ('__mypyc_attrs__')
424-
r55 = () :: tuple
424+
r55 = PyTuple_Pack(0)
425425
r56 = PyObject_SetAttr(r53, r54, r55)
426426
__main__.S = r53 :: type
427427
r57 = __main__.globals :: static
@@ -436,14 +436,14 @@ L6:
436436
r66 = unicode_6 :: static ('T')
437437
r67 = CPyDict_GetItem(r65, r66)
438438
r68 = PyObject_GetItem(r64, r67)
439-
r69 = (r60, r61, r68) :: tuple
439+
r69 = PyTuple_Pack(3, r60, r61, r68)
440440
r70 = unicode_7 :: static ('__main__')
441441
r71 = __main__.D_template :: type
442442
r72 = pytype_from_template(r71, r69, r70)
443443
r73 = D_trait_vtable_setup()
444444
r74 = unicode_8 :: static ('__mypyc_attrs__')
445445
r75 = unicode_11 :: static ('__dict__')
446-
r76 = (r75) :: tuple
446+
r76 = PyTuple_Pack(1, r75)
447447
r77 = PyObject_SetAttr(r72, r74, r76)
448448
__main__.D = r72 :: type
449449
r78 = __main__.globals :: static

mypyc/test-data/refcount.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ def g(x):
722722
L0:
723723
r0 = load_address PyLong_Type
724724
r1 = unicode_1 :: static ('base')
725-
r2 = (x) :: tuple
725+
r2 = PyTuple_Pack(1, x)
726726
r3 = box(short_int, 4)
727727
r4 = CPyDict_Build(1, r1, r3)
728728
dec_ref r3

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy