@@ -30,7 +30,6 @@ __version__ = '1.18'
30
30
cimport cython
31
31
from cpython.unicode cimport Py_UNICODE_TODECIMAL
32
32
from cpython.object cimport Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, Py_GE
33
- from cpython.version cimport PY_MAJOR_VERSION
34
33
from cpython.long cimport PyLong_FromString
35
34
36
35
cdef extern from * :
@@ -221,9 +220,6 @@ cdef _py_gcd(ullong a, ullong b):
221
220
return < long > _igcd[ulong](< ulong> a, < ulong> b)
222
221
elif b:
223
222
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
227
223
return a
228
224
229
225
@@ -442,7 +438,7 @@ cdef class Fraction:
442
438
443
439
cdef bint _normalize = True
444
440
if denominator is None :
445
- if type (numerator) is int or type (numerator) is long :
441
+ if type (numerator) is int :
446
442
self ._numerator = numerator
447
443
self ._denominator = 1
448
444
return
@@ -464,13 +460,6 @@ cdef class Fraction:
464
460
_normalize = False
465
461
# fall through to normalisation below
466
462
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
-
474
463
elif isinstance (numerator, float ) or (
475
464
not isinstance (numerator, type ) and hasattr (numerator, ' as_integer_ratio' )):
476
465
# Exact conversion
@@ -498,9 +487,6 @@ cdef class Fraction:
498
487
elif type (numerator) is int is type (denominator):
499
488
pass # *very* normal case
500
489
501
- elif PY_MAJOR_VERSION < 3 and type (numerator) is long is type (denominator):
502
- pass # *very* normal case
503
-
504
490
elif type (numerator) is Fraction is type (denominator):
505
491
numerator, denominator = (
506
492
(< Fraction> numerator)._numerator * (< Fraction> denominator)._denominator,
@@ -675,10 +661,8 @@ cdef class Fraction:
675
661
""" str(self)"""
676
662
if self ._denominator == 1 :
677
663
return str (self ._numerator)
678
- elif PY_MAJOR_VERSION > 2 :
679
- return f' {self._numerator}/{self._denominator}'
680
664
else :
681
- return ' %s / %s ' % ( self ._numerator, self ._denominator)
665
+ return f ' { self._numerator}/{ self._denominator} '
682
666
683
667
@cython.final
684
668
cdef _format_general(self , dict match):
@@ -1065,23 +1049,6 @@ cdef class Fraction:
1065
1049
1066
1050
cdef Py_hash_t result
1067
1051
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
-
1085
1052
# In order to make sure that the hash of a Fraction agrees
1086
1053
# with the hash of a numerically equal integer, float or
1087
1054
# Decimal instance, we follow the rules for numeric hashes
@@ -1205,8 +1172,6 @@ cdef class Fraction:
1205
1172
else :
1206
1173
# comparisons with complex should raise a TypeError, for consistency
1207
1174
# 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" )
1210
1175
return NotImplemented
1211
1176
1212
1177
if op == Py_LT:
@@ -1520,7 +1485,7 @@ cdef forward(a, b, math_func monomorphic_operator, pyoperator, handle_complex=Tr
1520
1485
an, ad = (< Fraction> a)._numerator, (< Fraction> a)._denominator
1521
1486
if type (b) is Fraction:
1522
1487
return monomorphic_operator(an, ad, (< Fraction> b)._numerator, (< Fraction> b)._denominator)
1523
- elif isinstance (b, ( int , long ) ):
1488
+ elif isinstance (b, int ):
1524
1489
return monomorphic_operator(an, ad, b, 1 )
1525
1490
elif isinstance (b, (Fraction, Rational)):
1526
1491
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
1534
1499
1535
1500
cdef reverse(a, b, math_func monomorphic_operator, pyoperator, handle_complex = True ):
1536
1501
bn, bd = (< Fraction> b)._numerator, (< Fraction> b)._denominator
1537
- if isinstance (a, ( int , long ) ):
1502
+ if isinstance (a, int ):
1538
1503
return monomorphic_operator(a, 1 , bn, bd)
1539
1504
elif isinstance (a, Rational):
1540
1505
return monomorphic_operator(a.numerator, a.denominator, bn, bd)
@@ -1591,8 +1556,6 @@ cdef _raise_invalid_input(s):
1591
1556
s = repr (s)
1592
1557
if s[:2 ] in (' b"' , " b'" ):
1593
1558
s = s[1 :]
1594
- elif PY_MAJOR_VERSION == 2 and s[:2 ] in (' u"' , " u'" ):
1595
- s = s[1 :]
1596
1559
raise ValueError (f' Invalid literal for Fraction: {s}' ) from None
1597
1560
1598
1561
@@ -1605,33 +1568,18 @@ cdef _raise_parse_overflow(s):
1605
1568
1606
1569
cdef extern from * :
1607
1570
"""
1608
- static CYTHON_INLINE int __QUICKTIONS_unpack_string (
1571
+ static CYTHON_INLINE int __QUICKTIONS_unpack_ustring (
1609
1572
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);
1626
1577
return 0;
1627
1578
}
1628
- #if PY_MAJOR_VERSION < 3
1629
- #define PyUnicode_READ(k, d, i) ((Py_UCS4) ((Py_UNICODE*) d) [i])
1630
- #endif
1631
1579
#define __QUICKTIONS_char_at(data, kind, index) \
1632
1580
(((kind == 1) ? (Py_UCS4) ((char*) data)[index] : (Py_UCS4) PyUnicode_READ(kind, data, index)))
1633
1581
"""
1634
- int _unpack_string " __QUICKTIONS_unpack_string " (
1582
+ int _unpack_ustring " __QUICKTIONS_unpack_ustring " (
1635
1583
object string, Py_ssize_t * length, void ** data, int * kind) except - 1
1636
1584
Py_UCS4 _char_at " __QUICKTIONS_char_at" (void * data, int kind, Py_ssize_t index)
1637
1585
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):
1687
1635
cdef char * cdata = NULL
1688
1636
1689
1637
if AnyString is unicode :
1690
- _unpack_string (s, & s_len, & s_data, & s_kind)
1638
+ _unpack_ustring (s, & s_len, & s_data, & s_kind)
1691
1639
if s_kind == 1 :
1692
1640
return _parse_fraction(< char * > s_data, s_len)
1693
1641
cdata = < char * > s_data
0 commit comments