Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ jobs:
- "3.10"
- "3.11"
- "3.12"
# TODO: Enable this once fixed https://github.com/jcrist/msgspec/issues/910
# - "3.13"
- "3.13"
- "3.14"

steps:
Expand Down
49 changes: 17 additions & 32 deletions src/msgspec/_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -5322,22 +5322,9 @@ ms_maybe_wrap_validation_error(PathNode *path) {
static PyTypeObject StructMixinType;


/* Note this always allocates an UNTRACKED object */
static PyObject *
Struct_alloc(PyTypeObject *type) {
PyObject *obj;
bool is_gc = MS_TYPE_IS_GC(type);

if (is_gc) {
obj = PyObject_GC_New(PyObject, type);
}
else {
obj = PyObject_New(PyObject, type);
}
if (obj == NULL) return NULL;
/* Zero out slot fields */
memset((char *)obj + sizeof(PyObject), '\0', type->tp_basicsize - sizeof(PyObject));
return obj;
return type->tp_alloc(type, type->tp_itemsize);
}

/* Mirrored from cpython Objects/typeobject.c */
Expand Down Expand Up @@ -7489,7 +7476,6 @@ Struct_decode_post_init(StructMetaObject *st_type, PyObject *obj, PathNode *path
return 0;
}

/* ASSUMPTION - obj is untracked and allocated via Struct_alloc */
static int
Struct_fill_in_defaults(StructMetaObject *st_type, PyObject *obj, PathNode *path) {
Py_ssize_t nfields, ndefaults, i;
Expand Down Expand Up @@ -7517,8 +7503,8 @@ Struct_fill_in_defaults(StructMetaObject *st_type, PyObject *obj, PathNode *path
}
}

if (is_gc && !should_untrack)
PyObject_GC_Track(obj);
if (is_gc && should_untrack && MS_IS_TRACKED(obj))
PyObject_GC_UnTrack(obj);

if (Struct_decode_post_init(st_type, obj, path) < 0) return -1;

Expand Down Expand Up @@ -7694,8 +7680,8 @@ Struct_vectorcall(PyTypeObject *cls, PyObject *const *args, size_t nargsf, PyObj
}
}

if (is_gc && !should_untrack)
PyObject_GC_Track(self);
if (is_gc && should_untrack && MS_IS_TRACKED(self))
PyObject_GC_UnTrack(self);

if (Struct_post_init(st_type, self) < 0) goto error;
return self;
Expand Down Expand Up @@ -7930,8 +7916,8 @@ Struct_copy(PyObject *self, PyObject *args)
Struct_set_index(res, i, val);
}
/* If self is tracked, then copy is tracked */
if (MS_OBJECT_IS_GC(self) && MS_IS_TRACKED(self))
PyObject_GC_Track(res);
if (MS_OBJECT_IS_GC(self) && !MS_IS_TRACKED(self))
PyObject_GC_UnTrack(res);
return res;
error:
Py_DECREF(res);
Expand Down Expand Up @@ -8002,9 +7988,8 @@ Struct_replace(
}
}

if (is_gc && !should_untrack) {
PyObject_GC_Track(out);
}
if (is_gc && should_untrack && MS_IS_TRACKED(out))
PyObject_GC_UnTrack(out);
return out;

error:
Expand Down Expand Up @@ -15826,8 +15811,8 @@ mpack_decode_struct_array_inner(
}
if (Struct_decode_post_init(st_type, res, path) < 0) goto error;
Py_LeaveRecursiveCall();
if (is_gc && !should_untrack)
PyObject_GC_Track(res);
if (is_gc && should_untrack && MS_IS_TRACKED(res))
PyObject_GC_UnTrack(res);
return res;
error:
Py_LeaveRecursiveCall();
Expand Down Expand Up @@ -18099,8 +18084,8 @@ json_decode_struct_array_inner(
}
if (Struct_decode_post_init(st_type, out, path) < 0) goto error;
Py_LeaveRecursiveCall();
if (is_gc && !should_untrack)
PyObject_GC_Track(out);
if (is_gc && should_untrack && MS_IS_TRACKED(out))
PyObject_GC_UnTrack(out);
return out;
error:
Py_LeaveRecursiveCall();
Expand Down Expand Up @@ -21250,8 +21235,8 @@ convert_seq_to_struct_array_inner(
}
if (Struct_decode_post_init(st_type, out, path) < 0) goto error;
Py_LeaveRecursiveCall();
if (is_gc && !should_untrack)
PyObject_GC_Track(out);
if (is_gc && should_untrack && MS_IS_TRACKED(out))
PyObject_GC_UnTrack(out);
return out;
error:
Py_LeaveRecursiveCall();
Expand Down Expand Up @@ -21743,8 +21728,8 @@ convert_object_to_struct(
if (Struct_decode_post_init(struct_type, out, path) < 0) goto error;

Py_LeaveRecursiveCall();
if (is_gc && !should_untrack)
PyObject_GC_Track(out);
if (is_gc && should_untrack && MS_IS_TRACKED(out))
PyObject_GC_UnTrack(out);
return out;

error:
Expand Down