|
|
f954f3 |
From 17a912a449fa75b5c12ac3acab596b476699c671 Mon Sep 17 00:00:00 2001
|
|
|
f954f3 |
From: "Richard W.M. Jones" <rjones@redhat.com>
|
|
|
f954f3 |
Date: Tue, 3 Aug 2021 14:19:38 +0100
|
|
|
f954f3 |
Subject: [PATCH] python: Implement .cleanup() method
|
|
|
f954f3 |
|
|
|
f954f3 |
(cherry picked from commit f2fe99e4b0f54467ab8028eaf2d039cf918b2961)
|
|
|
f954f3 |
---
|
|
|
f954f3 |
plugins/python/nbdkit-python-plugin.pod | 20 +++++++++++++++++---
|
|
|
f954f3 |
plugins/python/plugin.c | 19 +++++++++++++++++++
|
|
|
f954f3 |
2 files changed, 36 insertions(+), 3 deletions(-)
|
|
|
f954f3 |
|
|
|
f954f3 |
diff --git a/plugins/python/nbdkit-python-plugin.pod b/plugins/python/nbdkit-python-plugin.pod
|
|
|
f954f3 |
index 6f5f2c00..a92a557f 100644
|
|
|
f954f3 |
--- a/plugins/python/nbdkit-python-plugin.pod
|
|
|
f954f3 |
+++ b/plugins/python/nbdkit-python-plugin.pod
|
|
|
f954f3 |
@@ -257,6 +257,12 @@ There are no arguments or return value.
|
|
|
f954f3 |
|
|
|
f954f3 |
There are no arguments or return value.
|
|
|
f954f3 |
|
|
|
f954f3 |
+=item C<cleanup>
|
|
|
f954f3 |
+
|
|
|
f954f3 |
+(Optional, nbdkit E<ge> 1.28)
|
|
|
f954f3 |
+
|
|
|
f954f3 |
+There are no arguments or return value.
|
|
|
f954f3 |
+
|
|
|
f954f3 |
=item C<list_exports>
|
|
|
f954f3 |
|
|
|
f954f3 |
(Optional)
|
|
|
f954f3 |
@@ -498,10 +504,18 @@ optionally using C<nbdkit.set_error> first.
|
|
|
f954f3 |
|
|
|
f954f3 |
=over 4
|
|
|
f954f3 |
|
|
|
f954f3 |
-=item Missing: C<load> and C<unload>
|
|
|
f954f3 |
+=item Missing: C<load>
|
|
|
f954f3 |
|
|
|
f954f3 |
-These are not needed because you can just use ordinary Python
|
|
|
f954f3 |
-constructs.
|
|
|
f954f3 |
+This is not needed since you can use regular Python mechanisms like
|
|
|
f954f3 |
+top level statements to run code when the module is loaded.
|
|
|
f954f3 |
+
|
|
|
f954f3 |
+=item Missing: C<unload>
|
|
|
f954f3 |
+
|
|
|
f954f3 |
+This is missing, but in nbdkit E<ge> 1.28 you can put code in the
|
|
|
f954f3 |
+C<cleanup()> function to have it run when nbdkit exits. In earlier
|
|
|
f954f3 |
+versions of nbdkit, using a Python
|
|
|
f954f3 |
+L<atexit|https://docs.python.org/3/library/atexit.html> handler is
|
|
|
f954f3 |
+recommended.
|
|
|
f954f3 |
|
|
|
f954f3 |
=item Missing:
|
|
|
f954f3 |
C<name>,
|
|
|
f954f3 |
diff --git a/plugins/python/plugin.c b/plugins/python/plugin.c
|
|
|
f954f3 |
index 64430a1a..f85512b4 100644
|
|
|
f954f3 |
--- a/plugins/python/plugin.c
|
|
|
f954f3 |
+++ b/plugins/python/plugin.c
|
|
|
f954f3 |
@@ -298,6 +298,24 @@ py_after_fork (void)
|
|
|
f954f3 |
return 0;
|
|
|
f954f3 |
}
|
|
|
f954f3 |
|
|
|
f954f3 |
+static void
|
|
|
f954f3 |
+py_cleanup (void)
|
|
|
f954f3 |
+{
|
|
|
f954f3 |
+ ACQUIRE_PYTHON_GIL_FOR_CURRENT_SCOPE;
|
|
|
f954f3 |
+ PyObject *fn;
|
|
|
f954f3 |
+ PyObject *r;
|
|
|
f954f3 |
+
|
|
|
f954f3 |
+ if (callback_defined ("cleanup", &fn)) {
|
|
|
f954f3 |
+ PyErr_Clear ();
|
|
|
f954f3 |
+
|
|
|
f954f3 |
+ r = PyObject_CallObject (fn, NULL);
|
|
|
f954f3 |
+ Py_DECREF (fn);
|
|
|
f954f3 |
+ if (check_python_failure ("cleanup") == -1)
|
|
|
f954f3 |
+ return;
|
|
|
f954f3 |
+ Py_DECREF (r);
|
|
|
f954f3 |
+ }
|
|
|
f954f3 |
+}
|
|
|
f954f3 |
+
|
|
|
f954f3 |
static int
|
|
|
f954f3 |
py_list_exports (int readonly, int is_tls, struct nbdkit_exports *exports)
|
|
|
f954f3 |
{
|
|
|
f954f3 |
@@ -1039,6 +1057,7 @@ static struct nbdkit_plugin plugin = {
|
|
|
f954f3 |
.thread_model = py_thread_model,
|
|
|
f954f3 |
.get_ready = py_get_ready,
|
|
|
f954f3 |
.after_fork = py_after_fork,
|
|
|
f954f3 |
+ .cleanup = py_cleanup,
|
|
|
f954f3 |
.list_exports = py_list_exports,
|
|
|
f954f3 |
.default_export = py_default_export,
|
|
|
f954f3 |
|
|
|
f954f3 |
--
|
|
|
f954f3 |
2.31.1
|
|
|
f954f3 |
|