]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-128863: Deprecate _PyLong_FromDigits() function (#127939)
authorSergey B Kirpichev <skirpichev@gmail.com>
Fri, 24 Jan 2025 12:17:10 +0000 (15:17 +0300)
committerGitHub <noreply@github.com>
Fri, 24 Jan 2025 12:17:10 +0000 (13:17 +0100)
Doc/deprecations/c-api-pending-removal-in-3.18.rst
Doc/whatsnew/3.14.rst
Include/cpython/longintrepr.h
Misc/NEWS.d/next/C_API/2025-01-15-11-42-07.gh-issue-128863.C9MkB_.rst
Objects/longobject.c

index 361e1a9abf22d7cff98bf43589d315c9164c077f..0689d8b4f9e959fe4b1193e71f2032c06371acad 100644 (file)
@@ -7,7 +7,8 @@ Pending removal in Python 3.18
   * :c:func:`!_PyDict_GetItemStringWithError`: use :c:func:`PyDict_GetItemStringRef`.
   * :c:func:`!_PyDict_Pop()`: :c:func:`PyDict_Pop`.
   * :c:func:`!_PyLong_Sign()`: use :c:func:`PyLong_GetSign`.
-  * :c:func:`!_PyLong_New`: use :c:func:`PyLongWriter_Create`.
+  * :c:func:`!_PyLong_FromDigits` and :c:func:`!_PyLong_New`:
+    use :c:func:`PyLongWriter_Create`.
   * :c:func:`!_PyThreadState_UncheckedGet`: use :c:func:`PyThreadState_GetUnchecked`.
   * :c:func:`!_PyUnicode_AsString`: use :c:func:`PyUnicode_AsUTF8`.
   * :c:func:`!_Py_HashPointer`: use :c:func:`Py_HashPointer`.
index f463ed415d6a20a5682a3a4b7bac81d40334494c..b3c53b78903c7f158a8ffc309aa7cfdc70d2232c 100644 (file)
@@ -1396,7 +1396,8 @@ Deprecated
   * :c:func:`!_PyDict_GetItemStringWithError`: use :c:func:`PyDict_GetItemStringRef`.
   * :c:func:`!_PyDict_Pop()`: use :c:func:`PyDict_Pop`.
   * :c:func:`!_PyLong_Sign()`: use :c:func:`PyLong_GetSign`.
-  * :c:func:`!_PyLong_New`: use :c:func:`PyLongWriter_Create`.
+  * :c:func:`!_PyLong_FromDigits` and :c:func:`!_PyLong_New`:
+    use :c:func:`PyLongWriter_Create`.
   * :c:func:`!_PyThreadState_UncheckedGet`: use :c:func:`PyThreadState_GetUnchecked`.
   * :c:func:`!_PyUnicode_AsString`: use :c:func:`PyUnicode_AsUTF8`.
   * :c:func:`!_Py_HashPointer`: use :c:func:`Py_HashPointer`.
index f0d3d0d2b37e4f7165bccd2de7cc9bd92046b0be..4dd82600d562ee337e409abd105b91cf0578c931 100644 (file)
@@ -105,7 +105,7 @@ Py_DEPRECATED(3.14) PyAPI_FUNC(PyLongObject*) _PyLong_New(Py_ssize_t);
 // Return a copy of src.
 PyAPI_FUNC(PyObject*) _PyLong_Copy(PyLongObject *src);
 
-PyAPI_FUNC(PyLongObject*) _PyLong_FromDigits(
+Py_DEPRECATED(3.14) PyAPI_FUNC(PyLongObject*) _PyLong_FromDigits(
     int negative,
     Py_ssize_t digit_count,
     digit *digits);
index 7e6a8484b8887c86f9db12b2979ec178b7b3fa47..b2e5664138fba2d1841725a5dd2ec7b0b5167344 100644 (file)
@@ -5,7 +5,8 @@ Python 3.18:
 * :c:func:`!_PyDict_GetItemStringWithError`: use :c:func:`PyDict_GetItemStringRef`.
 * :c:func:`!_PyDict_Pop()`: use :c:func:`PyDict_Pop`.
 * :c:func:`!_PyLong_Sign()`: use :c:func:`PyLong_GetSign`.
-* :c:func:`!_PyLong_New`: use :c:func:`PyLongWriter_Create`.
+* :c:func:`!_PyLong_FromDigits` and :c:func:`!_PyLong_New`:
+  use :c:func:`PyLongWriter_Create`.
 * :c:func:`!_PyThreadState_UncheckedGet`: use :c:func:`PyThreadState_GetUnchecked`.
 * :c:func:`!_PyUnicode_AsString`: use :c:func:`PyUnicode_AsUTF8`.
 * :c:func:`!_Py_HashPointer`: use :c:func:`Py_HashPointer`.
index b4e3a70adf2b5bc2a3b0f8158fc2c0b48983291f..905c4695f60d4f3980440d7ead9836d1a8899458 100644 (file)
@@ -205,7 +205,6 @@ _PyLong_FromDigits(int negative, Py_ssize_t digit_count, digit *digits)
     }
     PyLongObject *result = long_alloc(digit_count);
     if (result == NULL) {
-        PyErr_NoMemory();
         return NULL;
     }
     _PyLong_SetSignAndDigitCount(result, negative?-1:1, digit_count);
@@ -217,15 +216,29 @@ PyObject *
 _PyLong_Copy(PyLongObject *src)
 {
     assert(src != NULL);
+    int sign;
 
     if (_PyLong_IsCompact(src)) {
         stwodigits ival = medium_value(src);
         if (IS_SMALL_INT(ival)) {
             return get_small_int((sdigit)ival);
         }
+        sign = _PyLong_CompactSign(src);
+    }
+    else {
+        sign = _PyLong_NonCompactSign(src);
     }
+
     Py_ssize_t size = _PyLong_DigitCount(src);
-    return (PyObject *)_PyLong_FromDigits(_PyLong_IsNegative(src), size, src->long_value.ob_digit);
+    PyLongObject *result = long_alloc(size);
+
+    if (result == NULL) {
+        return NULL;
+    }
+    _PyLong_SetSignAndDigitCount(result, sign, size);
+    memcpy(result->long_value.ob_digit, src->long_value.ob_digit,
+           size * sizeof(digit));
+    return (PyObject *)result;
 }
 
 static PyObject *