Pergunta

I'm trying to create a bunch of python functions from a windows dll, and would like to have docstrings attached to each new function.

My current code:

import ctypes

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

VCS_OpenDevice=lib['VCS_OpenDevice']
VCS_OpenDevice.restype=ctypes.c_double

When i run this script in my interpreter and try to use the function, I get the 'no docstring' message.

Can't figure out to add something there. Any help is appreciated.

enter image description here

Foi útil?

Solução

Assign to the __doc__ attribute of ctypes function pointers:

VCS_OpenDevice.__doc__ = 'My docstring'

__doc__ is the docstring attribute of Python objects, and ctypes objects allow you to write to this attribute.

Obligatory libc demonstration (I use Mac, not Windows, but the principle is the same):

>>> from ctypes import *
>>> libc = cdll.LoadLibrary("libc.dylib") 
>>> libc.time.__doc__ = 'time -- get time of day'
>>> print libc.time.__doc__
time -- get time of day

IPython seems to be able to pick that up just fine:

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

Outras dicas

BUT I WANT TO USE help()!!!!!

So instead of wining about the unfairness of life I created the help_support module that, when imported, will allow you to use help on documented ctypes functions and structures. It also allows you to document extra stuff like names of ctypes function arguments and they will be displayed too.

https://pypi.org/project/help-support/0.2/

If you ar making Python bindings then include help_support.py and import it to allow poor soles like me to develop faster with your lib.

Real documentation is pain in the A... and I am happy with original Python console.

After doing the obvious thing by adding the doc string to my functions from a C extension, seeing no result in help() and finding no solution on the internet I just couldn't bare such a situation. So here you are. Now you can use help() on functions pulled from a DLL (in Python 2 at least).

Here's an example:

# 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)
>>>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top