3

I'm trying to emulate code.InteractiveInterpreter from the embedded Python C API. I'm using PyEval_Evalcode to evaluate the user input. I am trying to evaluate user input in the interpreter and return the output as a string (just like the interpreter would). However, PyEval_Evalcode returns a multitude of datatypes wrapped in PyObject*. Is there any way to do what I am trying to do?

Constraints: It needs to be done using the embedding api. Cannot be done using PyRun_RunSimpleString() and laying down a code.InteractiveInterpreter.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
emist
  • 137
  • 1
  • 2
  • 8

2 Answers2

3

The object returned by PyEval_Evalcode() can be transformed to a Python string using PyObject_Repr() or PyObject_Str(). The resultant python string can be turned into a regular C string with PyString_AsString().

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
0

I have binary string and cannot return it as string because of null terminated string.

if(PyString_Check(pValue)) { const char* s=/*PyBytes_AsString*/PyString_AsString(PyObject_Repr(pValue)); //return hex representation in ascii int sz=PyString_Size(pValue);//size is valid const char* s= PyString_AsString(pValue);//return only below null terminated string }

mrgloom
  • 20,061
  • 36
  • 171
  • 301