Text archives Help
- From: bigler@sci.utah.edu
- To: manta@sci.utah.edu
- Subject: [MANTA] r1012 - trunk/SwigInterface
- Date: Tue, 25 Apr 2006 01:47:40 -0600 (MDT)
Author: bigler
Date: Tue Apr 25 01:47:38 2006
New Revision: 1012
Modified:
trunk/SwigInterface/manta.i
trunk/SwigInterface/pycallback.cc
trunk/SwigInterface/pycallback.h
trunk/SwigInterface/pycallback.i
trunk/SwigInterface/wxManta.py
Log:
Got a working version of a python callback being used as a Manta
transaction.
manta.i
Added TValueInt (TValue<int>) template.
pycallback.cc
pycallback.h
Removed PyCallback::destroy.
Added callFromC and createMantaCallback.
pycallback.i
Got rid of some testing code that wasn't needed anymore.
Fixed link to pycallback.h from python_callback.h.
wxManta.py
Registeres a simple callback when started, and when you left click
the mouse.
Modified: trunk/SwigInterface/manta.i
==============================================================================
--- trunk/SwigInterface/manta.i (original)
+++ trunk/SwigInterface/manta.i Tue Apr 25 01:47:38 2006
@@ -89,6 +89,8 @@
class Camera;
+ %template(TValueInt) TValue<int>;
+
// %template(CallbackBase_2Data_int_int) CallbackBase_2Data<int, int>;
}
Modified: trunk/SwigInterface/pycallback.cc
==============================================================================
--- trunk/SwigInterface/pycallback.cc (original)
+++ trunk/SwigInterface/pycallback.cc Tue Apr 25 01:47:38 2006
@@ -27,6 +27,7 @@
*/
#include <SwigInterface/pycallback.h>
+#include <Interface/Callback.h>
using namespace Manta;
@@ -42,9 +43,27 @@
args(args)
{
fprintf(stderr, "Constructor called for %p\n", this);
+
// Do reference counting
Py_XINCREF(function);
Py_XINCREF(args);
+
+ //////////////////////////////////////////////
+ // We should do some error checking
+
+ // Is it callable
+ if (!PyCallable_Check(function)) {
+ PyErr_SetString(PyExc_TypeError, "first parameter must be callable");
+ // Throw an exception....
+ return;
+ }
+
+ // Check the args, but only if something was passed in
+ if (args != NULL && !PyTuple_Check(args)) {
+ PyErr_SetString(PyExc_TypeError, "argument 2 needs to be a tuple");
+ // Throw an exception....
+ return;
+ }
}
PyCallback::PyCallback(const PyCallback& copy)
@@ -64,12 +83,6 @@
Py_XDECREF(args);
}
-void
-PyCallback::destroy(void* obj) {
- PyCallback* cb = reinterpret_cast<PyCallback*>(obj);
- delete cb;
-}
-
PyObject*
PyCallback::call(PyObject* new_args)
{
@@ -89,6 +102,37 @@
}
return result;
+}
+
+void
+PyCallback::callFromC()
+{
+ // Get ready to call python code
+ PyGILState_STATE gstate;
+ gstate = PyGILState_Ensure();
+
+ // Call the python function
+ PyObject* result = call();
+
+ // Check the result
+ if (result == NULL) {
+ // Something went wrong
+ PyObject* error = PyErr_Occurred();
+ if (error) {
+ PyErr_Print();
+ }
+ } else {
+ // We don't care what the result is
+ Py_DECREF(result);
+ }
+
+ // Done with python code
+ PyGILState_Release(gstate);
+}
+
+CallbackBase_0Data*
+PyCallback::createMantaCallback() {
+ return Callback::create(this, &PyCallback::callFromC);
}
#if 0
Modified: trunk/SwigInterface/pycallback.h
==============================================================================
--- trunk/SwigInterface/pycallback.h (original)
+++ trunk/SwigInterface/pycallback.h Tue Apr 25 01:47:38 2006
@@ -35,6 +35,8 @@
#include <stdio.h>
namespace Manta {
+ class CallbackBase_0Data;
+
struct PyCallback {
PyCallback();
PyCallback(PyObject* function, PyObject* args=0);
@@ -42,20 +44,21 @@
~PyCallback();
+ // This is what you should call if calling directly from python
PyObject* call(PyObject* args = 0);
-
- static void destroy(void* obj);
+
+ // This calls the function and throws exceptions if something went
+ // bad. You should not call this from swigged code as it will
+ // lock the interpreter.
+ void callFromC();
+
+ CallbackBase_0Data* createMantaCallback();
PyObject* function;
PyObject* args;
};
-#if 0
- PyObject* create_python_callback(PyObject* self, PyObject* args);
- PyObject* call_python_callback(PyObject* self, PyObject* args);
-#endif
-
} // end namespace Manta
#endif // #ifndef Manta_SwigInterface_pycallback_h
Modified: trunk/SwigInterface/pycallback.i
==============================================================================
--- trunk/SwigInterface/pycallback.i (original)
+++ trunk/SwigInterface/pycallback.i Tue Apr 25 01:47:38 2006
@@ -1,29 +1,7 @@
%module pycallback
-
-
-// %typemap(in) PyObjectArgs {
-// $1 = $input
-// }
-
-%typemap(in) int {
- $1 = PyInt_AsLong($input);
-}
-
-%{
-#include <stdio.h>
-%}
-
-void printInt(int val);
-
-%{
- void printInt(int val) {
- fprintf(stdout, "int = %d\n", val);
- }
-%}
-
-%typemap(in) int;
-
+// This will allow passing of PyObject*'s back and forth between
+// python and C, through swig.
%typemap(in) PyObject* {
$1 = $input;
}
@@ -32,21 +10,8 @@
$result = $1;
}
-int countArgs(PyObject* args);
-
-%{
- int countArgs(PyObject* args) {
- if (!PyTuple_Check(args)) {
- fprintf(stderr, "Not a Tuple\n");
- return 0;
- }
- int num_args = PyTuple_Size(args);
- return num_args;
- }
-%}
-
%{
-#include <SwigInterface/python_callback.h>
+#include <SwigInterface/pycallback.h>
%}
-%include <SwigInterface/python_callback.h>
+%include <SwigInterface/pycallback.h>
Modified: trunk/SwigInterface/wxManta.py
==============================================================================
--- trunk/SwigInterface/wxManta.py (original)
+++ trunk/SwigInterface/wxManta.py Tue Apr 25 01:47:38 2006
@@ -4,6 +4,7 @@
import sys
from manta import *
+from pycallback import *
# Number of workers.
@@ -50,6 +51,10 @@
scene.getRenderParameters().maxDepth = 5
return scene
+def print1(s1):
+ print "1: %s" % s1
+
+
class App(wx.App) :
@@ -90,6 +95,12 @@
#######################################################################
# Setup the UI events.
self.frame.Bind( wx.EVT_MOTION, self.onMotion )
+ self.frame.Bind( wx.EVT_LEFT_DOWN, self.onLeftDown )
+
+
#######################################################################
+ # Add a callback
+ self.cb = PyCallback(print1, ("hello",))
+ self.engine.addTransaction("print1",
manta_new(self.cb.createMantaCallback()))
return True
@@ -110,7 +121,14 @@
print "Motion: %dx%d" % (pos.x, pos.y)
event.Skip()
-
+
###########################################################################
+ ## onLeftDown
+
###########################################################################
+ def onLeftDown( self, event ):
+ # Register the event with Manta
+ self.engine.addTransaction("print1",
manta_new(self.cb.createMantaCallback()))
+ event.Skip()
+
app = App()
app.MainLoop()
- [MANTA] r1012 - trunk/SwigInterface, bigler, 04/25/2006
Archive powered by MHonArc 2.6.16.