Skip to content

Commit 3234249

Browse files
author
Release Manager
committed
gh-35931: cython-lint: full fix in cpython/ probability/ media/ and sat/ folders <!-- ^^^^^ Please provide a concise, informative and self-explanatory title. Don't put issue numbers in there, do this in the PR body below. For example, instead of "Fixes #1234" use "Introduce new method to calculate 1+1" --> <!-- Describe your changes here in detail --> fixing some cython-lint suggestions in a few folders, mostly in `cpython` <!-- Why is this change required? What problem does it solve? --> <!-- If this PR resolves an open issue, please link to it here. For example "Fixes #12345". --> <!-- If your change requires a documentation PR, please link it appropriately. --> ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> <!-- If your change requires a documentation PR, please link it appropriately --> <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> <!-- Feel free to remove irrelevant items. --> - [x] The title is concise, informative, and self-explanatory. - [x] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation accordingly. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on - #12345: short description why this is a dependency - #34567: ... --> <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> URL: #35931 Reported by: Frédéric Chapoton Reviewer(s): David Coudert
2 parents 48b56d2 + b799c21 commit 3234249

File tree

9 files changed

+62
-60
lines changed

9 files changed

+62
-60
lines changed

src/sage/cpython/atexit.pyx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
"""Utilities for interfacing with the standard library's atexit module."""
44

5-
#*****************************************************************************
5+
# ****************************************************************************
66
# Copyright (C) 2017 Erik M. Bray <[email protected]>
77
#
88
# This program is free software: you can redistribute it and/or modify
99
# it under the terms of the GNU General Public License as published by
1010
# the Free Software Foundation, either version 2 of the License, or
1111
# (at your option) any later version.
12-
# http://www.gnu.org/licenses/
13-
#*****************************************************************************
12+
# https://www.gnu.org/licenses/
13+
# ****************************************************************************
1414

1515
import atexit
1616

@@ -51,7 +51,8 @@ cdef class restore_atexit:
5151
sage: import atexit
5252
sage: from sage.cpython.atexit import restore_atexit
5353
sage: def handler(*args, **kwargs):
54-
....: import sys # see https://github.com/sagemath/sage/issues/25270#comment:56
54+
....: import sys
55+
....: # see https://github.com/sagemath/sage/issues/25270#comment:56
5556
....: sys.stdout.write(str((args, kwargs)))
5657
....: sys.stdout.write('\n')
5758
sage: atexit.register(handler, 1, 2, c=3)
@@ -188,6 +189,7 @@ cdef extern from *:
188189
PyObject* kwargs
189190
atexit_callback** _atexit_callbacks(object module)
190191

192+
191193
def _get_exithandlers():
192194
"""Return list of exit handlers registered with the atexit module."""
193195
cdef atexit_callback ** callbacks
@@ -206,10 +208,11 @@ def _get_exithandlers():
206208
else:
207209
kwargs = {}
208210
exithandlers.append((<object>callback.func,
209-
<object>callback.args,
210-
kwargs))
211+
<object>callback.args,
212+
kwargs))
211213
return exithandlers
212214

215+
213216
def _set_exithandlers(exithandlers):
214217
"""
215218
Replace the list of exit handlers registered with the atexit module

src/sage/cpython/debug.pyx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
"""
22
Various functions to debug Python internals
33
"""
4-
5-
#*****************************************************************************
4+
# ****************************************************************************
65
# Copyright (C) 2017 Jeroen Demeyer <[email protected]>
76
#
87
# This program is free software: you can redistribute it and/or modify
98
# it under the terms of the GNU General Public License as published by
109
# the Free Software Foundation, either version 2 of the License, or
1110
# (at your option) any later version.
12-
# http://www.gnu.org/licenses/
13-
#*****************************************************************************
11+
# https://www.gnu.org/licenses/
12+
# ****************************************************************************
1413

1514
from cpython.object cimport (PyObject, PyTypeObject, Py_TYPE,
16-
descrgetfunc, descrsetfunc)
15+
descrgetfunc, descrsetfunc)
1716

1817
cdef extern from "Python.h":
1918
# Helper to get a pointer to an object's __dict__ slot, if any
@@ -58,6 +57,7 @@ def shortrepr(obj, max=50):
5857

5958
cdef object _no_default = object() # Unique object
6059

60+
6161
def getattr_debug(obj, name, default=_no_default):
6262
r"""
6363
A re-implementation of ``getattr()`` with lots of debugging info.
@@ -153,9 +153,9 @@ def getattr_debug(obj, name, default=_no_default):
153153
dct = <object>(dictptr[0])
154154
print(f" object has __dict__ slot ({type(dct)})")
155155
else:
156-
print(f" object has uninitialized __dict__ slot")
156+
print(" object has uninitialized __dict__ slot")
157157
else:
158-
print(f" object does not have __dict__ slot")
158+
print(" object does not have __dict__ slot")
159159

160160
cdef descrgetfunc get = NULL
161161
cdef descrsetfunc set = NULL
@@ -172,15 +172,15 @@ def getattr_debug(obj, name, default=_no_default):
172172
set = Py_TYPE(attr).tp_descr_set
173173
if get is not NULL:
174174
if set is not NULL:
175-
print(f" attribute is data descriptor (has __get__ and __set__)")
175+
print(" attribute is data descriptor (has __get__ and __set__)")
176176
if dct is not None:
177-
print(f" ignoring __dict__ because we have a data descriptor")
178-
print(f" calling __get__()")
177+
print(" ignoring __dict__ because we have a data descriptor")
178+
print(" calling __get__()")
179179
attr = get(attr, obj, type(obj))
180180
print(f" returning {shortrepr(attr)} ({type(attr)})")
181181
return attr
182182
else:
183-
print(f" attribute is ordinary descriptor (has __get__)")
183+
print(" attribute is ordinary descriptor (has __get__)")
184184
attr_in_class = True
185185
break
186186

@@ -198,22 +198,22 @@ def getattr_debug(obj, name, default=_no_default):
198198

199199
if attr_in_class:
200200
if get is not NULL:
201-
print(f" calling __get__()")
201+
print(" calling __get__()")
202202
attr = get(attr, obj, type(obj))
203203
print(f" returning {shortrepr(attr)} ({type(attr)})")
204204
return attr
205205

206206
try:
207207
tpgetattr = type(obj).__getattr__
208208
except AttributeError:
209-
print(f" class does not have __getattr__")
209+
print(" class does not have __getattr__")
210210
else:
211-
print(f" calling __getattr__()")
211+
print(" calling __getattr__()")
212212
attr = tpgetattr(obj, name)
213213
print(f" returning {shortrepr(attr)} ({type(attr)})")
214214
return attr
215215

216-
print(f" attribute not found")
216+
print(" attribute not found")
217217
raise AttributeError(AttributeErrorMessage(obj, name))
218218

219219

src/sage/cpython/dict_del_by_value.pyx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,13 @@ cdef int del_dictitem_by_exact_value(PyDictObject *mp, PyObject *value, Py_hash_
160160
ep.me_value = NULL
161161
mp.ma_used -= 1
162162
dictkeys_set_index(keys, i, DKIX_DUMMY)
163-
#We have transferred the to-be-deleted references to the list T
164-
#we now delete the list so that the actual decref happens through a
165-
#deallocation routine that uses the Python Trashcan macros to
166-
#avoid stack overflow in deleting deep structures.
163+
# We have transferred the to-be-deleted references to the list T
164+
# we now delete the list so that the actual decref happens through a
165+
# deallocation routine that uses the Python Trashcan macros to
166+
# avoid stack overflow in deleting deep structures.
167167
del T
168168

169+
169170
def test_del_dictitem_by_exact_value(D, value, h):
170171
"""
171172
This function helps testing some cdef function used to delete dictionary items.

src/sage/cpython/string.pxd

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
#*****************************************************************************
1+
# ****************************************************************************
22
# Copyright (C) 2017 Erik M. Bray <[email protected]>
33
#
44
# This program is free software: you can redistribute it and/or modify
55
# it under the terms of the GNU General Public License as published by
66
# the Free Software Foundation, either version 2 of the License, or
77
# (at your option) any later version.
8-
# http://www.gnu.org/licenses/
9-
#*****************************************************************************
10-
11-
8+
# https://www.gnu.org/licenses/
9+
# ****************************************************************************
1210

1311
cdef extern from "string_impl.h":
1412
str _cstr_to_str(const char* c, encoding, errors)

src/sage/cpython/string.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ Check that this can be used outside of Sage (see :trac:`25549`)::
1414
hello world!
1515
"""
1616

17-
#*****************************************************************************
17+
# ****************************************************************************
1818
# Copyright (C) 2017 Erik M. Bray <[email protected]>
1919
#
2020
# This program is free software: you can redistribute it and/or modify
2121
# it under the terms of the GNU General Public License as published by
2222
# the Free Software Foundation, either version 2 of the License, or
2323
# (at your option) any later version.
24-
# http://www.gnu.org/licenses/
25-
#*****************************************************************************
24+
# https://www.gnu.org/licenses/
25+
# ****************************************************************************
2626

2727
import sys
2828

src/sage/cpython/wrapperdescr.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,6 @@ cdef wrapperdescr_fastcall(wrapper_descriptor slotwrapper, self, args, kwds):
9797

9898
if <PyObject*>kwds is not NULL and kwds:
9999
raise TypeError(f"wrapper {bytes_to_str(slotdef.name)} slotdef "
100-
"doesn't take keyword arguments")
100+
"doesn't take keyword arguments")
101101

102102
return slotdef.wrapper(self, args, slotwrapper.d_wrapped)

src/sage/media/channels.pyx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"_separate_channels"
22

3+
34
def _separate_channels(_data, _width, _nchannels):
45
"""
5-
Separates the channels. This is an internal helper method for
6+
Separate the channels. This is an internal helper method for
67
precomputing some data while initializing the wav object.
78
"""
89
cdef int nchannels = _nchannels
@@ -11,21 +12,21 @@ def _separate_channels(_data, _width, _nchannels):
1112
cdef int n
1213
cdef short int x
1314

14-
cdef int l = len(_data) / (width)
15+
cdef int ell = len(_data) // width
1516

1617
channel_data = [[] for i in range(nchannels)]
1718
if width == 1:
1819
# handle the one byte case
1920

20-
for n in range(l):
21-
channel_data[n % nchannels].append(ord(data[n])-127)
21+
for n in range(ell):
22+
channel_data[n % nchannels].append(ord(data[n]) - 127)
2223

2324
elif width == 2:
24-
a = 32768
25-
for n in range(l):
25+
# a = 32768
26+
for n in range(ell):
2627
# compute the value as an integer
27-
x = <int> (data[2*n]) + 256 * <int>(data[2*n + 1])
28-
#x -= 65536*(x > a)
28+
x = <int> (data[2*n]) + 256 * <int>(data[2 * n + 1])
29+
# x -= 65536*(x > a)
2930
channel_data[n % nchannels].append(x)
3031
else:
3132
raise NotImplementedError("greater than 16-bit wavs not supported")

src/sage/probability/probability_distribution.pyx

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ cdef class ProbabilityDistribution:
140140
2.0]
141141
"""
142142
import pylab
143-
l = [float(self.get_random_element()) for _ in range(num_samples)]
144-
S = pylab.hist(l, bins, density=True)
143+
ell = [float(self.get_random_element()) for _ in range(num_samples)]
144+
S = pylab.hist(ell, bins, density=True)
145145
return [list(S[0]), list(S[1])]
146146

147147
def generate_histogram_plot(self, name, num_samples = 1000, bins = 50):
@@ -171,8 +171,8 @@ cdef class ProbabilityDistribution:
171171
....: X.generate_histogram_plot(f.name)
172172
"""
173173
import pylab
174-
l = [float(self.get_random_element()) for _ in range(num_samples)]
175-
pylab.hist(l, bins, density=True)
174+
ell = [float(self.get_random_element()) for _ in range(num_samples)]
175+
pylab.hist(ell, bins, density=True)
176176
pylab.savefig(name)
177177

178178

@@ -309,7 +309,7 @@ cdef class SphericalDistribution(ProbabilityDistribution):
309309
gsl_ran_dir_nd(self.r, self.dimension, self.vec)
310310
for i in range(self.dimension):
311311
v[i] = self.vec[i]
312-
return vector(sage.rings.real_double.RDF, v) #This could be made more efficient by directly constructing the vector, TODO.
312+
return vector(sage.rings.real_double.RDF, v) # This could be made more efficient by directly constructing the vector, TODO.
313313

314314
def reset_distribution(self):
315315
"""
@@ -542,11 +542,12 @@ cdef class RealDistribution(ProbabilityDistribution):
542542
cdef double* parameters
543543
cdef long int seed
544544
cdef object name
545-
#cdef double (*generator_1)(gsl_rng*)
546-
#cdef double (*generator_2)(gsl_rng*, double)
547-
#cdef _get_random_element_c(self)
545+
# cdef double (*generator_1)(gsl_rng*)
546+
# cdef double (*generator_2)(gsl_rng*, double)
547+
# cdef _get_random_element_c(self)
548548

549-
def __init__(self, type = 'uniform', parameters = [], rng = 'default', seed = None):
549+
def __init__(self, type='uniform', parameters=None,
550+
rng='default', seed=None):
550551
r"""
551552
EXAMPLES::
552553
@@ -571,7 +572,6 @@ cdef class RealDistribution(ProbabilityDistribution):
571572
sage: one == three
572573
False
573574
"""
574-
575575
gsl_rng_env_setup()
576576
self.parameters = NULL
577577
self.set_random_number_generator(rng)
@@ -681,7 +681,7 @@ cdef class RealDistribution(ProbabilityDistribution):
681681

682682
return sage.rings.real_double.RDF(result)
683683

684-
def set_distribution(self, name = 'uniform', parameters = []):
684+
def set_distribution(self, name='uniform', parameters=None):
685685
"""
686686
This method can be called to change the current probability distribution.
687687
@@ -693,6 +693,9 @@ cdef class RealDistribution(ProbabilityDistribution):
693693
"""
694694
sig_free(self.parameters)
695695

696+
if parameters is None:
697+
parameters = []
698+
696699
if name == 'uniform':
697700
self.distribution_type = uniform
698701
for x in parameters:
@@ -809,7 +812,7 @@ cdef class RealDistribution(ProbabilityDistribution):
809812

810813
self.name = name
811814

812-
#def _get_random_element_c():
815+
# def _get_random_element_c():
813816

814817
def reset_distribution(self):
815818
"""
@@ -1028,7 +1031,6 @@ cdef class GeneralDiscreteDistribution(ProbabilityDistribution):
10281031
...
10291032
ValueError: The distribution probabilities must be non-negative
10301033
"""
1031-
10321034
cdef gsl_rng_type * T
10331035
cdef gsl_rng * r
10341036
cdef gsl_ran_discrete_t *dist
@@ -1070,7 +1072,6 @@ cdef class GeneralDiscreteDistribution(ProbabilityDistribution):
10701072
sage: X = GeneralDiscreteDistribution([1,2,2^1024])
10711073
sage: Counter(X.get_random_element() for _ in range(100))
10721074
Counter({2: 100})
1073-
10741075
"""
10751076
gsl_rng_env_setup()
10761077
self.set_random_number_generator(rng)
@@ -1093,7 +1094,7 @@ cdef class GeneralDiscreteDistribution(ProbabilityDistribution):
10931094
for i in range(n):
10941095
if P[i] < 0:
10951096
raise ValueError("The distribution probabilities must "
1096-
"be non-negative")
1097+
"be non-negative")
10971098
P_vec[i] = P[i]
10981099

10991100
self.dist = gsl_ran_discrete_preproc(n, P_vec)
@@ -1111,7 +1112,6 @@ cdef class GeneralDiscreteDistribution(ProbabilityDistribution):
11111112
sage: X.get_random_element()
11121113
1
11131114
"""
1114-
11151115
gsl_rng_set(self.r, seed)
11161116
self.seed = seed
11171117

@@ -1124,7 +1124,6 @@ cdef class GeneralDiscreteDistribution(ProbabilityDistribution):
11241124
sage: X = GeneralDiscreteDistribution([0.3, 0.4, 0.3])
11251125
sage: X.set_random_number_generator('taus')
11261126
"""
1127-
11281127
if rng == 'default':
11291128
self.T = gsl_rng_default
11301129
elif rng == 'luxury':

src/sage/sat/solvers/satsolver.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ cdef class SatSolver:
158158
if line.startswith("x"):
159159
line = line[1:].split(" ")
160160
clause = [int(e) for e in line if e]
161-
clause = clause[:-1] # strip trailing zero
161+
clause = clause[:-1] # strip trailing zero
162162
try:
163163
self.add_xor_clause(clause)
164164
except AttributeError:

0 commit comments

Comments
 (0)