aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/py/source
diff options
context:
space:
mode:
authorThomas Grill <xovo@users.sourceforge.net>2003-10-21 02:38:31 +0000
committerThomas Grill <xovo@users.sourceforge.net>2003-10-21 02:38:31 +0000
commitb23e41d46360404ab7bc19d31f6021a52ee9a08c (patch)
treec009a8c41ad8f41be4f3005a517e564d2223755c /externals/grill/py/source
parent10543931660faf1e9f45558a451cf4aac65a7b91 (diff)
""
svn path=/trunk/; revision=1121
Diffstat (limited to 'externals/grill/py/source')
-rw-r--r--externals/grill/py/source/clmeth.cpp2
-rw-r--r--externals/grill/py/source/main.cpp49
-rw-r--r--externals/grill/py/source/main.h12
-rw-r--r--externals/grill/py/source/py.cpp29
-rw-r--r--externals/grill/py/source/pyext.cpp103
-rw-r--r--externals/grill/py/source/pyext.h10
6 files changed, 150 insertions, 55 deletions
diff --git a/externals/grill/py/source/clmeth.cpp b/externals/grill/py/source/clmeth.cpp
index f800aa7d..0c28563a 100644
--- a/externals/grill/py/source/clmeth.cpp
+++ b/externals/grill/py/source/clmeth.cpp
@@ -194,7 +194,7 @@ PyObject *pyext::pyext_detach(PyObject *,PyObject *args)
}
else {
pyext *ext = GetThis(self);
- ext->m_detach(val != 0);
+ ext->detach = val != 0;
}
Py_INCREF(Py_None);
diff --git a/externals/grill/py/source/main.cpp b/externals/grill/py/source/main.cpp
index 03e2aab7..72396afb 100644
--- a/externals/grill/py/source/main.cpp
+++ b/externals/grill/py/source/main.cpp
@@ -38,7 +38,6 @@ py::py():
stoptick(0)
{
Lock();
- // under Max/MSP @ OS9: doesn't survive next line.....
if(!(pyref++)) {
Py_Initialize();
@@ -121,20 +120,47 @@ py::~py()
}
-V py::m_doc()
+void py::m__dir(PyObject *obj)
{
- if(dict) {
- PyObject *docf = PyDict_GetItemString(dict,"__doc__"); // borrowed!!!
+ if(obj) {
+ PY_LOCK
+
+ PyObject *pvar = PyObject_Dir(obj);
+ if(pvar == NULL) {
+ PyErr_Print(); // no method found
+ }
+ else {
+ AtomList *lst = GetPyArgs(pvar);
+ if(lst) {
+ // dump dir to attribute outlet
+ ToOutAnything(GetOutAttr(),thisTag(),lst->Count(),lst->Atoms());
+ delete lst;
+ }
+ else
+ post("%s - %s: List could not be created",thisName(),GetString(thisTag()));
+ Py_DECREF(pvar);
+ }
+
+ PY_UNLOCK
+ }
+}
+
+V py::m__doc(PyObject *obj)
+{
+ if(obj) {
+ PY_LOCK
+
+ PyObject *docf = PyDict_GetItemString(obj,"__doc__"); // borrowed!!!
if(docf && PyString_Check(docf)) {
post("");
post(PyString_AsString(docf));
}
+
+ PY_UNLOCK
}
}
-
-
V py::SetArgs(I argc,const t_atom *argv)
{
// script arguments
@@ -202,6 +228,15 @@ V py::GetModulePath(const C *mod,C *dir,I len)
if(dir == name) strcpy(dir,".");
#elif FLEXT_SYS == FLEXT_SYS_MAX
// how do i get the path in Max/MSP?
+ short path;
+ long type;
+ char smod[256];
+ strcat(strcpy(smod,mod),".py");
+ if(!locatefile_extended(smod,&path,&type,&type,-1))
+ path_topathname(path,NULL,dir);
+ else
+ // not found
+ *dir = 0;
#else
*dir = 0;
#endif
@@ -225,5 +260,3 @@ V py::AddToPath(const C *dir)
PySys_SetObject("path",pobj);
}
}
-
-
diff --git a/externals/grill/py/source/main.h b/externals/grill/py/source/main.h
index fd25e1f1..5188b7c3 100644
--- a/externals/grill/py/source/main.h
+++ b/externals/grill/py/source/main.h
@@ -11,6 +11,8 @@ WARRANTIES, see the file, "license.txt," in this distribution.
#ifndef __MAIN_H
#define __MAIN_H
+#define FLEXT_ATTRIBUTES 1
+
#include <flext.h>
#include <Python.h>
#include <map>
@@ -59,7 +61,11 @@ public:
protected:
- V m_doc();
+ V m__dir(PyObject *obj);
+ V m__doc(PyObject *obj);
+
+ V m_dir() { m__dir(module); }
+ V m_doc() { m__doc(dict); }
PyObject *module,*dict; // inherited user class module and associated dictionary
@@ -99,7 +105,6 @@ protected:
// ----thread stuff ------------
- V m_detach(BL det) { detach = det; }
virtual V m_stop(int argc,const t_atom *argv);
BL detach,shouldexit;
@@ -125,8 +130,9 @@ public:
protected:
// callbacks
- FLEXT_CALLBACK_B(m_detach)
+ FLEXT_ATTRVAR_B(detach)
FLEXT_CALLBACK_V(m_stop)
+ FLEXT_CALLBACK(m_dir)
FLEXT_CALLBACK(m_doc)
FLEXT_CALLBACK_T(tick)
};
diff --git a/externals/grill/py/source/py.cpp b/externals/grill/py/source/py.cpp
index 1955c250..d30da239 100644
--- a/externals/grill/py/source/py.cpp
+++ b/externals/grill/py/source/py.cpp
@@ -29,7 +29,8 @@ protected:
V m_reload();
V m_reload_(I argc,const t_atom *argv);
V m_set(I argc,const t_atom *argv);
- V m_doc_();
+ V m_dir_() { m__dir(function); }
+ V m_doc_() { m__doc(function); }
virtual V m_help();
@@ -56,6 +57,7 @@ private:
FLEXT_CALLBACK(m_reload)
FLEXT_CALLBACK_V(m_reload_)
FLEXT_CALLBACK_V(m_set)
+ FLEXT_CALLBACK(m_dir_)
FLEXT_CALLBACK(m_doc_)
FLEXT_CALLBACK_V(m_py_float)
@@ -82,9 +84,11 @@ void pyobj::Setup(t_classid c)
FLEXT_CADDMETHOD_(c,0,"doc",m_doc);
FLEXT_CADDMETHOD_(c,0,"doc+",m_doc_);
#ifdef FLEXT_THREADS
- FLEXT_CADDMETHOD_(c,0,"detach",m_detach);
+ FLEXT_CADDATTR_VAR1(c,"detach",detach);
FLEXT_CADDMETHOD_(c,0,"stop",m_stop);
#endif
+ FLEXT_CADDMETHOD_(c,0,"dir",m_dir);
+ FLEXT_CADDMETHOD_(c,0,"dir+",m_dir_);
FLEXT_CADDMETHOD_(c,1,"float",m_py_float);
FLEXT_CADDMETHOD_(c,1,"int",m_py_int);
@@ -201,29 +205,12 @@ V pyobj::m_set(I argc,const t_atom *argv)
PY_UNLOCK
}
-
-V pyobj::m_doc_()
-{
- PY_LOCK
-
- if(function) {
- PyObject *docf = PyObject_GetAttrString(function,"__doc__"); // borrowed!!!
- if(docf && PyString_Check(docf)) {
- post("");
- post(PyString_AsString(docf));
- }
- }
-
- PY_UNLOCK
-}
-
-
V pyobj::m_help()
{
post("");
- post("py %s - python script object, (C)2002 Thomas Grill",PY__VERSION);
+ post("py %s - python script object, (C)2002,2003 Thomas Grill",PY__VERSION);
#ifdef FLEXT_DEBUG
- post("compiled on " __DATE__ " " __TIME__);
+ post("DEBUG VERSION, compiled on " __DATE__ " " __TIME__);
#endif
post("Arguments: %s [script name] [function name] {args...}",thisName());
diff --git a/externals/grill/py/source/pyext.cpp b/externals/grill/py/source/pyext.cpp
index 14d0f7b6..a8dc581e 100644
--- a/externals/grill/py/source/pyext.cpp
+++ b/externals/grill/py/source/pyext.cpp
@@ -18,13 +18,18 @@ V pyext::Setup(t_classid c)
{
FLEXT_CADDMETHOD_(c,0,"reload.",m_reload);
FLEXT_CADDMETHOD_(c,0,"reload",m_reload_);
+ FLEXT_CADDMETHOD_(c,0,"dir",m_dir);
+ FLEXT_CADDMETHOD_(c,0,"dir+",m_dir_);
FLEXT_CADDMETHOD_(c,0,"doc",m_doc);
FLEXT_CADDMETHOD_(c,0,"doc+",m_doc_);
#ifdef FLEXT_THREADS
- FLEXT_CADDMETHOD_(c,0,"detach",m_detach);
+ FLEXT_CADDATTR_VAR1(c,"detach",detach);
FLEXT_CADDMETHOD_(c,0,"stop",m_stop);
#endif
+
+ FLEXT_CADDMETHOD_(c,0,"get",m_get);
+ FLEXT_CADDMETHOD_(c,0,"set",m_set);
}
pyext *pyext::GetThis(PyObject *self)
@@ -37,6 +42,20 @@ pyext *pyext::GetThis(PyObject *self)
}
+#if FLEXT_SYS == FLEXT_SYS_MAX
+static short patcher_myvol(t_patcher *x)
+{
+ t_box *w;
+ if (x->p_vol)
+ return x->p_vol;
+ else if (w = (t_box *)x->p_vnewobj)
+ return patcher_myvol(w->b_patcher);
+ else
+ return 0;
+}
+#endif
+
+
I pyext::pyextref = 0;
PyObject *pyext::class_obj = NULL;
PyObject *pyext::class_dict = NULL;
@@ -89,16 +108,19 @@ pyext::pyext(I argc,const t_atom *argv):
// init script module
if(argc >= 1) {
- C dir[1024];
+ char dir[1024];
+
#if FLEXT_SYS == FLEXT_SYS_PD
// add dir of current patch to path
- strcpy(dir,GetString(canvas_getdir(thisCanvas())));
- AddToPath(dir);
+ AddToPath(GetString(canvas_getdir(thisCanvas())));
// add current dir to path
- strcpy(dir,GetString(canvas_getcurrentdir()));
- AddToPath(dir);
-#else
-#pragma message("Adding current dir to path is not implemented")
+ AddToPath(GetString(canvas_getcurrentdir()));
+#elif FLEXT_SYS == FLEXT_SYS_MAX
+ short path = patcher_myvol(thisCanvas());
+ path_topathname(path,NULL,dir);
+ AddToPath(dir);
+#else
+ #pragma message("Adding current dir to path is not implemented")
#endif
GetModulePath(GetString(argv[0]),dir,sizeof(dir));
@@ -274,22 +296,61 @@ V pyext::m_reload_(I argc,const t_atom *argv)
m_reload();
}
-V pyext::m_doc_()
+void pyext::m_get(const t_symbol *s)
{
- if(pyobj) {
- PY_LOCK
+ PY_LOCK
- PyObject *docf = PyObject_GetAttrString(pyobj,"__doc__"); // borrowed!!!
- if(docf && PyString_Check(docf)) {
- post("");
- post(PyString_AsString(docf));
- }
-
- PY_UNLOCK
+ PyObject *pvar = PyObject_GetAttrString(pyobj,const_cast<char *>(GetString(s))); /* fetch bound method */
+ if(pvar == NULL) {
+ PyErr_Clear(); // no method found
+ post("%s - get: Python variable %s not found",thisName(),GetString(s));
}
+ else {
+ AtomList *lst = GetPyArgs(pvar);
+ if(lst) {
+ // dump value to attribute outlet
+ AtomAnything out("get",lst->Count()+1);
+ SetSymbol(out[0],s);
+ out.Set(lst->Count(),lst->Atoms(),1);
+ delete lst;
+
+ ToOutAnything(GetOutAttr(),out);
+ }
+ else
+ post("%s - get: List could not be created",thisName());
+ Py_DECREF(pvar);
+ }
+
+ PY_UNLOCK
}
+void pyext::m_set(int argc,const t_atom *argv)
+{
+ PY_LOCK
+
+ 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)) {
+ PyErr_Clear(); // no method found
+ post("%s - set: Python variable %s not found",thisName(),ch);
+ }
+ else {
+ PyObject *pval = MakePyArgs(sym_list,AtomList(argc-1,argv+1),-1,false);
+ if(!pval)
+ PyErr_Print();
+ else {
+ PyObject_SetAttrString(pyobj,ch,pval);
+ Py_DECREF(pval);
+ }
+ }
+ }
+ PY_UNLOCK
+}
BL pyext::m_method_(I n,const t_symbol *s,I argc,const t_atom *argv)
@@ -307,9 +368,9 @@ BL pyext::m_method_(I n,const t_symbol *s,I argc,const t_atom *argv)
V pyext::m_help()
{
post("");
- post("pyext %s - python script object, (C)2002 Thomas Grill",PY__VERSION);
+ post("pyext %s - python script object, (C)2002,2003 Thomas Grill",PY__VERSION);
#ifdef FLEXT_DEBUG
- post("compiled on " __DATE__ " " __TIME__);
+ post("DEBUG VERSION, compiled on " __DATE__ " " __TIME__);
#endif
post("Arguments: %s [script name] [class name] {args...}",thisName());
@@ -345,7 +406,7 @@ PyObject *pyext::call(const C *meth,I inlet,const t_symbol *s,I argc,const t_ato
else {
ret = PyEval_CallObject(pmeth, pargs);
if (ret == NULL) // function not found resp. arguments not matching
-#if 1 //def FLEXT_DEBUG
+#ifdef FLEXT_DEBUG
PyErr_Print();
#else
PyErr_Clear();
diff --git a/externals/grill/py/source/pyext.h b/externals/grill/py/source/pyext.h
index 9b37b6e4..8fe99ccc 100644
--- a/externals/grill/py/source/pyext.h
+++ b/externals/grill/py/source/pyext.h
@@ -48,9 +48,13 @@ protected:
V m_reload();
V m_reload_(I argc,const t_atom *argv);
- V m_doc_();
+ V m_dir_() { m__dir(pyobj); }
+ V m_doc_() { m__doc(pyobj); }
virtual V m_help();
+ V m_get(const t_symbol *s);
+ V m_set(I argc,const t_atom *argv);
+
const t_symbol *methname;
PyObject *pyobj;
I inlets,outlets;
@@ -103,7 +107,11 @@ private:
FLEXT_CALLBACK(m_reload)
FLEXT_CALLBACK_V(m_reload_)
+ FLEXT_CALLBACK(m_dir_)
FLEXT_CALLBACK(m_doc_)
+
+ FLEXT_CALLBACK_S(m_get)
+ FLEXT_CALLBACK_V(m_set)
};