python-gmp

Safe bindings to the GNU GMP library

50 个版本 Python >=3.11
安装
pip install python-gmp
poetry add python-gmp
pipenv install python-gmp
conda install python-gmp
描述

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 module shouldn't crash the interpreter.

The gmp can be used as a gmpy2/python-flint replacement to provide integer type (mpz), compatible with Python's int. It also includes functions, compatible with the Python stdlib's submodule math.integer <https://docs.python.org/3.15/library/math.integer.html>_.

This module requires Python 3.11 or later versions and has been tested with CPython 3.11 through 3.15, with PyPy3.11 7.3.23 and with GraalPy 25.0.3. Free-threading builds of the CPython are supported.

Releases are available in the Python Package Index (PyPI) at https://pypi.org/project/python-gmp/.

Motivation

The CPython (and most other Python implementations, like PyPy) is optimized to work with small (machine-sized) integers. Algorithms used here for big integers usually aren't best known in the field. Fortunately, it's possible to use bindings (for example, the gmpy2_ package) to the GNU GMP, which aims to be faster than any other bignum library for all operand sizes.

But such extension modules usually rely on default GMP's memory management and can't recover from allocation failure. So, it's easy to crash the Python interpreter during the interactive session. Following example with the gmpy2 will work if you set address space limit for the Python interpreter (e.g. by prlimit command on Linux):

.. code:: pycon

import gmpy2 gmpy2.version '2.2.1' z = gmpy2.mpz(29925959575501) while True: # this loop will crash interpter ... z = z*z ... GNU MP: Cannot allocate memory (size=46956584) Aborted

The gmp module handles such errors correctly:

.. code:: pycon

import gmp z = gmp.mpz(29925959575501) while True: ... z = zz ... Traceback (most recent call last): File "", line 2, in z = zz ~^~ MemoryError

interpreter still works, all variables in

the current scope are available,

z.bit_length() # including pre-failure value of z 93882077

Warning on --disable-alloca configure option

You should use the GNU GMP library, compiled with the '--disable-alloca' configure option to prevent using alloca() for temporary workspace allocation, or this module may crash the interpreter in case of a stack overflow.

.. _gmpy2: https://pypi.org/project/gmpy2/ .. _python-flint: https://pypi.org/project/python-flint/ .. _mpz: https://python-gmp.readthedocs.io/en/latest/#gmp.mpz .. _int: https://docs.python.org/3/library/functions.html#int

版本列表
0.6.0a2 2026-06-20
0.6.0a1 2026-02-20
0.6.0a0 2026-02-08
0.5.3 2026-06-20
0.5.2 2026-06-09
0.5.1 2026-03-20
0.5.0 2026-02-08
0.5.0b2 2026-02-04
0.5.0b1 2025-11-18
0.5.0a6 2025-11-07
0.5.0a5 2025-11-02
0.5.0a4 2025-10-07
0.5.0a3 2025-10-03
0.5.0a2 2025-08-30
0.5.0a1 2025-08-29
0.4.1 2026-01-14
0.4.0 2025-08-20
0.4.0b5 2025-08-17
0.4.0b4 2025-08-17
0.4.0b3 2025-08-06
0.4.0b2 2025-07-23
0.4.0b1 2025-07-21
0.4.0a5 2025-07-04
0.4.0a4 2025-06-08
0.4.0a3 2025-06-08
0.4.0a2 2025-03-09
0.4.0a1 2025-02-26
0.3.1 2025-03-10
0.3.0 2025-02-23
0.3.0b1 2025-02-22
0.3.0a8 2025-02-18
0.3.0a7 2025-02-17
0.3.0a6 2025-02-13
0.3.0a5 2025-01-30
0.3.0a4 2025-01-29
0.3.0a3 2025-01-28
0.3.0a2 2025-01-05
0.3.0a1 2025-01-04
0.2.2 2025-01-21
0.2.1 2025-01-02
0.2.0 2025-01-01
0.2.0b2 2024-12-31
0.2.0b1 2024-12-28
0.2.0a4 2024-12-22
0.2.0a3 2024-12-18
0.2.0a2 2024-12-15
0.2.0a1 2024-12-11
0.1.2 2024-12-08
0.1.1 2024-12-08
0.1.0 2024-12-02