From 12d8576f4bab14b6da85957f156ee0360ab3446f Mon Sep 17 00:00:00 2001 From: Thomas Grill Date: Thu, 3 Jan 2008 16:21:04 +0000 Subject: cross-platform fixes enable compiled-only scripts (without .py) use PyGILState_\*() functionality (enabled with PY_USE_GIL) fixes for non-GIL usage improved symbol comparison buffer protocol adapted for Python 2.5 enable module packages (module/__init__.py[co]), now also for Max svn path=/trunk/; revision=9120 --- externals/grill/py/source/pybase.h | 134 +++++++++++++++++++------------------ 1 file changed, 68 insertions(+), 66 deletions(-) (limited to 'externals/grill/py/source/pybase.h') diff --git a/externals/grill/py/source/pybase.h b/externals/grill/py/source/pybase.h index 437fa85f..48e2f772 100644 --- a/externals/grill/py/source/pybase.h +++ b/externals/grill/py/source/pybase.h @@ -1,11 +1,13 @@ /* - py/pyext - python script object for PD and MaxMSP -Copyright (c)2002-2007 Thomas Grill (gr@grrrr.org) +Copyright (c)2002-2008 Thomas Grill (gr@grrrr.org) For information on usage and redistribution, and for a DISCLAIMER OF ALL WARRANTIES, see the file, "license.txt," in this distribution. +$LastChangedRevision: 26 $ +$LastChangedDate: 2008-01-03 17:20:03 +0100 (Thu, 03 Jan 2008) $ +$LastChangedBy: thomas $ */ #ifndef __PYBASE_H @@ -17,24 +19,24 @@ WARRANTIES, see the file, "license.txt," in this distribution. #include "pybundle.h" #ifdef PY_USE_GIL - typedef PyGILState_STATE ThrState; + typedef PyGILState_STATE ThrState; #else - typedef PyThreadState *ThrState; + typedef PyThreadState *ThrState; #endif class pybase : public flext { public: - pybase(); - virtual ~pybase(); + pybase(); + virtual ~pybase(); void Exit(); - static PyObject *MakePyArgs(const t_symbol *s,int argc,const t_atom *argv,int inlet = -1); - static PyObject *MakePyArg(const t_symbol *s,int argc,const t_atom *argv); - static const t_symbol *GetPyArgs(AtomList &lst,PyObject *pValue,int offs = 0); - static const t_symbol *GetPyAtom(AtomList &lst,PyObject *pValue); + static PyObject *MakePyArgs(const t_symbol *s,int argc,const t_atom *argv,int inlet = -1); + static PyObject *MakePyArg(const t_symbol *s,int argc,const t_atom *argv); + static const t_symbol *GetPyArgs(AtomList &lst,PyObject *pValue,int offs = 0); + static const t_symbol *GetPyAtom(AtomList &lst,PyObject *pValue); static void lib_setup(); @@ -43,48 +45,48 @@ protected: virtual void DumpOut(const t_symbol *sym,int argc,const t_atom *argv) = 0; void m__dir(PyObject *obj); - void m__doc(PyObject *obj); + void m__doc(PyObject *obj); void m_dir() { m__dir(module); } void mg_dir(AtomList &lst) { m__dir(module); } void m_doc() { m__doc(dict); } std::string modname; // module name - PyObject *module,*dict; // object module and associated dictionary + PyObject *module,*dict; // object module and associated dictionary - static const char *py_doc; + static const char *py_doc; void GetDir(PyObject *obj,AtomList &lst); - AtomList args; + AtomList args; void AddCurrentPath(flext_base *o); - void SetArgs(); + void SetArgs(); bool OutObject(flext_base *ext,int o,PyObject *obj); // reload module and all connected objects void Reload(); - bool ImportModule(const char *name); - void UnimportModule(); - bool ReloadModule(); + bool ImportModule(const char *name); + void UnimportModule(); + bool ReloadModule(); // Get module registry - PyObject *GetRegistry(const char *regname); + PyObject *GetRegistry(const char *regname); // Set module registry - void SetRegistry(const char *regname,PyObject *reg); + void SetRegistry(const char *regname,PyObject *reg); // Register object - void Register(PyObject *reg); + void Register(PyObject *reg); // Unregister object - void Unregister(PyObject *reg); + void Unregister(PyObject *reg); - virtual void LoadModule() = 0; - virtual void UnloadModule() = 0; + virtual void LoadModule() = 0; + virtual void UnloadModule() = 0; - virtual void Load() = 0; - virtual void Unload() = 0; + virtual void Load() = 0; + virtual void Unload() = 0; void OpenEditor(); @@ -99,50 +101,50 @@ protected: void Report() { while(PyErr_Occurred()) PyErr_Print(); } - static bool IsAnything(const t_symbol *s) { return s && s != sym_float && s != sym_int && s != sym_symbol && s != sym_list && s != sym_pointer; } - static bool IsAtom(const t_symbol *s) { return s == sym_float || s == sym_int || s == sym_symbol || s == sym_pointer; } + static bool IsAnything(const t_symbol *s) { return s && s != sym_float && s != sym_int && s != sym_symbol && s != sym_list && s != sym_pointer; } + static bool IsAtom(const t_symbol *s) { return s == sym_float || s == sym_int || s == sym_symbol || s == sym_pointer; } -// enum retval { nothing,atom,sequ }; +// enum retval { nothing,atom,sequ }; - // --- module stuff ----- + // --- module stuff ----- - static PyObject *module_obj,*module_dict; - static PyObject *builtins_obj,*builtins_dict; - static PyMethodDef func_tbl[],attr_tbl[]; + static PyObject *module_obj,*module_dict; + static PyObject *builtins_obj,*builtins_dict; + static PyMethodDef func_tbl[],attr_tbl[]; - static PyObject *py__doc__(PyObject *,PyObject *args); - static PyObject *py_send(PyObject *,PyObject *args); + static PyObject *py__doc__(PyObject *,PyObject *args); + static PyObject *py_send(PyObject *,PyObject *args); #ifdef FLEXT_THREADS - static PyObject *py_priority(PyObject *,PyObject *args); + static PyObject *py_priority(PyObject *,PyObject *args); #endif - static PyObject *py_arraysupport(PyObject *,PyObject *args); - static PyObject *py_samplerate(PyObject *,PyObject *args); - static PyObject *py_blocksize(PyObject *,PyObject *args); + static PyObject *py_arraysupport(PyObject *,PyObject *args); + static PyObject *py_samplerate(PyObject *,PyObject *args); + static PyObject *py_blocksize(PyObject *,PyObject *args); - static PyObject *py_searchpaths(PyObject *,PyObject *args); - static PyObject *py_helppaths(PyObject *,PyObject *args); + static PyObject *py_searchpaths(PyObject *,PyObject *args); + static PyObject *py_helppaths(PyObject *,PyObject *args); #if FLEXT_SYS == FLEXT_SYS_PD - static PyObject *py_getvalue(PyObject *,PyObject *args); - static PyObject *py_setvalue(PyObject *,PyObject *args); + static PyObject *py_getvalue(PyObject *,PyObject *args); + static PyObject *py_setvalue(PyObject *,PyObject *args); #endif static PyObject *py_list(PyObject *,PyObject *args); static PyObject *py_tuple(PyObject *,PyObject *args); - // ----thread stuff ------------ + // ----thread stuff ------------ - virtual void m_stop(int argc,const t_atom *argv); + virtual void m_stop(int argc,const t_atom *argv); - bool respond; + bool respond; #ifdef FLEXT_THREADS - int thrcount; + int thrcount; bool shouldexit; - int stoptick; + int stoptick; Timer stoptmr; - void tick(void *); + void tick(void *); #endif int detach; @@ -187,7 +189,7 @@ protected: static PyFifo qufifo; static ThrCond qucond; - + #ifndef PY_USE_GIL static ThrState pythrsys; #endif @@ -201,7 +203,7 @@ protected: public: - static void AddToPath(const char *dir); + static void AddToPath(const char *dir); #ifdef FLEXT_THREADS // this is especially needed when one py/pyext object calls another one @@ -212,43 +214,43 @@ public: #ifdef PY_USE_GIL static inline ThrState FindThreadState() { return ThrState(); } - static inline ThrState PyLock(ThrState = ThrState()) { return PyGILState_Ensure(); } - static inline ThrState PyLockSys() { return PyLock(); } - static inline void PyUnlock(ThrState st) { PyGILState_Release(st); } + static inline ThrState PyLock(ThrState = ThrState()) { return PyGILState_Ensure(); } + static inline ThrState PyLockSys() { return PyLock(); } + static inline void PyUnlock(ThrState st) { PyGILState_Release(st); } #else // PY_USE_GIL static ThrState FindThreadState(); static void FreeThreadState(); - static ThrState PyLock(ThrState st = FindThreadState()) + static ThrState PyLock(ThrState st = FindThreadState()) { - if(!IsSystemThread() || !lockcount++) PyEval_AcquireLock(); - return PyThreadState_Swap(st); + if(st != pythrsys || !lockcount++) PyEval_AcquireLock(); + return PyThreadState_Swap(st); } #if 1 - static inline ThrState PyLockSys() { return PyLock(); } + static inline ThrState PyLockSys() { return PyLock(); } #else - static ThrState PyLockSys() + static ThrState PyLockSys() { if(!lockcount++) PyEval_AcquireLock(); - return PyThreadState_Swap(pythrsys); + return PyThreadState_Swap(pythrsys); } #endif - static void PyUnlock(ThrState st) + static void PyUnlock(ThrState st) { ThrState old = PyThreadState_Swap(st); if(old != pythrsys || !--lockcount) PyEval_ReleaseLock(); } #endif // PY_USE_GIL - + #else // FLEXT_THREADS - static inline ThrState PyLock(ThrState = NULL) { return NULL; } - static inline ThrState PyLockSys() { return NULL; } - static inline void PyUnlock(ThrState st) {} + static inline ThrState PyLock(ThrState = NULL) { return NULL; } + static inline ThrState PyLockSys() { return NULL; } + static inline void PyUnlock(ThrState st) {} #endif - static PyObject* StdOut_Write(PyObject* Self, PyObject* Args); + static PyObject* StdOut_Write(PyObject* Self, PyObject* Args); }; #endif -- cgit v1.2.1