| 1805 | } |
| 1806 | |
| 1807 | static inline int |
| 1808 | PyLong_Export(PyObject *obj, PyLongExport *export_long) |
| 1809 | { |
| 1810 | if (!PyLong_Check(obj)) { |
| 1811 | memset(export_long, 0, sizeof(*export_long)); |
| 1812 | PyErr_Format(PyExc_TypeError, "expected int, got %s", |
| 1813 | Py_TYPE(obj)->tp_name); |
| 1814 | return -1; |
| 1815 | } |
| 1816 | |
| 1817 | // Fast-path: try to convert to a int64_t |
| 1818 | PyLongObject *self = (PyLongObject*)obj; |
| 1819 | int overflow; |
| 1820 | #if SIZEOF_LONG == 8 |
| 1821 | long value = PyLong_AsLongAndOverflow(obj, &overflow); |
| 1822 | #else |
| 1823 | // Windows has 32-bit long, so use 64-bit long long instead |
| 1824 | long long value = PyLong_AsLongLongAndOverflow(obj, &overflow); |
| 1825 | #endif |
| 1826 | Py_BUILD_ASSERT(sizeof(value) == sizeof(int64_t)); |
| 1827 | // the function cannot fail since obj is a PyLongObject |
| 1828 | assert(!(value == -1 && PyErr_Occurred())); |
| 1829 | |
| 1830 | if (!overflow) { |
| 1831 | export_long->value = value; |
| 1832 | export_long->negative = 0; |
| 1833 | export_long->ndigits = 0; |
| 1834 | export_long->digits = 0; |
| 1835 | export_long->_reserved = 0; |
| 1836 | } |
| 1837 | else { |
| 1838 | export_long->value = 0; |
| 1839 | export_long->negative = _PyLong_Sign(obj) < 0; |
| 1840 | export_long->ndigits = _PyLong_DigitCount(self); |
| 1841 | if (export_long->ndigits == 0) { |
| 1842 | export_long->ndigits = 1; |
| 1843 | } |
| 1844 | export_long->digits = _PyLong_GetDigits(self); |
| 1845 | export_long->_reserved = (Py_uintptr_t)Py_NewRef(obj); |
| 1846 | } |
| 1847 | return 0; |
| 1848 | } |
| 1849 | |
| 1850 | static inline void |
| 1851 | PyLong_FreeExport(PyLongExport *export_long) |
nothing calls this directly
no test coverage detected