From 4bf634a645a217b3b8b2f1a6c9c34a42b05da91e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Fri, 3 Nov 2023 11:18:49 +0100 Subject: [PATCH 1/2] fixing most cython-lint suggestions in 3 files --- cypari2/convert.pyx | 25 +++++++-------- cypari2/gen.pyx | 64 +++++++++++++++++++-------------------- cypari2/pari_instance.pyx | 55 ++++++++++++++++----------------- 3 files changed, 72 insertions(+), 72 deletions(-) diff --git a/cypari2/convert.pyx b/cypari2/convert.pyx index 8d39c3c..a7ce1ff 100644 --- a/cypari2/convert.pyx +++ b/cypari2/convert.pyx @@ -27,7 +27,7 @@ Because of this difference in bit lengths, converting integers involves some bit shuffling. """ -#***************************************************************************** +# **************************************************************************** # Copyright (C) 2016 Jeroen Demeyer # Copyright (C) 2016 Luca De Feo # Copyright (C) 2016 Vincent Delecroix @@ -36,26 +36,26 @@ some bit shuffling. # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. -# http://www.gnu.org/licenses/ -#***************************************************************************** +# https://www.gnu.org/licenses/ +# **************************************************************************** from __future__ import absolute_import, division, print_function from cysignals.signals cimport sig_on, sig_off, sig_error from cpython.version cimport PY_MAJOR_VERSION -from cpython.object cimport Py_SIZE from cpython.int cimport PyInt_AS_LONG, PyInt_FromLong from cpython.longintrepr cimport (_PyLong_New, - digit, PyLong_SHIFT, PyLong_MASK) + digit, PyLong_SHIFT, PyLong_MASK) from libc.limits cimport LONG_MIN, LONG_MAX from libc.math cimport INFINITY from .paridecl cimport * from .stack cimport new_gen, reset_avma from .string_utils cimport to_string, to_bytes -from .pycore_long cimport (ob_digit, _PyLong_IsZero, _PyLong_IsNegative, - _PyLong_IsPositive, _PyLong_DigitCount, _PyLong_SetSignAndDigitCount) +from .pycore_long cimport (ob_digit, _PyLong_IsZero, + _PyLong_IsPositive, _PyLong_DigitCount, + _PyLong_SetSignAndDigitCount) ######################################################################## # Conversion PARI -> Python @@ -331,8 +331,9 @@ cdef PyObject_FromGEN(GEN g): lc = lg(g) if lc <= 1: return [[]] - lr = lg(gel(g,1)) - return [[PyObject_FromGEN(gcoeff(g, i, j)) for j in range(1, lc)] for i in range(1, lr)] + lr = lg(gel(g, 1)) + return [[PyObject_FromGEN(gcoeff(g, i, j)) for j in range(1, lc)] + for i in range(1, lr)] elif t == t_INFINITY: if inf_get_sign(g) >= 0: return INFINITY @@ -390,10 +391,10 @@ cdef GEN gtoi(GEN g0) except NULL: sig_on() g = simplify_shallow(g0) if typ(g) == t_COMPLEX: - if gequal0(gel(g,2)): - g = gel(g,1) + if gequal0(gel(g, 2)): + g = gel(g, 1) if typ(g) == t_INTMOD: - g = gel(g,2) + g = gel(g, 2) g = trunc_safe(g) if typ(g) != t_INT: sig_error() diff --git a/cypari2/gen.pyx b/cypari2/gen.pyx index 75050a0..e826fc3 100644 --- a/cypari2/gen.pyx +++ b/cypari2/gen.pyx @@ -40,7 +40,7 @@ AUTHORS: conversion """ -#***************************************************************************** +# **************************************************************************** # Copyright (C) 2006,2010 William Stein # Copyright (C) ???? Justin Walker # Copyright (C) ???? Gonzalo Tornaria @@ -52,15 +52,15 @@ AUTHORS: # Distributed under the terms of the GNU General Public License (GPL) # as published by the Free Software Foundation; either version 2 of # the License, or (at your option) any later version. -# http://www.gnu.org/licenses/ -#***************************************************************************** +# https://www.gnu.org/licenses/ +# **************************************************************************** from __future__ import absolute_import, division, print_function cimport cython -from cpython.object cimport (Py_EQ, Py_NE, Py_LE, Py_GE, Py_LT, Py_GT, - PyTypeObject) +from cpython.object cimport (Py_EQ, Py_NE, Py_LE, Py_GE, Py_LT, + PyTypeObject) from cysignals.memory cimport sig_free, check_malloc from cysignals.signals cimport sig_check, sig_on, sig_off, sig_block, sig_unblock @@ -69,8 +69,7 @@ from .types cimport * from .string_utils cimport to_string, to_bytes from .paripriv cimport * from .convert cimport PyObject_AsGEN, gen_to_integer -from .pari_instance cimport (prec_bits_to_words, prec_words_to_bits, - default_bitprec, get_var) +from .pari_instance cimport (prec_bits_to_words, get_var) from .stack cimport (new_gen, new_gens2, new_gen_noclear, clone_gen, clear_stack, reset_avma, remove_from_pari_stack, move_gens_to_heap) @@ -871,7 +870,7 @@ cdef class Gen(Gen_base): return [r1, r2] def nf_get_zk(self): - """ + r""" Returns a vector with a `\ZZ`-basis for the ring of integers of this number field. The first element is always `1`. @@ -1074,7 +1073,7 @@ cdef class Gen(Gen_base): return new_gen(idealmoddivisor(self.g, ideal.g)) def pr_get_p(self): - """ + r""" Returns the prime of `\ZZ` lying below this prime ideal. NOTE: ``self`` must be a PARI prime ideal (as returned by @@ -1096,7 +1095,7 @@ cdef class Gen(Gen_base): return clone_gen(pr_get_p(self.g)) def pr_get_e(self): - """ + r""" Returns the ramification index (over `\QQ`) of this prime ideal. NOTE: ``self`` must be a PARI prime ideal (as returned by @@ -1123,7 +1122,7 @@ cdef class Gen(Gen_base): return e def pr_get_f(self): - """ + r""" Returns the residue class degree (over `\QQ`) of this prime ideal. NOTE: ``self`` must be a PARI prime ideal (as returned by @@ -1354,7 +1353,7 @@ cdef class Gen(Gen_base): elif isinstance(n, slice): l = glength(self.g) start, stop, step = n.indices(l) - inds = xrange(start, stop, step) + inds = range(start, stop, step) k = len(inds) # fast exit for empty vector if k == 0: @@ -1372,9 +1371,9 @@ cdef class Gen(Gen_base): # Index is not a tuple or slice, convert to integer i = n - ## there are no "out of bounds" problems - ## for a polynomial or power series, so these go before - ## bounds testing + # there are no "out of bounds" problems + # for a polynomial or power series, so these go before + # bounds testing if pari_type == t_POL: sig_on() return new_gen(polcoeff0(self.g, i, -1)) @@ -1394,8 +1393,8 @@ cdef class Gen(Gen_base): raise IndexError("index out of range") elif pari_type == t_VEC or pari_type == t_MAT: - #t_VEC : row vector [ code ] [ x_1 ] ... [ x_k ] - #t_MAT : matrix [ code ] [ col_1 ] ... [ col_k ] + # t_VEC : row vector [ code ] [ x_1 ] ... [ x_k ] + # t_MAT : matrix [ code ] [ col_1 ] ... [ col_k ] ind = i if self.itemcache is not None and ind in self.itemcache: return self.itemcache[ind] @@ -1407,11 +1406,11 @@ cdef class Gen(Gen_base): return val elif pari_type == t_VECSMALL: - #t_VECSMALL: vec. small ints [ code ] [ x_1 ] ... [ x_k ] + # t_VECSMALL: vec. small ints [ code ] [ x_1 ] ... [ x_k ] return self.g[i+1] elif pari_type == t_STR: - #t_STR : string [ code ] [ man_1 ] ... [ man_k ] + # t_STR : string [ code ] [ man_1 ] ... [ man_k ] return chr(GSTR(self.g)[i]) elif pari_type == t_LIST: @@ -1515,7 +1514,7 @@ cdef class Gen(Gen_base): >>> type(v[0]) <... 'cypari2.gen.Gen'> """ - cdef Py_ssize_t i, j, step + cdef Py_ssize_t i, j cdef Gen x = objtogen(y) if isinstance(n, tuple): @@ -1529,14 +1528,14 @@ cdef class Gen(Gen_base): if j < 0 or j >= glength(self.g): raise IndexError("column j(=%s) must be between 0 and %s" % (j, self.ncols()-1)) - self.cache((i,j), x) + self.cache((i, j), x) xt = x.ref_target() set_gcoeff(self.g, i+1, j+1, xt) return elif isinstance(n, slice): l = glength(self.g) - inds = xrange(*n.indices(l)) + inds = range(*n.indices(l)) k = len(inds) if k > len(y): raise ValueError("attempt to assign sequence of size %s to slice of size %s" % (len(y), k)) @@ -1729,8 +1728,8 @@ cdef class Gen(Gen_base): return "0" lx = lgefint(x) - 2 # number of words size = lx * 4 * sizeof(long) - s = check_malloc(size+3) # 1 char for sign, 1 char for 0, 1 char for '\0' - sp = s + size + 3 - 1 # last character + s = check_malloc(size+3) # 1 char for sign, 1 char for 0, 1 char for '\0' + sp = s + size + 3 - 1 # last character sp[0] = 0 xp = int_LSW(x) for i from 0 <= i < lx: @@ -1772,11 +1771,11 @@ cdef class Gen(Gen_base): return "0x0" lx = lgefint(x) - 2 # number of words size = lx*2*sizeof(long) - s = check_malloc(size+4) # 1 char for sign, 2 chars for 0x, 1 char for '\0' - sp = s + size + 4 - 1 # last character + s = check_malloc(size+4) # 1 char for sign, 2 chars for 0x, 1 char for '\0' + sp = s + size + 4 - 1 # last character sp[0] = 0 xp = int_LSW(x) - for i from 0 <= i < lx: + for i in range(lx): w = xp[0] for j in range(2*sizeof(long)): sp -= 1 @@ -1925,7 +1924,6 @@ cdef class Gen(Gen_base): """ # TODO: deprecate cdef long n - cdef Gen t if typ(self.g) != t_VEC and typ(self.g) != t_COL: raise TypeError("Object (=%s) must be of type t_VEC or t_COL." % self) @@ -2730,7 +2728,6 @@ cdef class Gen(Gen_base): >>> pari('(2.4*x^2 - 1.7)/x').truncate() 2.40000000000000*x """ - cdef int n cdef long e cdef Gen y sig_on() @@ -3142,7 +3139,6 @@ cdef class Gen(Gen_base): """ cdef GEN G cdef long t - cdef Gen g sig_on() if find_root: t = itos(gissquareall(x.g, &G)) @@ -3445,7 +3441,7 @@ cdef class Gen(Gen_base): return new_gens2(x, y) def elltors(self): - """ + r""" Return information about the torsion subgroup of the given elliptic curve. @@ -3692,7 +3688,7 @@ cdef class Gen(Gen_base): return v def nfbasis(self, long flag=0, fa=None): - """ + r""" Integral basis of the field `\QQ[a]`, where ``a`` is a root of the polynomial x. @@ -4147,6 +4143,8 @@ cdef class Gen(Gen_base): """ Return the number of arguments of this ``t_CLOSURE``. + Examples: + >>> from cypari2 import Pari >>> pari = Pari() >>> pari("() -> 42").arity() @@ -4157,7 +4155,7 @@ cdef class Gen(Gen_base): 3 """ if typ(self.g) != t_CLOSURE: - raise TypeError(f"arity() requires a t_CLOSURE") + raise TypeError("arity() requires a t_CLOSURE") return closure_arity(self.g) def factorpadic(self, p, long r=20): diff --git a/cypari2/pari_instance.pyx b/cypari2/pari_instance.pyx index 1366ab4..a54170a 100644 --- a/cypari2/pari_instance.pyx +++ b/cypari2/pari_instance.pyx @@ -267,19 +267,18 @@ Test that changing the stack size using ``default`` works properly: 536870912 """ -#***************************************************************************** +# **************************************************************************** # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. -# http://www.gnu.org/licenses/ -#***************************************************************************** +# https://www.gnu.org/licenses/ +# **************************************************************************** from __future__ import absolute_import, division import sys from libc.stdio cimport * -cimport cython from cysignals.signals cimport sig_check, sig_on, sig_off, sig_error @@ -296,6 +295,7 @@ from .closure cimport _pari_init_closure # when no explicit precision is given and the inputs are exact. cdef long prec = prec_bits_to_words(53) + ################################################################# # conversions between various real precision models ################################################################# @@ -632,10 +632,10 @@ cdef class Pari(Pari_auto): # chances that something goes wrong here (for example, if we # are out of memory). printf("top = %p\navma = %p\nbot = %p\nsize = %lu\n", - pari_mainstack.top, - avma, - pari_mainstack.bot, - pari_mainstack.rsize) + pari_mainstack.top, + avma, + pari_mainstack.bot, + pari_mainstack.rsize) fflush(stdout) def __repr__(self): @@ -1025,7 +1025,7 @@ cdef class Pari(Pari_auto): set_pari_stack_size(s, sizemax) if not silent: print("PARI stack size set to {} bytes, maximum size set to {}". - format(self.stacksize(), self.stacksizemax())) + format(self.stacksize(), self.stacksizemax())) @staticmethod def pari_version(): @@ -1182,25 +1182,25 @@ cdef class Pari(Pari_auto): return new_gen(mpfact(n)) def polsubcyclo(self, long n, long d, v=None): - """ + r""" polsubcyclo(n, d, v=x): return the pari list of polynomial(s) defining the sub-abelian extensions of degree `d` of the cyclotomic field `\QQ(\zeta_n)`, where `d` divides `\phi(n)`. - Examples:: + Examples: >>> import cypari2 >>> pari = cypari2.Pari() - >>> pari.polsubcyclo(8, 4) - [x^4 + 1] - >>> pari.polsubcyclo(8, 2, 'z') - [z^2 + 2, z^2 - 2, z^2 + 1] - >>> pari.polsubcyclo(8, 1) - [x - 1] - >>> pari.polsubcyclo(8, 3) - [] + >>> pari.polsubcyclo(8, 4) + [x^4 + 1] + >>> pari.polsubcyclo(8, 2, 'z') + [z^2 + 2, z^2 - 2, z^2 + 1] + >>> pari.polsubcyclo(8, 1) + [x - 1] + >>> pari.polsubcyclo(8, 3) + [] """ cdef Gen plist sig_on() @@ -1265,8 +1265,8 @@ cdef class Pari(Pari_auto): v = self._empty_vector(n) if entries is not None: if len(entries) != n: - raise IndexError("length of entries (=%s) must equal n (=%s)"%\ - (len(entries), n)) + raise IndexError("length of entries (=%s) must equal n (=%s)" % + (len(entries), n)) for i, x in enumerate(entries): v[i] = x return v @@ -1293,32 +1293,33 @@ cdef class Pari(Pari_auto): cdef Gen x sig_on() - A = new_gen(zeromatcopy(m,n)) + A = new_gen(zeromatcopy(m, n)) if entries is not None: if len(entries) != m * n: - raise IndexError("len of entries (=%s) must be %s*%s=%s"%(len(entries),m,n,m*n)) + raise IndexError("len of entries (=%s) must be %s*%s=%s" % (len(entries), m, n, m * n)) k = 0 for i in range(m): for j in range(n): sig_check() x = objtogen(entries[k]) - set_gcoeff(A.g, i+1, j+1, x.ref_target()) - A.cache((i,j), x) + set_gcoeff(A.g, i + 1, j + 1, x.ref_target()) + A.cache((i, j), x) k += 1 return A def genus2red(self, P, p=None): - """ + r""" Let `P` be a polynomial with integer coefficients. Determines the reduction of the (proper, smooth) genus 2 curve `C/\QQ`, defined by the hyperelliptic equation `y^2 = P`. + The special syntax ``genus2red([P,Q])`` is also allowed, where the polynomials `P` and `Q` have integer coefficients, to represent the model `y^2 + Q(x)y = P(x)`. If the second argument `p` is specified, it must be a prime. Then only the local information at `p` is computed and returned. - + Examples: >>> import cypari2 From d641aa39845230209fc963bc28a26b59b37f0551 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Fri, 3 Nov 2023 13:51:11 +0100 Subject: [PATCH 2/2] fix import --- cypari2/gen.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cypari2/gen.pyx b/cypari2/gen.pyx index e826fc3..293b57a 100644 --- a/cypari2/gen.pyx +++ b/cypari2/gen.pyx @@ -69,7 +69,7 @@ from .types cimport * from .string_utils cimport to_string, to_bytes from .paripriv cimport * from .convert cimport PyObject_AsGEN, gen_to_integer -from .pari_instance cimport (prec_bits_to_words, get_var) +from .pari_instance cimport (default_bitprec, prec_bits_to_words, get_var) from .stack cimport (new_gen, new_gens2, new_gen_noclear, clone_gen, clear_stack, reset_avma, remove_from_pari_stack, move_gens_to_heap)