how to pass by reference??

Spencer Doidge spencer at spencerdoidge.com
Sun Feb 3 22:32:05 EST 2002


Thanks for all your patient help, Chris. Now I have run into an apparent
contradiction:

This is a method within SerialPort_win.py from Isaac Barona's site-package,
modified by myself because his 2nd ReadFile(..) parameter was an integer,
and I want the buffer instead. I need to see what was read...

   def read(self, num=1, bfr=[0]*256):
        """Read num bytes from the serial port.

        If self.__timeout!=0 and != None and the number of read bytes is
less
        than num an exception is generated because a timeout has expired.
        If self.__timeout==0 read is non-blocking and inmediately returns
        up to num bytes that have previously been received.
        """
        overlapped=OVERLAPPED()
        overlapped.hEvent=CreateEvent(None, 0,0, None)
        hr, bfr = ReadFile(self.__handle, bfr, overlapped)
        WaitForSingleObject(overlapped.hEvent, win32event.INFINITE)
        return hr

After I have run the script that defines read(...), the other methods
defined by the script work fine, but with read(..) I get this result:

>>> tty = SerialPort("COM1",None,None,None)
>>> b = [0] * 256
>>> tty.read(1,b)
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "C:\Python22\Lib\site-packages\MyScripts\MySerialPort_win.py", line
238, in read
    hr, bfr = ReadFile(self.__handle, bfr, overlapped)
TypeError: Second param must be an integer or a buffer object

If bfr isn't a "buffer object," then what is?

Spencer Doidge




"Chris Gonnerman" <chris.gonnerman at newcenturycomputers.net> wrote in message
news:mailman.1012787645.17462.python-list at python.org...
> ----- Original Message -----
> From: "Spencer Doidge" <spencer at spencerdoidge.com>
>
>
> > I really tried to RTM first this time, but I failed.
> > Can someone give me a hint where in the doc html's it explains how to
pass
> > by reference?
> > Specifically, I want to do the equivalent of this:
>
> Briefly, you don't.  Or you do.  Mutable types (lists, dictionaries,
> objects)
> can be changed as you request, but immutable types (int, float, string)
> can't.
>
> Since Python supports returning multiple values (via tuples or lists) you
> normally don't need to do this.
>
> If you have a C function like this:
>
>     int MyCFunction(char *buffer);
>
> the Python equivalent would be called like this:
>
>     intval, stringval = MyCFunctionWrapped()
>
> or if the buffer is both input and output:
>
>     intval, stringval = MyCFunctionWrapped(stringval)
>
>
>
>





More information about the Python-list mailing list