Skip to content

Wrong size of c_ulong/c_ulonglong in Linux #108907

Description

@dudekw

Bug report

Bug description:

Attribute _type_ of c_ulong returns L, which is correct following the format characters documentation; however, the documentation says it has 4 bytes size, but sizeof(c_ulong) returns 8 bytes.

>>> sizeof(c_ulong)
8
>>> c_ulong._type_
'L'

Also c_ulonglong is automatically casted to c_ulong:

t5=c_ulonglong(200000000000000000000000000000000000)
>>> type(t5)
<class 'ctypes.c_ulong'>

, thus if c_ulong will have 4 bytes, will be there available a type of 8 bytes size?

CPython versions tested on:

3.8

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Sep 5, 2023
  2. serhiy-storchaka commented on Sep 5, 2023

    @serhiy-storchaka
    Member

    The ‘Standard size’ column refers to the size of the packed value in bytes when using standard size; that is, when the format string starts with one of '<', '>', '!' or '='. When using native size, the size of the packed value is platform-dependent.

  3. terryjreedy commented on Sep 5, 2023

    @terryjreedy
    Member

    _type_ is apparently an attribute of each fundamental data type but unlike value, not of the base class _SimpleCData. Hence it is undocumented in this section. I think it should be, with a mention that the 'types' are 1 char strings from struct Format Characters(I am assuming this) -- with 'native' rather than 'standard' size implied. (I have no idea whether the above has always been true or if type is an addition.)

    _type_ is only mentioned in Arrays and Pointers as "Specifies the type ..." with no indication as to what such specification consists of.

  4. ronaldoussoren commented on Sep 6, 2023

    @ronaldoussoren
    Contributor

    IMHO there is no bug here, other than the documentation issue mentioned by @terryjreedy.

    c_ulong and c_ulonglong are documented as being representations of the C types unsigned long and unsigned long long. As such they'll have the size of associated with those types. 64-bit Linux systems generally use a LP64 model where unsigned long and unsigned long long are both 64-bit bit integers, and hence c_ulong and c_ulonglong are the same type. On 32-bit systems unsigned long is generally a 32-bit quantity, and hence c_ulong and c_ulonglong won't. be the same type there.

  5. added a commit that references this issue on Mar 23, 2026
  6. added a commit that references this issue on Mar 23, 2026
  7. added a commit that references this issue on Mar 23, 2026
  8. encukou commented on Mar 23, 2026

    @encukou
    Member

    Docs are updated.

    the 'types' are 1 char strings from struct Format Characters(I am assuming this) -- with 'native' rather than 'standard' size implied. (I have no idea whether the above has always been true or if type is an addition.)

    I've kept these -- and array -- separate. There are some differences, but if you're reading about one individual module it's better to get the full table than “X but with Y rather than Z”.

  9. added a commit that references this issue on Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dirtopic-ctypestype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions