[capi-sig] C functions that modify their arguments.

M.-A. Lemburg mal at egenix.com
Tue Feb 28 16:34:41 CET 2012


Wendell Nichols wrote:
> I'm code generating c code based on the library's headers using python itself.  half the point of
> this exercise was to learn python :)
> Turns out that I can detect arguments that are to be modified and return them as the second item in
> a tuple.
> so:
> int = func(&tobechanged,char*,char*)
> 
> Can become a python signature like
> 
> (int,int) = func(str,str)

Right, that's the standard Python approach to output variables. If you
have in/out variables, you'd use:

(int,int) = func(int,str,str)

> If this works as well for all the functions as it has for the first few then I'm off to the races. 
> The argument that gets modified internally by the lib all have a particular naming convention, and
> are always the first arg, and are always pointers to void*.  That makes detecting them easy and
> changing the generated code equally as easy.
> 
> Thanks for all your interest and assistance
> Wendell Nichols
> 
> 
> On 12-02-28 07:59 AM, M.-A. Lemburg wrote:
>> Wendell Nichols wrote:
>>> I have a library that I want to wrap for Python (as part of an exercise to learn Python, and provide
>>> some public good at the same time :)  The library is proprietary and I can't change it.  Many of its
>>> functions modify a pointer provided via an address arg.  Can python deal with functions that modify
>>> their arguments?  If so (generally) how?
>>>
>>> If there is no formalised way I'm may have to consider registering some sort of callback to alter
>>> the argument from within C... kind of awkward but since I'm code generating all the C so long as I
>>> have a workable approach it doesn't matter how much code it takes :)
>> You mean: The C functions write data into buffers your Python wrapper
>> has to provide ?
>>
>> That's possible, but you'll have to manage memory allocation in your
>> wrapper. Alternatively, you could copy the data into Python objects
>> to have Python take over memory management.
>>
>> PS: You might want to consider using a wrapper generator such as SWIG,
>> Boost or sip to help you with the wrapping of the library. They typically
>> take care of all this.
>>

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Feb 28 2012)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________
2012-02-13: Released eGenix pyOpenSSL 0.13        http://egenix.com/go26
2012-02-09: Released mxODBC.Zope.DA 2.0.2         http://egenix.com/go25
2012-02-06: Released eGenix mx Base 3.2.3         http://egenix.com/go24

::: Try our new mxODBC.Connect Python Database Interface for free ! ::::


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/


More information about the capi-sig mailing list