Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/libs/pari/handle_error.pyx
8871 views
1
"""
2
Functions for handling PARI errors
3
4
AUTHORS:
5
6
- Peter Bruin (September 2013): initial version (#9640)
7
8
"""
9
include "sage/ext/stdsage.pxi"
10
include "sage/ext/interrupt.pxi"
11
include "decl.pxi"
12
13
from cpython cimport PyErr_Occurred
14
15
16
# Global variable to record error string
17
cdef str pari_error_string
18
19
cdef void _pari_init_error_handling():
20
"""
21
Set up our code for handling PARI errors.
22
23
TEST::
24
25
sage: try:
26
....: p = pari.polcyclo(-1)
27
....: except PariError as e:
28
....: print e.errtext()
29
....:
30
*** argument must be positive in polcyclo.
31
32
"""
33
global pari_error_string
34
global cb_pari_err_recover
35
global cb_pari_handle_exception
36
pari_error_string = ""
37
cb_pari_err_recover = _pari_err_recover
38
cb_pari_handle_exception = _pari_handle_exception
39
40
cdef void _pari_check_warning():
41
"""
42
Print pending PARI library warning messages to stderr.
43
44
TEST::
45
46
sage: pari('warning("test")')
47
*** user warning: test
48
49
"""
50
global pari_error_string
51
if pari_error_string != "":
52
import sys
53
sys.stderr.write(pari_error_string)
54
pari_error_string = ""
55
56
cdef int _pari_handle_exception(long err) except 0:
57
"""
58
Convert a PARI error into a Sage exception, unless the error was
59
a stack overflow, in which case we enlarge the stack.
60
61
This function is a callback from the PARI error handler.
62
63
EXAMPLES::
64
65
sage: pari('error("test")')
66
Traceback (most recent call last):
67
...
68
RuntimeError: PARI user exception
69
*** at top-level: error("test")
70
*** ^-------------
71
*** user error: test
72
73
sage: pari(1)/pari(0)
74
Traceback (most recent call last):
75
...
76
PariError: division by zero
77
78
"""
79
if err == errpile:
80
# PARI is out of memory. We double the size of the PARI stack
81
# and retry the computation.
82
from sage.libs.pari.all import pari
83
pari.allocatemem(silent=True)
84
return 0
85
86
if err == user:
87
raise RuntimeError("PARI user exception\n%s" % pari_error_string)
88
else:
89
from sage.libs.pari.all import PariError
90
raise PariError(err, pari_error_string)
91
92
cdef void _pari_err_recover(long err):
93
"""
94
Reset the error string and jump back to ``sig_on()``, either to
95
retry the code (in case of no error) or to make the already-raised
96
exception known to Python.
97
98
TEST:
99
100
Perform a computation that requires doubling the default stack
101
several times::
102
103
sage: pari.allocatemem(2^12)
104
PARI stack size set to 4096 bytes
105
sage: x = pari('2^(2^26)')
106
sage: x == 2^(2^26)
107
True
108
109
"""
110
global pari_error_string
111
pari_error_string = ""
112
if not PyErr_Occurred():
113
# No exception was raised => retry the computation starting
114
# from sig_on(). This can happen if we successfully enlarged the
115
# PARI stack in _pari_handle_exception().
116
sig_retry()
117
else:
118
# An exception was raised. Jump to the signal-handling code
119
# which will cause sig_on() to see the exception.
120
sig_error()
121
122