Skip to content

Commit c15d99d

Browse files
author
Release Manager
committed
gh-40072: avoid using bytes_to_str in matrix/ (pyx files) <using instead the builtin `encode` or `decode` methods. ### 📝 Checklist - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. URL: #40072 Reported by: Frédéric Chapoton Reviewer(s): David Coudert
2 parents 873b7ee + aad7e39 commit c15d99d

File tree

4 files changed

+24
-23
lines changed

4 files changed

+24
-23
lines changed

src/sage/matrix/matrix_gfpn_dense.pyx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ cimport cython
4444
#
4545
####################
4646

47-
from sage.cpython.string cimport str_to_bytes
4847
from sage.cpython.string import FS_ENCODING
4948
from sage.rings.integer import Integer
5049
from sage.rings.finite_rings.finite_field_constructor import GF
@@ -467,8 +466,8 @@ cdef class Matrix_gfpn_dense(Matrix_dense):
467466
raise ValueError("cannot construct meataxe matrix from empty filename")
468467

469468
if type(filename) is not bytes:
470-
filename = str_to_bytes(filename, FS_ENCODING,
471-
'surrogateescape')
469+
filename = filename.encode(FS_ENCODING, 'surrogateescape')
470+
472471
sig_on()
473472
try:
474473
mat = MatLoad(filename)
@@ -1906,7 +1905,9 @@ def mtx_unpickle(f, int nr, int nc, data, bint m):
19061905
# in Python-3, Sage will receive a str in `latin1` encoding. Therefore,
19071906
# in the following line, we use a helper function that would return bytes,
19081907
# regardless whether the input is bytes or str.
1909-
cdef bytes Data = str_to_bytes(data, encoding='latin1')
1908+
if isinstance(data, str):
1909+
data = data.encode('latin1')
1910+
cdef bytes Data = data
19101911
if isinstance(f, int):
19111912
# This is for old pickles created with the group cohomology spkg
19121913
MS = MatrixSpace(GF(f, 'z'), nr, nc, implementation=Matrix_gfpn_dense)

src/sage/matrix/matrix_integer_dense.pyx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ TESTS::
6464
from libc.stdint cimport int64_t
6565
from libc.string cimport strcpy, strlen
6666

67-
from sage.cpython.string cimport char_to_str, str_to_bytes
67+
from sage.cpython.string cimport char_to_str
6868
from sage.ext.stdsage cimport PY_NEW
6969
from cysignals.signals cimport sig_check, sig_on, sig_str, sig_off
7070
from cysignals.memory cimport sig_malloc, sig_free, check_allocarray
@@ -497,7 +497,7 @@ cdef class Matrix_integer_dense(Matrix_dense):
497497
sage: matrix(ZZ,1,3,[1,193,15])._pickle() == (b'1 61 f', 0) # indirect doctest
498498
True
499499
"""
500-
return str_to_bytes(self._export_as_string(32), 'ascii')
500+
return self._export_as_string(32).encode('ascii')
501501

502502
cpdef _export_as_string(self, int base=10):
503503
"""
@@ -582,18 +582,18 @@ cdef class Matrix_integer_dense(Matrix_dense):
582582
self._unpickle_matrix_2x2_version0(data)
583583
else:
584584
raise RuntimeError("invalid pickle data")
585+
585586
else:
586-
raise RuntimeError("unknown matrix version (=%s)" % version)
587+
raise RuntimeError(f"unknown matrix version (={version})")
587588

588589
cdef _unpickle_version0(self, data):
589-
cdef Py_ssize_t i, j, n, k
590+
cdef Py_ssize_t i, j, k
590591
data = data.split()
591-
n = self._nrows * self._ncols
592-
if len(data) != n:
592+
if len(data) != self._nrows * self._ncols:
593593
raise RuntimeError("invalid pickle data")
594594
k = 0
595-
for i from 0 <= i < self._nrows:
596-
for j from 0 <= j < self._ncols:
595+
for i in range(self._nrows):
596+
for j in range(self._ncols):
597597
s = data[k]
598598
k += 1
599599
if fmpz_set_str(fmpz_mat_entry(self._matrix, i, j), s, 32):

src/sage/matrix/matrix_mod2_dense.pyx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ from sage.misc.verbose import verbose, get_verbose
118118
VectorSpace = None
119119
from sage.modules.vector_mod2_dense cimport Vector_mod2_dense
120120
from sage.structure.richcmp cimport rich_to_bool
121-
from sage.cpython.string cimport bytes_to_str, char_to_str, str_to_bytes
121+
from sage.cpython.string cimport char_to_str
122122
from sage.cpython.string import FS_ENCODING
123123

124124
cdef extern from "gd.h":
@@ -480,17 +480,17 @@ cdef class Matrix_mod2_dense(matrix_dense.Matrix_dense): # dense or sparse
480480
div_s[2*i] = c'+'
481481
last_i = i
482482

483-
for i from 0 <= i < self._nrows:
483+
for i in range(self._nrows):
484484
row_s = row = b'[' + empty_row + b']'
485-
for j from 0 <= j < self._ncols:
486-
row_s[1+2*j] = c'0' + mzd_read_bit(self._entries,i,j)
485+
for j in range(self._ncols):
486+
row_s[1+2*j] = c'0' + mzd_read_bit(self._entries, i, j)
487487
s.append(row)
488488

489489
if self._subdivisions is not None:
490490
for i in reversed(row_div):
491491
s.insert(i, row_divider)
492492

493-
return bytes_to_str(b"\n".join(s))
493+
return (b"\n".join(s)).decode()
494494

495495
def row(self, Py_ssize_t i, from_list=False):
496496
"""
@@ -2572,7 +2572,7 @@ def from_png(filename):
25722572
fn.close()
25732573

25742574
if type(filename) is not bytes:
2575-
filename = str_to_bytes(filename, FS_ENCODING, 'surrogateescape')
2575+
filename = filename.encode(FS_ENCODING, 'surrogateescape')
25762576

25772577
cdef FILE *f = fopen(filename, "rb")
25782578
sig_on()
@@ -2619,7 +2619,7 @@ def to_png(Matrix_mod2_dense A, filename):
26192619
fn.close()
26202620

26212621
if type(filename) is not bytes:
2622-
filename = str_to_bytes(filename, FS_ENCODING, 'surrogateescape')
2622+
filename = filename.encode(FS_ENCODING, 'surrogateescape')
26232623

26242624
cdef gdImagePtr im = gdImageCreate(c, r)
26252625
cdef FILE * out = fopen(filename, "wb")

src/sage/matrix/matrix_rational_dense.pyx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Test hashing::
7373
from libc.string cimport strcpy, strlen
7474

7575
from sage.categories.rings import Rings
76-
from sage.cpython.string cimport char_to_str, str_to_bytes
76+
from sage.cpython.string cimport char_to_str
7777

7878
from sage.modules.vector_rational_dense cimport Vector_rational_dense
7979
from sage.ext.stdsage cimport PY_NEW
@@ -374,13 +374,13 @@ cdef class Matrix_rational_dense(Matrix_dense):
374374
s = data[k]
375375
k += 1
376376
if '/' in s:
377-
num, den = [str_to_bytes(n) for n in s.split('/')]
377+
num, den = (n.encode() for n in s.split('/'))
378378
if fmpz_set_str(fmpq_mat_entry_num(self._matrix, i, j), num, 32) or \
379379
fmpz_set_str(fmpq_mat_entry_den(self._matrix, i, j), den, 32):
380380
raise RuntimeError("invalid pickle data")
381381
else:
382-
s = str_to_bytes(s)
383-
if fmpz_set_str(fmpq_mat_entry_num(self._matrix, i, j), s, 32):
382+
num = s.encode()
383+
if fmpz_set_str(fmpq_mat_entry_num(self._matrix, i, j), num, 32):
384384
raise RuntimeError("invalid pickle data")
385385
fmpz_one(fmpq_mat_entry_den(self._matrix, i, j))
386386

0 commit comments

Comments
 (0)