Skip to content

Commit 9b616e3

Browse files
authored
Use PYTHONCAPI_COMPAT_EXPECT_TYPE() (#214)
Avoid "%T" formatting which was was only added to Python 3.13. Move also PyLong_Check() checks to share them between non-limited API and limited API implementations.
1 parent 38fc1ec commit 9b616e3

1 file changed

Lines changed: 11 additions & 35 deletions

File tree

‎pythoncapi_compat.h‎

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,24 +1705,17 @@ PyUnicodeWriter_Format(PyUnicodeWriter *writer, const char *format, ...)
17051705
#if PY_VERSION_HEX < _Py_PACK_VERSION(3, 14) || defined(Py_LIMITED_API)
17061706
static inline int PyLong_GetSign(PyObject *obj, int *sign)
17071707
{
1708-
#ifndef Py_LIMITED_API
17091708
if (!PyLong_Check(obj)) {
1710-
PYTHONCAPI_COMPAT_EXPECT_TYPE("expect int, got ", obj);
1709+
PYTHONCAPI_COMPAT_EXPECT_TYPE("expected int, got ", obj);
17111710
return -1;
17121711
}
17131712

1713+
#ifndef Py_LIMITED_API
17141714
*sign = _PyLong_Sign(obj);
17151715
return 0;
17161716
#else
17171717
PyObject *zero = Py_GetConstant(Py_CONSTANT_ZERO);
1718-
int cmp;
1719-
1720-
if (!PyLong_Check(obj)) {
1721-
PYTHONCAPI_COMPAT_EXPECT_TYPE("expect int, got ", obj);
1722-
return -1;
1723-
}
1724-
1725-
cmp = PyObject_RichCompareBool(obj, zero, Py_GT);
1718+
int cmp = PyObject_RichCompareBool(obj, zero, Py_GT);
17261719
if (cmp > 0) {
17271720
*sign = 1;
17281721
return 0;
@@ -1749,42 +1742,31 @@ static inline int PyLong_GetSign(PyObject *obj, int *sign)
17491742
#if PY_VERSION_HEX < _Py_PACK_VERSION(3, 14) || defined(Py_LIMITED_API)
17501743
static inline int PyLong_IsPositive(PyObject *obj)
17511744
{
1752-
#ifndef Py_LIMITED_API
17531745
if (!PyLong_Check(obj)) {
17541746
PYTHONCAPI_COMPAT_EXPECT_TYPE("expected int, got ", obj);
17551747
return -1;
17561748
}
1749+
1750+
#ifndef Py_LIMITED_API
17571751
return _PyLong_Sign(obj) == 1;
17581752
#else
17591753
PyObject *zero = Py_GetConstant(Py_CONSTANT_ZERO);
1760-
1761-
if (!PyLong_Check(obj)) {
1762-
PyErr_Format(PyExc_TypeError, "expect int, got %R", obj);
1763-
return -1;
1764-
}
1765-
17661754
return PyObject_RichCompareBool(obj, zero, Py_GT);
17671755
#endif
17681756
}
17691757

17701758
static inline int PyLong_IsNegative(PyObject *obj)
17711759
{
1772-
#ifndef Py_LIMITED_API
17731760
if (!PyLong_Check(obj)) {
17741761
PYTHONCAPI_COMPAT_EXPECT_TYPE("expected int, got ", obj);
17751762
return -1;
17761763
}
1764+
1765+
#ifndef Py_LIMITED_API
17771766
return _PyLong_Sign(obj) == -1;
17781767
#else
17791768
PyObject *zero = Py_GetConstant(Py_CONSTANT_ZERO);
1780-
int cmp;
1781-
1782-
if (!PyLong_Check(obj)) {
1783-
PYTHONCAPI_COMPAT_EXPECT_TYPE("expect int, got ", obj);
1784-
return -1;
1785-
}
1786-
1787-
cmp = PyObject_RichCompareBool(obj, zero, Py_LT);
1769+
int cmp = PyObject_RichCompareBool(obj, zero, Py_LT);
17881770
if (cmp > 0) {
17891771
return 1;
17901772
}
@@ -1797,20 +1779,15 @@ static inline int PyLong_IsNegative(PyObject *obj)
17971779

17981780
static inline int PyLong_IsZero(PyObject *obj)
17991781
{
1800-
#ifndef Py_LIMITED_API
18011782
if (!PyLong_Check(obj)) {
18021783
PYTHONCAPI_COMPAT_EXPECT_TYPE("expected int, got ", obj);
18031784
return -1;
18041785
}
1786+
1787+
#ifndef Py_LIMITED_API
18051788
return _PyLong_Sign(obj) == 0;
18061789
#else
18071790
PyObject *zero = Py_GetConstant(Py_CONSTANT_ZERO);
1808-
1809-
if (!PyLong_Check(obj)) {
1810-
PYTHONCAPI_COMPAT_EXPECT_TYPE("expect int, got ", obj);
1811-
return -1;
1812-
}
1813-
18141791
return PyObject_RichCompareBool(obj, zero, Py_EQ);
18151792
#endif
18161793
}
@@ -1870,8 +1847,7 @@ static inline PyObject* PyBytes_Join(PyObject *sep, PyObject *iterable)
18701847
return NULL;
18711848
}
18721849
if (!PyBytes_Check(sep)) {
1873-
PyErr_Format(PyExc_TypeError,
1874-
"sep: expected bytes, got %T", sep);
1850+
PYTHONCAPI_COMPAT_EXPECT_TYPE("sep: expected bytes, got ", sep);
18751851
return NULL;
18761852
}
18771853

0 commit comments

Comments
 (0)