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

Re: Help with Python SWIG bindings and callbacks

From: Lawrence Stewart <lstewart_at_room52.net>
Date: Tue, 26 Aug 2008 11:18:55 +1000

David James wrote:
> On Sun, Aug 24, 2008 at 6:25 PM, Lawrence Stewart <lstewart_at_room52.net> wrote:
>> Lawrence Stewart wrote:
>>> Lawrence Stewart wrote:
>>>> Hi all,
>>>>
>>>> I'm playing with the Python SWIG bindings for SVN 1.5.1 and have run into
>>>> a brick wall. I want to update a working copy from a python script, and have
>>>> a python function callback executed for each item updated. The python code
>>>> below hopefully illustrates what I'm trying to do.
>>>>
>>>> def update_callback(notify, pool):
>>>> print "in callback"
>>>>
>>>> def update(wc, rev):
>>>> ctx = svn.client.svn_client_create_context()
>>>> ctx.notify_func2 = update_callback
>>>> r = svn.core.svn_opt_revision_t()
>>>> r.kind = svn.core.svn_opt_revision_head
>>>> result_rev = svn.client.svn_client_update(wc, r, True, ctx)
>>>> print "result_rev: %d" % result_rev
>>>>
>>>> The update function does work and my working copy does get updated the
>>>> HEAD, but my callback is never executed.
>>>>
>>>> I'm basing this code on the statement in
>>>> http://svn.collab.net/svn-doxygen/group__Update.html that says if
>>>> ctx->notify_func2 is not NULL it will be called for each item updated. This
>>>> of course may not work or be applicable in Python land... someone please
>>>> tell me if this is the case.
>>>>
>>>> I've tried countless variations of the above code and changing parameters
>>>> to the callback, changing the way the callback is assigned to the context
>>>> object (I tried following the way it was done in the client.py unit test
>>>> file for a different callback type that is used) etc. etc. without success.
>>>>
>>>> I suspect my problem is that I just don't grok the translation that SWIG
>>>> is doing from the C API up into Python and therein lies the problem.
>>>>
>>>> Any help in understanding what I'm doing wrong or how I can achieve
>>>> something equivalent would be greatly appreciated.
>>>
>>> Quick follow up... I just stumbled across this thread that describes what
>>> I'm trying to do.
>>>
>>> http://osdir.com/ml/programming.swig/2003-06/msg00058.html
>>>
>>> It provided the missing clue I needed to make this work.
>>>
>>> Apologies for the noise.
>>
>> Ok, so I can now get the callback to fire, but the arguments passed to it
>> look like random pointer junk and python coredumps somewhere within the
>> svn.client.svn_client_update3() call after having executed the update and a
>> few callbacks. Here's the new code:
>>
>>
>>
>> def update_callback(path, action, kind, mime_type, content_state,
>> prop_state, revision):
>>
>> print "callback\taction: %d\trev: %u" % (action,revision)
>>
>>
>>
>> def update(wc, rev):
>>
>> ctx = svn.client.svn_client_ctx_t()
>>
>> #ctx.notify_func = svn.client.svn_swig_py_notify_func
>> #ctx.notify_baton = update_callback
>>
>> ctx.notify_func2 = svn.client.svn_swig_py_notify_func
>> ctx.notify_baton2 = update_callback
>>
>> r = svn.core.svn_opt_revision_t()
>> r.kind = svn.core.svn_opt_revision_head
>>
>> try:
>>
>> result_revs = svn.client.svn_client_update3([wc], r,
>> svn.core.svn_depth_infinity, False, True, True, ctx)
>> for result_rev in result_revs:
>> print "result_rev: %d" % result_rev
>> print "done"
>>
>> except svn.core.SubversionException, e:
>> sys.stderr.write("Error (%d): %s\n" % (e.apr_err, e.message))
>> except Exception, e:
>> print "Unknown exception"
>> print e
>>
>>
>> I've also added some debug printfs to the underlying C functions
>> svn_swig_py_notify_func() and svn_swig_py_notify_func2() in swigutil_py.c to
>> check which function is being called and if the arguments coming into the C
>> function are the same as what is being reported by python.
>>
>
> Hi Lawrence,
>
> Have you considered trying out the new ctypes python bindings? I tried
> converting your script to use the new ctypes python bindings, and it
> seems to work fine. See the example below.
>
> from csvn.core import *
> import csvn.wc
> svn_cmdline_init("", csvn.core.stderr)
> wc = csvn.wc.WC("wc")
>
> def notify_func(obj):
> if obj.action == svn_wc_notify_update_update:
> print "U", obj.path
> elif obj.action == svn_wc_notify_update_completed:
> print "Completed", obj.path
>
> wc.checkout("http://svn.collab.net/repos/svn/trunk/www/")
> wc.update(revnum=32695)
> wc.set_notify_func(notify_func)
> wc.update(revnum=32696)
>
> The above example works on my machine, and outputs the following:
> U wc/faq.ja.html
> U wc
> Completed wc
>
> The ctypes python bindings are available at
> http://svn.collab.net/repos/svn/branches/ctypes-python-bindings/, and
> should work fine with Subversion 1.5.x. Hopefully they will be
> standard in Subversion 1.6.0, but we are still working on a few
> finishing touches.

No I haven't considered ctypes (wasn't even aware of it). Thanks for the
pointer. Is there other documentation than the README in the top level
branch dir that I should be referring to?

Is it anticipated that the ctypes interface will usurp the SWIG code? I
had kind of hoped to create a script that was backward compatible with
vanilla SVN back to 1.4.x hence the use of the SWIG API and not
something like PySVN, but I guess it's not disastrous if that's not the
case.

As a bit of background, I'm trying to create a script that allows me to
mirror a portion of a remote repo into my own development repo as a
vendor branch. The existing tools just don't seem to cut it for this use
case and hacking the svn C code seemed a bit extreme to get something
working relatively quickly. Given the trouble I'm having with the
bindings perhaps I should have just dived straight into writing a small
C app. I'm willing to be told otherwise if someone here has/knows such a
tool in which case my need for python bindings disappears :)

Cheers,
Lawrence

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe_at_subversion.tigris.org
For additional commands, e-mail: dev-help_at_subversion.tigris.org
Received on 2008-08-26 03:19:44 CEST

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

This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.