@@ -1335,14 +1335,14 @@ There are several ways to loaded shared libraries into the Python process. One
13351335way is to instantiate one of the following classes:
13361336
13371337
1338- .. class :: CDLL(name, mode=DEFAULT_MODE, handle=None)
1338+ .. class :: CDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False )
13391339
13401340 Instances of this class represent loaded shared libraries. Functions in these
13411341 libraries use the standard C calling convention, and are assumed to return
13421342 ``int ``.
13431343
13441344
1345- .. class :: OleDLL(name, mode=DEFAULT_MODE, handle=None)
1345+ .. class :: OleDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False )
13461346
13471347 Windows only: Instances of this class represent loaded shared libraries,
13481348 functions in these libraries use the ``stdcall `` calling convention, and are
@@ -1352,7 +1352,7 @@ way is to instantiate one of the following classes:
13521352 failure, an :class: `WindowsError ` is automatically raised.
13531353
13541354
1355- .. class :: WinDLL(name, mode=DEFAULT_MODE, handle=None)
1355+ .. class :: WinDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False )
13561356
13571357 Windows only: Instances of this class represent loaded shared libraries,
13581358 functions in these libraries use the ``stdcall `` calling convention, and are
@@ -1385,6 +1385,29 @@ it.
13851385The *mode * parameter can be used to specify how the library is loaded. For
13861386details, consult the ``dlopen(3) `` manpage, on Windows, *mode * is ignored.
13871387
1388+ The *use_errno * parameter, when set to True, enables a ctypes
1389+ mechanism that allows to access the system `errno ` error number in a
1390+ safe way. `ctypes ` maintains a thread-local copy of the systems
1391+ `errno ` variable; if you call foreign functions created with
1392+ `use_errno=True ` then the `errno ` value before the function call is
1393+ swapped with the ctypes private copy, the same happens immediately
1394+ after the function call.
1395+
1396+ The function `ctypes.get_errno() ` returns the value of the ctypes
1397+ private copy, and the function `ctypes.set_errno(value) ` changes the
1398+ ctypes private copy to `value ` and returns the former value.
1399+
1400+ The *use_last_error * parameter, when set to True, enables the same
1401+ mechanism for the Windows error code which is managed by the
1402+ GetLastError() and SetLastError() Windows api functions;
1403+ `ctypes.get_last_error() ` and `ctypes.set_last_error(value) ` are used
1404+ to request and change the ctypes private copy of the windows error
1405+ code.
1406+
1407+ .. versionchanged :: 2.6
1408+
1409+ The `use_errno ` and `use_last_error ` parameters were added in Python
1410+ 2.6.
13881411
13891412.. data :: RTLD_GLOBAL
13901413 :noindex:
@@ -1583,18 +1606,26 @@ implementation. The factory functions must be called with the desired result
15831606type and the argument types of the function.
15841607
15851608
1586- .. function :: CFUNCTYPE(restype, *argtypes)
1609+ .. function :: CFUNCTYPE(restype, *argtypes, use_errno=False, use_last_error=False )
15871610
15881611 The returned function prototype creates functions that use the standard C
15891612 calling convention. The function will release the GIL during the call.
1613+ If `use_errno ` is set to True, the ctypes private copy of the system `errno `
1614+ variable is exchanged with the real `errno ` value bafore and after the call;
1615+ `use_last_error ` does the same for the Windows error code.
1616+
1617+ .. versionchanged :: 2.6
15901618
1619+ The optional `use_errno ` and `use_last_error ` parameters were added
1620+ in Python 2.6.
15911621
1592- .. function :: WINFUNCTYPE(restype, *argtypes)
1622+
1623+ .. function :: WINFUNCTYPE(restype, *argtypes, use_errno=False, use_last_error=False)
15931624
15941625 Windows only: The returned function prototype creates functions that use the
15951626 ``stdcall `` calling convention, except on Windows CE where :func: `WINFUNCTYPE `
15961627 is the same as :func: `CFUNCTYPE `. The function will release the GIL during the
1597- call.
1628+ call. ` use_errno ` and ` use_last_error ` have the same meaning as above.
15981629
15991630
16001631.. function :: PYFUNCTYPE(restype, *argtypes)
@@ -1846,7 +1877,22 @@ Utility functions
18461877.. function :: GetLastError()
18471878
18481879 Windows only: Returns the last error code set by Windows in the calling thread.
1880+ This function calls the Windows `GetLastError() ` function directly,
1881+ it does not return the ctypes-private copy of the error code.
1882+
1883+ .. function :: get_errno()
1884+
1885+ Returns the current value of the ctypes-private copy of the system
1886+ `errno ` variable in the calling thread.
1887+
1888+ .. versionadded :: 2.6
18491889
1890+ .. function :: get_last_error()
1891+
1892+ Windows only: returns the current value of the ctypes-private copy of the system
1893+ `LastError ` variable in the calling thread.
1894+
1895+ .. versionadded :: 2.6
18501896
18511897.. function :: memmove(dst, src, count)
18521898
@@ -1899,6 +1945,22 @@ Utility functions
18991945 other systems ``('ascii', 'strict') ``.
19001946
19011947
1948+ .. function :: set_errno(value)
1949+
1950+ Set the current value of the ctypes-private copy of the system
1951+ `errno ` variable in the calling thread to `value ` and return the
1952+ previous value.
1953+
1954+ .. versionadded :: 2.6
1955+
1956+ .. function :: set_last_error(value)
1957+
1958+ Windows only: set the current value of the ctypes-private copy of
1959+ the system `LastError ` variable in the calling thread to `value `
1960+ and return the previous value.
1961+
1962+ .. versionadded :: 2.6
1963+
19021964.. function :: sizeof(obj_or_type)
19031965
19041966 Returns the size in bytes of a ctypes type or instance memory buffer. Does the
0 commit comments