[ctypes-commit] ctypes/source callproc.c,1.114,1.115
Brought to you by:
theller
From: Thomas H. <th...@us...> - 2004-10-29 18:46:49
|
Update of /cvsroot/ctypes/ctypes/source In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18585 Modified Files: callproc.c Log Message: Define a new protocol for function's restype. If it is a ctypes' type, it is handled in the usual way. But if it additionally has an _check_retval_ attribute, this is called with the result, and the return value of this call is the functions return value. This allows to define HRESULT as a ctypes' type, because it can check for failure in it's _check_retval_ static method, and raise a WindowsError. Although HRESULT is defined in python code (since it is derived from c_long), the _check_retval_ method is implemented in C - in this way the traceback won't include the _check_retval_ method definition itself. Index: callproc.c =================================================================== RCS file: /cvsroot/ctypes/ctypes/source/callproc.c,v retrieving revision 1.114 retrieving revision 1.115 diff -C2 -d -r1.114 -r1.115 *** callproc.c 28 Oct 2004 17:45:50 -0000 1.114 --- callproc.c 29 Oct 2004 18:46:38 -0000 1.115 *************** *** 768,772 **** Is there another way? */ ! PyObject *retval; char c; short s; --- 768,772 ---- Is there another way? */ ! PyObject *retval, *checker; char c; short s; *************** *** 798,802 **** break; } ! return retval; } if (PyCallable_Check(restype)) --- 798,812 ---- break; } ! if (retval == NULL) ! return NULL; ! checker = PyObject_GetAttrString(restype, "_check_retval_"); ! if (checker == NULL) { ! PyErr_Clear(); ! return retval; ! } else { ! PyObject *v = PyObject_CallFunctionObjArgs(checker, retval, NULL); ! Py_DECREF(retval); ! return v; ! } } if (PyCallable_Check(restype)) *************** *** 1418,1421 **** --- 1428,1432 ---- #endif + PyMethodDef module_methods[] = { {"string_at", string_at, METH_VARARGS, string_at_doc}, *************** *** 1433,1437 **** {"FreeLibrary", free_library, METH_VARARGS, free_library_doc}, {"call_commethod", call_commethod, METH_VARARGS }, ! {"HRESULT", check_hresult, METH_VARARGS}, #else {"dlopen", py_dl_open, METH_VARARGS, "dlopen a library"}, --- 1444,1448 ---- {"FreeLibrary", free_library, METH_VARARGS, free_library_doc}, {"call_commethod", call_commethod, METH_VARARGS }, ! {"_check_HRESULT", check_hresult, METH_VARARGS}, #else {"dlopen", py_dl_open, METH_VARARGS, "dlopen a library"}, |