Metadata-Version: 2.4
Name: python-gmp
Version: 0.4.0a5
Summary: Safe bindings to the GNU GMP library
Author-email: Sergey B Kirpichev <skirpichev@gmail.com>
Maintainer-email: Sergey B Kirpichev <skirpichev@gmail.com>
License-Expression: MIT
Project-URL: Homepage, https://github.com/diofant/python-gmp
Project-URL: Source Code, https://github.com/diofant/python-gmp
Project-URL: Bug Tracker, https://github.com/diofant/python-gmp/issues
Project-URL: Documentation, https://python-gmp.readthedocs.io/en/latest/
Keywords: gmp,multiple-precision,arbitrary-precision,bignum
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Natural Language :: English
Classifier: Operating System :: POSIX
Classifier: Programming Language :: C
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX
Classifier: Operating System :: Unix
Classifier: Operating System :: MacOS
Classifier: Topic :: Scientific/Engineering :: Mathematics
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.9
Description-Content-Type: text/x-rst
License-File: LICENSE
Provides-Extra: tests
Requires-Dist: pytest; extra == "tests"
Requires-Dist: hypothesis; extra == "tests"
Requires-Dist: mpmath>=1.4.0a6; extra == "tests"
Provides-Extra: docs
Requires-Dist: sphinx>=8.2; extra == "docs"
Provides-Extra: develop
Requires-Dist: python-gmp[docs,tests]; extra == "develop"
Requires-Dist: pre-commit; extra == "develop"
Requires-Dist: pyperf; extra == "develop"
Requires-Dist: gmpy2>=2.2; platform_python_implementation != "PyPy" and extra == "develop"
Requires-Dist: python-flint>=0.7.0a5; extra == "develop"
Dynamic: license-file

Python-GMP
==========

Python extension module providing safe bindings to the GNU GMP.  This module
shouldn't crash the interpreter!  It requires Python 3.9 or later versions and
has been tested with CPython 3.9 through 3.14 and for PyPy 3.10 and 3.11.

This module can be used as a gmpy2/python-flint replacement to provide
CPython-compatible integer (mpz) type.  It includes few functions (factorial,
gcd and isqrt), compatible with the stdlib's module math.

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


Warning on alloca
-----------------

Most GMP packages enable using alloca() for temporary workspace allocation.
This module can't prevent a crash in case of a stack overflow.  To avoid this,
you should compile the GMP library with '--disable-alloca' configure option to
use rather the heap for all temporary allocations.

Of course, published on the PyPI binary wheels aren't affected by this issue.


Warning on using mp_set_memory_functions()
------------------------------------------

This extension customize memory allocation routines, used by the GMP.  Don't
use together with other GMP bindings, like the gmpy2!


Motivation
----------

The CPython (and most other Python implementations, like PyPy) is optimized to
work with small integers.  Algorithms used here for "big enough" integers
usually aren't best known in the field.  Fortunately, it's possible to use
bindings (for example, the `gmpy2 <https://pypi.org/project/gmpy2/>`_ package)
to the GNU Multiple Precision Arithmetic Library (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 allocation
functions and can't recover from errors such as out of memory.  So, it's easy
to crash the Python interpreter during the interactive session.  Following
example with the gmpy2 should work on most Unix systems:

.. code:: pycon

   >>> import gmpy2, resource
   >>> gmpy2.__version__
   '2.2.1'
   >>> resource.setrlimit(resource.RLIMIT_AS, (1024*32*1024, -1))
   >>> z = gmpy2.mpz(29925959575501)
   >>> while True:  # this loop will crash interpter
   ...     z = z*z
   ...
   GNU MP: Cannot allocate memory (size=2949160)
   Aborted

The gmp module handles such errors correctly:

.. code:: pycon

   >>> import gmp, resource
   >>> resource.setrlimit(resource.RLIMIT_AS, (1024*32*1024, -1))
   >>> z = gmp.mpz(29925959575501)
   >>> while True:
   ...     z = z*z
   ...
   Traceback (most recent call last):
     File "<python-input-3>", line 2, in <module>
       z = z*z
           ~^~
   MemoryError
   >>> # interpreter still works, all variables in
   >>> # the current scope are available,
   >>> z.bit_length()  # including pre-failure value of z
   mpz(5867630)
