Skip to content

Commit 77a195a

Browse files
committed
Add deallocator for str subclasses
1 parent 21a3a0d commit 77a195a

File tree

5 files changed

+9
-35
lines changed

5 files changed

+9
-35
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ typedef struct {
103103
PyObject *weakreflist;
104104
} PyPickleBufferObject;
105105

106+
// defined in 'unicodeobject.c'
107+
void unicode_dealloc(PyObject *unicode);
106108

107109
static void object_dealloc(PyObject *self) {
108110
Py_TYPE(self)->tp_free(self);

graalpython/com.oracle.graal.python.cext/src/capi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ PY_TRUFFLE_TYPE(PyStaticMethod_Type, "staticmethod", &PyType_Type, sizeof(Py
326326
PY_TRUFFLE_TYPE(PySuper_Type, "super", &PyType_Type, sizeof(superobject)) \
327327
PY_TRUFFLE_TYPE(PyTraceBack_Type, "traceback", &PyType_Type, sizeof(PyTypeObject)) \
328328
PY_TRUFFLE_TYPE_GENERIC(PyTuple_Type, "tuple", &PyType_Type, sizeof(PyTupleObject) - sizeof(PyObject *), sizeof(PyObject *), PyTruffle_Tuple_Alloc, (destructor)PyTruffle_Tuple_Dealloc, 0, 0) \
329-
PY_TRUFFLE_TYPE(PyUnicode_Type, "str", &PyType_Type, sizeof(PyUnicodeObject)) \
329+
PY_TRUFFLE_TYPE_GENERIC(PyUnicode_Type, "str", &PyType_Type, sizeof(PyUnicodeObject), 0, NULL, unicode_dealloc, PyObject_Del, 0) \
330330
/* NOTE: we use the same Python type (namely 'PBuiltinFunction') for 'wrapper_descriptor' as for 'method_descriptor'; so the flags must be the same! */ \
331331
PY_TRUFFLE_TYPE(PyWrapperDescr_Type, "wrapper_descriptor", &PyType_Type, sizeof(PyWrapperDescrObject)) \
332332
PY_TRUFFLE_TYPE(PyZip_Type, "zip", &PyType_Type, sizeof(zipobject)) \

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

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ extern "C" {
119119
# define _PyUnicode_CHECK(op) PyUnicode_Check(op)
120120
#endif
121121

122-
#if 0 // GraalPy change
123122
#define _PyUnicode_UTF8(op) \
124123
(_PyCompactUnicodeObject_CAST(op)->utf8)
125124
#define PyUnicode_UTF8(op) \
@@ -190,7 +189,6 @@ extern "C" {
190189
((_PyUnicode_WSTR(op) && \
191190
(!PyUnicode_IS_READY(op) || \
192191
_PyUnicode_WSTR(op) != PyUnicode_DATA(op))))
193-
#endif // GraalPy change
194192

195193
/* Generic helper macro to convert characters of different types.
196194
from_type and to_type have to be valid type names, begin and end
@@ -1715,8 +1713,8 @@ _PyUnicode_Ready(PyObject *unicode)
17151713
return 0;
17161714
}
17171715

1718-
#if 0 // GraalPy change
1719-
static void
1716+
// GraalPy change: export
1717+
PyAPI_FUNC(void)
17201718
unicode_dealloc(PyObject *unicode)
17211719
{
17221720
#ifdef Py_DEBUG
@@ -1725,6 +1723,7 @@ unicode_dealloc(PyObject *unicode)
17251723
}
17261724
#endif
17271725

1726+
#if 0 // GraalPy change
17281727
switch (PyUnicode_CHECK_INTERNED(unicode)) {
17291728
case SSTATE_NOT_INTERNED:
17301729
break;
@@ -1753,6 +1752,7 @@ unicode_dealloc(PyObject *unicode)
17531752
default:
17541753
Py_UNREACHABLE();
17551754
}
1755+
#endif // GraalPy change
17561756

17571757
if (_PyUnicode_HAS_WSTR_MEMORY(unicode)) {
17581758
PyObject_Free(_PyUnicode_WSTR(unicode));
@@ -1767,6 +1767,7 @@ unicode_dealloc(PyObject *unicode)
17671767
Py_TYPE(unicode)->tp_free(unicode);
17681768
}
17691769

1770+
#if 0 // GraalPy change
17701771
#ifdef Py_DEBUG
17711772
static int
17721773
unicode_is_singleton(PyObject *unicode)
@@ -14466,24 +14467,6 @@ unicode_new_impl(PyTypeObject *type, PyObject *x, const char *encoding,
1446614467
PyAPI_FUNC(PyObject *) // GraalPy change: export for downcall
1446714468
unicode_subtype_new(PyTypeObject *type, PyObject *unicode)
1446814469
{
14469-
// GraalPy change: temporarily define struct access macros
14470-
#define _PyUnicode_STATE(op) \
14471-
(_PyASCIIObject_CAST(op)->state)
14472-
#define _PyUnicode_DATA_ANY(op) \
14473-
(_PyUnicodeObject_CAST(op)->data.any)
14474-
#define _PyUnicode_LENGTH(op) \
14475-
(_PyASCIIObject_CAST(op)->length)
14476-
#define _PyUnicode_HASH(op) \
14477-
(_PyASCIIObject_CAST(op)->hash)
14478-
#define _PyUnicode_UTF8(op) \
14479-
(_PyCompactUnicodeObject_CAST(op)->utf8)
14480-
#define _PyUnicode_UTF8_LENGTH(op) \
14481-
(_PyCompactUnicodeObject_CAST(op)->utf8_length)
14482-
#define _PyUnicode_WSTR(op) \
14483-
(_PyASCIIObject_CAST(op)->wstr)
14484-
#define _PyUnicode_WSTR_LENGTH(op) \
14485-
(_PyCompactUnicodeObject_CAST(op)->wstr_length)
14486-
1448714470
PyObject *self;
1448814471
Py_ssize_t length, char_size;
1448914472
int share_wstr, share_utf8;
@@ -14569,15 +14552,6 @@ unicode_subtype_new(PyTypeObject *type, PyObject *unicode)
1456914552
onError:
1457014553
Py_DECREF(self);
1457114554
return NULL;
14572-
// GraalPy change
14573-
#undef _PyUnicode_STATE
14574-
#undef _PyUnicode_DATA_ANY
14575-
#undef _PyUnicode_LENGTH
14576-
#undef _PyUnicode_HASH
14577-
#undef _PyUnicode_UTF8
14578-
#undef _PyUnicode_UTF8_LENGTH
14579-
#undef _PyUnicode_WSTR
14580-
#undef _PyUnicode_WSTR_LENGTH
1458114555
}
1458214556

1458314557
#if 0 // GraalPy change

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_unicode.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ def gen_intern_args():
224224
"UnicodeSubclass",
225225
'',
226226
struct_base='PyUnicodeObject base;',
227-
tp_itemsize='sizeof(char)',
228227
tp_base='&PyUnicode_Type',
229228
tp_new='0',
230229
tp_alloc='0',

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextUnicodeBuiltins.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,8 +1050,7 @@ static Object doNative(PythonAbstractNativeObject s,
10501050
if (lib.isNull(utf8)) {
10511051
PBytes bytes = (PBytes) asUTF8String.execute(s, T_STRICT);
10521052
int len = bufferLib.getBufferLength(bytes);
1053-
// TODO leaked?
1054-
Object mem = allocateNode.alloc(len + 1);
1053+
Object mem = allocateNode.alloc(len + 1, true);
10551054
writeByteNode.writeByteArray(mem, bufferLib.getInternalByteArray(bytes), len, 0, 0);
10561055
writePointerNode.writeToObj(s, CFields.PyCompactUnicodeObject__utf8, mem);
10571056
writeLongNode.writeToObject(s, CFields.PyCompactUnicodeObject__utf8_length, len);

0 commit comments

Comments
 (0)