This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: ctypes documentation
Type: crash Stage:
Components: ctypes Versions: Python 3.0
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: LambertDW, georg.brandl, ggenellina, jwilk, mnewman
Priority: normal Keywords:

Created on 2008-11-12 20:53 by LambertDW, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (7)
msg75793 - (view) Author: David W. Lambert (LambertDW) Date: 2008-11-12 20:53

    Where web page says

    >>> printf("An int %d, a double %f\n", 1234, c_double(3.14))
    Integer 1234, double 3.1400001049

    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

    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("")
print(libc.printf(c_char_p("An int %d, a double %f\n"), 1234,
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

from ctypes import *
libc = CDLL('')
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 

  u"World!" produces a syntax error.

  These wide character formats produce unintelligible output:

    for n in range(3,6):
        code = 'utf_%s'%2**n
        printf(b"Hello, %S\n", 'world'.encode(code))

  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 " 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.
msg89076 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) 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"
Date User Action Args
2022-04-11 14:56:41adminsetgithub: 48559
2009-06-09 00:18:16mnewmansetmessages: + msg89130
2009-06-08 13:28:50georg.brandlsetstatus: open -> closed
resolution: fixed
messages: + msg89076
2009-06-07 02:24:01mnewmansetnosy: + mnewman
messages: + msg89026
2009-02-16 10:04:31jwilksetnosy: + jwilk
2008-11-27 19:57:50ggenellinasetnosy: + ggenellina
2008-11-20 02:52:01LambertDWsetmessages: + msg76088
2008-11-19 15:45:42LambertDWsetmessages: + msg76053
2008-11-16 04:47:33LambertDWsetmessages: + msg75927
components: + ctypes, - Documentation
2008-11-12 20:53:13LambertDWcreate