10

I am trying to embed some python in my pet project. I have reduced my problem to the following code:

#include <Python.h>
#include "iostream"

int main(int argc, char *argv[])
{
    Py_Initialize();

    PyObject *globals = Py_BuildValue("{}");
    PyObject *locals = Py_BuildValue("{}");

    PyObject *string_result = PyRun_StringFlags(
        "a=5\n"
        "s='hello'\n"
        "d=dict()\n"
        ,
        Py_file_input, globals, locals, NULL);
    if ( PyErr_Occurred() ) {PyErr_Print();PyErr_Clear();return 1;}
    return 0;
}

(I know I'm not cleaning up any references. This is an example.)

it can be compiled by

c++ $(python-config --includes) $(python-config --libs) test.cpp -o test

If I run it I get the following error:

$ ./test 
Traceback (most recent call last):
  File "<string>", line 3, in <module>
NameError: name 'dict' is not defined

It seems the the builtin functions aren't loaded. I also cannot import anything. I get that __import__ is missing. How can I load the missing modules or whatever I am missing?

Thanks.

Amro
  • 123,847
  • 25
  • 243
  • 454
Simon
  • 961
  • 2
  • 9
  • 14

2 Answers2

11

One way:

g = PyDict_New();
if (!g)
    return NULL;

PyDict_SetItemString(g, "__builtins__", PyEval_GetBuiltins());

And then pass g as globals.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • Thanks! `PyEval_GetBuiltins` was exactly what I needed. I just didn't know how to find it. – Simon May 21 '12 at 11:15
  • I use this. But got `vars() argument must have __dict__ attribute` error in code `if not 'nickname' in vars(__builtins__):` – user3875388 Oct 13 '20 at 04:11
7

You could also execute the code inside the __main__ module namespace:

PyObject *globals = PyModule_GetDict(PyImport_AddModule("__main__"));
PyObject *obj = PyRun_String("...", Py_file_input, globals, globals);
Py_DECREF(obj);

This is in fact what PyRun_SimpleStringFlags does internally.

Amro
  • 123,847
  • 25
  • 243
  • 454