ch3ll0v3k / pybox2d

Automatically exported from code.google.com/p/pybox2d
Other
0 stars 0 forks source link

Assertions -> Exceptions? #14

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
With a few changes to the source code and the SWIG interface, it's possible
to take Box2D's assertions and make them into Python exceptions. The
question is, however, if we really want to do that.

Exceptions will likely slow the code down (Erin Catto cited this as the
main reason for not using them), and perhaps won't really help in all
cases. After the return from the assert, you might be left with a world
that can't be used anymore. The C++ code wasn't designed with this in mind.

In any case, I turn to all of you, the users, to see what you'd like
implemented.

No patch for now, but here's the changes you'd need to make:

(b2Settings.h)

#include <Python.h>

class b2AssertException {};

#define b2Assert(A) if (!(A)) { PyErr_SetString(PyExc_AssertionError, #A);
throw b2AssertException(); }

(Box2D.i)

    %include "exception.i"
    %exception {
        try {
            $action
        } catch(b2AssertException) {
            // error already set, pass it on to python
        }
    }

Original issue reported on code.google.com by sir...@gmail.com on 31 Oct 2008 at 10:33

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Exceptions would be nice! 
I personally prefer having speed, if exceptions would sacrifice them. But would 
they?

It think the main point to know if this should be done, is profiling how much 
speed 
would be sacrificed. Maybe someone with a larger experience with Python 
exceptions 
could give his opinion...

Don't know about other people, but in my programs (pybox2d) physics almost 
don't eat 
CPU. I just don't put 10000 objects in my scenes, my games will never need so 
much, 
hehe.

I think it's also valid to point that some exceptions wouldn't run in the game 
loop 
(exceptions for creating shapes, for example), and therefore wouldn't directly 
affect 
the simulations.

Another point is that it isn't mandatory to use exceptions. If someone really 
thinks 
that exceptions would kill his app, he may just not use them... Some days ago I 
was 
thinking about putting an exception in my main loop (for another library), and 
was 
concerned about performance. I have tried it and haven't noticed any 
difference... 

Basically, I think exceptions should be made.

Original comment by msiegwarth@gmail.com on 2 Nov 2008 at 7:14

GoogleCodeExporter commented 8 years ago
This can be enabled during compilation on the latest version of pybox2d. 
Calling it
fixed for now, but not enabling it for the default installation.

Original comment by sir...@gmail.com on 25 Feb 2009 at 6:36