[Python-3000] Ctypes as cross-interpreter C calling interface (original) (raw)
Paul Prescod paul at prescod.net
Fri Aug 11 01:45:00 CEST 2006
- Previous message: [Python-3000] Ctypes as cross-interpreter C calling interface
- Next message: [Python-3000] Ctypes as cross-interpreter C calling interface
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Sorry for the cc mistake.
I don't know enough about ctypes, but assuming I have a reason to
write an extension in C (e.g. Tkinter, which uses the Tcl/Tk API), how to I use ctypes to call things like PyDictGetItem() or PyErrSetString()?
There are two answers to your question. The simplest is that if you have a dict object called "foo" you just call 'foo["abc"]'. It's just Python. Same for the other one: you'd just call 'raise'.
Ctypes is the opposite model of the standard extension stuff. You're writing in Python so Python stuff is straightforward (just Python) and C stuff is a bit weird. So if you had to populate a Python dictionary from a C struct then it is the reading from the C struct that takes a bit of doing. The writing the Python dictionary is straightforward.
If there was a reason to call PyDict_GetItem directly (performance maybe???) then that's possible. You need to set up the function prototype (which you would probably do in a helper library) and then you just call PyDict_GetItem. CTypes would coerce the types. py_object is a native data type.
So I think it ends up looking like
from PythonConvenienceFunctions import PyDict_GetItem
obj = {} key = "Guido"
rc = PyDict_GetItem(obj, key)
I'm sure an expert will correct me if I'm wrong...
Paul Prescod -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/python-3000/attachments/20060810/d4b50a3e/attachment.html
- Previous message: [Python-3000] Ctypes as cross-interpreter C calling interface
- Next message: [Python-3000] Ctypes as cross-interpreter C calling interface
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]