Kross::Python::PythonInterpreter Class Reference

#include <pythoninterpreter.h>

Inheritance diagram for Kross::Python::PythonInterpreter:

Inheritance graph
[legend]
Collaboration diagram for Kross::Python::PythonInterpreter:

Collaboration graph
[legend]
List of all members.

Detailed Description

Python interpreter bridge.

Implements an Kross::Api::Interpreter for the python interpreter.

Definition at line 45 of file pythoninterpreter.h.

Public Member Functions

 PythonInterpreter (Kross::Api::InterpreterInfo *info)
virtual ~PythonInterpreter ()
virtual Kross::Api::ScriptcreateScript (Kross::Api::ScriptContainer *scriptcontainer)
PythonModulemainModule ()
PythonSecuritysecurityModule ()

Protected Attributes

InterpreterInfo * m_interpreterinfo
 The InterpreterInfo instance this interpreter belongs to.


Constructor & Destructor Documentation

PythonInterpreter::PythonInterpreter Kross::Api::InterpreterInfo info  ) 
 

Constructor.

Parameters:
info The Kross::Api::InterpreterInfo instance which describes the PythonInterpreter for applications using Kross.

Definition at line 78 of file pythoninterpreter.cpp.

References QString::append().

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 }

PythonInterpreter::~PythonInterpreter  )  [virtual]
 

Destructor.

Definition at line 191 of file pythoninterpreter.cpp.

References Kross::Python::PythonInterpreterPrivate::mainmodule, and Kross::Python::PythonInterpreterPrivate::security.

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 }


Member Function Documentation

Kross::Api::Script * PythonInterpreter::createScript Kross::Api::ScriptContainer scriptcontainer  )  [virtual]
 

Returns:
a PythonScript instance.

Implements Kross::Api::Interpreter.

Definition at line 242 of file pythoninterpreter.cpp.

00243 {
00244     return new PythonScript(this, scriptcontainer);
00245 }

PythonModule * PythonInterpreter::mainModule  ) 
 

Returns:
the MainModule instance.

Definition at line 247 of file pythoninterpreter.cpp.

References Kross::Python::PythonInterpreterPrivate::mainmodule.

00248 {
00249     return d->mainmodule;
00250 }

PythonSecurity * PythonInterpreter::securityModule  ) 
 

Returns:
the PythonSecurity instance.

Definition at line 252 of file pythoninterpreter.cpp.

References Kross::Python::PythonInterpreterPrivate::security.

00253 {
00254     return d->security;
00255 }


The documentation for this class was generated from the following files:
Generated on Thu Feb 9 18:01:29 2006 for Kross by  doxygen 1.4.6