From e3ed89b53ab59120ec86c5cdc951ce9e4325d9b5 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 27 Jan 2025 11:27:34 +0100 Subject: [PATCH] gh-102471: Change PyLongWriter_Discard(NULL) to do nothing It's convenient to be able to call PyLongWriter_Discard(NULL) in error handling code. --- Doc/c-api/long.rst | 2 +- Objects/longobject.c | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Doc/c-api/long.rst b/Doc/c-api/long.rst index 084ba513493ffe..25d9e62e387279 100644 --- a/Doc/c-api/long.rst +++ b/Doc/c-api/long.rst @@ -824,6 +824,6 @@ The :c:type:`PyLongWriter` API can be used to import an integer. Discard a :c:type:`PyLongWriter` created by :c:func:`PyLongWriter_Create`. - *writer* must not be ``NULL``. + If *writer* is ``NULL``, no operation is performed. The writer instance and the *digits* array are invalid after the call. diff --git a/Objects/longobject.c b/Objects/longobject.c index 905c4695f60d4f..43be1ab056e0fe 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -6953,6 +6953,10 @@ PyLongWriter_Create(int negative, Py_ssize_t ndigits, void **digits) void PyLongWriter_Discard(PyLongWriter *writer) { + if (writer == NULL) { + return; + } + PyLongObject *obj = (PyLongObject *)writer; assert(Py_REFCNT(obj) == 1); Py_DECREF(obj);