i need to convert PyInt to C int. In my code
count=PyInt_FromSsize_t(PyList_Size(pValue))
pValue is a PyObject, PyList. the problem i was having is the PyList_Size is not returning me the correct list size (count is supposed to be 5, but it gave me 6 million), or there is a problem with data types since im in C code interfacing to python scripts. Ideally, i want count to be in a C int type.
i've found python/c APIs that return me long C data types... which is not what i want... anybody can point me to the correct method or APIs??
The PyInt_FromSsize_t()
returns a full-fledged Python int
object sitting in memory and returns its memory address — that is where the 6-million number is coming from. You just want to get the scalar returned by PyList_Size()
and cast it to a C integer, I think:
count = (int) PyList_Size(pValue)
If the list could be very long you might want to think about making count
a long
instead, in which case you could cast to that specific type instead.
Note: a count of -1
means that Python encountered an exception while trying to measure the list length. Here are the docs you should read to know how to handle exceptions: