annotate Modules/_ctypes/callbacks.c @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents a171048daf76
children e0409f03822f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38780
381a72ab5fb8 Add pep-291 compatibility markers.
Thomas Heller <theller@ctypes.org>
parents: 38634
diff changeset
1 /*****************************************************************
381a72ab5fb8 Add pep-291 compatibility markers.
Thomas Heller <theller@ctypes.org>
parents: 38634
diff changeset
2 This file should be kept compatible with Python 2.3, see PEP 291.
381a72ab5fb8 Add pep-291 compatibility markers.
Thomas Heller <theller@ctypes.org>
parents: 38634
diff changeset
3 *****************************************************************/
381a72ab5fb8 Add pep-291 compatibility markers.
Thomas Heller <theller@ctypes.org>
parents: 38634
diff changeset
4
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
5 #include "Python.h"
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
6 #include "compile.h" /* required only for 2.3, as it seems */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
7 #include "frameobject.h"
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
8
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
9 #include <ffi.h>
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
10 #ifdef MS_WIN32
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
11 #include <windows.h>
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
12 #endif
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
13 #include "ctypes.h"
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
14
46546
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
15 /**************************************************************/
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
16
46569
ad3417503636 Add missing return type to dealloc.
Georg Brandl <georg@python.org>
parents: 46546
diff changeset
17 static void
ad3417503636 Add missing return type to dealloc.
Georg Brandl <georg@python.org>
parents: 46546
diff changeset
18 CThunkObject_dealloc(PyObject *_self)
46546
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
19 {
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
20 CThunkObject *self = (CThunkObject *)_self;
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
21 Py_XDECREF(self->converters);
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
22 Py_XDECREF(self->callable);
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
23 Py_XDECREF(self->restype);
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
24 if (self->pcl)
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 53182
diff changeset
25 _ctypes_free_closure(self->pcl);
46546
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
26 PyObject_Del(self);
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
27 }
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
28
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
29 static int
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
30 CThunkObject_traverse(PyObject *_self, visitproc visit, void *arg)
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
31 {
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
32 CThunkObject *self = (CThunkObject *)_self;
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
33 Py_VISIT(self->converters);
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
34 Py_VISIT(self->callable);
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
35 Py_VISIT(self->restype);
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
36 return 0;
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
37 }
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
38
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
39 static int
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
40 CThunkObject_clear(PyObject *_self)
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
41 {
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
42 CThunkObject *self = (CThunkObject *)_self;
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
43 Py_CLEAR(self->converters);
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
44 Py_CLEAR(self->callable);
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
45 Py_CLEAR(self->restype);
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
46 return 0;
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
47 }
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
48
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 53182
diff changeset
49 PyTypeObject PyCThunk_Type = {
46546
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
50 PyVarObject_HEAD_INIT(NULL, 0)
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
51 "_ctypes.CThunkObject",
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
52 sizeof(CThunkObject), /* tp_basicsize */
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
53 sizeof(ffi_type), /* tp_itemsize */
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
54 CThunkObject_dealloc, /* tp_dealloc */
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
55 0, /* tp_print */
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
56 0, /* tp_getattr */
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
57 0, /* tp_setattr */
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
58 0, /* tp_compare */
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
59 0, /* tp_repr */
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
60 0, /* tp_as_number */
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
61 0, /* tp_as_sequence */
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
62 0, /* tp_as_mapping */
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
63 0, /* tp_hash */
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
64 0, /* tp_call */
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
65 0, /* tp_str */
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
66 0, /* tp_getattro */
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
67 0, /* tp_setattro */
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
68 0, /* tp_as_buffer */
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
69 Py_TPFLAGS_DEFAULT, /* tp_flags */
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
70 "CThunkObject", /* tp_doc */
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
71 CThunkObject_traverse, /* tp_traverse */
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
72 CThunkObject_clear, /* tp_clear */
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
73 0, /* tp_richcompare */
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
74 0, /* tp_weaklistoffset */
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
75 0, /* tp_iter */
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
76 0, /* tp_iternext */
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
77 0, /* tp_methods */
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
78 0, /* tp_members */
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
79 };
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
80
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
81 /**************************************************************/
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
82
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
83 static void
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
84 PrintError(char *msg, ...)
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
85 {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
86 char buf[512];
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
87 PyObject *f = PySys_GetObject("stderr");
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
88 va_list marker;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
89
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
90 va_start(marker, msg);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
91 vsnprintf(buf, sizeof(buf), msg, marker);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
92 va_end(marker);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
93 if (f)
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
94 PyFile_WriteString(buf, f);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
95 PyErr_Print();
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
96 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
97
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
98
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
99 /* after code that pyrex generates */
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 53182
diff changeset
100 void _ctypes_add_traceback(char *funcname, char *filename, int lineno)
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
101 {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
102 PyObject *py_globals = 0;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
103 PyCodeObject *py_code = 0;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
104 PyFrameObject *py_frame = 0;
53930
a171048daf76 PyCode_NewEmpty:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 53365
diff changeset
105
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
106 py_globals = PyDict_New();
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
107 if (!py_globals) goto bad;
53930
a171048daf76 PyCode_NewEmpty:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 53365
diff changeset
108 py_code = PyCode_NewEmpty(filename, funcname, lineno);
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
109 if (!py_code) goto bad;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
110 py_frame = PyFrame_New(
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
111 PyThreadState_Get(), /*PyThreadState *tstate,*/
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
112 py_code, /*PyCodeObject *code,*/
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
113 py_globals, /*PyObject *globals,*/
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
114 0 /*PyObject *locals*/
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
115 );
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
116 if (!py_frame) goto bad;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
117 py_frame->f_lineno = lineno;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
118 PyTraceBack_Here(py_frame);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
119 bad:
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
120 Py_XDECREF(py_globals);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
121 Py_XDECREF(py_code);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
122 Py_XDECREF(py_frame);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
123 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
124
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
125 #ifdef MS_WIN32
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
126 /*
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
127 * We must call AddRef() on non-NULL COM pointers we receive as arguments
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
128 * to callback functions - these functions are COM method implementations.
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
129 * The Python instances we create have a __del__ method which calls Release().
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
130 *
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
131 * The presence of a class attribute named '_needs_com_addref_' triggers this
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
132 * behaviour. It would also be possible to call the AddRef() Python method,
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
133 * after checking for PyObject_IsTrue(), but this would probably be somewhat
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
134 * slower.
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
135 */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
136 static void
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
137 TryAddRef(StgDictObject *dict, CDataObject *obj)
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
138 {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
139 IUnknown *punk;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
140
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
141 if (NULL == PyDict_GetItemString((PyObject *)dict, "_needs_com_addref_"))
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
142 return;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
143
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
144 punk = *(IUnknown **)obj->b_ptr;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
145 if (punk)
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
146 punk->lpVtbl->AddRef(punk);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
147 return;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
148 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
149 #endif
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
150
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
151 /******************************************************************************
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
152 *
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
153 * Call the python object with all arguments
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
154 *
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
155 */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
156 static void _CallPythonObject(void *mem,
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
157 ffi_type *restype,
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
158 SETFUNC setfunc,
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
159 PyObject *callable,
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
160 PyObject *converters,
47488
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
161 int flags,
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
162 void **pArgs)
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
163 {
41998
7b84e8919b00 [ 1715718 ] x64 clean compile patch for _ctypes, by Kristj?n Valur
Thomas Heller <theller@ctypes.org>
parents: 41912
diff changeset
164 Py_ssize_t i;
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
165 PyObject *result;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
166 PyObject *arglist = NULL;
41998
7b84e8919b00 [ 1715718 ] x64 clean compile patch for _ctypes, by Kristj?n Valur
Thomas Heller <theller@ctypes.org>
parents: 41912
diff changeset
167 Py_ssize_t nArgs;
47488
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
168 PyObject *error_object = NULL;
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
169 int *space;
39074
892b7ac26d3b A misspelled preprocessor symbol caused ctypes to be always compiled
Thomas Heller <theller@ctypes.org>
parents: 38963
diff changeset
170 #ifdef WITH_THREAD
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
171 PyGILState_STATE state = PyGILState_Ensure();
38963
c6d04c9c3c4a Protect the thread api calls in the _ctypes extension module within
Thomas Heller <theller@ctypes.org>
parents: 38780
diff changeset
172 #endif
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
173
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
174 nArgs = PySequence_Length(converters);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
175 /* Hm. What to return in case of error?
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
176 For COM, 0xFFFFFFFF seems better than 0.
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
177 */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
178 if (nArgs < 0) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
179 PrintError("BUG: PySequence_Length");
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
180 goto Done;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
181 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
182
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
183 arglist = PyTuple_New(nArgs);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
184 if (!arglist) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
185 PrintError("PyTuple_New()");
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
186 goto Done;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
187 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
188 for (i = 0; i < nArgs; ++i) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
189 /* Note: new reference! */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
190 PyObject *cnv = PySequence_GetItem(converters, i);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
191 StgDictObject *dict;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
192 if (cnv)
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
193 dict = PyType_stgdict(cnv);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
194 else {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
195 PrintError("Getting argument converter %d\n", i);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
196 goto Done;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
197 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
198
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 53182
diff changeset
199 if (dict && dict->getfunc && !_ctypes_simple_instance(cnv)) {
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
200 PyObject *v = dict->getfunc(*pArgs, dict->size);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
201 if (!v) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
202 PrintError("create argument %d:\n", i);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
203 Py_DECREF(cnv);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
204 goto Done;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
205 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
206 PyTuple_SET_ITEM(arglist, i, v);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
207 /* XXX XXX XX
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
208 We have the problem that c_byte or c_short have dict->size of
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
209 1 resp. 4, but these parameters are pushed as sizeof(int) bytes.
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
210 BTW, the same problem occurrs when they are pushed as parameters
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
211 */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
212 } else if (dict) {
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 53182
diff changeset
213 /* Hm, shouldn't we use PyCData_AtAddress() or something like that instead? */
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
214 CDataObject *obj = (CDataObject *)PyObject_CallFunctionObjArgs(cnv, NULL);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
215 if (!obj) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
216 PrintError("create argument %d:\n", i);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
217 Py_DECREF(cnv);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
218 goto Done;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
219 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
220 if (!CDataObject_Check(obj)) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
221 Py_DECREF(obj);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
222 Py_DECREF(cnv);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
223 PrintError("unexpected result of create argument %d:\n", i);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
224 goto Done;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
225 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
226 memcpy(obj->b_ptr, *pArgs, dict->size);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
227 PyTuple_SET_ITEM(arglist, i, (PyObject *)obj);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
228 #ifdef MS_WIN32
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
229 TryAddRef(dict, obj);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
230 #endif
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
231 } else {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
232 PyErr_SetString(PyExc_TypeError,
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
233 "cannot build parameter");
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
234 PrintError("Parsing argument %d\n", i);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
235 Py_DECREF(cnv);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
236 goto Done;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
237 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
238 Py_DECREF(cnv);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
239 /* XXX error handling! */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
240 pArgs++;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
241 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
242
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
243 #define CHECK(what, x) \
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 53182
diff changeset
244 if (x == NULL) _ctypes_add_traceback(what, "_ctypes/callbacks.c", __LINE__ - 1), PyErr_Print()
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
245
47488
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
246 if (flags & (FUNCFLAG_USE_ERRNO | FUNCFLAG_USE_LASTERROR)) {
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 53182
diff changeset
247 error_object = _ctypes_get_errobj(&space);
47488
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
248 if (error_object == NULL)
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
249 goto Done;
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
250 if (flags & FUNCFLAG_USE_ERRNO) {
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
251 int temp = space[0];
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
252 space[0] = errno;
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
253 errno = temp;
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
254 }
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
255 #ifdef MS_WIN32
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
256 if (flags & FUNCFLAG_USE_LASTERROR) {
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
257 int temp = space[1];
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
258 space[1] = GetLastError();
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
259 SetLastError(temp);
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
260 }
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
261 #endif
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
262 }
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
263
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
264 result = PyObject_CallObject(callable, arglist);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
265 CHECK("'calling callback function'", result);
47488
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
266
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
267 #ifdef MS_WIN32
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
268 if (flags & FUNCFLAG_USE_LASTERROR) {
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
269 int temp = space[1];
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
270 space[1] = GetLastError();
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
271 SetLastError(temp);
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
272 }
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
273 #endif
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
274 if (flags & FUNCFLAG_USE_ERRNO) {
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
275 int temp = space[0];
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
276 space[0] = errno;
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
277 errno = temp;
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
278 }
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
279 Py_XDECREF(error_object);
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
280
39526
ffe7d3ab35a8 Remove the special casing of Py_None when converting the return value
Thomas Heller <theller@ctypes.org>
parents: 39486
diff changeset
281 if ((restype != &ffi_type_void) && result) {
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
282 PyObject *keep;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
283 assert(setfunc);
39486
a6a6dff17935 Revert the change that tries to zero out a closure's result storage
Thomas Heller <theller@ctypes.org>
parents: 39444
diff changeset
284 #ifdef WORDS_BIGENDIAN
a6a6dff17935 Revert the change that tries to zero out a closure's result storage
Thomas Heller <theller@ctypes.org>
parents: 39444
diff changeset
285 /* See the corresponding code in callproc.c, around line 961 */
a6a6dff17935 Revert the change that tries to zero out a closure's result storage
Thomas Heller <theller@ctypes.org>
parents: 39444
diff changeset
286 if (restype->type != FFI_TYPE_FLOAT && restype->size < sizeof(ffi_arg))
a6a6dff17935 Revert the change that tries to zero out a closure's result storage
Thomas Heller <theller@ctypes.org>
parents: 39444
diff changeset
287 mem = (char *)mem + sizeof(ffi_arg) - restype->size;
a6a6dff17935 Revert the change that tries to zero out a closure's result storage
Thomas Heller <theller@ctypes.org>
parents: 39444
diff changeset
288 #endif
38634
7bfafdeffee2 Use the same big-endian hack as in _ctypes/callproc.c for callback functions.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
289 keep = setfunc(mem, result, 0);
7bfafdeffee2 Use the same big-endian hack as in _ctypes/callproc.c for callback functions.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
290 CHECK("'converting callback result'", keep);
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
291 /* keep is an object we have to keep alive so that the result
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
292 stays valid. If there is no such object, the setfunc will
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
293 have returned Py_None.
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
294
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
295 If there is such an object, we have no choice than to keep
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
296 it alive forever - but a refcount and/or memory leak will
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
297 be the result. EXCEPT when restype is py_object - Python
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
298 itself knows how to manage the refcount of these objects.
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
299 */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
300 if (keep == NULL) /* Could not convert callback result. */
39444
b995e2760da4 When a ctypes C callback function is called, zero out the result
Thomas Heller <theller@ctypes.org>
parents: 39119
diff changeset
301 PyErr_WriteUnraisable(callable);
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
302 else if (keep == Py_None) /* Nothing to keep */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
303 Py_DECREF(keep);
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 53182
diff changeset
304 else if (setfunc != _ctypes_get_fielddesc("O")->setfunc) {
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
305 if (-1 == PyErr_Warn(PyExc_RuntimeWarning,
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
306 "memory leak in callback function."))
39444
b995e2760da4 When a ctypes C callback function is called, zero out the result
Thomas Heller <theller@ctypes.org>
parents: 39119
diff changeset
307 PyErr_WriteUnraisable(callable);
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
308 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
309 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
310 Py_XDECREF(result);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
311 Done:
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
312 Py_XDECREF(arglist);
39074
892b7ac26d3b A misspelled preprocessor symbol caused ctypes to be always compiled
Thomas Heller <theller@ctypes.org>
parents: 38963
diff changeset
313 #ifdef WITH_THREAD
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
314 PyGILState_Release(state);
38963
c6d04c9c3c4a Protect the thread api calls in the _ctypes extension module within
Thomas Heller <theller@ctypes.org>
parents: 38780
diff changeset
315 #endif
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
316 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
317
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
318 static void closure_fcn(ffi_cif *cif,
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
319 void *resp,
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
320 void **args,
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
321 void *userdata)
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
322 {
46546
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
323 CThunkObject *p = (CThunkObject *)userdata;
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
324
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
325 _CallPythonObject(resp,
46546
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
326 p->ffi_restype,
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
327 p->setfunc,
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
328 p->callable,
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
329 p->converters,
47488
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
330 p->flags,
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
331 args);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
332 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
333
46546
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
334 static CThunkObject* CThunkObject_new(Py_ssize_t nArgs)
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
335 {
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
336 CThunkObject *p;
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
337 int i;
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
338
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 53182
diff changeset
339 p = PyObject_NewVar(CThunkObject, &PyCThunk_Type, nArgs);
46546
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
340 if (p == NULL) {
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
341 PyErr_NoMemory();
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
342 return NULL;
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
343 }
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
344
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
345 p->pcl = NULL;
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
346 memset(&p->cif, 0, sizeof(p->cif));
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
347 p->converters = NULL;
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
348 p->callable = NULL;
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
349 p->setfunc = NULL;
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
350 p->ffi_restype = NULL;
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
351
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
352 for (i = 0; i < nArgs + 1; ++i)
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
353 p->atypes[i] = NULL;
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
354 return p;
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
355 }
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
356
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 53182
diff changeset
357 CThunkObject *_ctypes_alloc_callback(PyObject *callable,
46546
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
358 PyObject *converters,
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
359 PyObject *restype,
47488
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
360 int flags)
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
361 {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
362 int result;
46546
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
363 CThunkObject *p;
41998
7b84e8919b00 [ 1715718 ] x64 clean compile patch for _ctypes, by Kristj?n Valur
Thomas Heller <theller@ctypes.org>
parents: 41912
diff changeset
364 Py_ssize_t nArgs, i;
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
365 ffi_abi cc;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
366
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
367 nArgs = PySequence_Size(converters);
46546
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
368 p = CThunkObject_new(nArgs);
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
369 if (p == NULL)
37147
0057862a3bdb Include <malloc.h> on windows, to avoid warnings when compiling with mingw.
Thomas Heller <theller@ctypes.org>
parents: 37115
diff changeset
370 return NULL;
46546
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
371
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
372 assert(CThunk_CheckExact(p));
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
373
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 53182
diff changeset
374 p->pcl = _ctypes_alloc_closure();
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
375 if (p->pcl == NULL) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
376 PyErr_NoMemory();
37030
a253958bfa4c Rewrite the AllocFunctionCallback function for better error handling.
Thomas Heller <theller@ctypes.org>
parents: 36897
diff changeset
377 goto error;
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
378 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
379
47488
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
380 p->flags = flags;
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
381 for (i = 0; i < nArgs; ++i) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
382 PyObject *cnv = PySequence_GetItem(converters, i);
37030
a253958bfa4c Rewrite the AllocFunctionCallback function for better error handling.
Thomas Heller <theller@ctypes.org>
parents: 36897
diff changeset
383 if (cnv == NULL)
a253958bfa4c Rewrite the AllocFunctionCallback function for better error handling.
Thomas Heller <theller@ctypes.org>
parents: 36897
diff changeset
384 goto error;
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 53182
diff changeset
385 p->atypes[i] = _ctypes_get_ffi_type(cnv);
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
386 Py_DECREF(cnv);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
387 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
388 p->atypes[i] = NULL;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
389
46546
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
390 Py_INCREF(restype);
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
391 p->restype = restype;
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
392 if (restype == Py_None) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
393 p->setfunc = NULL;
46546
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
394 p->ffi_restype = &ffi_type_void;
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
395 } else {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
396 StgDictObject *dict = PyType_stgdict(restype);
40141
52fe0ecf55c1 ctypes callback functions only support 'fundamental' result types.
Thomas Heller <theller@ctypes.org>
parents: 39679
diff changeset
397 if (dict == NULL || dict->setfunc == NULL) {
52fe0ecf55c1 ctypes callback functions only support 'fundamental' result types.
Thomas Heller <theller@ctypes.org>
parents: 39679
diff changeset
398 PyErr_SetString(PyExc_TypeError,
52fe0ecf55c1 ctypes callback functions only support 'fundamental' result types.
Thomas Heller <theller@ctypes.org>
parents: 39679
diff changeset
399 "invalid result type for callback function");
52fe0ecf55c1 ctypes callback functions only support 'fundamental' result types.
Thomas Heller <theller@ctypes.org>
parents: 39679
diff changeset
400 goto error;
52fe0ecf55c1 ctypes callback functions only support 'fundamental' result types.
Thomas Heller <theller@ctypes.org>
parents: 39679
diff changeset
401 }
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
402 p->setfunc = dict->setfunc;
46546
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
403 p->ffi_restype = &dict->ffi_type_pointer;
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
404 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
405
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
406 cc = FFI_DEFAULT_ABI;
39679
3bfc8bb4e726 Port _ctypes.pyd to win64 on AMD64.
Thomas Heller <theller@ctypes.org>
parents: 39526
diff changeset
407 #if defined(MS_WIN32) && !defined(_WIN32_WCE) && !defined(MS_WIN64)
47488
4f5e6aaf5a6a Issue #1798: Add ctypes calling convention that allows safe access of errno.
Thomas Heller <theller@ctypes.org>
parents: 47469
diff changeset
408 if ((flags & FUNCFLAG_CDECL) == 0)
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
409 cc = FFI_STDCALL;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
410 #endif
41998
7b84e8919b00 [ 1715718 ] x64 clean compile patch for _ctypes, by Kristj?n Valur
Thomas Heller <theller@ctypes.org>
parents: 41912
diff changeset
411 result = ffi_prep_cif(&p->cif, cc,
7b84e8919b00 [ 1715718 ] x64 clean compile patch for _ctypes, by Kristj?n Valur
Thomas Heller <theller@ctypes.org>
parents: 41912
diff changeset
412 Py_SAFE_DOWNCAST(nArgs, Py_ssize_t, int),
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 53182
diff changeset
413 _ctypes_get_ffi_type(restype),
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
414 &p->atypes[0]);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
415 if (result != FFI_OK) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
416 PyErr_Format(PyExc_RuntimeError,
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
417 "ffi_prep_cif failed with %d", result);
37030
a253958bfa4c Rewrite the AllocFunctionCallback function for better error handling.
Thomas Heller <theller@ctypes.org>
parents: 36897
diff changeset
418 goto error;
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
419 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
420 result = ffi_prep_closure(p->pcl, &p->cif, closure_fcn, p);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
421 if (result != FFI_OK) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
422 PyErr_Format(PyExc_RuntimeError,
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
423 "ffi_prep_closure failed with %d", result);
37030
a253958bfa4c Rewrite the AllocFunctionCallback function for better error handling.
Thomas Heller <theller@ctypes.org>
parents: 36897
diff changeset
424 goto error;
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
425 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
426
46546
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
427 Py_INCREF(converters);
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
428 p->converters = converters;
46546
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
429 Py_INCREF(callable);
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
430 p->callable = callable;
37147
0057862a3bdb Include <malloc.h> on windows, to avoid warnings when compiling with mingw.
Thomas Heller <theller@ctypes.org>
parents: 37115
diff changeset
431 return p;
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
432
37030
a253958bfa4c Rewrite the AllocFunctionCallback function for better error handling.
Thomas Heller <theller@ctypes.org>
parents: 36897
diff changeset
433 error:
46546
4ff8dfaec3fc Remove cyclic reference in CFuncPtr instances; see issue #2682.
Thomas Heller <theller@ctypes.org>
parents: 45028
diff changeset
434 Py_XDECREF(p);
37030
a253958bfa4c Rewrite the AllocFunctionCallback function for better error handling.
Thomas Heller <theller@ctypes.org>
parents: 36897
diff changeset
435 return NULL;
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
436 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
437
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
438 #ifdef MS_WIN32
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
439
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
440 static void LoadPython(void)
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
441 {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
442 if (!Py_IsInitialized()) {
39119
c5759b5a57f0 Make sure the _ctypes extension can be compiled when WITH_THREAD is
Thomas Heller <theller@ctypes.org>
parents: 39074
diff changeset
443 #ifdef WITH_THREAD
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
444 PyEval_InitThreads();
39119
c5759b5a57f0 Make sure the _ctypes extension can be compiled when WITH_THREAD is
Thomas Heller <theller@ctypes.org>
parents: 39074
diff changeset
445 #endif
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
446 Py_Initialize();
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
447 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
448 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
449
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
450 /******************************************************************/
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
451
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
452 long Call_GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
453 {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
454 PyObject *mod, *func, *result;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
455 long retval;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
456 static PyObject *context;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
457
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
458 if (context == NULL)
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47488
diff changeset
459 context = PyString_InternFromString("_ctypes.DllGetClassObject");
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
460
44439
0cbe94bea2c7 Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0
Christian Heimes <christian@cheimes.de>
parents: 42266
diff changeset
461 mod = PyImport_ImportModuleNoBlock("ctypes");
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
462 if (!mod) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
463 PyErr_WriteUnraisable(context ? context : Py_None);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
464 /* There has been a warning before about this already */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
465 return E_FAIL;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
466 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
467
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
468 func = PyObject_GetAttrString(mod, "DllGetClassObject");
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
469 Py_DECREF(mod);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
470 if (!func) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
471 PyErr_WriteUnraisable(context ? context : Py_None);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
472 return E_FAIL;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
473 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
474
41582
25cc1cee5e69 Don't truncate pointers to integers (on win64 platform).
Thomas Heller <theller@ctypes.org>
parents: 40141
diff changeset
475 {
41912
ddf0471a6de3 Fix compiler warnings.
Thomas Heller <theller@ctypes.org>
parents: 41582
diff changeset
476 PyObject *py_rclsid = PyLong_FromVoidPtr((void *)rclsid);
ddf0471a6de3 Fix compiler warnings.
Thomas Heller <theller@ctypes.org>
parents: 41582
diff changeset
477 PyObject *py_riid = PyLong_FromVoidPtr((void *)riid);
41582
25cc1cee5e69 Don't truncate pointers to integers (on win64 platform).
Thomas Heller <theller@ctypes.org>
parents: 40141
diff changeset
478 PyObject *py_ppv = PyLong_FromVoidPtr(ppv);
25cc1cee5e69 Don't truncate pointers to integers (on win64 platform).
Thomas Heller <theller@ctypes.org>
parents: 40141
diff changeset
479 if (!py_rclsid || !py_riid || !py_ppv) {
25cc1cee5e69 Don't truncate pointers to integers (on win64 platform).
Thomas Heller <theller@ctypes.org>
parents: 40141
diff changeset
480 Py_XDECREF(py_rclsid);
25cc1cee5e69 Don't truncate pointers to integers (on win64 platform).
Thomas Heller <theller@ctypes.org>
parents: 40141
diff changeset
481 Py_XDECREF(py_riid);
25cc1cee5e69 Don't truncate pointers to integers (on win64 platform).
Thomas Heller <theller@ctypes.org>
parents: 40141
diff changeset
482 Py_XDECREF(py_ppv);
25cc1cee5e69 Don't truncate pointers to integers (on win64 platform).
Thomas Heller <theller@ctypes.org>
parents: 40141
diff changeset
483 Py_DECREF(func);
25cc1cee5e69 Don't truncate pointers to integers (on win64 platform).
Thomas Heller <theller@ctypes.org>
parents: 40141
diff changeset
484 PyErr_WriteUnraisable(context ? context : Py_None);
25cc1cee5e69 Don't truncate pointers to integers (on win64 platform).
Thomas Heller <theller@ctypes.org>
parents: 40141
diff changeset
485 return E_FAIL;
25cc1cee5e69 Don't truncate pointers to integers (on win64 platform).
Thomas Heller <theller@ctypes.org>
parents: 40141
diff changeset
486 }
25cc1cee5e69 Don't truncate pointers to integers (on win64 platform).
Thomas Heller <theller@ctypes.org>
parents: 40141
diff changeset
487 result = PyObject_CallFunctionObjArgs(func,
25cc1cee5e69 Don't truncate pointers to integers (on win64 platform).
Thomas Heller <theller@ctypes.org>
parents: 40141
diff changeset
488 py_rclsid,
25cc1cee5e69 Don't truncate pointers to integers (on win64 platform).
Thomas Heller <theller@ctypes.org>
parents: 40141
diff changeset
489 py_riid,
25cc1cee5e69 Don't truncate pointers to integers (on win64 platform).
Thomas Heller <theller@ctypes.org>
parents: 40141
diff changeset
490 py_ppv,
25cc1cee5e69 Don't truncate pointers to integers (on win64 platform).
Thomas Heller <theller@ctypes.org>
parents: 40141
diff changeset
491 NULL);
25cc1cee5e69 Don't truncate pointers to integers (on win64 platform).
Thomas Heller <theller@ctypes.org>
parents: 40141
diff changeset
492 Py_DECREF(py_rclsid);
25cc1cee5e69 Don't truncate pointers to integers (on win64 platform).
Thomas Heller <theller@ctypes.org>
parents: 40141
diff changeset
493 Py_DECREF(py_riid);
25cc1cee5e69 Don't truncate pointers to integers (on win64 platform).
Thomas Heller <theller@ctypes.org>
parents: 40141
diff changeset
494 Py_DECREF(py_ppv);
25cc1cee5e69 Don't truncate pointers to integers (on win64 platform).
Thomas Heller <theller@ctypes.org>
parents: 40141
diff changeset
495 }
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
496 Py_DECREF(func);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
497 if (!result) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
498 PyErr_WriteUnraisable(context ? context : Py_None);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
499 return E_FAIL;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
500 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
501
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
502 retval = PyInt_AsLong(result);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
503 if (PyErr_Occurred()) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
504 PyErr_WriteUnraisable(context ? context : Py_None);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
505 retval = E_FAIL;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
506 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
507 Py_DECREF(result);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
508 return retval;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
509 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
510
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
511 STDAPI DllGetClassObject(REFCLSID rclsid,
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
512 REFIID riid,
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
513 LPVOID *ppv)
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
514 {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
515 long result;
39074
892b7ac26d3b A misspelled preprocessor symbol caused ctypes to be always compiled
Thomas Heller <theller@ctypes.org>
parents: 38963
diff changeset
516 #ifdef WITH_THREAD
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
517 PyGILState_STATE state;
38963
c6d04c9c3c4a Protect the thread api calls in the _ctypes extension module within
Thomas Heller <theller@ctypes.org>
parents: 38780
diff changeset
518 #endif
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
519
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
520 LoadPython();
39074
892b7ac26d3b A misspelled preprocessor symbol caused ctypes to be always compiled
Thomas Heller <theller@ctypes.org>
parents: 38963
diff changeset
521 #ifdef WITH_THREAD
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
522 state = PyGILState_Ensure();
38963
c6d04c9c3c4a Protect the thread api calls in the _ctypes extension module within
Thomas Heller <theller@ctypes.org>
parents: 38780
diff changeset
523 #endif
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
524 result = Call_GetClassObject(rclsid, riid, ppv);
39074
892b7ac26d3b A misspelled preprocessor symbol caused ctypes to be always compiled
Thomas Heller <theller@ctypes.org>
parents: 38963
diff changeset
525 #ifdef WITH_THREAD
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
526 PyGILState_Release(state);
38963
c6d04c9c3c4a Protect the thread api calls in the _ctypes extension module within
Thomas Heller <theller@ctypes.org>
parents: 38780
diff changeset
527 #endif
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
528 return result;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
529 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
530
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
531 long Call_CanUnloadNow(void)
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
532 {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
533 PyObject *mod, *func, *result;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
534 long retval;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
535 static PyObject *context;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
536
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
537 if (context == NULL)
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47488
diff changeset
538 context = PyString_InternFromString("_ctypes.DllCanUnloadNow");
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
539
44439
0cbe94bea2c7 Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0
Christian Heimes <christian@cheimes.de>
parents: 42266
diff changeset
540 mod = PyImport_ImportModuleNoBlock("ctypes");
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
541 if (!mod) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
542 /* OutputDebugString("Could not import ctypes"); */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
543 /* We assume that this error can only occur when shutting
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
544 down, so we silently ignore it */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
545 PyErr_Clear();
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
546 return E_FAIL;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
547 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
548 /* Other errors cannot be raised, but are printed to stderr */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
549 func = PyObject_GetAttrString(mod, "DllCanUnloadNow");
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
550 Py_DECREF(mod);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
551 if (!func) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
552 PyErr_WriteUnraisable(context ? context : Py_None);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
553 return E_FAIL;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
554 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
555
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
556 result = PyObject_CallFunction(func, NULL);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
557 Py_DECREF(func);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
558 if (!result) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
559 PyErr_WriteUnraisable(context ? context : Py_None);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
560 return E_FAIL;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
561 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
562
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
563 retval = PyInt_AsLong(result);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
564 if (PyErr_Occurred()) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
565 PyErr_WriteUnraisable(context ? context : Py_None);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
566 retval = E_FAIL;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
567 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
568 Py_DECREF(result);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
569 return retval;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
570 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
571
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
572 /*
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
573 DllRegisterServer and DllUnregisterServer still missing
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
574 */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
575
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
576 STDAPI DllCanUnloadNow(void)
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
577 {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
578 long result;
39074
892b7ac26d3b A misspelled preprocessor symbol caused ctypes to be always compiled
Thomas Heller <theller@ctypes.org>
parents: 38963
diff changeset
579 #ifdef WITH_THREAD
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
580 PyGILState_STATE state = PyGILState_Ensure();
38963
c6d04c9c3c4a Protect the thread api calls in the _ctypes extension module within
Thomas Heller <theller@ctypes.org>
parents: 38780
diff changeset
581 #endif
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
582 result = Call_CanUnloadNow();
39074
892b7ac26d3b A misspelled preprocessor symbol caused ctypes to be always compiled
Thomas Heller <theller@ctypes.org>
parents: 38963
diff changeset
583 #ifdef WITH_THREAD
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
584 PyGILState_Release(state);
38963
c6d04c9c3c4a Protect the thread api calls in the _ctypes extension module within
Thomas Heller <theller@ctypes.org>
parents: 38780
diff changeset
585 #endif
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
586 return result;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
587 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
588
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
589 #ifndef Py_NO_ENABLE_SHARED
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
590 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvRes)
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
591 {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
592 switch(fdwReason) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
593 case DLL_PROCESS_ATTACH:
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
594 DisableThreadLibraryCalls(hinstDLL);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
595 break;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
596 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
597 return TRUE;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
598 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
599 #endif
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
600
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
601 #endif
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
602
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
603 /*
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
604 Local Variables:
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
605 compile-command: "cd .. && python setup.py -q build_ext"
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
606 End:
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
607 */