Mercurial > cpython-lru_cache-weakref
annotate Modules/atexitmodule.c @ 70817:dd775b1561ba
Fix markup.
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Tue, 14 Jun 2011 21:09:55 +0200 |
| parents | 4a82be47a948 |
| children | 7c48bb929e6e |
| rev | line source |
|---|---|
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
1 /* |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
2 * atexit - allow programmer to define multiple exit functions to be executed |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
3 * upon normal program termination. |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
4 * |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
5 * Translated from atexit.py by Collin Winter. |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
6 + Copyright 2007 Python Software Foundation. |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
7 */ |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
8 |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
9 #include "Python.h" |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
10 |
|
41294
ef70cd4dba50
Address some XXXs from Neal Norwitz.
Collin Winter <collinw@gmail.com>
parents:
41254
diff
changeset
|
11 /* Forward declaration (for atexit_cleanup) */ |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
12 static PyObject *atexit_clear(PyObject*, PyObject*); |
|
49276
0de5a388a261
Fix for issue 3666 - atexit.register with bad inputs segfaults on exit.
Skip Montanaro <skip@pobox.com>
parents:
47580
diff
changeset
|
13 /* Forward declaration (for atexit_callfuncs) */ |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
14 static void atexit_cleanup(PyObject*); |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
15 /* Forward declaration of module object */ |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
16 static struct PyModuleDef atexitmodule; |
|
41294
ef70cd4dba50
Address some XXXs from Neal Norwitz.
Collin Winter <collinw@gmail.com>
parents:
41254
diff
changeset
|
17 |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
18 /* ===================================================================== */ |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
19 /* Callback machinery. */ |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
20 |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
21 typedef struct { |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
22 PyObject *func; |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
23 PyObject *args; |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
24 PyObject *kwargs; |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
25 } atexit_callback; |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
26 |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
27 typedef struct { |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
28 atexit_callback **atexit_callbacks; |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
29 int ncallbacks; |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
30 int callback_len; |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
31 } atexitmodule_state; |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
32 |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
33 #define GET_ATEXIT_STATE(mod) ((atexitmodule_state*)PyModule_GetState(mod)) |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
34 |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
35 |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
36 /* Installed into pythonrun.c's atexit mechanism */ |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
37 |
|
49276
0de5a388a261
Fix for issue 3666 - atexit.register with bad inputs segfaults on exit.
Skip Montanaro <skip@pobox.com>
parents:
47580
diff
changeset
|
38 static void |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
39 atexit_callfuncs(void) |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
40 { |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
41 PyObject *exc_type = NULL, *exc_value, *exc_tb, *r; |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
42 atexit_callback *cb; |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
43 PyObject *module; |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
44 atexitmodule_state *modstate; |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
45 int i; |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
46 |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
47 module = PyState_FindModule(&atexitmodule); |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
48 if (module == NULL) |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
49 return; |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
50 modstate = GET_ATEXIT_STATE(module); |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
51 |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
52 if (modstate->ncallbacks == 0) |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
53 return; |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
54 |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
55 |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
56 for (i = modstate->ncallbacks - 1; i >= 0; i--) |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
57 { |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
58 cb = modstate->atexit_callbacks[i]; |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
59 if (cb == NULL) |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
60 continue; |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
61 |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
62 r = PyObject_Call(cb->func, cb->args, cb->kwargs); |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
63 Py_XDECREF(r); |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
64 if (r == NULL) { |
| 41254 | 65 /* Maintain the last exception, but don't leak if there are |
| 66 multiple exceptions. */ | |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
67 if (exc_type) { |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
68 Py_DECREF(exc_type); |
| 41254 | 69 Py_XDECREF(exc_value); |
| 70 Py_XDECREF(exc_tb); | |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
71 } |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
72 PyErr_Fetch(&exc_type, &exc_value, &exc_tb); |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
73 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) { |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
74 PySys_WriteStderr("Error in atexit._run_exitfuncs:\n"); |
|
67369
4a82be47a948
Issue #10756: atexit normalizes the exception before displaying it.
Victor Stinner <victor.stinner@haypocalc.com>
parents:
49610
diff
changeset
|
75 PyErr_NormalizeException(&exc_type, &exc_value, &exc_tb); |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
76 PyErr_Display(exc_type, exc_value, exc_tb); |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
77 } |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
78 } |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
79 } |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
80 |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
81 atexit_cleanup(module); |
|
49276
0de5a388a261
Fix for issue 3666 - atexit.register with bad inputs segfaults on exit.
Skip Montanaro <skip@pobox.com>
parents:
47580
diff
changeset
|
82 |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
83 if (exc_type) |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
84 PyErr_Restore(exc_type, exc_value, exc_tb); |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
85 } |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
86 |
|
49276
0de5a388a261
Fix for issue 3666 - atexit.register with bad inputs segfaults on exit.
Skip Montanaro <skip@pobox.com>
parents:
47580
diff
changeset
|
87 static void |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
88 atexit_delete_cb(PyObject *self, int i) |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
89 { |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
90 atexitmodule_state *modstate; |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
91 atexit_callback *cb; |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
92 |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
93 modstate = GET_ATEXIT_STATE(self); |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
94 cb = modstate->atexit_callbacks[i]; |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
95 modstate->atexit_callbacks[i] = NULL; |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
96 Py_DECREF(cb->func); |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
97 Py_DECREF(cb->args); |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
98 Py_XDECREF(cb->kwargs); |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
99 PyMem_Free(cb); |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
100 } |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
101 |
|
49276
0de5a388a261
Fix for issue 3666 - atexit.register with bad inputs segfaults on exit.
Skip Montanaro <skip@pobox.com>
parents:
47580
diff
changeset
|
102 static void |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
103 atexit_cleanup(PyObject *self) |
|
41294
ef70cd4dba50
Address some XXXs from Neal Norwitz.
Collin Winter <collinw@gmail.com>
parents:
41254
diff
changeset
|
104 { |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
105 PyObject *r = atexit_clear(self, NULL); |
|
41294
ef70cd4dba50
Address some XXXs from Neal Norwitz.
Collin Winter <collinw@gmail.com>
parents:
41254
diff
changeset
|
106 Py_DECREF(r); |
|
ef70cd4dba50
Address some XXXs from Neal Norwitz.
Collin Winter <collinw@gmail.com>
parents:
41254
diff
changeset
|
107 } |
|
ef70cd4dba50
Address some XXXs from Neal Norwitz.
Collin Winter <collinw@gmail.com>
parents:
41254
diff
changeset
|
108 |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
109 /* ===================================================================== */ |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
110 /* Module methods. */ |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
111 |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
112 PyDoc_STRVAR(atexit_register__doc__, |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
113 "register(func, *args, **kwargs) -> func\n\ |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
114 \n\ |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
115 Register a function to be executed upon normal program termination\n\ |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
116 \n\ |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
117 func - function to be called at exit\n\ |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
118 args - optional arguments to pass to func\n\ |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
119 kwargs - optional keyword arguments to pass to func\n\ |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
120 \n\ |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
121 func is returned to facilitate usage as a decorator."); |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
122 |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
123 static PyObject * |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
124 atexit_register(PyObject *self, PyObject *args, PyObject *kwargs) |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
125 { |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
126 atexitmodule_state *modstate; |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
127 atexit_callback *new_callback; |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
128 PyObject *func = NULL; |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
129 |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
130 modstate = GET_ATEXIT_STATE(self); |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
131 |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
132 if (modstate->ncallbacks >= modstate->callback_len) { |
|
41294
ef70cd4dba50
Address some XXXs from Neal Norwitz.
Collin Winter <collinw@gmail.com>
parents:
41254
diff
changeset
|
133 atexit_callback **r; |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
134 modstate->callback_len += 16; |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
135 r = (atexit_callback**)PyMem_Realloc(modstate->atexit_callbacks, |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
136 sizeof(atexit_callback*) * modstate->callback_len); |
|
41294
ef70cd4dba50
Address some XXXs from Neal Norwitz.
Collin Winter <collinw@gmail.com>
parents:
41254
diff
changeset
|
137 if (r == NULL) |
|
ef70cd4dba50
Address some XXXs from Neal Norwitz.
Collin Winter <collinw@gmail.com>
parents:
41254
diff
changeset
|
138 return PyErr_NoMemory(); |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
139 modstate->atexit_callbacks = r; |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
140 } |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
141 |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
142 if (PyTuple_GET_SIZE(args) == 0) { |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
143 PyErr_SetString(PyExc_TypeError, |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
144 "register() takes at least 1 argument (0 given)"); |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
145 return NULL; |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
146 } |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
147 |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
148 func = PyTuple_GET_ITEM(args, 0); |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
149 if (!PyCallable_Check(func)) { |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
150 PyErr_SetString(PyExc_TypeError, |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
151 "the first argument must be callable"); |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
152 return NULL; |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
153 } |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
154 |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
155 new_callback = PyMem_Malloc(sizeof(atexit_callback)); |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
156 if (new_callback == NULL) |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
157 return PyErr_NoMemory(); |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
158 |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
159 new_callback->args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
160 if (new_callback->args == NULL) { |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
161 PyMem_Free(new_callback); |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
162 return NULL; |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
163 } |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
164 new_callback->func = func; |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
165 new_callback->kwargs = kwargs; |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
166 Py_INCREF(func); |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
167 Py_XINCREF(kwargs); |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
168 |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
169 modstate->atexit_callbacks[modstate->ncallbacks++] = new_callback; |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
170 |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
171 Py_INCREF(func); |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
172 return func; |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
173 } |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
174 |
| 42464 | 175 PyDoc_STRVAR(atexit_run_exitfuncs__doc__, |
| 176 "_run_exitfuncs() -> None\n\ | |
| 177 \n\ | |
| 178 Run all registered exit functions."); | |
| 179 | |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
180 static PyObject * |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
181 atexit_run_exitfuncs(PyObject *self, PyObject *unused) |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
182 { |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
183 atexit_callfuncs(); |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
184 if (PyErr_Occurred()) |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
185 return NULL; |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
186 Py_RETURN_NONE; |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
187 } |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
188 |
| 42464 | 189 PyDoc_STRVAR(atexit_clear__doc__, |
| 190 "_clear() -> None\n\ | |
| 191 \n\ | |
| 192 Clear the list of previously registered exit functions."); | |
| 193 | |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
194 static PyObject * |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
195 atexit_clear(PyObject *self, PyObject *unused) |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
196 { |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
197 atexitmodule_state *modstate; |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
198 atexit_callback *cb; |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
199 int i; |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
200 |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
201 modstate = GET_ATEXIT_STATE(self); |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
202 |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
203 for (i = 0; i < modstate->ncallbacks; i++) { |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
204 cb = modstate->atexit_callbacks[i]; |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
205 if (cb == NULL) |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
206 continue; |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
207 |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
208 atexit_delete_cb(self, i); |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
209 } |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
210 modstate->ncallbacks = 0; |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
211 Py_RETURN_NONE; |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
212 } |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
213 |
| 42464 | 214 PyDoc_STRVAR(atexit_unregister__doc__, |
| 215 "unregister(func) -> None\n\ | |
| 216 \n\ | |
| 217 Unregister a exit function which was previously registered using\n\ | |
| 218 atexit.register\n\ | |
| 219 \n\ | |
| 220 func - function to be unregistered"); | |
| 221 | |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
222 static PyObject * |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
223 atexit_unregister(PyObject *self, PyObject *func) |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
224 { |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
225 atexitmodule_state *modstate; |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
226 atexit_callback *cb; |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
227 int i, eq; |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
228 |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
229 modstate = GET_ATEXIT_STATE(self); |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
230 |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
231 for (i = 0; i < modstate->ncallbacks; i++) |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
232 { |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
233 cb = modstate->atexit_callbacks[i]; |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
234 if (cb == NULL) |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
235 continue; |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
236 |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
237 eq = PyObject_RichCompareBool(cb->func, func, Py_EQ); |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
238 if (eq < 0) |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
239 return NULL; |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
240 if (eq) |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
241 atexit_delete_cb(self, i); |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
242 } |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
243 Py_RETURN_NONE; |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
244 } |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
245 |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
246 static PyMethodDef atexit_methods[] = { |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
247 {"register", (PyCFunction) atexit_register, METH_VARARGS|METH_KEYWORDS, |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
248 atexit_register__doc__}, |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
249 {"_clear", (PyCFunction) atexit_clear, METH_NOARGS, |
| 42464 | 250 atexit_clear__doc__}, |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
251 {"unregister", (PyCFunction) atexit_unregister, METH_O, |
| 42464 | 252 atexit_unregister__doc__}, |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
253 {"_run_exitfuncs", (PyCFunction) atexit_run_exitfuncs, METH_NOARGS, |
| 42464 | 254 atexit_run_exitfuncs__doc__}, |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
255 {NULL, NULL} /* sentinel */ |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
256 }; |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
257 |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
258 /* ===================================================================== */ |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
259 /* Initialization function. */ |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
260 |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
261 PyDoc_STRVAR(atexit__doc__, |
| 42464 | 262 "allow programmer to define multiple exit functions to be executed\ |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
263 upon normal program termination.\n\ |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
264 \n\ |
| 42464 | 265 Two public functions, register and unregister, are defined.\n\ |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
266 "); |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
267 |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
42464
diff
changeset
|
268 |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
42464
diff
changeset
|
269 static struct PyModuleDef atexitmodule = { |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
42464
diff
changeset
|
270 PyModuleDef_HEAD_INIT, |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
42464
diff
changeset
|
271 "atexit", |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
42464
diff
changeset
|
272 atexit__doc__, |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
273 sizeof(atexitmodule_state), |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
42464
diff
changeset
|
274 atexit_methods, |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
42464
diff
changeset
|
275 NULL, |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
42464
diff
changeset
|
276 NULL, |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
42464
diff
changeset
|
277 NULL, |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
42464
diff
changeset
|
278 NULL |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
42464
diff
changeset
|
279 }; |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
42464
diff
changeset
|
280 |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
281 PyMODINIT_FUNC |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
42464
diff
changeset
|
282 PyInit_atexit(void) |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
283 { |
|
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
284 PyObject *m; |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
285 atexitmodule_state *modstate; |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
286 |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
42464
diff
changeset
|
287 m = PyModule_Create(&atexitmodule); |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
288 if (m == NULL) |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
42464
diff
changeset
|
289 return NULL; |
|
49610
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
290 |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
291 modstate = GET_ATEXIT_STATE(m); |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
292 modstate->callback_len = 32; |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
293 modstate->ncallbacks = 0; |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
294 modstate->atexit_callbacks = PyMem_New(atexit_callback*, |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
295 modstate->callback_len); |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
296 if (modstate->atexit_callbacks == NULL) |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
297 return NULL; |
|
d2ec6d5b2db9
Issue #4200: Changed the atexit module to store its state in its PyModuleDef atexitmodule. This fixes a bug with multiple subinterpeters. The bug was found by Graham Dumpletom during his work on a 3.0 port of mod_wsgi. The patch has been reviewed by Benjamin.
Christian Heimes <christian@cheimes.de>
parents:
49276
diff
changeset
|
298 |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
299 _Py_PyAtExit(atexit_callfuncs); |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
42464
diff
changeset
|
300 return m; |
|
41253
f29f1a8b7869
Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents:
diff
changeset
|
301 } |
