win32file.AllocateReadBuffer

Robert Brewer fumanchu at amor.org
Fri Jun 11 22:38:38 CEST 2004


Still hacking pygarmin (eTrex GPS wrapper). Can anyone help explain the following snippet:

   def read(self, n):
      buffer = win32file.AllocateReadBuffer(n)
      rc, data = win32file.ReadFile(self.f, buffer)
##      if len(data) != n:
##         raise LinkException, "time out";
      return data

I had some problems reading from a GPS device until I commented out those two lines. Can anyone explain 1) why they're there at all and/or 2) potential side-effects I'm going to run into by commenting them out? Everything I've read seems to indicate that the AllocateReadBuffer/ReadFile pair blocks--so under what circumstances will n != len(data)?

OR, should I instead check the value of rc for errors like this:


      try:
        hr, data = win32file.ReadFile(srcHandle, buffer)
        if hr == winerror.ERROR_HANDLE_EOF:
          break
      except pywintypes.error, e:
        if e.args[0] == winerror.ERROR_BROKEN_PIPE:      
          break
        else:
          raise e

OR, should I go with my "other rewrite", which doesn't pre-alloc:

        data = ""
        while 1:
            rc, chars = win32file.ReadFile(self.f, n, None)
            data += chars
            if len(data) >= n:
                break
            if len(chars) == 0:
                raise LinkException, "timeout"
        return data


Not a windows guru by any means,

Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org




More information about the Python-list mailing list