Skip to content

Commit a59c040

Browse files
Merge pull request #1681 from leftaroundabout/cleanup/remove-python2
Python 3 has been ubiquitous for a long while now, and most of the newer developments around ODL cannot possibly be backported to Python 2. Yet, the ODL codebase still had some small special-case handling for Python 2 (which never trigger anymore) and mentioned distinctions between versions 2 and 3 in the documentation. Both provides at this point only useless clutter, so we should get rid of it.
2 parents 1dc39b6 + 503806e commit a59c040

File tree

7 files changed

+14
-183
lines changed

7 files changed

+14
-183
lines changed

.travis.yml

Lines changed: 0 additions & 103 deletions
This file was deleted.

odl/operator/operator.py

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -95,30 +95,8 @@ def _function_signature(func):
9595
sig : string
9696
Signature of the function.
9797
"""
98-
if sys.version_info.major > 2:
99-
# Python 3 already implements this functionality
100-
return func.__name__ + str(inspect.signature(func))
101-
102-
# In Python 2 we have to do it manually, unfortunately
103-
spec = inspect.getargspec(func)
104-
posargs = spec.args
105-
defaults = spec.defaults if spec.defaults is not None else []
106-
varargs = spec.varargs
107-
kwargs = spec.keywords
108-
deflen = 0 if defaults is None else len(defaults)
109-
nodeflen = 0 if posargs is None else len(posargs) - deflen
110-
111-
args = ['{}'.format(arg) for arg in posargs[:nodeflen]]
112-
args.extend('{}={}'.format(arg, dval)
113-
for arg, dval in zip(posargs[nodeflen:], defaults))
114-
if varargs:
115-
args.append('*{}'.format(varargs))
116-
if kwargs:
117-
args.append('**{}'.format(kwargs))
118-
119-
argstr = ', '.join(args)
120-
121-
return '{}({})'.format(func.__name__, argstr)
98+
assert (sys.version_info.major > 2)
99+
return func.__name__ + str(inspect.signature(func))
122100

123101

124102
def _dispatch_call_args(cls=None, bound_call=None, unbound_call=None,
@@ -128,12 +106,9 @@ def _dispatch_call_args(cls=None, bound_call=None, unbound_call=None,
128106
The ``_call()`` method of `Operator` is allowed to have the
129107
following signatures:
130108
131-
Python 2 and 3:
132109
- ``_call(self, x)``
133110
- ``_call(self, vec, out)``
134111
- ``_call(self, x, out=None)``
135-
136-
Python 3 only:
137112
- ``_call(self, x, *, out=None)`` (``out`` as keyword-only
138113
argument)
139114
@@ -144,7 +119,7 @@ def _dispatch_call_args(cls=None, bound_call=None, unbound_call=None,
144119
argument may have any name.
145120
146121
Additional variable ``**kwargs`` and keyword-only arguments
147-
(Python 3 only) are also allowed.
122+
are also allowed.
148123
149124
Not allowed:
150125
- ``_call(self)`` -- No arguments except instance:
@@ -491,12 +466,9 @@ def _call(self, x, out=None, **kwargs):
491466
492467
The following signatures are allowed:
493468
494-
Python 2 and 3:
495469
- ``_call(self, x)`` --> out-of-place evaluation
496470
- ``_call(self, vec, out)`` --> in-place evaluation
497471
- ``_call(self, x, out=None)`` --> both
498-
499-
Python 3 only:
500472
- ``_call(self, x, *, out=None)`` (``out`` as keyword-only
501473
argument) --> both
502474
@@ -507,7 +479,7 @@ def _call(self, x, out=None, **kwargs):
507479
argument may have any name.
508480
509481
Additional variable ``**kwargs`` and keyword-only arguments
510-
(Python 3 only) are also allowed.
482+
are also allowed.
511483
512484
Notes
513485
-----

odl/set/sets.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,6 @@ def __ne__(self, other):
122122
"""Return ``self != other``."""
123123
return not self.__eq__(other)
124124

125-
def __cmp__(self, other):
126-
"""Comparsion not implemented."""
127-
# Stops python 2 from allowing comparsion of arbitrary objects
128-
raise TypeError('unorderable types: {}, {}'
129-
''.format(self.__class__.__name__, type(other)))
130-
131125
def element(self, inp=None):
132126
"""Return an element from ``inp`` or from scratch.
133127

odl/set/space.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -918,12 +918,6 @@ def __pos__(self):
918918
"""Return ``+self``."""
919919
return self.copy()
920920

921-
def __cmp__(self, other):
922-
"""Comparsion not implemented."""
923-
# Stops python 2 from allowing comparsion of arbitrary objects
924-
raise TypeError('unorderable types: {}, {}'
925-
''.format(self.__class__.__name__, type(other)))
926-
927921
# Metric space method
928922
def __eq__(self, other):
929923
"""Return ``self == other``.

odl/space/npy_tensors.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,13 +1426,6 @@ def __int__(self):
14261426
"""Return ``int(self)``."""
14271427
return int(self.data)
14281428

1429-
def __long__(self):
1430-
"""Return ``long(self)``.
1431-
1432-
This method is only useful in Python 2.
1433-
"""
1434-
return long(self.data)
1435-
14361429
def __float__(self):
14371430
"""Return ``float(self)``."""
14381431
return float(self.data)

odl/test/util/vectorization_test.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,10 @@ def simple_func(x):
164164
assert simple_func(val_1) == 0
165165
assert simple_func(val_2) == 1
166166

167-
# Python 2 really swallows this stuff in comparisons...
168167
bogus_input = [lambda x: x, object, Exception]
169-
if sys.version_info.major > 2:
170-
for b in bogus_input:
171-
with pytest.raises(TypeError):
172-
simple_func(b)
168+
for b in bogus_input:
169+
with pytest.raises(TypeError):
170+
simple_func(b)
173171

174172
# In-place
175173
out = np.empty(5, dtype='int')

odl/util/utility.py

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from collections import OrderedDict
1616
from contextlib import contextmanager
1717
from itertools import product
18+
from functools import lru_cache
1819

1920
import numpy as np
2021

@@ -26,7 +27,6 @@
2627
'array_str',
2728
'dtype_repr',
2829
'dtype_str',
29-
'cache_arguments',
3030
'is_numeric_dtype',
3131
'is_int_dtype',
3232
'is_floating_dtype',
@@ -320,58 +320,41 @@ def dtype_str(dtype):
320320
return '{}'.format(dtype)
321321

322322

323-
def cache_arguments(function):
324-
"""Decorate function to cache the result with given arguments.
325323

326-
This is equivalent to `functools.lru_cache` with Python 3, and currently
327-
does nothing with Python 2 but this may change at some later point.
328-
329-
Parameters
330-
----------
331-
function : `callable`
332-
Function that should be wrapped.
333-
"""
334-
try:
335-
from functools import lru_cache
336-
return lru_cache()(function)
337-
except ImportError:
338-
return function
339-
340-
341-
@cache_arguments
324+
@lru_cache
342325
def is_numeric_dtype(dtype):
343326
"""Return ``True`` if ``dtype`` is a numeric type."""
344327
dtype = np.dtype(dtype)
345328
return np.issubdtype(getattr(dtype, 'base', None), np.number)
346329

347330

348-
@cache_arguments
331+
@lru_cache
349332
def is_int_dtype(dtype):
350333
"""Return ``True`` if ``dtype`` is an integer type."""
351334
dtype = np.dtype(dtype)
352335
return np.issubdtype(getattr(dtype, 'base', None), np.integer)
353336

354337

355-
@cache_arguments
338+
@lru_cache
356339
def is_floating_dtype(dtype):
357340
"""Return ``True`` if ``dtype`` is a floating point type."""
358341
return is_real_floating_dtype(dtype) or is_complex_floating_dtype(dtype)
359342

360343

361-
@cache_arguments
344+
@lru_cache
362345
def is_real_dtype(dtype):
363346
"""Return ``True`` if ``dtype`` is a real (including integer) type."""
364347
return is_numeric_dtype(dtype) and not is_complex_floating_dtype(dtype)
365348

366349

367-
@cache_arguments
350+
@lru_cache
368351
def is_real_floating_dtype(dtype):
369352
"""Return ``True`` if ``dtype`` is a real floating point type."""
370353
dtype = np.dtype(dtype)
371354
return np.issubdtype(getattr(dtype, 'base', None), np.floating)
372355

373356

374-
@cache_arguments
357+
@lru_cache
375358
def is_complex_floating_dtype(dtype):
376359
"""Return ``True`` if ``dtype`` is a complex floating point type."""
377360
dtype = np.dtype(dtype)

0 commit comments

Comments
 (0)