Skip to content

Commit f33d7d2

Browse files
authored
Merge pull request #331 from skirpichev/misc
Misc fixes
2 parents a125412 + c619768 commit f33d7d2

9 files changed

Lines changed: 93 additions & 79 deletions

File tree

.github/workflows/wheels.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ jobs:
66
runs-on: ${{ matrix.os }}
77
strategy:
88
matrix:
9-
os: [ubuntu-24.04, ubuntu-24.04-arm, macos-15-intel, macos-15,
10-
windows-2022]
9+
os: [ubuntu-24.04, ubuntu-24.04-arm, macos-26-intel, macos-26,
10+
windows-2025]
1111
msystem: [ucrt64]
1212
menv: [ucrt-x86_64]
1313
include:
@@ -18,7 +18,7 @@ jobs:
1818
- uses: actions/checkout@v6
1919
with:
2020
fetch-depth: 0
21-
- uses: msys2/setup-msys2@v2.30.0
21+
- uses: msys2/setup-msys2@v2.31.0
2222
name: Setup msys2
2323
with:
2424
install: >-

bench/README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ It's possible to run them also with gmpy2's and flint's integer types:
55
.. code:: sh
66
77
( export T="gmpy2.mpz"; \
8+
export PYTHONPATH=. ; \
89
python bench/mul.py -q --copy-env --rigorous -o $T.json )
910
1011
Beware, that the gmp prefers clang over gcc and extensions might

bench/collatz.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
1-
# collatz.py
2-
3-
import os
1+
# bench/collatz.py
42

53
import pyperf
64

7-
if os.getenv("T") == "gmpy2.mpz":
8-
from gmpy2 import mpz
9-
elif os.getenv("T") == "flint.fmpz":
10-
from flint import fmpz as mpz
11-
elif os.getenv("T") == "int":
12-
mpz = int
13-
else:
14-
from gmp import mpz
5+
from bench.utils import mpz
156

167
zero = mpz(0)
178
one = mpz(1)

bench/harmonic.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# bench/harmonic.py
2+
3+
from fractions import Fraction
4+
5+
import pyperf
6+
7+
from bench.utils import mpz, mysum
8+
9+
runner = pyperf.Runner()
10+
for n in [100, 1000]:
11+
xs = [Fraction(mpz(1), mpz(i)) for i in range(1, n + 1)]
12+
runner.bench_func(f"H({n})", mysum, xs)

bench/mul.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
1-
# mul.py
1+
# bench/mul.py
22

3-
import os
43
from operator import mul
54

65
import pyperf
76

8-
if os.getenv("T") == "gmpy2.mpz":
9-
from gmpy2 import mpz
10-
elif os.getenv("T") == "flint.fmpz":
11-
from flint import fmpz as mpz
12-
elif os.getenv("T") == "int":
13-
mpz = int
14-
else:
15-
from gmp import mpz
7+
from bench.utils import mpz
168

179
values = ["1<<7", "1<<38", "1<<300", "1<<3000"]
1810

bench/sum.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# bench/sum.py
2+
3+
import pyperf
4+
5+
from bench.utils import mpz, mysum
6+
7+
runner = pyperf.Runner()
8+
for n in [100, 1000]:
9+
xs = [mpz(i) for i in range(1, n + 1)]
10+
runner.bench_func(f"sum({n})", mysum, xs)

bench/utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import os
2+
3+
if os.getenv("T") == "gmpy2.mpz":
4+
from gmpy2 import mpz
5+
elif os.getenv("T") == "flint.fmpz":
6+
from flint import fmpz as mpz
7+
elif os.getenv("T") == "int":
8+
mpz = int
9+
else:
10+
pass
11+
12+
13+
def mysum(xs):
14+
total = xs[0]
15+
for t in xs[1:]:
16+
total += t
17+
return t

gmp.c

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -729,22 +729,21 @@ str(PyObject *self)
729729

730730
#define Number_Check(op) (PyFloat_Check((op)) || PyComplex_Check((op)))
731731

732-
#define CHECK_OP(u, a) \
733-
if (MPZ_Check(a)) { \
734-
u = (MPZ_Object *)a; \
735-
Py_INCREF(a); \
736-
} \
737-
else if (PyLong_Check(a)) { \
738-
u = MPZ_from_int(a); \
739-
if (!u) { \
740-
goto end; \
741-
} \
742-
} \
743-
else if (Number_Check(a)) { \
744-
goto numbers; \
745-
} \
746-
else { \
747-
goto fallback; \
732+
#define CHECK_OP(u, a) \
733+
if (MPZ_Check(a)) { \
734+
u = (MPZ_Object *)Py_NewRef(a); \
735+
} \
736+
else if (PyLong_Check(a)) { \
737+
u = MPZ_from_int(a); \
738+
if (!u) { \
739+
goto end; \
740+
} \
741+
} \
742+
else if (Number_Check(a)) { \
743+
goto numbers; \
744+
} \
745+
else { \
746+
goto fallback; \
748747
}
749748

750749
PyObject *
@@ -916,19 +915,18 @@ to_bool(PyObject *self)
916915
return !zz_iszero(&((MPZ_Object *)self)->z);
917916
}
918917

919-
#define CHECK_OPv2(u, a) \
920-
if (MPZ_Check(a)) { \
921-
u = (MPZ_Object *)a; \
922-
Py_INCREF(a); \
923-
} \
924-
else if (PyLong_Check(a)) { \
925-
; \
926-
} \
927-
else if (Number_Check(a)) { \
928-
goto numbers; \
929-
} \
930-
else { \
931-
goto fallback; \
918+
#define CHECK_OPv2(u, a) \
919+
if (MPZ_Check(a)) { \
920+
u = (MPZ_Object *)Py_NewRef(a); \
921+
} \
922+
else if (PyLong_Check(a)) { \
923+
; \
924+
} \
925+
else if (Number_Check(a)) { \
926+
goto numbers; \
927+
} \
928+
else { \
929+
goto fallback; \
932930
}
933931

934932
#define BINOP(suff, slot) \
@@ -1004,8 +1002,7 @@ done: \
10041002
PyObject *uf, *vf, *rf; \
10051003
\
10061004
if (Number_Check(self)) { \
1007-
uf = self; \
1008-
Py_INCREF(uf); \
1005+
uf = Py_NewRef(self); \
10091006
} \
10101007
else { \
10111008
uf = to_float(self); \
@@ -1014,8 +1011,7 @@ done: \
10141011
} \
10151012
} \
10161013
if (Number_Check(other)) { \
1017-
vf = other; \
1018-
Py_INCREF(vf); \
1014+
vf = Py_NewRef(other); \
10191015
} \
10201016
else { \
10211017
vf = to_float(other); \
@@ -1309,8 +1305,7 @@ nb_truediv(PyObject *self, PyObject *other)
13091305
PyObject *uf, *vf;
13101306

13111307
if (Number_Check(self)) {
1312-
uf = self;
1313-
Py_INCREF(uf);
1308+
uf = Py_NewRef(self);
13141309
}
13151310
else {
13161311
uf = to_float(self);
@@ -1319,8 +1314,7 @@ nb_truediv(PyObject *self, PyObject *other)
13191314
}
13201315
}
13211316
if (Number_Check(other)) {
1322-
vf = other;
1323-
Py_INCREF(vf);
1317+
vf = Py_NewRef(other);
13241318
}
13251319
else {
13261320
vf = to_float(other);
@@ -1337,17 +1331,16 @@ nb_truediv(PyObject *self, PyObject *other)
13371331
return res;
13381332
}
13391333

1340-
#define CHECK_OP_INT(u, a) \
1341-
if (MPZ_Check(a)) { \
1342-
u = (MPZ_Object *)a; \
1343-
Py_INCREF(a); \
1344-
} \
1345-
else { \
1346-
u = MPZ_from_int(a); \
1347-
if (!u) { \
1348-
goto end; \
1349-
} \
1350-
} \
1334+
#define CHECK_OP_INT(u, a) \
1335+
if (MPZ_Check(a)) { \
1336+
u = (MPZ_Object *)Py_NewRef(a); \
1337+
} \
1338+
else { \
1339+
u = MPZ_from_int(a); \
1340+
if (!u) { \
1341+
goto end; \
1342+
} \
1343+
} \
13511344

13521345
#define BINOP_INT(suff) \
13531346
static PyObject * \
@@ -1527,8 +1520,7 @@ power(PyObject *self, PyObject *other, PyObject *module)
15271520
PyObject *uf, *vf;
15281521

15291522
if (Number_Check(self)) {
1530-
uf = self;
1531-
Py_INCREF(uf);
1523+
uf = Py_NewRef(self);
15321524
}
15331525
else {
15341526
uf = to_float(self);
@@ -1537,8 +1529,7 @@ power(PyObject *self, PyObject *other, PyObject *module)
15371529
}
15381530
}
15391531
if (Number_Check(other)) {
1540-
vf = other;
1541-
Py_INCREF(vf);
1532+
vf = Py_NewRef(other);
15421533
}
15431534
else {
15441535
vf = to_float(other);

tests/conftest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ def pytest_report_header(config):
2020
print(f"""
2121
Using the ZZ library v{gmp._zz_version}
2222
23-
Bits per digit : {gmp.mpz_info.bits_per_digit}
24-
sizeof(zz_digit_t): {gmp.mpz_info.sizeof_digit}
25-
Maximal bit count : {gmp.mpz_info.bitcnt_max}
23+
Bits per digit : {gmp.mpz_info.bits_per_digit}
24+
sizeof(digit) : {gmp.mpz_info.sizeof_digit}
25+
Maximal bit count: {gmp.mpz_info.bitcnt_max}
2626
2727
The gmp module v{gmp.__version__}
2828
""")

0 commit comments

Comments
 (0)