Skip to content

Commit 4a3e8a0

Browse files
committed
Remove Py2 code.
1 parent 302375b commit 4a3e8a0

File tree

1 file changed

+11
-63
lines changed

1 file changed

+11
-63
lines changed

src/quicktions.pyx

Lines changed: 11 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ __version__ = '1.18'
3030
cimport cython
3131
from cpython.unicode cimport Py_UNICODE_TODECIMAL
3232
from cpython.object cimport Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, Py_GE
33-
from cpython.version cimport PY_MAJOR_VERSION
3433
from cpython.long cimport PyLong_FromString
3534

3635
cdef extern from *:
@@ -221,9 +220,6 @@ cdef _py_gcd(ullong a, ullong b):
221220
return <long> _igcd[ulong](<ulong> a, <ulong> b)
222221
elif b:
223222
a = _igcd[ullong](a, b)
224-
# try PyInt downcast in Py2
225-
if PY_MAJOR_VERSION < 3 and a <= <ullong>LONG_MAX:
226-
return <long>a
227223
return a
228224

229225

@@ -442,7 +438,7 @@ cdef class Fraction:
442438

443439
cdef bint _normalize = True
444440
if denominator is None:
445-
if type(numerator) is int or type(numerator) is long:
441+
if type(numerator) is int:
446442
self._numerator = numerator
447443
self._denominator = 1
448444
return
@@ -464,13 +460,6 @@ cdef class Fraction:
464460
_normalize = False
465461
# fall through to normalisation below
466462

467-
elif PY_MAJOR_VERSION < 3 and isinstance(numerator, bytes):
468-
numerator, denominator, is_normalised = _parse_fraction(
469-
<bytes>numerator, len(<bytes>numerator))
470-
if is_normalised:
471-
_normalize = False
472-
# fall through to normalisation below
473-
474463
elif isinstance(numerator, float) or (
475464
not isinstance(numerator, type) and hasattr(numerator, 'as_integer_ratio')):
476465
# Exact conversion
@@ -498,9 +487,6 @@ cdef class Fraction:
498487
elif type(numerator) is int is type(denominator):
499488
pass # *very* normal case
500489

501-
elif PY_MAJOR_VERSION < 3 and type(numerator) is long is type(denominator):
502-
pass # *very* normal case
503-
504490
elif type(numerator) is Fraction is type(denominator):
505491
numerator, denominator = (
506492
(<Fraction>numerator)._numerator * (<Fraction>denominator)._denominator,
@@ -675,10 +661,8 @@ cdef class Fraction:
675661
"""str(self)"""
676662
if self._denominator == 1:
677663
return str(self._numerator)
678-
elif PY_MAJOR_VERSION > 2:
679-
return f'{self._numerator}/{self._denominator}'
680664
else:
681-
return '%s/%s' % (self._numerator, self._denominator)
665+
return f'{self._numerator}/{self._denominator}'
682666

683667
@cython.final
684668
cdef _format_general(self, dict match):
@@ -1065,23 +1049,6 @@ cdef class Fraction:
10651049

10661050
cdef Py_hash_t result
10671051

1068-
# Py2 and Py3 use completely different hash functions, we provide both
1069-
if PY_MAJOR_VERSION == 2:
1070-
if self._denominator == 1:
1071-
# Get integers right.
1072-
result = hash(self._numerator)
1073-
else:
1074-
# Expensive check, but definitely correct.
1075-
float_val = _as_float(self._numerator, self._denominator)
1076-
if self == float_val:
1077-
result = hash(float_val)
1078-
else:
1079-
# Use tuple's hash to avoid a high collision rate on
1080-
# simple fractions.
1081-
result = hash((self._numerator, self._denominator))
1082-
self._hash = result
1083-
return result
1084-
10851052
# In order to make sure that the hash of a Fraction agrees
10861053
# with the hash of a numerically equal integer, float or
10871054
# Decimal instance, we follow the rules for numeric hashes
@@ -1205,8 +1172,6 @@ cdef class Fraction:
12051172
else:
12061173
# comparisons with complex should raise a TypeError, for consistency
12071174
# with int<->complex, float<->complex, and complex<->complex comparisons.
1208-
if PY_MAJOR_VERSION < 3 and isinstance(other, complex):
1209-
raise TypeError("no ordering relation is defined for complex numbers")
12101175
return NotImplemented
12111176

12121177
if op == Py_LT:
@@ -1520,7 +1485,7 @@ cdef forward(a, b, math_func monomorphic_operator, pyoperator, handle_complex=Tr
15201485
an, ad = (<Fraction>a)._numerator, (<Fraction>a)._denominator
15211486
if type(b) is Fraction:
15221487
return monomorphic_operator(an, ad, (<Fraction>b)._numerator, (<Fraction>b)._denominator)
1523-
elif isinstance(b, (int, long)):
1488+
elif isinstance(b, int):
15241489
return monomorphic_operator(an, ad, b, 1)
15251490
elif isinstance(b, (Fraction, Rational)):
15261491
return monomorphic_operator(an, ad, b.numerator, b.denominator)
@@ -1534,7 +1499,7 @@ cdef forward(a, b, math_func monomorphic_operator, pyoperator, handle_complex=Tr
15341499

15351500
cdef reverse(a, b, math_func monomorphic_operator, pyoperator, handle_complex=True):
15361501
bn, bd = (<Fraction>b)._numerator, (<Fraction>b)._denominator
1537-
if isinstance(a, (int, long)):
1502+
if isinstance(a, int):
15381503
return monomorphic_operator(a, 1, bn, bd)
15391504
elif isinstance(a, Rational):
15401505
return monomorphic_operator(a.numerator, a.denominator, bn, bd)
@@ -1591,8 +1556,6 @@ cdef _raise_invalid_input(s):
15911556
s = repr(s)
15921557
if s[:2] in ('b"', "b'"):
15931558
s = s[1:]
1594-
elif PY_MAJOR_VERSION ==2 and s[:2] in ('u"', "u'"):
1595-
s = s[1:]
15961559
raise ValueError(f'Invalid literal for Fraction: {s}') from None
15971560

15981561

@@ -1605,33 +1568,18 @@ cdef _raise_parse_overflow(s):
16051568

16061569
cdef extern from *:
16071570
"""
1608-
static CYTHON_INLINE int __QUICKTIONS_unpack_string(
1571+
static CYTHON_INLINE int __QUICKTIONS_unpack_ustring(
16091572
PyObject* string, Py_ssize_t *length, void** data, int *kind) {
1610-
if (PyBytes_Check(string)) {
1611-
*kind = 1;
1612-
*length = PyBytes_GET_SIZE(string);
1613-
*data = PyBytes_AS_STRING(string);
1614-
} else {
1615-
#if CYTHON_PEP393_ENABLED
1616-
if (PyUnicode_READY(string) < 0) return -1;
1617-
*kind = PyUnicode_KIND(string);
1618-
*length = PyUnicode_GET_LENGTH(string);
1619-
*data = PyUnicode_DATA(string);
1620-
#else
1621-
*kind = 0;
1622-
*length = PyUnicode_GET_SIZE(string);
1623-
*data = (void*)PyUnicode_AS_UNICODE(string);
1624-
#endif
1625-
}
1573+
if (PyUnicode_READY(string) < 0) return -1;
1574+
*kind = PyUnicode_KIND(string);
1575+
*length = PyUnicode_GET_LENGTH(string);
1576+
*data = PyUnicode_DATA(string);
16261577
return 0;
16271578
}
1628-
#if PY_MAJOR_VERSION < 3
1629-
#define PyUnicode_READ(k, d, i) ((Py_UCS4) ((Py_UNICODE*) d) [i])
1630-
#endif
16311579
#define __QUICKTIONS_char_at(data, kind, index) \
16321580
(((kind == 1) ? (Py_UCS4) ((char*) data)[index] : (Py_UCS4) PyUnicode_READ(kind, data, index)))
16331581
"""
1634-
int _unpack_string "__QUICKTIONS_unpack_string" (
1582+
int _unpack_ustring "__QUICKTIONS_unpack_ustring" (
16351583
object string, Py_ssize_t *length, void **data, int *kind) except -1
16361584
Py_UCS4 _char_at "__QUICKTIONS_char_at" (void *data, int kind, Py_ssize_t index)
16371585
Py_UCS4 PyUnicode_READ(int kind, void *data, Py_ssize_t index)
@@ -1687,7 +1635,7 @@ cdef tuple _parse_fraction(AnyString s, Py_ssize_t s_len):
16871635
cdef char* cdata = NULL
16881636

16891637
if AnyString is unicode:
1690-
_unpack_string(s, &s_len, &s_data, &s_kind)
1638+
_unpack_ustring(s, &s_len, &s_data, &s_kind)
16911639
if s_kind == 1:
16921640
return _parse_fraction(<char*> s_data, s_len)
16931641
cdata = <char*> s_data

0 commit comments

Comments
 (0)