Commit 56956099 authored by Pino Toscano's avatar Pino Toscano
Browse files

scripting: remove char* hacks

The Python C API has been using const char * arguments for the APIs
we use, so there is (hopefully) no more need to use temporary strings.
Also, docs and examples show that static strings are fine.

Hence, remove the hacks used when calling PyImport_AppendInittab,
PyRun_SimpleString, and PyImport_AddModule, and directly pass static
C strings.
parent 67eaa48a
......@@ -373,48 +373,23 @@ public:
dict mainnamespace;
};
// allocates a new string using new [], and copies contents into it..
static char* newstring( const char* contents )
{
char* ret = new char[strlen( contents ) + 1];
strcpy( ret, contents );
return ret;
}
PythonScripter::PythonScripter()
{
d = new Private;
// tell the python interpreter about our API..
// the newstring stuff is to prevent warnings about conversion from
// const char* to char*..
char* s = newstring( "kig" );
PyImport_AppendInittab( s, PyInit_kig );
// we can't delete this yet, since python keeps a pointer to it..
// This means we have a small but harmless memory leak here, but it
// doesn't hurt at all, since it could only be freed at the end of
// the program, at which time it is freed by the system anyway if we
// don't do it..
//delete [] s;
PyImport_AppendInittab( "kig", PyInit_kig );
Py_Initialize();
s = newstring( "import math; from math import *;" );
PyRun_SimpleString( s );
delete [] s;
s = newstring( "import kig; from kig import *;" );
PyRun_SimpleString( s );
delete [] s;
s = newstring( "import traceback;" );
PyRun_SimpleString( s );
delete [] s;
PyRun_SimpleString( "import math; from math import *;" );
PyRun_SimpleString( "import kig; from kig import *;" );
PyRun_SimpleString( "import traceback;" );
// find the main namespace..
s = newstring( "__main__" );
handle<> main_module( borrowed( PyImport_AddModule( s ) ) );
delete [] s;
handle<> main_module( borrowed( PyImport_AddModule( "__main__" ) ) );
handle<> mnh(borrowed( PyModule_GetDict(main_module.get()) ));
d->mainnamespace = extract<dict>( mnh.get() );
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment