[ python-Bugs-1703952 ] ctypes: problem with large integers

SourceForge.net noreply at sourceforge.net
Fri Apr 20 21:37:45 CEST 2007


Bugs item #1703952, was opened at 2007-04-19 17:51
Message generated for change (Comment added) made by belopolsky
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1703952&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Extension Modules
Group: Python 2.5
>Status: Open
Resolution: Invalid
Priority: 5
Private: No
Submitted By: Alexander Belopolsky (belopolsky)
Assigned to: Thomas Heller (theller)
Summary: ctypes: problem with large integers

Initial Comment:
Python 2.5 (r25:51908, Nov 24 2006, 11:03:50)
[GCC 3.4.4 20050721 (Red Hat 3.4.4-2)] on linux2
>>> from ctypes import *
>>> c_int(2**31).value
-2147483648
>>> c_long(2**32-1).value
-1

In a 64-bit build, the situation is even worse:

>>> c_int(int(2**32)).value
0
>>> c_int(2**32).value
0

Another way to see the problem:
>>> c = CDLL(None)
>>> c.printf("%d\n", 2**32)
0
2


----------------------------------------------------------------------

>Comment By: Alexander Belopolsky (belopolsky)
Date: 2007-04-20 15:37

Message:
Logged In: YES 
user_id=835142
Originator: YES

An issue remains with the implicit conversion:

On a 64-bit platform (sizeof(long)=64):

>>> c.printf("%d\n",1<<64)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ctypes.ArgumentError: argument 2: <type 'exceptions.OverflowError'>: long
int too long to convert

so it does do overflow checking, but

>>> c.printf("%d\n",(1<<64)-1)
-1
3
>>> c.printf("%d\n",(1<<32))
0
2



----------------------------------------------------------------------

Comment By: Thomas Heller (theller)
Date: 2007-04-20 14:55

Message:
Logged In: YES 
user_id=11105
Originator: NO

This works as designed.  ctypes intentionally does no overflow checking
when using the c_int, c_uint, and related integer types.  Instead, only the
available bits are used - just as in C.

Closing as invalid (sorry).

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1703952&group_id=5470


More information about the Python-bugs-list mailing list