如何在方法签名中注释指向C字符数组的指针? [英] How to annotate pointer to C character array in method signature?

查看:98
本文介绍了如何在方法签名中注释指向C字符数组的指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Python编写C库的包装.我正在尝试正确注释所有方法,因此我的IDE可以帮助我捕获错误.我被困在注释一种方法上,您能帮我找出正确的注释吗?

I am writing a wrapper for a C library in Python. I am trying to properly annotate all of the methods, so my IDE can help me catch errors. I am stuck annotating one method, can you help me figure out the proper annotation?

C库中的一种方法如下:

One of the methods in the C library works as follows:

  1. 使用一个arg:指向字符缓冲区的指针

  1. Takes one arg: pointer to a character buffer

  • 通过以下方式创建缓冲区:char_buffer = ctypes.create_string_buffer(16)

使用输出值填充char缓冲区

Populates the char buffer with the output value

  • 通过CMethod(char_buffer)
  • 完成

然后通过执行类似char_buffer.value的操作来解析缓冲区.

One then parses the buffer by doing something like char_buffer.value.

如何注释包装器方法以查找指向字符缓冲区的指针?目前,我有以下内容,但是我认为这是不正确的,因为POINTER似乎只是_ctypes.py中的一个函数.

How can I annotate the wrapper method to look for a pointer to a character buffer? Currently, I have the below, but I think this is incorrect, since POINTER seems to be just a function in _ctypes.py.

from ctypes import POINTER

def wrapped_method(char_buffer: POINTER):
    CMethod(char_buffer)

推荐答案

根据

此函数创建一个可变字符缓冲区.返回的对象是 c_char 的ctypes数组.

This function creates a mutable character buffer. The returned object is a ctypes array of c_char.

示例:

>>> import ctypes
>>>
>>> CharArr16 = ctypes.c_char * 16
>>> s = ctypes.create_string_buffer(16)
>>>
>>> isinstance(s, CharArr16)
True
>>> isinstance(s, ctypes.c_char * 15)
False
>>> isinstance(s, ctypes.c_char * 17)
False
>>>
>>> # A more general form, but it WILL FAIL for non array instances
...
>>> isinstance(s, s._type_ * s._length_)
True
>>>
>>> # A more general form that WILL WORK
...
>>> issubclass(CharArr16, ctypes.Array)
True
>>> isinstance(s, ctypes.Array)
True

这篇关于如何在方法签名中注释指向C字符数组的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆