lib
pythoninterpreter.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "pythoninterpreter.h"
00021 #include "pythonscript.h"
00022 #include "pythonmodule.h"
00023 #include "pythonsecurity.h"
00024
00025 #include "../api/variant.h"
00026
00027 #include <kglobal.h>
00028 #include <kstandarddirs.h>
00029
00030 #if defined(Q_WS_WIN)
00031 #define PYPATHDELIMITER ";"
00032 #else
00033 #define PYPATHDELIMITER ":"
00034 #endif
00035
00036 extern "C"
00037 {
00047 void* krossinterpreter(Kross::Api::InterpreterInfo* info)
00048 {
00049 try {
00050 return new Kross::Python::PythonInterpreter(info);
00051 }
00052 catch(Kross::Api::Exception::Ptr e) {
00053 Kross::krosswarning("krossinterpreter(Kross::Api::InterpreterInfo* info): Unhandled exception.");
00054 }
00055 return 0;
00056 }
00057 }
00058
00059 using namespace Kross::Python;
00060
00061 namespace Kross { namespace Python {
00062
00064 class PythonInterpreterPrivate
00065 {
00066 public:
00067
00069 PythonModule* mainmodule;
00070
00072 PythonSecurity* security;
00073 };
00074
00075 }}
00076
00077 PythonInterpreter::PythonInterpreter(Kross::Api::InterpreterInfo* info)
00078 : Kross::Api::Interpreter(info)
00079 , d(new PythonInterpreterPrivate())
00080 {
00081
00082 initialize();
00083
00084
00085 Py_SetProgramName(const_cast<char*>("Kross"));
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096 QString path;
00097
00098
00099
00100 Py::Module sysmod( PyImport_ImportModule("sys"), true );
00101 Py::Dict sysmoddict = sysmod.getDict();
00102 Py::Object syspath = sysmoddict.getItem("path");
00103 if(syspath.isList()) {
00104 Py::List syspathlist = syspath;
00105 for(Py::List::iterator it = syspathlist.begin(); it != syspathlist.end(); ++it)
00106 if( (*it).isString() )
00107 path.append( QString(Py::String(*it).as_string().c_str()) + PYPATHDELIMITER );
00108 }
00109 else
00110 path = Py_GetPath();
00111
00112
00113
00114 QStringList krossdirs = KGlobal::dirs()->findDirs("data", "kross/python");
00115 for(QStringList::Iterator krossit = krossdirs.begin(); krossit != krossdirs.end(); ++krossit)
00116 path.append(*krossit + PYPATHDELIMITER);
00117
00118 QStringList appdirs = KGlobal::dirs()->findDirs("appdata", "kross/python");
00119 for(QStringList::Iterator appit = appdirs.begin(); appit != appdirs.end(); ++appit)
00120 path.append(*appit + PYPATHDELIMITER);
00121
00122
00123 PySys_SetPath( (char*) path.latin1() );
00124
00125 krossdebug(QString("Python ProgramName: %1").arg(Py_GetProgramName()));
00126 krossdebug(QString("Python ProgramFullPath: %1").arg(Py_GetProgramFullPath()));
00127 krossdebug(QString("Python Version: %1").arg(Py_GetVersion()));
00128 krossdebug(QString("Python Platform: %1").arg(Py_GetPlatform()));
00129 krossdebug(QString("Python Prefix: %1").arg(Py_GetPrefix()));
00130 krossdebug(QString("Python ExecPrefix: %1").arg(Py_GetExecPrefix()));
00131 krossdebug(QString("Python Path: %1").arg(Py_GetPath()));
00132 krossdebug(QString("Python System Path: %1").arg(path));
00133
00134
00135 d->mainmodule = new PythonModule(this);
00136
00137
00138 Py::Dict moduledict = d->mainmodule->getDict();
00139
00140
00141
00142 QString s =
00143 "import sys\n"
00144
00145
00146
00147 "sys.argv = ['']\n"
00148
00149
00150
00151 "import cStringIO\n"
00152 "sys.stdin = cStringIO.StringIO()\n"
00153
00154
00155
00156 "class Redirect:\n"
00157 " def __init__(self, target):\n"
00158 " self.target = target\n"
00159 " def write(self, s):\n"
00160 " self.target.call(s)\n"
00161
00162
00163
00164
00165
00166 "import __builtin__\n"
00167 "import __main__\n"
00168 "class Importer:\n"
00169 " def __init__(self):\n"
00170 " self.realImporter = __builtin__.__import__\n"
00171 " __builtin__.__import__ = self._import\n"
00172 " def _import(self, name, globals=None, locals=None, fromlist=[]):\n"
00173 " mod = __main__._import(name, globals, locals, fromlist)\n"
00174 " if mod != None: return mod\n"
00175 " return self.realImporter(name, globals, locals, fromlist)\n"
00176 "Importer()\n"
00177 ;
00178
00179 PyObject* pyrun = PyRun_String(s.latin1(), Py_file_input, moduledict.ptr(), moduledict.ptr());
00180 if(! pyrun) {
00181 Py::Object errobj = Py::value(Py::Exception());
00182 throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(QString("Failed to prepare the __main__ module: %1").arg(errobj.as_string().c_str())) );
00183 }
00184 Py_XDECREF(pyrun);
00185
00186
00187 d->security = new PythonSecurity(this);
00188 }
00189
00190 PythonInterpreter::~PythonInterpreter()
00191 {
00192
00193 delete d->security; d->security = 0;
00194
00195 delete d->mainmodule; d->mainmodule = 0;
00196
00197 finalize();
00198
00199 delete d;
00200 }
00201
00202 void PythonInterpreter::initialize()
00203 {
00204
00205 Py_Initialize();
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 }
00223
00224 void PythonInterpreter::finalize()
00225 {
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 Py_Finalize();
00239 }
00240
00241 Kross::Api::Script* PythonInterpreter::createScript(Kross::Api::ScriptContainer* scriptcontainer)
00242 {
00243 return new PythonScript(this, scriptcontainer);
00244 }
00245
00246 PythonModule* PythonInterpreter::mainModule()
00247 {
00248 return d->mainmodule;
00249 }
00250
00251 PythonSecurity* PythonInterpreter::securityModule()
00252 {
00253 return d->security;
00254 }
00255
|