Skip to content

Commit cc1d510

Browse files
committed
New function new_gens2() to safely convert 2 GENs
1 parent 9070ab6 commit cc1d510

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

cypari2/gen.pyx

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ from .paripriv cimport *
7171
from .convert cimport PyObject_AsGEN, gen_to_integer
7272
from .pari_instance cimport (prec_bits_to_words, prec_words_to_bits,
7373
default_bitprec, get_var)
74-
from .stack cimport (new_gen, new_gen_noclear,
74+
from .stack cimport (new_gen, new_gens2, new_gen_noclear,
7575
clone_gen, clear_stack, reset_avma,
7676
remove_from_pari_stack, move_gens_to_heap)
7777
from .closure cimport objtoclosure
@@ -2980,11 +2980,11 @@ cdef class Gen(Gen_base):
29802980
>>> s == ('1.00000000000000 - 2.710505431 E-20*I' if bitness == '32' else '1.00000000000000 - 2.71050543121376 E-20*I')
29812981
True
29822982
"""
2983-
cdef GEN zetan
2983+
cdef GEN ans, zetan
29842984
cdef Gen t0 = objtogen(n)
29852985
sig_on()
2986-
ans = new_gen_noclear(gsqrtn(x.g, t0.g, &zetan, prec_bits_to_words(precision)))
2987-
return ans, new_gen(zetan)
2986+
ans = gsqrtn(x.g, t0.g, &zetan, prec_bits_to_words(precision))
2987+
return new_gens2(ans, zetan)
29882988

29892989
def ffprimroot(self):
29902990
r"""
@@ -3334,13 +3334,9 @@ cdef class Gen(Gen_base):
33343334
[1, -1, 0, 4, 3]
33353335
"""
33363336
cdef GEN x, y
3337-
cdef Gen model, change
3338-
cdef pari_sp t
33393337
sig_on()
33403338
x = ellminimalmodel(self.g, &y)
3341-
change = new_gen_noclear(y)
3342-
model = new_gen(x)
3343-
return model, change
3339+
return new_gens2(x, y)
33443340

33453341
def elltors(self):
33463342
"""
@@ -3659,7 +3655,7 @@ cdef class Gen(Gen_base):
36593655
"""
36603656
cdef Gen t0
36613657
cdef GEN g0
3662-
cdef GEN disc
3658+
cdef GEN ans, disc
36633659
if fa is not None:
36643660
t0 = objtogen(fa)
36653661
g0 = t0.g
@@ -3668,9 +3664,8 @@ cdef class Gen(Gen_base):
36683664
else:
36693665
g0 = NULL
36703666
sig_on()
3671-
B = new_gen_noclear(nfbasis(self.g, &disc, g0))
3672-
D = new_gen(disc)
3673-
return B, D
3667+
ans = nfbasis(self.g, &disc, g0)
3668+
return new_gens2(ans, disc)
36743669

36753670
def nfbasistoalg_lift(nf, x):
36763671
r"""
@@ -4428,8 +4423,7 @@ cdef class Gen(Gen_base):
44284423
cdef GEN dy, g
44294424
sig_on()
44304425
g = polint(self.g, t0.g, t1.g, &dy)
4431-
dif = new_gen_noclear(dy)
4432-
return new_gen(g), dif
4426+
return new_gens2(g, dy)
44334427

44344428
def ellwp(self, z='z', long n=20, long flag=0, unsigned long precision=0):
44354429
"""

cypari2/stack.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ from .gen cimport Gen_base, Gen
33

44

55
cdef Gen new_gen(GEN x)
6+
cdef new_gens2(GEN x, GEN y)
67
cdef Gen new_gen_noclear(GEN x)
78
cdef Gen clone_gen(GEN x)
89
cdef Gen clone_gen_noclear(GEN x)

cypari2/stack.pyx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,22 @@ cdef Gen new_gen(GEN x):
153153
return new_gen_noclear(x)
154154

155155

156+
cdef new_gens2(GEN x, GEN y):
157+
"""
158+
Create a 2-tuple of new ``Gen``s from 2 ``GEN``s.
159+
160+
Also call ``sig_off``() and clear the PARI stack.
161+
"""
162+
sig_off()
163+
global avma
164+
av = avma
165+
g1 = new_gen_noclear(x)
166+
# Restore avma in case that remove_from_pari_stack() was called
167+
avma = av
168+
g2 = new_gen_noclear(y)
169+
return (g1, g2)
170+
171+
156172
cdef Gen new_gen_noclear(GEN x):
157173
"""
158174
Create a new ``Gen`` from a ``GEN``.

0 commit comments

Comments
 (0)