[Python-checkins] bpo-39573: Add Py_SET_SIZE() function (GH-18400)

Victor Stinner webhook-mailer at python.org
Fri Feb 7 06:05:17 EST 2020


https://github.com/python/cpython/commit/b10dc3e7a11fcdb97e285882eba6da92594f90f9
commit: b10dc3e7a11fcdb97e285882eba6da92594f90f9
branch: master
author: Victor Stinner <vstinner at python.org>
committer: GitHub <noreply at github.com>
date: 2020-02-07T12:05:12+01:00
summary:

bpo-39573: Add Py_SET_SIZE() function (GH-18400)

Add Py_SET_SIZE() function to set the size of an object.

files:
A Misc/NEWS.d/next/C API/2020-02-07-10-41-53.bpo-39573.EG9VDI.rst
M Doc/c-api/structures.rst
M Include/cpython/objimpl.h
M Include/object.h
M Objects/object.c

diff --git a/Doc/c-api/structures.rst b/Doc/c-api/structures.rst
index 8a1431c2de7fa..75e2383beb216 100644
--- a/Doc/c-api/structures.rst
+++ b/Doc/c-api/structures.rst
@@ -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
diff --git a/Include/cpython/objimpl.h b/Include/cpython/objimpl.h
index ebb3e234e36fe..8e3c964cf44e7 100644
--- a/Include/cpython/objimpl.h
+++ b/Include/cpython/objimpl.h
@@ -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;
 }
diff --git a/Include/object.h b/Include/object.h
index eb887f4c6eb50..68200f7666f17 100644
--- a/Include/object.h
+++ b/Include/object.h
@@ -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
index 0000000000000..d84cddc57636b
--- /dev/null
+++ b/Misc/NEWS.d/next/C API/2020-02-07-10-41-53.bpo-39573.EG9VDI.rst	
@@ -0,0 +1 @@
+Add :c:func:`Py_SET_SIZE` function to set the size of an object.
diff --git a/Objects/object.c b/Objects/object.c
index ff6c497900cf2..81de3b8253040 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -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;
 }



More information about the Python-checkins mailing list