2010-12-31 19:13:28 +01:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
|
|
|
|
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine 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 General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Python.h>
|
|
|
|
|
2010-12-31 21:29:52 +01:00
|
|
|
#include "pythonengine.h"
|
2010-12-31 19:13:28 +01:00
|
|
|
#include "pythonscript.h"
|
2011-04-24 19:02:32 +02:00
|
|
|
#include "core/logging.h"
|
2011-01-17 21:01:16 +01:00
|
|
|
#include "scripting/scriptinfo.h"
|
2011-05-15 15:41:54 +02:00
|
|
|
#include "scripting/scriptinterface.h"
|
2010-12-31 19:13:28 +01:00
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QtDebug>
|
|
|
|
|
|
|
|
|
2011-01-17 21:01:16 +01:00
|
|
|
PythonScript::PythonScript(PythonEngine* engine, const ScriptInfo& info)
|
|
|
|
: Script(engine, info),
|
2011-01-02 03:23:10 +01:00
|
|
|
engine_(engine),
|
2011-05-15 15:41:54 +02:00
|
|
|
module_name_(QString(PythonEngine::kScriptModulePrefix) + "." + info.id())
|
2010-12-31 19:13:28 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PythonScript::Init() {
|
2011-05-15 15:41:54 +02:00
|
|
|
engine_->manager()->AddLogLine("Python",
|
|
|
|
"Loading script file \"" + info().script_file() + "\"", false);
|
2011-01-02 02:07:19 +01:00
|
|
|
|
2011-05-20 01:02:15 +02:00
|
|
|
PythonQt* python_qt = PythonQt::self();
|
|
|
|
|
|
|
|
// Create a module for the script
|
|
|
|
module_ = python_qt->createModuleFromScript(module_name_);
|
|
|
|
|
|
|
|
// Set __path__ - don't use PythonQtObjectPtr::addVariable because it sets
|
|
|
|
// it as a tuple instead of a list.
|
|
|
|
PyObject* __path__ = PyList_New(1);
|
|
|
|
PyList_SetItem(__path__, 0, PyString_FromString(info().path().toLocal8Bit().constData()));
|
|
|
|
PyModule_AddObject(module_, "__path__", __path__);
|
2010-12-31 19:13:28 +01:00
|
|
|
|
2011-05-22 17:23:12 +02:00
|
|
|
// Set __file__
|
|
|
|
module_.addVariable("__file__", info().script_file());
|
|
|
|
|
2011-05-15 15:41:54 +02:00
|
|
|
// Set script object
|
|
|
|
module_.addObject("script", interface());
|
2010-12-31 21:29:52 +01:00
|
|
|
|
2011-05-20 01:02:15 +02:00
|
|
|
// Eval the script
|
|
|
|
PythonQtObjectPtr code = python_qt->parseFile(info().script_file());
|
|
|
|
if (code) {
|
|
|
|
PyObject* dict = PyModule_GetDict(module_);
|
|
|
|
PyObject* r = PyEval_EvalCode((PyCodeObject*)code.object(), dict, dict);
|
|
|
|
if (r) {
|
|
|
|
Py_DECREF(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
PythonQt::self()->handleError();
|
|
|
|
engine_->manager()->AddLogLine("Python", "Failed to create module for script", true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-12-31 19:13:28 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-02 03:23:10 +01:00
|
|
|
bool PythonScript::Unload() {
|
2011-05-22 17:23:12 +02:00
|
|
|
// Remove this module and all its children from sys.modules. That should be
|
|
|
|
// the only place that references it, so this will clean up the modules'
|
|
|
|
// dict and all globals.
|
|
|
|
PyInterpreterState *interp = PyThreadState_GET()->interp;
|
|
|
|
PyObject* modules = interp->modules;
|
|
|
|
|
|
|
|
QStringList keys_to_delete;
|
|
|
|
|
|
|
|
Py_ssize_t pos = 0;
|
|
|
|
PyObject* key;
|
|
|
|
PyObject* value;
|
|
|
|
while (PyDict_Next(modules, &pos, &key, &value)) {
|
|
|
|
const char* name = PyString_AS_STRING(key);
|
|
|
|
if (PyString_Check(key) && PyModule_Check(value)) {
|
|
|
|
if (QString(name).startsWith(module_name_)) {
|
|
|
|
keys_to_delete << name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-01-02 19:10:26 +01:00
|
|
|
|
2011-05-22 17:23:12 +02:00
|
|
|
foreach (const QString& key, keys_to_delete) {
|
|
|
|
// Workaround Python issue 10068 (only affects 2.7.0)
|
|
|
|
_PyModule_Clear(PyDict_GetItemString(modules, key.toAscii().constData()));
|
|
|
|
|
|
|
|
PyDict_DelItemString(modules, key.toAscii().constData());
|
|
|
|
}
|
|
|
|
|
|
|
|
module_ = PythonQtObjectPtr();
|
2010-12-31 19:13:28 +01:00
|
|
|
return true;
|
|
|
|
}
|