Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b795f52

Browse files
author
Thomas Heller
committed
Merged revisions 63988,63991 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r63988 | thomas.heller | 2008-06-06 20:37:55 +0200 (Fr, 06 Jun 2008) | 3 lines Performance improvement: Use PyDict_Get/SetItem instead of PyDict_Get/SetItemString. ........ r63991 | thomas.heller | 2008-06-06 22:05:15 +0200 (Fr, 06 Jun 2008) | 5 lines Document the new ctypes features. It would be great if someone could review both sematics, markup, and spelling, and correct the versionadded and versionchanges markers. ........
1 parent c5d0126 commit b795f52

2 files changed

Lines changed: 77 additions & 9 deletions

File tree

Doc/library/ctypes.rst

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,14 +1335,14 @@ There are several ways to loaded shared libraries into the Python process. One
13351335
way 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.
13851385
The *mode* parameter can be used to specify how the library is loaded. For
13861386
details, 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
15831606
type 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

Modules/_ctypes/callproc.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,18 @@ get_error_object(int **pspace)
117117
{
118118
PyObject *dict = PyThreadState_GetDict();
119119
PyObject *errobj;
120+
static PyObject *error_object_name;
120121
if (dict == 0) {
121122
PyErr_SetString(PyExc_RuntimeError,
122123
"cannot get thread state");
123124
return NULL;
124125
}
125-
errobj = PyDict_GetItemString(dict, "ctypes.error_object");
126+
if (error_object_name == NULL) {
127+
error_object_name = PyString_InternFromString("ctypes.error_object");
128+
if (error_object_name == NULL)
129+
return NULL;
130+
}
131+
errobj = PyDict_GetItem(dict, error_object_name);
126132
if (errobj)
127133
Py_INCREF(errobj);
128134
else {
@@ -133,8 +139,8 @@ get_error_object(int **pspace)
133139
errobj = PyCObject_FromVoidPtr(space, PyMem_Free);
134140
if (errobj == NULL)
135141
return NULL;
136-
if (-1 == PyDict_SetItemString(dict, "ctypes.error_object",
137-
errobj)) {
142+
if (-1 == PyDict_SetItem(dict, error_object_name,
143+
errobj)) {
138144
Py_DECREF(errobj);
139145
return NULL;
140146
}

0 commit comments

Comments
 (0)