Text archives Help
- From: bigler@sci.utah.edu
- To: manta@sci.utah.edu
- Subject: [MANTA] r1145 - trunk/SwigInterface
- Date: Tue, 18 Jul 2006 17:53:21 -0600 (MDT)
Author: bigler
Date: Tue Jul 18 17:53:20 2006
New Revision: 1145
Modified:
trunk/SwigInterface/pycallback.cc
trunk/SwigInterface/pycallback.h
trunk/SwigInterface/wxManta.py
Log:
SwigInterface/pycallback.cc
SwigInterface/pycallback.h
Got rid of the PyCallback class. Just use namespace global
functions. All the static functions now are regular ones.
Revamped the way the python interpreter was locked. I was doing it
the wrong way. I'm surprised nothing broke.
The PyObjectContainer is largely useless as I don't keep reference
counts anymore. It is just too risky and causes the interpreter to
crash.
SwigInterface/wxManta.py
Got rid of some debug print statements.
Updated API for creating callbacks and transactions.
Resizing now is much better. The window starts off the right size
and I can squish it down to a 20x20 window.
Modified: trunk/SwigInterface/pycallback.cc
==============================================================================
--- trunk/SwigInterface/pycallback.cc (original)
+++ trunk/SwigInterface/pycallback.cc Tue Jul 18 17:53:20 2006
@@ -35,89 +35,64 @@
using namespace std;
+// #define INCREMENT_PYTHON_REFERENCE_COUNTER 1
// #define DECREMENT_PYTHON_REFERENCE_COUNTER 1
PyObjectContainer::PyObjectContainer(PyObject* object_in)
: object(object_in)
{
+#ifdef INCREMENT_PYTHON_REFERENCE_COUNTER
+ PyGILState_STATE gstate;
+ gstate = PyGILState_Ensure();
Py_XINCREF(object);
+ PyEval_ReleaseLock();
+#endif
}
// Copy constructor
PyObjectContainer::PyObjectContainer(const PyObjectContainer& copy)
: object(copy.object)
{
+#ifdef INCREMENT_PYTHON_REFERENCE_COUNTER
+ PyGILState_STATE gstate;
+ gstate = PyGILState_Ensure();
Py_XINCREF(object);
+ PyEval_ReleaseLock();
+#endif
}
PyObjectContainer::~PyObjectContainer()
{
#ifdef DECREMENT_PYTHON_REFERENCE_COUNTER
+ PyGILState_STATE gstate;
+ gstate = PyGILState_Ensure();
Py_XDECREF(object);
+ PyEval_ReleaseLock();
#endif
}
void
-PyObjectContainer::set(PyObject* new_object) {
+PyObjectContainer::set(PyObject* new_object)
+{
+#if defined(INCREMENT_PYTHON_REFERENCE_COUNTER) ||
defined(DECREMENT_PYTHON_REFERENCE_COUNTER)
+ PyGILState_STATE gstate;
+ gstate = PyGILState_Ensure();
+#endif
+#ifdef INCREMENT_PYTHON_REFERENCE_COUNTER
Py_XINCREF(new_object);
+#endif
#ifdef DECREMENT_PYTHON_REFERENCE_COUNTER
Py_XDECREF(object);
#endif
+#if defined(INCREMENT_PYTHON_REFERENCE_COUNTER) ||
defined(DECREMENT_PYTHON_REFERENCE_COUNTER)
+ PyEval_ReleaseLock();
+#endif
object = new_object;
}
-//////////////////////////////////////////////////////////////
-// PyCallback
-PyCallback::PyCallback()
- : function(NULL),
- args(NULL)
-{
-}
-
-PyCallback::PyCallback(PyObject* function, PyObject* args)
- : function(function),
- args(args)
-{
-}
-
-PyCallback::PyCallback(const PyCallback& copy)
- : function(copy.function),
- args(copy.args)
-{
-}
-
-PyCallback::~PyCallback()
-{
-}
-
-PyObject*
-PyCallback::call(PyObject* new_args)
-{
- PyObject* result;
-
- if (new_args == NULL) {
- // Use our stored args
- result = PyEval_CallObject(function(), args());
- } else {
- result = PyEval_CallObject(function(), new_args);
- }
-
- return result;
-}
-
-void
-PyCallback::callFromC(PyObjectContainer new_args)
-{
- if (new_args() == NULL)
- // Use our stored args
- callFromC2(function, args);
- else
- // Use the ones passed in
- callFromC2(function, new_args);
-}
-
-static
-void makePyCall(PyObject* function, PyObject* args)
+// This function assumes that someone has locked the GIL
+static void
+makePyCall(PyObject* function, PyObject* args)
{
// Call the python function
PyObject* result = PyEval_CallObject(function, args);
@@ -135,9 +110,10 @@
}
}
+namespace Manta {
+
void
-PyCallback::callFromC2(PyObjectContainer function,
- PyObjectContainer args)
+callFromC(PyObjectContainer function, PyObjectContainer args)
{
// Get ready to call python code
PyGILState_STATE gstate;
@@ -150,25 +126,20 @@
}
CallbackBase_0Data*
-PyCallback::createMantaTransaction(PyObject* new_args)
-{
- return Callback::create(this, &PyCallback::callFromC,
- PyObjectContainer(new_args));
-}
-
-CallbackBase_0Data*
-PyCallback::static_createMantaTransaction(PyObject* function,
- PyObject* args)
+createMantaTransaction(PyObject* function, PyObject* args)
{
- return Callback::create(PyCallback::callFromC2,
+#ifndef INCREMENT_PYTHON_REFERENCE_COUNTER
+ Py_XINCREF(function);
+ Py_XINCREF(args);
+#endif
+ return Callback::create(&callFromC,
PyObjectContainer(function),
PyObjectContainer(args));
}
void
-PyCallback::callFromC_OneShot(int a1, int a2,
- PyObjectContainer function,
- PyObjectContainer args)
+callFromC_OneShot(int a1, int a2,
+ PyObjectContainer function, PyObjectContainer args)
{
// Get ready to call python code
PyGILState_STATE gstate;
@@ -176,7 +147,6 @@
// Create a new Touple to accomodate the new args
int num_args = PyTuple_Size(args());
- // cerr << "PyCallback::callFromC_OneShot:: num_args = "<<num_args<<"\n";
PyObject* new_args = PyTuple_New(num_args + 2);
// Pack the new args to the arguments
PyTuple_SET_ITEM(new_args, 0, PyInt_FromLong(a1));
@@ -184,30 +154,29 @@
for(int i = 0; i < num_args; ++i) {
PyTuple_SET_ITEM(new_args, i+2, PyTuple_GET_ITEM(args(), i));
}
- // cerr << "new_args.size = "<<PyTuple_Size(new_args)<<"\n";
makePyCall(function(), new_args);
+#ifdef DECREMENT_PYTHON_REFERENCE_COUNTER
+ // You can't decrement this here, because it could lead to deleted
+ // SWIG proxy classes.
Py_DECREF(new_args);
+#endif
// Done with python code
PyGILState_Release(gstate);
}
CallbackBase_2Data<int, int>*
-PyCallback::static_createMantaOneShotCallback(PyObject* function,
- PyObject* args)
+createMantaOneShotCallback(PyObject* function, PyObject* args)
{
- return Callback::create(PyCallback::callFromC_OneShot,
+#ifndef INCREMENT_PYTHON_REFERENCE_COUNTER
+ Py_XINCREF(function);
+ Py_XINCREF(args);
+#endif
+ return Callback::create(&callFromC_OneShot,
PyObjectContainer(function),
PyObjectContainer(args));
}
-PyObject*
-PyCallback::setArgs(PyObject* new_args)
-{
- args.set(new_args);
-
- Py_INCREF(Py_None);
- return Py_None;
-}
+} // end namespace Manta
Modified: trunk/SwigInterface/pycallback.h
==============================================================================
--- trunk/SwigInterface/pycallback.h (original)
+++ trunk/SwigInterface/pycallback.h Tue Jul 18 17:53:20 2006
@@ -66,44 +66,21 @@
// parameters is done for us in SWIG, so you don't need to do it in
// the class itself. BTW, don't change the names of these
// parameters without making updates to pycallback.i.
- class PyCallback {
- public:
- PyCallback();
- PyCallback(PyObject* function, PyObject* args=0);
- PyCallback(const PyCallback& copy);
-
- ~PyCallback();
-
- // This is what you should call if calling directly from python
- PyObject* call(PyObject* args = 0);
-
- // 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(PyObjectContainer new_args);
- static
- void callFromC2(PyObjectContainer function,
- PyObjectContainer args);
-
- Manta::CallbackBase_0Data* createMantaTransaction(PyObject* new_args =
0);
-
- static Manta::CallbackBase_0Data*
- static_createMantaTransaction(PyObject* function, PyObject* args);
-
- static
- void callFromC_OneShot(int a1, int a2,
- PyObjectContainer function,
- PyObjectContainer args);
- static Manta::CallbackBase_2Data<int, int>*
- static_createMantaOneShotCallback(PyObject* function,
- PyObject* args);
-
- PyObject* setArgs(PyObject* new_args);
- protected:
- PyObjectContainer function;
- PyObjectContainer args;
-
- };
+#ifndef SWIG
+ // You should not call this from swigged code as it will lock the
+ // interpreter.
+ void callFromC(PyObjectContainer function, PyObjectContainer args);
+#endif
+
+ Manta::CallbackBase_0Data*
+ createMantaTransaction(PyObject* function, PyObject* args);
+
+#ifndef SWIG
+ void callFromC_OneShot(int a1, int a2,
+ PyObjectContainer function, PyObjectContainer args);
+#endif
+ Manta::CallbackBase_2Data<int, int>*
+ createMantaOneShotCallback(PyObject* function, PyObject* args);
} // end namespace Manta
Modified: trunk/SwigInterface/wxManta.py
==============================================================================
--- trunk/SwigInterface/wxManta.py (original)
+++ trunk/SwigInterface/wxManta.py Tue Jul 18 17:53:20 2006
@@ -87,8 +87,6 @@
def __init__(self, parent, sync_display, opengl_display,
updateFramerate):
wxGLCanvas.__init__(self, parent, -1, style=wx.NO_BORDER,
size=wx.Size(xres, yres) )
- self.printSize("panel", parent.GetSize())
-
self.sync_display = sync_display
self.opengl_display = opengl_display
# We bind the paint event to allow for some initialization
@@ -105,7 +103,7 @@
print "%s size = (%d, %d)" % (name, size.x, size.y)
def OnSize(self, event):
- print "event.size = %s" % event.GetSize()
+ pass
def OnPaint(self, event):
# Note that you must always generate a wxPaintDC object,
@@ -127,7 +125,6 @@
self.SwapBuffers()
def InitGL(self):
- print "mantaGLCanvas::InitGL"
self.opengl_display.init()
self.clearScreen()
@@ -239,7 +236,7 @@
newPosition = Vector(x,y,z)
cbArgs = ( newPosition, )
self.engine.addTransaction("Light Position",
-
manta_new(PyCallback.static_createMantaTransaction(spinner.light.setPosition,
cbArgs)))
+
manta_new(createMantaTransaction(spinner.light.setPosition, cbArgs)))
def OnSelectColor(self, event):
@@ -259,7 +256,7 @@
color.Blue() /255.0)
cbArgs = ( Color(rgbColor), )
self.engine.addTransaction("Light Color",
-
manta_new(PyCallback.static_createMantaTransaction(colorButton.light.setColor,
cbArgs)))
+
manta_new(createMantaTransaction(colorButton.light.setColor, cbArgs)))
class MantaFrame(wx.Frame):
def __init__(self, parent=None, title="Manta",
@@ -286,14 +283,16 @@
self.SetMenuBar(menuBar)
# Create the Main panel that will hold the renderer
- self.panel = wx.Panel(self)
+ self.panel = wx.Panel(self, size=renderSize)
############################################################
# Layout
box = wx.BoxSizer(wx.HORIZONTAL)
box.Add(self.panel, 1, wx.EXPAND)
- self.SetSizer(box)
- self.SetClientSize(renderSize)
+ self.SetSizerAndFit(box)
+ self.panel.SetMinSize((20,20))
+ self.SetMinSize(self.GetSize() - self.panel.GetSize()
+ + self.panel.GetMinSize())
#######################################################################
@@ -372,18 +371,13 @@
###########################################################################
def OnSize(self, event):
panelSize = self.panel.GetSize()
- print "Size panel to %s" % panelSize
- print "Size canvas to %s" % self.canvas.GetSize()
self.canvas.SetSize(panelSize)
cbArgs = (self.channelID, panelSize.width, panelSize.height)
self.engine.addOneShotCallback(MantaInterface.Relative, 0,
-
manta_new(PyCallback.static_createMantaOneShotCallback(self.changeResolution,
cbArgs)))
+
manta_new(createMantaOneShotCallback(self.changeResolution, cbArgs)))
event.Skip(True)
def changeResolution(self, a, b, channel, new_xres, new_yres):
- print "a = %s, b = %s" % (a, b)
- print "channel = %s" % channel
- print "new_res = (%s, %s)" % (new_xres, new_yres)
self.engine.changeResolution(channel, new_xres, new_yres, True);
###########################################################################
@@ -502,7 +496,7 @@
camera = self.engine.getCamera(channel)
cbArgs = ( trans, Camera.LookAt )
self.engine.addTransaction("rotate",
-
manta_new(PyCallback.static_createMantaTransaction(camera.transform, cbArgs)))
+
manta_new(createMantaTransaction(camera.transform, cbArgs)))
self.last_x = mouse_pos.x;
self.last_y = mouse_pos.y;
- [MANTA] r1145 - trunk/SwigInterface, bigler, 07/18/2006
Archive powered by MHonArc 2.6.16.