You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@subversion.apache.org by Douglas Waer <do...@boeing.com> on 2004/10/01 16:09:14 UTC

Python/swig bindings.

I'm trying to create some Python scripts, and I'm having some trouble
figuring out what the parameter lists to the calls should be.  For example,
in svn_client.h, svn_client_status shows 11 parameters, but the python call
only requires 9.  Is there some resource, header file, or document that I'm
just overlooking to figure out the parameters?

Doug Waer
Boeing, Mesa.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: Python/swig bindings.

Posted by Greg Stein <gs...@lyra.org>.
On Sat, Oct 02, 2004 at 12:32:04PM -0400, Greg Hudson wrote:
> On Fri, 2004-10-01 at 12:21, C. Michael Pilato wrote:
> >    3.  Callback function/baton pairs get reduced to just callback
> >        functions, and the benefit you get from batons is gotten
> >        instead through Python default arguments:
> [...]
> >               def function(callback_arg1, ..., userdata1=whatever, ...):
> 
> Is the use of default arguments really necessary?  Can't you just put
> the "whatever" in the body of function?  In my tests, an inner function
> seems to be able to refer to the outer function's local variables just
> fine; for instance, the following prints out "5":
> 
> def call(func):
>   x = 3
>   func()
> 
> def function():
>   x = 5
>   def subfunction():
>     y = x
>     print y
>   call(subfunction)
> 
> function()

That only works for newer versions of Python (2.2 and later?). They can do
lexical scoping. Older versions could not, so you had to pass outer
variables into the function through default args.

Mike's point about using default args works, but I tend to use class
instances to hold the state:

  class Foo:
    def __init__(self, state):
      self.state = state
    def callback(self, arg1):
      return arg1 + self.state

  f = Foo(10)
  register_callback(f.callback)

The "baton" is the instance itself. Through the "self" variable, I can
access any info needed.

Cheers,
-g

-- 
Greg Stein, http://www.lyra.org/

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: Python/swig bindings.

Posted by Greg Hudson <gh...@MIT.EDU>.
On Fri, 2004-10-01 at 12:21, C. Michael Pilato wrote:
>    3.  Callback function/baton pairs get reduced to just callback
>        functions, and the benefit you get from batons is gotten
>        instead through Python default arguments:
[...]
>               def function(callback_arg1, ..., userdata1=whatever, ...):

Is the use of default arguments really necessary?  Can't you just put
the "whatever" in the body of function?  In my tests, an inner function
seems to be able to refer to the outer function's local variables just
fine; for instance, the following prints out "5":

def call(func):
  x = 3
  func()

def function():
  x = 5
  def subfunction():
    y = x
    print y
  call(subfunction)

function()


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org

Re: Python/swig bindings.

Posted by "C. Michael Pilato" <cm...@collab.net>.
Douglas Waer <do...@boeing.com> writes:

> I'm trying to create some Python scripts, and I'm having some trouble
> figuring out what the parameter lists to the calls should be.  For example,
> in svn_client.h, svn_client_status shows 11 parameters, but the python call
> only requires 9.  Is there some resource, header file, or document that I'm
> just overlooking to figure out the parameters?

The argument-reductions laws of the SWIG bindings go like this:

   1.  Python functions don't return errors.  They throw exceptions.
       Which means that...

   2.  ...Python functions will return the "other" stuff that the C
       functions "return" instead.  C functions which populate
       pointers with new data (you know, values that are returned to
       the caller, but not as "return values") will return those
       values directly in Python.  So:

          error = foo (object **returned_obj, int blah);

       becomes:

          try:
              returned_obj = foo (blah)
          except:
              # handle it

   3.  Callback function/baton pairs get reduced to just callback
       functions, and the benefit you get from batons is gotten
       instead through Python default arguments:

          error = foo (callback_t function, void *baton);

       becomes:

          try:
              def function(callback_arg1, ..., userdata1=whatever, ...):
                  # do stuff here
              foo(function)
          except:
              # handle it

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org