aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/py/source
diff options
context:
space:
mode:
authorThomas Grill <xovo@users.sourceforge.net>2009-04-01 21:13:09 +0000
committerThomas Grill <xovo@users.sourceforge.net>2009-04-01 21:13:09 +0000
commit0ed7a8b68dd73e2b0473b8127aeca99f3bac9061 (patch)
tree5c67818b38a5cc2f9caa5ca7f8640ca356adf02b /externals/grill/py/source
parentbb4c7f6a245394d09dac9adfb2efb093d3d98452 (diff)
cleaned up grill externals - replaced with svn:externals to svn.grrrr.org/ext/trunk/
svn path=/trunk/; revision=10951
Diffstat (limited to 'externals/grill/py/source')
-rw-r--r--externals/grill/py/source/bound.cpp202
-rw-r--r--externals/grill/py/source/clmeth.cpp426
-rw-r--r--externals/grill/py/source/main.cpp15
-rw-r--r--externals/grill/py/source/main.h43
-rw-r--r--externals/grill/py/source/modmeth.cpp304
-rw-r--r--externals/grill/py/source/py.cpp419
-rw-r--r--externals/grill/py/source/pyargs.cpp231
-rw-r--r--externals/grill/py/source/pyatom.cpp63
-rw-r--r--externals/grill/py/source/pyatom.h21
-rw-r--r--externals/grill/py/source/pybase.cpp923
-rw-r--r--externals/grill/py/source/pybase.h256
-rw-r--r--externals/grill/py/source/pybuffer.cpp828
-rw-r--r--externals/grill/py/source/pybuffer.h81
-rw-r--r--externals/grill/py/source/pybundle.cpp234
-rw-r--r--externals/grill/py/source/pybundle.h62
-rw-r--r--externals/grill/py/source/pydsp.cpp192
-rw-r--r--externals/grill/py/source/pyext.cpp662
-rw-r--r--externals/grill/py/source/pyext.h158
-rw-r--r--externals/grill/py/source/pymeth.cpp430
-rw-r--r--externals/grill/py/source/pyprefix.h54
-rw-r--r--externals/grill/py/source/pysymbol.cpp266
-rw-r--r--externals/grill/py/source/pysymbol.h94
-rw-r--r--externals/grill/py/source/register.cpp153
23 files changed, 0 insertions, 6117 deletions
diff --git a/externals/grill/py/source/bound.cpp b/externals/grill/py/source/bound.cpp
deleted file mode 100644
index 266bb9c5..00000000
--- a/externals/grill/py/source/bound.cpp
+++ /dev/null
@@ -1,202 +0,0 @@
-/*
-py/pyext - python external object for PD and MaxMSP
-
-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 $
-*/
-
-#include "pyext.h"
-#include "flinternal.h"
-
-#include <set>
-
-class MethodCompare:
- public std::less<PyObject *>
-{
-public:
- bool operator()(PyObject *a,PyObject *b) const
- {
- if(PyMethod_Check(a))
- if(PyMethod_Check(b)) {
- // both are methods
- PyObject *sa = PyMethod_GET_SELF(a);
- PyObject *sb = PyMethod_GET_SELF(b);
- if(sa)
- if(sb) {
- // both have self
- if(sa == sb)
- return PyMethod_GET_FUNCTION(a) < PyMethod_GET_FUNCTION(b);
- else
- return sa < sb;
- }
- else
- return false;
- else
- if(sb)
- return true;
- else
- return PyMethod_GET_FUNCTION(a) < PyMethod_GET_FUNCTION(b);
- }
- else
- return false;
- else
- if(PyMethod_Check(b))
- return true;
- else
- // both are non-method callables
- return a < b;
- }
-};
-
-typedef std::set<PyObject *,MethodCompare> FuncSet;
-
-struct bounddata
-{
- PyObject *self;
- FuncSet funcs;
-};
-
-bool pyext::boundmeth(flext_base *th,t_symbol *sym,int argc,t_atom *argv,void *data)
-{
- bounddata *obj = (bounddata *)data;
- pyext *pyth = static_cast<pyext *>(th);
-
- ThrState state = pyth->PyLock();
-
- PyObject *args = MakePyArgs(sym,argc,argv);
-
- // call all functions bound by this symbol
- for(FuncSet::iterator it = obj->funcs.begin(); it != obj->funcs.end(); ++it) {
- PyObject *ret = PyObject_CallObject(*it,args);
- if(!ret)
- PyErr_Print();
- else
- Py_DECREF(ret);
- }
-
- Py_XDECREF(args);
-
- pyth->PyUnlock(state);
- return true;
-}
-
-PyObject *pyext::pyext_bind(PyObject *,PyObject *args)
-{
- PyObject *self,*meth,*name;
- if(!PyArg_ParseTuple(args, "OOO:pyext_bind", &self,&name,&meth)) // borrowed references
- post("py/pyext - Wrong arguments!");
- else if(!PyInstance_Check(self) || !PyCallable_Check(meth)) {
- post("py/pyext - Wrong argument types!");
- }
- else {
- pyext *th = GetThis(self);
- if(!th) {
- PyErr_SetString(PyExc_RuntimeError,"pyext - _bind: instance not associated with pd object");
- return NULL;
- }
-
- const t_symbol *recv = pyObject_AsSymbol(name);
-
- void *data = NULL;
- if(recv && th->GetBoundMethod(recv,boundmeth,data)) {
- // already bound to that symbol and function
- bounddata *bdt = (bounddata *)data;
- FLEXT_ASSERT(bdt != NULL && bdt->self == self);
-
- FuncSet::iterator it = bdt->funcs.find(meth);
- if(it == bdt->funcs.end()) {
- bdt->funcs.insert(meth);
- Py_INCREF(meth);
- }
- }
- else {
- Py_INCREF(self); // self is borrowed reference
- Py_INCREF(meth);
-
- bounddata *data = new bounddata;
- data->self = self;
- data->funcs.insert(meth);
-
- th->BindMethod(recv,boundmeth,data);
- }
- }
-
- Py_INCREF(Py_None);
- return Py_None;
-}
-
-PyObject *pyext::pyext_unbind(PyObject *,PyObject *args)
-{
- PyObject *self,*meth,*name;
- if(!PyArg_ParseTuple(args, "OOO:pyext_bind", &self,&name,&meth)) // borrowed references
- post("py/pyext - Wrong arguments!");
- else if(!PyInstance_Check(self) || !PyCallable_Check(meth)) {
- post("py/pyext - Wrong argument types!");
- }
- else {
- pyext *th = GetThis(self);
- if(!th) {
- PyErr_SetString(PyExc_RuntimeError,"pyext - _unbind: instance not associated with pd object");
- return NULL;
- }
-
- const t_symbol *recv = pyObject_AsSymbol(name);
-
- void *data = NULL;
- if(recv && th->GetBoundMethod(recv,boundmeth,data)) {
- bounddata *bdt = (bounddata *)data;
- FLEXT_ASSERT(bdt != NULL);
-
- // erase from map
- // ATTENTION: meth is different from the element found in the map
- // it just points to the same instance method
- FuncSet::iterator it = bdt->funcs.find(meth);
- if(it != bdt->funcs.end()) {
- Py_DECREF(*it);
- bdt->funcs.erase(it);
- }
- else
- post("py/pyext - Function to unbind couldn't be found");
-
- if(bdt->funcs.empty()) {
- Py_DECREF(bdt->self);
- delete bdt;
-
- th->UnbindMethod(recv,boundmeth,NULL);
- }
- }
- }
-
- Py_INCREF(Py_None);
- return Py_None;
-}
-
-
-void pyext::ClearBinding()
-{
- // in case the object couldn't be constructed...
- if(!pyobj) return;
-
- pyext *th = GetThis(pyobj);
- if(!th) return;
-
- void *data = NULL;
- const t_symbol *sym = NULL;
-
- // unbind all
- while(th->UnbindMethod(sym,NULL,&data)) {
- bounddata *bdt = (bounddata *)data;
- if(bdt) {
- for(FuncSet::iterator it = bdt->funcs.begin(); it != bdt->funcs.end(); ++it)
- Py_DECREF(*it);
-
- Py_DECREF(bdt->self);
- delete bdt;
- }
- }
-}
diff --git a/externals/grill/py/source/clmeth.cpp b/externals/grill/py/source/clmeth.cpp
deleted file mode 100644
index 62da451b..00000000
--- a/externals/grill/py/source/clmeth.cpp
+++ /dev/null
@@ -1,426 +0,0 @@
-/*
-py/pyext - python external object for PD and Max/MSP
-
-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 $
-*/
-
-#include "pyext.h"
-
-
-PyMethodDef pyext::meth_tbl[] =
-{
-/*
- {"__init__", pyext::pyext__init__, METH_VARARGS, "Constructor"},
- {"__del__", pyext::pyext__del__, METH_VARARGS, "Destructor"},
-*/
- {"__str__", pyext::pyext__str__, METH_VARARGS, "stringify"},
- {"_outlet", pyext::pyext_outlet, METH_VARARGS,"Send message to outlet"},
-#if FLEXT_SYS == FLEXT_SYS_PD
- {"_tocanvas", pyext::pyext_tocanvas, METH_VARARGS,"Send message to canvas" },
-#endif
-
- { "_bind", pyext::pyext_bind, METH_VARARGS,"Bind function to a receiving symbol" },
- { "_unbind", pyext::pyext_unbind, METH_VARARGS,"Unbind function from a receiving symbol" },
-#ifdef FLEXT_THREADS
- { "_detach", pyext::pyext_detach, METH_VARARGS,"Set detach flag for called methods" },
- { "_stop", pyext::pyext_stop, METH_VARARGS,"Stop running threads" },
-#endif
- { "_invec", pyext::pyext_invec, METH_VARARGS,"Get input vector" },
- { "_outvec", pyext::pyext_outvec, METH_VARARGS,"Get output vector" },
- {NULL, NULL, 0, NULL} /* Sentinel */
-};
-
-PyMethodDef pyext::attr_tbl[] =
-{
- { "__setattr__", pyext::pyext_setattr, METH_VARARGS,"Set class attribute" },
- { "__getattr__", pyext::pyext_getattr, METH_VARARGS,"Get class attribute" },
- { NULL, NULL,0,NULL },
-};
-
-
-const char *pyext::pyext_doc =
- "py/pyext - python external object for PD and Max/MSP, (C)2002-2008 Thomas Grill\n"
- "\n"
- "This is the pyext base class. Available methods:\n"
- "_outlet(self,ix,args...): Send a message to an indexed outlet\n"
-#if FLEXT_SYS == FLEXT_SYS_PD
- "_tocanvas(self,args...): Send a message to the parent canvas\n"
-#endif
- "_bind(self,name,func): Bind a python function to a symbol\n"
- "_unbind(self,name,func): Unbind a python function from a symbol\n"
- "_isthreaded: Query whether threading is enabled\n"
-#ifdef FLEXT_THREADS
- "_detach(self,int): Define whether a called Python method has its own thread\n"
- "_stop(self): Stop running threads\n"
- "_shouldexit: Query whether threads should terminate\n"
-#endif
-;
-
-/*
-PyObject* pyext::pyext__init__(PyObject *,PyObject *args)
-{
-// post("pyext.__init__ called");
-
- Py_INCREF(Py_None);
- return Py_None;
-}
-
-PyObject* pyext::pyext__del__(PyObject *,PyObject *args)
-{
-// post("pyext.__del__ called");
-
- Py_INCREF(Py_None);
- return Py_None;
-}
-*/
-
-PyObject* pyext::pyext__str__(PyObject *,PyObject *args)
-{
- PyObject *self;
- if(!PyArg_ParseTuple(args, "O:pyext__str__",&self)) {
- // handle error
- ERRINTERNAL();
- return NULL;
- }
-
- return PyString_FromFormat("<pyext object %p>",self);
-}
-
-PyObject* pyext::pyext_setattr(PyObject *,PyObject *args)
-{
- PyObject *self,*name,*val;
- if(!PyArg_ParseTuple(args, "OOO:pyext_setattr", &self,&name,&val)) {
- // handle error
- ERRINTERNAL();
- return NULL;
- }
-
- bool handled = false;
-
-/*
- if(PyString_Check(name)) {
- char* sname = PyString_AsString(name);
- if (sname) {
-// post("pyext::setattr %s",sname);
- }
- }
-*/
- if(!handled) {
- if(PyInstance_Check(self))
- PyDict_SetItem(((PyInstanceObject *)self)->in_dict, name,val);
- else
- ERRINTERNAL();
- }
-
- Py_INCREF(Py_None);
- return Py_None;
-}
-
-PyObject* pyext::pyext_getattr(PyObject *,PyObject *args)
-{
- PyObject *self,*name,*ret = NULL;
- if(!PyArg_ParseTuple(args, "OO:pyext_getattr", &self,&name)) {
- // handle error
- ERRINTERNAL();
- }
-
- if(PyString_Check(name)) {
- char* sname = PyString_AS_STRING(name);
- if(sname) {
-#ifdef FLEXT_THREADS
- if(!strcmp(sname,"_shouldexit")) {
- pyext *ext = GetThis(self);
- if(ext)
- ret = PyLong_FromLong(ext->shouldexit?1:0);
- else {
- // return true for _shouldexit if association has been removed
- Py_INCREF(Py_True);
- ret = Py_True;
- }
- }
- else
-#endif
- if(!strcmp(sname,"_isthreaded")) {
- #ifdef FLEXT_THREADS
- Py_INCREF(Py_True);
- ret = Py_True;
- #else
- Py_INCREF(Py_False);
- ret = Py_False;
- #endif
- }
- }
- }
-
- if(!ret) {
-#if PY_VERSION_HEX >= 0x02020000
- ret = PyObject_GenericGetAttr(self,name); // new reference (?)
-#else
- if(PyInstance_Check(self))
- // borrowed reference
- ret = PyDict_GetItem(((PyInstanceObject *)self)->in_dict,name);
-#endif
- }
- return ret;
-}
-
-//! Send message to outlet
-PyObject *pyext::pyext_outlet(PyObject *,PyObject *args)
-{
- bool ok = false;
-
- // should always be a tuple!
- FLEXT_ASSERT(PyTuple_Check(args));
-
- int sz = PyTuple_GET_SIZE(args);
-
- // borrowed references!
- PyObject *self,*outl;
-
- if(
- sz >= 2 &&
- (self = PyTuple_GET_ITEM(args,0)) != NULL && PyInstance_Check(self) &&
- (outl = PyTuple_GET_ITEM(args,1)) != NULL && PyInt_Check(outl)
- ) {
- pyext *ext = GetThis(self);
- if(!ext) {
- PyErr_SetString(PyExc_RuntimeError,"pyext - _outlet: instance not associated with pd object");
- return NULL;
- }
-
- PyObject *val;
-#if 0
- if(sz == 3) {
- val = PyTuple_GET_ITEM(args,2); // borrow reference
- Py_INCREF(val);
- tp = PySequence_Check(val);
- }
- else
- tp = false;
-
- if(!tp)
- val = PySequence_GetSlice(args,2,sz); // new ref
-#else
- if(sz == 3) {
- val = PyTuple_GET_ITEM(args,2); // borrow reference
- Py_INCREF(val);
- }
- else
- val = PyTuple_GetSlice(args,2,sz); // new ref
-#endif
-
- int o = PyInt_AS_LONG(outl);
- if(o >= 1 && o <= ext->Outlets()) {
- // offset outlet by signal outlets
- o += ext->sigoutlets;
-
- if(ext->OutObject(ext,o-1,val))
- ok = true;
- else
- PyErr_SetString(PyExc_ValueError,"pyext - _outlet: invalid arguments");
- }
- else
- PyErr_SetString(PyExc_ValueError,"pyext - _outlet: index out of range");
-
- Py_DECREF(val);
- }
- else
- PyErr_SetString(PyExc_SyntaxError,"pyext - Syntax: _outlet(self,outlet,args...)");
-
- if(!ok) return NULL;
- Py_INCREF(Py_None);
- return Py_None;
-}
-
-
-
-#ifdef FLEXT_THREADS
-//! Detach threads
-PyObject *pyext::pyext_detach(PyObject *,PyObject *args)
-{
- PyObject *self;
- int val;
- if(!PyArg_ParseTuple(args, "Oi:pyext_detach",&self,&val)) {
- // handle error
- PyErr_SetString(PyExc_SyntaxError,"pyext - Syntax: _detach(self,[0/1/2])");
- return NULL;
- }
- else if(val < 0 || val > 2) {
- PyErr_SetString(PyExc_ValueError,"pyext - _detach must be in the range 0..2");
- return NULL;
- }
- else {
- pyext *ext = GetThis(self);
- if(!ext) {
- PyErr_SetString(PyExc_RuntimeError,"pyext - _detach: instance not associated with pd object");
- return NULL;
- }
-
- ext->detach = val;
- }
-
- Py_INCREF(Py_None);
- return Py_None;
-}
-
-//! Stop running threads
-PyObject *pyext::pyext_stop(PyObject *,PyObject *args)
-{
- PyObject *self;
- int val = -1;
- if(!PyArg_ParseTuple(args, "O|i:pyext_stop",&self,&val)) {
- // handle error
- PyErr_SetString(PyExc_SyntaxError,"pyext - Syntax: _stop(self,{wait time})");
- return NULL;
- }
- else if(val < 0) {
- PyErr_SetString(PyExc_ValueError,"pyext - _stop time must be >= 0");
- return NULL;
- }
- else {
- pyext *ext = GetThis(self);
- if(!ext) {
- PyErr_SetString(PyExc_RuntimeError,"pyext - _stop: instance not associated with pd object");
- return NULL;
- }
-
- int cnt;
- t_atom at;
- if(val >= 0) cnt = 1,flext::SetInt(at,val);
- else cnt = 0;
- ext->m_stop(cnt,&at);
- }
-
- Py_INCREF(Py_None);
- return Py_None;
-}
-
-#endif
-
-
-#if FLEXT_SYS == FLEXT_SYS_PD
-//! Send message to canvas
-PyObject *pyext::pyext_tocanvas(PyObject *,PyObject *args)
-{
- FLEXT_ASSERT(PyTuple_Check(args));
-
- int sz = PyTuple_GET_SIZE(args);
-
- bool ok = false;
- PyObject *self; // borrowed ref
- if(
- sz >= 1 &&
- (self = PyTuple_GET_ITEM(args,0)) != NULL && PyInstance_Check(self)
- ) {
- pyext *ext = GetThis(self);
- if(!ext) {
- PyErr_SetString(PyExc_RuntimeError,"pyext - _tocanvas: instance not associated with pd object");
- return NULL;
- }
-
- PyObject *val;
-
- bool tp =
- sz == 2 &&
- PySequence_Check(
- val = PyTuple_GET_ITEM(args,1) // borrowed ref
- );
-
- if(!tp)
- val = PyTuple_GetSlice(args,1,sz); // new ref
-
- flext::AtomListStatic<16> lst;
- const t_symbol *sym = GetPyArgs(lst,val);
- if(sym) {
- t_glist *gl = ext->thisCanvas();
- if(gl) {
- // \TODO find a flext-based non-locking method
- sys_lock();
- pd_forwardmess((t_class **)gl,lst.Count(),lst.Atoms());
- sys_unlock();
- }
-#ifdef FLEXT_DEBUG
- else
- post("pyext - no parent canvas?!");
-#endif
- ok = true;
- }
- else
- post("py/pyext - No data to send");
-
- if(!tp) Py_DECREF(val);
- }
-
- if(!ok) {
- PyErr_SetString(PyExc_SyntaxError,"pyext - Syntax: _tocanvas(self,args...)");
- return NULL;
- }
-
- Py_INCREF(Py_None);
- return Py_None;
-}
-#endif
-
-PyObject *pyext::pyext_invec(PyObject *,PyObject *args)
-{
- PyObject *self;
- int val = -1;
- if(!PyArg_ParseTuple(args, "O|i:pyext_invec",&self,&val)) {
- // handle error
- PyErr_SetString(PyExc_SyntaxError,"pyext - Syntax: _invec(self,inlet)");
- return NULL;
- }
- else if(val < 0) {
- PyErr_SetString(PyExc_ValueError,"pyext - _invec: index out of range");
- return NULL;
- }
- else {
- pyext *ext = GetThis(self);
- if(ext) {
- PyObject *b = ext->GetSig(val,true);
- if(b) return b;
- }
- else {
- PyErr_SetString(PyExc_RuntimeError,"pyext - _invec: instance not associated with pd object");
- return NULL;
- }
- }
-
- Py_INCREF(Py_None);
- return Py_None;
-}
-
-PyObject *pyext::pyext_outvec(PyObject *,PyObject *args)
-{
- PyObject *self;
- int val = -1;
- if(!PyArg_ParseTuple(args, "O|i:pyext_outvec",&self,&val)) {
- // handle error
- PyErr_SetString(PyExc_SyntaxError,"pyext - Syntax: _outvec(self,inlet)");
- return NULL;
- }
- else if(val < 0) {
- PyErr_SetString(PyExc_ValueError,"pyext - _outvec: index out of range");
- return NULL;
- }
- else {
- pyext *ext = GetThis(self);
- if(ext) {
- PyObject *b = ext->GetSig(val,false);
- if(b) return b;
- }
- else {
- PyErr_SetString(PyExc_RuntimeError,"pyext - _outvec: instance not associated with pd object");
- return NULL;
- }
- }
-
- Py_INCREF(Py_None);
- return Py_None;
-}
diff --git a/externals/grill/py/source/main.cpp b/externals/grill/py/source/main.cpp
deleted file mode 100644
index fc93084e..00000000
--- a/externals/grill/py/source/main.cpp
+++ /dev/null
@@ -1,15 +0,0 @@
-/*
-py/pyext - python external object for PD and MaxMSP
-
-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 $
-*/
-
-#include "pybase.h"
-
-
diff --git a/externals/grill/py/source/main.h b/externals/grill/py/source/main.h
deleted file mode 100644
index 0d5c001c..00000000
--- a/externals/grill/py/source/main.h
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
-py/pyext - python script object for PD and MaxMSP
-
-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 __MAIN_H
-#define __MAIN_H
-
-#include "pyprefix.h"
-
-#define PY__VERSION "0.2.1pre"
-
-
-#define PYEXT_MODULE "pyext" // name for module
-#define PYEXT_CLASS "_class" // name for base class
-
-#define REGNAME "_registry"
-
-#define PY_STOP_WAIT 100 // ms
-#define PY_STOP_TICK 1 // ms
-
-
-class pybase;
-
-class FifoEl
- : public FifoCell
-{
-public:
- void Set(pybase *t,PyObject *f,PyObject *a) { th = t,fun = f,args = a; }
- pybase *th;
- PyObject *fun,*args;
-};
-
-typedef PooledFifo<FifoEl> PyFifo;
-
-#endif
diff --git a/externals/grill/py/source/modmeth.cpp b/externals/grill/py/source/modmeth.cpp
deleted file mode 100644
index b388f240..00000000
--- a/externals/grill/py/source/modmeth.cpp
+++ /dev/null
@@ -1,304 +0,0 @@
-/*
-py/pyext - python external object for PD and Max/MSP
-
-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 $
-*/
-
-#include "pybase.h"
-
-
-// function table for module
-PyMethodDef pybase::func_tbl[] =
-{
- { "_send", pybase::py_send, METH_VARARGS,"Send message to a named object" },
-#ifdef FLEXT_THREADS
- { "_priority", pybase::py_priority, METH_VARARGS,"Set priority of current thread" },
-#endif
-
- { "_arraysupport", pybase::py_arraysupport, METH_NOARGS,"Query Python array support" },
- { "_samplerate", pybase::py_samplerate, METH_NOARGS,"Get system sample rate" },
- { "_blocksize", pybase::py_blocksize, METH_NOARGS,"Get system block size" },
-
- { "_searchpaths", pybase::py_searchpaths, METH_NOARGS,"Get system search paths" },
- { "_helppaths", pybase::py_helppaths, METH_NOARGS,"Get system help paths" },
-
-#if FLEXT_SYS == FLEXT_SYS_PD
- { "_getvalue", pybase::py_getvalue, METH_VARARGS,"Get value of a 'value' object" },
- { "_setvalue", pybase::py_setvalue, METH_VARARGS,"Set value of a 'value' object" },
-#endif
-
- { "_list", pybase::py_list, METH_VARARGS,"Make a list from arguments" },
- { "_tuple", pybase::py_tuple, METH_VARARGS,"Make a tuple from arguments" },
-
- {NULL, NULL, 0, NULL} // sentinel
-};
-
-const char *pybase::py_doc =
- "py/pyext - python external object for PD and Max/MSP, (C)2002-2008 Thomas Grill\n"
- "\n"
- "This is the pyext module. Available function:\n"
- "_send(args...): Send a message to a send symbol\n"
-#ifdef FLEXT_THREADS
- "_priority(int): Raise/lower thread priority\n"
-#endif
- "_samplerate(): Get system sample rate\n"
- "_blocksize(): Get current blocksize\n"
- "_getvalue(name): Get value of a 'value' object\n"
- "_setvalue(name,float): Set value of a 'value' object\n"
-
- "_list(args...): Make a list from args\n"
- "_tuple(args...): Make a tuple from args\n"
-;
-
-#ifdef FLEXT_THREADS
-void pybase::tick(void *)
-{
- Lock();
-
- if(!thrcount) {
- // all threads have stopped
- shouldexit = false;
- stoptick = 0;
- }
- else {
- // still active threads
- if(!--stoptick) {
- post("py/pyext - Threads couldn't be stopped entirely - %i remaining",thrcount);
- shouldexit = false;
- }
- else
- // continue waiting
- stoptmr.Delay(PY_STOP_TICK/1000.);
- }
-
- Unlock();
-}
-#endif
-
-void pybase::m_stop(int argc,const t_atom *argv)
-{
-#ifdef FLEXT_THREADS
- if(thrcount) {
- Lock();
-
- int wait = PY_STOP_WAIT;
- if(argc >= 1 && CanbeInt(argv[0])) wait = GetAInt(argv[0]);
-
- int ticks = wait/PY_STOP_TICK;
- if(stoptick) {
- // already stopping
- if(ticks < stoptick) stoptick = ticks;
- }
- else
- stoptick = ticks;
- shouldexit = true;
- stoptmr.Delay(PY_STOP_TICK/1000.);
-
- Unlock();
- }
-#endif
-}
-
-PyObject *pybase::py_samplerate(PyObject *self,PyObject *args)
-{
- return PyFloat_FromDouble(sys_getsr());
-}
-
-PyObject *pybase::py_blocksize(PyObject *self,PyObject *args)
-{
- return PyLong_FromLong(sys_getblksize());
-}
-
-PyObject *pybase::py_searchpaths(PyObject *self,PyObject *args)
-{
-#if FLEXT_SYS == FLEXT_SYS_PD && defined(PD_DEVEL_VERSION) && defined(PY_USE_INOFFICIAL)
- PyObject *ret = PyList_New(0);
- char *dir;
- for(int i = 0; (dir = namelist_get(sys_searchpath,i)) != NULL; ++i)
- PyList_Append(ret,PyString_FromString(dir));
- return ret;
-#else
- Py_INCREF(Py_None);
- return Py_None;
-#endif
-}
-
-PyObject *pybase::py_helppaths(PyObject *self,PyObject *args)
-{
-#if FLEXT_SYS == FLEXT_SYS_PD && defined(PD_DEVEL_VERSION) && defined(PY_USE_INOFFICIAL)
- PyObject *ret = PyList_New(0);
- char *dir;
- for(int i = 0; (dir = namelist_get(sys_helppath,i)) != NULL; ++i)
- PyList_Append(ret,PyString_FromString(dir));
- return ret;
-#else
- Py_INCREF(Py_None);
- return Py_None;
-#endif
-}
-
-PyObject *pybase::py_send(PyObject *,PyObject *args)
-{
- // should always be a tuple
- FLEXT_ASSERT(PyTuple_Check(args));
-
- const int sz = PyTuple_GET_SIZE(args);
-
- const t_symbol *recv;
- if(
- sz >= 1 &&
- (recv = pyObject_AsSymbol(PyTuple_GET_ITEM(args,0))) != NULL
- ) {
- PyObject *val;
-
-#if 0
- bool tp =
- sz == 2 &&
- PySequence_Check(
- val = PyTuple_GET_ITEM(args,1) // borrowed ref
- );
-
- if(!tp)
- val = PySequence_GetSlice(args,1,sz); // new ref
-#else
- if(sz == 2) {
- val = PyTuple_GET_ITEM(args,1); // borrow reference
- Py_INCREF(val);
- }
- else
- val = PySequence_GetSlice(args,1,sz); // new ref
-#endif
-
- AtomListStatic<16> lst;
- const t_symbol *sym = GetPyArgs(lst,val);
- Py_DECREF(val);
-
- if(sym) {
- bool ok = Forward(recv,sym,lst.Count(),lst.Atoms());
-#ifdef FLEXT_DEBUG
- if(!ok)
- post("py/pyext - Receiver doesn't exist");
-#endif
- Py_INCREF(Py_None);
- return Py_None;
- }
-/*
- else if(PyErr_Occurred())
- PyErr_Print();
- else
- post("py/pyext - No data to send");
-*/
- else {
- FLEXT_ASSERT(PyErr_Occurred());
- return NULL;
- }
- }
-/*
- else
- post("py/pyext - Send name is invalid");
-*/
- else {
- PyErr_SetString(PyExc_ValueError,"py/pyext - Send name is invalid");
- return NULL;
- }
-}
-
-#ifdef FLEXT_THREADS
-PyObject *pybase::py_priority(PyObject *self,PyObject *args)
-{
- int val;
- if(!PyArg_ParseTuple(args, "i:py_priority", &val)) {
- post("py/pyext - Syntax: _priority [int]");
- }
- else
- RelPriority(val);
-
- Py_INCREF(Py_None);
- return Py_None;
-}
-#endif
-
-#if FLEXT_SYS == FLEXT_SYS_PD
-PyObject *pybase::py_getvalue(PyObject *self,PyObject *args)
-{
- FLEXT_ASSERT(PyTuple_Check(args));
-
- const int sz = PyTuple_GET_SIZE(args);
- const t_symbol *sym;
- PyObject *ret;
-
- if(
- sz == 1 &&
- (sym = pyObject_AsSymbol(PyTuple_GET_ITEM(args,0))) != NULL
- ) {
- float f;
- if(value_getfloat(const_cast<t_symbol *>(sym),&f)) {
- post("py/pyext - Could not get value '%s'",GetString(sym));
- Py_INCREF(Py_None);
- ret = Py_None;
- }
- else
- ret = PyFloat_FromDouble(f);
- }
- else {
- post("py/pyext - Syntax: _getvalue [name]");
- Py_INCREF(Py_None);
- ret = Py_None;
- }
- return ret;
-}
-
-PyObject *pybase::py_setvalue(PyObject *self,PyObject *args)
-{
- FLEXT_ASSERT(PyTuple_Check(args));
-
- const int sz = PyTuple_GET_SIZE(args);
- const t_symbol *sym;
- PyObject *val; // borrowed reference
-
- if(
- sz == 2 &&
- (sym = pyObject_AsSymbol(PyTuple_GET_ITEM(args,0))) != NULL &&
- PyNumber_Check(val = PyTuple_GET_ITEM(args,1))
- ) {
- float f = (float)PyFloat_AsDouble(val);
-
- if(value_setfloat(const_cast<t_symbol *>(sym),f))
- post("py/pyext - Could not set value '%s'",GetString(sym));
- }
- else
- post("py/pyext - Syntax: _setvalue [name] [value]");
-
- Py_INCREF(Py_None);
- return Py_None;
-}
-#endif
-
-PyObject *pybase::py_list(PyObject *,PyObject *args)
-{
- // should always be a tuple
- FLEXT_ASSERT(PyTuple_Check(args));
-
- const int sz = PyTuple_GET_SIZE(args);
- PyObject *ret = PyList_New(sz);
- for(int i = 0; i < sz; ++i) {
- PyObject *el = PyTuple_GET_ITEM(args,i);
- Py_INCREF(el);
- PyList_SET_ITEM(ret,i,el);
- }
- return ret;
-}
-
-PyObject *pybase::py_tuple(PyObject *,PyObject *args)
-{
- // should always be a tuple
- FLEXT_ASSERT(PyTuple_Check(args));
- Py_INCREF(args);
- return args;
-}
diff --git a/externals/grill/py/source/py.cpp b/externals/grill/py/source/py.cpp
deleted file mode 100644
index 1745ad74..00000000
--- a/externals/grill/py/source/py.cpp
+++ /dev/null
@@ -1,419 +0,0 @@
-/*
-py/pyext - python script object for PD and Max/MSP
-
-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 $
-*/
-
-#include "pybase.h"
-
-class pyobj
- : public pybase
- , public flext_base
-{
- FLEXT_HEADER_S(pyobj,flext_base,Setup)
-
-public:
- pyobj(int argc,const t_atom *argv);
- ~pyobj();
-
-protected:
- virtual void Exit();
-
- virtual bool CbMethodResort(int n,const t_symbol *s,int argc,const t_atom *argv);
- virtual void CbClick();
-
- void m_help();
-
- void m_reload() { Reload(); }
- void m_reload_(int argc,const t_atom *argv) { args(argc,argv); Reload(); }
- void m_set(int argc,const t_atom *argv);
- void m_dir_() { m__dir(function); }
- void m_doc_() { m__doc(function); }
-
- const t_symbol *funname;
- PyObject *function;
- bool withfunction;
-
- virtual void LoadModule();
- virtual void UnloadModule();
-
- virtual void Load();
- virtual void Unload();
-
- bool SetFunction(const t_symbol *func);
- bool ResetFunction();
-
- virtual void DumpOut(const t_symbol *sym,int argc,const t_atom *argv);
-
- PyObject **objects;
-
-private:
-
- virtual void callpy(PyObject *fun,PyObject *args);
-
- static void Setup(t_classid c);
-
- FLEXT_CALLBACK(m_help)
- FLEXT_CALLBACK(m_reload)
- FLEXT_CALLBACK_V(m_reload_)
- FLEXT_CALLBACK_V(m_set)
- FLEXT_CALLBACK(m_dir_)
- FLEXT_CALLBACK(m_doc_)
-
- // callbacks
- FLEXT_ATTRVAR_I(detach)
- FLEXT_ATTRVAR_B(pymsg)
- FLEXT_ATTRVAR_B(respond)
-
- FLEXT_CALLBACK_V(m_stop)
- FLEXT_CALLBACK(m_dir)
- FLEXT_CALLGET_V(mg_dir)
- FLEXT_CALLBACK(m_doc)
-
- FLEXT_CALLBACK(CbClick)
-
-#ifdef FLEXT_THREADS
- FLEXT_CALLBACK_T(tick)
-#endif
-};
-
-FLEXT_LIB_V("py",pyobj)
-
-
-void pyobj::Setup(t_classid c)
-{
- FLEXT_CADDMETHOD_(c,0,"doc",m_doc);
- FLEXT_CADDMETHOD_(c,0,"dir",m_dir);
-#ifdef FLEXT_THREADS
- FLEXT_CADDATTR_VAR1(c,"detach",detach);
- FLEXT_CADDMETHOD_(c,0,"stop",m_stop);
-#endif
-
- FLEXT_CADDMETHOD_(c,0,"help",m_help);
- FLEXT_CADDMETHOD_(c,0,"reload",m_reload_);
- FLEXT_CADDMETHOD_(c,0,"reload.",m_reload);
- FLEXT_CADDMETHOD_(c,0,"doc+",m_doc_);
- FLEXT_CADDMETHOD_(c,0,"dir+",m_dir_);
-
- FLEXT_CADDMETHOD_(c,0,"set",m_set);
-
- FLEXT_CADDMETHOD_(c,0,"edit",CbClick);
-
- FLEXT_CADDATTR_VAR1(c,"py",pymsg);
- FLEXT_CADDATTR_VAR1(c,"respond",respond);
-}
-
-pyobj::pyobj(int argc,const t_atom *argv)
- : funname(NULL)
- , function(NULL)
- , withfunction(false)
- , objects(NULL)
-{
-#ifdef FLEXT_THREADS
- FLEXT_ADDTIMER(stoptmr,tick);
-#endif
-
- ThrState state = PyLockSys();
-
- int inlets;
- if(argc && CanbeInt(*argv)) {
- inlets = GetAInt(*argv);
- if(inlets < 0) inlets = 1;
- argv++,argc--;
- }
- else
- // -1 signals non-explicit definition
- inlets = -1;
-
- if(inlets >= 1) {
- objects = new PyObject *[inlets];
- for(int i = 0; i < inlets; ++i) { objects[i] = Py_None; Py_INCREF(Py_None); }
- }
-
- AddInAnything(1+(inlets < 0?1:inlets));
- AddOutAnything();
-
- const t_symbol *funnm = NULL;
-
- // init script module
- if(argc) {
- AddCurrentPath(this);
-
- const char *sn = GetAString(*argv);
- argv++,argc--;
-
- if(sn) {
- char modnm[64];
- strcpy(modnm,sn);
-
- char *pt = strrchr(modnm,'.'); // search for last dot
- if(pt && *pt) {
- funnm = MakeSymbol(pt+1);
- *pt = 0;
- }
-
- if(*modnm)
- ImportModule(modnm);
- else
- ImportModule(NULL);
- }
- else
- PyErr_SetString(PyExc_ValueError,"Invalid module name");
- }
-
- Register(GetRegistry(REGNAME));
-
- if(funnm || argc) {
- if(!funnm) {
- funnm = GetASymbol(*argv);
- argv++,argc--;
- }
-
- if(funnm)
- SetFunction(funnm);
- else
- PyErr_SetString(PyExc_ValueError,"Invalid function name");
- }
-
- if(argc) args(argc,argv);
-
- Report();
-
- PyUnlock(state);
-}
-
-pyobj::~pyobj()
-{
- ThrState state = PyLockSys();
- if(objects) {
- for(int i = 0; i < CntIn()-1; ++i) Py_DECREF(objects[i]);
- delete[] objects;
- }
-
- Unregister(GetRegistry(REGNAME));
- Report();
- PyUnlock(state);
-}
-
-void pyobj::Exit()
-{
- pybase::Exit();
- flext_base::Exit();
-}
-
-void pyobj::m_set(int argc,const t_atom *argv)
-{
- ThrState state = PyLockSys();
-
- // function name has precedence
- if(argc >= 2) {
- const char *sn = GetAString(*argv);
- ++argv,--argc;
-
- if(sn) {
-// if(!module || !strcmp(sn,PyModule_GetName(module)))
- {
- ImportModule(sn);
- Register(GetRegistry(REGNAME));
- }
- }
- else
- PyErr_SetString(PyExc_ValueError,"Invalid module name");
- }
-
- if(argc) {
- const t_symbol *fn = GetASymbol(*argv);
- if(fn)
- SetFunction(fn);
- else
- PyErr_SetString(PyExc_ValueError,"Invalid function name");
- }
-
- Report();
-
- PyUnlock(state);
-}
-
-void pyobj::m_help()
-{
- post("");
- post("%s %s - python script object, (C)2002-2008 Thomas Grill",thisName(),PY__VERSION);
-#ifdef FLEXT_DEBUG
- post("DEBUG VERSION, compiled on " __DATE__ " " __TIME__);
-#endif
-
- post("Arguments: %s [script name] [function name] {args...}",thisName());
-
- post("Inlet 1:messages to control the py object");
- post(" 2:call python function with message as argument(s)");
- post("Outlet: 1:return values from python function");
- post("Methods:");
- post("\thelp: shows this help");
- post("\tbang: call script without arguments");
- post("\tset [script name] [function name]: set (script and) function name");
- post("\treload {args...}: reload python script");
- post("\treload. : reload with former arguments");
- post("\tdoc: display module doc string");
- post("\tdoc+: display function doc string");
- post("\tdir: dump module dictionary");
- post("\tdir+: dump function dictionary");
-#ifdef FLEXT_THREADS
- post("\tdetach 0/1/2: detach threads");
- post("\tstop {wait time (ms)}: stop threads");
-#endif
- post("");
-}
-
-bool pyobj::ResetFunction()
-{
- // function was borrowed from dict!
- function = NULL;
-
- if(!dict)
- post("%s - No namespace available",thisName());
- else {
- if(funname) {
- function = PyDict_GetItemString(dict,(char *)GetString(funname)); // borrowed!!!
-
- if(!function && dict == module_dict)
- // search also in __builtins__
- function = PyDict_GetItemString(builtins_dict,(char *)GetString(funname)); // borrowed!!!
-
- if(!function)
- PyErr_SetString(PyExc_AttributeError,"Function not found");
- else if(!PyCallable_Check(function)) {
- function = NULL;
- PyErr_SetString(PyExc_TypeError,"Attribute is not callable");
- }
- }
- }
-
- // exception could be set here
- return function != NULL;
-}
-
-bool pyobj::SetFunction(const t_symbol *func)
-{
- if(func) {
- funname = func;
- withfunction = ResetFunction();
- }
- else {
- function = NULL,funname = NULL;
- withfunction = false;
- }
-
- // exception could be set here
- return withfunction;
-}
-
-
-void pyobj::LoadModule()
-{
- SetFunction(funname);
-}
-
-void pyobj::UnloadModule()
-{
-}
-
-void pyobj::Load()
-{
- ResetFunction();
-}
-
-void pyobj::Unload()
-{
-// SetFunction(NULL);
- function = NULL; // just clear the PyObject, not the function name
-}
-
-void pyobj::callpy(PyObject *fun,PyObject *args)
-{
- PyObject *ret = PyObject_CallObject(fun,args);
- if(ret) {
- OutObject(this,0,ret); // exception might be raised here
- Py_DECREF(ret);
- }
-}
-
-bool pyobj::CbMethodResort(int n,const t_symbol *s,int argc,const t_atom *argv)
-{
- if(n == 0 && s != sym_bang)
- return flext_base::CbMethodResort(n,s,argc,argv);
-
- ThrState state = PyLockSys();
-
- bool ret = false;
-
- if(objects && n >= 1) {
- // store args
- PyObject *&obj = objects[n-1];
- Py_DECREF(obj);
- obj = MakePyArg(s,argc,argv); // steal reference
-
- if(n > 1) ret = true; // just store, don't trigger
- }
-
- if(!ret) {
- if(withfunction) {
- if(function) {
- Py_INCREF(function);
-
- PyObject *pargs;
-
- if(objects || CntIn() == 1) {
- int inlets = CntIn()-1;
- pargs = PyTuple_New(inlets);
- for(int i = 0; i < inlets; ++i) {
- Py_INCREF(objects[i]);
- PyTuple_SET_ITEM(pargs,i,objects[i]);
- }
- }
- else
- // construct tuple from args
- // if n == 0, it's a pure bang
- pargs = MakePyArgs(n?s:NULL,argc,argv);
-
- gencall(function,pargs); // references are stolen
- ret = true;
- }
- else
- PyErr_SetString(PyExc_RuntimeError,"No function set");
- }
- else if(module) {
- // no function defined as creation argument -> use message tag
- if(s) {
- PyObject *func = PyObject_GetAttrString(module,const_cast<char *>(GetString(s)));
- if(func) {
- PyObject *pargs = MakePyArgs(sym_list,argc,argv);
- gencall(func,pargs);
- ret = true;
- }
- }
- else
- PyErr_SetString(PyExc_RuntimeError,"No function set");
- }
-
- Report();
- }
-
- PyUnlock(state);
-
- Respond(ret);
-
- return ret;
-}
-
-void pyobj::CbClick() { pybase::OpenEditor(); }
-
-void pyobj::DumpOut(const t_symbol *sym,int argc,const t_atom *argv)
-{
- ToOutAnything(GetOutAttr(),sym?sym:thisTag(),argc,argv);
-}
diff --git a/externals/grill/py/source/pyargs.cpp b/externals/grill/py/source/pyargs.cpp
deleted file mode 100644
index 73d8f027..00000000
--- a/externals/grill/py/source/pyargs.cpp
+++ /dev/null
@@ -1,231 +0,0 @@
-/*
-py/pyext - python external object for PD and MaxMSP
-
-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 $
-*/
-
-#include "pybase.h"
-#include "pyatom.h"
-
-static const t_symbol *symatom = flext::MakeSymbol(" py ");
-
-static PyObject *MakePyAtom(const t_atom &at)
-{
- if(flext::IsSymbol(at))
- return pySymbol_FromSymbol(flext::GetSymbol(at));
-#if 1
- else if(flext::CanbeFloat(at)) {
- // if a number can be an integer... let it be an integer!
- int ival = flext::GetAInt(at);
- double fval = flext::GetAFloat(at);
- return (double)ival == fval?PyInt_FromLong(ival):PyFloat_FromDouble(fval);
- }
-#else
- else if(flext::IsFloat(at))
- return PyFloat_FromDouble(flext::GetFloat(at));
- else if(flext::IsInt(at))
- return PyInt_FromLong(flext::GetInt(at));
-#endif
- return NULL;
-}
-
-static PyObject *MakePyAtom(int argc,const t_atom *argv)
-{
- if(argc != sizeof(size_t)/2) return NULL;
-
- size_t atom = 0;
- for(int i = sizeof(size_t)/2-1; i >= 0; --i)
- if(!flext::CanbeInt(argv[i])) {
- atom = 0;
- break;
- }
- else
- atom = (atom<<16)+flext::GetAInt(argv[i]);
-
- if(atom) {
- PyObject *el = PyAtom::Retrieve(atom);
- if(!el) el = Py_None; // object already gone....
- Py_INCREF(el);
- return el;
- }
- else
- return NULL;
-}
-
-PyObject *pybase::MakePyArgs(const t_symbol *s,int argc,const t_atom *argv,int inlet)
-{
- PyObject *ret,*el;
-
- if(s == symatom && (el = MakePyAtom(argc,argv)) != NULL) {
- ret = PyTuple_New(1);
- PyTuple_SET_ITEM(ret,0,el);
- }
- else {
- bool any = IsAnything(s);
- ret = PyTuple_New(argc+(any?1:0)+(inlet >= 0?1:0));
-
- int pix = 0;
-
- if(inlet >= 0)
- PyTuple_SET_ITEM(ret,pix++,PyInt_FromLong(inlet));
-
- if(any)
- PyTuple_SET_ITEM(ret,pix++,pySymbol_FromSymbol(s));
-
- for(int i = 0; i < argc; ++i) {
- el = MakePyAtom(argv[i]);
- if(!el) {
- post("py/pyext: cannot convert argument %i",any?i+1:i);
-
- el = Py_None;
- Py_INCREF(Py_None);
- }
-
- PyTuple_SET_ITEM(ret,pix++,el); // reference stolen
- }
- }
-
- return ret;
-}
-
-PyObject *pybase::MakePyArg(const t_symbol *s,int argc,const t_atom *argv)
-{
- PyObject *ret;
-
- if(s == symatom && (ret = MakePyAtom(argc,argv)) != NULL) {
- // ok!
- }
- else if(argc == 1 && !IsAnything(s))
- // convert atoms and one-element lists
- ret = MakePyAtom(*argv);
- else {
- bool any = s != sym_list;
- ret = PyTuple_New(argc+(any?1:0));
-
- int pix = 0;
- if(any)
- PyTuple_SET_ITEM(ret,pix++,pySymbol_FromSymbol(s));
-
- for(int i = 0; i < argc; ++i) {
- PyObject *el = MakePyAtom(argv[i]);
- if(!el) {
- post("py/pyext: cannot convert argument %i",any?i+1:i);
-
- el = Py_None;
- Py_INCREF(Py_None);
- }
-
- PyTuple_SET_ITEM(ret,pix++,el); // reference stolen
- }
- }
-
- return ret;
-}
-
-inline bool issym(PyObject *p)
-{
- return PyString_Check(p) || pySymbol_Check(p);
-}
-
-inline bool isseq(PyObject *p)
-{
- return PySequence_Check(p) && !issym(p);
-}
-
-const t_symbol *pybase::getone(t_atom &at,PyObject *arg)
-{
- if(PyInt_Check(arg)) { flext::SetInt(at,PyInt_AsLong(arg)); return sym_fint; }
- else if(PyLong_Check(arg)) { flext::SetInt(at,PyLong_AsLong(arg)); return sym_fint; }
- else if(PyFloat_Check(arg)) { flext::SetFloat(at,(float)PyFloat_AsDouble(arg)); return flext::sym_float; }
- else if(pySymbol_Check(arg)) { flext::SetSymbol(at,pySymbol_AS_SYMBOL(arg)); return flext::sym_symbol; }
- else if(PyString_Check(arg)) { flext::SetString(at,PyString_AS_STRING(arg)); return flext::sym_symbol; }
- else {
- PyObject *tp = PyObject_Type(arg);
- PyObject *stp = tp?PyObject_Str(tp):NULL;
- char *tmp = "";
- if(stp) tmp = PyString_AS_STRING(stp);
- flext::post("py/pyext: Could not convert argument %s",tmp);
- Py_XDECREF(stp);
- Py_XDECREF(tp);
-
- flext::SetSymbol(at,flext::sym__);
- return sym_symbol;
- }
-}
-
-const t_symbol *pybase::getlist(t_atom *lst,PyObject *seq,int cnt,int offs)
-{
- for(int ix = 0; ix < cnt; ++ix) {
- PyObject *arg = PySequence_GetItem(seq,ix+offs); // new reference
- getone(lst[ix],arg);
- Py_DECREF(arg);
- }
- return flext::sym_list;
-}
-
-const t_symbol *pybase::GetPyArgs(AtomList &lst,PyObject *pValue,int offs)
-{
- if(pValue == NULL) return false;
-
- // output bang on None returned
- if(pValue == Py_None) return sym_bang;
-
- // analyze return value or tuple
- const t_symbol *sym = NULL;
-
- if(isseq(pValue)) {
- // Python might crash here if pValue is no "real" sequence, but rather e.g. an instance
-
- int rargc = PySequence_Size(pValue);
-
- if(rargc == 2) {
- // check if syntax is symbol/string, list -> anything message
- PyObject *s = PySequence_GetItem(pValue,0);
- PyObject *l = PySequence_GetItem(pValue,1);
-
- if(issym(s) && isseq(l)) {
- // is anything message
- rargc = PySequence_Size(l);
- lst(offs+rargc);
- getlist(lst.Atoms(),l,rargc);
- sym = pyObject_AsSymbol(s);
- }
- else {
- // (symbol,atom) list
- lst(offs+rargc);
- sym = getlist(lst.Atoms(),pValue,rargc);
- }
-
- Py_DECREF(s);
- Py_DECREF(l);
- }
- else {
- lst(offs+rargc);
- sym = getlist(lst.Atoms(),pValue,rargc);
- }
- }
- else {
- lst(offs+1);
- sym = getone(lst[offs],pValue);
- }
-
- return sym;
-}
-
-
-const t_symbol *pybase::GetPyAtom(AtomList &lst,PyObject *obj)
-{
- size_t atom = PyAtom::Register(obj);
- size_t szat = sizeof(atom)/2;
-
- lst(szat);
- for(size_t i = 0; i < szat; ++i,atom >>= 16)
- flext::SetInt(lst[i],(int)(atom&((1<<16)-1)));
- return symatom;
-}
diff --git a/externals/grill/py/source/pyatom.cpp b/externals/grill/py/source/pyatom.cpp
deleted file mode 100644
index 7b45028e..00000000
--- a/externals/grill/py/source/pyatom.cpp
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
-py/pyext - python script object for PD and Max/MSP
-
-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 $
-*/
-
-#include "pyatom.h"
-#include <map>
-
-#define INTV 0.01
-
-typedef std::map<size_t,PyObject *> ObjMap;
-
-static ObjMap objmap;
-static size_t collix = 0,curix = 0;
-static double last = 0;
-
-size_t PyAtom::Register(PyObject *obj)
-{
- Collect();
-
- Py_INCREF(obj);
- objmap[++curix] = obj;
-
-#ifdef _DEBUG
-// post("REG %p (%i)\n",obj,objmap.size());
-#endif
- return curix;
-}
-
-PyObject *PyAtom::Retrieve(size_t id)
-{
- ObjMap::iterator it = objmap.find(id);
- PyObject *ret = it == objmap.end()?NULL:it->second;
- Collect();
- return ret;
-}
-
-void PyAtom::Collect()
-{
- for(;;) {
- ObjMap::iterator it = objmap.begin();
- if(it == objmap.end() || it->first > collix) break;
-
- PyObject *obj = it->second;
- Py_DECREF(obj);
- objmap.erase(it);
-
-#ifdef _DEBUG
-// post("DEL %p\n",obj);
-#endif
- }
-
- // schedule next collect time
- double tm = flext::GetTime();
- if(tm > last+INTV) last = tm,collix = curix;
-}
diff --git a/externals/grill/py/source/pyatom.h b/externals/grill/py/source/pyatom.h
deleted file mode 100644
index beff390a..00000000
--- a/externals/grill/py/source/pyatom.h
+++ /dev/null
@@ -1,21 +0,0 @@
-/*
-py/pyext - python script object for PD and Max/MSP
-
-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 $
-*/
-
-#include "main.h"
-
-class PyAtom
-{
-public:
- static size_t Register(PyObject *obj);
- static PyObject *Retrieve(size_t id);
- static void Collect();
-};
diff --git a/externals/grill/py/source/pybase.cpp b/externals/grill/py/source/pybase.cpp
deleted file mode 100644
index 033219e4..00000000
--- a/externals/grill/py/source/pybase.cpp
+++ /dev/null
@@ -1,923 +0,0 @@
-/*
-py/pyext - python external object for PD and MaxMSP
-
-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 18:00:03 +0100 (Thu, 03 Jan 2008) $
-$LastChangedBy: thomas $
-*/
-
-#include "pybase.h"
-#include <map>
-
-#if FLEXT_OS == FLEXT_OS_WIN
-#include <windows.h>
-#elif FLEXT_OS == FLEXT_OS_MAC
-#include <ApplicationServices/ApplicationServices.h>
-#endif
-
-static PyMethodDef StdOut_Methods[] =
-{
- { "write", pybase::StdOut_Write, 1 },
- { NULL, NULL, }
-};
-
-static PyObject *gcollect = NULL;
-
-#ifdef FLEXT_THREADS
-
-class ThrCmp
-{
-public:
- inline bool operator()(const flext::thrid_t &a,const flext::thrid_t &b) const
- {
- if(sizeof(a) == sizeof(size_t))
- return *(size_t *)&a < *(size_t *)&b;
- else
- return memcmp(&a,&b,sizeof(a)) < 0;
- }
-};
-
-
-int pybase::lockcount = 0;
-
-#ifndef PY_USE_GIL
-static PyInterpreterState *pymain = NULL;
-
-typedef std::map<flext::thrid_t,ThrState,ThrCmp> PyThrMap;
-
-static PyThrMap pythrmap;
-ThrState pybase::pythrsys = NULL;
-
-ThrState pybase::FindThreadState()
-{
- flext::thrid_t id = flext::GetThreadId();
- PyThrMap::iterator it = pythrmap.find(id);
- if(it == pythrmap.end()) {
- // Make new thread state
- ThrState st = PyThreadState_New(pymain);
- pythrmap[id] = st;
- return st;
- }
- else
- return it->second;
-}
-
-void pybase::FreeThreadState()
-{
- flext::thrid_t id = flext::GetThreadId();
- PyThrMap::iterator it = pythrmap.find(id);
- if(it != pythrmap.end()) {
- // clear out any cruft from thread state object
- PyThreadState_Clear(it->second);
- // delete my thread state object
- PyThreadState_Delete(it->second);
- // delete from map
- pythrmap.erase(it);
- }
-}
-#endif // PY_USE_GIL
-
-PyFifo pybase::qufifo;
-flext::ThrCond pybase::qucond;
-#endif
-
-
-PyObject *pybase::module_obj = NULL;
-PyObject *pybase::module_dict = NULL;
-
-PyObject *pybase::builtins_obj = NULL;
-PyObject *pybase::builtins_dict = NULL;
-
-const t_symbol *pybase::sym_fint = NULL;
-const t_symbol *pybase::sym_response = NULL;
-
-// -----------------------------------------------------------------------------------------------------------
-
-
-void initsymbol();
-void initsamplebuffer();
-void initbundle();
-
-void pybase::lib_setup()
-{
- post("");
- post("------------------------------------------------");
- post("py/pyext %s - python script objects",PY__VERSION);
- post("(C)2002-2008 Thomas Grill - http://grrrr.org/ext");
- post("");
- post("using Python %s",Py_GetVersion());
-
-#ifdef FLEXT_DEBUG
- post("");
- post("DEBUG version compiled on %s %s",__DATE__,__TIME__);
-#endif
-
- // -------------------------------------------------------------
-
- sym_response = flext::MakeSymbol("response");
-
-#if FLEXT_SYS == FLEXT_SYS_PD
- sym_fint = sym_float;
-#else
- sym_fint = sym_int;
-#endif
-
- // -------------------------------------------------------------
-
- Py_Initialize();
-
-#ifdef FLEXT_DEBUG
-// Py_DebugFlag = 1;
-// Py_VerboseFlag = 1;
-#else
- Py_OptimizeFlag = 1;
-#endif
-
-#ifdef FLEXT_THREADS
- // enable thread support and acquire the global thread lock
- PyEval_InitThreads();
-
-#ifndef PY_USE_GIL
- // get thread state
- pythrsys = PyThreadState_Get();
- // get main interpreter state
- pymain = pythrsys->interp;
-
- // add thread state of main thread to map
- pythrmap[GetThreadId()] = pythrsys;
-#endif // PY_USE_GIL
-
-#endif
-
- // sys.argv must be set to empty tuple
- char *nothing = "";
- PySys_SetArgv(0,&nothing);
-
- // register/initialize pyext module only once!
- module_obj = Py_InitModule(PYEXT_MODULE, func_tbl);
- module_dict = PyModule_GetDict(module_obj); // borrowed reference
-
- PyModule_AddStringConstant(module_obj,"__doc__",(char *)py_doc);
-
- // redirect stdout
- PyObject* py_out;
- py_out = Py_InitModule("stdout", StdOut_Methods);
- PySys_SetObject("stdout", py_out);
- py_out = Py_InitModule("stderr", StdOut_Methods);
- PySys_SetObject("stderr", py_out);
-
- // get garbage collector function
- PyObject *gcobj = PyImport_ImportModule("gc");
- if(gcobj) {
- gcollect = PyObject_GetAttrString(gcobj,"collect");
- Py_DECREF(gcobj);
- }
-
- builtins_obj = PyImport_ImportModule("__builtin__");
- builtins_dict = PyModule_GetDict(builtins_obj); // borrowed reference
-
- // add symbol type
- initsymbol();
- PyModule_AddObject(module_obj,"Symbol",(PyObject *)&pySymbol_Type);
-
- // pre-defined symbols
- PyModule_AddObject(module_obj,"_s_",(PyObject *)pySymbol__);
- PyModule_AddObject(module_obj,"_s_bang",(PyObject *)pySymbol_bang);
- PyModule_AddObject(module_obj,"_s_list",(PyObject *)pySymbol_list);
- PyModule_AddObject(module_obj,"_s_symbol",(PyObject *)pySymbol_symbol);
- PyModule_AddObject(module_obj,"_s_float",(PyObject *)pySymbol_float);
- PyModule_AddObject(module_obj,"_s_int",(PyObject *)pySymbol_int);
-
- // add samplebuffer type
- initsamplebuffer();
- PyModule_AddObject(module_obj,"Buffer",(PyObject *)&pySamplebuffer_Type);
-
- // add message bundle type
- initbundle();
- PyModule_AddObject(module_obj,"Bundle",(PyObject *)&pyBundle_Type);
-
- // -------------------------------------------------------------
-#if FLEXT_SYS == FLEXT_SYS_PD && defined(PD_DEVEL_VERSION) && defined(PY_USE_INOFFICIAL)
- // add PD paths
-
- char *dir;
- for(int i = 0; (dir = namelist_get(sys_searchpath,i)) != NULL; ++i) {
- AddToPath(dir);
- }
-#endif
- // -------------------------------------------------------------
-
- FLEXT_SETUP(pyobj);
- FLEXT_SETUP(pymeth);
- FLEXT_SETUP(pyext);
-#ifndef PY_NODSP
- FLEXT_DSP_SETUP(pydsp);
-#endif
-
-#ifdef FLEXT_THREADS
- // release global lock
- PyEval_ReleaseLock();
-
- // launch thread worker
- LaunchThread(quworker,NULL);
-
- // launch python worker
- LaunchThread(pyworker,NULL);
-#endif
-
- post("------------------------------------------------");
- post("");
-}
-
-FLEXT_LIB_SETUP(py,pybase::lib_setup)
-
-
-// -----------------------------------------------------------------------------------------------------------
-
-
-pybase::pybase()
- : module(NULL)
- , dict(NULL)
-#ifdef FLEXT_THREADS
- , thrcount(0)
- , shouldexit(false),stoptick(0)
-#endif
- , detach(0)
- , pymsg(false)
-{
- ThrState state = PyLock();
- Py_INCREF(module_obj);
- PyUnlock(state);
-}
-
-pybase::~pybase()
-{
- ThrState state = PyLock();
- Py_XDECREF(module_obj);
- PyUnlock(state);
-}
-
-void pybase::Exit()
-{
-#ifdef FLEXT_THREADS
- erasethreads();
-
- shouldexit = true;
- qucond.Signal();
-
- if(thrcount) {
- // Wait for a certain time
- for(int i = 0; i < (PY_STOP_WAIT/PY_STOP_TICK) && thrcount; ++i)
- Sleep(PY_STOP_TICK*0.001f);
- if(thrcount) {
- // Wait forever
- post("py/pyext - Waiting for thread termination!");
- while(thrcount) Sleep(PY_STOP_TICK*0.001f);
- post("py/pyext - Okay, all threads have terminated");
- }
- }
-#endif
-}
-
-void pybase::GetDir(PyObject *obj,AtomList &lst)
-{
- if(obj) {
- ThrState state = PyLock();
-
- PyObject *pvar = PyObject_Dir(obj);
- if(!pvar)
- PyErr_Print(); // no method found
- else {
- const t_symbol *sym = GetPyArgs(lst,pvar);
- if(!sym)
- post("py/pyext - Argument list could not be created");
- else
- FLEXT_ASSERT(sym == sym_list);
- Py_DECREF(pvar);
- }
-
- PyUnlock(state);
- }
-}
-
-void pybase::m__dir(PyObject *obj)
-{
- AtomList lst;
- GetDir(obj,lst);
- // dump dir to attribute outlet
- DumpOut(NULL,lst.Count(),lst.Atoms());
-}
-
-void pybase::m__doc(PyObject *obj)
-{
- if(obj) {
- ThrState state = PyLock();
-
- PyObject *docf = PyDict_GetItemString(obj,"__doc__"); // borrowed!!!
- if(docf && PyString_Check(docf)) {
- post("");
- const char *s = PyString_AS_STRING(docf);
-
- // FIX: Python doc strings can easily be larger than 1k characters
- // -> split into separate lines
- for(;;) {
- char buf[1024];
- char *nl = strchr((char *)s,'\n'); // the cast is for Borland C++
- if(!nl) {
- // no more newline found
- post(s);
- break;
- }
- else {
- // copy string before newline to temp buffer and post
- unsigned int l = nl-s;
- if(l >= sizeof(buf)) l = sizeof buf-1;
- strncpy(buf,s,l); // copy all but newline
- buf[l] = 0;
- post(buf);
- s = nl+1; // set after newline
- }
- }
- }
-
- PyUnlock(state);
- }
-}
-
-void pybase::OpenEditor()
-{
- if(!module) return;
- const char *mname = PyModule_GetFilename(module);
- if(!mname) {
- PyErr_Clear();
- return;
- }
-
- char fname[1024];
- strcpy(fname,mname);
-
- // replacing .pyc or .pyo for source file name
- char *dt = strrchr(fname,'.');
- if(dt && !strncmp(dt,".py",2)) strcpy(dt,".py");
-
- // this should open the editor....
-#if FLEXT_OS == FLEXT_OS_WIN
- int err = (int)ShellExecute(NULL,"edit",fname,NULL,NULL,SW_SHOW);
- if(err == SE_ERR_NOASSOC) {
- // no association found - try notepad
- err = (int)ShellExecute(NULL,NULL,"notepad.exe",fname,NULL,SW_SHOW);
- }
- else if(err == ERROR_FILE_NOT_FOUND || err == SE_ERR_FNF)
- post("py/pyext - File not %s found",fname);
- else if(err <= 32)
- post("py/pyext - Unknown error opening %s",fname);
-
-#elif FLEXT_OS == FLEXT_OS_MAC
- FSRef ref;
- OSStatus err = FSPathMakeRef((unsigned char *)fname,&ref,NULL);
- if(err)
- post("py/pyext - Error interpreting path %s",fname);
- else {
- FSRef editor;
- err = LSGetApplicationForItem(&ref,kLSRolesEditor,&editor,NULL);
- if(err) {
- // Can't find associated application... try Textedit
- err = FSPathMakeRef((unsigned char *)"/Applications/TextEdit.app",&editor,NULL);
- if(err)
- post("py/pyext - Can't find Textedit application");
- }
-
- if(!err) {
- LSLaunchFSRefSpec lspec;
- lspec.appRef = &editor;
- lspec.numDocs = 1;
- lspec.itemRefs = &ref;
- lspec.passThruParams = NULL;
- lspec.launchFlags = kLSLaunchDefaults;
- lspec.asyncRefCon = NULL;
- err = LSOpenFromRefSpec(&lspec,NULL);
- if(err)
- post("py/pyext - Couldn't launch editor");
- }
- }
-#else
- // thanks to Tim Blechmann
-
- char *editor = getenv("EDITOR");
-
- if(!editor) { // || !strcmp(editor, "/usr/bin/nano") || !strcmp(editor, "/usr/bin/pico") || !strcmp(editor, "/usr/bin/vi")) {
- // no environment variable or console text editor found ... use idle instead (should have come with Python)
- editor = "idle";
- }
-
- pid_t child = fork();
- if(!child) {
- char cmd[80];
- strcpy(cmd,editor);
- strcat(cmd," ");
- strcat(cmd,fname);
- execl("/bin/sh", "sh", "-c", cmd, (char *) NULL);
- }
-#endif
-}
-
-void pybase::SetArgs()
-{
- // script arguments
- int argc = args.Count();
- const t_atom *argv = args.Atoms();
- char **sargv = new char *[argc+1];
- for(int i = 0; i <= argc; ++i) {
- sargv[i] = new char[256];
- if(!i)
- strcpy(sargv[i],"py/pyext");
- else
- GetAString(argv[i-1],sargv[i],255);
- }
-
- // the arguments to the module are only recognized once! (at first use in a patcher)
- PySys_SetArgv(argc+1,sargv);
-
- for(int j = 0; j <= argc; ++j) delete[] sargv[j];
- delete[] sargv;
-}
-
-static bool getmodulesub(const char *mod,char *dir,int len,char *ext)
-{
-#if FLEXT_SYS == FLEXT_SYS_PD
- char *name;
- int fd = open_via_path("",mod,ext,dir,&name,len,0);
- if(fd > 0) {
- FLEXT_ASSERT(name && *name);
- close(fd);
- }
- else {
- // look for mod/__init__.py
- std::string tmp(mod);
- int l = tmp.size();
- tmp += "/__init__";
- fd = open_via_path("",tmp.c_str(),ext,dir,&name,len,0);
- if(fd > 0) {
- FLEXT_ASSERT(name && *name);
- close(fd);
- // we must remove the module name from dir
- char *t = dir+strlen(dir)-l;
- FLEXT_ASSERT(!strcmp(mod,t) && t[-1] == '/');
- t[-1] = 0;
- }
- else
- name = NULL;
- }
-
- // if dir is current working directory... name points to dir
- if(dir == name) strcpy(dir,".");
- return name != NULL;
-#elif FLEXT_SYS == FLEXT_SYS_MAX
- short path;
- long type;
- char smod[1024];
- strcpy(smod,mod);
- strcat(smod,ext);
- bool ok = !locatefile_extended(smod,&path,&type,&type,0);
- if(ok)
- // convert pathname to unix style
- path_topathname(path,NULL,smod);
- else {
- // do path/file.ext combinations work at all under Max?
- strcpy(smod,mod);
-
- short path;
- type = 'fold';
- ok = !locatefile_extended(smod,&path,&type,&type,1);
- if(ok) {
- // convert pathname to unix style (including trailing slash)
- path_topathname(path,NULL,smod);
- char *end = smod+strlen(smod);
- strcpy(end,mod);
- strcat(end,"/__init__");
- strcat(end,ext);
-
- // check if file is really existing: try to open it
- FILE *f = fopen(smod,"r");
- if(f) {
- *end = 0; // clear module part ... we only need base path
- fclose(f);
- }
- else
- ok = false;
- }
- }
-
- if(ok) {
- // convert path into slash style needed for Python
-#if 0
- // Max API function uses Volume:/Path notation
- path_nameconform(smod,dir,PATH_STYLE_SLASH,PATH_TYPE_ABSOLUTE);
-#else
-#if FLEXT_OS == FLEXT_OS_WIN
- char *colon = NULL;
-#else
- char *colon = strchr(smod,':');
-#endif
- if(colon) {
- *colon = 0;
- strcpy(dir,"/Volumes/");
- strcat(dir,smod);
- strcat(dir,colon+1);
- }
- else
- strcpy(dir,smod);
-#endif
- return true;
- }
- else
- // not found
- return false;
-#else
-#pragma message("Not implemented");
- return false;
-#endif
-}
-
-static bool getmodulepath(const char *mod,char *dir,int len)
-{
- return
- getmodulesub(mod,dir,len,".py") ||
- getmodulesub(mod,dir,len,".pyc") ||
- getmodulesub(mod,dir,len,".pyo");
-}
-
-bool pybase::ImportModule(const char *name)
-{
- if(name) {
- if(modname == name) {
- // module with the same name is already loaded
- if(module) return true;
- }
- else
- modname = name;
- }
- else
- modname.clear();
-
- UnimportModule();
- return ReloadModule();
-}
-
-void pybase::UnimportModule()
-{
- if(module) {
- FLEXT_ASSERT(dict && module_obj && module_dict);
-
- Py_DECREF(module);
-
- // reference count to module is not 0 here, altough probably the last instance was unloaded
- // Python retains one reference to the module all the time
- // we don't care
-
- module = NULL;
- dict = NULL;
- }
-}
-
-bool pybase::ReloadModule()
-{
- SetArgs();
- PyObject *newmod;
-
- if(modname.length()) {
-
- if(module)
- newmod = PyImport_ReloadModule(module);
- else {
- // search in module path (TODO: check before if module is already present to avoid costly searching)
- char dir[1024];
- if(!getmodulepath(modname.c_str(),dir,sizeof(dir)))
- PyErr_SetString(PyExc_ImportError,"Module not found in path");
- else
- AddToPath(dir);
-
- // module could also be loaded ok, even if it's not in the path (e.g. for internal stuff)
- newmod = PyImport_ImportModule((char *)modname.c_str());
- }
- }
- else {
- // if no module name given, take py module
- newmod = module_obj;
- Py_INCREF(newmod);
- }
-
- if(!newmod) {
- // unload faulty module
- UnimportModule();
- return false;
- }
- else {
- Py_XDECREF(module);
- module = newmod;
- dict = PyModule_GetDict(module); // borrowed
- return true;
- }
-}
-
-void pybase::AddToPath(const char *dir)
-{
- if(dir && *dir) {
- PyObject *pobj = PySys_GetObject("path");
- if(pobj && PyList_Check(pobj)) {
- PyObject *ps = PyString_FromString(dir);
- if(!PySequence_Contains(pobj,ps))
- PyList_Append(pobj,ps); // makes new reference
- Py_DECREF(ps);
- }
- PySys_SetObject("path",pobj); // steals reference to pobj
- }
-}
-
-void pybase::AddCurrentPath(flext_base *o)
-{
- char dir[1024];
-
- // add dir of current patch to path
- o->GetCanvasDir(dir,sizeof(dir));
- if(*dir) AddToPath(dir);
-
- // add current dir to path
-#if FLEXT_SYS == FLEXT_SYS_PD
- AddToPath(GetString(canvas_getcurrentdir()));
-#elif FLEXT_SYS == FLEXT_SYS_MAX
- short path = path_getdefault();
- path_topathname(path,NULL,dir);
- AddToPath(dir);
-#endif
-}
-
-bool pybase::OutObject(flext_base *ext,int o,PyObject *obj)
-{
- flext::AtomListStatic<16> lst;
- const t_symbol *sym = pymsg?GetPyAtom(lst,obj):GetPyArgs(lst,obj);
- if(sym) {
- // call to outlet _outside_ the Mutex lock!
- // otherwise (if not detached) deadlock will occur
- ext->ToOutAnything(o,sym,lst.Count(),lst.Atoms());
- return true;
- }
- else
- return false;
-}
-
-void pybase::Reload()
-{
- ThrState state = PyLock();
-
- PyObject *reg = GetRegistry(REGNAME);
-
- if(reg) {
- PyObject *key;
- Py_ssize_t pos = 0;
- while(PyDict_Next(reg,&pos,&key,NULL)) {
- pybase *th = (pybase *)PyLong_AsLong(key);
- FLEXT_ASSERT(th);
- th->Unload();
- }
-
- UnloadModule();
- }
-
- bool ok = ReloadModule();
-
- if(ok) {
- LoadModule();
-
- if(reg) {
- SetRegistry(REGNAME,reg);
-
- PyObject *key;
- Py_ssize_t pos = 0;
- while(PyDict_Next(reg,&pos,&key,NULL)) {
- pybase *th = (pybase *)PyLong_AsLong(key);
- FLEXT_ASSERT(th);
- th->Load();
- }
- }
- else
- Load();
- }
-
- Report();
- PyUnlock(state);
-}
-
-static PyObject *output = NULL;
-
-// post to the console
-PyObject* pybase::StdOut_Write(PyObject* self, PyObject* args)
-{
- // should always be a tuple
- FLEXT_ASSERT(PyTuple_Check(args));
-
- const int sz = PyTuple_GET_SIZE(args);
-
- for(int i = 0; i < sz; ++i) {
- PyObject *val = PyTuple_GET_ITEM(args,i); // borrowed reference
- PyObject *str = PyObject_Str(val); // new reference
- char *cstr = PyString_AS_STRING(str);
- char *lf = strchr(cstr,'\n');
-
- // line feed in string
- if(!lf) {
- // no -> just append
- if(output)
- PyString_ConcatAndDel(&output,str); // str is decrefd
- else
- output = str; // take str reference
- }
- else {
- // yes -> append up to line feed, reset output buffer to string remainder
- PyObject *part = PyString_FromStringAndSize(cstr,lf-cstr); // new reference
- if(output)
- PyString_ConcatAndDel(&output,part); // str is decrefd
- else
- output = part; // take str reference
-
- // output concatenated string
- post(PyString_AS_STRING(output));
-
- Py_DECREF(output);
- output = PyString_FromString(lf+1); // new reference
- }
- }
-
- Py_INCREF(Py_None);
- return Py_None;
-}
-
-
-class work_data
-{
-public:
- work_data(PyObject *f,PyObject *a): fun(f),args(a) {}
- ~work_data() { Py_DECREF(fun); Py_DECREF(args); }
-
- PyObject *fun,*args;
-};
-
-bool pybase::gencall(PyObject *pmeth,PyObject *pargs)
-{
- bool ret = false;
-
- // Now call method
- switch(detach) {
- case 0:
- ret = docall(pmeth,pargs);
- Py_DECREF(pargs);
- Py_DECREF(pmeth);
- break;
-#ifdef FLEXT_THREADS
- case 1:
- // put call into queue
- ret = qucall(pmeth,pargs);
- break;
- case 2:
- // each call a new thread
- if(!shouldexit) {
- thr_params *p = new thr_params;
- p->cl = (flext_base *)this;
- p->var->_ext = new work_data(pmeth,pargs);
- ret = LaunchThread(thrworker,p);
- if(!ret) post("py/pyext - Failed to launch thread!");
- }
- break;
-#endif
- default:
- post("py/pyext - Unknown detach mode");
- }
- return ret;
-}
-
-void pybase::exchandle()
-{
-#if 0
- // want to use that, but exception keeps a reference to the object
- // might be a Python bug!
- PyErr_Print();
-#else
- // must use that instead... clear the exception
- PyObject *type,*value,*traceback;
- PyErr_Fetch(&type,&value,&traceback);
- PyErr_NormalizeException(&type,&value,&traceback);
- PyErr_Display(type,value,traceback);
-
- Py_XDECREF(type);
- Py_XDECREF(value);
- Py_XDECREF(traceback);
-#endif
-}
-
-#ifdef FLEXT_THREADS
-void pybase::thrworker(thr_params *p)
-{
- FLEXT_ASSERT(p);
- pybase *th = (pybase *)p->cl;
- work_data *w = (work_data *)p->var->_ext;
-
- ++th->thrcount; // \todo this should be atomic
- ThrState state = PyLock();
-
- // call worker
- th->docall(w->fun,w->args);
- delete w;
-
- PyUnlock(state);
- --th->thrcount; // \todo this should be atomic
-}
-
-/*! This thread function basically keeps alive the Python interpreter in the background
- It's good for threads that have been started from scripted functions
-*/
-void pybase::pyworker(thr_params *)
-{
- ThrState state = PyLock();
-
- PyObject *timemod = PyImport_ImportModule("time");
- PyObject *sleep = PyObject_GetAttrString(timemod,"sleep");
- PyObject *args = PyTuple_New(1);
- PyTuple_SET_ITEM(args,0,PyFloat_FromDouble(1000000));
-
- for(;;) {
- PyObject *res = PyObject_CallObject(sleep,args);
- Py_DECREF(res);
- }
-
- PyUnlock(state);
-}
-
-void pybase::quworker(thr_params *)
-{
- FifoEl *el;
- ThrState my = FindThreadState(),state;
-
- for(;;) {
- while(el = qufifo.Get()) {
- ++el->th->thrcount; // \todo this should be atomic
- state = PyLock(my);
- el->th->docall(el->fun,el->args);
- Py_XDECREF(el->fun);
- Py_XDECREF(el->args);
- PyUnlock(state);
- --el->th->thrcount; // \todo this should be atomic
- qufifo.Free(el);
- }
- qucond.Wait();
- }
-
- // we never end
-#if 0
- state = PyLock(my);
- // unref remaining Python objects
- while(el = qufifo.Get()) {
- Py_XDECREF(el->fun);
- Py_XDECREF(el->args);
- qufifo.Free(el);
- }
- PyUnlock(state);
-#endif
-}
-
-void pybase::erasethreads()
-{
- PyFifo tmp;
- FifoEl *el;
- while(el = qufifo.Get()) {
- if(el->th == this) {
- Py_XDECREF(el->fun);
- Py_XDECREF(el->args);
- qufifo.Free(el);
- }
- else
- tmp.Put(el);
- }
- // Push back
- while(el = tmp.Get()) qufifo.Put(el);
-}
-#endif
-
-bool pybase::collect()
-{
- if(gcollect) {
- PyObject *ret = PyObject_CallObject(gcollect,NULL);
- if(ret) {
-#ifdef FLEXT_DEBUG
- int refs = PyInt_AsLong(ret);
- if(refs) post("py/pyext - Garbage collector reports %i unreachable objects",refs);
-#endif
- Py_DECREF(ret);
- return false;
- }
- }
- return true;
-}
diff --git a/externals/grill/py/source/pybase.h b/externals/grill/py/source/pybase.h
deleted file mode 100644
index 48e2f772..00000000
--- a/externals/grill/py/source/pybase.h
+++ /dev/null
@@ -1,256 +0,0 @@
-/*
-py/pyext - python script object for PD and MaxMSP
-
-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
-#define __PYBASE_H
-
-#include "main.h"
-#include "pysymbol.h"
-#include "pybuffer.h"
-#include "pybundle.h"
-
-#ifdef PY_USE_GIL
- typedef PyGILState_STATE ThrState;
-#else
- typedef PyThreadState *ThrState;
-#endif
-
-class pybase
- : public flext
-{
-public:
- 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 void lib_setup();
-
-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_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
-
- static const char *py_doc;
-
- void GetDir(PyObject *obj,AtomList &lst);
-
- AtomList args;
-
- void AddCurrentPath(flext_base *o);
- 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();
-
- // Get module registry
- PyObject *GetRegistry(const char *regname);
- // Set module registry
- void SetRegistry(const char *regname,PyObject *reg);
-
- // Register object
- void Register(PyObject *reg);
- // Unregister object
- void Unregister(PyObject *reg);
-
- virtual void LoadModule() = 0;
- virtual void UnloadModule() = 0;
-
- virtual void Load() = 0;
- virtual void Unload() = 0;
-
- void OpenEditor();
-
- void Respond(bool b)
- {
- if(respond) {
- t_atom a;
- SetBool(a,b);
- DumpOut(sym_response,1,&a);
- }
- }
-
- 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; }
-
-// enum retval { nothing,atom,sequ };
-
- // --- module stuff -----
-
- 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);
-#ifdef FLEXT_THREADS
- 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_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);
-#endif
-
- static PyObject *py_list(PyObject *,PyObject *args);
- static PyObject *py_tuple(PyObject *,PyObject *args);
-
- // ----thread stuff ------------
-
- virtual void m_stop(int argc,const t_atom *argv);
-
- bool respond;
-#ifdef FLEXT_THREADS
- int thrcount;
- bool shouldexit;
- int stoptick;
- Timer stoptmr;
-
- void tick(void *);
-#endif
-
- int detach;
- bool pymsg;
-
- bool gencall(PyObject *fun,PyObject *args);
-
- bool docall(PyObject *fun,PyObject *args)
- {
- callpy(fun,args);
- if(PyErr_Occurred()) {
- exchandle();
- return false;
- }
- else
- return true;
- }
-
- virtual void callpy(PyObject *fun,PyObject *args) = 0;
-
- void exchandle();
-
- static bool collect();
-
-protected:
-
-#ifdef FLEXT_THREADS
- static void thrworker(thr_params *data);
-
- bool qucall(PyObject *fun,PyObject *args)
- {
- FifoEl *el = qufifo.New();
- el->Set(this,fun,args);
- qufifo.Put(el);
- qucond.Signal();
- return true;
- }
-
- static void quworker(thr_params *);
- static void pyworker(thr_params *);
- void erasethreads();
-
- static PyFifo qufifo;
- static ThrCond qucond;
-
-#ifndef PY_USE_GIL
- static ThrState pythrsys;
-#endif
-#endif
-
- static const t_symbol *sym_fint; // float or int symbol, depending on native number message type
- static const t_symbol *sym_response;
-
- static const t_symbol *getone(t_atom &at,PyObject *arg);
- static const t_symbol *getlist(t_atom *lst,PyObject *seq,int cnt,int offs = 0);
-
-public:
-
- static void AddToPath(const char *dir);
-
-#ifdef FLEXT_THREADS
- // this is especially needed when one py/pyext object calls another one
- // we don't want the message to be queued, but otoh we have to avoid deadlock
- // (recursive calls can only happen in the system thread)
- static int lockcount;
-
-#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); }
-#else // PY_USE_GIL
- static ThrState FindThreadState();
- static void FreeThreadState();
-
- static ThrState PyLock(ThrState st = FindThreadState())
- {
- if(st != pythrsys || !lockcount++) PyEval_AcquireLock();
- return PyThreadState_Swap(st);
- }
-
-#if 1
- static inline ThrState PyLockSys() { return PyLock(); }
-#else
- static ThrState PyLockSys()
- {
- if(!lockcount++) PyEval_AcquireLock();
- return PyThreadState_Swap(pythrsys);
- }
-#endif
-
- 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) {}
-#endif
-
- static PyObject* StdOut_Write(PyObject* Self, PyObject* Args);
-};
-
-#endif
diff --git a/externals/grill/py/source/pybuffer.cpp b/externals/grill/py/source/pybuffer.cpp
deleted file mode 100644
index 82414e11..00000000
--- a/externals/grill/py/source/pybuffer.cpp
+++ /dev/null
@@ -1,828 +0,0 @@
-/*
-py/pyext - python script object for PD and Max/MSP
-
-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-04 12:24:43 +0100 (Fri, 04 Jan 2008) $
-$LastChangedBy: thomas $
-*/
-
-#include "pybase.h"
-
-#undef PY_ARRAYS
-
-
-#if defined(PY_NUMERIC) || defined(PY_NUMPY) || defined(PY_NUMARRAY)
- #define PY_ARRAYS 1
-#endif
-
-#ifdef PY_ARRAYS
-
-#ifdef PY_NUMARRAY
-# if FLEXT_OS == FLEXT_OS_MAC
-# include <Python/numarray/libnumarray.h>
-# else
-# include <numarray/libnumarray.h>
-# endif
-
-static NumarrayType numtype = tAny;
-inline bool arrsupport() { return numtype != tAny; }
-
-#else
-# if defined(PY_NUMPY)
-# include <numpy/arrayobject.h>
-# else
-# if FLEXT_OS == FLEXT_OS_MAC
-# include <Python/numarray/arrayobject.h>
-# else
-# include <numarray/arrayobject.h>
-# endif
-# endif
-
- static PyArray_TYPES numtype = PyArray_NOTYPE;
- inline bool arrsupport() { return numtype != PyArray_NOTYPE; }
-#endif
-#endif
-
-
-PyObject *pybase::py_arraysupport(PyObject *self,PyObject *args)
-{
- PyObject *ret;
-#ifdef PY_ARRAYS
- ret = Py_True;
-#else
- ret = Py_False;
-#endif
- Py_INCREF(ret);
- return ret;
-}
-
-
-// PD defines a T_OBJECT symbol
-#undef T_OBJECT
-
-#if FLEXT_OS == FLEXT_OS_MAC
-#include "Python/bufferobject.h"
-#include "Python/structmember.h"
-#else
-#include "bufferobject.h"
-#include "structmember.h"
-#endif
-
-static PyObject *buffer_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
-{
- pySamplebuffer *self = (pySamplebuffer *)pySamplebuffer_Type.tp_alloc(&pySamplebuffer_Type, 0);
- self->sym = NULL;
- self->buf = NULL;
- self->dirty = false;
- return (PyObject *)self;
-}
-
-static void buffer_dealloc(PyObject *obj)
-{
- pySamplebuffer *self = (pySamplebuffer *)obj;
-
- if(self->buf) {
- self->buf->Unlock(self->lock);
- if(self->dirty) self->buf->Dirty(true);
- delete self->buf;
- }
-
- obj->ob_type->tp_free(obj);
-}
-
-static int buffer_init(PyObject *obj, PyObject *args, PyObject *kwds)
-{
- FLEXT_ASSERT(pySamplebuffer_Check(obj));
-
- PyObject *arg = PySequence_GetItem(args,0); // new reference
- if(!arg) return -1;
-
- int ret = 0;
-
- pySamplebuffer *self = (pySamplebuffer *)obj;
- FLEXT_ASSERT(!self->sym && !self->buf);
-
- if(pySymbol_Check(arg))
- self->sym = pySymbol_AS_SYMBOL(arg);
- else if(PyString_Check(arg))
- self->sym = flext::MakeSymbol(PyString_AS_STRING(arg));
- else
- ret = -1;
- Py_DECREF(arg);
-
- if(self->sym) {
- flext::buffer *b = new flext::buffer(self->sym);
- if(b->Ok() && b->Valid())
- self->lock = (self->buf = b)->Lock();
- else
- delete b;
- }
-
- return ret;
-}
-
-static PyObject *buffer_repr(PyObject *self)
-{
- FLEXT_ASSERT(pySamplebuffer_Check(self));
- return (PyObject *)PyString_FromFormat("<Samplebuffer %s>",pySamplebuffer_AS_STRING(self));
-}
-
-static long buffer_hash(PyObject *self)
-{
- FLEXT_ASSERT(pySamplebuffer_Check(self));
- return (long)(((pySamplebuffer *)self)->buf);
-}
-
-static PyObject *buffer_getsymbol(pySamplebuffer* self,void *closure)
-{
- if(self->sym)
- return pySymbol_FromSymbol(self->sym);
- else {
- Py_INCREF(Py_None);
- return Py_None;
- }
-}
-
-static PyGetSetDef buffer_getseters[] = {
- {"symbol",(getter)buffer_getsymbol, NULL, NULL},
- {NULL} /* Sentinel */
-};
-
-static PyObject *buffer_dirty(PyObject *obj)
-{
- ((pySamplebuffer *)obj)->dirty = true;
- Py_INCREF(Py_None);
- return Py_None;
-}
-
-static PyObject *buffer_resize(PyObject *obj,PyObject *args,PyObject *kwds)
-{
- flext::buffer *b = ((pySamplebuffer *)obj)->buf;
- if(b) {
- int frames,keep = 1,zero = 1;
- static char *kwlist[] = {"frames", "keep", "zero", NULL};
- if(!PyArg_ParseTupleAndKeywords(args, kwds, "i|ii", kwlist, &frames, &keep, &zero))
- return NULL;
-
- b->Frames(frames,keep != 0,zero != 0);
-
- Py_INCREF(obj);
- return obj;
- }
- else {
- PyErr_SetString(PyExc_RuntimeError,"Invalid buffer");
- return NULL;
- }
-}
-
-static PyMethodDef buffer_methods[] = {
- {"dirty", (PyCFunction)buffer_dirty,METH_NOARGS,"Mark buffer as dirty"},
- {"resize", (PyCFunction)buffer_resize,METH_VARARGS|METH_KEYWORDS,"Resize buffer"},
- {NULL} /* Sentinel */
-};
-
-
-
-// support the buffer protocol
-
-static Py_ssize_t buffer_readbuffer(PyObject *obj, Py_ssize_t segment, void **ptrptr)
-{
- flext::buffer *b = ((pySamplebuffer *)obj)->buf;
- ptrptr[0] = b->Data();
- return b->Channels()*b->Frames()*sizeof(t_sample);
-}
-
-static Py_ssize_t buffer_writebuffer(PyObject *obj, Py_ssize_t segment, void **ptrptr)
-{
- flext::buffer *b = ((pySamplebuffer *)obj)->buf;
- ptrptr[0] = b->Data();
- return b->Channels()*b->Frames()*sizeof(t_sample);
-}
-
-static Py_ssize_t buffer_segcount(PyObject *obj, Py_ssize_t *lenp)
-{
- flext::buffer *b = ((pySamplebuffer *)obj)->buf;
- if(lenp) lenp[0] = b->Channels()*b->Frames()*sizeof(t_sample);
- return 1;
-}
-
-static Py_ssize_t buffer_charbuffer(PyObject *obj, Py_ssize_t segment,
-#if PY_VERSION_HEX < 0x02050000
- const
-#endif
- char **ptrptr)
-{
- flext::buffer *b = ((pySamplebuffer *)obj)->buf;
- ptrptr[0] = (char *)b->Data();
- return b->Channels()*b->Frames()*sizeof(t_sample);
-}
-
-static PyBufferProcs buffer_as_buffer = {
- buffer_readbuffer,
- buffer_writebuffer,
- buffer_segcount,
- buffer_charbuffer
-};
-
-static Py_ssize_t buffer_length(PyObject *s)
-{
- pySamplebuffer *self = reinterpret_cast<pySamplebuffer *>(s);
- return self->buf?self->buf->Frames():0;
-}
-
-static PyObject *buffer_item(PyObject *s,Py_ssize_t i)
-{
- pySamplebuffer *self = reinterpret_cast<pySamplebuffer *>(s);
- PyObject *ret;
- if(self->buf) {
- if (i < 0 || i >= self->buf->Frames()) {
- PyErr_SetString(PyExc_IndexError,"Index out of range");
- ret = NULL;
- }
- else {
- if(self->buf->Channels() == 1)
- ret = PyFloat_FromDouble(self->buf->Data()[i]);
- else {
- PyErr_SetString(PyExc_NotImplementedError,"Multiple channels not implemented yet");
- ret = NULL;
- }
- }
- }
- else {
- Py_INCREF(Py_None);
- ret = Py_None;
- }
- return ret;
-}
-
-PyObject *arrayfrombuffer(PyObject *buf,int c,int n)
-{
-#ifdef PY_ARRAYS
- if(arrsupport()) {
- PyObject *arr;
- int shape[2];
- shape[0] = n;
- shape[1] = c;
-#ifdef PY_NUMARRAY
- arr = (PyObject *)NA_NewAllFromBuffer(c == 1?1:2,shape,numtype,buf,0,0,NA_ByteOrder(),1,1);
-#else
- void *data;
- Py_ssize_t len;
- int err = PyObject_AsWriteBuffer(buf,&data,&len);
- if(!err) {
- FLEXT_ASSERT(len <= n*c*sizeof(t_sample));
- Py_INCREF(buf);
- // \todo change to new API!
- arr = PyArray_FromDimsAndData(c == 1?1:2,shape,numtype,(char *)data);
- }
- else {
- // exception string is already set
- arr = NULL;
- }
-#endif
- return arr;
- }
- else
-#endif
- return NULL;
-}
-
-static PyObject *buffer_slice(PyObject *s,Py_ssize_t ilow = 0,Py_ssize_t ihigh = 1<<(sizeof(int)*8-2))
-{
- pySamplebuffer *self = reinterpret_cast<pySamplebuffer *>(s);
- PyObject *ret;
-#ifdef PY_ARRAYS
- if(arrsupport()) {
- if(self->buf) {
- const int n = self->buf->Frames();
- const int c = self->buf->Channels();
- if(ilow < 0) ilow += n;
- if(ilow >= n) ilow = n-1;
- if(ihigh < 0) ihigh += n;
- if(ihigh > n) ihigh = n;
-
- PyObject *nobj = arrayfrombuffer((PyObject *)self,c,n);
- if(ilow != 0 || ihigh != n) {
- ret = PySequence_GetSlice(nobj,ilow,ihigh);
- Py_DECREF(nobj);
- }
- else
- ret = nobj;
- }
- else {
- Py_INCREF(Py_None);
- ret = Py_None;
- }
- }
- else
-#endif
- {
- PyErr_SetString(PyExc_RuntimeError,"No numarray support");
- ret = NULL;
- }
- return ret;
-}
-
-static int buffer_ass_item(PyObject *s,Py_ssize_t i,PyObject *v)
-{
- pySamplebuffer *self = reinterpret_cast<pySamplebuffer *>(s);
- int ret;
- if(self->buf) {
- if (i < 0 || i >= self->buf->Frames()) {
- PyErr_Format(PyExc_IndexError,"Index out of range");
- ret = -1;
- }
- else {
- if(self->buf->Channels() == 1) {
- self->buf->Data()[i] = (t_sample)PyFloat_AsDouble(v);
- if(PyErr_Occurred()) {
- // cast to double failed
- PyErr_SetString(PyExc_TypeError,"Value must be a numarray");
- ret = -1;
- }
- else {
- self->dirty = true;
- ret = 0;
- }
- }
- else {
- PyErr_SetString(PyExc_NotImplementedError,"Multiple channels not implemented yet");
- ret = -1;
- }
- }
- }
- else
- ret = -1;
- return ret;
-}
-
-static int buffer_ass_slice(PyObject *s,Py_ssize_t ilow,Py_ssize_t ihigh,PyObject *value)
-{
- pySamplebuffer *self = reinterpret_cast<pySamplebuffer *>(s);
- int ret;
-#ifdef PY_ARRAYS
- if(arrsupport()) {
- if(!value) {
- PyErr_SetString(PyExc_TypeError,"Object doesn't support item deletion");
- ret = -1;
- }
- else if(self->buf) {
- const int n = self->buf->Frames();
- const int c = self->buf->Channels();
- if(ilow < 0) ilow += n;
- if(ilow >= n) ilow = n-1;
- if(ihigh < 0) ihigh += n;
- if(ihigh > n) ihigh = n;
-
-#ifdef PY_NUMARRAY
- PyArrayObject *out = NA_InputArray(value,numtype,NUM_C_ARRAY);
-#else
- PyArrayObject *out = (PyArrayObject *)PyArray_ContiguousFromObject(value,numtype,0,0);
-#endif
- if(!out) {
- PyErr_SetString(PyExc_TypeError,"Assigned object must be a numarray");
- ret = -1;
- }
- else if(out->nd != 1) {
- PyErr_SetString(PyExc_NotImplementedError,"Multiple dimensions not supported yet");
- ret = -1;
- }
- else {
- int dlen = ihigh-ilow;
- int slen = out->dimensions[0];
-#ifdef PY_NUMARRAY
- flext::CopySamples(self->buf->Data()+ilow,(t_sample *)NA_OFFSETDATA(out),slen < dlen?slen:dlen);
-#else
- flext::CopySamples(self->buf->Data()+ilow,(t_sample *)out->data,slen < dlen?slen:dlen);
-#endif
- self->dirty = true;
- ret = 0;
- }
-
- Py_XDECREF(out);
- }
- else {
- PyErr_SetString(PyExc_ValueError,"Buffer is not assigned");
- ret = -1;
- }
- }
- else
-#endif
- {
- PyErr_SetString(PyExc_RuntimeError,"No numarray support");
- ret = -1;
- }
- return ret;
-}
-
-static PyObject *buffer_concat(PyObject *s,PyObject *op)
-{
- pySamplebuffer *self = reinterpret_cast<pySamplebuffer *>(s);
- PyObject *nobj = buffer_slice(s);
- if(nobj) {
- PyObject *ret = PySequence_Concat(nobj,op);
- if(ret == nobj) self->dirty = true;
- Py_DECREF(nobj);
- return ret;
- }
- else
- return NULL;
-}
-
-static PyObject *buffer_repeat(PyObject *s,Py_ssize_t rep)
-{
- pySamplebuffer *self = reinterpret_cast<pySamplebuffer *>(s);
- PyObject *nobj = buffer_slice(s);
- if(nobj) {
- PyObject *ret = PySequence_Repeat(nobj,rep);
- if(ret == nobj) self->dirty = true;
- Py_DECREF(nobj);
- return ret;
- }
- else
- return NULL;
-}
-
-
-static PySequenceMethods buffer_as_seq = {
- buffer_length, /* inquiry sq_length; __len__ */
- buffer_concat, /* __add__ */
- buffer_repeat, /* __mul__ */
- buffer_item, /* intargfunc sq_item; __getitem__ */
- buffer_slice, /* intintargfunc sq_slice; __getslice__ */
- buffer_ass_item, /* intobjargproc sq_ass_item; __setitem__ */
- buffer_ass_slice, /* intintobjargproc sq_ass_slice; __setslice__ */
-};
-
-static PyObject *buffer_iter(PyObject *s)
-{
- pySamplebuffer *self = reinterpret_cast<pySamplebuffer *>(s);
- PyObject *nobj = buffer_slice(s);
- if(nobj) {
- PyObject *it = PyObject_GetIter(nobj);
- Py_DECREF(nobj);
- return it;
- }
- else
- return NULL;
-}
-
-
-static PyObject *buffer_add(PyObject *s,PyObject *op)
-{
- pySamplebuffer *self = reinterpret_cast<pySamplebuffer *>(s);
- PyObject *nobj = buffer_slice(s);
- if(nobj) {
- PyObject *ret = PyNumber_Add(nobj,op);
- if(ret == nobj) self->dirty = true;
- Py_DECREF(nobj);
- return ret;
- }
- else
- return NULL;
-}
-
-static PyObject *buffer_subtract(PyObject *s,PyObject *op)
-{
- pySamplebuffer *self = reinterpret_cast<pySamplebuffer *>(s);
- PyObject *nobj = buffer_slice(s);
- if(nobj) {
- PyObject *ret = PyNumber_Subtract(nobj,op);
- if(ret == nobj) self->dirty = true;
- Py_DECREF(nobj);
- return ret;
- }
- else
- return NULL;
-}
-
-static PyObject *buffer_multiply(PyObject *s,PyObject *op)
-{
- pySamplebuffer *self = reinterpret_cast<pySamplebuffer *>(s);
- PyObject *nobj = buffer_slice(s);
- if(nobj) {
- PyObject *ret = PyNumber_Multiply(nobj,op);
- if(ret == nobj) self->dirty = true;
- Py_DECREF(nobj);
- return ret;
- }
- else
- return NULL;
-}
-
-static PyObject *buffer_divide(PyObject *s,PyObject *op)
-{
- pySamplebuffer *self = reinterpret_cast<pySamplebuffer *>(s);
- PyObject *nobj = buffer_slice(s);
- if(nobj) {
- PyObject *ret = PyNumber_Divide(nobj,op);
- if(ret == nobj) self->dirty = true;
- Py_DECREF(nobj);
- return ret;
- }
- else
- return NULL;
-}
-
-static PyObject *buffer_remainder(PyObject *s,PyObject *op)
-{
- pySamplebuffer *self = reinterpret_cast<pySamplebuffer *>(s);
- PyObject *nobj = buffer_slice(s);
- if(nobj) {
- PyObject *ret = PyNumber_Remainder(nobj,op);
- if(ret == nobj) self->dirty = true;
- Py_DECREF(nobj);
- return ret;
- }
- else
- return NULL;
-}
-
-static PyObject *buffer_divmod(PyObject *s,PyObject *op)
-{
- pySamplebuffer *self = reinterpret_cast<pySamplebuffer *>(s);
- PyObject *nobj = buffer_slice(s);
- if(nobj) {
- PyObject *ret = PyNumber_Divmod(nobj,op);
- if(ret == nobj) self->dirty = true;
- Py_DECREF(nobj);
- return ret;
- }
- else
- return NULL;
-}
-
-static PyObject *buffer_power(PyObject *s,PyObject *op1,PyObject *op2)
-{
- pySamplebuffer *self = reinterpret_cast<pySamplebuffer *>(s);
- PyObject *nobj = buffer_slice(s);
- if(nobj) {
- PyObject *ret = PyNumber_Power(nobj,op1,op2);
- if(ret == nobj) self->dirty = true;
- Py_DECREF(nobj);
- return ret;
- }
- else
- return NULL;
-}
-
-static PyObject *buffer_negative(PyObject *s)
-{
- pySamplebuffer *self = reinterpret_cast<pySamplebuffer *>(s);
- PyObject *nobj = buffer_slice(s);
- if(nobj) {
- PyObject *ret = PyNumber_Negative(nobj);
- if(ret == nobj) self->dirty = true;
- Py_DECREF(nobj);
- return ret;
- }
- else
- return NULL;
-}
-
-static PyObject *buffer_pos(PyObject *s)
-{
- pySamplebuffer *self = reinterpret_cast<pySamplebuffer *>(s);
- PyObject *nobj = buffer_slice(s);
- if(nobj) {
- PyObject *ret = PyNumber_Positive(nobj);
- Py_DECREF(nobj);
- return ret;
- }
- else
- return NULL;
-}
-
-static PyObject *buffer_absolute(PyObject *s)
-{
- pySamplebuffer *self = reinterpret_cast<pySamplebuffer *>(s);
- PyObject *nobj = buffer_slice(s);
- if(nobj) {
- PyObject *ret = PyNumber_Absolute(nobj);
- if(ret == nobj) self->dirty = true;
- Py_DECREF(nobj);
- return ret;
- }
- else
- return NULL;
-}
-
-static int buffer_coerce(PyObject **pm, PyObject **pw)
-{
- if(pySamplebuffer_Check(*pw)) {
- Py_INCREF(*pm);
- Py_INCREF(*pw);
- return 0;
- }
- else
- return 1;
-}
-
-static PyObject *buffer_inplace_add(PyObject *s,PyObject *op)
-{
- pySamplebuffer *self = reinterpret_cast<pySamplebuffer *>(s);
- PyObject *nobj = buffer_slice(s);
- if(nobj) {
- PyObject *ret = PyNumber_InPlaceAdd(nobj,op);
- if(ret == nobj) self->dirty = true;
- Py_DECREF(nobj);
- return ret;
- }
- else
- return NULL;
-}
-
-static PyObject *buffer_inplace_subtract(PyObject *s,PyObject *op)
-{
- pySamplebuffer *self = reinterpret_cast<pySamplebuffer *>(s);
- PyObject *nobj = buffer_slice(s);
- if(nobj) {
- PyObject *ret = PyNumber_InPlaceSubtract(nobj,op);
- if(ret == nobj) self->dirty = true;
- Py_DECREF(nobj);
- return ret;
- }
- else
- return NULL;
-}
-
-static PyObject *buffer_inplace_multiply(PyObject *s,PyObject *op)
-{
- pySamplebuffer *self = reinterpret_cast<pySamplebuffer *>(s);
- PyObject *nobj = buffer_slice(s);
- if(nobj) {
- PyObject *ret = PyNumber_InPlaceMultiply(nobj,op);
- if(ret == nobj) self->dirty = true;
- Py_DECREF(nobj);
- return ret;
- }
- else
- return NULL;
-}
-
-static PyObject *buffer_inplace_divide(PyObject *s,PyObject *op)
-{
- pySamplebuffer *self = reinterpret_cast<pySamplebuffer *>(s);
- PyObject *nobj = buffer_slice(s);
- if(nobj) {
- PyObject *ret = PyNumber_InPlaceDivide(nobj,op);
- if(ret == nobj) self->dirty = true;
- Py_DECREF(nobj);
- return ret;
- }
- else
- return NULL;
-}
-
-static PyObject *buffer_inplace_remainder(PyObject *s,PyObject *op)
-{
- pySamplebuffer *self = reinterpret_cast<pySamplebuffer *>(s);
- PyObject *nobj = buffer_slice(s);
- if(nobj) {
- PyObject *ret = PyNumber_InPlaceRemainder(nobj,op);
- if(ret == nobj) self->dirty = true;
- Py_DECREF(nobj);
- return ret;
- }
- else
- return NULL;
-}
-
-static PyObject *buffer_inplace_power(PyObject *s,PyObject *op1,PyObject *op2)
-{
- pySamplebuffer *self = reinterpret_cast<pySamplebuffer *>(s);
- PyObject *nobj = buffer_slice(s);
- if(nobj) {
- PyObject *ret = PyNumber_InPlacePower(nobj,op1,op2);
- if(ret == nobj) self->dirty = true;
- Py_DECREF(nobj);
- return ret;
- }
- else
- return NULL;
-}
-
-
-
-static PyNumberMethods buffer_as_number = {
- (binaryfunc)buffer_add, /*nb_add*/
- (binaryfunc)buffer_subtract, /*nb_subtract*/
- (binaryfunc)buffer_multiply, /*nb_multiply*/
- (binaryfunc)buffer_divide, /*nb_divide*/
- (binaryfunc)buffer_remainder, /*nb_remainder*/
- (binaryfunc)buffer_divmod, /*nb_divmod*/
- (ternaryfunc)buffer_power, /*nb_power*/
- (unaryfunc)buffer_negative,
- (unaryfunc)buffer_pos, /*nb_pos*/
- (unaryfunc)buffer_absolute, /* (unaryfunc)buffer_abs, */
- 0, //(inquiry)buffer_nonzero, /*nb_nonzero*/
- 0, /*nb_invert*/
- 0, /*nb_lshift*/
- 0, /*nb_rshift*/
- 0, /*nb_and*/
- 0, /*nb_xor*/
- 0, /*nb_or*/
- (coercion)buffer_coerce, /*nb_coerce*/
- 0, /*nb_int*/
- 0, /*nb_long*/
- 0, /*nb_float*/
- 0, /*nb_oct*/
- 0, /*nb_hex*/
- (binaryfunc)buffer_inplace_add, /* nb_inplace_add */
- (binaryfunc)buffer_inplace_subtract, /* nb_inplace_subtract */
- (binaryfunc)buffer_inplace_multiply, /* nb_inplace_multiply */
- (binaryfunc)buffer_inplace_divide, /* nb_inplace_divide */
- (binaryfunc)buffer_inplace_remainder, /* nb_inplace_remainder */
- (ternaryfunc)buffer_inplace_power, /* nb_inplace_power */
- 0, /* nb_inplace_lshift */
- 0, /* nb_inplace_rshift */
- 0, /* nb_inplace_and */
- 0, /* nb_inplace_xor */
- 0, /* nb_inplace_or */
-// buffer_floor_div, /* nb_floor_divide */
-// buffer_div, /* nb_true_divide */
-// buffer_inplace_floor_div, /* nb_inplace_floor_divide */
-// buffer_inplace_div, /* nb_inplace_true_divide */
-};
-
-PyTypeObject pySamplebuffer_Type = {
- PyObject_HEAD_INIT(NULL)
- 0, /*ob_size*/
- "Buffer", /*tp_name*/
- sizeof(pySamplebuffer), /*tp_basicsize*/
- 0, /*tp_itemsize*/
- buffer_dealloc, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_compare*/
- buffer_repr, /*tp_repr*/
- &buffer_as_number, /*tp_as_number*/
- &buffer_as_seq, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- buffer_hash, /*tp_hash */
- 0, /*tp_call*/
- 0, /*tp_str*/
- 0, /*tp_getattro*/
- 0, /*tp_setattro*/
- &buffer_as_buffer, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT /*| Py_TPFLAGS_BASETYPE*/, /*tp_flags*/
- "Samplebuffer objects", /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0 /*buffer_richcompare*/, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- buffer_iter, /* tp_iter */
- 0, /* tp_iternext */
- buffer_methods, /* tp_methods */
- 0, /* tp_members */
- buffer_getseters, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- buffer_init, /* tp_init */
- 0, /* tp_alloc */
- buffer_new, /* tp_new */
-};
-
-// Must have this as a function because the import_array macro in numpy version 1.01 strangely has a return statement included.
-// Furthermore the import error printout from this macro is ugly, but we accept that for more, waiting for later numpy updates to fix all of this.
-static void __import_array__()
-{
-#ifdef PY_NUMARRAY
- import_libnumarray();
-#else
- import_array();
-#endif
-}
-
-void initsamplebuffer()
-{
-#ifdef PY_ARRAYS
- __import_array__();
- if(PyErr_Occurred())
- // catch import error
- PyErr_Clear();
- else {
- // numarray support ok
-#ifdef PY_NUMARRAY
- numtype = sizeof(t_sample) == 4?tFloat32:tFloat64;
-#else
- numtype = sizeof(t_sample) == 4?PyArray_FLOAT:PyArray_DOUBLE;
-#endif
- post("");
- post("Python array support enabled");
- }
-#endif
-
- if(PyType_Ready(&pySamplebuffer_Type) < 0)
- FLEXT_ASSERT(false);
- else
- Py_INCREF(&pySamplebuffer_Type);
-}
diff --git a/externals/grill/py/source/pybuffer.h b/externals/grill/py/source/pybuffer.h
deleted file mode 100644
index fe6bbe63..00000000
--- a/externals/grill/py/source/pybuffer.h
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
-py/pyext - python script object for PD and Max/MSP
-
-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 __PYBUFFER_H
-#define __PYBUFFER_H
-
-#include <flext.h>
-
-#if !defined(FLEXT_VERSION) || (FLEXT_VERSION < 500)
-#error You need at least flext version 0.5.0
-#endif
-
-#if FLEXT_OS == FLEXT_OS_MAC
-#include <Python/Python.h>
-#else
-#include <Python.h>
-#endif
-
-
-#ifdef _MSC_VER
- #ifdef PY_EXPORTS
- #define PY_EXPORT __declspec(dllexport)
- #else
- #define PY_EXPORT __declspec(dllimport)
- #endif
-#else
- #define PY_EXPORT
-#endif
-
-typedef struct {
- PyObject_HEAD
- /* Type-specific fields go here. */
- const t_symbol *sym;
- flext::buffer *buf;
- flext::buffer::lock_t lock;
- bool dirty;
-} pySamplebuffer;
-
-PY_EXPORT extern PyTypeObject pySamplebuffer_Type;
-
-#define pySamplebuffer_Check(op) PyObject_TypeCheck(op, &pySamplebuffer_Type)
-#define pySamplebuffer_CheckExact(op) ((op)->ob_type == &pySamplebuffer_Type)
-
-
-PY_EXPORT PyObject *pySamplebuffer_FromSymbol(const t_symbol *sym);
-
-inline PyObject *pySamplebuffer_FromString(const char *str)
-{
- return pySamplebuffer_FromSymbol(flext::MakeSymbol(str));
-}
-
-inline PyObject *pySamplebuffer_FromString(PyObject *str)
-{
- return pySamplebuffer_FromString(PyString_AsString(str));
-}
-
-inline const t_symbol *pySamplebuffer_AS_SYMBOL(PyObject *op)
-{
- return ((pySamplebuffer *)op)->sym;
-}
-
-inline const t_symbol *pySamplebuffer_AsSymbol(PyObject *op)
-{
- return pySamplebuffer_Check(op)?pySamplebuffer_AS_SYMBOL(op):NULL;
-}
-
-inline const char *pySamplebuffer_AS_STRING(PyObject *op)
-{
- return flext::GetString(pySamplebuffer_AS_SYMBOL(op));
-}
-
-#endif
diff --git a/externals/grill/py/source/pybundle.cpp b/externals/grill/py/source/pybundle.cpp
deleted file mode 100644
index cb7e927a..00000000
--- a/externals/grill/py/source/pybundle.cpp
+++ /dev/null
@@ -1,234 +0,0 @@
-/*
-py/pyext - python script object for PD and Max/MSP
-
-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 $
-*/
-
-#include "pyprefix.h"
-#include "pybundle.h"
-#include "pyext.h"
-
-static PyObject *bundle_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
-{
- pyBundle *self = (pyBundle *)pyBundle_Type.tp_alloc(&pyBundle_Type, 0);
- if(self) self->bundle = flext::MsgNew();
- return (PyObject *)self;
-}
-
-static int bundle_init(PyObject *self, PyObject *args, PyObject *kwds)
-{
- FLEXT_ASSERT(pyBundle_Check(self));
-
- int len = PySequence_Length(args);
- if(len) {
- PyErr_SetString(PyExc_TypeError,"no arguments expected");
- return -1;
- }
-
- return 0;
-}
-
-static void bundle_dealloc(PyObject *obj)
-{
- pyBundle *self = (pyBundle *)obj;
- if(self->bundle) flext::MsgFree(self->bundle);
- obj->ob_type->tp_free(obj);
-}
-
-static PyObject *bundle_send(PyObject *obj)
-{
- pyBundle *self = (pyBundle *)obj;
- if(self->bundle) {
- flext::ToOutMsg(self->bundle);
- self->bundle = NULL;
-
- Py_INCREF(obj);
- return obj;
- }
- else {
- PyErr_SetString(PyExc_RuntimeError,"already sent");
- return NULL;
- }
-}
-
-static PyObject *bundle_repr(PyObject *self)
-{
- FLEXT_ASSERT(pyBundle_Check(self));
- return (PyObject *)PyString_FromFormat("<Bundle %p>",pyBundle_AS_BUNDLE(self));
-}
-
-static PyObject *bundle_str(PyObject *self)
-{
- return bundle_repr(self);
-}
-
-static PyObject *bundle_richcompare(PyObject *a,PyObject *b,int cmp)
-{
- if(pyBundle_Check(a) && pyBundle_Check(b)) {
- const flext::MsgBundle *abnd = pyBundle_AS_BUNDLE(a);
- const flext::MsgBundle *bbnd = pyBundle_AS_BUNDLE(b);
- bool ret;
- switch(cmp) {
- case Py_LT: ret = abnd < bbnd; break;
- case Py_LE: ret = abnd <= bbnd; break;
- case Py_EQ: ret = abnd == bbnd; break;
- case Py_NE: ret = abnd != bbnd; break;
- case Py_GT: ret = abnd > bbnd; break;
- case Py_GE: ret = abnd >= bbnd; break;
- }
- return PyBool_FromLong(ret);
- }
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
-}
-
-static long bundle_hash(PyObject *self)
-{
- FLEXT_ASSERT(pyBundle_Check(self));
- return (long)pyBundle_AS_BUNDLE(self);
-}
-
-
-static PyObject *bundle_append(PyObject *self,PyObject *args)
-{
- flext::MsgBundle *b = pyBundle_AS_BUNDLE(self);
- if(b) {
- int sz = PyTuple_GET_SIZE(args),offs = 0;
- PyObject *tg,*outl;
- pyext *ext = NULL;
- const t_symbol *recv;
- int o;
-
- if(sz > 2 &&
- (tg = PyTuple_GET_ITEM(args,0)) != NULL && PyInstance_Check(tg) &&
- (outl = PyTuple_GET_ITEM(args,1)) != NULL && PyInt_Check(outl)
- ) {
- // Sending to outlet
- ext = pyext::GetThis(tg);
- o = PyInt_AS_LONG(outl);
-
- if(o < 1 || o > ext->Outlets()) {
- PyErr_SetString(PyExc_ValueError,"Outlet index out of range");
- return NULL;
- }
-
- offs += 2;
- }
- else if(sz > 1 &&
- (tg = PyTuple_GET_ITEM(args,0)) != NULL && (recv = pyObject_AsSymbol(tg)) != NULL
- ) {
- // Sending to receiver
- offs++;
- }
- else {
- // not recognized
- PyErr_SetString(PyExc_SyntaxError,"Unrecognized arguments");
- return NULL;
- }
-
- PyObject *val;
- if(sz-offs == 1) {
- val = PyTuple_GET_ITEM(args,offs); // borrow reference
- Py_INCREF(val);
- }
- else
- val = PyTuple_GetSlice(args,offs,sz); // new ref
-
- flext::AtomListStatic<16> lst;
- const t_symbol *sym = pybase::GetPyArgs(lst,val);
- Py_DECREF(val);
-
- if(sym) {
- if(ext) {
- FLEXT_ASSERT(outl);
- ext->MsgAddAnything(b,o-1,sym,lst.Count(),lst.Atoms());
- }
- else {
- FLEXT_ASSERT(sym);
- if(!flext::MsgForward(b,recv,sym,lst.Count(),lst.Atoms())) {
- PyErr_SetString(PyExc_ValueError,"Receiver not found");
- return NULL;
- }
- }
-
- Py_INCREF(Py_None);
- return Py_None;
- }
- else {
- FLEXT_ASSERT(PyErr_Occurred());
- return NULL;
- }
-
- Py_INCREF(self);
- return self;
- }
- else {
- PyErr_SetString(PyExc_RuntimeError,"Invalid bundle");
- return NULL;
- }
-}
-
-static PyMethodDef bundle_methods[] = {
- {"append", (PyCFunction)bundle_append,METH_VARARGS,"Append message to bundle"},
- {"send", (PyCFunction)bundle_send,METH_NOARGS,"Send bundle"},
- {NULL} /* Sentinel */
-};
-
-
-
-PyTypeObject pyBundle_Type = {
- PyObject_HEAD_INIT(NULL)
- 0, /*ob_size*/
- "Bundle", /*tp_name*/
- sizeof(pyBundle), /*tp_basicsize*/
- 0, /*tp_itemsize*/
- bundle_dealloc, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_compare*/
- bundle_repr, /*tp_repr*/
- 0, /*tp_as_number*/
- 0, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- bundle_hash, /*tp_hash */
- 0, /*tp_call*/
- bundle_str, /*tp_str*/
- 0, /*tp_getattro*/
- 0, /*tp_setattro*/
- 0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT /*| Py_TPFLAGS_BASETYPE*/, /*tp_flags*/
- "Bundle objects", /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- bundle_richcompare, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- bundle_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- bundle_init, /* tp_init */
- 0, /* tp_alloc */
- bundle_new, /* tp_new */
-};
-
-
-void initbundle()
-{
- if(PyType_Ready(&pyBundle_Type) < 0)
- FLEXT_ASSERT(false);
- else
- Py_INCREF(&pyBundle_Type);
-}
diff --git a/externals/grill/py/source/pybundle.h b/externals/grill/py/source/pybundle.h
deleted file mode 100644
index f525f219..00000000
--- a/externals/grill/py/source/pybundle.h
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
-py/pyext - python script object for PD and Max/MSP
-
-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 __PYBUNDLE_H
-#define __PYBUNDLE_H
-
-#include <flext.h>
-
-#if !defined(FLEXT_VERSION) || (FLEXT_VERSION < 500)
-#error You need at least flext version 0.5.0
-#endif
-
-#if FLEXT_OS == FLEXT_OS_MAC
-#include <Python/Python.h>
-#else
-#include <Python.h>
-#endif
-
-
-#ifdef _MSC_VER
- #ifdef PY_EXPORTS
- #define PY_EXPORT __declspec(dllexport)
- #else
- #define PY_EXPORT __declspec(dllimport)
- #endif
-#else
- #define PY_EXPORT
-#endif
-
-typedef struct {
- PyObject_HEAD
- /* Type-specific fields go here. */
- flext::MsgBundle *bundle;
-} pyBundle;
-
-PY_EXPORT extern PyTypeObject pyBundle_Type;
-
-#define pyBundle_Check(op) PyObject_TypeCheck(op, &pyBundle_Type)
-#define pyBundle_CheckExact(op) ((op)->ob_type == &pyBundle_Type)
-
-
-inline flext::MsgBundle *pyBundle_AS_BUNDLE(PyObject *op)
-{
- return ((pyBundle *)op)->bundle;
-}
-
-inline flext::MsgBundle *pyBundle_AsBundle(PyObject *op)
-{
- return pyBundle_Check(op)?pyBundle_AS_BUNDLE(op):NULL;
-}
-
-
-#endif
diff --git a/externals/grill/py/source/pydsp.cpp b/externals/grill/py/source/pydsp.cpp
deleted file mode 100644
index 38edb6cd..00000000
--- a/externals/grill/py/source/pydsp.cpp
+++ /dev/null
@@ -1,192 +0,0 @@
-/*
-py/pyext - python script object for PD and Max/MSP
-
-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 PY_NODSP
-
-#include "pyext.h"
-
-class pydsp
- : public pyext
-{
- FLEXT_HEADER(pydsp,pyext)
-public:
- pydsp(int argc,const t_atom *argv);
-
-protected:
- virtual bool CbDsp();
- virtual void CbSignal();
-
- virtual bool DoInit();
- virtual void DoExit();
-
- virtual PyObject *GetSig(int ix,bool in);
-
- void NewBuffers();
- void FreeBuffers();
-
- PyObject *dspfun,*sigfun;
- PyObject **buffers;
-};
-
-FLEXT_LIB_DSP_V("pyext~ pyext.~ pyx~ pyx.~",pydsp)
-
-pydsp::pydsp(int argc,const t_atom *argv)
- : pyext(argc,argv,true)
- , dspfun(NULL),sigfun(NULL)
-{}
-
-bool pydsp::DoInit()
-{
- if(!pyext::DoInit()) return false;
-
- if(pyobj)
- {
- dspfun = PyObject_GetAttrString(pyobj,"_dsp"); // get ref
- if(!dspfun)
- PyErr_Clear();
- else if(!PyMethod_Check(dspfun)) {
- Py_DECREF(dspfun);
- dspfun = NULL;
- }
- }
- return true;
-}
-
-void pydsp::DoExit()
-{
- if(dspfun) { Py_DECREF(dspfun); dspfun = NULL; }
- if(sigfun) { Py_DECREF(sigfun); sigfun = NULL; }
-
- FreeBuffers();
-}
-
-PyObject *arrayfrombuffer(PyObject *buf,int c,int n);
-
-void pydsp::NewBuffers()
-{
- int i,n = Blocksize();
- const int ins = CntInSig(),outs = CntOutSig();
- t_sample *const *insigs = InSig();
- t_sample *const *outsigs = OutSig();
-
- // inlet/outlet count can't change so we don't have to deallocate
- if(!buffers) {
- int cnt = ins+outs;
- buffers = new PyObject *[cnt];
- memset(buffers,0,cnt*sizeof(*buffers));
- }
-
- for(i = 0; i < ins; ++i) {
- Py_XDECREF(buffers[i]);
- PyObject *b = PyBuffer_FromReadWriteMemory(insigs[i],n*sizeof(t_sample));
- buffers[i] = arrayfrombuffer(b,1,n);
- Py_DECREF(b);
- }
- for(i = 0; i < outs; ++i) {
- Py_XDECREF(buffers[ins+i]);
- if(i < ins && outsigs[i] == insigs[i]) {
- // same vectors - share the objects!
- buffers[ins+i] = buffers[i];
- Py_XINCREF(buffers[i]);
- }
- else {
- PyObject *b = PyBuffer_FromReadWriteMemory(outsigs[i],n*sizeof(t_sample));
- buffers[ins+i] = arrayfrombuffer(b,1,n);
- Py_DECREF(b);
- }
- }
-}
-
-void pydsp::FreeBuffers()
-{
- if(buffers) {
- int cnt = CntInSig()+CntOutSig();
- for(int i = 0; i < cnt; ++i) Py_XDECREF(buffers[i]);
- delete[] buffers;
- buffers = NULL;
- }
-}
-
-bool pydsp::CbDsp()
-{
- if(pyobj && (CntInSig() || CntOutSig()))
- {
- ThrState state = PyLockSys();
-
- NewBuffers();
-
- bool dodsp = true;
- if(dspfun) {
- PyObject *ret = PyObject_CallObject(dspfun,NULL);
- if(ret) {
- dodsp = PyObject_IsTrue(ret) != 0;
- Py_DECREF(ret);
- }
- else {
-#ifdef FLEXT_DEBUG
- PyErr_Print();
-#else
- PyErr_Clear();
-#endif
- }
- }
-
- // do that here instead of where dspfun is initialized, so that
- // _signal can be assigned in _dsp
- // optimizations may be done there to assign the right _signal version
- Py_XDECREF(sigfun);
-
- if(dodsp) {
- sigfun = PyObject_GetAttrString(pyobj,"_signal"); // get ref
- if(!sigfun)
- PyErr_Clear();
- else if(!PyMethod_Check(sigfun)) {
- Py_DECREF(sigfun);
- sigfun = NULL;
- }
- }
- else
- sigfun = NULL;
-
- PyUnlock(state);
- return sigfun != NULL;
- }
- else
- // switch on dsp only if there are signal inlets or outlets
- return false;
-}
-
-void pydsp::CbSignal()
-{
- ThrState state = PyLockSys();
- PyObject *ret = PyObject_CallObject(sigfun,NULL);
-
- if(ret)
- Py_DECREF(ret);
- else {
-#ifdef FLEXT_DEBUG
- PyErr_Print();
-#else
- PyErr_Clear();
-#endif
- }
- PyUnlock(state);
-}
-
-PyObject *pydsp::GetSig(int ix,bool in)
-{
- PyObject *r = buffers[in?ix:CntInSig()+ix];
- Py_XINCREF(r);
- return r;
-}
-
-#endif
diff --git a/externals/grill/py/source/pyext.cpp b/externals/grill/py/source/pyext.cpp
deleted file mode 100644
index b2b7baca..00000000
--- a/externals/grill/py/source/pyext.cpp
+++ /dev/null
@@ -1,662 +0,0 @@
-/*
-py/pyext - python script object for PD and Max/MSP
-
-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 $
-*/
-
-#include "pyext.h"
-#include <flinternal.h>
-
-
-FLEXT_LIB_V("pyext pyext. pyx pyx.",pyext)
-
-
-static const t_symbol *sym_get;
-
-void pyext::Setup(t_classid c)
-{
- sym_get = flext::MakeSymbol("get");
-
- FLEXT_CADDMETHOD_(c,0,"doc",m_doc);
- FLEXT_CADDMETHOD_(c,0,"dir",m_dir);
-#ifdef FLEXT_THREADS
- FLEXT_CADDATTR_VAR1(c,"detach",detach);
- FLEXT_CADDMETHOD_(c,0,"stop",m_stop);
-#endif
-
- FLEXT_CADDMETHOD_(c,0,"help",m_help);
-
- FLEXT_CADDMETHOD_(c,0,"reload",m_reload_);
- FLEXT_CADDMETHOD_(c,0,"reload.",m_reload);
- FLEXT_CADDMETHOD_(c,0,"doc+",m_doc_);
- FLEXT_CADDMETHOD_(c,0,"dir+",m_dir_);
- FLEXT_CADDATTR_GET(c,"dir+",mg_dir_);
-
- FLEXT_CADDATTR_VAR(c,"args",initargs,ms_initargs);
-
- FLEXT_CADDMETHOD_(c,0,"get",m_get);
- FLEXT_CADDMETHOD_(c,0,"set",m_set);
-
- FLEXT_CADDMETHOD_(c,0,"edit",CbClick);
-
- FLEXT_CADDATTR_VAR1(c,"py",pymsg);
- FLEXT_CADDATTR_VAR1(c,"respond",respond);
-
- // ----------------------------------------------------
-
- // register/initialize pyext base class along with module
- class_dict = PyDict_New();
- PyObject *className = PyString_FromString(PYEXT_CLASS);
- PyMethodDef *def;
-
- // add setattr/getattr to class
- for(def = attr_tbl; def->ml_name; def++) {
- PyObject *func = PyCFunction_New(def, NULL);
- PyDict_SetItemString(class_dict, def->ml_name, func);
- Py_DECREF(func);
- }
-
- class_obj = PyClass_New(NULL, class_dict, className);
- Py_DECREF(className);
-
- // add methods to class
- for (def = meth_tbl; def->ml_name != NULL; def++) {
- PyObject *func = PyCFunction_New(def, NULL);
- PyObject *method = PyMethod_New(func, NULL, class_obj); // increases class_obj ref count by 1
- PyDict_SetItemString(class_dict, def->ml_name, method);
- Py_DECREF(func);
- Py_DECREF(method);
- }
-
-#if PY_VERSION_HEX >= 0x02020000
- // not absolutely necessary, existent in python 2.2 upwards
- // make pyext functions available in class scope
- PyDict_Merge(class_dict,module_dict,0);
-#endif
- // after merge so that it's not in class_dict as well...
- PyDict_SetItemString(module_dict, PYEXT_CLASS,class_obj); // increases class_obj ref count by 1
-
- PyDict_SetItemString(class_dict,"__doc__",PyString_FromString(pyext_doc));
-}
-
-pyext *pyext::GetThis(PyObject *self)
-{
- PyObject *th = PyObject_GetAttrString(self,"_this");
- if(th) {
- pyext *ret = static_cast<pyext *>(PyLong_AsVoidPtr(th));
- Py_DECREF(th);
- return ret;
- }
- else {
- PyErr_Clear();
- return NULL;
- }
-}
-
-void pyext::SetThis()
-{
- // remember the this pointer
- PyObject *th = PyLong_FromVoidPtr(this);
- PyObject_SetAttrString(pyobj,"_this",th); // ref is taken
-}
-
-void pyext::ClearThis()
-{
- int ret = PyObject_DelAttrString(pyobj,"_this");
- FLEXT_ASSERT(ret != -1);
-}
-
-PyObject *pyext::class_obj = NULL;
-PyObject *pyext::class_dict = NULL;
-
-pyext::pyext(int argc,const t_atom *argv,bool sig):
- methname(NULL),
- pyobj(NULL),
- inlets(-1),outlets(-1),
- siginlets(0),sigoutlets(0)
-#ifndef PY_USE_GIL
- ,pythr(NULL)
-#endif
-{
-#ifdef FLEXT_THREADS
- FLEXT_ADDTIMER(stoptmr,tick);
-#endif
-
- if(argc >= 2 && CanbeInt(argv[0]) && CanbeInt(argv[1])) {
- inlets = GetAInt(argv[0]);
- outlets = GetAInt(argv[1]);
- argv += 2,argc -= 2;
- }
-
- if(sig && argc >= 2 && CanbeInt(argv[0]) && CanbeInt(argv[1])) {
- siginlets = GetAInt(argv[0]);
- sigoutlets = GetAInt(argv[1]);
- argv += 2,argc -= 2;
- }
-
- const t_symbol *clname = NULL;
-
- // check if the object name is pyext. , pyx. or similar
- bool dotted = strrchr(thisName(),'.') != NULL;
-
- ThrState state = PyLockSys();
-
- // init script module
- if(argc) {
- AddCurrentPath(this);
-
- const t_symbol *scr = GetASymbol(*argv);
- argv++,argc--;
-
- if(scr) {
- char modnm[64];
- strcpy(modnm,GetString(scr));
-
- if(!dotted) {
- char *pt = strrchr(modnm,'.'); // search for last dot
- if(pt && *pt) {
- clname = MakeSymbol(pt+1);
- *pt = 0;
- }
- }
-
- ImportModule(modnm);
- }
- else
- PyErr_SetString(PyExc_ValueError,"Invalid module name");
-
- // check for alias creation names
- if(dotted) clname = scr;
- }
-
- Register(GetRegistry(REGNAME));
-
- if(argc || clname) {
- if(!clname) {
- clname = GetASymbol(*argv);
- argv++,argc--;
- }
-
- if(clname)
- methname = clname;
- else
- PyErr_SetString(PyExc_ValueError,"Invalid class name");
- }
-
- if(argc) initargs(argc,argv);
-
- Report();
-
- PyUnlock(state);
-}
-
-bool pyext::Init()
-{
- ThrState state = PyLockSys();
-
- if(methname) {
- MakeInstance();
- if(pyobj) InitInOut(inlets,outlets);
- }
- else
- inlets = outlets = 0;
-
- if(inlets < 0) inlets = 0;
- if(outlets < 0) outlets = 0;
-
- AddInSignal(siginlets);
- AddInAnything((siginlets?0:1)+inlets);
- AddOutSignal(sigoutlets);
- AddOutAnything(outlets);
-
- Report();
- PyUnlock(state);
-
- return pyobj && flext_dsp::Init();
-}
-
-bool pyext::Finalize()
-{
- bool ok = true;
- ThrState state = PyLockSys();
-
- PyObject *init = PyObject_GetAttrString(pyobj,"_init"); // get ref
- if(init) {
- if(PyMethod_Check(init)) {
- PyObject *res = PyObject_CallObject(init,NULL);
- if(!res) {
- // exception is set
- ok = false;
- // we want to know why __init__ failed...
- PyErr_Print();
- }
- else
- Py_DECREF(res);
- }
- Py_DECREF(init);
- }
- else
- // __init__ has not been found - don't care
- PyErr_Clear();
-
- PyUnlock(state);
- return ok && flext_dsp::Finalize();
-}
-
-void pyext::Exit()
-{
- pybase::Exit(); // exit threads
-
- ThrState state = PyLockSys();
- DoExit();
-
- Unregister(GetRegistry(REGNAME));
- UnimportModule();
-
- Report();
- PyUnlock(state);
-
- flext_dsp::Exit();
-}
-
-bool pyext::DoInit()
-{
- // call init now, after _this has been set, which is
- // important for eventual callbacks from __init__ to c
- PyObject *pargs = MakePyArgs(NULL,initargs.Count(),initargs.Atoms());
- if(pargs) {
- bool ok = true;
-
- SetThis();
-
- PyObject *init = PyObject_GetAttrString(pyobj,"__init__"); // get ref
- if(init) {
- if(PyMethod_Check(init)) {
- PyObject *res = PyObject_CallObject(init,pargs);
- if(!res) {
- // exception is set
- ok = false;
- // we want to know why __init__ failed...
- PyErr_Print();
- }
- else
- Py_DECREF(res);
- }
- Py_DECREF(init);
- }
- else
- // __init__ has not been found - don't care
- PyErr_Clear();
-
- Py_DECREF(pargs);
- return ok;
- }
- else
- return false;
-}
-
-void pyext::DoExit()
-{
- ClearBinding();
-
- bool gcrun = false;
- if(pyobj) {
- // try to run del to clean up the class instance
- PyObject *objdel = PyObject_GetAttrString(pyobj,"_del");
- if(objdel) {
- PyObject *ret = PyObject_CallObject(objdel,NULL);
- if(ret)
- Py_DECREF(ret);
- else
- PyErr_Print();
- Py_DECREF(objdel);
- }
- else
- // _del has not been found - don't care
- PyErr_Clear();
-
- ClearThis();
-
- gcrun = pyobj->ob_refcnt > 1;
- Py_DECREF(pyobj); // opposite of SetClssMeth
- }
-
- if(gcrun && !collect()) {
- post("%s - Unloading: Object is still referenced",thisName());
- }
-}
-
-bool pyext::InitInOut(int &inl,int &outl)
-{
- if(inl >= 0) {
- // set number of inlets
- int ret = PyObject_SetAttrString(pyobj,"_inlets",PyInt_FromLong(inl));
- FLEXT_ASSERT(!ret);
- }
- if(outl >= 0) {
- // set number of outlets
- int ret = PyObject_SetAttrString(pyobj,"_outlets",PyInt_FromLong(outl));
- FLEXT_ASSERT(!ret);
- }
-
- // __init__ can override the number of inlets and outlets
- if(!DoInit()) // call __init__ constructor
- return false;
-
- if(inl < 0) {
- // get number of inlets
- inl = inlets;
- PyObject *res = PyObject_GetAttrString(pyobj,"_inlets"); // get ref
- if(res) {
- if(PyCallable_Check(res)) {
- PyObject *fres = PyEval_CallObject(res,NULL);
- Py_DECREF(res);
- res = fres;
- }
- if(PyInt_Check(res))
- inl = PyInt_AS_LONG(res);
- Py_DECREF(res);
- }
- else
- PyErr_Clear();
- }
- if(outl < 0) {
- // get number of outlets
- outl = outlets;
- PyObject *res = PyObject_GetAttrString(pyobj,"_outlets"); // get ref
- if(res) {
- if(PyCallable_Check(res)) {
- PyObject *fres = PyEval_CallObject(res,NULL);
- Py_DECREF(res);
- res = fres;
- }
- if(PyInt_Check(res))
- outl = PyInt_AS_LONG(res);
- Py_DECREF(res);
- }
- else
- PyErr_Clear();
- }
-
- return true;
-}
-
-bool pyext::MakeInstance()
-{
- // pyobj should already have been decref'd / cleared before getting here!!
-
- if(module && methname) {
- PyObject *pref = PyObject_GetAttrString(module,const_cast<char *>(GetString(methname)));
- if(!pref)
- PyErr_Print();
- else {
- if(PyClass_Check(pref)) {
- // make instance, but don't call __init__
- pyobj = PyInstance_NewRaw(pref,NULL);
-
- if(!pyobj) PyErr_Print();
- }
- else
- post("%s - Type of \"%s\" is unhandled!",thisName(),GetString(methname));
-
- Py_DECREF(pref);
- }
- return true;
- }
- else
- return false;
-}
-
-void pyext::LoadModule()
-{
-}
-
-void pyext::UnloadModule()
-{
-}
-
-void pyext::Load()
-{
- FLEXT_ASSERT(!pyobj);
-
- bool ok = MakeInstance();
-
- if(ok) {
- int inl = -1,outl = -1;
- ok = InitInOut(inl,outl);
-
- if((inl >= 0 && inl != inlets) || (outl >= 0 && outl != outlets))
- post("%s - Inlet and outlet count can't be changed by reload",thisName());
- }
-
-// return ok;
-}
-
-void pyext::Unload()
-{
- DoExit();
- pyobj = NULL;
-}
-
-void pyext::m_get(const t_symbol *s)
-{
- ThrState state = PyLockSys();
-
- PyObject *pvar = PyObject_GetAttrString(pyobj,const_cast<char *>(GetString(s))); /* fetch bound method */
- if(pvar) {
- flext::AtomListStatic<16> lst;
- const t_symbol *sym = GetPyArgs(lst,pvar,1);
- if(sym) {
- FLEXT_ASSERT(!IsAnything(sym));
- // dump value to attribute outlet
- SetSymbol(lst[0],s);
- ToOutAnything(GetOutAttr(),sym_get,lst.Count(),lst.Atoms());
- }
-
- Py_DECREF(pvar);
- }
-
- Report();
-
- PyUnlock(state);
-}
-
-void pyext::m_set(int argc,const t_atom *argv)
-{
- ThrState state = PyLockSys();
-
- if(argc < 2 || !IsString(argv[0]))
- post("%s - Syntax: set varname arguments...",thisName());
- else if(*GetString(argv[0]) == '_')
- post("%s - set: variables with leading _ are reserved and can't be set",thisName());
- else {
- char *ch = const_cast<char *>(GetString(argv[0]));
- if(PyObject_HasAttrString(pyobj,ch)) {
- PyObject *pval = MakePyArgs(NULL,argc-1,argv+1);
- if(pval) {
- if(PySequence_Size(pval) == 1) {
- // reduce lists of one element to element itself
-
- PyObject *val1 = PySequence_GetItem(pval,0); // new reference
- Py_DECREF(pval);
- pval = val1;
- }
-
- PyObject_SetAttrString(pyobj,ch,pval);
- Py_DECREF(pval);
- }
- }
- }
-
- Report();
-
- PyUnlock(state);
-}
-
-
-bool pyext::CbMethodResort(int n,const t_symbol *s,int argc,const t_atom *argv)
-{
- if(!n)
- return flext_dsp::CbMethodResort(n,s,argc,argv);
-
- return pyobj && work(n,s,argc,argv);
-}
-
-
-void pyext::m_help()
-{
- post("");
- post("%s %s - python class object, (C)2002-2008 Thomas Grill",thisName(),PY__VERSION);
-#ifdef FLEXT_DEBUG
- post("DEBUG VERSION, compiled on " __DATE__ " " __TIME__);
-#endif
-
- post("Arguments: %s {inlets outlets} [script name] [class name] {args...}",thisName());
-
- post("Inlet 1: messages to control the pyext object");
- post(" 2...: python inlets");
- post("Outlets: python outlets");
- post("Methods:");
- post("\thelp: shows this help");
- post("\treload {args...}: reload python script");
- post("\treload. : reload with former arguments");
- post("\tdoc: display module doc string");
- post("\tdoc+: display class doc string");
- post("\tdir: dump module dictionary");
- post("\tdir+: dump class dictionary");
-#ifdef FLEXT_THREADS
- post("\tdetach 0/1: detach threads");
- post("\tstop {wait time (ms)}: stop threads");
-#endif
- post("");
-}
-
-void pyext::callpy(PyObject *fun,PyObject *args)
-{
- PyObject *ret = PyObject_CallObject(fun,args);
- if(ret) {
- // function worked fine
- if(!PyObject_Not(ret)) post("pyext - returned value is ignored");
- Py_DECREF(ret);
- }
-}
-
-
-bool pyext::call(const char *meth,int inlet,const t_symbol *s,int argc,const t_atom *argv)
-{
- bool ret = false;
-
- PyObject *pmeth = PyObject_GetAttrString(pyobj,const_cast<char *>(meth)); /* fetch bound method */
- if(pmeth == NULL) {
- PyErr_Clear(); // no method found
- }
- else {
- PyObject *pargs = MakePyArgs(s,argc,argv,inlet?inlet:-1); //,true);
- if(!pargs) {
- PyErr_Print();
- Py_DECREF(pmeth);
- }
- else {
- gencall(pmeth,pargs);
- ret = true;
- }
- }
- return ret;
-}
-
-bool pyext::work(int n,const t_symbol *s,int argc,const t_atom *argv)
-{
- bool ret = false;
-
- ThrState state = PyLock();
-
- // should be enough...
- char str[256];
-
- // offset inlet index by signal inlets
- // \note first one is shared with messages!
- if(siginlets) n += siginlets-1;
-
- // try tag/inlet
- if(!ret) {
- sprintf(str,"%s_%i",GetString(s),n);
- ret = call(str,0,NULL,argc,argv);
- }
-
- if(!ret && argc == 1) {
- if(s == sym_float) {
- // try truncated float
- t_atom at; SetInt(at,GetAInt(argv[0]));
- sprintf(str,"int_%i",n);
- ret = call(str,0,NULL,1,&at);
- }
- else if(s == sym_int) {
- // try floating int
- t_atom at; SetFloat(at,GetAFloat(argv[0]));
- sprintf(str,"float_%i",n);
- ret = call(str,0,NULL,1,&at);
- }
- }
-
- // try anything/inlet
- if(!ret) {
- sprintf(str,"_anything_%i",n);
- ret = call(str,0,s,argc,argv);
- }
-
- // try tag at any inlet
- if(!ret) {
- sprintf(str,"%s_",GetString(s));
- ret = call(str,n,NULL,argc,argv);
- }
-
- if(!ret && argc == 1) {
- if(s == sym_float) {
- // try truncated float at any inlet
- t_atom at; SetInt(at,GetAInt(argv[0]));
- ret = call("int_",0,NULL,1,&at);
- }
- else if(s == sym_int) {
- // try floating int at any inlet
- t_atom at; SetFloat(at,GetAFloat(argv[0]));
- ret = call("float_",0,NULL,1,&at);
- }
- }
-
- if(!ret) {
- // try anything at any inlet
- const char *str1 = "_anything_";
- if(s == sym_bang && !argc) {
- t_atom argv;
- SetSymbol(argv,sym__);
- ret = call(str1,n,s,1,&argv);
- }
- else
- ret = call(str1,n,s,argc,argv);
- }
-
- if(!ret)
- // no matching python method found
- post("%s - no matching method found for '%s' into inlet %i",thisName(),GetString(s),n);
-
- PyUnlock(state);
-
- Respond(ret);
- return ret;
-}
-
-PyObject *pyext::GetSig(int ix,bool in) { return NULL; }
-
-void pyext::CbClick() { pybase::OpenEditor(); }
-bool pyext::CbDsp() { return false; }
-
-void pyext::DumpOut(const t_symbol *sym,int argc,const t_atom *argv)
-{
- ToOutAnything(GetOutAttr(),sym?sym:thisTag(),argc,argv);
-}
diff --git a/externals/grill/py/source/pyext.h b/externals/grill/py/source/pyext.h
deleted file mode 100644
index acfef40d..00000000
--- a/externals/grill/py/source/pyext.h
+++ /dev/null
@@ -1,158 +0,0 @@
-/*
-py/pyext - python external object for PD and MaxMSP
-
-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 __PYEXT_H
-#define __PYEXT_H
-
-#include "pybase.h"
-
-class pyext
- : public pybase
- , public flext_dsp
-{
- FLEXT_HEADER_S(pyext,flext_dsp,Setup)
-
-public:
- pyext(int argc,const t_atom *argv,bool sig = false);
-
- static PyObject *pyext__str__(PyObject *,PyObject *args);
-
- static PyObject *pyext_outlet(PyObject *,PyObject *args);
-#if FLEXT_SYS == FLEXT_SYS_PD
- static PyObject *pyext_tocanvas(PyObject *,PyObject *args);
-#endif
-
- static PyObject *pyext_setattr(PyObject *,PyObject *args);
- static PyObject *pyext_getattr(PyObject *,PyObject *args);
-
- static PyObject *pyext_detach(PyObject *,PyObject *args);
- static PyObject *pyext_stop(PyObject *,PyObject *args);
- static PyObject *pyext_isthreaded(PyObject *,PyObject *);
-
- static PyObject *pyext_inbuf(PyObject *,PyObject *args);
- static PyObject *pyext_invec(PyObject *,PyObject *args);
- static PyObject *pyext_outbuf(PyObject *,PyObject *args);
- static PyObject *pyext_outvec(PyObject *,PyObject *args);
-
- int Inlets() const { return inlets; }
- int Outlets() const { return outlets; }
-
- static pyext *GetThis(PyObject *self);
-
-protected:
-
- virtual bool Init();
- virtual bool Finalize();
- virtual void Exit();
-
- virtual bool CbMethodResort(int n,const t_symbol *s,int argc,const t_atom *argv);
- virtual void CbClick();
- virtual bool CbDsp();
-
- virtual void DumpOut(const t_symbol *sym,int argc,const t_atom *argv);
-
- bool work(int n,const t_symbol *s,int argc,const t_atom *argv);
-
- void m_help();
-
- void m_reload() { Reload(); }
- void m_reload_(int argc,const t_atom *argv) { initargs(argc,argv); Reload(); }
- void ms_initargs(const AtomList &a) { m_reload_(a.Count(),a.Atoms()); }
- void m_dir_() { m__dir(pyobj); }
- void mg_dir_(AtomList &lst) { GetDir(pyobj,lst); }
- void m_doc_() { m__doc(((PyInstanceObject *)pyobj)->in_class->cl_dict); }
-
- void m_get(const t_symbol *s);
- void m_set(int argc,const t_atom *argv);
-
- const t_symbol *methname;
- PyObject *pyobj;
- int inlets,outlets;
- int siginlets,sigoutlets;
-
- flext::AtomList initargs;
-
- virtual void LoadModule();
- virtual void UnloadModule();
-
- virtual void Load();
- virtual void Unload();
-
- virtual bool DoInit();
- virtual void DoExit();
-
- virtual PyObject *GetSig(int ix,bool in);
-
-private:
- static void Setup(t_classid);
-
- void SetThis();
- void ClearThis();
-
- void ClearBinding();
- bool MakeInstance();
- bool InitInOut(int &inlets,int &outlets);
-
- static PyObject *class_obj,*class_dict;
- static PyMethodDef attr_tbl[],meth_tbl[];
- static const char *pyext_doc;
-
- // -------- bind stuff ------------------
- static PyObject *pyext_bind(PyObject *,PyObject *args);
- static PyObject *pyext_unbind(PyObject *,PyObject *args);
-
- // ---------------------------
-
- bool call(const char *meth,int inlet,const t_symbol *s,int argc,const t_atom *argv);
-
- virtual void callpy(PyObject *fun,PyObject *args);
- static bool stcallpy(PyObject *fun,PyObject *args);
-
-#ifndef PY_USE_GIL
- ThrState pythr;
-#endif
-
-private:
- static bool boundmeth(flext_base *,t_symbol *sym,int argc,t_atom *argv,void *data);
-
- FLEXT_CALLBACK(m_help)
-
- FLEXT_CALLBACK(m_reload)
- FLEXT_CALLBACK_V(m_reload_)
- FLEXT_CALLBACK(m_dir_)
- FLEXT_CALLGET_V(mg_dir_)
- FLEXT_CALLBACK(m_doc_)
-
- FLEXT_ATTRGET_V(initargs)
- FLEXT_CALLSET_V(ms_initargs)
-
- FLEXT_CALLBACK_S(m_get)
- FLEXT_CALLBACK_V(m_set)
-
- // callbacks
- FLEXT_ATTRVAR_I(detach)
- FLEXT_ATTRVAR_B(pymsg)
- FLEXT_ATTRVAR_B(respond)
-
- FLEXT_CALLBACK_V(m_stop)
- FLEXT_CALLBACK(m_dir)
- FLEXT_CALLGET_V(mg_dir)
- FLEXT_CALLBACK(m_doc)
-
- FLEXT_CALLBACK(CbClick)
-
-#ifdef FLEXT_THREADS
- FLEXT_CALLBACK_T(tick)
-#endif
-};
-
-#endif
diff --git a/externals/grill/py/source/pymeth.cpp b/externals/grill/py/source/pymeth.cpp
deleted file mode 100644
index e696e89b..00000000
--- a/externals/grill/py/source/pymeth.cpp
+++ /dev/null
@@ -1,430 +0,0 @@
-/*
-py/pyext - python script object for PD and Max/MSP
-
-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 $
-*/
-
-#include "pybase.h"
-#include <map>
-
-struct xlt { const t_symbol *from,*to; };
-
-static const xlt xtdefs[] = {
- { flext::MakeSymbol("+"),flext::MakeSymbol("__add__") },
- { flext::MakeSymbol("+="),flext::MakeSymbol("__iadd__") },
- { flext::MakeSymbol("!+"),flext::MakeSymbol("__radd__") },
- { flext::MakeSymbol("-"),flext::MakeSymbol("__sub__") },
- { flext::MakeSymbol("-="),flext::MakeSymbol("__isub__") },
- { flext::MakeSymbol("!-"),flext::MakeSymbol("__rsub__") },
- { flext::MakeSymbol("*"),flext::MakeSymbol("__mul__") },
- { flext::MakeSymbol("*="),flext::MakeSymbol("__imul__") },
- { flext::MakeSymbol("!*"),flext::MakeSymbol("__rmul__") },
- { flext::MakeSymbol("/"),flext::MakeSymbol("__div__") },
- { flext::MakeSymbol("/="),flext::MakeSymbol("__idiv__") },
- { flext::MakeSymbol("!/"),flext::MakeSymbol("__rdiv__") },
- { flext::MakeSymbol("//"),flext::MakeSymbol("__floordiv__") },
- { flext::MakeSymbol("//="),flext::MakeSymbol("__ifloordiv__") },
- { flext::MakeSymbol("!//"),flext::MakeSymbol("__rfloordiv__") },
- { flext::MakeSymbol("%"),flext::MakeSymbol("__mod__") },
- { flext::MakeSymbol("%="),flext::MakeSymbol("__imod__") },
- { flext::MakeSymbol("!%"),flext::MakeSymbol("__rmod__") },
- { flext::MakeSymbol("**"),flext::MakeSymbol("__pow__") },
- { flext::MakeSymbol("**="),flext::MakeSymbol("__ipow__") },
- { flext::MakeSymbol("!**"),flext::MakeSymbol("__rpow__") },
- { flext::MakeSymbol("&"),flext::MakeSymbol("__and__") },
- { flext::MakeSymbol("&="),flext::MakeSymbol("__iand__") },
- { flext::MakeSymbol("!&"),flext::MakeSymbol("__rand__") },
- { flext::MakeSymbol("|"),flext::MakeSymbol("__or__") },
- { flext::MakeSymbol("|="),flext::MakeSymbol("__ior__") },
- { flext::MakeSymbol("!|"),flext::MakeSymbol("__ror__") },
- { flext::MakeSymbol("^"),flext::MakeSymbol("__xor__") },
- { flext::MakeSymbol("^="),flext::MakeSymbol("__ixor__") },
- { flext::MakeSymbol("!^"),flext::MakeSymbol("__rxor__") },
- { flext::MakeSymbol("<<"),flext::MakeSymbol("__lshift__") },
- { flext::MakeSymbol("<<="),flext::MakeSymbol("__ilshift__") },
- { flext::MakeSymbol("!<<"),flext::MakeSymbol("__rlshift__") },
- { flext::MakeSymbol(">>"),flext::MakeSymbol("__rshift__") },
- { flext::MakeSymbol(">>="),flext::MakeSymbol("__irshift__") },
- { flext::MakeSymbol("!>>"),flext::MakeSymbol("__rrshift__") },
- { flext::MakeSymbol("=="),flext::MakeSymbol("__eq__") },
- { flext::MakeSymbol("!="),flext::MakeSymbol("__ne__") },
- { flext::MakeSymbol("<"),flext::MakeSymbol("__lt__") },
- { flext::MakeSymbol(">"),flext::MakeSymbol("__gt__") },
- { flext::MakeSymbol("<="),flext::MakeSymbol("__le__") },
- { flext::MakeSymbol(">="),flext::MakeSymbol("__ge__") },
- { flext::MakeSymbol("!"),flext::MakeSymbol("__nonzero__") },
- { flext::MakeSymbol("~"),flext::MakeSymbol("__invert__") },
- { flext::MakeSymbol("[]"),flext::MakeSymbol("__getitem__") },
- { flext::MakeSymbol("[]="),flext::MakeSymbol("__setitem__") },
- { flext::MakeSymbol("[:]"),flext::MakeSymbol("__getslice__") },
- { flext::MakeSymbol("[:]="),flext::MakeSymbol("__setslice__") },
-
- { flext::MakeSymbol(".abs"),flext::MakeSymbol("__abs__") },
- { flext::MakeSymbol(".neg"),flext::MakeSymbol("__neg__") },
- { flext::MakeSymbol(".pos"),flext::MakeSymbol("__pos__") },
- { flext::MakeSymbol(".divmod"),flext::MakeSymbol("__divmod__") },
-
- { flext::MakeSymbol(".int"),flext::MakeSymbol("__int__") },
- { flext::MakeSymbol(".long"),flext::MakeSymbol("__long__") },
- { flext::MakeSymbol(".float"),flext::MakeSymbol("__float__") },
- { flext::MakeSymbol(".complex"),flext::MakeSymbol("__complex__") },
- { flext::MakeSymbol(".str"),flext::MakeSymbol("__str__") },
- { flext::MakeSymbol(".coerce"),flext::MakeSymbol("__coerce__") },
-
- { flext::MakeSymbol(".doc"),flext::MakeSymbol("__doc__") },
- { flext::MakeSymbol(".repr"),flext::MakeSymbol("__repr__") },
-
- { flext::MakeSymbol(".len"),flext::MakeSymbol("__len__") },
- { flext::MakeSymbol(".in"),flext::MakeSymbol("__contains") },
-
- { NULL,NULL } // sentinel
-};
-
-typedef std::map<const t_symbol *,const t_symbol *> XTable;
-static XTable xtable;
-
-
-class pymeth
- : public pybase
- , public flext_base
-{
- FLEXT_HEADER_S(pymeth,flext_base,Setup)
-
-public:
- pymeth(int argc,const t_atom *argv);
- ~pymeth();
-
-protected:
- virtual void Exit();
-
- virtual bool CbMethodResort(int n,const t_symbol *s,int argc,const t_atom *argv);
-
- void m_help();
-
- void m_reload() { Reload(); }
- void m_reload_(int argc,const t_atom *argv) { args(argc,argv); Reload(); }
- void m_set(int argc,const t_atom *argv);
- void m_dir_() { m__dir(function); }
- void m_doc_() { m__doc(function); }
-
- const t_symbol *funname;
- PyObject *function;
-
- virtual void LoadModule();
- virtual void UnloadModule();
-
- virtual void Load();
- virtual void Unload();
-
- void SetFunction(const t_symbol *func);
- void ResetFunction();
-
- virtual void DumpOut(const t_symbol *sym,int argc,const t_atom *argv);
-
- PyObject **objects;
-
-private:
-
- virtual void callpy(PyObject *fun,PyObject *args);
-
- static void Setup(t_classid c);
-
- FLEXT_CALLBACK(m_help)
- FLEXT_CALLBACK(m_reload)
- FLEXT_CALLBACK_V(m_reload_)
- FLEXT_CALLBACK_V(m_set)
- FLEXT_CALLBACK(m_dir_)
- FLEXT_CALLBACK(m_doc_)
-
- // callbacks
- FLEXT_ATTRVAR_I(detach)
- FLEXT_ATTRVAR_B(pymsg)
- FLEXT_ATTRVAR_B(respond)
-
- FLEXT_CALLBACK_V(m_stop)
- FLEXT_CALLBACK(m_dir)
- FLEXT_CALLGET_V(mg_dir)
- FLEXT_CALLBACK(m_doc)
-
-#ifdef FLEXT_THREADS
- FLEXT_CALLBACK_T(tick)
-#endif
-};
-
-FLEXT_LIB_V("pym",pymeth)
-
-
-void pymeth::Setup(t_classid c)
-{
- FLEXT_CADDMETHOD_(c,0,"doc",m_doc);
- FLEXT_CADDMETHOD_(c,0,"dir",m_dir);
-#ifdef FLEXT_THREADS
- FLEXT_CADDATTR_VAR1(c,"detach",detach);
- FLEXT_CADDMETHOD_(c,0,"stop",m_stop);
-#endif
-
- FLEXT_CADDMETHOD_(c,0,"help",m_help);
- FLEXT_CADDMETHOD_(c,0,"reload",m_reload_);
- FLEXT_CADDMETHOD_(c,0,"reload.",m_reload);
- FLEXT_CADDMETHOD_(c,0,"doc+",m_doc_);
- FLEXT_CADDMETHOD_(c,0,"dir+",m_dir_);
-
- FLEXT_CADDMETHOD_(c,0,"set",m_set);
-
- FLEXT_CADDATTR_VAR1(c,"py",pymsg);
- FLEXT_CADDATTR_VAR1(c,"respond",respond);
-
- // init translation map
- for(const xlt *xi = xtdefs; xi->from; ++xi) xtable[xi->from] = xi->to;
-}
-
-pymeth::pymeth(int argc,const t_atom *argv)
- : funname(NULL)
- , function(NULL)
- , objects(NULL)
-{
-#ifdef FLEXT_THREADS
- FLEXT_ADDTIMER(stoptmr,tick);
-#endif
-
- ThrState state = PyLockSys();
-
- int inlets;
- if(argc && CanbeInt(*argv)) {
- inlets = GetAInt(*argv);
- if(inlets < 1) inlets = 1;
- argv++,argc--;
- }
- else inlets = 1;
-
- objects = new PyObject *[inlets];
- for(int i = 0; i < inlets; ++i) { objects[i] = Py_None; Py_INCREF(Py_None); }
-
- if(inlets <= 0) InitProblem();
-
- AddInAnything(1+(inlets < 0?1:inlets));
- AddOutAnything();
-
- Register(GetRegistry(REGNAME));
-
- if(argc) {
- const t_symbol *funnm = GetASymbol(*argv);
- argv++,argc--;
-
- if(funnm)
- SetFunction(funnm);
- else
- PyErr_SetString(PyExc_ValueError,"Invalid function name");
- }
-
- if(argc) args(argc,argv);
-
- Report();
-
- PyUnlock(state);
-}
-
-pymeth::~pymeth()
-{
- if(objects) {
- for(int i = 0; i < CntIn()-1; ++i) Py_DECREF(objects[i]);
- delete[] objects;
- }
-
- ThrState state = PyLockSys();
- Unregister(GetRegistry(REGNAME));
- Report();
- PyUnlock(state);
-}
-
-void pymeth::Exit()
-{
- pybase::Exit();
- flext_base::Exit();
-}
-
-void pymeth::m_set(int argc,const t_atom *argv)
-{
- ThrState state = PyLockSys();
-
- // function name has precedence
- if(argc >= 2) {
- const char *sn = GetAString(*argv);
- ++argv,--argc;
-
- if(sn) {
- if(!module || !strcmp(sn,PyModule_GetName(module))) {
- ImportModule(sn);
- Register(GetRegistry(REGNAME));
- }
- }
- else
- PyErr_SetString(PyExc_ValueError,"Invalid module name");
- }
-
- if(argc) {
- const t_symbol *fn = GetASymbol(*argv);
- if(fn)
- SetFunction(fn);
- else
- PyErr_SetString(PyExc_ValueError,"Invalid function name");
- }
-
- Report();
-
- PyUnlock(state);
-}
-
-void pymeth::m_help()
-{
- post("");
- post("%s %s - python method object, (C)2002-2008 Thomas Grill",thisName(),PY__VERSION);
-#ifdef FLEXT_DEBUG
- post("DEBUG VERSION, compiled on " __DATE__ " " __TIME__);
-#endif
-
- post("Arguments: %s [method name] {args...}",thisName());
-
- post("Inlet 1:messages to control the py object");
- post(" 2:call python function with message as argument(s)");
- post("Outlet: 1:return values from python function");
- post("Methods:");
- post("\thelp: shows this help");
- post("\tbang: call script without arguments");
- post("\tset [script name] [function name]: set (script and) function name");
- post("\treload {args...}: reload python script");
- post("\treload. : reload with former arguments");
- post("\tdoc: display module doc string");
- post("\tdoc+: display function doc string");
- post("\tdir: dump module dictionary");
- post("\tdir+: dump function dictionary");
-#ifdef FLEXT_THREADS
- post("\tdetach 0/1/2: detach threads");
- post("\tstop {wait time (ms)}: stop threads");
-#endif
- post("");
-}
-
-void pymeth::ResetFunction()
-{
- Py_XDECREF(function);
- function = NULL;
-
- if(funname && objects[0] != Py_None) {
- function = PyObject_GetAttrString(objects[0],(char *)GetString(funname)); // new reference
- if(!function)
- PyErr_SetString(PyExc_AttributeError,"Method not found");
- }
-
- // exception could be set here
-}
-
-void pymeth::SetFunction(const t_symbol *func)
-{
- // look for method name in translation table
- XTable::iterator it = xtable.find(func);
- funname = it == xtable.end()?func:it->second;
-
- ResetFunction();
-}
-
-
-void pymeth::LoadModule()
-{
- SetFunction(funname);
-}
-
-void pymeth::UnloadModule()
-{
-}
-
-void pymeth::Load()
-{
- ResetFunction();
-}
-
-void pymeth::Unload()
-{
- SetFunction(NULL);
-}
-
-void pymeth::callpy(PyObject *fun,PyObject *args)
-{
- PyObject *ret = PyObject_CallObject(fun,args);
- if(ret) {
- OutObject(this,0,ret); // exception might be raised here
- Py_DECREF(ret);
- }
-}
-
-bool pymeth::CbMethodResort(int n,const t_symbol *s,int argc,const t_atom *argv)
-{
- if(n == 0 && s != sym_bang)
- return flext_base::CbMethodResort(n,s,argc,argv);
-
- ThrState state = PyLockSys();
-
- bool ret = false;
-
- if(n >= 1) {
- // store args
- PyObject *&obj = objects[n-1];
- Py_DECREF(obj);
- obj = MakePyArg(s,argc,argv); // steal reference
-
- if(n > 1) ret = true; // just store, don't trigger
- }
-
- if(!ret) {
- if(function) {
- PyObject *self = PyMethod_Self(function);
- PyErr_Clear();
- if(!self || self->ob_type != objects[0]->ob_type)
- // type has changed, search for new method
- ResetFunction();
- else if(self != objects[0]) {
- // type hasn't changed, but object has
- PyObject *f = function;
- function = PyMethod_New(PyMethod_GET_FUNCTION(f),objects[0],PyMethod_GET_CLASS(f));
- Py_DECREF(f);
- }
- }
- else
- ResetFunction();
-
- if(function) {
- Py_INCREF(function);
-
- int inlets = CntIn()-1;
- PyObject *pargs = PyTuple_New(inlets-1);
- for(int i = 1; i < inlets; ++i) {
- Py_INCREF(objects[i]);
- PyTuple_SET_ITEM(pargs,i-1,objects[i]);
- }
-
- gencall(function,pargs); // references are stolen
- ret = true;
- }
- else
- PyErr_SetString(PyExc_RuntimeError,"No function set");
-
- Report();
- }
-
- PyUnlock(state);
-
- Respond(ret);
-
- return ret;
-}
-
-void pymeth::DumpOut(const t_symbol *sym,int argc,const t_atom *argv)
-{
- ToOutAnything(GetOutAttr(),sym?sym:thisTag(),argc,argv);
-}
diff --git a/externals/grill/py/source/pyprefix.h b/externals/grill/py/source/pyprefix.h
deleted file mode 100644
index 0614b3e0..00000000
--- a/externals/grill/py/source/pyprefix.h
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
-py/pyext - python script object for PD and MaxMSP
-
-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-04 12:58:11 +0100 (Fri, 04 Jan 2008) $
-$LastChangedBy: thomas $
-*/
-
-#ifndef __PYPREFIX_H
-#define __PYPREFIX_H
-
-#define FLEXT_ATTRIBUTES 1
-#include <flext.h>
-
-// hack: must include math.h before Python.h (at least on OSX)
-// otherwise some functions don't get defined
-#include <math.h>
-
-#if FLEXT_OS == FLEXT_OS_MAC
-#include <Python/Python.h>
-#else
-#include <Python.h>
-#endif
-
-#if !defined(FLEXT_VERSION) || (FLEXT_VERSION < 501)
-#error You need at least flext version 0.5.1
-#endif
-
-#if FLEXT_OS == FLEXT_LINUX || FLEXT_OS == FLEXT_IRIX
-#include <unistd.h>
-#endif
-
-#if FLEXT_SYS == FLEXT_SYS_PD && (!defined (PD_MINOR_VERSION) || PD_MINOR_VERSION < 37)
-#error PD version >= 0.37 required, please upgrade!
-#endif
-
-#include <flcontainers.h>
-#include <string>
-
-#if FLEXT_SYS == FLEXT_SYS_PD && defined(PY_USE_INOFFICIAL)
-extern "C" {
-#include <s_stuff.h>
-}
-#endif
-
-#if PY_VERSION_HEX < 0x02050000
-typedef int Py_ssize_t;
-#endif
-
-#endif
diff --git a/externals/grill/py/source/pysymbol.cpp b/externals/grill/py/source/pysymbol.cpp
deleted file mode 100644
index b812e134..00000000
--- a/externals/grill/py/source/pysymbol.cpp
+++ /dev/null
@@ -1,266 +0,0 @@
-/*
-py/pyext - python script object for PD and Max/MSP
-
-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 18:15:53 +0100 (Thu, 03 Jan 2008) $
-$LastChangedBy: thomas $
-*/
-
-#include "pyprefix.h"
-#include "pysymbol.h"
-
-inline pySymbol *symbol_newsym(const t_symbol *sym)
-{
- pySymbol *self = (pySymbol *)pySymbol_Type.tp_alloc(&pySymbol_Type, 0);
- if(self) self->sym = sym;
- return self;
-}
-
-static PyObject *symbol_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
-{
- return (PyObject *)symbol_newsym(flext::sym__);
-}
-
-static int symbol_init(PyObject *self, PyObject *args, PyObject *kwds)
-{
- FLEXT_ASSERT(pySymbol_Check(self));
-
- PyObject *arg = PySequence_GetItem(args,0); // new reference
- if(!arg) return -1;
-
- int ret = 0;
-
- if(pySymbol_Check(arg))
- ((pySymbol *)self)->sym = pySymbol_AS_SYMBOL(arg);
- else if(PyString_Check(arg))
- ((pySymbol *)self)->sym = flext::MakeSymbol(PyString_AS_STRING(arg));
- else {
- PyErr_SetString(PyExc_TypeError,"string or symbol argument expected");
- ret = -1;
- }
- Py_DECREF(arg);
-
- return ret;
-}
-
-static PyObject *symbol_str(PyObject *self)
-{
- FLEXT_ASSERT(pySymbol_Check(self));
- return (PyObject *)PyString_FromString(pySymbol_AS_STRING(self));
-}
-
-static PyObject *symbol_repr(PyObject *self)
-{
- FLEXT_ASSERT(pySymbol_Check(self));
- return (PyObject *)PyString_FromFormat("<Symbol %s>",pySymbol_AS_STRING(self));
-}
-
-static PyObject *symbol_richcompare(PyObject *a,PyObject *b,int cmp)
-{
- if(pySymbol_Check(a) && pySymbol_Check(b)) {
- const t_symbol *asym = pySymbol_AS_SYMBOL(a);
- const t_symbol *bsym = pySymbol_AS_SYMBOL(b);
-
- int res = asym == bsym?0:strcmp(flext::GetString(asym),flext::GetString(bsym));
-
- bool ret;
- switch(cmp) {
- case Py_LT: ret = res < 0; break;
- case Py_LE: ret = res <= 0; break;
- case Py_EQ: ret = res == 0; break;
- case Py_NE: ret = res != 0; break;
- case Py_GE: ret = res >= 0; break;
- case Py_GT: ret = res > 0; break;
- default:
- FLEXT_ASSERT(false);
- }
- return PyBool_FromLong(ret);
- }
- Py_INCREF(Py_NotImplemented);
- return Py_NotImplemented;
-}
-
-static long symbol_hash(PyObject *self)
-{
- FLEXT_ASSERT(pySymbol_Check(self));
- return (long)pySymbol_AS_SYMBOL(self);
-}
-
-
-static Py_ssize_t symbol_length(PyObject *s)
-{
- pySymbol *self = reinterpret_cast<pySymbol *>(s);
- return strlen(flext::GetString(self->sym));
-}
-
-static PyObject *symbol_item(PyObject *s,Py_ssize_t i)
-{
- pySymbol *self = reinterpret_cast<pySymbol *>(s);
- const char *str = flext::GetString(self->sym);
- int len = strlen(str);
- if(i < 0) i += len;
-
- if(i >= 0 && i < len)
- return PyString_FromStringAndSize(str+i,1);
- else {
- Py_INCREF(Py_None);
- return Py_None;
- }
-}
-
-static PyObject *symbol_slice(PyObject *s,Py_ssize_t ilow = 0,Py_ssize_t ihigh = 1<<(sizeof(int)*8-2))
-{
- pySymbol *self = reinterpret_cast<pySymbol *>(s);
- const char *str = flext::GetString(self->sym);
- int len = strlen(str);
- if(ilow < 0) {
- ilow += len;
- if(ilow < 0) ilow = 0;
- }
- if(ihigh < 0) ihigh += len;
- if(ihigh >= len) ihigh = len-1;
-
- return PyString_FromStringAndSize(str+ilow,ilow <= ihigh?ihigh-ilow+1:0);
-}
-
-static PyObject *symbol_concat(PyObject *s,PyObject *op)
-{
- pySymbol *self = reinterpret_cast<pySymbol *>(s);
- PyObject *nobj = symbol_slice(s); // take all
- if(nobj) {
- PyObject *ret = PySequence_Concat(nobj,op);
- Py_DECREF(nobj);
- return ret;
- }
- else
- return NULL;
-}
-
-static PyObject *symbol_repeat(PyObject *s,Py_ssize_t rep)
-{
- pySymbol *self = reinterpret_cast<pySymbol *>(s);
- PyObject *nobj = symbol_slice(s); // take all
- if(nobj) {
- PyObject *ret = PySequence_Repeat(nobj,rep);
- Py_DECREF(nobj);
- return ret;
- }
- else
- return NULL;
-}
-
-static PySequenceMethods symbol_as_seq = {
- symbol_length, /* inquiry sq_length; __len__ */
- symbol_concat, /* __add__ */
- symbol_repeat, /* __mul__ */
- symbol_item, /* intargfunc sq_item; __getitem__ */
- symbol_slice, /* intintargfunc sq_slice; __getslice__ */
- NULL, /* intobjargproc sq_ass_item; __setitem__ */
- NULL, /* intintobjargproc sq_ass_slice; __setslice__ */
-};
-
-static PyObject *symbol_iter(PyObject *s)
-{
- pySymbol *self = reinterpret_cast<pySymbol *>(s);
- PyObject *nobj = symbol_slice(s);
- if(nobj) {
- PyObject *it = PyObject_GetIter(nobj);
- Py_DECREF(nobj);
- return it;
- }
- else
- return NULL;
-}
-
-
-
-PyTypeObject pySymbol_Type = {
- PyObject_HEAD_INIT(NULL)
- 0, /*ob_size*/
- "Symbol", /*tp_name*/
- sizeof(pySymbol), /*tp_basicsize*/
- 0, /*tp_itemsize*/
- 0, /*tp_dealloc*/
- 0, /*tp_print*/
- 0, /*tp_getattr*/
- 0, /*tp_setattr*/
- 0, /*tp_compare*/
- symbol_repr, /*tp_repr*/
- 0, /*tp_as_number*/
- &symbol_as_seq, /*tp_as_sequence*/
- 0, /*tp_as_mapping*/
- symbol_hash, /*tp_hash */
- 0, /*tp_call*/
- symbol_str, /*tp_str*/
- 0, /*tp_getattro*/
- 0, /*tp_setattro*/
- 0, /*tp_as_buffer*/
- Py_TPFLAGS_DEFAULT /*| Py_TPFLAGS_BASETYPE*/, /*tp_flags*/
- "Symbol objects", /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- symbol_richcompare, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- symbol_iter, /* tp_iter */
- 0, /* tp_iternext */
- 0, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- symbol_init, /* tp_init */
- 0, /* tp_alloc */
- symbol_new, /* tp_new */
-};
-
-pySymbol *pySymbol__;
-pySymbol *pySymbol_bang;
-pySymbol *pySymbol_list;
-pySymbol *pySymbol_symbol;
-pySymbol *pySymbol_float;
-pySymbol *pySymbol_int;
-
-
-void initsymbol()
-{
- if(PyType_Ready(&pySymbol_Type) < 0)
- return;
-
- Py_INCREF(&pySymbol_Type);
-
- // initialize predefined objects
- pySymbol__ = symbol_newsym(flext::sym__);
- pySymbol_bang = symbol_newsym(flext::sym_bang);
- pySymbol_list = symbol_newsym(flext::sym_list);
- pySymbol_symbol = symbol_newsym(flext::sym_symbol);
- pySymbol_float = symbol_newsym(flext::sym_float);
- pySymbol_int = symbol_newsym(flext::sym_int);
-}
-
-
-PyObject *pySymbol_FromSymbol(const t_symbol *sym)
-{
- pySymbol *op;
- if(sym == flext::sym__)
- Py_INCREF(op = pySymbol__);
- else if(sym == flext::sym_bang)
- Py_INCREF(op = pySymbol_bang);
- else if(sym == flext::sym_list)
- Py_INCREF(op = pySymbol_list);
- else if(sym == flext::sym_symbol)
- Py_INCREF(op = pySymbol_symbol);
- else if(sym == flext::sym_float)
- Py_INCREF(op = pySymbol_float);
- else if(sym == flext::sym_int)
- Py_INCREF(op = pySymbol_int);
- else
- op = symbol_newsym(sym);
- return (PyObject *)op;
-}
diff --git a/externals/grill/py/source/pysymbol.h b/externals/grill/py/source/pysymbol.h
deleted file mode 100644
index 6ab0aac9..00000000
--- a/externals/grill/py/source/pysymbol.h
+++ /dev/null
@@ -1,94 +0,0 @@
-/*
-py/pyext - python script object for PD and Max/MSP
-
-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 __PYSYMBOL_H
-#define __PYSYMBOL_H
-
-#include <flext.h>
-
-#if !defined(FLEXT_VERSION) || (FLEXT_VERSION < 500)
-#error You need at least flext version 0.5.0
-#endif
-
-#if FLEXT_OS == FLEXT_OS_MAC
-#include <Python/Python.h>
-#else
-#include <Python.h>
-#endif
-
-
-#ifdef _MSC_VER
- #ifdef PY_EXPORTS
- #define PY_EXPORT __declspec(dllexport)
- #else
- #define PY_EXPORT __declspec(dllimport)
- #endif
-#else
- #define PY_EXPORT
-#endif
-
-typedef struct {
- PyObject_HEAD
- /* Type-specific fields go here. */
- const t_symbol *sym;
-} pySymbol;
-
-PY_EXPORT extern PyTypeObject pySymbol_Type;
-
-PY_EXPORT extern pySymbol *pySymbol__;
-PY_EXPORT extern pySymbol *pySymbol_bang;
-PY_EXPORT extern pySymbol *pySymbol_list;
-PY_EXPORT extern pySymbol *pySymbol_symbol;
-PY_EXPORT extern pySymbol *pySymbol_float;
-PY_EXPORT extern pySymbol *pySymbol_int;
-
-
-#define pySymbol_Check(op) PyObject_TypeCheck(op, &pySymbol_Type)
-#define pySymbol_CheckExact(op) ((op)->ob_type == &pySymbol_Type)
-
-
-PY_EXPORT PyObject *pySymbol_FromSymbol(const t_symbol *sym);
-
-inline PyObject *pySymbol_FromString(const char *str)
-{
- return pySymbol_FromSymbol(flext::MakeSymbol(str));
-}
-
-inline PyObject *pySymbol_FromString(PyObject *str)
-{
- return pySymbol_FromString(PyString_AsString(str));
-}
-
-inline const t_symbol *pySymbol_AS_SYMBOL(PyObject *op)
-{
- return ((pySymbol *)op)->sym;
-}
-
-inline const t_symbol *pySymbol_AsSymbol(PyObject *op)
-{
- return pySymbol_Check(op)?pySymbol_AS_SYMBOL(op):NULL;
-}
-
-inline const char *pySymbol_AS_STRING(PyObject *op)
-{
- return flext::GetString(pySymbol_AS_SYMBOL(op));
-}
-
-inline const t_symbol *pyObject_AsSymbol(PyObject *op)
-{
- if(PyString_Check(op))
- return flext::MakeSymbol(PyString_AS_STRING(op));
- else
- return pySymbol_AsSymbol(op);
-}
-
-#endif
diff --git a/externals/grill/py/source/register.cpp b/externals/grill/py/source/register.cpp
deleted file mode 100644
index 0f8684b6..00000000
--- a/externals/grill/py/source/register.cpp
+++ /dev/null
@@ -1,153 +0,0 @@
-/*
-py/pyext - python external object for PD and MaxMSP
-
-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 $
-*/
-
-#include "pybase.h"
-
-#if 1
-
-PyObject *pybase::GetRegistry(const char *regnm)
-{
- if(module) {
- FLEXT_ASSERT(dict); // module must have a valid dict
-
- // add this to module registry
- PyObject *reg = PyDict_GetItemString(dict,(char *)regnm); // borrowed!!!
- if(reg)
- FLEXT_ASSERT(PyDict_Check(reg));
- else {
- // make a new empty registry
- reg = PyDict_New();
- PyDict_SetItemString(dict,(char *)regnm,reg);
- }
- return reg;
- }
- else
- return NULL;
-}
-
-void pybase::SetRegistry(const char *regnm,PyObject *reg)
-{
- if(module) {
- FLEXT_ASSERT(dict); // module must have a valid dict
- FLEXT_ASSERT(reg && PyDict_Check(reg));
- PyDict_SetItemString(dict,(char *)regnm,reg);
- }
-}
-
-void pybase::Register(PyObject *reg)
-{
- if(!module) return;
- FLEXT_ASSERT(reg && PyDict_Check(reg));
-
- // add this to module registry
- Py_INCREF(Py_None);
- PyObject *key = PyLong_FromUnsignedLong((size_t)this);
- PyDict_SetItem(reg,key,Py_None);
-}
-
-void pybase::Unregister(PyObject *reg)
-{
- if(!module) return;
- FLEXT_ASSERT(reg && PyDict_Check(reg));
-
- // remove this from module registry
- PyObject *key = PyLong_FromUnsignedLong((size_t)this);
- PyObject *item = PyDict_GetItem(reg,key);
- if(!item)
- post("py/pyext - Internal error: object not found in registry");
- else
- PyDict_DelItem(reg,key);
-}
-
-/*
-void pybase::RegLoad(PyObject *reg)
-{
-
-}
-
-void pybase::RegUnload(PyObject *reg)
-{
-}
-*/
-
-#else
-
-void pybase::Register(const char *regnm)
-{
- if(module) {
- // add this to module registry
-
- PyObject *reg = PyDict_GetItemString(dict,(char *)regnm); // borrowed!!!
- PyObject *add = Py_BuildValue("[i]",(long)this);
- if(!reg || !PyList_Check(reg)) {
- if(PyDict_SetItemString(dict,(char *)regnm,add)) {
- post("py/pyext - Could not set registry");
- }
- }
- else {
- PySequence_InPlaceConcat(reg,add);
- }
- }
-}
-
-void pybase::Unregister(const char *regnm)
-{
- if(module) {
- // remove this from module registry
-
- PyObject *reg = PyDict_GetItemString(dict,(char *)regnm); // borrowed!!!
- PyObject *add = Py_BuildValue("i",(int)this);
- if(!reg || !PySequence_Check(reg))
- post("py/pyext - Internal error: Registry not found!?");
- else {
- int ix = PySequence_Index(reg,add);
- if(ix < 0) {
- post("py/pyext - Internal error: object not found in registry?!");
- }
- else {
- PySequence_DelItem(reg,ix);
- }
- }
- Py_DECREF(add);
- }
-}
-
-void pybase::Reregister(const char *regnm)
-{
- if(module) {
- // remove this from module registry
-
- PyObject *reg = PyDict_GetItemString(dict,(char *)regnm); // borrowed!!!
-
- if(!reg || !PySequence_Check(reg))
- post("py/pyext - Internal error: Registry not found!?");
- else {
- int cnt = PySequence_Size(reg);
- for(int i = 0; i < cnt; ++i) {
- PyObject *it = PySequence_GetItem(reg,i); // new reference
- if(!it || !PyInt_Check(it)) {
- post("py/pyext - Internal error: Corrupt registry?!");
- }
- else {
- pybase *th = (pybase *)PyInt_AsLong(it);
- th->module = module;
- th->dict = dict;
- th->Reload();
- }
-
- Py_XDECREF(it);
- }
- }
- }
-}
-
-#endif