]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-128863: Deprecate _PyLong_Sign() function (#129176)
authorVictor Stinner <vstinner@python.org>
Thu, 23 Jan 2025 02:11:53 +0000 (03:11 +0100)
committerGitHub <noreply@github.com>
Thu, 23 Jan 2025 02:11:53 +0000 (03:11 +0100)
Replace _PyLong_Sign() with PyLong_GetSign().

Doc/deprecations/c-api-pending-removal-in-3.18.rst
Doc/whatsnew/3.14.rst
Include/cpython/longobject.h
Misc/NEWS.d/next/C_API/2025-01-15-11-42-07.gh-issue-128863.C9MkB_.rst
Modules/_pickle.c
Modules/_testinternalcapi.c
Objects/floatobject.c
Objects/longobject.c
Objects/sliceobject.c

index d04c746cd9f33e3e341f1a4b6298a626b9ca84f4..9da3d73c9e12528767ec2e080e80ad756fa5250e 100644 (file)
@@ -6,6 +6,7 @@ Pending removal in Python 3.18
   * :c:func:`!_PyBytes_Join`: use :c:func:`PyBytes_Join`.
   * :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:`!_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 531c5ed6226fe43c347a98139aa397cece220d20..421375ebaac1848404e2457d156f8add01aedf5b 100644 (file)
@@ -1395,6 +1395,7 @@ Deprecated
   * :c:func:`!_PyBytes_Join`: use :c:func:`PyBytes_Join`.
   * :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:`!_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 4d6e618f831ad8aa849cf0101dc249443180e837..7f28ad60b7467b3a9d525800a6adaa8fca051669 100644 (file)
@@ -86,7 +86,7 @@ PyAPI_FUNC(int) PyLong_IsZero(PyObject *obj);
    - On failure, set an exception, and return -1. */
 PyAPI_FUNC(int) PyLong_GetSign(PyObject *v, int *sign);
 
-PyAPI_FUNC(int) _PyLong_Sign(PyObject *v);
+Py_DEPRECATED(3.14) PyAPI_FUNC(int) _PyLong_Sign(PyObject *v);
 
 /* _PyLong_NumBits.  Return the number of bits needed to represent the
    absolute value of a long.  For example, this returns 1 for 1 and -1, 2
index a94d7933ad32efa08c6ba4972878c7444fcb79c3..5e55cfb3c74a4bb7ad11f4c72f33bbc9eec4c01c 100644 (file)
@@ -4,6 +4,7 @@ Python 3.18:
 * :c:func:`!_PyBytes_Join`: use :c:func:`PyBytes_Join`.
 * :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:`!_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 599b5f92c2a1f7d8b5efc7ece4e2ef283277405d..a6cfb2deeb23c196983ad57d70bc1d36023042d8 100644 (file)
@@ -2159,8 +2159,10 @@ save_long(PicklerObject *self, PyObject *obj)
         unsigned char *pdata;
         char header[5];
         int i;
-        int sign = _PyLong_Sign(obj);
 
+        int sign;
+        assert(PyLong_Check(obj));
+        (void)PyLong_GetSign(obj, &sign);
         if (sign == 0) {
             header[0] = LONG1;
             header[1] = 0;      /* It's 0 -- an empty bytestring. */
index e77df5b57b05045d943c08a6b22fe09f81e9fde5..f3d234a7f9595ef44020a2297b89d2725cfda65a 100644 (file)
@@ -25,7 +25,6 @@
 #include "pycore_hashtable.h"     // _Py_hashtable_new()
 #include "pycore_initconfig.h"    // _Py_GetConfigsAsDict()
 #include "pycore_instruction_sequence.h"  // _PyInstructionSequence_New()
-#include "pycore_long.h"          // _PyLong_Sign()
 #include "pycore_object.h"        // _PyObject_IsFreed()
 #include "pycore_optimizer.h"     // JitOptSymbol, etc.
 #include "pycore_pathconfig.h"    // _PyPathConfig_ClearGlobal()
@@ -1798,14 +1797,14 @@ _testinternalcapi_test_long_numbits_impl(PyObject *module)
 
     for (i = 0; i < Py_ARRAY_LENGTH(testcases); ++i) {
         uint64_t nbits;
-        int sign;
+        int sign = -7;
         PyObject *plong;
 
         plong = PyLong_FromLong(testcases[i].input);
         if (plong == NULL)
             return NULL;
         nbits = _PyLong_NumBits(plong);
-        sign = _PyLong_Sign(plong);
+        (void)PyLong_GetSign(plong, &sign);
 
         Py_DECREF(plong);
         if (nbits != testcases[i].nbits)
@@ -1813,7 +1812,7 @@ _testinternalcapi_test_long_numbits_impl(PyObject *module)
                             "wrong result for _PyLong_NumBits");
         if (sign != testcases[i].sign)
             return raiseTestError("test_long_numbits",
-                            "wrong result for _PyLong_Sign");
+                            "wrong result for PyLong_GetSign()");
     }
     Py_RETURN_NONE;
 }
index bcc77287454768e4a5b35930b3f18061777ac9c1..7ca43033d722abf67f86d70a018c8e67160a0e60 100644 (file)
@@ -428,9 +428,10 @@ float_richcompare(PyObject *v, PyObject *w, int op)
 
     else if (PyLong_Check(w)) {
         int vsign = i == 0.0 ? 0 : i < 0.0 ? -1 : 1;
-        int wsign = _PyLong_Sign(w);
+        int wsign;
         int exponent;
 
+        (void)PyLong_GetSign(w, &wsign);
         if (vsign != wsign) {
             /* Magnitudes are irrelevant -- the signs alone
              * determine the outcome.
index d449a01cedf8863476c8ac7562ef3c3245c06d68..370931e64d36277cf6c6465ea2f4ab07a6f314bd 100644 (file)
@@ -827,19 +827,25 @@ PyLong_IsZero(PyObject *obj)
     return _PyLong_IsZero((PyLongObject *)obj);
 }
 
-int
-_PyLong_Sign(PyObject *vv)
+static int
+long_sign(PyObject *vv)
 {
+    assert(vv != NULL);
+    assert(PyLong_Check(vv));
     PyLongObject *v = (PyLongObject *)vv;
 
-    assert(v != NULL);
-    assert(PyLong_Check(v));
     if (_PyLong_IsCompact(v)) {
         return _PyLong_CompactSign(v);
     }
     return _PyLong_NonCompactSign(v);
 }
 
+int
+_PyLong_Sign(PyObject *vv)
+{
+    return long_sign(vv);
+}
+
 int
 PyLong_GetSign(PyObject *vv, int *sign)
 {
@@ -848,7 +854,7 @@ PyLong_GetSign(PyObject *vv, int *sign)
         return -1;
     }
 
-    *sign = _PyLong_Sign(vv);
+    *sign = long_sign(vv);
     return 0;
 }
 
index 4fef0af93fe09595cf6d905bb6378182db9dbe91..1b07c2a2c498b93ce176b9afd525b3aa5e5daf1d 100644 (file)
@@ -399,11 +399,14 @@ _PySlice_GetLongIndices(PySliceObject *self, PyObject *length,
         step_is_negative = 0;
     }
     else {
-        int step_sign;
         step = evaluate_slice_index(self->step);
-        if (step == NULL)
+        if (step == NULL) {
             goto error;
-        step_sign = _PyLong_Sign(step);
+        }
+        assert(PyLong_Check(step));
+
+        int step_sign;
+        (void)PyLong_GetSign(step, &step_sign);
         if (step_sign == 0) {
             PyErr_SetString(PyExc_ValueError,
                             "slice step cannot be zero");