lib

pythoninterpreter.cpp

00001 /***************************************************************************
00002  * pythoninterpreter.cpp
00003  * This file is part of the KDE project
00004  * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org)
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this program; see the file COPYING.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  ***************************************************************************/
00019 
00020 #include "pythoninterpreter.h"
00021 #include "pythonscript.h"
00022 #include "pythonmodule.h"
00023 #include "pythonsecurity.h"
00024 //#include "pythonextension.h"
00025 #include "../api/variant.h"
00026 
00027 #include <kglobal.h>
00028 #include <kdebug.h>
00029 #include <kstandarddirs.h>
00030 
00031 #if defined(Q_WS_WIN)
00032   #define PYPATHDELIMITER ";"
00033 #else
00034   #define PYPATHDELIMITER ":"
00035 #endif
00036 
00037 extern "C"
00038 {
00048     void* krossinterpreter(Kross::Api::InterpreterInfo* info)
00049     {
00050         try {
00051             return new Kross::Python::PythonInterpreter(info);
00052         }
00053         catch(Kross::Api::Exception::Ptr e) {
00054             kdWarning() << "krossinterpreter(Kross::Api::InterpreterInfo* info): Unhandled exception." << endl;
00055         }
00056         return 0;
00057     }
00058 }
00059 
00060 using namespace Kross::Python;
00061 
00062 namespace Kross { namespace Python {
00063 
00065     class PythonInterpreterPrivate
00066     {
00067         public:
00068 
00070             PythonModule* mainmodule;
00071 
00073             PythonSecurity* security;
00074     };
00075 
00076 }}
00077 
00078 PythonInterpreter::PythonInterpreter(Kross::Api::InterpreterInfo* info)
00079     : Kross::Api::Interpreter(info)
00080     , d(new PythonInterpreterPrivate())
00081 {
00082     // Initialize the python interpreter.
00083     initialize();
00084 
00085     // Set name of the program.
00086     Py_SetProgramName(const_cast<char*>("Kross"));
00087 
00088     /*
00089     // Set arguments.
00090     //char* comm[0];
00091     const char* comm = const_cast<char*>("kross"); // name.
00092     PySys_SetArgv(1, comm);
00093     */
00094 
00095     // In the python sys.path are all module-directories are
00096     // listed in.
00097     QString path;
00098 
00099     // First import the sys-module to remember it's sys.path
00100     // list in our path QString.
00101     Py::Module sysmod( PyImport_ImportModule("sys"), true );
00102     Py::Dict sysmoddict = sysmod.getDict();
00103     Py::Object syspath = sysmoddict.getItem("path");
00104     if(syspath.isList()) {
00105         Py::List syspathlist = syspath;
00106         for(Py::List::iterator it = syspathlist.begin(); it != syspathlist.end(); ++it)
00107             if( (*it).isString() )
00108                 path.append( QString(Py::String(*it).as_string().c_str()) + PYPATHDELIMITER );
00109     }
00110     else
00111         path = Py_GetPath();
00112 
00113     // Determinate additional module-paths we like to add.
00114     // First add the global Kross modules-path.
00115     QStringList krossdirs = KGlobal::dirs()->findDirs("data", "kross/python");
00116     for(QStringList::Iterator krossit = krossdirs.begin(); krossit != krossdirs.end(); ++krossit)
00117         path.append(*krossit + PYPATHDELIMITER);
00118     // Then add the application modules-path.
00119     QStringList appdirs = KGlobal::dirs()->findDirs("appdata", "kross/python");
00120     for(QStringList::Iterator appit = appdirs.begin(); appit != appdirs.end(); ++appit)
00121         path.append(*appit + PYPATHDELIMITER);
00122 
00123     // Set the extended sys.path.
00124     PySys_SetPath( (char*) path.latin1() );
00125 
00126     kdDebug() << "Python ProgramName: " << Py_GetProgramName() << endl;
00127     kdDebug() << "Python ProgramFullPath: " << Py_GetProgramFullPath() << endl;
00128     kdDebug() << "Python Version: " << Py_GetVersion() << endl;
00129     kdDebug() << "Python Platform: " << Py_GetPlatform() << endl;
00130     kdDebug() << "Python Prefix: " << Py_GetPrefix() << endl;
00131     kdDebug() << "Python ExecPrefix: " << Py_GetExecPrefix() << endl;
00132     kdDebug() << "Python Path: " << Py_GetPath() << endl;
00133     kdDebug() << "Python System Path: " << path << endl;
00134 
00135     // Initialize the main module.
00136     d->mainmodule = new PythonModule(this);
00137 
00138     // The main dictonary.
00139     Py::Dict moduledict = d->mainmodule->getDict();
00140     //TODO moduledict["KrossPythonVersion"] = Py::Int(KROSS_PYTHON_VERSION);
00141 
00142     // Prepare the interpreter.
00143     QString s =
00144         "import sys\n"
00145         //"sys.setdefaultencoding('latin-1')\n"
00146 
00147         // Dirty hack to get sys.argv defined. Needed for e.g. TKinter.
00148         "sys.argv = ['']\n"
00149 
00150         // On the try to read something from stdin always return an empty
00151         // string. That way such reads don't block our script.
00152         "import cStringIO\n"
00153         "sys.stdin = cStringIO.StringIO()\n"
00154 
00155         // Class to redirect something. We use this class e.g. to redirect
00156         // <stdout> and <stderr> to a c++ event.
00157         "class Redirect:\n"
00158         "  def __init__(self, target):\n"
00159         "    self.target = target\n"
00160         "  def write(self, s):\n"
00161         "    self.target.call(s)\n"
00162 
00163         // Wrap builtin __import__ method. All import requests are
00164         // first redirected to our PythonModule.import method and
00165         // if the call returns None, then we call the original
00166         // python import mechanism.
00167         "import __builtin__\n"
00168         "import __main__\n"
00169         "class Importer:\n"
00170         "    def __init__(self):\n"
00171         "        self.realImporter = __builtin__.__import__\n"
00172         "        __builtin__.__import__ = self._import\n"
00173         "    def _import(self, name, globals=None, locals=None, fromlist=[]):\n"
00174         "        mod = __main__._import(name, globals, locals, fromlist)\n"
00175         "        if mod != None: return mod\n"
00176         "        return self.realImporter(name, globals, locals, fromlist)\n"
00177         "Importer()\n"
00178         ;
00179 
00180     PyObject* pyrun = PyRun_String(s.latin1(), Py_file_input, moduledict.ptr(), moduledict.ptr());
00181     if(! pyrun) {
00182         Py::Object errobj = Py::value(Py::Exception()); // get last error
00183         throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(QString("Failed to prepare the __main__ module: %1").arg(errobj.as_string().c_str())) );
00184     }
00185     Py_XDECREF(pyrun); // free the reference.
00186 
00187     // Initialize the RestrictedPython module.
00188     d->security = new PythonSecurity(this);
00189 }
00190 
00191 PythonInterpreter::~PythonInterpreter()
00192 {
00193     // Free the zope security module.
00194     delete d->security; d->security = 0;
00195     // Free the main module.
00196     delete d->mainmodule; d->mainmodule = 0;
00197     // Finalize the python interpreter.
00198     finalize();
00199     // Delete the private d-pointer.
00200     delete d;
00201 }
00202 
00203 void PythonInterpreter::initialize()
00204 {
00205     // Initialize python.
00206     Py_Initialize();
00207 
00208     /* Not needed cause we use the >= Python 2.3 GIL-mechanism.
00209     PyThreadState* d->globalthreadstate, d->threadstate;
00210     // First we have to initialize threading if python supports it.
00211     PyEval_InitThreads();
00212     // The main thread. We don't use it later.
00213     d->globalthreadstate = PyThreadState_Swap(NULL);
00214     d->globalthreadstate = PyEval_SaveThread();
00215     // We use an own sub-interpreter for each thread.
00216     d->threadstate = Py_NewInterpreter();
00217     // Note that this application has multiple threads.
00218     // It maintains a separate interp (sub-interpreter) for each thread.
00219     PyThreadState_Swap(d->threadstate);
00220     // Work done, release the lock.
00221     PyEval_ReleaseLock();
00222     */
00223 }
00224 
00225 void PythonInterpreter::finalize()
00226 {
00227     /* Not needed cause we use the >= Python 2.3 GIL-mechanism.
00228     // Lock threads.
00229     PyEval_AcquireLock();
00230     // Free the used thread.
00231     PyEval_ReleaseThread(d->threadstate);
00232     // Set back to rememberd main thread.
00233     PyThreadState_Swap(d->globalthreadstate);
00234     // Work done, unlock.
00235     PyEval_ReleaseLock();
00236     */
00237 
00238     // Finalize python.
00239     Py_Finalize();
00240 }
00241 
00242 Kross::Api::Script* PythonInterpreter::createScript(Kross::Api::ScriptContainer* scriptcontainer)
00243 {
00244     return new PythonScript(this, scriptcontainer);
00245 }
00246 
00247 PythonModule* PythonInterpreter::mainModule()
00248 {
00249     return d->mainmodule;
00250 }
00251 
00252 PythonSecurity* PythonInterpreter::securityModule()
00253 {
00254     return d->security;
00255 }
00256 
KDE Home | KDE Accessibility Home | Description of Access Keys