By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,676 Members | 2,249 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,676 IT Pros & Developers. It's quick & easy.

int vs long

P: n/a

The readFile function from the win32 package aparently really expect an
integer :

def inWaiting(self):
"""Returns the number of bytes waiting to be read"""
flags, comstat = ClearCommError(self.__handle)
return comstat.cbInQue

ReadFile(h, s.inWaiting())

My code crashes because inWaiting returns a long, not an int

Why is that different on my machine and my collegues ? Have I or he
installed a wrong version of a package?
CPython 2.5.

Was not expecting int<->long type problems in excactly python language.
Is that because we are navigating so close to the win32 api that the types
are more strictly enforced ?

Thx in advance
Troels


Dec 16 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
En Sun, 16 Dec 2007 20:28:02 -0300, Troels Thomsen <"nej
tak..."@bag.python.orgescribi�:
>
The readFile function from the win32 package aparently really expect an
integer :

def inWaiting(self):
"""Returns the number of bytes waiting to be read"""
flags, comstat = ClearCommError(self.__handle)
return comstat.cbInQue

ReadFile(h, s.inWaiting())

My code crashes because inWaiting returns a long, not an int
That's very strange. The cbInQue field is a DWORD in C, seen as an int in
Python. How do you know it returns a long?
Why is that different on my machine and my collegues ? Have I or he
installed a wrong version of a package?
CPython 2.5.
And pywin32 build 210, I presume.
Was not expecting int<->long type problems in excactly python language.
Is that because we are navigating so close to the win32 api that the
types
are more strictly enforced ?
Somewhat. At the API level, function arguments have to be converted to
native C types, like ReadFile expecting a DWORD. Any number greater than
2**32 won't fit, but I can't think how such thing could happen looking at
those few posted code lines.

--
Gabriel Genellina

Dec 17 '07 #2

P: n/a
Gabriel Genellina <ga*******@yahoo.com.arwrote:
En Sun, 16 Dec 2007 20:28:02 -0300, Troels Thomsen <"nej
tak..."@bag.python.orgescribi?:

The readFile function from the win32 package aparently really expect an
integer :

def inWaiting(self):
"""Returns the number of bytes waiting to be read"""
flags, comstat = ClearCommError(self.__handle)
return comstat.cbInQue

ReadFile(h, s.inWaiting())

My code crashes because inWaiting returns a long, not an int

That's very strange. The cbInQue field is a DWORD in C, seen as an int in
Python. How do you know it returns a long?
Why is that different on my machine and my collegues ? Have I or he
installed a wrong version of a package?
CPython 2.5.

And pywin32 build 210, I presume.
Was not expecting int<->long type problems in excactly python language.
Is that because we are navigating so close to the win32 api that the
types
are more strictly enforced ?

Somewhat. At the API level, function arguments have to be converted to
native C types, like ReadFile expecting a DWORD. Any number greater than
2**32 won't fit, but I can't think how such thing could happen looking at
those few posted code lines.
Actually any number >= 2**31 won't fit in a python int.
>>2**31
2147483648L

According to my headers DWORD is defined like this

typedef unsigned long DWORD;

So you might see longs returned when you expected ints if the result
was >= 0x8000000.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Dec 17 '07 #3

P: n/a
En Mon, 17 Dec 2007 10:30:05 -0300, Nick Craig-Wood <ni**@craig-wood.com>
escribió:
Gabriel Genellina <ga*******@yahoo.com.arwrote:
> En Sun, 16 Dec 2007 20:28:02 -0300, Troels Thomsen <"nej
tak..."@bag.python.orgescribi?:
>
The readFile function from the win32 package aparently really expect
an
integer :

def inWaiting(self):
"""Returns the number of bytes waiting to be read"""
flags, comstat = ClearCommError(self.__handle)
return comstat.cbInQue

ReadFile(h, s.inWaiting())

My code crashes because inWaiting returns a long, not an int

That's very strange. The cbInQue field is a DWORD in C, seen as an int
in
Python. How do you know it returns a long?
Why is that different on my machine and my collegues ? Have I or he
installed a wrong version of a package?
CPython 2.5.

And pywin32 build 210, I presume.
Was not expecting int<->long type problems in excactly python
language.
Is that because we are navigating so close to the win32 api that the
types
are more strictly enforced ?

Somewhat. At the API level, function arguments have to be converted to
native C types, like ReadFile expecting a DWORD. Any number greater
than
2**32 won't fit, but I can't think how such thing could happen looking
at
those few posted code lines.

Actually any number >= 2**31 won't fit in a python int.
>>2**31
2147483648L

According to my headers DWORD is defined like this

typedef unsigned long DWORD;

So you might see longs returned when you expected ints if the result
was >= 0x8000000.
More than 2GB waiting to be read from a serial port? If that were the case
the OP has a very big throughput problem rather than an API mismatch :)

--
Gabriel Genellina

Dec 18 '07 #4

P: n/a
"Nick Craig-Wood" <nic...od.comwrote:
So you might see longs returned when you expected ints if the result
was >= 0x8000000.
did you mean 0x80000000 ?

;-)

- Hendrik

Dec 18 '07 #5

P: n/a
Hendrik van Rooyen <ma**@microcorp.co.zawrote:
"Nick Craig-Wood" <nic...od.comwrote:
So you might see longs returned when you expected ints if the result
was >= 0x8000000.

did you mean 0x80000000 ?

;-)
Yes - well spotted!
--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Dec 20 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.