diff options
author | Victor Stinner <vstinner@python.org> | 2023-08-24 21:55:30 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-24 21:55:30 (GMT) |
commit | be436e08b8bd9fcd2202d6ce4d924bba7551e96f (patch) | |
tree | df142099ae50383af0e76ed09f5b4d8d1cc15d3e /Modules/_testcapi/long.c | |
parent | feb9a49c9c09d08cb8c24cb74d90a218de6af244 (diff) | |
download | cpython-be436e08b8bd9fcd2202d6ce4d924bba7551e96f.zip cpython-be436e08b8bd9fcd2202d6ce4d924bba7551e96f.tar.gz cpython-be436e08b8bd9fcd2202d6ce4d924bba7551e96f.tar.bz2 |
gh-108444: Add PyLong_AsInt() public function (#108445)
* Rename _PyLong_AsInt() to PyLong_AsInt().
* Add documentation.
* Add test.
* For now, keep _PyLong_AsInt() as an alias to PyLong_AsInt().
Diffstat (limited to 'Modules/_testcapi/long.c')
-rw-r--r-- | Modules/_testcapi/long.c | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/Modules/_testcapi/long.c b/Modules/_testcapi/long.c index ede43f6..6b74e0a 100644 --- a/Modules/_testcapi/long.c +++ b/Modules/_testcapi/long.c @@ -37,6 +37,9 @@ raise_test_long_error(const char* msg) return raiseTestError("test_long_api", msg); } +// Test PyLong_FromLong()/PyLong_AsLong() +// and PyLong_FromUnsignedLong()/PyLong_AsUnsignedLong(). + #define TESTNAME test_long_api_inner #define TYPENAME long #define F_S_TO_PY PyLong_FromLong @@ -64,6 +67,9 @@ _testcapi_test_long_api_impl(PyObject *module) #undef F_U_TO_PY #undef F_PY_TO_U +// Test PyLong_FromLongLong()/PyLong_AsLongLong() +// and PyLong_FromUnsignedLongLong()/PyLong_AsUnsignedLongLong(). + static PyObject * raise_test_longlong_error(const char* msg) { @@ -595,6 +601,24 @@ _testcapi_call_long_compact_api(PyObject *module, PyObject *arg) return Py_BuildValue("in", is_compact, value); } +/*[clinic input] +_testcapi.PyLong_AsInt + arg: object + / +[clinic start generated code]*/ + +static PyObject * +_testcapi_PyLong_AsInt(PyObject *module, PyObject *arg) +/*[clinic end generated code: output=0df9f19de5fa575b input=9561b97105493a67]*/ +{ + assert(!PyErr_Occurred()); + int value = PyLong_AsInt(arg); + if (value == -1 && PyErr_Occurred()) { + return NULL; + } + return PyLong_FromLong(value); +} + static PyMethodDef test_methods[] = { _TESTCAPI_TEST_LONG_AND_OVERFLOW_METHODDEF _TESTCAPI_TEST_LONG_API_METHODDEF @@ -605,6 +629,7 @@ static PyMethodDef test_methods[] = { _TESTCAPI_TEST_LONG_NUMBITS_METHODDEF _TESTCAPI_TEST_LONGLONG_API_METHODDEF _TESTCAPI_CALL_LONG_COMPACT_API_METHODDEF + _TESTCAPI_PYLONG_ASINT_METHODDEF {NULL}, }; |