FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpython/Modules/atexitmodule.c at main · python/cpython · GitHub
python
/
cpython
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
37.4k
Star
77.5k
Code
Issues
5k+
Pull requests
2.7k
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpython
/
Modules
/
atexitmodule.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
358 lines (304 loc) · 9.64 KB
Breadcrumbs
cpython
/
Modules
/
atexitmodule.c
Copy path
File metadata and controls
358 lines (304 loc) · 9.64 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* atexit - allow programmer to define multiple exit functions to be executed
* upon normal program termination.
*
* Translated from atexit.py by Collin Winter.
+ Copyright 2007 Python Software Foundation.
*/
#include
"Python.h"
#include
"pycore_atexit.h"
// export _Py_AtExit()
#include
"pycore_initconfig.h"
// _PyStatus_NO_MEMORY
#include
"pycore_interp.h"
// PyInterpreterState.atexit
#include
"pycore_pystate.h"
// _PyInterpreterState_GET
/*[clinic input]
module atexit
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ac8fd67d15bf23fc]*/
#include
"clinic/atexitmodule.c.h"
/* ===================================================================== */
/* Callback machinery. */
static
inline
struct
atexit_state
*
get_atexit_state
(
void
)
{
PyInterpreterState
*
interp
=
_PyInterpreterState_GET
();
return
&
interp
->
atexit
;
}
int
PyUnstable_AtExit
(
PyInterpreterState
*
interp
,
atexit_datacallbackfunc
func
,
void
*
data
)
{
PyThreadState
*
tstate
=
_PyThreadState_GET
();
_Py_EnsureTstateNotNULL
(
tstate
);
assert
(
tstate
->
interp
==
interp
);
atexit_callback
*
callback
=
PyMem_Malloc
(
sizeof
(
atexit_callback
));
if
(
callback
==
NULL
) {
PyErr_NoMemory
();
return
-1
;
}
callback
->
func
=
func
;
callback
->
data
=
data
;
callback
->
next
=
NULL
;
struct
atexit_state
*
state
=
&
interp
->
atexit
;
_PyAtExit_LockCallbacks
(
state
);
atexit_callback
*
top
=
state
->
ll_callbacks
;
if
(
top
==
NULL
) {
state
->
ll_callbacks
=
callback
;
}
else
{
callback
->
next
=
top
;
state
->
ll_callbacks
=
callback
;
}
_PyAtExit_UnlockCallbacks
(
state
);
return
0
;
}
/* Clear all callbacks without calling them */
static
void
atexit_cleanup
(
struct
atexit_state
*
state
)
{
PyList_Clear
(
state
->
callbacks
);
}
PyStatus
_PyAtExit_Init
(
PyInterpreterState
*
interp
)
{
struct
atexit_state
*
state
=
&
interp
->
atexit
;
// _PyAtExit_Init() must only be called once
assert
(
state
->
callbacks
==
NULL
);
state
->
callbacks
=
PyList_New
(
0
);
if
(
state
->
callbacks
==
NULL
) {
return
_PyStatus_NO_MEMORY
();
}
return
_PyStatus_OK
();
}
void
_PyAtExit_Fini
(
PyInterpreterState
*
interp
)
{
// In theory, there shouldn't be any threads left by now, so we
// won't lock this.
struct
atexit_state
*
state
=
&
interp
->
atexit
;
atexit_cleanup
(
state
);
Py_CLEAR
(
state
->
callbacks
);
atexit_callback
*
next
=
state
->
ll_callbacks
;
state
->
ll_callbacks
=
NULL
;
while
(
next
!=
NULL
) {
atexit_callback
*
callback
=
next
;
next
=
callback
->
next
;
atexit_datacallbackfunc
exitfunc
=
callback
->
func
;
void
*
data
=
callback
->
data
;
// It was allocated in _PyAtExit_AddCallback().
PyMem_Free
(
callback
);
exitfunc
(
data
);
}
}
static
void
atexit_callfuncs
(
struct
atexit_state
*
state
)
{
assert
(!
PyErr_Occurred
());
assert
(
state
->
callbacks
!=
NULL
);
assert
(
PyList_CheckExact
(
state
->
callbacks
));
// Create a copy of the list for thread safety
PyObject
*
copy
=
PyList_GetSlice
(
state
->
callbacks
,
0
,
PyList_GET_SIZE
(
state
->
callbacks
));
if
(
copy
==
NULL
)
{
PyErr_FormatUnraisable
(
"Exception ignored while "
"copying atexit callbacks"
);
atexit_cleanup
(
state
);
return
;
}
for
(
Py_ssize_t
i
=
0
;
i
<
PyList_GET_SIZE
(
copy
);
++
i
) {
// We don't have to worry about evil borrowed references, because
// no other threads can access this list.
PyObject
*
tuple
=
PyList_GET_ITEM
(
copy
,
i
);
assert
(
PyTuple_CheckExact
(
tuple
));
PyObject
*
func
=
PyTuple_GET_ITEM
(
tuple
,
0
);
PyObject
*
args
=
PyTuple_GET_ITEM
(
tuple
,
1
);
PyObject
*
kwargs
=
PyTuple_GET_ITEM
(
tuple
,
2
);
PyObject
*
res
=
PyObject_Call
(
func
,
args
,
kwargs
==
Py_None
?
NULL
:
kwargs
);
if
(
res
==
NULL
) {
PyErr_FormatUnraisable
(
"Exception ignored in atexit callback %R"
,
func
);
}
else
{
Py_DECREF
(
res
);
}
}
Py_DECREF
(
copy
);
atexit_cleanup
(
state
);
assert
(!
PyErr_Occurred
());
}
void
_PyAtExit_Call
(
PyInterpreterState
*
interp
)
{
struct
atexit_state
*
state
=
&
interp
->
atexit
;
atexit_callfuncs
(
state
);
}
/* ===================================================================== */
/* Module methods. */
/*[clinic input]
atexit.register
func: object
/
*args: tuple
**kwargs: dict
Register a function to be executed upon normal program termination
func - function to be called at exit
args - optional arguments to pass to func
kwargs - optional keyword arguments to pass to func
func is returned to facilitate usage as a decorator.
[clinic start generated code]*/
static
PyObject
*
atexit_register_impl
(
PyObject
*
module
,
PyObject
*
func
,
PyObject
*
args
,
PyObject
*
kwargs
)
/*[clinic end generated code: output=c921286994bd8612 input=a682b5a343a82e4a]*/
{
if
(!
PyCallable_Check
(
func
)) {
PyErr_SetString
(
PyExc_TypeError
,
"the first argument must be callable"
);
return
NULL
;
}
PyObject
*
callback
=
PyTuple_Pack
(
3
,
func
,
args
,
kwargs
);
if
(
callback
==
NULL
)
{
return
NULL
;
}
struct
atexit_state
*
state
=
get_atexit_state
();
// atexit callbacks go in a LIFO order
if
(
PyList_Insert
(
state
->
callbacks
,
0
,
callback
)
<
0
)
{
Py_DECREF
(
callback
);
return
NULL
;
}
Py_DECREF
(
callback
);
return
Py_NewRef
(
func
);
}
PyDoc_STRVAR
(
atexit_run_exitfuncs__doc__
,
"_run_exitfuncs($module, /)\n\
--\n\
\n\
Run all registered exit functions.\n\
\n\
If a callback raises an exception, it is logged with sys.unraisablehook."
);
static
PyObject
*
atexit_run_exitfuncs
(
PyObject
*
module
,
PyObject
*
Py_UNUSED
(
dummy
))
{
struct
atexit_state
*
state
=
get_atexit_state
();
atexit_callfuncs
(
state
);
Py_RETURN_NONE
;
}
PyDoc_STRVAR
(
atexit_clear__doc__
,
"_clear($module, /)\n\
--\n\
\n\
Clear the list of previously registered exit functions."
);
static
PyObject
*
atexit_clear
(
PyObject
*
module
,
PyObject
*
Py_UNUSED
(
dummy
))
{
atexit_cleanup
(
get_atexit_state
());
Py_RETURN_NONE
;
}
PyDoc_STRVAR
(
atexit_ncallbacks__doc__
,
"_ncallbacks($module, /)\n\
--\n\
\n\
Return the number of registered exit functions."
);
static
PyObject
*
atexit_ncallbacks
(
PyObject
*
module
,
PyObject
*
Py_UNUSED
(
dummy
))
{
struct
atexit_state
*
state
=
get_atexit_state
();
assert
(
state
->
callbacks
!=
NULL
);
assert
(
PyList_CheckExact
(
state
->
callbacks
));
return
PyLong_FromSsize_t
(
PyList_GET_SIZE
(
state
->
callbacks
));
}
static
int
atexit_unregister_locked
(
PyObject
*
callbacks
,
PyObject
*
func
)
{
for
(
Py_ssize_t
i
=
PyList_GET_SIZE
(
callbacks
)
-
1
;
i
>=
0
;
--
i
) {
PyObject
*
tuple
=
Py_NewRef
(
PyList_GET_ITEM
(
callbacks
,
i
));
assert
(
PyTuple_CheckExact
(
tuple
));
PyObject
*
to_compare
=
PyTuple_GET_ITEM
(
tuple
,
0
);
int
cmp
=
PyObject_RichCompareBool
(
func
,
to_compare
,
Py_EQ
);
if
(
cmp
<
0
) {
Py_DECREF
(
tuple
);
return
-1
;
}
if
(
cmp
==
1
) {
// We found a callback!
// But its index could have changed if it or other callbacks were
// unregistered during the comparison.
Py_ssize_t
j
=
PyList_GET_SIZE
(
callbacks
)
-
1
;
j
=
Py_MIN
(
j
,
i
);
for
(;
j
>=
0
;
--
j
) {
if
(
PyList_GET_ITEM
(
callbacks
,
j
)
==
tuple
) {
// We found the callback index! For real!
if
(
PyList_SetSlice
(
callbacks
,
j
,
j
+
1
,
NULL
)
<
0
) {
Py_DECREF
(
tuple
);
return
-1
;
}
i
=
j
;
break
;
}
}
}
Py_DECREF
(
tuple
);
if
(
i
>=
PyList_GET_SIZE
(
callbacks
)) {
i
=
PyList_GET_SIZE
(
callbacks
);
}
}
return
0
;
}
PyDoc_STRVAR
(
atexit_unregister__doc__
,
"unregister($module, func, /)\n\
--\n\
\n\
Unregister an exit function which was previously registered using\n\
atexit.register\n\
\n\
func - function to be unregistered"
);
static
PyObject
*
atexit_unregister
(
PyObject
*
module
,
PyObject
*
func
)
{
struct
atexit_state
*
state
=
get_atexit_state
();
int
result
;
Py_BEGIN_CRITICAL_SECTION
(
state
->
callbacks
);
result
=
atexit_unregister_locked
(
state
->
callbacks
,
func
);
Py_END_CRITICAL_SECTION
();
return
result
<
0
?
NULL
:
Py_None
;
}
static
PyMethodDef
atexit_methods
[]
=
{
ATEXIT_REGISTER_METHODDEF
{
"_clear"
,
atexit_clear
,
METH_NOARGS
,
atexit_clear__doc__
},
{
"unregister"
,
atexit_unregister
,
METH_O
,
atexit_unregister__doc__
},
{
"_run_exitfuncs"
,
atexit_run_exitfuncs
,
METH_NOARGS
,
atexit_run_exitfuncs__doc__
},
{
"_ncallbacks"
,
atexit_ncallbacks
,
METH_NOARGS
,
atexit_ncallbacks__doc__
},
{
NULL
,
NULL
}
/* sentinel */
};
/* ===================================================================== */
/* Initialization function. */
PyDoc_STRVAR
(
atexit__doc__
,
"allow programmer to define multiple exit functions to be executed\n\
upon normal program termination.\n\
\n\
Two public functions, register and unregister, are defined.\n\
"
);
static
PyModuleDef_Slot
atexitmodule_slots
[]
=
{
_Py_ABI_SLOT
,
{
Py_mod_multiple_interpreters
,
Py_MOD_PER_INTERPRETER_GIL_SUPPORTED
},
{
Py_mod_gil
,
Py_MOD_GIL_NOT_USED
},
{
0
,
NULL
}
};
static
struct
PyModuleDef
atexitmodule
=
{
PyModuleDef_HEAD_INIT
,
.
m_name
=
"atexit"
,
.
m_doc
=
atexit__doc__
,
.
m_size
=
0
,
.
m_methods
=
atexit_methods
,
.
m_slots
=
atexitmodule_slots
,
};
PyMODINIT_FUNC
PyInit_atexit
(
void
)
{
return
PyModuleDef_Init
(
&
atexitmodule
);
}
Back
|
FazBrowse Home
|
New Git URL