@@ -9417,6 +9417,33 @@ typedef struct {
94179417 bool standard_getattr;
94189418} DataclassIter;
94199419
9420+ static MS_INLINE PyObject *
9421+ ms_type_lookup_ref(PyTypeObject *type, PyObject *name) {
9422+ #if PY313_PLUS
9423+ return _PyType_LookupRef(type, name);
9424+ #else
9425+ PyObject *out = _PyType_Lookup(type, name);
9426+ Py_XINCREF(out);
9427+ return out;
9428+ #endif
9429+ }
9430+
9431+ static PyObject *
9432+ ms_get_dataclass_fields(MsgspecState *mod, PyTypeObject *type, PyObject *obj) {
9433+ PyObject *fields = ms_type_lookup_ref(type, mod->str___dataclass_fields__);
9434+ if (fields == NULL) return NULL;
9435+ if (MS_LIKELY(
9436+ PyDict_CheckExact(fields) &&
9437+ type->tp_getattro == PyObject_GenericGetAttr
9438+ )) {
9439+ return fields;
9440+ }
9441+ Py_DECREF(fields);
9442+ fields = PyObject_GetAttr(obj, mod->str___dataclass_fields__);
9443+ if (fields == NULL) PyErr_Clear();
9444+ return fields;
9445+ }
9446+
94209447static bool
94219448dataclass_iter_setup(DataclassIter *iter, PyObject *obj, PyObject *fields) {
94229449 iter->dict = NULL;
@@ -13704,15 +13731,12 @@ mpack_encode_uncommon(EncoderState *self, PyTypeObject *type, PyObject *obj)
1370413731 return mpack_encode_set(self, obj);
1370513732 }
1370613733 else if (!PyType_Check(obj) && type->tp_dict != NULL) {
13707- PyObject *fields = PyObject_GetAttr(obj, self->mod->str___dataclass_fields__ );
13734+ PyObject *fields = ms_get_dataclass_fields( self->mod, type, obj );
1370813735 if (fields != NULL) {
1370913736 int status = mpack_encode_dataclass(self, obj, fields);
1371013737 Py_DECREF(fields);
1371113738 return status;
1371213739 }
13713- else {
13714- PyErr_Clear();
13715- }
1371613740 if (PyDict_Contains(type->tp_dict, self->mod->str___attrs_attrs__)) {
1371713741 return mpack_encode_object(self, obj);
1371813742 }
@@ -14843,15 +14867,12 @@ json_encode_uncommon(EncoderState *self, PyTypeObject *type, PyObject *obj) {
1484314867 return json_encode_set(self, obj);
1484414868 }
1484514869 else if (!PyType_Check(obj) && type->tp_dict != NULL) {
14846- PyObject *fields = PyObject_GetAttr(obj, self->mod->str___dataclass_fields__ );
14870+ PyObject *fields = ms_get_dataclass_fields( self->mod, type, obj );
1484714871 if (fields != NULL) {
1484814872 int status = json_encode_dataclass(self, obj, fields);
1484914873 Py_DECREF(fields);
1485014874 return status;
1485114875 }
14852- else {
14853- PyErr_Clear();
14854- }
1485514876 if (PyDict_Contains(type->tp_dict, self->mod->str___attrs_attrs__)) {
1485614877 return json_encode_object(self, obj);
1485714878 }
@@ -20653,15 +20674,12 @@ to_builtins(ToBuiltinsState *self, PyObject *obj, bool is_key) {
2065320674 return to_builtins_set(self, obj, is_key);
2065420675 }
2065520676 else if (!PyType_Check(obj) && type->tp_dict != NULL) {
20656- PyObject *fields = PyObject_GetAttr(obj, self->mod->str___dataclass_fields__ );
20677+ PyObject *fields = ms_get_dataclass_fields( self->mod, type, obj );
2065720678 if (fields != NULL) {
2065820679 PyObject *out = to_builtins_dataclass(self, obj, fields);
2065920680 Py_DECREF(fields);
2066020681 return out;
2066120682 }
20662- else {
20663- PyErr_Clear();
20664- }
2066520683 if (PyDict_Contains(type->tp_dict, self->mod->str___attrs_attrs__)) {
2066620684 return to_builtins_object(self, obj);
2066720685 }
0 commit comments