Author mnewman
Recipients LambertDW, georg.brandl, ggenellina, jwilk, mnewman
Date 2009-06-07.02:24:01
SpamBayes Score 1.11401e-09
Marked as misclassified No
Message-id <>
Regarding Section " Calling functions, continued" on:

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

>>> printf = libc.printf
>>> printf(b"Hello, %s\n", b"World!")
Hello, World!
>>> printf(c_char_p("Hello, %s\n"), c_char_p("World!"))
Hello, World!
>>> printf(b"Hello, %S\n", "World!")
Hello, World!
>>> printf(c_char_p("Hello, %S\n"), "World!")
Hello, World!
>>> printf(c_char_p("%d bottles of beer\n"), 42)
42 bottles of beer
>>> 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

Aside: For reference, here is how I started up the interactive session:
mike@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("")

Note the "printf.argtypes" method is discussed later in Section
" Specifying the required argument types (function
prototypes)", so it might be premature to use it here.
Date User Action Args
2009-06-07 02:24:05mnewmansetrecipients: + mnewman, georg.brandl, ggenellina, LambertDW, jwilk
2009-06-07 02:24:05mnewmansetmessageid: <>
2009-06-07 02:24:01mnewmanlinkissue4309 messages
2009-06-07 02:24:01mnewmancreate