8000 gh-106320: Remove private _PyLong_IsCompact() function · python/cpython@974d26b · GitHub
[go: up one dir, main page]

Skip to content

Commit 974d26b

Browse files
committed
gh-106320: Remove private _PyLong_IsCompact() function
Move the private _PyLong_IsCompact() and _PyLong_CompactValue() functions to the internal C API (pycore_long.h). Public PyUnstable_Long_IsCompact() and PyUnstable_Long_CompactValue() functions can be used instead. Remove "const" qualifier from PyUnstable_Long_IsCompact() and PyUnstable_Long_CompactValue() parameter type.
1 parent c1e2f3b commit 974d26b

File tree

5 files changed

+24
-37
lines changed

5 files changed

+24
-37
lines changed

Doc/c-api/long.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
332332
Returns ``NULL`` on error. Use :c:func:`PyErr_Occurred` to disambiguate.
333333
334334
335-
.. c:function:: int PyUnstable_Long_IsCompact(const PyLongObject* op)
335+
.. c:function:: int PyUnstable_Long_IsCompact(PyLongObject* op)
336336
337337
Return 1 if *op* is compact, 0 otherwise.
338338
@@ -347,7 +347,7 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
347347
Exactly what values are considered compact is an implementation detail
348348
and is subject to change.
349349
350-
.. c:function:: Py_ssize_t PyUnstable_Long_CompactValue(const PyLongObject* op)
350+
.. c:function:: Py_ssize_t PyUnstable_Long_CompactValue(PyLongObject* op)
351351
352352
If *op* is compact, as determined by :c:func:`PyUnstable_Long_IsCompact`,
353353
return its value.

Include/cpython/longintrepr.h

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -89,34 +89,6 @@ struct _longobject {
8989
_PyLongValue long_value;
9090
};
9191

92-
93-
/* Inline some internals for speed. These should be in pycore_long.h
94-
* if user code didn't need them inlined. */
95-
96-
#define _PyLong_SIGN_MASK 3
97-
#define _PyLong_NON_SIZE_BITS 3
98-
99-
100-
static inline int
101-
_PyLong_IsCompact(const PyLongObject* op) {
102-
assert(PyType_HasFeature((op)->ob_base.ob_type, Py_TPFLAGS_LONG_SUBCLASS));
103-
return op->long_value.lv_tag < (2 << _PyLong_NON_SIZE_BITS);
104-
}
105-
106-
#define PyUnstable_Long_IsCompact _PyLong_IsCompact
107-
108-
static inline Py_ssize_t
109-
_PyLong_CompactValue(const PyLongObject *op)
110-
{
111-
assert(PyType_HasFeature((op)->ob_base.ob_type, Py_TPFLAGS_LONG_SUBCLASS));
112-
assert(PyUnstable_Long_IsCompact(op));
113-
Py_ssize_t sign = 1 - (op->long_value.lv_tag & _PyLong_SIGN_MASK);
114-
return sign * (Py_ssize_t)op->long_value.ob_digit[0];
115-
}
116-
117-
#define PyUnstable_Long_CompactValue _PyLong_CompactValue
118-
119-
12092
#ifdef __cplusplus
12193
}
12294
#endif

Include/cpython/longobject.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ PyAPI_FUNC(int) _PyLong_Sign(PyObject *v);
1919
*/
2020
PyAPI_FUNC(size_t) _PyLong_NumBits(PyObject *v);
2121

22-
PyAPI_FUNC(int) PyUnstable_Long_IsCompact(const PyLongObject* op);
23-
PyAPI_FUNC(Py_ssize_t) PyUnstable_Long_CompactValue(const PyLongObject* op);
22+
PyAPI_FUNC(int) PyUnstable_Long_IsCompact(PyLongObject* op);
23+
PyAPI_FUNC(Py_ssize_t) PyUnstable_Long_CompactValue(PyLongObject* op);
2424

Include/internal/pycore_long.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@ PyAPI_FUNC(int) _PyLong_UnsignedLongLong_Converter(PyObject *, void *);
214214
// Export for '_testclinic' shared extension (Argument Clinic code)
215215
PyAPI_FUNC(int) _PyLong_Size_t_Converter(PyObject *, void *);
216216

217+
#define _PyLong_SIGN_MASK 3
218+
#define _PyLong_NON_SIZE_BITS 3
219+
217220
/* Long value tag bits:
218221
* 0-1: Sign bits value = (1-sign), ie. negative=2, positive=0, zero=1.
219222
* 2: Reserved for immortality bit
@@ -245,6 +248,21 @@ static_assert(NON_SIZE_BITS == _PyLong_NON_SIZE_BITS, "NON_SIZE_BITS does not ma
245248
* will be signed 63 (or fewer) bit values
246249
*/
247250

251+
static inline int
252+
_PyLong_IsCompact(const PyLongObject* op) {
253+
assert(PyType_HasFeature((op)->ob_base.ob_type, Py_TPFLAGS_LONG_SUBCLASS));
254+
return op->long_value.lv_tag < (2 << _PyLong_NON_SIZE_BITS);
255+
}
256+
257+
static inline Py_ssize_t
258+
_PyLong_CompactValue(const PyLongObject *op)
259+
{
260+
assert(PyType_HasFeature((op)->ob_base.ob_type, Py_TPFLAGS_LONG_SUBCLASS));
261+
assert(_PyLong_IsCompact(op));
262+
Py_ssize_t sign = 1 - (op->long_value.lv_tag & _PyLong_SIGN_MASK);
263+
return sign * (Py_ssize_t)op->long_value.ob_digit[0];
264+
}
265+
248266
/* Return 1 if the argument is compact int */
249267
static inline int
250268
_PyLong_IsNonNegativeCompact(const PyLongObject* op) {

Objects/longobject.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6369,16 +6369,13 @@ _PyLong_FiniTypes(PyInterpreterState *interp)
63696369
_PyStructSequence_FiniBuiltin(interp, &Int_InfoType);
63706370
}
63716371

6372-
#undef PyUnstable_Long_IsCompact
63736372

63746373
int
6375-
PyUnstable_Long_IsCompact(const PyLongObject* op) {
6374+
PyUnstable_Long_IsCompact(PyLongObject* op) {
63766375
return _PyLong_IsCompact(op);
63776376
}
63786377

6379-
#undef PyUnstable_Long_CompactValue
6380-
63816378
Py_ssize_t
6382-
PyUnstable_Long_CompactValue(const PyLongObject* op) {
6379+
PyUnstable_Long_CompactValue(PyLongObject* op) {
63836380
return _PyLong_CompactValue(op);
63846381
}

0 commit comments

Comments
 (0)
0