From 92acd43985dde3139657136e6a4cc2481f0b3aa7 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Fri, 14 Jan 2022 17:38:51 -0500 Subject: [PATCH 1/4] Trac #33176: fix Cython warnings in sage.libs.ntl. To fix some Cython warnings like warning: sage/libs/ntl/ntl_ZZ.pyx:274:23: local variable 'ans' referenced before assignment we initialize a few int/long variables to zero when they are declared. These variables are all ultimately passed by reference to NTL to be overwritten, so the existing code is not wrong, but the warnings are unattractive. --- src/sage/libs/ntl/ntl_ZZ.pyx | 2 +- src/sage/libs/ntl/ntl_ZZ_pX.pyx | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sage/libs/ntl/ntl_ZZ.pyx b/src/sage/libs/ntl/ntl_ZZ.pyx index e488b7adc43..3d4134376e4 100644 --- a/src/sage/libs/ntl/ntl_ZZ.pyx +++ b/src/sage/libs/ntl/ntl_ZZ.pyx @@ -270,7 +270,7 @@ cdef class ntl_ZZ(object): AUTHOR: David Harvey (2006-08-05) """ - cdef int ans + cdef int ans = 0 ZZ_conv_to_int(ans, self.x) return ans diff --git a/src/sage/libs/ntl/ntl_ZZ_pX.pyx b/src/sage/libs/ntl/ntl_ZZ_pX.pyx index bd34a79c85c..56dbe3ee82b 100644 --- a/src/sage/libs/ntl/ntl_ZZ_pX.pyx +++ b/src/sage/libs/ntl/ntl_ZZ_pX.pyx @@ -276,7 +276,7 @@ cdef class ntl_ZZ_pX(object): """ self.c.restore_c() cdef ZZ_p_c r - cdef long l + cdef long l = 0 sig_on() r = ZZ_pX_coeff( self.x, i) ZZ_conv_to_long(l, ZZ_p_rep(r)) @@ -1152,7 +1152,7 @@ cdef class ntl_ZZ_pX(object): ZZ_pX_Modulus_build(mod, modulus.x) cdef ntl_ZZ_pX mod_prime cdef ntl_ZZ_pContext_class ctx - cdef long mini, minval + cdef long mini = 0, minval = 0 if Integer(modulus[0].lift()).valuation(p) == 1: eisenstein = True for c in modulus.list()[1:-1]: From 5594ba894abd548e403593378c588a6571fb9db6 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Fri, 14 Jan 2022 17:54:25 -0500 Subject: [PATCH 2/4] Trac #33176: update API usage in sage.libs.gap.element. The C_NEW_STRING() macro in GAP's stringobj.h is deprecated (according to the source), and should be replaced with MakeStringWithLen(). So that's what we do. The conversion is straightforward and happens to fix the Cython warnings, warning: sage/libs/gap/element.pyx:267:21: local variable 'result' referenced before assignment warning: sage/libs/gap/element.pyx:268:15: local variable 'result' referenced before assignment because now the return value of MakeStringWithLen() is assigned to the "result" variable. --- src/sage/libs/gap/element.pyx | 2 +- src/sage/libs/gap/gap_includes.pxd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sage/libs/gap/element.pyx b/src/sage/libs/gap/element.pyx index e6a7b3800e9..c26c80b2cb6 100644 --- a/src/sage/libs/gap/element.pyx +++ b/src/sage/libs/gap/element.pyx @@ -264,7 +264,7 @@ cdef Obj make_gap_string(sage_string) except NULL: try: GAP_Enter() b = str_to_bytes(sage_string) - C_NEW_STRING(result, len(b), b) + result = MakeStringWithLen(b, len(b)) return result finally: GAP_Leave() diff --git a/src/sage/libs/gap/gap_includes.pxd b/src/sage/libs/gap/gap_includes.pxd index 5a9ab486f77..6d22e32540b 100644 --- a/src/sage/libs/gap/gap_includes.pxd +++ b/src/sage/libs/gap/gap_includes.pxd @@ -182,4 +182,4 @@ cdef extern from "gap/stringobj.h" nogil: bint IS_STRING(Obj obj) bint IsStringConv(Obj obj) Obj NEW_STRING(Int) - void C_NEW_STRING(Obj new_gap_string, int length, char* c_string) + Obj MakeStringWithLen(const char* buf, size_t len) From 01176df32fc2e748c6e0a6330a22ea8e93547c89 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Fri, 14 Jan 2022 18:20:35 -0500 Subject: [PATCH 3/4] Trac #33176: fix a Cython warning in sage.libs.singular.groebner_strategy. In the NCPolynomial_plural normal_form() method, the int "max_ind" is passed by reference into Singular's redNF() function where it is promptly overwritten. This is fine, but Cython cannot know that, and throws warnings: warning: sage/libs/singular/groebner_strategy.pyx:540:67: local variable 'max_ind' referenced before assignment warning: sage/libs/singular/groebner_strategy.pyx:542:32: local variable 'max_ind' referenced before assignment Here we initialize "max_ind" to zero to avoid them. --- src/sage/libs/singular/groebner_strategy.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sage/libs/singular/groebner_strategy.pyx b/src/sage/libs/singular/groebner_strategy.pyx index 20751eafa71..e5807a7a9d4 100644 --- a/src/sage/libs/singular/groebner_strategy.pyx +++ b/src/sage/libs/singular/groebner_strategy.pyx @@ -536,7 +536,7 @@ cdef class NCGroebnerStrategy(SageObject): if unlikely(self._parent._ring != currRing): rChangeCurrRing(self._parent._ring) - cdef int max_ind + cdef int max_ind = 0 cdef poly *_p = redNF(p_Copy(p._poly, self._parent._ring), max_ind, 0, self._strat) if likely(_p!=NULL): _p = redtailBba(_p, max_ind, self._strat) From 93f2e907d774599444c8b9ff4f3e89a132fbaa6b Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Fri, 14 Jan 2022 18:55:23 -0500 Subject: [PATCH 4/4] Trac #33176: fix Cython warning in sage.matrix.matrix_modn_dense_template. The linbox_det() method in this file creates a float/double variable "d" and passes it by reference to the FFLAS/FFPACK pDet() method, which in turn passes it by reference to the Det() method to be overwritten with the return value from that function. This is all fine, but Cython doesn't know what the uninitialized variable is used for and throws warnings like, warning: sage/matrix/matrix_modn_dense_template.pxi:283:19: local variable 'd' referenced before assignment Here we initialize it to zero to avoid the warnings. --- src/sage/matrix/matrix_modn_dense_template.pxi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sage/matrix/matrix_modn_dense_template.pxi b/src/sage/matrix/matrix_modn_dense_template.pxi index ffdf576df34..9f945e9440b 100644 --- a/src/sage/matrix/matrix_modn_dense_template.pxi +++ b/src/sage/matrix/matrix_modn_dense_template.pxi @@ -274,7 +274,7 @@ cdef inline celement linbox_det(celement modulus, celement* entries, Py_ssize_t cdef ModField *F = new ModField(modulus) cdef celement *cpy = linbox_copy(modulus, entries, n, n) - cdef celement d + cdef celement d = 0 cdef size_t nbthreads nbthreads = Parallelism().get('linbox')