Skip to content
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

Closed
lambertdw mannequin opened this issue Nov 12, 2008 · 7 comments
Closed

ctypes documentation #48559

lambertdw mannequin opened this issue Nov 12, 2008 · 7 comments
Assignees
Labels
topic-ctypes type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@lambertdw
Copy link
Mannequin

lambertdw mannequin commented Nov 12, 2008

BPO 4309
Nosy @birkenfeld, @jwilk

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:

assignee = 'https://github.com/birkenfeld'
closed_at = <Date 2009-06-08.13:28:50.749>
created_at = <Date 2008-11-12.20:53:12.862>
labels = ['ctypes', 'type-crash']
title = 'ctypes documentation'
updated_at = <Date 2009-06-09.00:18:16.634>
user = 'https://bugs.python.org/LambertDW'

bugs.python.org fields:

activity = <Date 2009-06-09.00:18:16.634>
actor = 'mnewman'
assignee = 'georg.brandl'
closed = True
closed_date = <Date 2009-06-08.13:28:50.749>
closer = 'georg.brandl'
components = ['ctypes']
creation = <Date 2008-11-12.20:53:12.862>
creator = 'LambertDW'
dependencies = []
files = []
hgrepos = []
issue_num = 4309
keywords = []
message_count = 7.0
messages = ['75793', '75927', '76053', '76088', '89026', '89076', '89130']
nosy_count = 5.0
nosy_names = ['georg.brandl', 'ggenellina', 'LambertDW', 'jwilk', 'mnewman']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = None
status = 'closed'
superseder = None
type = 'crash'
url = 'https://bugs.python.org/issue4309'
versions = ['Python 3.0']

@lambertdw
Copy link
Mannequin Author

lambertdw mannequin commented Nov 12, 2008

'''
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)))

@lambertdw lambertdw mannequin added the docs Documentation in the Doc dir label Nov 12, 2008
@lambertdw lambertdw mannequin assigned birkenfeld Nov 12, 2008
@lambertdw lambertdw mannequin added the type-crash A hard crash of the interpreter, possibly with a core dump label Nov 12, 2008
@lambertdw
Copy link
Mannequin Author

lambertdw mannequin commented Nov 16, 2008

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."

@lambertdw lambertdw mannequin added topic-ctypes and removed docs Documentation in the Doc dir labels Nov 16, 2008
@lambertdw
Copy link
Mannequin Author

lambertdw mannequin commented Nov 19, 2008

Changing the string to type byte

'Works'
from ctypes import *
libc = CDLL('libc.so.6')
libc.printf(b'hello')

@lambertdw
Copy link
Mannequin Author

lambertdw mannequin commented Nov 20, 2008

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.

@mnewman
Copy link
Mannequin

mnewman mannequin commented Jun 7, 2009

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@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.

@birkenfeld
Copy link
Member

I fixed this, and a few other bytes/string issues, in r73293.

@mnewman
Copy link
Mannequin

mnewman mannequin commented Jun 9, 2009

Watch out on Line 247 of r73293:
"bytes objcet"
should be:
"bytes object"

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-ctypes type-crash A hard crash of the interpreter, possibly with a core dump
Projects
None yet
Development

No branches or pull requests

1 participant