[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

swig, python, lifetimes

From: David Glasser <glasser_at_davidglasser.net>
Date: Thu, 15 May 2008 12:52:29 -0700

So, let's say I write the following swig-py code:

def foo():
  editor = MyEditor()
  e, eb = svn.delta.make_editor(editor)
  r, rb = svn.ra.do_update2(ra, ..., e, eb, ...)
  # do stuff with r, rb

That's fine. Now let's say I do this:

def DoUpdate(editor):
  e, eb = svn.delta.make_editor(editor)
  r, rb = svn.ra.do_update2(ra, ..., e, eb, ...)
  return r, rb

def foo():
  editor = MyEditor()
  r, rb = DoUpdate(editor)
  # Do stuff with r, rb.

This can lead to segfaults. Why? Because e and eb are destroyed at
the end of DoUpdate, even though the C reporter in r and rb is holding
onto references to it.

In my particular code, I was actually wrapping r and rb in a Reporter
class anyway, so I just added "reporter._hold_refs_ = [e, eb]".
That's kind of hacky, though.

I talked to epg about this for a while. Ideally what one would want
would be for the do_update2 wrapper to do an incref on its e/eb
arguments, and for something (__del__ on r, say) to decref them. This
seems like it'll be a lot of work.

The basic issue is that a lot of our APIs have the informal
requirement that some arguments have a long enough lifetime. In C
this is implemented by judicious choice of pools.

Maybe we could add a function to svn's swig-py that would allow the
user to explicitly make objects live at a certain length? Like:

def DoUpdate(editor):
  e, eb = svn.delta.make_editor(editor)
  r, rb = svn.ra.do_update2(ra, ..., e, eb, ...)
  svn.live_as_long_as(e, r)
  svn.live_as_long_as(eb, r)
  return r, rb

where this would incref the first argument, and somehow get a
__del__-ish callback on the second argument which decrefs the first
argument?

swig experts, is this feasible?

--dave

-- 
David Glasser | glasser@davidglasser.net | http://www.davidglasser.net/
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe_at_subversion.tigris.org
For additional commands, e-mail: dev-help_at_subversion.tigris.org
Received on 2008-05-15 21:52:42 CEST

This is an archived mail posted to the Subversion Dev mailing list.