Skip to content

Commit 8b4fee4

Browse files
committed
Monkey-patch default(parisize) and default(parisizemax)
1 parent 3009359 commit 8b4fee4

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

cypari2/handle_error.pyx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,12 @@ cdef void _pari_err_recover(long errnum):
221221
"""
222222
reset_avma()
223223

224+
# Special case errnum == -1 corresponds to a reallocation of the
225+
# PARI stack. This is not an error, so just proceed as if nothing
226+
# happened.
227+
if errnum < 0:
228+
return
229+
224230
# An exception was raised. Jump to the signal-handling code
225231
# which will cause sig_on() to see the exception.
226232
sig_error()

cypari2/pari_instance.pyx

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,17 @@ PARI stack size set to 8000000 bytes, maximum size set to ...
254254
... pari.binomial(2**100, 2**22)
255255
... except AlarmInterrupt:
256256
... pass
257+
258+
Test that changing the stack size using ``default`` works properly:
259+
260+
>>> pari.default("parisizemax", 2**23)
261+
>>> pari = cypari2.Pari() # clear stack
262+
>>> a = pari(1)
263+
>>> pari.default("parisizemax", 2**29)
264+
>>> a + a
265+
2
266+
>>> pari.default("parisizemax")
267+
536870912
257268
"""
258269

259270
#*****************************************************************************
@@ -270,7 +281,7 @@ import sys
270281
from libc.stdio cimport *
271282
cimport cython
272283

273-
from cysignals.signals cimport sig_check, sig_on, sig_off
284+
from cysignals.signals cimport sig_check, sig_on, sig_off, sig_error
274285

275286
from .string_utils cimport to_string, to_bytes
276287
from .paridecl cimport *
@@ -522,6 +533,14 @@ cdef class Pari(Pari_auto):
522533
global factor_proven
523534
factor_proven = 1
524535

536+
# Monkey-patch default(parisize) and default(parisizemax)
537+
ep = pari_is_default("parisize")
538+
if ep:
539+
ep.value = <void*>patched_parisize
540+
ep = pari_is_default("parisizemax")
541+
if ep:
542+
ep.value = <void*>patched_parisizemax
543+
525544
def __init__(self, size_t size=8000000, size_t sizemax=0, unsigned long maxprime=500000):
526545
"""
527546
(Re)-Initialize the PARI system.
@@ -1385,6 +1404,25 @@ cdef long get_var(v) except -2:
13851404
return varno
13861405

13871406

1407+
# Monkey-patched versions of default(parisize) and default(parisizemax).
1408+
# We need to call move_gens_to_heap(-1) before reallocating the PARI
1409+
# stack. The monkey-patching is set up in PariInstance.__cinit__
1410+
cdef GEN patched_parisize(const char* v, long flag):
1411+
try:
1412+
move_gens_to_heap(-1)
1413+
except:
1414+
sig_error()
1415+
return sd_parisize(v, flag)
1416+
1417+
1418+
cdef GEN patched_parisizemax(const char* v, long flag):
1419+
try:
1420+
move_gens_to_heap(-1)
1421+
except:
1422+
sig_error()
1423+
return sd_parisizemax(v, flag)
1424+
1425+
13881426
IF HAVE_PLOT_SVG:
13891427
cdef void get_plot_ipython(PARI_plot* T):
13901428
# Values copied from src/graph/plotsvg.c in PARI sources

cypari2/types.pxd

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ cdef extern from "pari/pari.h":
9595
long fheight
9696
void (*draw)(PARI_plot *T, GEN w, GEN x, GEN y)
9797

98+
ctypedef struct entree:
99+
const char *name
100+
ulong valence
101+
void *value
102+
long menu
103+
const char *code
104+
const char *help
105+
void *pvalue
106+
long arity
107+
ulong hash
108+
entree *next
109+
98110
# Various structures that we don't interface but which need to be
99111
# declared, such that Cython understands the declarations of
100112
# functions using these types.
@@ -108,7 +120,6 @@ cdef extern from "pari/pari.h":
108120
struct forpart_t
109121
struct forprime_t
110122
struct forvec_t
111-
struct entree
112123
struct gp_context
113124
struct nfbasic_t
114125
struct pariFILE

0 commit comments

Comments
 (0)