--- c:/Users/native_api/AppData/Local/Temp/thg.xssmi5/cpython2.5.93140/Doc/library/ctypes.rst Thu Oct 23 00:02:12 2014 +++ C:/Users/native_api/Documents/cpython2/Doc/library/ctypes.rst Wed Oct 22 23:14:08 2014 @@ -401,6 +401,10 @@ It is possible to specify the required argument types of functions exported from DLLs by setting the :attr:`argtypes` attribute. +.. warning:: + + Use caution modifying function pointers that are cached globally. See the warning at :class:`_FuncPtr` . + :attr:`argtypes` must be a sequence of C data types (the ``printf`` function is probably not a good example here, because it takes a variable number and different types of parameters depending on the format string, on the other hand @@ -444,6 +448,10 @@ return types can be specified by setting the :attr:`restype` attribute of the function object. +.. warning:: + + Use caution modifying function pointers that are cached globally. See the warning at :class:`_FuncPtr` . + Here is a more advanced example, it uses the ``strchr`` function, which expects a string pointer and a char, and returns a pointer to a string:: @@ -1443,11 +1451,15 @@ The default mode which is used to load shared libraries. On OSX 10.3, this is *RTLD_GLOBAL*, otherwise it is the same as *RTLD_LOCAL*. -Instances of these classes have no public methods. Functions exported by the -shared library can be accessed as attributes or by index. Please note that -accessing the function through an attribute caches the result and therefore -accessing it repeatedly returns the same object each time. On the other hand, -accessing it through an index returns a new object each time: +.. method:: PyDLL.__getattr__(name) + + Return a :class:`_FuncPtr` instance representing an exported function. + Caches the result, so calling this function repeatedly returns the same + object each time. + +.. method:: PyDLL.__getitem__(name) + + Same as :meth:`PyDLL.__getattr__` but return a new instance each time. >>> libc.time == libc.time True @@ -1549,6 +1561,22 @@ This behavior can be customized by assigning to special attributes of the foreign function object. + .. warning:: + + :class:`_FuncPtr` objects retrieved from the prefabricated + :class:`LibraryLoader` instances are cached globally, unless methods that + bypass the cache were used. Modifying their attributes can break other code + if it expects incompatible values. + + To avoid this, make a private copy at whichever level is most convenient: + + >>> windll=LibraryLoader(WinDLL) # of a LibraryLoader + >>> kernel32=copy.copy(windll.kernel32) # of an xDLL + >>> kernel32=windll.LoadLibrary("kernel32") + >>> GetModuleHandleA = \ # of a _FuncPtr + windll.kernel32["GetModuleHandleA"] + >>> GetModuleHandleA = cast(windll.kernel32.GetModuleHandleA, + WINFUNCTYPE(HMODULE,LPCSTR,use_last_error=True)) .. attribute:: restype