From c61bce4e728f87ea8d10f5e2cd0d10135807b72f Mon Sep 17 00:00:00 2001 From: Advait Dixit <48302999+advait-dixit@users.noreply.github.com> Date: Fri, 17 Jan 2025 02:33:51 -0800 Subject: [PATCH] [mypyc] Fixing __init__ for classes with @attr.s(slots=True). (#18447) Fixes mypyc/mypyc#1079. `@attr.s` generates a `__init__` function which was getting lost in `CPyDataclass_SleightOfHand`. This change copies the generated `__init__` function and a couple of others ones to maintain consistency with CPython. --- mypyc/irbuild/classdef.py | 3 ++- mypyc/irbuild/util.py | 2 ++ mypyc/lib-rt/CPy.h | 3 ++- mypyc/lib-rt/misc_ops.c | 28 +++++++++++++++++++++++++--- mypyc/primitives/misc_ops.py | 8 +++++++- mypyc/test-data/run-classes.test | 14 ++++++++++++++ 6 files changed, 52 insertions(+), 6 deletions(-) diff --git a/mypyc/irbuild/classdef.py b/mypyc/irbuild/classdef.py index dda8f31fd893..03368d74c407 100644 --- a/mypyc/irbuild/classdef.py +++ b/mypyc/irbuild/classdef.py @@ -381,9 +381,10 @@ def finalize(self, ir: ClassIR) -> None: dec = self.builder.accept( next(d for d in self.cdef.decorators if is_dataclass_decorator(d)) ) + dataclass_type_val = self.builder.load_str(dataclass_type(self.cdef) or "unknown") self.builder.call_c( dataclass_sleight_of_hand, - [dec, self.type_obj, self.non_ext.dict, self.non_ext.anns], + [dec, self.type_obj, self.non_ext.dict, self.non_ext.anns, dataclass_type_val], self.cdef.line, ) diff --git a/mypyc/irbuild/util.py b/mypyc/irbuild/util.py index e27e509ad7fa..43ee547f8b4f 100644 --- a/mypyc/irbuild/util.py +++ b/mypyc/irbuild/util.py @@ -73,6 +73,8 @@ def is_dataclass(cdef: ClassDef) -> bool: return any(is_dataclass_decorator(d) for d in cdef.decorators) +# The string values returned by this function are inspected in +# mypyc/lib-rt/misc_ops.c:CPyDataclass_SleightOfHand(...). def dataclass_type(cdef: ClassDef) -> str | None: for d in cdef.decorators: typ = dataclass_decorator_type(d) diff --git a/mypyc/lib-rt/CPy.h b/mypyc/lib-rt/CPy.h index 1e6f50306ba1..f72eaea55daf 100644 --- a/mypyc/lib-rt/CPy.h +++ b/mypyc/lib-rt/CPy.h @@ -860,7 +860,8 @@ PyObject *CPyType_FromTemplateWrapper(PyObject *template_, PyObject *orig_bases, PyObject *modname); int CPyDataclass_SleightOfHand(PyObject *dataclass_dec, PyObject *tp, - PyObject *dict, PyObject *annotations); + PyObject *dict, PyObject *annotations, + PyObject *dataclass_type); PyObject *CPyPickle_SetState(PyObject *obj, PyObject *state); PyObject *CPyPickle_GetState(PyObject *obj); CPyTagged CPyTagged_Id(PyObject *o); diff --git a/mypyc/lib-rt/misc_ops.c b/mypyc/lib-rt/misc_ops.c index a7f67fd67d50..e71ef0dc6b48 100644 --- a/mypyc/lib-rt/misc_ops.c +++ b/mypyc/lib-rt/misc_ops.c @@ -347,13 +347,15 @@ static int _CPy_UpdateObjFromDict(PyObject *obj, PyObject *dict) * tp: The class we are making a dataclass * dict: The dictionary containing values that dataclasses needs * annotations: The type annotation dictionary + * dataclass_type: A str object with the return value of util.py:dataclass_type() */ int CPyDataclass_SleightOfHand(PyObject *dataclass_dec, PyObject *tp, - PyObject *dict, PyObject *annotations) { + PyObject *dict, PyObject *annotations, + PyObject *dataclass_type) { PyTypeObject *ttp = (PyTypeObject *)tp; Py_ssize_t pos; - PyObject *res; + PyObject *res = NULL; /* Make a copy of the original class __dict__ */ PyObject *orig_dict = PyDict_Copy(ttp->tp_dict); @@ -381,17 +383,37 @@ CPyDataclass_SleightOfHand(PyObject *dataclass_dec, PyObject *tp, if (!res) { goto fail; } - Py_DECREF(res); + const char *dataclass_type_ptr = PyUnicode_AsUTF8(dataclass_type); + if (dataclass_type_ptr == NULL) { + goto fail; + } + if (strcmp(dataclass_type_ptr, "attr") == 0 || + strcmp(dataclass_type_ptr, "attr-auto") == 0) { + // These attributes are added or modified by @attr.s(slots=True). + const char * const keys[] = {"__attrs_attrs__", "__attrs_own_setattr__", "__init__", ""}; + for (const char * const *key_iter = keys; **key_iter != '\0'; key_iter++) { + PyObject *value = NULL; + int rv = PyObject_GetOptionalAttrString(res, *key_iter, &value); + if (rv == 1) { + PyObject_SetAttrString(tp, *key_iter, value); + Py_DECREF(value); + } else if (rv == -1) { + goto fail; + } + } + } /* Copy back the original contents of the dict */ if (_CPy_UpdateObjFromDict(tp, orig_dict) != 0) { goto fail; } + Py_DECREF(res); Py_DECREF(orig_dict); return 1; fail: + Py_XDECREF(res); Py_XDECREF(orig_dict); return 0; } diff --git a/mypyc/primitives/misc_ops.py b/mypyc/primitives/misc_ops.py index e9016e24c46d..2d8a2d362293 100644 --- a/mypyc/primitives/misc_ops.py +++ b/mypyc/primitives/misc_ops.py @@ -224,7 +224,13 @@ # Create a dataclass from an extension class. See # CPyDataclass_SleightOfHand for more docs. dataclass_sleight_of_hand = custom_op( - arg_types=[object_rprimitive, object_rprimitive, dict_rprimitive, dict_rprimitive], + arg_types=[ + object_rprimitive, + object_rprimitive, + dict_rprimitive, + dict_rprimitive, + str_rprimitive, + ], return_type=bit_rprimitive, c_function_name="CPyDataclass_SleightOfHand", error_kind=ERR_FALSE, diff --git a/mypyc/test-data/run-classes.test b/mypyc/test-data/run-classes.test index 0eab15d89746..168477d5a8ee 100644 --- a/mypyc/test-data/run-classes.test +++ b/mypyc/test-data/run-classes.test @@ -2705,3 +2705,17 @@ print(native.ColorCode.OKGREEN.value) [out] okgreen + +[case testAttrWithSlots] +import attr + +@attr.s(slots=True) +class A: + ints: list[int] = attr.ib() + +[file driver.py] +import native +print(native.A(ints=[1, -17]).ints) + +[out] +\[1, -17]