| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 92d5fba commit b4ce1fc
18 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,7 +25,7 @@ Initializing and finalizing the interpreter | |||
| 25 | 25 | triple: module; search; path | |
| 26 | 26 | single: PySys_SetArgv() | |
| 27 | 27 | single: PySys_SetArgvEx() | |
| 28 | - single: Py_Finalize() | ||
| 28 | + single: Py_FinalizeEx() | ||
| 29 | 29 | ||
| 30 | 30 | Initialize the Python interpreter. In an application embedding Python, this | |
| 31 | 31 | should be called before using any other Python/C API functions; with the | |
@@ -34,7 +34,7 @@ Initializing and finalizing the interpreter | |||
| 34 | 34 | modules :mod:`builtins`, :mod:`__main__` and :mod:`sys`. It also initializes | |
| 35 | 35 | the module search path (``sys.path``). It does not set ``sys.argv``; use | |
| 36 | 36 | :c:func:`PySys_SetArgvEx` for that. This is a no-op when called for a second time | |
| 37 | - (without calling :c:func:`Py_Finalize` first). There is no return value; it is a | ||
| 37 | + (without calling :c:func:`Py_FinalizeEx` first). There is no return value; it is a | ||
| 38 | 38 | fatal error if the initialization fails. | |
| 39 | 39 | ||
| 40 | 40 | ||
@@ -48,19 +48,20 @@ Initializing and finalizing the interpreter | |||
| 48 | 48 | .. c:function:: int Py_IsInitialized() | |
| 49 | 49 | ||
| 50 | 50 | Return true (nonzero) when the Python interpreter has been initialized, false | |
| 51 | - (zero) if not. After :c:func:`Py_Finalize` is called, this returns false until | ||
| 51 | + (zero) if not. After :c:func:`Py_FinalizeEx` is called, this returns false until | ||
| 52 | 52 | :c:func:`Py_Initialize` is called again. | |
| 53 | 53 | ||
| 54 | 54 | ||
| 55 | - .. c:function:: void Py_Finalize() | ||
| 55 | + .. c:function:: int Py_FinalizeEx() | ||
| 56 | 56 | ||
| 57 | 57 | Undo all initializations made by :c:func:`Py_Initialize` and subsequent use of | |
| 58 | 58 | Python/C API functions, and destroy all sub-interpreters (see | |
| 59 | 59 | :c:func:`Py_NewInterpreter` below) that were created and not yet destroyed since | |
| 60 | 60 | the last call to :c:func:`Py_Initialize`. Ideally, this frees all memory | |
| 61 | 61 | allocated by the Python interpreter. This is a no-op when called for a second | |
| 62 | - time (without calling :c:func:`Py_Initialize` again first). There is no return | ||
| 63 | - value; errors during finalization are ignored. | ||
| 62 | + time (without calling :c:func:`Py_Initialize` again first). Normally the | ||
| 63 | + return value is 0. If there were errors during finalization | ||
| 64 | + (flushing buffered data), -1 is returned. | ||
| 64 | 65 | ||
| 65 | 66 | This function is provided for a number of reasons. An embedding application | |
| 66 | 67 | might want to restart Python without having to restart the application itself. | |
@@ -79,7 +80,15 @@ Initializing and finalizing the interpreter | |||
| 79 | 80 | freed. Some memory allocated by extension modules may not be freed. Some | |
| 80 | 81 | extensions may not work properly if their initialization routine is called more | |
| 81 | 82 | than once; this can happen if an application calls :c:func:`Py_Initialize` and | |
| 82 | - :c:func:`Py_Finalize` more than once. | ||
| 83 | + :c:func:`Py_FinalizeEx` more than once. | ||
| 84 | + | ||
| 85 | + .. versionadded:: 3.6 | ||
| 86 | + | ||
| 87 | + | ||
| 88 | + .. c:function:: void Py_Finalize() | ||
| 89 | + | ||
| 90 | + This is a backwards-compatible version of :c:func:`Py_FinalizeEx` that | ||
| 91 | + disregards the return value. | ||
| 83 | 92 | ||
| 84 | 93 | ||
| 85 | 94 | Process-wide parameters | |
@@ -107,7 +116,7 @@ Process-wide parameters | |||
| 107 | 116 | Note that :data:`sys.stderr` always uses the "backslashreplace" error | |
| 108 | 117 | handler, regardless of this (or any other) setting. | |
| 109 | 118 | ||
| 110 | - If :c:func:`Py_Finalize` is called, this function will need to be called | ||
| 119 | + If :c:func:`Py_FinalizeEx` is called, this function will need to be called | ||
| 111 | 120 | again in order to affect subsequent calls to :c:func:`Py_Initialize`. | |
| 112 | 121 | ||
| 113 | 122 | Returns 0 if successful, a nonzero value on error (e.g. calling after the | |
@@ -918,7 +927,7 @@ using the following functions: | |||
| 918 | 927 | entry.) | |
| 919 | 928 | ||
| 920 | 929 | .. index:: | |
| 921 | - single: Py_Finalize() | ||
| 930 | + single: Py_FinalizeEx() | ||
| 922 | 931 | single: Py_Initialize() | |
| 923 | 932 | ||
| 924 | 933 | Extension modules are shared between (sub-)interpreters as follows: the first | |
@@ -928,22 +937,22 @@ using the following functions: | |||
| 928 | 937 | and filled with the contents of this copy; the extension's ``init`` function is | |
| 929 | 938 | not called. Note that this is different from what happens when an extension is | |
| 930 | 939 | imported after the interpreter has been completely re-initialized by calling | |
| 931 | - :c:func:`Py_Finalize` and :c:func:`Py_Initialize`; in that case, the extension's | ||
| 940 | + :c:func:`Py_FinalizeEx` and :c:func:`Py_Initialize`; in that case, the extension's | ||
| 932 | 941 | ``initmodule`` function *is* called again. | |
| 933 | 942 | ||
| 934 | 943 | .. index:: single: close() (in module os) | |
| 935 | 944 | ||
| 936 | 945 | ||
| 937 | 946 | .. c:function:: void Py_EndInterpreter(PyThreadState *tstate) | |
| 938 | 947 | ||
| 939 | - .. index:: single: Py_Finalize() | ||
| 948 | + .. index:: single: Py_FinalizeEx() | ||
| 940 | 949 | ||
| 941 | 950 | Destroy the (sub-)interpreter represented by the given thread state. The given | |
| 942 | 951 | thread state must be the current thread state. See the discussion of thread | |
| 943 | 952 | states below. When the call returns, the current thread state is *NULL*. All | |
| 944 | 953 | thread states associated with this interpreter are destroyed. (The global | |
| 945 | 954 | interpreter lock must be held before calling this function and is still held | |
| 946 | - when it returns.) :c:func:`Py_Finalize` will destroy all sub-interpreters that | ||
| 955 | + when it returns.) :c:func:`Py_FinalizeEx` will destroy all sub-interpreters that | ||
| 947 | 956 | haven't been explicitly destroyed at that point. | |
| 948 | 957 | ||
| 949 | 958 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -578,9 +578,9 @@ Sometimes, it is desirable to "uninitialize" Python. For instance, the | |||
| 578 | 578 | application may want to start over (make another call to | |
| 579 | 579 | :c:func:`Py_Initialize`) or the application is simply done with its use of | |
| 580 | 580 | Python and wants to free memory allocated by Python. This can be accomplished | |
| 581 | - by calling :c:func:`Py_Finalize`. The function :c:func:`Py_IsInitialized` returns | ||
| 581 | + by calling :c:func:`Py_FinalizeEx`. The function :c:func:`Py_IsInitialized` returns | ||
| 582 | 582 | true if Python is currently in the initialized state. More information about | |
| 583 | - these functions is given in a later chapter. Notice that :c:func:`Py_Finalize` | ||
| 583 | + these functions is given in a later chapter. Notice that :c:func:`Py_FinalizeEx` | ||
| 584 | 584 | does *not* free all memory allocated by the Python interpreter, e.g. memory | |
| 585 | 585 | allocated by extension modules currently cannot be released. | |
| 586 | 586 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -212,20 +212,24 @@ Process Control | |||
| 212 | 212 | .. c:function:: void Py_Exit(int status) | |
| 213 | 213 | ||
| 214 | 214 | .. index:: | |
| 215 | - single: Py_Finalize() | ||
| 215 | + single: Py_FinalizeEx() | ||
| 216 | 216 | single: exit() | |
| 217 | 217 | ||
| 218 | - Exit the current process. This calls :c:func:`Py_Finalize` and then calls the | ||
| 219 | - standard C library function ``exit(status)``. | ||
| 218 | + Exit the current process. This calls :c:func:`Py_FinalizeEx` and then calls the | ||
| 219 | + standard C library function ``exit(status)``. If :c:func:`Py_FinalizeEx` | ||
| 220 | + indicates an error, the exit status is set to 120. | ||
| 221 | + | ||
| 222 | + .. versionchanged:: 3.6 | ||
| 223 | + Errors from finalization no longer ignored. | ||
| 220 | 224 | ||
| 221 | 225 | ||
| 222 | 226 | .. c:function:: int Py_AtExit(void (*func) ()) | |
| 223 | 227 | ||
| 224 | 228 | .. index:: | |
| 225 | - single: Py_Finalize() | ||
| 229 | + single: Py_FinalizeEx() | ||
| 226 | 230 | single: cleanup functions | |
| 227 | 231 | ||
| 228 | - Register a cleanup function to be called by :c:func:`Py_Finalize`. The cleanup | ||
| 232 | + Register a cleanup function to be called by :c:func:`Py_FinalizeEx`. The cleanup | ||
| 229 | 233 | function will be called with no arguments and should return no value. At most | |
| 230 | 234 | 32 cleanup functions can be registered. When the registration is successful, | |
| 231 | 235 | :c:func:`Py_AtExit` returns ``0``; on failure, it returns ``-1``. The cleanup | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -67,7 +67,9 @@ perform some operation on a file. :: | |||
| 67 | 67 | Py_Initialize(); | |
| 68 | 68 | PyRun_SimpleString("from time import time,ctime\n" | |
| 69 | 69 | "print('Today is', ctime(time()))\n"); | |
| 70 | - Py_Finalize(); | ||
| 70 | + if (Py_FinalizeEx() < 0) { | ||
| 71 | + exit(120); | ||
| 72 | + } | ||
| 71 | 73 | PyMem_RawFree(program); | |
| 72 | 74 | return 0; | |
| 73 | 75 | } | |
@@ -76,7 +78,7 @@ The :c:func:`Py_SetProgramName` function should be called before | |||
| 76 | 78 | :c:func:`Py_Initialize` to inform the interpreter about paths to Python run-time | |
| 77 | 79 | libraries. Next, the Python interpreter is initialized with | |
| 78 | 80 | :c:func:`Py_Initialize`, followed by the execution of a hard-coded Python script | |
| 79 | - that prints the date and time. Afterwards, the :c:func:`Py_Finalize` call shuts | ||
| 81 | + that prints the date and time. Afterwards, the :c:func:`Py_FinalizeEx` call shuts | ||
| 80 | 82 | the interpreter down, followed by the end of the program. In a real program, | |
| 81 | 83 | you may want to get the Python script from another source, perhaps a text-editor | |
| 82 | 84 | routine, a file, or a database. Getting the Python code from a file can better | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -63,6 +63,8 @@ main(int argc, char *argv[]) | |||
| 63 | 63 | fprintf(stderr, "Failed to load \"%s\"\n", argv[1]); | |
| 64 | 64 | return 1; | |
| 65 | 65 | } | |
| 66 | - Py_Finalize(); | ||
| 66 | + if (Py_FinalizeEx() < 0) { | ||
| 67 | + return 120; | ||
| 68 | + } | ||
| 67 | 69 | return 0; | |
| 68 | 70 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -255,7 +255,7 @@ always available. | |||
| 255 | 255 | (defaulting to zero), or another type of object. If it is an integer, zero | |
| 256 | 256 | is considered "successful termination" and any nonzero value is considered | |
| 257 | 257 | "abnormal termination" by shells and the like. Most systems require it to be | |
| 258 | - in the range 0-127, and produce undefined results otherwise. Some systems | ||
| 258 | + in the range 0--127, and produce undefined results otherwise. Some systems | ||
| 259 | 259 | have a convention for assigning specific meanings to specific exit codes, but | |
| 260 | 260 | these are generally underdeveloped; Unix programs generally use 2 for command | |
| 261 | 261 | line syntax errors and 1 for all other kind of errors. If another type of | |
@@ -268,6 +268,11 @@ always available. | |||
| 268 | 268 | the process when called from the main thread, and the exception is not | |
| 269 | 269 | intercepted. | |
| 270 | 270 | ||
| 271 | + .. versionchanged:: 3.6 | ||
| 272 | + If an error occurs in the cleanup after the Python interpreter | ||
| 273 | + has caught :exc:`SystemExit` (such as an error flushing buffered data | ||
| 274 | + in the standard streams), the exit status is changed to 120. | ||
| 275 | + | ||
| 271 | 276 | ||
| 272 | 277 | .. data:: flags | |
| 273 | 278 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -171,7 +171,8 @@ Optimizations | |||
| 171 | 171 | Build and C API Changes | |
| 172 | 172 | ======================= | |
| 173 | 173 | ||
| 174 | - * None yet. | ||
| 174 | + * New :c:func:`Py_FinalizeEx` API which indicates if flushing buffered data | ||
| 175 | + failed (:issue:`5319`). | ||
| 175 | 176 | ||
| 176 | 177 | ||
| 177 | 178 | Deprecated | |
@@ -247,4 +248,5 @@ Changes in the Python API | |||
| 247 | 248 | Changes in the C API | |
| 248 | 249 | -------------------- | |
| 249 | 250 | ||
| 250 | - * None yet. | ||
| 251 | + * :c:func:`Py_Exit` (and the main interpreter) now override the exit status | ||
| 252 | + with 120 if flushing buffered data failed. See :issue:`5319`. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,6 +27,7 @@ PyAPI_FUNC(void) Py_InitializeEx(int); | |||
| 27 | 27 | PyAPI_FUNC(void) _Py_InitializeEx_Private(int, int); | |
| 28 | 28 | #endif | |
| 29 | 29 | PyAPI_FUNC(void) Py_Finalize(void); | |
| 30 | + PyAPI_FUNC(int) Py_FinalizeEx(void); | ||
| 30 | 31 | PyAPI_FUNC(int) Py_IsInitialized(void); | |
| 31 | 32 | PyAPI_FUNC(PyThreadState *) Py_NewInterpreter(void); | |
| 32 | 33 | PyAPI_FUNC(void) Py_EndInterpreter(PyThreadState *); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -348,8 +348,9 @@ def test_stdout_flush_at_shutdown(self): | |||
| 348 | 348 | test.support.SuppressCrashReport().__enter__() | |
| 349 | 349 | sys.stdout.write('x') | |
| 350 | 350 | os.close(sys.stdout.fileno())""" | |
| 351 | - rc, out, err = assert_python_ok('-c', code) | ||
| 351 | + rc, out, err = assert_python_failure('-c', code) | ||
| 352 | 352 | self.assertEqual(b'', out) | |
| 353 | + self.assertEqual(120, rc) | ||
| 353 | 354 | self.assertRegex(err.decode('ascii', 'ignore'), | |
| 354 | 355 | 'Exception ignored in.*\nOSError: .*') | |
| 355 | 356 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,6 +10,9 @@ Release date: XXXX-XX-XX | |||
| 10 | 10 | Core and Builtins | |
| 11 | 11 | ----------------- | |
| 12 | 12 | ||
| 13 | + - Issue #5319: New Py_FinalizeEx() API allowing Python to set an exit status | ||
| 14 | + of 120 on failure to flush buffered streams. | ||
| 15 | + | ||
| 13 | 16 | - Issue #25485: telnetlib.Telnet is now a context manager. | |
| 14 | 17 | ||
| 15 | 18 | - Issue #24097: Fixed crash in object.__reduce__() if slot name is freed inside | |
| Back | FazBrowse Home | New Git URL |
0 commit comments