Skip to content

Commit 8a25471

Browse files
Kumzysobolevn
andauthored
Skip instance __dataclass_fields__ lookup for non-dataclass types (#1196)
Skip the instance `__dataclass_fields__` lookup when the type doesn't define it, so objects with a Python level `__getattr__` (like pydantic models) reach `enc_hook` without running Python code. --------- Co-authored-by: sobolevn <[email protected]>
1 parent 7930780 commit 8a25471

2 files changed

Lines changed: 42 additions & 12 deletions

File tree

‎src/msgspec/_core.c‎

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
94209447
static bool
94219448
dataclass_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
}

‎tests/unit/test_common.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3522,6 +3522,18 @@ def __getattr__(self, key):
35223522
sol = proto.encode(msg)
35233523
assert res == sol
35243524

3525+
def test_encode_non_dataclass_skips_instance_getattr(self, proto):
3526+
calls = []
3527+
3528+
class Ex:
3529+
def __getattr__(self, key):
3530+
calls.append(key)
3531+
raise AttributeError(key)
3532+
3533+
res = proto.encode([Ex()], enc_hook=lambda x: "hook")
3534+
assert res == proto.encode(["hook"])
3535+
assert calls == []
3536+
35253537
@pytest.mark.parametrize("field", "xyz")
35263538
def test_encode_dataclass_invalid_field_errors(self, proto, field):
35273539
@dataclass

0 commit comments

Comments
 (0)