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

Skip to content

Commit c6be6f8

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 3edcf74 commit c6be6f8

File tree

5 files changed

+27
-51
lines changed

5 files changed

+27
-51
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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@
44

55
PyAPI_FUNC(PyObject*) PyLong_FromUnicodeObject(PyObject *u, int base);
66

7-
PyAPI_FUNC(int) PyUnstable_Long_IsCompact(const PyLongObject* op);
8-
PyAPI_FUNC(Py_ssize_t) PyUnstable_Long_CompactValue(const PyLongObject* op);
9-
7+
PyAPI_FUNC(int) PyUnstable_Long_IsCompact(PyLongObject* op);
8+
PyAPI_FUNC(Py_ssize_t) PyUnstable_Long_CompactValue(PyLongObject* op);

Include/internal/pycore_long.h

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -231,37 +231,45 @@ PyAPI_FUNC(int) _PyLong_UnsignedLongLong_Converter(PyObject *, void *);
231231
// Export for '_testclinic' shared extension (Argument Clinic code)
232232
PyAPI_FUNC(int) _PyLong_Size_t_Converter(PyObject *, void *);
233233

234+
#define _PyLong_SIGN_MASK 3
235+
#define _PyLong_NON_SIZE_BITS 3
236+
234237
/* Long value tag bits:
235238
* 0-1: Sign bits value = (1-sign), ie. negative=2, positive=0, zero=1.
236239
* 2: Reserved for immortality bit
237240
* 3+ Unsigned digit count
238241
*/
239-
#define SIGN_MASK 3
242+
#define SIGN_MASK _PyLong_SIGN_MASK
240243
#define SIGN_ZERO 1
241244
#define SIGN_NEGATIVE 2
242-
#define NON_SIZE_BITS 3
245+
#define NON_SIZE_BITS _PyLong_NON_SIZE_BITS
243246

244-
/* The functions _PyLong_IsCompact and _PyLong_CompactValue are defined
245-
* in Include/cpython/longobject.h, since they need to be inline.
246-
*
247-
* "Compact" values have at least one bit to spare,
247+
/* "Compact" values have at least one bit to spare,
248248
* so that addition and subtraction can be performed on the values
249249
* without risk of overflow.
250-
*
251-
* The inline functions need tag bits.
252-
* For readability, rather than do `#define SIGN_MASK _PyLong_SIGN_MASK`
253-
* we define them to the numbers in both places and then assert that
254-
* they're the same.
255250
*/
256-
static_assert(SIGN_MASK == _PyLong_SIGN_MASK, "SIGN_MASK does not match _PyLong_SIGN_MASK");
257-
static_assert(NON_SIZE_BITS == _PyLong_NON_SIZE_BITS, "NON_SIZE_BITS does not match _PyLong_NON_SIZE_BITS");
258251

259252
/* All *compact" values are guaranteed to fit into
260253
* a Py_ssize_t with at least one bit to spare.
261254
* In other words, for 64 bit machines, compact
262255
* will be signed 63 (or fewer) bit values
263256
*/
264257

258+
static inline int
259+
_PyLong_IsCompact(const PyLongObject* op) {
260+
assert(PyType_HasFeature((op)->ob_base.ob_type, Py_TPFLAGS_LONG_SUBCLASS));
261+
return op->long_value.lv_tag < (2 << _PyLong_NON_SIZE_BITS);
262+
}
263+
264+
static inline Py_ssize_t
265+
_PyLong_CompactValue(const PyLongObject *op)
266+
{
267+
assert(PyType_HasFeature((op)->ob_base.ob_type, Py_TPFLAGS_LONG_SUBCLASS));
268+
assert(_PyLong_IsCompact(op));
269+
Py_ssize_t sign = 1 - (op->long_value.lv_tag & _PyLong_SIGN_MASK);
270+
return sign * (Py_ssize_t)op->long_value.ob_digit[0];
271+
}
272+
265273
/* Return 1 if the argument is compact int */
266274
static inline int
267275
_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