C API: Getting PyObject by name

pbienst peter.bienstman at gmail.com
Sat Oct 2 14:47:46 EDT 2010


For reference to posterity, this is how I got it to work in the end:

  PyObject* module = PyImport_ImportModule("__builtin__");
  PyObject* obj = PyRun_String("1", Py_eval_input,
PyModule_GetDict(module), NULL);
  Py_DECREF(module);
  long d = PyLong_AsLong(obj);
  printf("long:%ld\n", d);
  Py_DECREF(obj);

Can't say I really understand it, though, all rather esoteric...

Peter

On Oct 1, 12:16 pm, pbienst <peter.bienst... at gmail.com> wrote:
> Here is what I tried, but it prints out '-1':
>
> PyObject* obj = PyRun_String("1", Py_single_input,
> PyEval_GetGlobals(), PyEval_GetLocals());
> long d = PyLong_AsLong(obj);
> printf("long:%ld\n", d);
> Py_DECREF(obj);
>
> Peter



More information about the Python-list mailing list