]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-39573: Add Py_SET_SIZE() function (GH-18400)
authorVictor Stinner <vstinner@python.org>
Fri, 7 Feb 2020 11:05:12 +0000 (12:05 +0100)
committerGitHub <noreply@github.com>
Fri, 7 Feb 2020 11:05:12 +0000 (12:05 +0100)
Add Py_SET_SIZE() function to set the size of an object.

Doc/c-api/structures.rst
Include/cpython/objimpl.h
Include/object.h
Misc/NEWS.d/next/C API/2020-02-07-10-41-53.bpo-39573.EG9VDI.rst [new file with mode: 0644]
Objects/object.c

index 8a1431c2de7faff0798a01d7af50f325f378ceb5..75e2383beb21602e7174b9d96386034a87335b8a 100644 (file)
@@ -101,6 +101,13 @@ the definition of all other Python objects.
       (((PyVarObject*)(o))->ob_size)
 
 
+.. c:function:: void Py_SET_SIZE(PyVarObject *o, Py_ssize_t size)
+
+   Set the object *o* size of *size*.
+
+   .. versionadded:: 3.9
+
+
 .. c:macro:: PyObject_HEAD_INIT(type)
 
    This is a macro which expands to initialization values for a new
index ebb3e234e36fef528588da8c63c815ddc0015025..8e3c964cf44e76c0d3c591a943d0f74c011a2cfa 100644 (file)
@@ -30,7 +30,7 @@ static inline PyVarObject*
 _PyObject_INIT_VAR(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size)
 {
     assert(op != NULL);
-    Py_SIZE(op) = size;
+    Py_SET_SIZE(op, size);
     PyObject_INIT((PyObject *)op, typeobj);
     return op;
 }
index eb887f4c6eb50d258d1cd2dc5de8db5d958fd8a6..68200f7666f17001f908fdc39198c94570043f8f 100644 (file)
@@ -133,6 +133,11 @@ static inline void _Py_SET_TYPE(PyObject *ob, PyTypeObject *type) {
 }
 #define Py_SET_TYPE(ob, type) _Py_SET_TYPE(_PyObject_CAST(ob), type)
 
+static inline void _Py_SET_SIZE(PyVarObject *ob, Py_ssize_t refcnt) {
+    ob->ob_size = refcnt;
+}
+#define Py_SET_SIZE(ob, refcnt) _Py_SET_SIZE(_PyVarObject_CAST(ob), refcnt)
+
 
 /*
 Type objects contain a string containing the type name (to help somewhat
diff --git a/Misc/NEWS.d/next/C API/2020-02-07-10-41-53.bpo-39573.EG9VDI.rst b/Misc/NEWS.d/next/C API/2020-02-07-10-41-53.bpo-39573.EG9VDI.rst
new file mode 100644 (file)
index 0000000..d84cddc
--- /dev/null
@@ -0,0 +1 @@
+Add :c:func:`Py_SET_SIZE` function to set the size of an object.
index ff6c497900cf207b0a4165af4e10a77f62ac1c39..81de3b82530402e6bbd628a027b956fdcd8b5434 100644 (file)
@@ -160,7 +160,7 @@ PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size)
         return (PyVarObject *) PyErr_NoMemory();
     }
 
-    Py_SIZE(op) = size;
+    Py_SET_SIZE(op, size);
     PyObject_Init((PyObject *)op, tp);
     return op;
 }