summaryrefslogtreecommitdiffstats
path: root/Include/internal
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2023-04-06 00:42:02 (GMT)
committerGitHub <noreply@github.com>2023-04-06 00:42:02 (GMT)
commit03089fdccc7dbe3f69227fbd570df92278371e7f (patch)
tree3b5440d14daa9f23689bfb2e07d0730f3839b219 /Include/internal
parent4ec8dd10bd4682793559c4eccbcf6ae00688c4c3 (diff)
downloadcpython-03089fdccc7dbe3f69227fbd570df92278371e7f.zip
cpython-03089fdccc7dbe3f69227fbd570df92278371e7f.tar.gz
cpython-03089fdccc7dbe3f69227fbd570df92278371e7f.tar.bz2
gh-101659: Add _Py_AtExit() (gh-103298)
The function is like Py_AtExit() but for a single interpreter. This is a companion to the atexit module's register() function, taking a C callback instead of a Python one. We also update the _xxinterpchannels module to use _Py_AtExit(), which is the motivating case. (This is inspired by pain points felt while working on gh-101660.)
Diffstat (limited to 'Include/internal')
-rw-r--r--Include/internal/pycore_atexit.h56
-rw-r--r--Include/internal/pycore_interp.h17
-rw-r--r--Include/internal/pycore_runtime.h5
3 files changed, 60 insertions, 18 deletions
diff --git a/Include/internal/pycore_atexit.h b/Include/internal/pycore_atexit.h
new file mode 100644
index 0000000..b4663b3
--- /dev/null
+++ b/Include/internal/pycore_atexit.h
@@ -0,0 +1,56 @@
+#ifndef Py_INTERNAL_ATEXIT_H
+#define Py_INTERNAL_ATEXIT_H
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifndef Py_BUILD_CORE
+# error "this header requires Py_BUILD_CORE define"
+#endif
+
+
+//###############
+// runtime atexit
+
+typedef void (*atexit_callbackfunc)(void);
+
+struct _atexit_runtime_state {
+#define NEXITFUNCS 32
+ atexit_callbackfunc callbacks[NEXITFUNCS];
+ int ncallbacks;
+};
+
+
+//###################
+// interpreter atexit
+
+struct atexit_callback;
+typedef struct atexit_callback {
+ atexit_datacallbackfunc func;
+ void *data;
+ struct atexit_callback *next;
+} atexit_callback;
+
+typedef struct {
+ PyObject *func;
+ PyObject *args;
+ PyObject *kwargs;
+} atexit_py_callback;
+
+struct atexit_state {
+ atexit_callback *ll_callbacks;
+ atexit_callback *last_ll_callback;
+
+ // XXX The rest of the state could be moved to the atexit module state
+ // and a low-level callback added for it during module exec.
+ // For the moment we leave it here.
+ atexit_py_callback **callbacks;
+ int ncallbacks;
+ int callback_len;
+};
+
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* !Py_INTERNAL_ATEXIT_H */
diff --git a/Include/internal/pycore_interp.h b/Include/internal/pycore_interp.h
index 1f2c0db..d64a68c 100644
--- a/Include/internal/pycore_interp.h
+++ b/Include/internal/pycore_interp.h
@@ -10,8 +10,9 @@ extern "C" {
#include <stdbool.h>
-#include "pycore_atomic.h" // _Py_atomic_address
#include "pycore_ast_state.h" // struct ast_state
+#include "pycore_atexit.h" // struct atexit_state
+#include "pycore_atomic.h" // _Py_atomic_address
#include "pycore_ceval_state.h" // struct _ceval_state
#include "pycore_code.h" // struct callable_cache
#include "pycore_context.h" // struct _Py_context_state
@@ -32,20 +33,6 @@ extern "C" {
#include "pycore_warnings.h" // struct _warnings_runtime_state
-// atexit state
-typedef struct {
- PyObject *func;
- PyObject *args;
- PyObject *kwargs;
-} atexit_callback;
-
-struct atexit_state {
- atexit_callback **callbacks;
- int ncallbacks;
- int callback_len;
-};
-
-
struct _Py_long_state {
int max_str_digits;
};
diff --git a/Include/internal/pycore_runtime.h b/Include/internal/pycore_runtime.h
index 8877b28..3ebe499 100644
--- a/Include/internal/pycore_runtime.h
+++ b/Include/internal/pycore_runtime.h
@@ -8,6 +8,7 @@ extern "C" {
# error "this header requires Py_BUILD_CORE define"
#endif
+#include "pycore_atexit.h" // struct atexit_runtime_state
#include "pycore_atomic.h" /* _Py_atomic_address */
#include "pycore_ceval_state.h" // struct _ceval_runtime_state
#include "pycore_floatobject.h" // struct _Py_float_runtime_state
@@ -131,9 +132,7 @@ typedef struct pyruntimestate {
struct _parser_runtime_state parser;
-#define NEXITFUNCS 32
- void (*exitfuncs[NEXITFUNCS])(void);
- int nexitfuncs;
+ struct _atexit_runtime_state atexit;
struct _import_runtime_state imports;
struct _ceval_runtime_state ceval;