More Questions on Embedding Python

Gerson Kurz gerson.kurz at t-online.de
Sun Dec 31 17:34:57 CET 2000


1. Question: 

The following script, slightly modified from demo/embed.c, works just
as expected.
-----------------------------------------------------------
static PyObject* gped_OutputDebugString(
	PyObject *self, PyObject *args )
{
    char* buffer = 0;
    if( PyArg_ParseTuple(args, "s", &buffer))
        OutputDebugString(buffer);
    return NULL;
}

static PyMethodDef gped_methods[] = {
    {"OutputDebugString", gped_OutputDebugString, 1},
    {NULL, NULL}
};

bool PythonSupport::Initialize()
{
    Py_Initialize();
    Py_SetProgramName("gped");
    PyObject* pThisModule = PyImport_AddModule("gped");
    Py_InitModule("gped", gped_methods);

    // startup code
    PyRun_SimpleString("import gped, sys\n");
    PyRun_SimpleString("class gped_stdout:\n"
                       "\tdef write(self,str):\n"
                       "\t\tgped.OutputDebugString(str)\n"
                       "\t\treturn 1\n" );
    PyRun_SimpleString("sys.stdout = gped_stdout()\n" );
    PyRun_SimpleString("print(\"Hello, World\")\n" );
    PyRun_SimpleString("print sys.modules.keys()\n" );
    return true;
}
-----------------------------------------------------------
I will see both "Hello, World" and the sys keys. However, if I issue 

    PyRun_SimpleString("sys.stderr = gped_stdout()\n" );

right before the Hello World statement, I get a "SystemError", and DO
NOT see the keys. Can somebody please explain ?

2. Question: 

Say I want to use a Python as a configuration script for my editor.
For example, I would like to have a configure.py that contains the
lines

	gped.MainWindowPos = [0,0,1024,800]

How can I query that list ? If I use PyArg_ParseTuples, I must first
have a parent object to begin with. 

3. Happy New Year!




More information about the Python-list mailing list