Skip to content

Commit 1a26a8c

Browse files
author
Release Manager
committed
gh-40060: use pathlib and decode in Kohel database some changes in the kohel_database file - use the decode method of bytes instead of our custom `bytes_to_str` - use pathlib.Path instead of os ### 📝 Checklist - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. URL: #40060 Reported by: Frédéric Chapoton Reviewer(s): David Coudert
2 parents 97993c4 + 57dd38e commit 1a26a8c

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

src/sage/databases/db_modular_polynomials.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@
3232
# https://www.gnu.org/licenses/
3333
# ****************************************************************************
3434
import bz2
35-
import os
36-
from sage.cpython.string import bytes_to_str
35+
from pathlib import Path
3736

3837

39-
def _dbz_to_string(name):
38+
def _dbz_to_string(name) -> str:
4039
r"""
4140
TESTS::
4241
@@ -54,17 +53,16 @@ def _dbz_to_string(name):
5453
'0\n1\n'
5554
"""
5655
from sage.env import SAGE_SHARE
57-
dblocation = os.path.join(SAGE_SHARE, 'kohel')
58-
filename = os.path.join(dblocation, name)
56+
filename = Path(SAGE_SHARE) / 'kohel' / name
5957
try:
6058
with open(filename, 'rb') as f:
6159
data = bz2.decompress(f.read())
6260
except OSError:
63-
raise ValueError('file not found in the Kohel database')
64-
return bytes_to_str(data)
61+
raise FileNotFoundError('file not found in the Kohel database')
62+
return data.decode()
6563

6664

67-
def _dbz_to_integer_list(name):
65+
def _dbz_to_integer_list(name) -> list[list]:
6866
r"""
6967
TESTS::
7068
@@ -90,7 +88,7 @@ def _dbz_to_integer_list(name):
9088
for row in data.split("\n")[:-1]]
9189

9290

93-
def _dbz_to_integers(name):
91+
def _dbz_to_integers(name) -> list:
9492
r"""
9593
TESTS::
9694
@@ -103,19 +101,20 @@ def _dbz_to_integers(name):
103101

104102

105103
class ModularPolynomialDatabase:
106-
def _dbpath(self, level):
104+
def _dbpath(self, level) -> Path:
107105
r"""
108106
TESTS::
109107
110108
sage: C = ClassicalModularPolynomialDatabase()
111109
sage: C._dbpath(3)
112-
'PolMod/Cls/pol.003.dbz'
110+
PosixPath('PolMod/Cls/pol.003.dbz')
113111
sage: C._dbpath(8)
114-
'PolMod/Cls/pol.008.dbz'
112+
PosixPath('PolMod/Cls/pol.008.dbz')
115113
"""
116-
return "PolMod/%s/pol.%03d.dbz" % (self.model, level)
114+
path = Path("PolMod")
115+
return path / self.model / ("pol.%03d.dbz" % level)
117116

118-
def __repr__(self):
117+
def __repr__(self) -> str:
119118
r"""
120119
EXAMPLES::
121120
@@ -161,7 +160,7 @@ def __getitem__(self, level):
161160
sage: DBMP[50]
162161
Traceback (most recent call last):
163162
...
164-
ValueError: file not found in the Kohel database
163+
FileNotFoundError: file not found in the Kohel database
165164
"""
166165
from sage.rings.integer import Integer
167166
from sage.rings.integer_ring import IntegerRing
@@ -198,16 +197,16 @@ def __getitem__(self, level):
198197

199198

200199
class ModularCorrespondenceDatabase(ModularPolynomialDatabase):
201-
def _dbpath(self, level):
200+
def _dbpath(self, level) -> Path:
202201
r"""
203202
TESTS::
204203
205204
sage: DB = DedekindEtaModularCorrespondenceDatabase()
206205
sage: DB._dbpath((2,4))
207-
'PolMod/EtaCrr/crr.02.004.dbz'
206+
PosixPath('PolMod/EtaCrr/crr.02.004.dbz')
208207
"""
209-
(Nlevel, crrlevel) = level
210-
return "PolMod/%s/crr.%02d.%03d.dbz" % (self.model, Nlevel, crrlevel)
208+
path = Path("PolMod")
209+
return path / self.model / ("crr.%02d.%03d.dbz" % level)
211210

212211

213212
class ClassicalModularPolynomialDatabase(ModularPolynomialDatabase):

src/sage/schemes/elliptic_curves/mod_poly.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,13 @@ def classical_modular_polynomial(l, j=None):
118118

119119
try:
120120
Phi = ZZ['X,Y'](_db[l])
121-
except ValueError:
121+
except (FileNotFoundError, ValueError):
122122
try:
123123
pari_Phi = pari.polmodular(l)
124124
except PariError:
125125
raise NotImplementedError('modular polynomial is not in database and computing it on the fly is not yet implemented')
126-
d = {(i, j): c for i,f in enumerate(pari_Phi) for j, c in enumerate(f)}
126+
d = {(i, j): c for i, f in enumerate(pari_Phi)
127+
for j, c in enumerate(f)}
127128
Phi = ZZ['X,Y'](d)
128129

129130
if l <= _cache_bound:
@@ -140,7 +141,7 @@ def classical_modular_polynomial(l, j=None):
140141
return _cache[l](j, Y)
141142
try:
142143
Phi = _db[l]
143-
except ValueError:
144+
except (ValueError, FileNotFoundError):
144145
pass
145146
else:
146147
if l <= _cache_bound:

0 commit comments

Comments
 (0)