[issue4309] ctypes documentation

Michael Newman report at bugs.python.org
Sun Jun 7 04:24:05 CEST 2009


Michael Newman <michael.b.newman at gmail.com> added the comment:

Regarding Section "15.15.1.5. Calling functions, continued" on:
http://docs.python.org/3.0/library/ctypes.html

I would recommend changing the first example code block to the following:

>>> printf = libc.printf
>>> printf(b"Hello, %s\n", b"World!")
Hello, World!
14
>>> printf(c_char_p("Hello, %s\n"), c_char_p("World!"))
Hello, World!
14
>>> printf(b"Hello, %S\n", "World!")
Hello, World!
14
>>> printf(c_char_p("Hello, %S\n"), "World!")
Hello, World!
14
>>> printf(c_char_p("%d bottles of beer\n"), 42)
42 bottles of beer
19
>>> printf(c_char_p("%f bottles of beer\n"), 42.5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ctypes.ArgumentError: argument 2: <class 'TypeError'>: Don't know how to
convert parameter 2

And change the second example block to:

>>> printf(c_char_p("An int %d, a double %f\n"), 1234, c_double(3.14))
An int 1234, a double 3.140000
31

Aside: For reference, here is how I started up the interactive session:
mike at www:~$ python3.0
Python 3.0.1 (r301:69556, Jun  6 2009, 21:34:43)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import *
>>> libc = CDLL("libc.so.6")

Note the "printf.argtypes" method is discussed later in Section
"15.15.1.7. Specifying the required argument types (function
prototypes)", so it might be premature to use it here.

----------
nosy: +mnewman

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue4309>
_______________________________________


More information about the Python-bugs-list mailing list