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

Add support for ssize_t #50978

Closed
nikratio mannequin opened this issue Aug 18, 2009 · 7 comments
Closed

Add support for ssize_t #50978

nikratio mannequin opened this issue Aug 18, 2009 · 7 comments
Assignees
Labels
topic-ctypes type-feature A feature request or enhancement

Comments

@nikratio
Copy link
Mannequin

nikratio mannequin commented Aug 18, 2009

BPO 6729
Nosy @theller, @gpshead
Files
  • issue6729.patch: Adds C type ssize_t to ctypes.
  • 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/gpshead'
    closed_at = <Date 2010-03-01.04:56:34.292>
    created_at = <Date 2009-08-18.20:16:39.353>
    labels = ['ctypes', 'type-feature']
    title = 'Add support for ssize_t'
    updated_at = <Date 2010-03-01.04:56:34.290>
    user = 'https://bugs.python.org/nikratio'

    bugs.python.org fields:

    activity = <Date 2010-03-01.04:56:34.290>
    actor = 'gregory.p.smith'
    assignee = 'gregory.p.smith'
    closed = True
    closed_date = <Date 2010-03-01.04:56:34.292>
    closer = 'gregory.p.smith'
    components = ['ctypes']
    creation = <Date 2009-08-18.20:16:39.353>
    creator = 'nikratio'
    dependencies = []
    files = ['16405']
    hgrepos = []
    issue_num = 6729
    keywords = ['patch']
    message_count = 7.0
    messages = ['91713', '92763', '92775', '92843', '93246', '100238', '100246']
    nosy_count = 5.0
    nosy_names = ['theller', 'gregory.p.smith', 'rcoyner', 'nikratio', 'robotify']
    pr_nums = []
    priority = 'normal'
    resolution = 'accepted'
    stage = None
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue6729'
    versions = ['Python 2.7', 'Python 3.2']

    @nikratio
    Copy link
    Mannequin Author

    nikratio mannequin commented Aug 18, 2009

    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.

    @nikratio nikratio mannequin assigned theller Aug 18, 2009
    @nikratio nikratio mannequin added topic-ctypes type-feature A feature request or enhancement labels Aug 18, 2009
    @theller
    Copy link

    theller commented Sep 17, 2009

    Would you like to work on a patch?

    @nikratio
    Copy link
    Mannequin Author

    nikratio mannequin commented Sep 17, 2009

    I can give it a shot if you give me a rough idea where I have to make
    the appropriate changes.

    @theller
    Copy link

    theller commented Sep 18, 2009

    Find where c_size_t is defined: in Lib/ctypes/init.py

    @nikratio
    Copy link
    Mannequin Author

    nikratio mannequin commented Sep 28, 2009

    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?

    @rcoyner
    Copy link
    Mannequin

    rcoyner mannequin commented Mar 1, 2010

    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

    @gpshead
    Copy link
    Member

    gpshead commented Mar 1, 2010

    committed in r78544.

    @gpshead gpshead closed this as completed Mar 1, 2010
    @gpshead gpshead assigned gpshead and unassigned theller Mar 1, 2010
    @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
    topic-ctypes type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants