[pygobject/benzea/tmp] fixup! async: Add a new async type that is an awaitable for a _finish call




commit 9f05178755b63cdb702a17f965f61608894611b4
Author: Benjamin Berg <bberg redhat com>
Date:   Fri Nov 26 15:17:08 2021 +0100

    fixup! async: Add a new async type that is an awaitable for a _finish call

 gi/pygi-async.c | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)
---
diff --git a/gi/pygi-async.c b/gi/pygi-async.c
index d40a609d..c721b11a 100644
--- a/gi/pygi-async.c
+++ b/gi/pygi-async.c
@@ -157,8 +157,7 @@ async_add_done_callback (PyGIAsync *self, PyObject *args, PyObject *kwargs)
     else
         Py_INCREF(callback.context);
 #else
-    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:add_done_callback", NULL,
-                                     &callback.func))
+    if (!PyArg_ParseTuple(args, "O:add_done_callback", &callback.func))
         return NULL;
 #endif
 
@@ -343,7 +342,7 @@ static PyAsyncMethods async_async_methods = {
 };
 
 static void
-async_dealloc(PyGIAsync *self)
+async_finalize(PyGIAsync *self)
 {
     PyObject *error_type, *error_value, *error_traceback;
     PyObject *context = NULL;
@@ -381,7 +380,7 @@ async_dealloc(PyGIAsync *self)
 
     res = PyObject_CallFunction(call_exception_handler, "(O)", context);
     if (res == NULL)
-        PyErr_WriteUnraisable(call_exception_handler);
+        PyErr_WriteUnraisable(context);
 
 finally:
     Py_CLEAR (res);
@@ -402,12 +401,22 @@ finally:
     if (self->callbacks)
         g_array_free (self->callbacks, TRUE);
 
-    PyObject_DEL(self);
-
     /* Restore the saved exception. */
     PyErr_Restore(error_type, error_value, error_traceback);
 }
 
+static void
+async_dealloc(PyGIAsync *self)
+{
+    /* The finalizer might resurrect the object */
+    if (PyObject_CallFinalizerFromDealloc((PyObject *)self) < 0)
+        return;
+
+    PyObject_GC_UnTrack((PyObject *)self);
+
+    Py_TYPE(self)->tp_free((PyObject *)self);
+}
+
 void
 pygi_async_finish_cb (GObject *source_object, gpointer res, PyGIAsync *self)
 {
@@ -576,9 +585,10 @@ static struct PyMemberDef async_members[] = {
 int pygi_async_register_types(PyObject *module) {
     PyObject *asyncio = NULL;
 
+    PyGIAsync_Type.tp_finalize = (destructor)async_finalize;
     PyGIAsync_Type.tp_dealloc = (destructor)async_dealloc;
     PyGIAsync_Type.tp_repr = (reprfunc)async_repr;
-    PyGIAsync_Type.tp_flags = Py_TPFLAGS_DEFAULT;
+    PyGIAsync_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE;
     PyGIAsync_Type.tp_methods = async_methods;
     PyGIAsync_Type.tp_members = async_members;
     PyGIAsync_Type.tp_as_async = &async_async_methods;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]