开发者

Embedding Python in C for configuration files

开发者 https://www.devze.com 2023-01-20 18:46 出处:网络
I\'m trying to embed python into c to use it for configuration: If I do it like this: /******************************************************************************

I'm trying to embed python into c to use it for configuration:

If I do it like this:

/******************************************************************************
* 
* Embeding Python Example
*
* To run: 
*   gcc -c test.c -o test.o -I"C:/Python25/include"
*   gcc -o test.exe test.o -L"C:/Python25/libs" -lpython25
*   test.exe
*
******************************************************************************/

#include <Python.h>

int main(int argc, char *argv[])
{
   开发者_Python百科 PyObject *run;
    PyObject *globals = PyDict_New();
    PyObject *locals = PyDict_New();

    Py_Initialize();

    run = PyRun_String("from time import time,ctime\n"
                       "print 'Today is',ctime(time())\n"
                        "test = 5\n"
                        "print test\n", Py_file_input, globals, locals);


    Py_Finalize();
    return 0;
}

I'm getting a runtime error from the the Microsoft VIsual C++ Runtime, with this message:

Exception exceptions.ImportError: '__import__ not found' in 'garbage collection' ignored
Fatal Python error: unexpected exception during garbage collection

What am I doing wrong?


What you're doing wrong is exactly what I was doing wrong.

You are initializing your own globals dictionary to be empty. This means even things like __import__ are not defined for the current scope.

In your simple example you can replace

run = PyRun_String("from time import time,ctime\n"
                   "print 'Today is',ctime(time())\n"
                    "test = 5\n"
                    "print test\n", Py_file_input, globals, locals);

with

PyRun_SimpleString("from time import time,ctime\n"
                   "print 'Today is',ctime(time())\n"
                   "test = 5\n"
                   "print test\n");

You'll probably want to remove globals and locals entirely, too.

If I find out how to access the default globals dict, I'll add a comment or edit here.

Edit: The implementation of PyRun_SimpleStringFlags (Python 2.7.3) looks like this:

int PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
{
    PyObject *m, *d, *v;
    m = PyImport_AddModule("__main__");
    if (m == NULL)
        return -1;
    d = PyModule_GetDict(m);
    v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
    ...

So to get the default globals dict you would have to import __main__ and get its dictionary. This will contain all the default built-in functions, etc.


You're creating 2 Python objects before you've initialized the Python engine.

Also, that's silly. There are plenty of JSON parsers for C.

0

精彩评论

暂无评论...
验证码 换一张
取 消