New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ctypes documentation #48559
Comments
'''
>>> 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
''' # 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))) |
Conversely, if the documentation is correct then my ctypes is flawed. "None, integers, byte strings and unicode strings are the only native |
Changing the string to type byte 'Works' |
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
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, |
Regarding Section "15.15.1.5. 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!
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@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 |
I fixed this, and a few other bytes/string issues, in r73293. |
Watch out on Line 247 of r73293: |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: