[Pyrex] [patch] Memory leak when using __Pyx_GetStarArgs() with keyword-only arguments

Stefan Behnel stefan_ml at behnel.de
Wed Dec 19 11:54:59 CET 2007


Hi,

I recently noticed a problem with functions that use keyword-only arguments.
It looks like the arguments cause a memory leak. The problem is with the args
tuple rather than the keyword arguments, as I'm not passing any keyword
arguments when the problem occurs.

Here is a simple test .pyx:

  import gc
  gc.set_debug(gc.DEBUG_LEAK)

  def test(arg, *, kw=None):
      pass

  while 1:
      gc.collect()
      print "BEFORE", len(gc.get_objects())

      s = "test" + "test"
      test(s)

      gc.collect()
      print "AFTER ", len(gc.get_objects())

Note that the test program above does not currently compile as the generated C
code lacks the __Pyx_GetStarArgs function. The (obvious) fix is part of the patch.

When you run the program, you can see how the number of objects keeps
increasing at each step, so the string is not freed after the call. When you
change the signature to this:

  def test(arg, *args, kw=None):

the GetStarArgs function will be called, but the code will work correctly. The
difference is an additional

  Py_XDECREF(__pyx_v_args);

at the end of the generated function which is not generated in the kw-only
case. I think the code generation needs a bit of a cleanup here, to make sure
the decision if star(star) arguments are in use is taken in the same way
everywhere.

Stefan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: kw-only-fixes.patch
Type: text/x-patch
Size: 1651 bytes
Desc: not available
Url : http://lists.copyleft.no/pipermail/pyrex/attachments/20071219/918907a2/attachment.bin 


More information about the Pyrex mailing list