Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ build:
pre_create_environment:
- |
PREFIX=$(pwd)/.local
ZZ_VERSION=0.8.0
ZZ_VERSION=0.9.0a1
ZZ_DIR=zz-${ZZ_VERSION}
GITHUB_URL=https://github.com/diofant/zz/releases/download/
ZZ_URL=${GITHUB_URL}v${ZZ_VERSION}/${ZZ_DIR}.tar.gz
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Python-GMP
==========

Python extension module, providing bindings to the GNU GMP via the `ZZ library
<https://github.com/diofant/zz>`_ (version 0.8.0 or later required). This
<https://github.com/diofant/zz>`_ (version 0.9.0 or later required). This
module shouldn't crash the interpreter.

The gmp can be used as a `gmpy2`_/`python-flint`_ replacement to provide
Expand Down
2 changes: 2 additions & 0 deletions bench/collatz.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from gmpy2 import mpz
elif os.getenv("T") == "flint.fmpz":
from flint import fmpz as mpz
elif os.getenv("T") == "int":
mpz = int
else:
from gmp import mpz

Expand Down
2 changes: 2 additions & 0 deletions bench/mul.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from gmpy2 import mpz
elif os.getenv("T") == "flint.fmpz":
from flint import fmpz as mpz
elif os.getenv("T") == "int":
mpz = int
else:
from gmp import mpz

Expand Down
56 changes: 1 addition & 55 deletions gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,61 +140,12 @@ MPZ_from_str(PyObject *obj, int base)
if (!str) {
return NULL; /* LCOV_EXCL_LINE */
}
if (base < 0) {
goto bad_base;
}

MPZ_Object *res = MPZ_new();

if (!res) {
return (MPZ_Object *)PyErr_NoMemory(); /* LCOV_EXCL_LINE */
}
while (isspace(*str)) {
str++;
}

bool cast_negative = (str[0] == '-');

str += cast_negative;
if (str[0] == '+') {
str++;
}
if (str[0] == '0') {
if (base == 0) {
if (tolower(str[1]) == 'b') {
base = 2;
}
else if (tolower(str[1]) == 'o') {
base = 8;
}
else if (tolower(str[1]) == 'x') {
base = 16;
}
else if (!isspace(str[1]) && str[1] != '\0') {
goto err;
}
}
if ((tolower(str[1]) == 'b' && base == 2)
|| (tolower(str[1]) == 'o' && base == 8)
|| (tolower(str[1]) == 'x' && base == 16))
{
str += 2;
if (str[0] == '_') {
str++;
}
}
else {
goto skip_negation;
}
}
else {
skip_negation:
str -= cast_negative;
cast_negative = false;
}
if (base == 0) {
base = 10;
}

zz_err ret = zz_set_str(str, base, &res->z);

Expand All @@ -206,22 +157,17 @@ MPZ_from_str(PyObject *obj, int base)
}
else if (ret == ZZ_VAL) {
Py_DECREF(res);
if (2 <= base && base <= 36) {
err:
if (!base || (2 <= base && base <= 36)) {
PyErr_Format(PyExc_ValueError,
"invalid literal for mpz() with base %d: %.200R",
base, obj);
}
else {
bad_base:
PyErr_SetString(PyExc_ValueError,
"mpz base must be >= 2 and <= 36, or 0");
}
return NULL;
}
if (cast_negative) {
(void)zz_neg(&res->z, &res->z);
}
return res;
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/cibw_before_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ make --silent all install

cd ..

ZZ_VERSION=0.8.0
ZZ_VERSION=0.9.0a1
ZZ_DIR=zz-${ZZ_VERSION}
ZZ_URL=https://github.com/diofant/zz/releases/download/v${ZZ_VERSION}/${ZZ_DIR}.tar.gz

Expand Down
9 changes: 4 additions & 5 deletions tests/test_mpz.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,17 +725,16 @@ def test_power_errors():
pow(mpz(1<<64), (1<<31) - 1)
with pytest.raises(MemoryError):
pow(mpz(1<<64), (1<<31))
with pytest.raises(OverflowError):
mpz(1) << ((1 << 63) - 1)
else:
with pytest.raises(MemoryError):
pow(mpz(1<<64), (1<<63) - 1)
if platform.system() != "Darwin":
with pytest.raises(MemoryError):
pow(mpz(1<<64), (1<<63))
with pytest.raises(OverflowError):
mpz(1) << ((1 << 64) - 1)
if platform.system() != "Darwin":
with pytest.raises(MemoryError):
mpz(1) << ((1 << 63) - 1)
with pytest.raises(OverflowError):
mpz(1) << ((1 << 64) - 1)


@given(bigints(), integers(max_value=12345))
Expand Down