[Pythonmac-SIG] extension design question

has hengist.podd at virgin.net
Mon Nov 14 21:40:20 CET 2005


Hi all,

I'm patching and extending copies of Carbon.AE and Carbon.OSA, and looking for advice on the best way to do the following, given that my C sucks:

To allow clients to install Python functions via OSASetCreateProc(), I've defined a GenericCreateFunction() procedure similar to the GenericEventHandler() procedure used in _AEmodule.c:

static pascal OSErr 
GenericCreateFunction(AEEventClass theAEEventClass,
                                  AEEventID theAEEventID,
                                  const AEAddressDesc *target,
                                  short returnID,
                                  long transactionID,
                                  AppleEvent *result,
                                  long refcon) // refcon holds ptr to client's Python function
{
    PyObject *args, *res;
    
    args = Py_BuildValue("O&O&O&hl",
                                PyMac_BuildOSType, theAEEventClass,
                                PyMac_BuildOSType, theAEEventID,
                                AEDesc_New, target,
                                returnID,
                                transactionID);
    if (args == NULL) {
        PySys_WriteStderr("Exception in UPPWrapper function.\n");
        PyErr_Print();
        return errOSAGeneralError;
    }
    res = PyEval_CallObject((PyObject *)refcon, args);
    if (res == NULL) {
        PySys_WriteStderr("Exception in GenericCreateFunction().\n");
        PyErr_Print();
        return errOSAGeneralError;
    }
    if (!AEDesc_Check(res)) { // <---- (1)
        Py_DECREF(res);
        PySys_WriteStderr("GenericCreateFunction() didn't return an AEDesc.\n");
        return errOSAGeneralError;
    }
    if (AEDuplicateDesc(&((AEDescObject *)res)->ob_itself, result)) { // <---- (2)
        Py_DECREF(res);
        return -1;
    }
    Py_DECREF(res);
    return noErr;
}

The problem is that the two marked lines refer to AEDesc_Check() and AEDescObject which are defined in _AEmodule.c. For the typecheck (1), I'm thinking I could import AE.so using PyImport_AddModule() and use PyObject_IsInstance() to check against the AEDesc type defined there. For the cast (2), I can copy-n-paste the original AEDescObject struct from _AEmodule.c to _OSAmodule.c, but I'd really like to think there was a more elegant way to do this.

Thanks,

has
-- 
http://freespace.virgin.net/hamish.sanderson/


More information about the Pythonmac-SIG mailing list