On Wed, Aug 3, 2016 at 1:21 PM, Bill Somerville
<bsomervi...@flexerasoftware.com> wrote:
> I can extend the SWIG shadow class to do the creation of the prototype and 
> thunk, the
> required cast above and any extra attributes or methods like _as_parameter_ 
> and
> from_param() but I can't see any way of having the Python callback (my_cb) 
> magically
> receive the SWIG wrapped 'my_type' struct.

from_param is a hook method for a type that's set in a function
pointer's argtypes. It gets called to convert an argument when the
function pointer is called from Python. The return value can be a
ctypes instance, an object with a hard-coded conversion (e.g. a string
or integer), or an object that defines an _as_parameter_ attribute.

Only ctypes types are supported in callbacks, which unfortunately
isn't documented clearly. Specifically, the class dict needs to be an
extended C storage dict (i.e. StgDictObject), either to look up the
getfunc of a simple type or to ensure that instantiating a non-simple
type returns a ctypes instance (i.e. CDataObject) with a known size.
The relevant code in _CallPythonObject is as follows (when stripped of
declarations and error handling):

    cnv = PySequence_GetItem(converters, i);
    dict = PyType_stgdict(cnv);
    if (dict && dict->getfunc && !_ctypes_simple_instance(cnv)) {
        v = dict->getfunc(*pArgs, dict->size);
        PyTuple_SET_ITEM(arglist, i, v);
    } else if (dict) {
        obj = (CDataObject *)PyObject_CallFunctionObjArgs(cnv, NULL);
        memcpy(obj->b_ptr, *pArgs, dict->size);
        PyTuple_SET_ITEM(arglist, i, (PyObject *)obj);
    } else {
        PyErr_SetString(PyExc_TypeError,
                        "cannot build parameter");

I don't have much experience with SWIG. Does it provide some means to
instantiate a wrapped type from an address? If it does, then you can
use a void pointer as the callback parameter.
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to