0

I need to convert a PyObject* to and int variable in C.
I understand we can parse arguments with PyArg_ParseTuple and that we can build Python object using Py_BuildValue but what is the 'inverse' of Py_BuildValue?

For instance say I have a Python tuple (1, 2) and I'd like to use the content of the tuple to access an array :

int x, y;
PyObject* t_0, t_1;

t_0 = PyTuple_GetItem(tuple_ref, 0);
t_1 = PyTuple_GetItem(tuple_ref, 1);
//t_0 and t_1 are of type PyObject* and I need to use them to access a C array 
//convert t_0 and t_1 to int and save them in x and y, how?
MFranc
  • 366
  • 1
  • 2
  • 14
  • What's wrong with `PyArg_ParseTuple`? I know it's mostly used on function arguments but it doesn't have to be – DavidW Apr 21 '20 at 20:15
  • @DavidW Ah, I really tought I was exclusive for argument parsing. I've figured out there is a ```PyLong_AsLong(PyObject* val)``` function allowing the conversion. If you'd like to write both this comment and yours as an answer I'll gladly accept it and close the question! – MFranc Apr 22 '20 at 14:30

0 Answers0