This repository was archived by the owner on Dec 16, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathhandle.c
More file actions
361 lines (292 loc) · 10.7 KB
/
Copy pathhandle.c
File metadata and controls
361 lines (292 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/* Taken from http://bugs.python.org/issue8212
* A function to do the necessary adjustments if we find that code
* run during a tp_dealloc or tp_free has resurrected an
* object. It adjusts the total reference count and adds a new
* reference to the type.
*/
static void
resurrect_object(PyObject *self)
{
/* The object lives again. We must now undo the _Py_ForgetReference
* done in _Py_Dealloc in object.c.
*/
Py_ssize_t refcnt = Py_REFCNT(self);
ASSERT(Py_REFCNT(self) != 0);
_Py_NewReference(self);
Py_REFCNT(self) = refcnt;
/* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
* we need to undo that. */
#ifdef _Py_DEC_REFTOTAL
_Py_DEC_REFTOTAL;
#endif
/* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
* chain, so no more to do there.
* If COUNT_ALLOCS, the original decref bumped tp_frees, and
* _Py_NewReference bumped tp_allocs: both of those need to be
* undone.
*/
#ifdef COUNT_ALLOCS
--Py_TYPE(self)->tp_frees;
--Py_TYPE(self)->tp_allocs;
#endif
/* When called from a heap type's dealloc (subtype_dealloc avove), the type will be
* decref'ed on return. This counteracts that. There is no way to otherwise
* let subtype_dealloc know that calling a parent class' tp_dealloc slot caused
* the instance to be resurrected.
*/
if (PyType_HasFeature(Py_TYPE(self), Py_TPFLAGS_HEAPTYPE))
Py_INCREF(Py_TYPE(self));
return;
}
static void
pyuv__handle_close_cb(uv_handle_t *handle)
{
PyGILState_STATE gstate = PyGILState_Ensure();
Handle *self;
PyObject *result;
ASSERT(handle);
/* Can't use container_of here */
self = (Handle *)handle->data;
if (self->on_close_cb != Py_None) {
result = PyObject_CallFunctionObjArgs(self->on_close_cb, self, NULL);
if (result == NULL) {
handle_uncaught_exception(self->loop);
}
Py_XDECREF(result);
}
Py_DECREF(self->on_close_cb);
self->on_close_cb = NULL;
Py_DECREF(self->loop);
self->loop = (Loop *)Py_None;
Py_INCREF(Py_None);
/* Remove extra reference, in case it was added along the way */
PYUV_HANDLE_DECREF(self);
/* Refcount was increased in the caller function */
Py_DECREF(self);
PyGILState_Release(gstate);
}
static void
pyuv__handle_dealloc_close_cb(uv_handle_t *handle)
{
PyGILState_STATE gstate = PyGILState_Ensure();
Handle *self;
ASSERT(handle);
/* Can't use container_of here */
self = (Handle *)handle->data;
Py_DECREF(self);
PyGILState_Release(gstate);
}
static void
initialize_handle(Handle *self, Loop *loop)
{
PyObject *tmp;
tmp = (PyObject *)self->loop;
Py_INCREF(loop);
self->loop = loop;
Py_XDECREF(tmp);
self->flags = 0;
self->initialized = True;
}
static PyObject *
Handle_func_close(Handle *self, PyObject *args)
{
PyObject *callback = Py_None;
RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL);
RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL);
if (!PyArg_ParseTuple(args, "|O:close", &callback)) {
return NULL;
}
if (callback != Py_None && !PyCallable_Check(callback)) {
PyErr_SetString(PyExc_TypeError, "a callable or None is required");
return NULL;
}
Py_INCREF(callback);
self->on_close_cb = callback;
/* Increase refcount so that object is not removed before the callback is called */
Py_INCREF(self);
uv_close(self->uv_handle, pyuv__handle_close_cb);
Py_RETURN_NONE;
}
static PyObject *
Handle_active_get(Handle *self, void *closure)
{
UNUSED_ARG(closure);
RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL);
return PyBool_FromLong((long)uv_is_active(self->uv_handle));
}
static PyObject *
Handle_closed_get(Handle *self, void *closure)
{
UNUSED_ARG(closure);
RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL);
return PyBool_FromLong((long)uv_is_closing(self->uv_handle));
}
static PyObject *
Handle_ref_get(Handle *self, void *closure)
{
UNUSED_ARG(closure);
RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL);
return PyBool_FromLong((long)uv_has_ref(self->uv_handle));
}
static int
Handle_ref_set(Handle *self, PyObject* val, void* c)
{
long ref;
UNUSED_ARG(c);
ref = PyLong_AsLong(val);
if (ref == -1 && PyErr_Occurred()) {
return -1;
}
if (ref) {
uv_ref(self->uv_handle);
} else {
uv_unref(self->uv_handle);
}
return 0;
}
static PyObject *
Handle_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
Handle *self = (Handle *)PyType_GenericNew(type, args, kwargs);
if (!self) {
return NULL;
}
self->handle_magic = PYUV_HANDLE_MAGIC;
self->initialized = False;
self->uv_handle = NULL;
self->weakreflist = NULL;
return (PyObject *)self;
}
static int
Handle_tp_traverse(Handle *self, visitproc visit, void *arg)
{
Py_VISIT(self->on_close_cb);
Py_VISIT(self->loop);
Py_VISIT(self->dict);
return 0;
}
static int
Handle_tp_clear(Handle *self)
{
Py_CLEAR(self->on_close_cb);
Py_CLEAR(self->loop);
Py_CLEAR(self->dict);
return 0;
}
static void
Handle_tp_dealloc(Handle *self)
{
ASSERT(self->uv_handle);
if (self->initialized && !uv_is_closing(self->uv_handle)) {
uv_close(self->uv_handle, pyuv__handle_dealloc_close_cb);
ASSERT(uv_is_closing(self->uv_handle));
/* resurrect the Python object until the close callback is called */
Py_INCREF(self);
resurrect_object((PyObject *)self);
return;
} else {
/* There are a few cases why the code will take this path:
* - A subclass of a handle didn't call it's parent's __init__
* - Aclosed handle is deallocated. Refcount is increased in close(),
* so it's guaranteed that if we arrived here and the user had called close(),
* the callback was already executed.
* - A handle goes out of scope and it's closed here in tp_dealloc and resurrected.
* Once it's deallocated again it will take this path because the handle is now
* closed.
*/
;
}
if (self->weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject *)self);
}
subtype_clear((PyObject *)self);
Py_TYPE(self)->tp_free((PyObject *)self);
}
static PyObject*
Handle_dict_get(Handle *self, void* c)
{
UNUSED_ARG(c);
if (self->dict == NULL) {
self->dict = PyDict_New();
if (self->dict == NULL) {
return NULL;
}
}
Py_INCREF(self->dict);
return self->dict;
}
static int
Handle_dict_set(Handle *self, PyObject* val, void* c)
{
PyObject* tmp;
UNUSED_ARG(c);
if (val == NULL) {
PyErr_SetString(PyExc_TypeError, "__dict__ may not be deleted");
return -1;
}
if (!PyDict_Check(val)) {
PyErr_SetString(PyExc_TypeError, "__dict__ must be a dictionary");
return -1;
}
tmp = self->dict;
Py_INCREF(val);
self->dict = val;
Py_XDECREF(tmp);
return 0;
}
static PyMethodDef
Handle_tp_methods[] = {
{ "close", (PyCFunction)Handle_func_close, METH_VARARGS, "Close handle." },
{ NULL }
};
static PyMemberDef Handle_tp_members[] = {
{"loop", T_OBJECT_EX, offsetof(Handle, loop), READONLY, "Loop where this handle belongs."},
{NULL}
};
static PyGetSetDef Handle_tp_getsets[] = {
{"__dict__", (getter)Handle_dict_get, (setter)Handle_dict_set, NULL},
{"active", (getter)Handle_active_get, NULL, "Indicates if this handle is active.", NULL},
{"ref", (getter)Handle_ref_get, (setter)Handle_ref_set, "Indicates if this handle is ref'd or not.", NULL},
{"closed", (getter)Handle_closed_get, NULL, "Indicates if this handle is closing or already closed.", NULL},
{NULL}
};
static PyTypeObject HandleType = {
PyVarObject_HEAD_INIT(NULL, 0)
"pyuv._cpyuv.Handle", /*tp_name*/
sizeof(Handle), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)Handle_tp_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
0, /*tp_doc*/
(traverseproc)Handle_tp_traverse, /*tp_traverse*/
(inquiry)Handle_tp_clear, /*tp_clear*/
0, /*tp_richcompare*/
offsetof(Handle, weakreflist), /*tp_weaklistoffset*/
0, /*tp_iter*/
0, /*tp_iternext*/
Handle_tp_methods, /*tp_methods*/
Handle_tp_members, /*tp_members*/
Handle_tp_getsets, /*tp_getsets*/
0, /*tp_base*/
0, /*tp_dict*/
0, /*tp_descr_get*/
0, /*tp_descr_set*/
offsetof(Handle, dict), /*tp_dictoffset*/
0, /*tp_init*/
0, /*tp_alloc*/
Handle_tp_new, /*tp_new*/
};