将docstring添加到使用ctypes从dll提取的python函数中

将docstring添加到使用ctypes从dll提取的python函数中,python,dll,ctypes,docstring,Python,Dll,Ctypes,Docstring,我正在尝试从windows dll创建一组python函数,并希望将docstring附加到每个新函数 我当前的代码: import ctypes lib=ctypes.WinDLL('example.dll') VCS_OpenDevice=lib['VCS_OpenDevice'] VCS_OpenDevice.restype=ctypes.c_double 当我在解释器中运行此脚本并尝试使用该函数时,我会收到“no docstring”消息 我想不出要在那里添加什么。感谢您的帮助 分

我正在尝试从windows dll创建一组python函数,并希望将docstring附加到每个新函数

我当前的代码:

import ctypes

lib=ctypes.WinDLL('example.dll')

VCS_OpenDevice=lib['VCS_OpenDevice']
VCS_OpenDevice.restype=ctypes.c_double
当我在解释器中运行此脚本并尝试使用该函数时,我会收到“no docstring”消息

我想不出要在那里添加什么。感谢您的帮助


分配给
ctypes
函数指针的
\uuuu doc\uuuu
属性:

VCS_OpenDevice.__doc__ = 'My docstring'
\uuuu doc\uuu
是Python对象的docstring属性,而
ctypes
对象允许您写入此属性

强制性的libc演示(我使用Mac,而不是Windows,但原理相同):

IPython似乎能够很好地理解这一点:

In [4]: libc.time?
Type:       _FuncPtr
String Form:<_FuncPtr object at 0x1054b8ef0>
File:       /opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ctypes/__init__.py
Docstring:  time -- get time of day
[4]中的
:libc.time?
类型:_FuncPtr
字符串形式:
文件:/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ctypes/\uuuu init\uuuuu.py
Docstring:time——获取一天中的时间

分配给
ctypes
函数指针的
\uuuu doc\uuuu
属性:

VCS_OpenDevice.__doc__ = 'My docstring'
\uuuu doc\uuu
是Python对象的docstring属性,而
ctypes
对象允许您写入此属性

强制性的libc演示(我使用Mac,而不是Windows,但原理相同):

IPython似乎能够很好地理解这一点:

In [4]: libc.time?
Type:       _FuncPtr
String Form:<_FuncPtr object at 0x1054b8ef0>
File:       /opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ctypes/__init__.py
Docstring:  time -- get time of day
[4]中的
:libc.time?
类型:_FuncPtr
字符串形式:
文件:/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ctypes/\uuuu init\uuuuu.py
Docstring:time——获取一天中的时间
但我想使用help()

因此,我没有为生活的不公平而大发雷霆,而是创造了帮助和支持 模块,该模块在导入时将允许您使用有关文档化ctypes函数和结构的帮助。 它还允许您记录额外的内容,如ctypes函数参数的名称,它们也将显示出来

如果您正在制作Python绑定,那么请包含help_support.py并导入它,以允许像我这样的穷用户使用您的库更快地开发

真正的文件是痛苦的。。。我对原始的Python控制台很满意

在做了一件显而易见的事情之后,通过从C扩展向我的函数添加doc字符串,在help()中看不到任何结果,并且在internet上找不到解决方案,我无法忍受这种情况。 给你。现在可以对从DLL中提取的函数使用help()(至少在Python 2中是这样)

下面是一个例子:

# examp_module:
import ctypes
import ctypes.util

import help_support
del help_support # If you want you can remove it now
                 # to avoid cluttering your globals() namespace.
                 # Once it is called you do not usually need it any more.

l = ctypes.CDLL(ctypes.util.find_library("c"))

# Pull the time() function from libc,
# declare and document it:
time = l.time
time.argtypes = []
#time.argnames = ["c_void"] # The function takes no arguments, but you can trick help_support 
                            # to show something in parenthesis if you want to be consistent with C
                            # If there is/are argument(s) you should put its/their name(s) in "argnames".
time.restype = ctypes.c_int
time.__doc__ = "Function that returns a system time in seconds."
-------------------------------------------
>>> # Usage:
>>> import examp_module
>>> help(examp_module)
>>> help(examp_module.time)
>>>
但是我想使用帮助()

因此,我没有为生活的不公平而大发雷霆,而是创造了帮助和支持 模块,该模块在导入时将允许您使用有关文档化ctypes函数和结构的帮助。 它还允许您记录额外的内容,如ctypes函数参数的名称,它们也将显示出来

如果您正在制作Python绑定,那么请包含help_support.py并导入它,以允许像我这样的穷用户使用您的库更快地开发

真正的文件是痛苦的。。。我对原始的Python控制台很满意

在做了一件显而易见的事情之后,通过从C扩展向我的函数添加doc字符串,在help()中看不到任何结果,并且在internet上找不到解决方案,我无法忍受这种情况。 给你。现在可以对从DLL中提取的函数使用help()(至少在Python 2中是这样)

下面是一个例子:

# examp_module:
import ctypes
import ctypes.util

import help_support
del help_support # If you want you can remove it now
                 # to avoid cluttering your globals() namespace.
                 # Once it is called you do not usually need it any more.

l = ctypes.CDLL(ctypes.util.find_library("c"))

# Pull the time() function from libc,
# declare and document it:
time = l.time
time.argtypes = []
#time.argnames = ["c_void"] # The function takes no arguments, but you can trick help_support 
                            # to show something in parenthesis if you want to be consistent with C
                            # If there is/are argument(s) you should put its/their name(s) in "argnames".
time.restype = ctypes.c_int
time.__doc__ = "Function that returns a system time in seconds."
-------------------------------------------
>>> # Usage:
>>> import examp_module
>>> help(examp_module)
>>> help(examp_module.time)
>>>

虽然您可以分配
\uuuu doc\uuuu
,但当您执行
帮助(…)
@DavidHeffernan:Bah,humbug,它也不会显示您分配给该属性的内容。不过,IPython会使用它吗?和
义务。
因为缩写是必须的,不是吗?;-)嗯,我对奥比格没有异议。但是,不是每个人都能说一口流利的英语。有时候连我都不知道!我不确定IPython GUI。PythonWin也会选择它+1、打得好,先生。我想
help()
很特别@DavidHeffernan:该对象是一个python类(尽管它继承了
CFuncPtr
C-defined类型),因此设置应该允许在其上设置
\uuuu doc\uuuu
的想法并不太牵强虽然您可以分配
\uuuu doc\uuuu
,但当您执行
帮助(…)
@DavidHeffernan:Bah,humbug,它也不会显示您分配给该属性的内容。不过,IPython会使用它吗?和
义务。
因为缩写是必须的,不是吗?;-)嗯,我对奥比格没有异议。但是,不是每个人都能说一口流利的英语。有时候连我都不知道!我不确定IPython GUI。PythonWin也会选择它+1、打得好,先生。我想
help()
很特别@DavidHeffernan:该对象是一个python类(尽管它继承了
CFuncPtr
C-defined类型),因此设置应该允许在其上设置
\uuuu doc\uuuu
的想法并不太牵强您的代码导入一个函数,然后立即用
ctypes.c\u double
!>>VCS_OpenDevice=lib['VCS_OpenDevice']>>>VCS_OpenDevice=ctypes.c_double>>>打印VCS_OpenDevice您的代码导入一个函数,但随后立即将其替换为
ctypes.c_double
!>>VCS\u OpenDevice=lib['VCS\u OpenDevice']>>VCS\u OpenDevice=ctypes.c\u double>>>打印VCS\u OpenDevice