2 * Copyright (C) 2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #if (defined HAVE_CONFIG_H) && (!defined TARGET_WINDOWS)
25 // python.h should always be included first before any other includes
30 #include "PythonInvoker.h"
31 #include "Application.h"
32 #include "ApplicationMessenger.h"
33 #include "addons/AddonManager.h"
34 #include "dialogs/GUIDialogKaiToast.h"
35 #include "filesystem/File.h"
36 #include "filesystem/SpecialProtocol.h"
37 #include "guilib/GraphicContext.h"
38 #include "guilib/GUIWindowManager.h"
39 #include "interfaces/legacy/Addon.h"
40 #include "interfaces/python/LanguageHook.h"
41 #include "interfaces/python/PyContext.h"
42 #include "interfaces/python/pythreadstate.h"
43 #include "interfaces/python/swig.h"
44 #include "interfaces/python/XBPython.h"
45 #include "threads/SingleLock.h"
46 #if defined(TARGET_WINDOWS)
47 #include "utils/CharsetConverter.h"
48 #endif // defined(TARGET_WINDOWS)
49 #include "utils/log.h"
50 #include "utils/URIUtils.h"
53 extern "C" FILE *fopen_utf8(const char *_Filename, const char *_Mode);
55 #define fopen_utf8 fopen
62 #define PY_PATH_SEP DELIM
64 // Time before ill-behaved scripts are terminated
65 #define PYTHON_SCRIPT_TIMEOUT 5000 // ms
68 using namespace XFILE;
72 int xbp_chdir(const char *dirname);
73 char* dll_getenv(const char* szKey);
76 static const CStdString getListOfAddonClassesAsString(XBMCAddon::AddonClass::Ref<XBMCAddon::Python::LanguageHook>& languageHook)
79 XBMCAddon::AddonClass::Synchronize l(*(languageHook.get()));
80 std::set<XBMCAddon::AddonClass*>& acs = languageHook->GetRegisteredAddonClasses();
81 bool firstTime = true;
82 for (std::set<XBMCAddon::AddonClass*>::iterator iter = acs.begin(); iter != acs.end(); ++iter)
88 message += (*iter)->GetClassname().c_str();
94 CPythonInvoker::CPythonInvoker(ILanguageInvocationHandler *invocationHandler)
95 : ILanguageInvoker(invocationHandler),
96 m_source(NULL), m_argc(0), m_argv(NULL),
97 m_threadState(NULL), m_stop(false)
100 CPythonInvoker::~CPythonInvoker()
102 // nothing to do for the default invoker used for registration with the
103 // CScriptInvocationManager
107 if (GetState() < InvokerStateDone)
108 CLog::Log(LOGDEBUG, "CPythonInvoker(%d): waiting for python thread \"%s\" to stop",
109 GetId(), (m_source != NULL ? m_source : "unknown script"));
111 g_pythonParser.PulseGlobalEvent();
116 for (unsigned int i = 0; i < m_argc; i++)
120 g_pythonParser.FinalizeScript();
123 bool CPythonInvoker::Execute(const std::string &script, const std::vector<std::string> &arguments /* = std::vector<std::string>() */)
128 if (!CFile::Exists(script))
130 CLog::Log(LOGERROR, "CPythonInvoker(%d): python script \"%s\" does not exist", GetId(), CSpecialProtocol::TranslatePath(script).c_str());
134 if (!g_pythonParser.InitializeEngine())
137 return ILanguageInvoker::Execute(script, arguments);
140 bool CPythonInvoker::execute(const std::string &script, const std::vector<std::string> &arguments)
142 // copy the code/script into a local string buffer
143 #ifdef TARGET_WINDOWS
144 CStdString strsrc = script;
145 g_charsetConverter.utf8ToSystem(strsrc);
146 m_source = new char[strsrc.length() + 1];
147 strcpy(m_source, strsrc);
149 m_source = new char[script.length() + 1];
150 strcpy(m_source, script.c_str());
153 // copy the arguments into a local buffer
154 m_argc = arguments.size();
155 m_argv = new char*[m_argc];
156 for (unsigned int i = 0; i < m_argc; i++)
158 m_argv[i] = new char[arguments.at(i).length() + 1];
159 strcpy(m_argv[i], arguments.at(i).c_str());
162 CLog::Log(LOGDEBUG, "CPythonInvoker(%d, %s): start processing", GetId(), m_source);
163 int m_Py_file_input = Py_file_input;
165 // get the global lock
166 PyEval_AcquireLock();
167 PyThreadState* state = Py_NewInterpreter();
170 PyEval_ReleaseLock();
171 CLog::Log(LOGERROR, "CPythonInvoker(%d, %s): FAILED to get thread state!", GetId(), m_source);
174 // swap in my thread state
175 PyThreadState_Swap(state);
177 XBMCAddon::AddonClass::Ref<XBMCAddon::Python::LanguageHook> languageHook(new XBMCAddon::Python::LanguageHook(state->interp));
178 languageHook->RegisterMe();
180 g_pythonParser.InitializeInterpreter(m_addon);
181 setState(InvokerStateInitialized);
183 CLog::Log(LOGDEBUG, "CPythonInvoker(%d, %s): the source file to load is %s", GetId(), m_source, m_source);
185 // get path from script file name and add python path's
186 // this is used for python so it will search modules from script path first
187 CStdString scriptDir;
188 URIUtils::GetDirectory(CSpecialProtocol::TranslatePath(m_source), scriptDir);
189 URIUtils::RemoveSlashAtEnd(scriptDir);
192 // add on any addon modules the user has installed
193 ADDON::VECADDONS addons;
194 ADDON::CAddonMgr::Get().GetAddons(ADDON::ADDON_SCRIPT_MODULE, addons);
195 for (unsigned int i = 0; i < addons.size(); ++i)
196 addPath(CSpecialProtocol::TranslatePath(addons[i]->LibPath()));
198 // we want to use sys.path so it includes site-packages
199 // if this fails, default to using Py_GetPath
200 PyObject *sysMod(PyImport_ImportModule((char*)"sys")); // must call Py_DECREF when finished
201 PyObject *sysModDict(PyModule_GetDict(sysMod)); // borrowed ref, no need to delete
202 PyObject *pathObj(PyDict_GetItemString(sysModDict, "path")); // borrowed ref, no need to delete
204 if (pathObj != NULL && PyList_Check(pathObj))
206 for (int i = 0; i < PyList_Size(pathObj); i++)
208 PyObject *e = PyList_GetItem(pathObj, i); // borrowed ref, no need to delete
209 if (e != NULL && PyString_Check(e))
210 addPath(PyString_AsString(e)); // returns internal data, don't delete or modify
214 addPath(Py_GetPath());
216 Py_DECREF(sysMod); // release ref to sysMod
218 // set current directory and python's path.
220 PySys_SetArgv(m_argc, m_argv);
222 CLog::Log(LOGDEBUG, "CPythonInvoker(%d, %s): setting the Python path to %s", GetId(), m_source, m_pythonPath.c_str());
223 PySys_SetPath((char *)m_pythonPath.c_str());
225 CLog::Log(LOGDEBUG, "CPythonInvoker(%d, %s): entering source directory %s", GetId(), m_source, scriptDir.c_str());
226 PyObject* module = PyImport_AddModule((char*)"__main__");
227 PyObject* moduleDict = PyModule_GetDict(module);
229 // when we are done initing we store thread state so we can be aborted
230 PyThreadState_Swap(NULL);
231 PyEval_ReleaseLock();
233 // we need to check if we was asked to abort before we had inited
234 bool stopping = false;
235 { CSingleLock lock(m_critical);
236 m_threadState = state;
240 PyEval_AcquireLock();
241 PyThreadState_Swap(state);
248 // run script from file
249 // We need to have python open the file because on Windows the DLL that python
250 // is linked against may not be the DLL that xbmc is linked against so
251 // passing a FILE* to python from an fopen has the potential to crash.
252 PyObject* file = PyFile_FromString((char *) CSpecialProtocol::TranslatePath(m_source).c_str(), (char*)"r");
253 FILE *fp = PyFile_AsFile(file);
257 PyObject *f = PyString_FromString(CSpecialProtocol::TranslatePath(m_source).c_str());
258 PyDict_SetItemString(moduleDict, "__file__", f);
260 if (m_addon.get() != NULL)
262 PyObject *pyaddonid = PyString_FromString(m_addon->ID().c_str());
263 PyDict_SetItemString(moduleDict, "__xbmcaddonid__", pyaddonid);
265 CStdString version = ADDON::GetXbmcApiVersionDependency(m_addon);
266 PyObject *pyxbmcapiversion = PyString_FromString(version.c_str());
267 PyDict_SetItemString(moduleDict, "__xbmcapiversion__", pyxbmcapiversion);
269 CLog::Log(LOGDEBUG, "CPythonInvoker(%d, %s): instantiating addon using automatically obtained id of \"%s\" dependent on version %s of the xbmc.python api",
270 GetId(), m_source, m_addon->ID().c_str(), version.c_str());
274 setState(InvokerStateRunning);
275 XBMCAddon::Python::PyContext pycontext; // this is a guard class that marks this callstack as being in a python context
276 PyRun_FileExFlags(fp, CSpecialProtocol::TranslatePath(m_source).c_str(), m_Py_file_input, moduleDict, moduleDict,1,NULL);
279 CLog::Log(LOGERROR, "CPythonInvoker(%d, %s): %s not found!", GetId(), m_source, m_source);
281 catch (const XbmcCommons::Exception& e)
283 setState(InvokerStateFailed);
289 setState(InvokerStateFailed);
290 CLog::Log(LOGERROR, "CPythonInvoker(%d, %s): failure in script", GetId(), m_source);
295 bool systemExitThrown = false;
296 if (!failed && !PyErr_Occurred())
298 CLog::Log(LOGINFO, "CPythonInvoker(%d, %s): script successfully run", GetId(), m_source);
299 setState(InvokerStateDone);
301 else if (PyErr_ExceptionMatches(PyExc_SystemExit))
303 systemExitThrown = true;
304 CLog::Log(LOGINFO, "CPythonInvoker(%d, %s): script aborted", GetId(), m_source);
305 setState(InvokerStateFailed);
309 setState(InvokerStateFailed);
311 // if it failed with an exception we already logged the details
314 PythonBindings::PythonToCppException e;
319 CPyThreadState releaseGil;
320 CSingleLock gc(g_graphicsContext);
322 CGUIDialogKaiToast *pDlgToast = (CGUIDialogKaiToast*)g_windowManager.GetWindow(WINDOW_DIALOG_KAI_TOAST);
323 if (pDlgToast != NULL)
327 if (m_addon.get() != NULL)
328 script = m_addon->Name();
332 URIUtils::Split(m_source, path, script);
333 if (script.Equals("default.py"))
336 URIUtils::RemoveSlashAtEnd(path);
337 URIUtils::Split(path, path2, script);
341 desc.Format(g_localizeStrings.Get(2100), script);
342 pDlgToast->QueueNotification(CGUIDialogKaiToast::Error, g_localizeStrings.Get(257), desc);
347 // no need to do anything else because the script has already stopped
351 PyObject *m = PyImport_AddModule((char*)"xbmc");
352 if (m == NULL || PyObject_SetAttrString(m, (char*)"abortRequested", PyBool_FromLong(1)))
353 CLog::Log(LOGERROR, "CPythonInvoker(%d, %s): failed to set abortRequested", GetId(), m_source);
355 // make sure all sub threads have finished
356 for (PyThreadState* s = state->interp->tstate_head, *old = NULL; s;)
365 CLog::Log(LOGINFO, "CPythonInvoker(%d, %s): waiting on thread %"PRIu64, GetId(), m_source, (uint64_t)s->thread_id);
369 CPyThreadState pyState;
373 s = state->interp->tstate_head;
376 // pending calls must be cleared out
377 XBMCAddon::RetardedAsynchCallbackHandler::clearPendingCalls(state);
379 PyThreadState_Swap(NULL);
380 PyEval_ReleaseLock();
382 // set stopped event - this allows ::stop to run and kill remaining threads
383 // this event has to be fired without holding m_critical
384 // also the GIL (PyEval_AcquireLock) must not be held
385 // if not obeyed there is still no deadlock because ::stop waits with timeout (smart one!)
386 m_stoppedEvent.Set();
388 { CSingleLock lock(m_critical);
389 m_threadState = NULL;
392 PyEval_AcquireLock();
393 PyThreadState_Swap(state);
395 g_pythonParser.DeInitializeInterpreter();
397 // run the gc before finishing
399 // if the script exited by throwing a SystemExit excepton then going back
400 // into the interpreter causes this python bug to get hit:
401 // http://bugs.python.org/issue10582
402 // and that causes major failures. So we are not going to go back in
403 // to run the GC if that's the case.
404 if (!m_stop && languageHook->HasRegisteredAddonClasses() && !systemExitThrown &&
405 PyRun_SimpleString(GC_SCRIPT) == -1)
406 CLog::Log(LOGERROR, "CPythonInvoker(%d, %s): failed to run the gc to clean up after running prior to shutting down the Interpreter", GetId(), m_source);
408 Py_EndInterpreter(state);
410 // If we still have objects left around, produce an error message detailing what's been left behind
411 if (languageHook->HasRegisteredAddonClasses())
412 CLog::Log(LOGWARNING, "CPythonInvoker(%d, %s): the python script \"%s\" has left several "
413 "classes in memory that we couldn't clean up. The classes include: %s",
414 GetId(), m_source, m_source, getListOfAddonClassesAsString(languageHook).c_str());
416 // unregister the language hook
417 languageHook->UnregisterMe();
419 PyEval_ReleaseLock();
424 bool CPythonInvoker::stop(bool abort)
426 CSingleLock lock(m_critical);
432 setState(InvokerStateStopping);
434 if (m_threadState != NULL)
436 PyEval_AcquireLock();
437 PyThreadState* old = PyThreadState_Swap((PyThreadState*)m_threadState);
439 //tell xbmc.Monitor to call onAbortRequested()
441 g_pythonParser.OnAbortRequested(m_addon->ID());
444 m = PyImport_AddModule((char*)"xbmc");
445 if (m == NULL || PyObject_SetAttrString(m, (char*)"abortRequested", PyBool_FromLong(1)))
446 CLog::Log(LOGERROR, "CPythonInvoker(%d, %s): failed to set abortRequested", GetId(), m_source);
448 PyThreadState_Swap(old);
450 PyEval_ReleaseLock();
452 XbmcThreads::EndTime timeout(PYTHON_SCRIPT_TIMEOUT);
453 while (!m_stoppedEvent.WaitMSec(15))
455 if (timeout.IsTimePast())
457 CLog::Log(LOGERROR, "CPythonInvoker(%d, %s): script didn't stop in %d seconds - let's kill it", GetId(), m_source, PYTHON_SCRIPT_TIMEOUT / 1000);
461 // We can't empty-spin in the main thread and expect scripts to be able to
462 // dismantle themselves. Python dialogs aren't normal XBMC dialogs, they rely
463 // on TMSG_GUI_PYTHON_DIALOG messages, so pump the message loop.
464 if (g_application.IsCurrentThread())
466 CSingleExit ex(g_graphicsContext);
467 CApplicationMessenger::Get().ProcessMessages();
471 // Useful for add-on performance metrics
472 if (!timeout.IsTimePast())
473 CLog::Log(LOGDEBUG, "CPythonInvoker(%d, %s): script termination took %dms", GetId(), m_source, PYTHON_SCRIPT_TIMEOUT - timeout.MillisLeft());
475 // everything which didn't exit by now gets killed
477 // grabbing the PyLock while holding the m_critical is asking for a deadlock
478 CSingleExit ex2(m_critical);
479 PyEval_AcquireLock();
482 // Since we released the m_critical it's possible that the state is cleaned up
483 // so we need to recheck for m_threadState == NULL
484 if (m_threadState != NULL)
486 old = PyThreadState_Swap((PyThreadState*)m_threadState);
487 for (PyThreadState* state = ((PyThreadState*)m_threadState)->interp->tstate_head; state; state = state->next)
489 // Raise a SystemExit exception in python threads
490 Py_XDECREF(state->async_exc);
491 state->async_exc = PyExc_SystemExit;
492 Py_XINCREF(state->async_exc);
495 // If a dialog entered its doModal(), we need to wake it to see the exception
496 g_pythonParser.PulseGlobalEvent();
500 PyThreadState_Swap(old);
503 PyEval_ReleaseLock();
509 void CPythonInvoker::onError()
511 PyThreadState_Swap(NULL);
512 PyEval_ReleaseLock();
514 setState(InvokerStateFailed);
515 CLog::Log(LOGERROR, "CPythonInvoker(%d, %s): abnormally terminating python thread", GetId(), m_source);
517 CSingleLock lock(m_critical);
518 m_threadState = NULL;
520 ILanguageInvoker::onError();
523 void CPythonInvoker::addPath(const std::string path)
528 if (!m_pythonPath.empty())
529 m_pythonPath += PY_PATH_SEP;
531 #if defined(TARGET_WINDOWS)
532 CStdString tmp(path);
533 g_charsetConverter.utf8ToSystem(tmp);
536 m_pythonPath += path;
537 #endif // defined(TARGET_WINDOWS)