msg75793 - (view) |
Author: David W. Lambert (LambertDW) |
Date: 2008-11-12 20:53 |
''' http://docs.python.org/dev/3.0/library/ctypes.html Where web page says >>> printf("An int %d, a double %f\n", 1234, c_double(3.14)) Integer 1234, double 3.1400001049 31 >>> should instead read >>> printf(c_char_p("An int %d, a double %f\n"), 1234, c_double(3.14)) An int 1234, a double 3.140000 31 Although the intent of the message is clear, it is inexact with regard to "An int" and "a double". Core dump is bigger problem: Processor: Dual-Core AMD Opteron(tm) Processor 2218 Python: Python 3.0rc1+ (py3k, Nov 5 2008, 14:44:46) [GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] on linux2 core dumps by segmentation fault for all the printf examples without specifying c_char_p("string"). ''' # this program succeeds from ctypes import * libc = CDLL("libc.so.6") print(libc.printf(c_char_p("An int %d, a double %f\n"), 1234, c_double(3.14))) |
|
|
msg75927 - (view) |
Author: David W. Lambert (LambertDW) |
Date: 2008-11-16 04:47 |
Conversely, if the documentation is correct then my ctypes is flawed. "None, integers, byte strings and unicode strings are the only native Python objects that can directly be used as parameters in these function calls. None is passed as a C NULL pointer, byte strings and unicode strings are passed as pointer to the memory block that contains their data (char * or wchar_t *). Python integers are passed as the platforms default C int type, their value is masked to fit into the C type." |
|
|
msg76053 - (view) |
Author: David W. Lambert (LambertDW) |
Date: 2008-11-19 15:45 |
Changing the string to type byte 'Works' from ctypes import * libc = CDLL('libc.so.6') libc.printf(b'hello') |
|
|
msg76088 - (view) |
Author: David W. Lambert (LambertDW) |
Date: 2008-11-20 02:52 |
When patching py3k/Doc/library/ctypes.rst or ctypes module tree please consider u"World!" produces a syntax error. These wide character formats produce unintelligible output: for n in range(3,6): code = 'utf_%s'%2**n print(code) printf(b"Hello, %S\n", 'world'.encode(code)) http://mail.python.org/pipermail/python-3000/2008-November/015315.html And that early in the doc this is probably meant to be a simple, somewhat complete example. |
|
|
msg89026 - (view) |
Author: Michael Newman (mnewman) |
Date: 2009-06-07 02:24 |
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 "", line 1, in 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@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. |
|
|
msg89076 - (view) |
Author: Georg Brandl (georg.brandl) *  |
Date: 2009-06-08 13:28 |
I fixed this, and a few other bytes/string issues, in r73293. |
|
|
msg89130 - (view) |
Author: Michael Newman (mnewman) |
Date: 2009-06-09 00:18 |
Watch out on Line 247 of r73293: "bytes objcet" should be: "bytes object" |
|
|