@@ -1735,100 +1735,31 @@ PyLong_AsLongLongAndOverflow(PyObject *vv, int *overflow)
17351735 return res ;
17361736}
17371737
1738- int
1739- _PyLong_UnsignedShort_Converter (PyObject * obj , void * ptr )
1740- {
1741- unsigned long uval ;
1742-
1743- if (PyLong_Check (obj ) && _PyLong_IsNegative ((PyLongObject * )obj )) {
1744- PyErr_SetString (PyExc_ValueError , "value must be positive" );
1745- return 0 ;
1746- }
1747- uval = PyLong_AsUnsignedLong (obj );
1748- if (uval == (unsigned long )-1 && PyErr_Occurred ())
1749- return 0 ;
1750- if (uval > USHRT_MAX ) {
1751- PyErr_SetString (PyExc_OverflowError ,
1752- "Python int too large for C unsigned short" );
1753- return 0 ;
1754- }
1755-
1756- * (unsigned short * )ptr = Py_SAFE_DOWNCAST (uval , unsigned long , unsigned short );
1757- return 1 ;
1758- }
1759-
1760- int
1761- _PyLong_UnsignedInt_Converter (PyObject * obj , void * ptr )
1762- {
1763- unsigned long uval ;
1764-
1765- if (PyLong_Check (obj ) && _PyLong_IsNegative ((PyLongObject * )obj )) {
1766- PyErr_SetString (PyExc_ValueError , "value must be positive" );
1767- return 0 ;
1768- }
1769- uval = PyLong_AsUnsignedLong (obj );
1770- if (uval == (unsigned long )-1 && PyErr_Occurred ())
1771- return 0 ;
1772- if (uval > UINT_MAX ) {
1773- PyErr_SetString (PyExc_OverflowError ,
1774- "Python int too large for C unsigned int" );
1775- return 0 ;
1776- }
1777-
1778- * (unsigned int * )ptr = Py_SAFE_DOWNCAST (uval , unsigned long , unsigned int );
1779- return 1 ;
1780- }
1781-
1782- int
1783- _PyLong_UnsignedLong_Converter (PyObject * obj , void * ptr )
1784- {
1785- unsigned long uval ;
1786-
1787- if (PyLong_Check (obj ) && _PyLong_IsNegative ((PyLongObject * )obj )) {
1788- PyErr_SetString (PyExc_ValueError , "value must be positive" );
1789- return 0 ;
1790- }
1791- uval = PyLong_AsUnsignedLong (obj );
1792- if (uval == (unsigned long )-1 && PyErr_Occurred ())
1793- return 0 ;
1794-
1795- * (unsigned long * )ptr = uval ;
1796- return 1 ;
1797- }
1798-
1799- int
1800- _PyLong_UnsignedLongLong_Converter (PyObject * obj , void * ptr )
1801- {
1802- unsigned long long uval ;
1803-
1804- if (PyLong_Check (obj ) && _PyLong_IsNegative ((PyLongObject * )obj )) {
1805- PyErr_SetString (PyExc_ValueError , "value must be positive" );
1806- return 0 ;
1807- }
1808- uval = PyLong_AsUnsignedLongLong (obj );
1809- if (uval == (unsigned long long )-1 && PyErr_Occurred ())
1810- return 0 ;
1811-
1812- * (unsigned long long * )ptr = uval ;
1813- return 1 ;
1814- }
1815-
1816- int
1817- _PyLong_Size_t_Converter (PyObject * obj , void * ptr )
1818- {
1819- size_t uval ;
1820-
1821- if (PyLong_Check (obj ) && _PyLong_IsNegative ((PyLongObject * )obj )) {
1822- PyErr_SetString (PyExc_ValueError , "value must be positive" );
1823- return 0 ;
1824- }
1825- uval = PyLong_AsSize_t (obj );
1826- if (uval == (size_t )-1 && PyErr_Occurred ())
1827- return 0 ;
1828-
1829- * (size_t * )ptr = uval ;
1830- return 1 ;
1831- }
1738+ #define UNSIGNED_INT_CONVERTER (NAME , TYPE ) \
1739+ int \
1740+ _PyLong_##NAME##_Converter(PyObject *obj, void *ptr) \
1741+ { \
1742+ Py_ssize_t bytes = PyLong_AsNativeBytes(obj, ptr, sizeof(TYPE), \
1743+ Py_ASNATIVEBYTES_NATIVE_ENDIAN | \
1744+ Py_ASNATIVEBYTES_ALLOW_INDEX | \
1745+ Py_ASNATIVEBYTES_REJECT_NEGATIVE | \
1746+ Py_ASNATIVEBYTES_UNSIGNED_BUFFER); \
1747+ if (bytes < 0) { \
1748+ return 0; \
1749+ } \
1750+ if ((size_t)bytes > sizeof(TYPE)) { \
1751+ PyErr_SetString(PyExc_OverflowError, \
1752+ "Python int too large for C "#TYPE); \
1753+ return 0; \
1754+ } \
1755+ return 1; \
1756+ }
1757+
1758+ UNSIGNED_INT_CONVERTER (UnsignedShort , unsigned short )
1759+ UNSIGNED_INT_CONVERTER (UnsignedInt , unsigned int )
1760+ UNSIGNED_INT_CONVERTER (UnsignedLong , unsigned long )
1761+ UNSIGNED_INT_CONVERTER (UnsignedLongLong , unsigned long long)
1762+ UNSIGNED_INT_CONVERTER (Size_t , size_t )
18321763
18331764
18341765#define CHECK_BINOP (v ,w ) \
0 commit comments