[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] The gsoc physics module, bug fixing?



Hi Zhang and others!

Is it too early to test for bugs? I don't know what method you are using for 
development, mine is to test every new thing I write and make sure it works 
before I proceed. Depending on your aproach you may not want bug reports 
already?

Anyway, after calling world.update(1.0) any following function call will 
yield this error:

  TypeError: a float is required
  Fatal Python error: (pygame parachute) Segmentation Fault
  Aborted

Unfortunately, Debian testing has some serious issues with it's python-dbg 
package on 64-bit, it thinks it's missing Py_InitModule4_64, so I cannot 
debug further using gdb, but I know this error from before.

The problem line is in pgWorldObject.c:

  static PyObject* _world_update(pgWorldObject* world,PyObject* pyfloat)
  {
    double dt = PyFloat_AsDouble(pyfloat);  <-- problem line

pyfloat is tuple instance, you have METH_VARARGS there which indicates that 
the second argument will be a tuple containing the arguments from the Python 
call. What you want though is the METH_O, just pass ONE object to the C 
call.

So:

  { "update", (PyCFunction) _world_update, METH_VARARGS, "" },

changes to

  { "update", (PyCFunction) _world_update, METH_O, "" },

and the function _world_update should look something like:

  static PyObject* _world_update(pgWorldObject* world, PyObject* pyfloat)
  {
      if (!PyFloat_Check(pyfloat)) {
          PyErr_SetString(PyExc_TypeError, "World update requires a float");
          return NULL;
      }
      double dt = PyFloat_AsDouble(pyfloat);
      PG_Update(world,dt);
      Py_RETURN_NONE;
  }

That will make a safe and correct call to update using one float. Your other 
alternative, if you want more than one argument, is to stick with 
METH_VARARGS and do do this:
  
  static PyObject* _world_update(pgWorldObject *world, PyObject *args)
  {
      PyFloatObject *farg;
      if (!PyArg_ParseTuple(args, "f", &farg))
          return NULL; // fail if the argument isn't a float

However, if you're only looking for a single float argument, it makes no 
sense for Python to first create a tuple argument list just so you can parse 
out one single argument.

I don't know if you want patches of the files, commits to svn or keep 
explaining the problems in emails like this, whatever works best and most 
efficiently for you.

I'm really looking forward to test some more!
Best regards, Peter