Skip to content

Commit b176f43

Browse files
committed
Replace deprecated _PyLong_new with PyLongWriter API
1 parent 1eb9d4c commit b176f43

File tree

2 files changed

+8
-27
lines changed

2 files changed

+8
-27
lines changed

mypyc/lib-rt/int_ops.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,6 @@ PyObject *CPyBool_Str(bool b) {
232232
return PyObject_Str(b ? Py_True : Py_False);
233233
}
234234

235-
static void CPyLong_NormalizeUnsigned(PyLongObject *v) {
236-
Py_ssize_t i = CPY_LONG_SIZE_UNSIGNED(v);
237-
while (i > 0 && CPY_LONG_DIGIT(v, i - 1) == 0)
238-
i--;
239-
CPyLong_SetUnsignedSize(v, i);
240-
}
241-
242235
// Bitwise op '&', '|' or '^' using the generic (slow) API
243236
static CPyTagged GenericBitwiseOp(CPyTagged a, CPyTagged b, char op) {
244237
PyObject *aobj = CPyTagged_AsObject(a);
@@ -302,7 +295,6 @@ CPyTagged CPyTagged_BitwiseLongOp_(CPyTagged a, CPyTagged b, char op) {
302295
digit *adigits = GetIntDigits(a, &asize, abuf);
303296
digit *bdigits = GetIntDigits(b, &bsize, bbuf);
304297

305-
PyLongObject *r;
306298
if (unlikely(asize < 0 || bsize < 0)) {
307299
// Negative operand. This is slower, but bitwise ops on them are pretty rare.
308300
return GenericBitwiseOp(a, b, op);
@@ -317,31 +309,31 @@ CPyTagged CPyTagged_BitwiseLongOp_(CPyTagged a, CPyTagged b, char op) {
317309
asize = bsize;
318310
bsize = tmp_size;
319311
}
320-
r = _PyLong_New(op == '&' ? asize : bsize);
321-
if (unlikely(r == NULL)) {
312+
void *digits = NULL;
313+
PyLongWriter *writer = PyLongWriter_Create(0, op == '&' ? asize : bsize, &digits);
314+
if (unlikely(writer == NULL)) {
322315
CPyError_OutOfMemory();
323316
}
324317
Py_ssize_t i;
325318
if (op == '&') {
326319
for (i = 0; i < asize; i++) {
327-
CPY_LONG_DIGIT(r, i) = adigits[i] & bdigits[i];
320+
((digit *)digits)[i] = adigits[i] & bdigits[i];
328321
}
329322
} else {
330323
if (op == '|') {
331324
for (i = 0; i < asize; i++) {
332-
CPY_LONG_DIGIT(r, i) = adigits[i] | bdigits[i];
325+
((digit *)digits)[i] = adigits[i] | bdigits[i];
333326
}
334327
} else {
335328
for (i = 0; i < asize; i++) {
336-
CPY_LONG_DIGIT(r, i) = adigits[i] ^ bdigits[i];
329+
((digit *)digits)[i] = adigits[i] ^ bdigits[i];
337330
}
338331
}
339332
for (; i < bsize; i++) {
340-
CPY_LONG_DIGIT(r, i) = bdigits[i];
333+
((digit *)digits)[i] = bdigits[i];
341334
}
342335
}
343-
CPyLong_NormalizeUnsigned(r);
344-
return CPyTagged_StealFromObject((PyObject *)r);
336+
return CPyTagged_StealFromObject(PyLongWriter_Finish(writer));
345337
}
346338

347339
// Bitwise '~' slow path

mypyc/lib-rt/mypyc_util.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,24 +124,13 @@ static inline CPyTagged CPyTagged_ShortFromSsize_t(Py_ssize_t x) {
124124
// Number of digits, assuming int is non-negative
125125
#define CPY_LONG_SIZE_UNSIGNED(o) CPY_LONG_SIZE(o)
126126

127-
static inline void CPyLong_SetUnsignedSize(PyLongObject *o, Py_ssize_t n) {
128-
if (n == 0)
129-
o->long_value.lv_tag = CPY_SIGN_ZERO;
130-
else
131-
o->long_value.lv_tag = n << CPY_NON_SIZE_BITS;
132-
}
133-
134127
#else
135128

136129
#define CPY_LONG_DIGIT(o, n) ((o)->ob_digit[n])
137130
#define CPY_LONG_IS_NEGATIVE(o) (((o)->ob_base.ob_size < 0)
138131
#define CPY_LONG_SIZE_SIGNED(o) ((o)->ob_base.ob_size)
139132
#define CPY_LONG_SIZE_UNSIGNED(o) ((o)->ob_base.ob_size)
140133

141-
static inline void CPyLong_SetUnsignedSize(PyLongObject *o, Py_ssize_t n) {
142-
o->ob_base.ob_size = n;
143-
}
144-
145134
#endif
146135

147136
// Are we targeting Python 3.13 or newer?

0 commit comments

Comments
 (0)