Skip to content

Commit b3e5ec9

Browse files
committed
Refactor out more conversions
1 parent 726a699 commit b3e5ec9

File tree

8 files changed

+41
-30
lines changed

8 files changed

+41
-30
lines changed

src/flint/arb.pyx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from cpython.version cimport PY_MAJOR_VERSION
2+
from flint.utils.conversion cimport chars_from_str, str_from_chars
3+
14
cdef _str_trunc(s, trunc=0):
25
if trunc > 0 and len(s) > 3 * trunc:
36
left = right = trunc

src/flint/flint_base/flint_context.pxd

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,3 @@ cdef class FlintContext:
99
cdef arf_rnd_t rnd
1010
cdef public bint unicode
1111
cdef public long _cap
12-
13-
pass

src/flint/fmpz.pyx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from cpython.version cimport PY_MAJOR_VERSION
2+
from flint.utils.conversion cimport chars_from_str
3+
14
cdef inline int fmpz_set_pylong(fmpz_t x, obj):
25
cdef int overflow
36
cdef slong longval

src/flint/fmpz_mpoly.pyx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from cpython.version cimport PY_MAJOR_VERSION
2+
from flint.utils.conversion cimport str_from_chars
3+
14
cdef any_as_fmpz_mpoly(x):
25
cdef fmpz_mpoly res
36
"""

src/flint/fmpz_poly.pyx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from cpython.version cimport PY_MAJOR_VERSION
2+
13
cdef any_as_fmpz_poly(x):
24
cdef fmpz_poly res
35
if typecheck(x, fmpz_poly):

src/flint/nmod_mat.pyx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from flint.utils.conversion cimport matrix_to_str
2+
13
cdef any_as_nmod_mat(obj, nmod_t mod):
24
cdef nmod_mat r
35
cdef mp_limb_t v

src/flint/pyflint.pyx

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,6 @@ cdef extern from "Python.h":
2424
double PyComplex_RealAsDouble(PyObject *op)
2525
double PyComplex_ImagAsDouble(PyObject *op)
2626

27-
from cpython.version cimport PY_MAJOR_VERSION
28-
29-
cdef chars_from_str(s):
30-
if PY_MAJOR_VERSION < 3:
31-
return s
32-
else:
33-
return s.encode('ascii')
34-
35-
cdef str_from_chars(s):
36-
if PY_MAJOR_VERSION < 3:
37-
return str(s)
38-
else:
39-
return bytes(s).decode('ascii')
40-
41-
cdef matrix_to_str(tab):
42-
if len(tab) == 0 or len(tab[0]) == 0:
43-
return "[]"
44-
tab = [[str(c) for c in row] for row in tab]
45-
widths = []
46-
for i in xrange(len(tab[0])):
47-
w = max([len(row[i]) for row in tab])
48-
widths.append(w)
49-
for i in xrange(len(tab)):
50-
tab[i] = [s.rjust(widths[j]) for j, s in enumerate(tab[i])]
51-
tab[i] = "[" + (", ".join(tab[i])) + "]"
52-
return "\n".join(tab)
53-
5427
cdef inline bint typecheck(object ob, object tp):
5528
return PyObject_TypeCheck(ob, <PyTypeObject*>tp)
5629

src/flint/utils/conversion.pxd

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
1+
from cpython.version cimport PY_MAJOR_VERSION
2+
13
cdef inline long prec_to_dps(n):
24
return max(1, int(round(int(n)/3.3219280948873626)-1))
35

46
cdef inline long dps_to_prec(n):
5-
return max(1, int(round((int(n)+1)*3.3219280948873626)))
7+
return max(1, int(round((int(n)+1)*3.3219280948873626)))
8+
9+
cdef inline chars_from_str(s):
10+
if PY_MAJOR_VERSION < 3:
11+
return s
12+
else:
13+
return s.encode('ascii')
14+
15+
cdef inline str_from_chars(s):
16+
if PY_MAJOR_VERSION < 3:
17+
return str(s)
18+
else:
19+
return bytes(s).decode('ascii')
20+
21+
cdef inline matrix_to_str(tab):
22+
if len(tab) == 0 or len(tab[0]) == 0:
23+
return "[]"
24+
tab = [[str(c) for c in row] for row in tab]
25+
widths = []
26+
for i in xrange(len(tab[0])):
27+
w = max([len(row[i]) for row in tab])
28+
widths.append(w)
29+
for i in xrange(len(tab)):
30+
tab[i] = [s.rjust(widths[j]) for j, s in enumerate(tab[i])]
31+
tab[i] = "[" + (", ".join(tab[i])) + "]"
32+
return "\n".join(tab)

0 commit comments

Comments
 (0)