Module _gmp.gmp_error¶
We gently trap errors generated by the GNU MP library. When the latter
sends the SIGFPE (Floating Point Error) signal following a
fatal error (division by zero, for example), the gmp module
captures this signal and raises an appropriate exception without
aborting the Python interpreter. Let’s look at some examples:
>>> z = mpz(-1); u = mpz()
>>> z.sqrt()
Traceback (most recent call last):
File "<python-input-3>", line 1, in <module>
z.sqrt()
~~~~~~^^
_gmp.gmp_error: square root of negative (gmp_errno=4)
>>> z // u
Traceback (most recent call last):
File "<python-input-4>", line 1, in <module>
z // u
~~^^~~
_gmp.gmp_error: division by zero (gmp_errno=2)
>>> # Python interpreter still alive - no abort()
Same example using a block try / except:
>>> z = mpz(-1); u = mpz()
>>> try:
... z.sqrt()
... except GMPErrSqrtOfNegative:
... # Do something.
>>> try:
... z // u
... except GMPErrDivisionByZero:
... # Do something.
>>> # Python interpreter still alive - no abort()
Warning
The trick is, after capturing the SIGFPE signal, to
perform a non-local goto using system calls
sigsetjmp() and siglongjmp(), which of course
assumes that these are present on the operating system
(which should be the case for any system conforming to
the standard POSIX.1-2008).
Exceptions generated by the GNU MP library¶
- exception _gmp.gmp_error.GMPErrUnsupportedArgument¶
- exception _gmp.gmp_error.GMPErrDivisionByZero¶
- exception _gmp.gmp_error.GMPErrSqrtOfNegative¶
- exception _gmp.gmp_error.GMPErrInvalidArgument¶
- exception _gmp.gmp_error.GMPErrMPZOverflow¶
_gmp.gmp_error Error |
GNU MP Error (defined in |
|---|---|
|
|
|
|
|
|
|
|
|