Re: [ctypes-users] wrapping api calls with decorators
Brought to you by:
theller
From: Thomas H. <th...@py...> - 2005-01-10 14:56:17
|
Niki Spahiev <ni...@vi...> writes: > Thomas Heller wrote: >> Then, it inspects the original function. If there's no function body >> (empty function bodies always have a 4-byte byte code), it creates a >> code string containing a function body which simply contains >> 'return _api_(<functions arguments>)'. >> Then, the byte code of the function is patched: LOAD_GLOBAL <_api_> >> opcodes are replaced by 'LOAD_CONST <exported_function>' opcodes. >> This uses a variant of Raymond Hettinger's cookbook recipe >> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/277940 >> This approach is somewhat hacky, but I were not able to find a better >> approach. If there are better ideas, please let me know. > > Why not use nested scopes for _api_ resolution? Because there is no nested scope. Anyway, having posted the above and having thought over it the weekend, I should probably drop the magic at all, and assign the native callable to a function attribute. So the example becomes: @ stdcall(DWORD, "kernel32", [HMODULE, POINTER(c_char), c_ulong]) def GetModuleFileNameA(handle=0): # 0 means current module buf = create_string_buffer(256) if 0 == GetModuleFileNameA._api_(handle, buf, sizeof(buf)): raise WinError() return buf.value Thomas |