[python-win32] VT_UI1 ByRef behavior

Kevin Patterson patter001 at gmail.com
Thu May 10 14:40:01 CEST 2007


In case anyone else is interested, below is the code I ended up using to
filter buffers from the input and output of the com. It makes use of the
Python2.4 decorators that are added to all the functions in th py file
generated by makepy.

I never thought I would find a reason for the decorator, but now I'm really
glad that feature is there!!  :) The code was figured out by looking at the
argument/return type check that is shown in the decorator PEP and making
tweaks.

Hope this helps!

http://www.python.org/dev/peps/pep-0318/

def list2buffer(list):
    if type(list) == types.ListType:
        val =  buffer(array.array('B', list))
    else:
        val = list
    return val

def buffer2list(abuffer,width=1):
    """handles buffers or lists of buffers
    if you actually want the buffer broken down differently than
    a single byte, then specify the width
    """
    newlist = []
    if type(abuffer) == types.ListType:
        for l in abuffer:
            newlist.append( buffer2list(l) )
    elif type(abuffer) == types.BufferType:
        if width is 4:
            typecode = 'L'
        elif width is 2:
            typecode = 'H'
        elif width is 1:
            typecode = 'B'
        else:
            raise ValueError("Unsupported width: %d"%width)
        return array.array(typecode, str(abuffer)).tolist()
    else:
        return abuffer

def filterin(f):
    def new_f(*args, **kwds):
        newargs = []
        for i in range(len(args)):
            newargs.append( list2buffer(args[i]) )
        for k in kwds.keys():
            kwds[k] = list2buffer(args[i])
        return f(*newargs, **kwds)
    new_f.func_name = f.func_name
    return new_f

def filterout(f):
    def new_f(*args, **kwds):
        result = f(*args, **kwds)
        return buffer2list( result )
    new_f.func_name = f.func_name
    return new_f

@filterin
@filterout
def myfunction(somearg1,somarg2):
   pass


On 5/9/07, Kevin Patterson <patter001 at gmail.com> wrote:
>
> Alrighty, I'll keep looking in to it. I think it is easy to make the
> buffers get returned as a python list. Almost all the code exists to help
> with that. The tough part is telling the COM that the array being passed IN
> needs to become a Safe Array where the size of each element is only VT_UI1.
> The buffer is a great thing for that and at first glance the only thing I
> can think of would be to modify PyCom_VariantFromPyObject to check the size
> of the number to see if it will fit in VT_UI1 (it is already doing this for
> ints and longs)...but i think that would be way to dangerous...
>
> I might still look into a proposal for changing the output, but for the
> input, I can't think of how to do it any other way than what you already
> have. Perhaps I will just have to write a script that searches my generated
> file from makepy and creates a layer in the python code...instead of on the
> C side...oh well...
>
> -Kevin
>
> On 4/29/07, Mark Hammond <mhammond at skippinet.com.au> wrote:
> >
> > > I was curious is there a way to prevent an array of VT_UI1's from
> > being
> > > converted to a buffer? Or perhaps a way to modify the file generated
> > > from "makepy" to prevent the conversion?
> >
> > Nope, no such facility exists.  Feel free to propose a patch though.
> >
> > Cheers,
> >
> > Mark
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-win32/attachments/20070510/b1a0769c/attachment.htm 


More information about the Python-win32 mailing list