@@ -52,11 +52,11 @@ from libc.math cimport INFINITY
5252
5353from .paridecl cimport *
5454from .stack cimport new_gen
55+ from .string_utils cimport to_string
5556
5657cdef extern from * :
5758 Py_ssize_t* Py_SIZE_PTR " &Py_SIZE" (object )
5859
59-
6060# ###################################
6161# Integers
6262# ###################################
@@ -74,28 +74,32 @@ cpdef integer_to_gen(x):
7474 >>> a = integer_to_gen(int(12345)); a; type(a)
7575 12345
7676 <... 'cypari2.gen.Gen'>
77- >>> a = integer_to_gen(long(12345)); a; type(a)
78- 12345
79- <... 'cypari2.gen.Gen'>
8077 >>> integer_to_gen(float(12345))
8178 Traceback (most recent call last):
8279 ...
8380 TypeError: integer_to_gen() needs an int or long argument, not float
81+ >>> integer_to_gen(2**100)
82+ 1267650600228229401496703205376
8483
8584 Tests:
8685
86+ >>> import sys
87+ >>> if sys.version_info.major == 3:
88+ ... long = int
89+ >>> assert integer_to_gen(long(12345)) == 12345
8790 >>> for i in range(10000):
8891 ... x = 3**i
89- ... if pari(long(x)) != pari(x):
92+ ... if pari(long(x)) != pari(x) or pari(int(x)) != pari(x) :
9093 ... print(x)
9194 """
92- if isinstance (x, int ):
93- sig_on()
94- return new_gen(stoi(PyInt_AS_LONG(x)))
95- elif isinstance (x, long ):
95+ if isinstance (x, long ):
9696 sig_on()
9797 return new_gen(PyLong_AsGEN(x))
98- raise TypeError (" integer_to_gen() needs an int or long argument, not {}" .format(type (x).__name__))
98+ elif isinstance (x, int ):
99+ sig_on()
100+ return new_gen(stoi(PyInt_AS_LONG(x)))
101+ else :
102+ raise TypeError (" integer_to_gen() needs an int or long argument, not {}" .format(type (x).__name__))
99103
100104cpdef gen_to_integer(Gen x):
101105 """
@@ -115,9 +119,8 @@ cpdef gen_to_integer(Gen x):
115119 >>> a = gen_to_integer(pari("12345")); a; type(a)
116120 12345
117121 <... 'int'>
118- >>> a = gen_to_integer(pari("10^30")); a; type(a)
119- 1000000000000000000000000000000L
120- <... 'long'>
122+ >>> gen_to_integer(pari("10^30")) == 10**30
123+ True
121124 >>> gen_to_integer(pari("19/5"))
122125 3
123126 >>> gen_to_integer(pari("1 + 0.0*I"))
@@ -130,11 +133,14 @@ cpdef gen_to_integer(Gen x):
130133 5
131134 >>> gen_to_integer(pari("Pol(42)"))
132135 42
133- >>> gen_to_integer(pari("x "))
136+ >>> gen_to_integer(pari("u "))
134137 Traceback (most recent call last):
135138 ...
136- TypeError: unable to convert PARI object x of type t_POL to an integer
137- >>> gen_to_integer(pari("x + O(x^2)"))
139+ TypeError: unable to convert PARI object u of type t_POL to an integer
140+ >>> s = pari("x + O(x^2)")
141+ >>> s
142+ x + O(x^2)
143+ >>> gen_to_integer(s)
138144 Traceback (most recent call last):
139145 ...
140146 TypeError: unable to convert PARI object x + O(x^2) of type t_SER to an integer
@@ -145,14 +151,17 @@ cpdef gen_to_integer(Gen x):
145151
146152 Tests:
147153
154+ >>> gen_to_integer(pari("1.0 - 2^64")) == -18446744073709551615
155+ True
156+ >>> gen_to_integer(pari("1 - 2^64")) == -18446744073709551615
157+ True
158+ >>> import sys
159+ >>> if sys.version_info.major == 3:
160+ ... long = int
148161 >>> for i in range(10000):
149162 ... x = 3**i
150- ... if long(pari(x)) != long(x):
163+ ... if long(pari(x)) != long(x) or int(pari(x)) != x :
151164 ... print(x)
152- >>> gen_to_integer(pari("1.0 - 2^64"))
153- -18446744073709551615L
154- >>> gen_to_integer(pari("1 - 2^64"))
155- -18446744073709551615L
156165
157166 Check some corner cases:
158167
@@ -212,9 +221,10 @@ cdef GEN gtoi(GEN g0) except NULL:
212221 sig_error()
213222 sig_off()
214223 except RuntimeError :
215- raise TypeError (stack_sprintf(
224+ s = to_string (stack_sprintf(
216225 " unable to convert PARI object %P s of type %s to an integer" ,
217226 g0, type_name(typ(g0))))
227+ raise TypeError (s)
218228 return g
219229
220230
@@ -460,9 +470,10 @@ cpdef gen_to_python(Gen z):
460470 >>> type( a)
461471 <... 'int'>
462472
463- >>> a = gen_to_python( pari( '3^ 50'))
464- >>> type( a)
465- <... 'long'>
473+ >>> gen_to_python( pari( '3^ 50')) == 3** 50
474+ True
475+ >>> type( gen_to_python( pari( '3^ 50'))) == type( 3** 50)
476+ True
466477
467478 Converting rational numbers:
468479
@@ -525,21 +536,21 @@ cpdef gen_to_python(Gen z):
525536 [1, 2, 3 ]
526537 >>> type( a1)
527538 <... 'list'>
528- >>> map ( type, a1 )
539+ >>> [ type(x) for x in a1 ]
529540 [<... 'int'>, <... 'int'>, <... 'int'> ]
530541
531542 >>> a2 = gen_to_python( z2) ; a2
532543 [1, 3.4, [-5, 2 ], inf]
533544 >>> type( a2)
534545 <... 'list'>
535- >>> map ( type, a2 )
546+ >>> [ type(x) for x in a2 ]
536547 [<... 'int'>, <... 'float'>, <... 'list'>, <... 'float'> ]
537548
538549 >>> a3 = gen_to_python( z3) ; a3
539550 [1, 5.2 ]
540551 >>> type( a3)
541552 <... 'list'>
542- >>> map ( type, a3 )
553+ >>> [ type(x) for x in a3 ]
543554 [<... 'int'>, <... 'float'> ]
544555
545556 Converting matrices:
@@ -607,6 +618,6 @@ cpdef gen_to_python(Gen z):
607618 else :
608619 return - INFINITY
609620 elif t == t_STR:
610- return str (z )
621+ return to_string(GSTR(g) )
611622 else :
612623 raise NotImplementedError (" conversion not implemented for {}" .format(z.type()))
0 commit comments