Permalink
Browse files

Make it possible to pass non string arguments at start of a python sc…

…ript. (Since the python api doesn't really allow that, I pass those objects under sys.PARAMETERNAME
  • Loading branch information...
1 parent d43af28 commit 81ff31cf9cf272ad1c2605e1f22de04a0669f428 Fice committed Feb 13, 2013
View
12 xbmc/interfaces/python/XBPyThread.cpp
@@ -82,6 +82,7 @@ XBPyThread::XBPyThread(XBPython *pExecuter, int id) : CThread("XBPyThread")
m_source = NULL;
m_argc = 0;
m_type = 0;
+ m_sysParameterPtr = NULL;
}
XBPyThread::~XBPyThread()
@@ -148,6 +149,12 @@ int XBPyThread::setArgv(const std::vector<CStdString> &argv)
return 0;
}
+void XBPyThread::setSysParameter(char* sysName, PyObject* sysObject)
+{
+ m_sysParameterPtr = sysObject;
+ m_sysParameterName = sysName;
+}
+
#define GC_SCRIPT \
"import gc\n" \
"gc.collect(2)\n"
@@ -245,6 +252,11 @@ void XBPyThread::Process()
// set current directory and python's path.
if (m_argv != NULL)
PySys_SetArgv(m_argc, m_argv);
+
+ if (m_sysParameterPtr != NULL) {
+ if(0!=PySys_SetObject(m_sysParameterName, m_sysParameterPtr))
+ CLog::Log(LOGDEBUG, "setSysParameter failed!");
+ }
CLog::Log(LOGDEBUG, "%s - Setting the Python path to %s", __FUNCTION__, path.c_str());
View
5 xbmc/interfaces/python/XBPyThread.h
@@ -25,6 +25,7 @@
#include "threads/Thread.h"
#include "threads/Event.h"
#include "addons/IAddon.h"
+#include <Python.h>
class XBPython;
@@ -35,6 +36,7 @@ class XBPyThread : public CThread
virtual ~XBPyThread();
int evalFile(const CStdString &src);
int evalString(const CStdString &src);
+ void setSysParameter(char* sysName, PyObject* sysObject);
int setArgv(const std::vector<CStdString> &argv);
bool isStopping();
void stop();
@@ -46,6 +48,9 @@ class XBPyThread : public CThread
CEvent stoppedEvent;
void *m_threadState;
+ PyObject* m_sysParameterPtr;
+ char* m_sysParameterName;
+
char m_type;
char *m_source;
char **m_argv;
View
38 xbmc/interfaces/python/XBPython.cpp
@@ -732,6 +732,44 @@ int XBPython::evalFile(const CStdString &src, ADDON::AddonPtr addon)
std::vector<CStdString> argv;
return evalFile(src, argv, addon);
}
+
+
+int XBPython::evalFile(const CStdString &src, PyObject* sysObject, char* sysName, ADDON::AddonPtr addon)
+{
+
+ CSingleExit ex(g_graphicsContext);
+ // return if file doesn't exist
+ if (!XFILE::CFile::Exists(src))
+ {
+ CLog::Log(LOGERROR, "Python script \"%s\" does not exist", CSpecialProtocol::TranslatePath(src).c_str());
+ return -1;
+ }
+
+ // check if locked
+ if (g_settings.GetCurrentProfile().programsLocked() && !g_passwordManager.IsMasterLockUnlocked(true))
+ return -1;
+
+ CSingleLock lock(m_critSection);
+ Initialize();
+
+ if (!m_bInitialized) return -1;
+
+ m_nextid++;
+ boost::shared_ptr<XBPyThread> pyThread = boost::shared_ptr<XBPyThread>(new XBPyThread(this, m_nextid));
+ pyThread->setSysParameter(sysName, sysObject);
+ pyThread->setAddon(addon);
+ pyThread->evalFile(src);
+ PyElem inf;
+ inf.id = m_nextid;
+ inf.bDone = false;
+ inf.strFile = src;
+ inf.pyThread = pyThread;
+
+ m_vecPyList.push_back(inf);
+
+ return m_nextid;
+}
+
// execute script, returns -1 if script doesn't exist
int XBPython::evalFile(const CStdString &src, const std::vector<CStdString> &argv, ADDON::AddonPtr addon)
{
View
2 xbmc/interfaces/python/XBPython.h
@@ -26,7 +26,6 @@
#include "threads/CriticalSection.h"
#include "interfaces/IAnnouncer.h"
#include "addons/IAddon.h"
-
#include <boost/shared_ptr.hpp>
#include <vector>
@@ -92,6 +91,7 @@ class XBPython :
int GetPythonScriptId(int scriptPosition);
int evalFile(const CStdString &src, ADDON::AddonPtr addon);
int evalFile(const CStdString &src, const std::vector<CStdString> &argv, ADDON::AddonPtr addon);
+ int evalFile(const CStdString &src, PyObject* sysObject, char* sysName, ADDON::AddonPtr addon);
int evalString(const CStdString &src, const std::vector<CStdString> &argv);
bool isRunning(int scriptId);

0 comments on commit 81ff31c

Please sign in to comment.