Skip to content

ctypes stubs incomplete #475

Closed
Closed

Description

@gvanrossum

Sadly #454 leaves our code using ctypes a bunch of errors, not all of which are our fault. A few examples:

  • wintypes.pyi is empty?!
  • offset in from_buffer_copy() is optional
  • apparently c_char*16 is valid to declare a 16-byte buffer

Activity

  1. euresti commented on Aug 22, 2016

    @euresti
    Contributor

    Couple of others:

    • offset in from_buffer is optional
    • As typed from_buffer and from_buffer_copy expect _CData as first argument
    • from_buffer and from_buffer_copy return _CData instead of the class that is being created.
      (e.g.
     from ctypes import Structure, c_ulong, c_ushort
    
    class POINT(Structure):
       _fields_ = [(u"x", c_ulong),
                   (u"y", c_ulong),
                   ]
    
    p = POINT()
    
    buf = bytearray(b'\00' * 8 + b'\01' + b'\00' * 7)
    p2 = POINT.from_buffer_copy(buf)
    
    reveal_type(p)    # Point
    reveal_type(p2)  # _CData
    
    • The attributes of Structure can't be accessed. So in the above p.x yields a type error.
  2. gvanrossum commented on Aug 23, 2016

    @gvanrossum
    MemberAuthor

    Fixing the return type of from_buffer()/from_buffer_copy() would require fixing python/mypy#1212; I think I'll use Any for now. This isn't great, but that bug is pretty subtle. Most other things are doable.

  3. gvanrossum commented on Aug 24, 2016

    @gvanrossum
    MemberAuthor

    Fixing c_char*16 is also problematic, because c_char is a class! The __mul__ operation is overloaded on the metaclass. But mypy doesn't support that yet. I think that's another case of python/mypy#1267.

  4. gvanrossum commented on Aug 24, 2016

    @gvanrossum
    MemberAuthor

    @matthiaskramm @tharvik How bad would it be if I just reverted #454? There's no way I can get our internal usages of ctypes to pass without fixing some tricky mypy issues (see previous comments). Putting # type: ignore in the stub won't be possible for these -- we'd have to ignore each call site of those features.

  5. tharvik commented on Aug 24, 2016

    @tharvik
    Contributor

    wintypes.pyi is empty?!

    It is not documented and only available on Windows (which I don't have).

    Fixing c_char*16 is also problematic, because c_char is a class!

    I found a way by using (not pretty, but working)

    class _MulType(Generic[_T]):
        def __call__(self, *args: Any, **kwargs: Any) -> _T: ...
        def __mul__(self, size: int) -> _MulType[_T]: ...
    
    c_int = ...  # type: _MulType[_SimpleCData[int]]

    @gvanrossum I guess we can revert it but keep this issue open to have a remainder to do something when mypy is fixed.

  6. gvanrossum commented on Aug 24, 2016

    @gvanrossum
    MemberAuthor

    I've deleted the ctypes stub for now. After mypy 0.4.4 has been released (tomorrow?) I'll try to restore the previous version and improve it to the point where our internal code can pass with it.

  7. JukkaL commented on Aug 24, 2016

    @JukkaL
    Contributor

    There's also the option of annotation c_char with type Any. Again, not optimal, but some type checking is better than nothing.

  8. gvanrossum commented on Aug 24, 2016

    @gvanrossum
    MemberAuthor

    I think @tharvik's solution can be made to work, I just don't want to do it so close to the 0.4.4 release.

  9. JelleZijlstra commented on Apr 4, 2018

    @JelleZijlstra
    Member

    We have pretty good ctypes stubs now.

  10. added a commit that references this issue on May 25, 2025
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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions