Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ctypes: problem with large integers #44869

Closed
abalkin opened this issue Apr 19, 2007 · 4 comments
Closed

ctypes: problem with large integers #44869

abalkin opened this issue Apr 19, 2007 · 4 comments
Assignees
Labels
extension-modules C modules in the Modules dir

Comments

@abalkin
Copy link
Member

abalkin commented Apr 19, 2007

BPO 1703952
Nosy @theller, @abalkin

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/theller'
closed_at = <Date 2007-07-13.17:51:28.000>
created_at = <Date 2007-04-19.21:51:13.000>
labels = ['extension-modules']
title = 'ctypes: problem with large integers'
updated_at = <Date 2007-07-13.17:51:28.000>
user = 'https://github.com/abalkin'

bugs.python.org fields:

activity = <Date 2007-07-13.17:51:28.000>
actor = 'theller'
assignee = 'theller'
closed = True
closed_date = None
closer = None
components = ['Extension Modules']
creation = <Date 2007-04-19.21:51:13.000>
creator = 'belopolsky'
dependencies = []
files = []
hgrepos = []
issue_num = 1703952
keywords = []
message_count = 4.0
messages = ['31837', '31838', '31839', '31840']
nosy_count = 2.0
nosy_names = ['theller', 'belopolsky']
pr_nums = []
priority = 'normal'
resolution = 'wont fix'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue1703952'
versions = ['Python 2.5']

@abalkin
Copy link
Member Author

abalkin commented Apr 19, 2007

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

@abalkin abalkin closed this as completed Apr 19, 2007
@abalkin abalkin closed this as completed Apr 19, 2007
@abalkin abalkin added the extension-modules C modules in the Modules dir label Apr 19, 2007
@abalkin abalkin added the extension-modules C modules in the Modules dir label Apr 19, 2007
@theller
Copy link

theller commented Apr 20, 2007

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).

@abalkin
Copy link
Member Author

abalkin commented Apr 20, 2007

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

@theller
Copy link

theller commented Jul 13, 2007

I must say I do not care too much about the remaining issue. To be portable between 32-bit and 64-bit platforms you should define .argtypes anyway or explicitely wrap the arguments into ctypes instances, if setting .argtypes is not possible as for printf.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-modules C modules in the Modules dir
Projects
None yet
Development

No branches or pull requests

2 participants