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 <sip.h>
|
2010-12-31 19:13:28 +01:00
|
|
|
|
2010-12-31 21:29:52 +01:00
|
|
|
#include "pythonengine.h"
|
2010-12-31 19:13:28 +01:00
|
|
|
#include "pythonscript.h"
|
2010-12-31 21:29:52 +01:00
|
|
|
#include "sipAPIclementine.h"
|
2011-01-17 21:01:16 +01:00
|
|
|
#include "scripting/scriptinfo.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-01-17 21:01:16 +01:00
|
|
|
module_name_(QString(PythonEngine::kModulePrefix) + "." + info.id())
|
2010-12-31 19:13:28 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PythonScript::Init() {
|
2011-01-17 21:01:16 +01:00
|
|
|
engine_->AddLogLine("Loading script file \"" + info().script_file() + "\"", false);
|
2011-01-02 02:07:19 +01:00
|
|
|
|
2010-12-31 19:13:28 +01:00
|
|
|
// Open the file
|
2011-01-17 21:01:16 +01:00
|
|
|
QFile file(info().script_file());
|
2010-12-31 19:13:28 +01:00
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
2011-01-02 02:07:19 +01:00
|
|
|
engine_->AddLogLine("Could not open file", true);
|
2010-12-31 19:13:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyEval_AcquireLock();
|
2010-12-31 21:29:52 +01:00
|
|
|
|
2011-01-02 02:07:19 +01:00
|
|
|
// Create a module for this script
|
|
|
|
// TODO: allowed characters?
|
2011-01-02 03:23:10 +01:00
|
|
|
PyObject* module = PyImport_AddModule(module_name_.toAscii().constData());
|
2011-01-02 02:07:19 +01:00
|
|
|
PyObject* dict = PyModule_GetDict(module);
|
2010-12-31 21:29:52 +01:00
|
|
|
|
2011-01-02 02:07:19 +01:00
|
|
|
// Add __builtins__
|
|
|
|
PyObject* builtin_mod = PyImport_ImportModule("__builtin__");
|
|
|
|
PyModule_AddObject(module, "__builtins__", builtin_mod);
|
2010-12-31 21:29:52 +01:00
|
|
|
|
2011-01-02 02:07:19 +01:00
|
|
|
// Set __file__
|
2011-01-17 21:01:16 +01:00
|
|
|
PyModule_AddStringConstant(module, "__file__", info().script_file().toLocal8Bit().constData());
|
2011-01-02 02:07:19 +01:00
|
|
|
|
2011-01-02 02:33:54 +01:00
|
|
|
// Set __path__
|
|
|
|
PyObject* __path__ = PyList_New(1);
|
2011-01-17 21:01:16 +01:00
|
|
|
PyList_SetItem(__path__, 0, PyString_FromString(info().path().toLocal8Bit().constData()));
|
2011-01-02 02:33:54 +01:00
|
|
|
PyModule_AddObject(module, "__path__", __path__);
|
|
|
|
|
2011-01-02 02:07:19 +01:00
|
|
|
// Set script
|
|
|
|
PyObject* script = engine_->sip_api()->api_convert_from_type(
|
|
|
|
interface(), sipType_ScriptInterface, NULL);
|
|
|
|
PyModule_AddObject(module, "script", script);
|
2010-12-31 19:13:28 +01:00
|
|
|
|
|
|
|
// Get a file stream from the file handle
|
|
|
|
FILE* stream = fdopen(file.handle(), "r");
|
|
|
|
|
2011-01-02 02:07:19 +01:00
|
|
|
// Run the script
|
2011-01-02 02:33:54 +01:00
|
|
|
PyObject* result = PyRun_File(stream,
|
2011-01-17 21:01:16 +01:00
|
|
|
info().script_file().toLocal8Bit().constData(), Py_file_input, dict, dict);
|
2011-01-02 02:07:19 +01:00
|
|
|
if (result == NULL) {
|
|
|
|
engine_->AddLogLine("Could not execute file", true);
|
|
|
|
PyErr_Print();
|
2010-12-31 19:13:28 +01:00
|
|
|
PyEval_ReleaseLock();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-02 02:07:19 +01:00
|
|
|
Py_DECREF(result);
|
|
|
|
PyEval_ReleaseLock();
|
2010-12-31 19:13:28 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-02 03:23:10 +01:00
|
|
|
bool PythonScript::Unload() {
|
|
|
|
// 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.
|
|
|
|
PyEval_AcquireLock();
|
|
|
|
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-01 20:03:33 +01:00
|
|
|
|
2011-01-02 03:23:10 +01:00
|
|
|
foreach (const QString& key, keys_to_delete) {
|
2011-01-29 01:57:58 +01:00
|
|
|
// Workaround Python issue 10068 (only affects 2.7.0)
|
|
|
|
_PyModule_Clear(PyDict_GetItemString(modules, key.toAscii().constData()));
|
|
|
|
|
2011-01-02 03:23:10 +01:00
|
|
|
PyDict_DelItemString(modules, key.toAscii().constData());
|
|
|
|
}
|
|
|
|
PyEval_ReleaseLock();
|
2011-01-02 19:10:26 +01:00
|
|
|
|
|
|
|
// Delete any native objects this script created
|
|
|
|
qDeleteAll(native_objects_);
|
2011-01-12 22:20:20 +01:00
|
|
|
native_objects_.clear();
|
2011-01-02 19:10:26 +01:00
|
|
|
|
2010-12-31 19:13:28 +01:00
|
|
|
return true;
|
|
|
|
}
|