This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add support for ssize_t
Type: enhancement Stage:
Components: ctypes Versions: Python 3.2, Python 2.7
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: gregory.p.smith Nosy List: gregory.p.smith, nikratio, rcoyner, robotify, theller
Priority: normal Keywords: patch

Created on 2009-08-18 20:16 by nikratio, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue6729.patch rcoyner, 2010-03-01 02:58 Adds C type ssize_t to ctypes.
Messages (7)
msg91713 - (view) Author: Nikolaus Rath (nikratio) * Date: 2009-08-18 20:16
ctypes currently has a datatype c_size_t which corresponds to size_t in
C, but there is no datatype for the C ssize_t.
msg92763 - (view) Author: Thomas Heller (theller) * (Python committer) Date: 2009-09-17 13:27
Would you like to work on a patch?
msg92775 - (view) Author: Nikolaus Rath (nikratio) * Date: 2009-09-17 15:59
I can give it a shot if you give me a rough idea where I have to make
the appropriate changes.
msg92843 - (view) Author: Thomas Heller (theller) * (Python committer) Date: 2009-09-18 20:20
Find where c_size_t is defined: in Lib/ctypes/__init__.py
msg93246 - (view) Author: Nikolaus Rath (nikratio) * Date: 2009-09-28 23:34
Ok, apparently the lines that define c_size_t are these:

if sizeof(c_uint) == sizeof(c_void_p):
    c_size_t = c_uint
elif sizeof(c_ulong) == sizeof(c_void_p):
    c_size_t = c_ulong
elif sizeof(c_ulonglong) == sizeof(c_void_p):
    c_size_t = c_ulonglong

(side remark: wouldn't a simple c_size_t = c_void_p do exactly the same
as the above and be much clearer?) 

Unfortunately I have no real idea how to come up with something similar
for ssize_t, since there is no predefined object that is of type ssize_t
(as c_void_p is for type size_t). Any ideas what to do?
msg100238 - (view) Author: Ryan Coyner (rcoyner) Date: 2010-03-01 02:58
You don't want to do c_size_t = c_void_p because that will prevent type checking. We want c_size_t to be integers; setting it to c_void_p will accept other values. The lines that define c_size_t are doing a sizeof check to determine how many bits the CPU supports, and c_size_t should represent unsigned integers [1].

On a 16-bit machine: c_size_t = c_uint
On a 32-bit machine: c_size_t = c_ulong
On a 64-bit machine: c_size_t = c_ulonglong

Now, ssize_t is like size_t, except that it is signed [2]. So if I am not mistaken, all we have to do is:

if sizeof(c_uint) == sizeof(c_void_p):
    c_size_t = c_uint
    c_ssize_t = c_int
elif sizeof(c_ulong) == sizeof(c_void_p):
    c_size_t = c_ulong
    c_ssize_t = c_long
elif sizeof(c_ulonglong) == sizeof(c_void_p):
    c_size_t = c_ulonglong
    c_ssize_t = c_longlong


Patch attached with documentation and unit test.


[1] - http://www.gnu.org/software/libc/manual/html_node/Important-Data-Types.html
[2] - http://www.gnu.org/software/libc/manual/html_node/I_002fO-Primitives.html
msg100246 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2010-03-01 04:56
committed in r78544.
History
Date User Action Args
2022-04-11 14:56:52adminsetgithub: 50978
2010-03-01 04:56:34gregory.p.smithsetstatus: open -> closed

assignee: theller -> gregory.p.smith
versions: - Python 3.0, Python 3.1
nosy: + gregory.p.smith

messages: + msg100246
resolution: accepted
2010-03-01 02:58:32rcoynersetfiles: + issue6729.patch

nosy: + rcoyner
messages: + msg100238

keywords: + patch
2009-10-25 13:29:19robotifysetnosy: + robotify
2009-09-28 23:34:03nikratiosetmessages: + msg93246
2009-09-18 20:20:52thellersetmessages: + msg92843
2009-09-17 15:59:09nikratiosetmessages: + msg92775
2009-09-17 13:27:49thellersetmessages: + msg92763
versions: + Python 3.0, Python 3.1, Python 2.7, Python 3.2, - Python 2.6
2009-08-18 20:16:39nikratiocreate