mirror of
https://github.com/clementine-player/Clementine
synced 2025-01-31 11:35:24 +01:00
Set __path__ properly so scripts can import other modules, and put all scripts in a namespace
This commit is contained in:
parent
c8d8ef208d
commit
7a7597500b
@ -89,6 +89,10 @@ Script* PythonEngine::CreateScript(const QString& path,
|
||||
AddObject(manager()->data().player_, sipType_Player, "player");
|
||||
AddObject(this, sipType_PythonEngine, "pythonengine");
|
||||
|
||||
// Create a module for scripts
|
||||
PyObject* scripts_module = PyImport_AddModule("clementinescripts");
|
||||
Py_DECREF(scripts_module);
|
||||
|
||||
// Run the startup script - this redirects sys.stdout and sys.stderr to our
|
||||
// log handler.
|
||||
QFile python_startup(":pythonstartup.py");
|
||||
|
@ -47,17 +47,21 @@ bool PythonScript::Init() {
|
||||
|
||||
// Create a module for this script
|
||||
// TODO: allowed characters?
|
||||
PyObject* module = PyImport_AddModule(id().toAscii().constData());
|
||||
PyObject* module = PyImport_AddModule(("clementinescripts." + id()).toAscii().constData());
|
||||
PyObject* dict = PyModule_GetDict(module);
|
||||
|
||||
// Add __builtins__
|
||||
PyObject* builtin_mod = PyImport_ImportModule("__builtin__");
|
||||
PyModule_AddObject(module, "__builtins__", builtin_mod);
|
||||
Py_DECREF(builtin_mod);
|
||||
|
||||
// Set __file__
|
||||
PyModule_AddStringConstant(module, "__file__", script_file().toLocal8Bit().constData());
|
||||
|
||||
// Set __path__
|
||||
PyObject* __path__ = PyList_New(1);
|
||||
PyList_SetItem(__path__, 0, PyString_FromString(path().toLocal8Bit().constData()));
|
||||
PyModule_AddObject(module, "__path__", __path__);
|
||||
|
||||
// Set script
|
||||
PyObject* script = engine_->sip_api()->api_convert_from_type(
|
||||
interface(), sipType_ScriptInterface, NULL);
|
||||
@ -67,8 +71,8 @@ bool PythonScript::Init() {
|
||||
FILE* stream = fdopen(file.handle(), "r");
|
||||
|
||||
// Run the script
|
||||
PyObject* result = PyRun_File(
|
||||
stream, script_file().toLocal8Bit().constData(), Py_file_input, dict, dict);
|
||||
PyObject* result = PyRun_File(stream,
|
||||
script_file().toLocal8Bit().constData(), Py_file_input, dict, dict);
|
||||
if (result == NULL) {
|
||||
engine_->AddLogLine("Could not execute file", true);
|
||||
PyErr_Print();
|
||||
|
Loading…
x
Reference in New Issue
Block a user