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
00025 using namespace Kross::Python;
00026
00027 namespace Kross { namespace Python {
00028
00030 class PythonModulePrivate
00031 {
00032 public:
00033
00038 PythonInterpreter* m_interpreter;
00039
00044 QMap<QString, PythonExtension*> m_modules;
00045
00046 };
00047
00048 }}
00049
00050 PythonModule::PythonModule(PythonInterpreter* interpreter)
00051 : Py::ExtensionModule<PythonModule>("__main__")
00052 , d(new PythonModulePrivate())
00053 {
00054 #ifdef KROSS_PYTHON_MODULE_DEBUG
00055 krossdebug( QString("Kross::Python::PythonModule::Constructor") );
00056 #endif
00057
00058 d->m_interpreter = interpreter;
00059
00060 add_varargs_method("_import", &PythonModule::import, "FIXME: Documentation");
00061
00062 initialize("The PythonModule is the __main__ python environment used as global object namespace.");
00063 }
00064
00065 PythonModule::~PythonModule()
00066 {
00067 #ifdef KROSS_PYTHON_MODULE_DEBUG
00068 krossdebug( QString("Kross::Python::PythonModule::Destructor name='%1'").arg(name().c_str()) );
00069 #endif
00070
00071 delete d;
00072 }
00073
00074 Py::Dict PythonModule::getDict()
00075 {
00076 return moduleDictionary();
00077 }
00078
00079 Py::Object PythonModule::import(const Py::Tuple& args)
00080 {
00081 if(args.size() > 0) {
00082 QString modname = args[0].as_string().c_str();
00083 if(modname.startsWith("kross")) {
00084 #ifdef KROSS_PYTHON_MODULE_DEBUG
00085 krossdebug( QString("Kross::Python::PythonModule::import() module=%1").arg(modname) );
00086 #endif
00087 if( modname.find( QRegExp("[^a-zA-Z0-9\\_\\-]") ) >= 0 ) {
00088 krosswarning( QString("Denied import of Kross module '%1' cause of untrusted chars.").arg(modname) );
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 krosswarning( QString("Loading of Kross module '%1' failed.").arg(modname) );
00095 }
00096
00097 }
00098 }
00099 return Py::None();
00100 }
|