Skip to content

Commit b10dc3e

Browse files
authored
bpo-39573: Add Py_SET_SIZE() function (GH-18400)
Add Py_SET_SIZE() function to set the size of an object.
1 parent 877ea88 commit b10dc3e

File tree

5 files changed

+15
-2
lines changed

5 files changed

+15
-2
lines changed

Doc/c-api/structures.rst

+7
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ the definition of all other Python objects.
101101
(((PyVarObject*)(o))->ob_size)
102102
103103
104+
.. c:function:: void Py_SET_SIZE(PyVarObject *o, Py_ssize_t size)
105+
106+
Set the object *o* size of *size*.
107+
108+
.. versionadded:: 3.9
109+
110+
104111
.. c:macro:: PyObject_HEAD_INIT(type)
105112
106113
This is a macro which expands to initialization values for a new

Include/cpython/objimpl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static inline PyVarObject*
3030
_PyObject_INIT_VAR(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size)
3131
{
3232
assert(op != NULL);
33-
Py_SIZE(op) = size;
33+
Py_SET_SIZE(op, size);
3434
PyObject_INIT((PyObject *)op, typeobj);
3535
return op;
3636
}

Include/object.h

+5
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ static inline void _Py_SET_TYPE(PyObject *ob, PyTypeObject *type) {
133133
}
134134
#define Py_SET_TYPE(ob, type) _Py_SET_TYPE(_PyObject_CAST(ob), type)
135135

136+
static inline void _Py_SET_SIZE(PyVarObject *ob, Py_ssize_t refcnt) {
137+
ob->ob_size = refcnt;
138+
}
139+
#define Py_SET_SIZE(ob, refcnt) _Py_SET_SIZE(_PyVarObject_CAST(ob), refcnt)
140+
136141

137142
/*
138143
Type objects contain a string containing the type name (to help somewhat
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add :c:func:`Py_SET_SIZE` function to set the size of an object.

Objects/object.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size)
160160
return (PyVarObject *) PyErr_NoMemory();
161161
}
162162

163-
Py_SIZE(op) = size;
163+
Py_SET_SIZE(op, size);
164164
PyObject_Init((PyObject *)op, tp);
165165
return op;
166166
}

0 commit comments

Comments
 (0)