Skip to content

Commit

Permalink
[mypyc] Merge pytype_from_template_op (python#9359)
Browse files Browse the repository at this point in the history
This PR merges pytype_from_template_op op.
  • Loading branch information
TH3CHARLie authored Sep 4, 2020
1 parent 57d3473 commit 66687bf
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 15 deletions.
5 changes: 3 additions & 2 deletions mypyc/codegen/emitmodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -897,8 +897,9 @@ def generate_module_def(self, emitter: Emitter, module_name: str, module: Module
if cl.is_generated:
type_struct = emitter.type_struct_name(cl)
emitter.emit_lines(
'{t} = (PyTypeObject *)CPyType_FromTemplate({t}_template, NULL, modname);'.
format(t=type_struct))
'{t} = (PyTypeObject *)CPyType_FromTemplate('
'(PyObject *){t}_template, NULL, modname);'
.format(t=type_struct))
emitter.emit_lines('if (unlikely(!{}))'.format(type_struct),
' return NULL;')

Expand Down
4 changes: 2 additions & 2 deletions mypyc/irbuild/classdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ def allocate_class(builder: IRBuilder, cdef: ClassDef) -> Value:
template = builder.add(LoadStatic(object_rprimitive, cdef.name + "_template",
builder.module_name, NAMESPACE_TYPE))
# Create the class
tp = builder.primitive_op(pytype_from_template_op,
[template, tp_bases, modname], cdef.line)
tp = builder.call_c(pytype_from_template_op,
[template, tp_bases, modname], cdef.line)
# Immediately fix up the trait vtables, before doing anything with the class.
ir = builder.mapper.type_to_ir[cdef.info]
if not ir.is_trait and not ir.builtin_base:
Expand Down
5 changes: 4 additions & 1 deletion mypyc/lib-rt/CPy.h
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,12 @@ PyObject *CPy_GetCoro(PyObject *obj);
PyObject *CPyIter_Send(PyObject *iter, PyObject *val);
int CPy_YieldFromErrorHandle(PyObject *iter, PyObject **outp);
PyObject *CPy_FetchStopIterationValue(void);
PyObject *CPyType_FromTemplate(PyTypeObject *template_,
PyObject *CPyType_FromTemplate(PyObject *template_,
PyObject *orig_bases,
PyObject *modname);
PyObject *CPyType_FromTemplateWarpper(PyObject *template_,
PyObject *orig_bases,
PyObject *modname);
int CPyDataclass_SleightOfHand(PyObject *dataclass_dec, PyObject *tp,
PyObject *dict, PyObject *annotations);
PyObject *CPyPickle_SetState(PyObject *obj, PyObject *state);
Expand Down
3 changes: 2 additions & 1 deletion mypyc/lib-rt/misc_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ static bool _CPy_IsSafeMetaClass(PyTypeObject *metaclass) {
// This is super hacky and maybe we should suck it up and use PyType_FromSpec instead.
// We allow bases to be NULL to represent just inheriting from object.
// We don't support NULL bases and a non-type metaclass.
PyObject *CPyType_FromTemplate(PyTypeObject *template_,
PyObject *CPyType_FromTemplate(PyObject *template,
PyObject *orig_bases,
PyObject *modname) {
PyTypeObject *template_ = (PyTypeObject *)template;
PyHeapTypeObject *t = NULL;
PyTypeObject *dummy_class = NULL;
PyObject *name = NULL;
Expand Down
10 changes: 4 additions & 6 deletions mypyc/primitives/misc_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,11 @@

# Create a heap type based on a template non-heap type.
# See CPyType_FromTemplate for more docs.
pytype_from_template_op = custom_op(
pytype_from_template_op = c_custom_op(
arg_types=[object_rprimitive, object_rprimitive, str_rprimitive],
result_type=object_rprimitive,
error_kind=ERR_MAGIC,
format_str='{dest} = pytype_from_template({comma_args})',
emit=simple_emit(
'{dest} = CPyType_FromTemplate((PyTypeObject *){args[0]}, {args[1]}, {args[2]});'))
return_type=object_rprimitive,
c_function_name='CPyType_FromTemplate',
error_kind=ERR_MAGIC)

# Create a dataclass from an extension class. See
# CPyDataclass_SleightOfHand for more docs.
Expand Down
6 changes: 3 additions & 3 deletions mypyc/test-data/irbuild-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ L6:
r39 = <error> :: object
r40 = load_global CPyStatic_unicode_7 :: static ('__main__')
r41 = __main__.C_template :: type
r42 = pytype_from_template(r41, r39, r40)
r42 = CPyType_FromTemplate(r41, r39, r40)
r43 = C_trait_vtable_setup()
r44 = load_global CPyStatic_unicode_8 :: static ('__mypyc_attrs__')
r45 = PyTuple_Pack(0)
Expand All @@ -416,7 +416,7 @@ L6:
r50 = <error> :: object
r51 = load_global CPyStatic_unicode_7 :: static ('__main__')
r52 = __main__.S_template :: type
r53 = pytype_from_template(r52, r50, r51)
r53 = CPyType_FromTemplate(r52, r50, r51)
r54 = load_global CPyStatic_unicode_8 :: static ('__mypyc_attrs__')
r55 = PyTuple_Pack(0)
r56 = PyObject_SetAttr(r53, r54, r55)
Expand All @@ -436,7 +436,7 @@ L6:
r69 = PyTuple_Pack(3, r60, r61, r68)
r70 = load_global CPyStatic_unicode_7 :: static ('__main__')
r71 = __main__.D_template :: type
r72 = pytype_from_template(r71, r69, r70)
r72 = CPyType_FromTemplate(r71, r69, r70)
r73 = D_trait_vtable_setup()
r74 = load_global CPyStatic_unicode_8 :: static ('__mypyc_attrs__')
r75 = load_global CPyStatic_unicode_11 :: static ('__dict__')
Expand Down

0 comments on commit 66687bf

Please sign in to comment.