Skip to content

Commit 61d200a

Browse files
committed
Make native tuples usable right after allocation
1 parent defec9d commit 61d200a

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

graalpython/com.oracle.graal.python.cext/src/tupleobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ PyObject * tuple_subtype_new(PyTypeObject *type, PyObject *iterable) {
7979
if (newobj == NULL) {
8080
return NULL;
8181
}
82-
newobj->ob_item = (PyObject **) ((char *)newobj + offsetof(PyTupleObject, ob_item) + sizeof(PyObject **));
8382

8483
// This polyglot type cast is important such that we can directly read and
8584
// write members of the pointer from Java code.
@@ -109,6 +108,7 @@ PyObject* PyTruffle_Tuple_Alloc(PyTypeObject* cls, Py_ssize_t nitems) {
109108
*((PyObject **) ((char *)newObj + cls->tp_dictoffset)) = NULL;
110109
}
111110
PyObject_INIT_VAR(newObj, cls, nitems);
111+
((PyTupleObject*)newObj)->ob_item = (PyObject **) ((char *)newObj + offsetof(PyTupleObject, ob_item) + sizeof(PyObject **));
112112
return newObj;
113113
}
114114

graalpython/com.oracle.graal.python.cext/src/typeobject.c

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -306,29 +306,31 @@ static void inherit_slots(PyTypeObject *type, PyTypeObject *base) {
306306
COPYSLOT(tp_iter);
307307
COPYSLOT(tp_iternext);
308308
}
309-
310-
if ((PyTypeObject_tp_flags(type) & Py_TPFLAGS_HAVE_FINALIZE) &&
311-
(PyTypeObject_tp_flags(base) & Py_TPFLAGS_HAVE_FINALIZE)) {
312-
COPYSLOT(tp_finalize);
313-
}
314-
if ((PyTypeObject_tp_flags(type) & Py_TPFLAGS_HAVE_GC) ==
315-
(PyTypeObject_tp_flags(base) & Py_TPFLAGS_HAVE_GC)) {
316-
/* They agree about gc. */
317-
COPYSLOT(tp_free);
318-
}
319-
else if ((PyTypeObject_tp_flags(type) & Py_TPFLAGS_HAVE_GC) &&
320-
PyTypeObject_tp_free(type) == NULL &&
321-
PyTypeObject_tp_free(base) == PyObject_Free) {
322-
/* A bit of magic to plug in the correct default
323-
* tp_free function when a derived class adds gc,
324-
* didn't define tp_free, and the base uses the
325-
* default non-gc tp_free.
309+
{
310+
COPYSLOT(tp_alloc);
311+
if ((PyTypeObject_tp_flags(type) & Py_TPFLAGS_HAVE_FINALIZE) &&
312+
(PyTypeObject_tp_flags(base) & Py_TPFLAGS_HAVE_FINALIZE)) {
313+
COPYSLOT(tp_finalize);
314+
}
315+
if ((PyTypeObject_tp_flags(type) & Py_TPFLAGS_HAVE_GC) ==
316+
(PyTypeObject_tp_flags(base) & Py_TPFLAGS_HAVE_GC)) {
317+
/* They agree about gc. */
318+
COPYSLOT(tp_free);
319+
}
320+
else if ((PyTypeObject_tp_flags(type) & Py_TPFLAGS_HAVE_GC) &&
321+
PyTypeObject_tp_free(type) == NULL &&
322+
PyTypeObject_tp_free(base) == PyObject_Free) {
323+
/* A bit of magic to plug in the correct default
324+
* tp_free function when a derived class adds gc,
325+
* didn't define tp_free, and the base uses the
326+
* default non-gc tp_free.
327+
*/
328+
set_PyTypeObject_tp_free(type, PyObject_GC_Del);
329+
}
330+
/* else they didn't agree about gc, and there isn't something
331+
* obvious to be done -- the type is on its own.
326332
*/
327-
set_PyTypeObject_tp_free(type, PyObject_GC_Del);
328333
}
329-
/* else they didn't agree about gc, and there isn't something
330-
* obvious to be done -- the type is on its own.
331-
*/
332334
}
333335

334336
static int add_member(PyTypeObject* cls, PyObject* type_dict, const char* mname, int mtype, Py_ssize_t moffset, int mflags, char* mdoc) {

0 commit comments

Comments
 (0)