Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add pycore_atexit.h.
  • Loading branch information
ericsnowcurrently committed Apr 5, 2023
commit e6d4776ad1861b05bf3b16ac74151ee33a583c83
39 changes: 39 additions & 0 deletions Include/internal/pycore_atexit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#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


typedef void (*atexit_callbackfunc)(void);


typedef struct {
PyObject *func;
PyObject *args;
PyObject *kwargs;
} atexit_py_callback;

struct atexit_state {
atexit_py_callback **callbacks;
int ncallbacks;
int callback_len;
};



struct _atexit_runtime_state {
#define NEXITFUNCS 32
atexit_callbackfunc callbacks[NEXITFUNCS];
int ncallbacks;
};


#ifdef __cplusplus
}
#endif
#endif /* !Py_INTERNAL_ATEXIT_H */
17 changes: 2 additions & 15 deletions Include/internal/pycore_interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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_py_callback;

struct atexit_state {
atexit_py_callback **callbacks;
int ncallbacks;
int callback_len;
};


struct _Py_long_state {
int max_str_digits;
};
Expand Down
5 changes: 2 additions & 3 deletions Include/internal/pycore_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -1660,6 +1660,7 @@ PYTHON_HEADERS= \
$(srcdir)/Include/internal/pycore_asdl.h \
$(srcdir)/Include/internal/pycore_ast.h \
$(srcdir)/Include/internal/pycore_ast_state.h \
$(srcdir)/Include/internal/pycore_atexit.h \
$(srcdir)/Include/internal/pycore_atomic.h \
$(srcdir)/Include/internal/pycore_atomic_funcs.h \
$(srcdir)/Include/internal/pycore_bitutils.h \
Expand Down
1 change: 1 addition & 0 deletions Modules/atexitmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

#include "Python.h"
#include "pycore_atexit.h"
#include "pycore_initconfig.h" // _PyStatus_NO_MEMORY
#include "pycore_interp.h" // PyInterpreterState.atexit
#include "pycore_pystate.h" // _PyInterpreterState_GET
Expand Down
1 change: 1 addition & 0 deletions PCbuild/pythoncore.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@
<ClInclude Include="..\Include\internal\pycore_asdl.h" />
<ClInclude Include="..\Include\internal\pycore_ast.h" />
<ClInclude Include="..\Include\internal\pycore_ast_state.h" />
<ClInclude Include="..\Include\internal\pycore_atexit.h" />
<ClInclude Include="..\Include\internal\pycore_atomic.h" />
<ClInclude Include="..\Include\internal\pycore_atomic_funcs.h" />
<ClInclude Include="..\Include\internal\pycore_bitutils.h" />
Expand Down
3 changes: 3 additions & 0 deletions PCbuild/pythoncore.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,9 @@
<ClInclude Include="..\Include\internal\pycore_ast_state.h">
<Filter>Include\internal</Filter>
</ClInclude>
<ClInclude Include="..\Include\internal\pycore_atexit.h">
<Filter>Include\internal</Filter>
</ClInclude>
<ClInclude Include="..\Include\internal\pycore_atomic.h">
<Filter>Include\internal</Filter>
</ClInclude>
Expand Down
14 changes: 7 additions & 7 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -2937,23 +2937,23 @@ wait_for_thread_shutdown(PyThreadState *tstate)
Py_DECREF(threading);
}

#define NEXITFUNCS 32
int Py_AtExit(void (*func)(void))
{
if (_PyRuntime.nexitfuncs >= NEXITFUNCS)
if (_PyRuntime.atexit.ncallbacks >= NEXITFUNCS)
return -1;
_PyRuntime.exitfuncs[_PyRuntime.nexitfuncs++] = func;
_PyRuntime.atexit.callbacks[_PyRuntime.atexit.ncallbacks++] = func;
return 0;
}

static void
call_ll_exitfuncs(_PyRuntimeState *runtime)
{
while (runtime->nexitfuncs > 0) {
struct _atexit_runtime_state *state = &runtime->atexit;
while (state->ncallbacks > 0) {
/* pop last function from the list */
runtime->nexitfuncs--;
void (*exitfunc)(void) = runtime->exitfuncs[runtime->nexitfuncs];
runtime->exitfuncs[runtime->nexitfuncs] = NULL;
state->ncallbacks--;
atexit_callbackfunc exitfunc = state->callbacks[state->ncallbacks];
state->callbacks[state->ncallbacks] = NULL;

exitfunc();
}
Expand Down