win32file.AllocateReadBuffer

R

Robert Brewer

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
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top