lib
pythonmodule.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "pythonmodule.h"
00021 #include "pythoninterpreter.h"
00022
00023 #include <qregexp.h>
00024 #include <kdebug.h>
00025
00026 using namespace Kross::Python;
00027
00028 namespace Kross { namespace Python {
00029
00031 class PythonModulePrivate
00032 {
00033 public:
00034
00039 PythonInterpreter* m_interpreter;
00040
00045 QMap<QString, PythonExtension*> m_modules;
00046
00047 };
00048
00049 }}
00050
00051 PythonModule::PythonModule(PythonInterpreter* interpreter)
00052 : Py::ExtensionModule<PythonModule>("__main__")
00053 , d(new PythonModulePrivate())
00054 {
00055 #ifdef KROSS_PYTHON_MODULE_DEBUG
00056 kdDebug() << QString("Kross::Python::PythonModule::Constructor") << endl;
00057 #endif
00058
00059 d->m_interpreter = interpreter;
00060
00061 add_varargs_method("_import", &PythonModule::import, "FIXME: Documentation");
00062
00063 initialize("The PythonModule is the __main__ python environment used as global object namespace.");
00064 }
00065
00066 PythonModule::~PythonModule()
00067 {
00068 #ifdef KROSS_PYTHON_MODULE_DEBUG
00069 kdDebug() << QString("Kross::Python::PythonModule::Destructor name='%1'").arg(name().c_str()) << endl;
00070 #endif
00071
00072 delete d;
00073 }
00074
00075 Py::Dict PythonModule::getDict()
00076 {
00077 return moduleDictionary();
00078 }
00079
00080 Py::Object PythonModule::import(const Py::Tuple& args)
00081 {
00082 if(args.size() > 0) {
00083 QString modname = args[0].as_string().c_str();
00084 if(modname.startsWith("kross")) {
00085 kdDebug() << QString("Kross::Python::PythonModule::import() module=%1").arg(modname) << endl;
00086
00087 if( modname.find( QRegExp("[^a-zA-Z0-9\\_\\-]") ) >= 0 ) {
00088 kdWarning() << QString("Denied import of Kross module '%1' cause of untrusted chars.").arg(modname) << endl;
00089 }
00090 else {
00091 Kross::Api::Module::Ptr module = Kross::Api::Manager::scriptManager()->loadModule(modname);
00092 if(module)
00093 return PythonExtension::toPyObject( Kross::Api::Object::Ptr(module) );
00094 kdWarning() << QString("Loading of Kross module '%1' failed.").arg(modname) << endl;
00095 }
00096
00097 }
00098 }
00099 return Py::None();
00100 }
|