1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
/***************************************************************************
* pythonmodule.cpp
* This file is part of the KDE project
* copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
* You should have received a copy of the GNU Library General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
***************************************************************************/
#include "pythonmodule.h"
#include "pythoninterpreter.h"
#include <QRegExp>
using namespace Kross;
namespace Kross {
/// @internal
class PythonModulePrivate
{
public:
/**
* The \a PythonInterpreter instance this module is
* part of.
*/
PythonInterpreter* const m_interpreter;
#ifdef KROSS_PYTHON_MODULE_CTORDTOR_DEBUG
/// \internal string for debugging.
QString debuginfo;
#endif
PythonModulePrivate(PythonInterpreter* const interpreter)
: m_interpreter(interpreter) {}
};
}
PythonModule::PythonModule(PythonInterpreter* interpreter)
: Py::ExtensionModule<PythonModule>("__main__")
, d(new PythonModulePrivate(interpreter))
{
#ifdef KROSS_PYTHON_MODULE_CTORDTOR_DEBUG
d->debuginfo = QString("name=%1").arg(name().c_str());
krossdebug( QString("PythonModule Ctor %1").arg(d->debuginfo) );
#endif
add_varargs_method("_import", &PythonModule::import, "Kross import hook.");
initialize("The PythonModule is the __main__ python environment used as global object namespace.");
}
PythonModule::~PythonModule()
{
#ifdef KROSS_PYTHON_MODULE_CTORDTOR_DEBUG
krossdebug( QString("PythonModule Dtor %1").arg(d->debuginfo) );
#endif
delete d;
}
Py::Dict PythonModule::getDict()
{
return moduleDictionary();
}
Py::Object PythonModule::import(const Py::Tuple& args)
{
if(args.size() >= 2) {
QString modname = args[1].as_string().c_str();
Py::ExtensionObject<PythonExtension> extobj( args[0] );
PythonExtension* extension = extobj.extensionObject();
Action* action = dynamic_cast< Action* >( extension->object() );
#ifdef KROSS_PYTHON_MODULE_IMPORT_DEBUG
krossdebug( QString("PythonModule::import() module=%1 action=%2").arg(modname).arg(action ? action->objectName() : "NULL") );
#endif
if( action && action->hasObject(modname) ) {
#ifdef KROSS_PYTHON_MODULE_IMPORT_DEBUG
krossdebug( QString("PythonModule::import() module=%1 is internal local child").arg(modname) );
#endif
QObject* object = action->object(modname);
Q_ASSERT(object);
return Py::asObject( new PythonExtension(object) );
}
if(Kross::Manager::self().hasObject(modname)) {
#ifdef KROSS_PYTHON_MODULE_IMPORT_DEBUG
krossdebug( QString("PythonModule::import() module=%1 is internal global child").arg(modname) );
#endif
QObject* object = Kross::Manager::self().object(modname);
Q_ASSERT(object);
return Py::asObject( new PythonExtension(object) );
}
}
return Py::None();
}
|