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 PyDict_GetItem() or
PyErr_SetString()?
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
_______________________________________________ Python-3000 mailing list [email protected] http://mail.python.org/mailman/listinfo/python-3000 Unsubscribe: http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com
