@@ -1209,6 +1209,35 @@ test_dict_api(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
12091209}
12101210
12111211
1212+ static PyObject *
1213+ test_long_api (PyObject * Py_UNUSED (module ), PyObject * Py_UNUSED (args ))
1214+ {
1215+ // test PyLong_AsInt()
1216+ assert (!PyErr_Occurred ());
1217+ PyObject * obj = PyLong_FromLong (123 );
1218+ if (obj == NULL ) {
1219+ return NULL ;
1220+ }
1221+ int value = PyLong_AsInt (obj );
1222+ assert (value == 123 );
1223+ assert (!PyErr_Occurred ());
1224+ Py_DECREF (obj );
1225+
1226+ // test PyLong_AsInt() with overflow
1227+ PyObject * obj2 = PyLong_FromLongLong ((long long )INT_MAX + 1 );
1228+ if (obj2 == NULL ) {
1229+ return NULL ;
1230+ }
1231+ value = PyLong_AsInt (obj2 );
1232+ assert (value == -1 );
1233+ assert (PyErr_ExceptionMatches (PyExc_OverflowError ));
1234+ PyErr_Clear ();
1235+ Py_DECREF (obj2 );
1236+
1237+ Py_RETURN_NONE ;
1238+ }
1239+
1240+
12121241static struct PyMethodDef methods [] = {
12131242 {"test_object" , test_object , METH_NOARGS , _Py_NULL },
12141243 {"test_py_is" , test_py_is , METH_NOARGS , _Py_NULL },
@@ -1234,6 +1263,7 @@ static struct PyMethodDef methods[] = {
12341263 {"test_getattr" , test_getattr , METH_NOARGS , _Py_NULL },
12351264 {"test_getitem" , test_getitem , METH_NOARGS , _Py_NULL },
12361265 {"test_dict_api" , test_dict_api , METH_NOARGS , _Py_NULL },
1266+ {"test_long_api" , test_long_api , METH_NOARGS , _Py_NULL },
12371267 {_Py_NULL , _Py_NULL , 0 , _Py_NULL }
12381268};
12391269
0 commit comments