summaryrefslogtreecommitdiff
path: root/Python/pystate.c
AgeCommit message (Collapse)AuthorLines
2021-03-10bpo-43311: Create GIL autoTSSkey ealier (GH-24819)Victor Stinner-4/+15
At Python startup, call _PyGILState_Init() before PyInterpreterState_New() which calls _PyThreadState_GET(). When Python is built using --with-experimental-isolated-subinterpreters, _PyThreadState_GET() uses autoTSSkey.
2021-02-26bpo-42128: Structural Pattern Matching (PEP 634) (GH-22917)Brandt Bucher-0/+2
Co-authored-by: Guido van Rossum <guido@python.org> Co-authored-by: Talin <viridia@gmail.com> Co-authored-by: Pablo Galindo <pablogsal@gmail.com>
2021-02-19bpo-43268: Pass interp rather than tstate to internal functions (GH-24580)Victor Stinner-4/+4
Pass the current interpreter (interp) rather than the current Python thread state (tstate) to internal functions which only use the interpreter. Modified functions: * _PyXXX_Fini() and _PyXXX_ClearFreeList() functions * _PyEval_SignalAsyncExc(), make_pending_calls() * _PySys_GetObject(), sys_set_object(), sys_set_object_id(), sys_set_object_str() * should_audit(), set_flags_from_config(), make_flags() * _PyAtExit_Call() * init_stdio_encoding() * etc.
2021-02-19bpo-43268: _Py_IsMainInterpreter() now expects interp (GH-24577)Victor Stinner-1/+1
The _Py_IsMainInterpreter() function now expects interp rather than tstate.
2021-02-04bpo-42882: Fix MSVC warnings in pystate.c (GH-24440)Ken Jin-1/+1
_PyRuntimeState.unicode_ids.next_index type is Py_ssize_t.
2021-01-12bpo-42882: _PyRuntimeState_Init() leaves unicode next_index unchanged (GH-24193)Victor Stinner-1/+4
Fix the _PyUnicode_FromId() function (_Py_IDENTIFIER(var) API) when Py_Initialize() / Py_Finalize() is called multiple times: preserve _PyRuntime.unicode_ids.next_index value. Use _PyRuntimeState_INIT macro instead memset(0) to reset _PyRuntimeState members to zero.
2020-12-26bpo-42745: Make the type cache per-interpreter (GH-23947)Victor Stinner-0/+2
Make the type attribute lookup cache per-interpreter. Add private _PyType_InitCache() function, called by PyInterpreterState_New(). Continue to share next_version_tag between interpreters, since static types are still shared by interpreters. Remove MCACHE macro: the cache is no longer disabled if the EXPERIMENTAL_ISOLATED_SUBINTERPRETERS macro is defined.
2020-12-26bpo-39465: Fix _PyUnicode_FromId() for subinterpreters (GH-20058)Victor Stinner-11/+19
Make _PyUnicode_FromId() function compatible with subinterpreters. Each interpreter now has an array of identifier objects (interned strings decoded from UTF-8). * Add PyInterpreterState.unicode.identifiers: array of identifiers objects. * Add _PyRuntimeState.unicode_ids used to allocate unique indexes to _Py_Identifier. * Rewrite the _Py_Identifier structure. Microbenchmark on _PyUnicode_FromId(&PyId_a) with _Py_IDENTIFIER(a): [ref] 2.42 ns +- 0.00 ns -> [atomic] 3.39 ns +- 0.00 ns: 1.40x slower This change adds 1 ns per _PyUnicode_FromId() call in average.
2020-12-15bpo-42639: Move atexit state to PyInterpreterState (GH-23763)Victor Stinner-0/+1
* Add _PyAtExit_Call() function and remove pyexitfunc and pyexitmodule members of PyInterpreterState. The function logs atexit callback errors using _PyErr_WriteUnraisableMsg(). * Add _PyAtExit_Init() and _PyAtExit_Fini() functions. * Remove traverse, clear and free functions of the atexit module. Co-authored-by: Dong-hee Na <donghee.na@python.org>
2020-12-02bpo-42500: Fix recursion in or after except (GH-23568)Mark Shannon-1/+1
* Use counter, rather boolean state when handling soft overflows.
2020-11-05bpo-42260: Add _PyInterpreterState_SetConfig() (GH-23158)Victor Stinner-5/+11
* Inline _PyInterpreterState_SetConfig(): replace it with _PyConfig_Copy(). * Add _PyErr_SetFromPyStatus() * Add _PyInterpreterState_GetConfigCopy() * Add a new _PyInterpreterState_SetConfig() function. * Add an unit which gets, modifies, and sets the config.
2020-11-03bpo-41796: Call _PyAST_Fini() earlier to fix a leak (GH-23131)Victor Stinner-4/+7
Call _PyAST_Fini() on all interpreters, not only on the main interpreter. Also, call it ealier to fix a reference leak. Python types contain a reference to themselves in in their PyTypeObject.tp_mro member. _PyAST_Fini() must called before the last GC collection to destroy AST types. _PyInterpreterState_Clear() now calls _PyAST_Fini(). It now also calls _PyWarnings_Fini() on subinterpeters, not only on the main interpreter. Add an assertion in AST init_types() to ensure that the _ast module is no longer used after _PyAST_Fini() has been called.
2020-11-02bpo-41435: Add sys._current_exceptions() function (GH-21689)Julien Danjou-0/+63
This adds a new function named sys._current_exceptions() which is equivalent ot sys._current_frames() except that it returns the exceptions currently handled by other threads. It is equivalent to calling sys.exc_info() for each running thread.
2020-10-30bpo-42208: Call GC collect earlier in PyInterpreterState_Clear() (GH-23044)Victor Stinner-5/+27
The last GC collection is now done before clearing builtins and sys dictionaries. Add also assertions to ensure that gc.collect() is no longer called after _PyGC_Fini(). Pass also the tstate to PyInterpreterState_Clear() to pass the correct tstate to _PyGC_CollectNoFail() and _PyGC_Fini().
2020-10-05bpo-41936. Remove macros Py_ALLOW_RECURSION/Py_END_ALLOW_RECURSION (GH-22552)Serhiy Storchaka-1/+0
2020-08-12bpo-1635741: Clean sysdict and builtins of interpreter at exit (GH-21605)Hai Shi-2/+8
2020-06-13bpo-40834: Fix truncate when sending str object with channel (GH-20555)An Long-1/+1
2020-06-02PyOS_AfterFork_Child() uses PyStatus (GH-20596)Victor Stinner-22/+23
PyOS_AfterFork_Child() helper functions now return a PyStatus: PyOS_AfterFork_Child() is now responsible to handle errors. * Move _PySignal_AfterFork() to the internal C API * Add #ifdef HAVE_FORK on _PyGILState_Reinit(), _PySignal_AfterFork() and _PyInterpreterState_DeleteExceptMain().
2020-06-01bpo-40826: Add _Py_EnsureTstateNotNULL() macro (GH-20571)Victor Stinner-14/+4
Add _Py_EnsureTstateNotNULL(tstate) macro: call Py_FatalError() if tstate is NULL, the error message contains the current function name.
2020-05-05bpo-40522: _PyThreadState_Swap() sets autoTSSkey (GH-19939)Victor Stinner-0/+17
In the experimental isolated subinterpreters build mode, _PyThreadState_GET() gets the autoTSSkey variable and _PyThreadState_Swap() sets the autoTSSkey variable. * Add _PyThreadState_GetTSS() * _PyRuntimeState_GetThreadState() and _PyThreadState_GET() return _PyThreadState_GetTSS() * PyEval_SaveThread() sets the autoTSSkey variable to current Python thread state rather than NULL. * eval_frame_handle_pending() doesn't check that _PyThreadState_Swap() result is NULL. * _PyThreadState_Swap() gets the current Python thread state with _PyThreadState_GetTSS() rather than _PyRuntimeGILState_GetThreadState(). * PyGILState_Ensure() no longer checks _PyEval_ThreadsInitialized() since it cannot access the current interpreter.
2020-04-29bpo-40429: PyThreadState_GetFrame() returns a strong ref (GH-19781)Victor Stinner-3/+5
The PyThreadState_GetFrame() function now returns a strong reference to the frame.
2020-04-17bpo-40302: Replace PY_INT64_T with int64_t (GH-19573)Victor Stinner-3/+3
* Replace PY_INT64_T with int64_t * Replace PY_UINT32_T with uint32_t * Replace PY_UINT64_T with uint64_t sha3module.c no longer checks if PY_UINT64_T is defined since it's always defined and uint64_t is always available on platforms supported by Python.
2020-04-15bpo-40268: Remove unused pycore_pymem.h includes (GH-19531)Victor Stinner-1/+1
2020-04-15bpo-40232: Update PyOS_AfterFork_Child() to use _PyThread_at_fork_reinit() ↵Dong-hee Na-6/+8
(GH-19450)
2020-04-14bpo-40268: Remove a few pycore_pystate.h includes (GH-19510)Victor Stinner-1/+1
2020-04-14bpo-40268: Rename _PyInterpreterState_GET_UNSAFE() (GH-19509)Victor Stinner-1/+1
Rename _PyInterpreterState_GET_UNSAFE() to _PyInterpreterState_GET() for consistency with _PyThreadState_GET() and to have a shorter name (help to fit into 80 columns). Add also "assert(tstate != NULL);" to the function.
2020-04-13bpo-40268: Add pycore_runtime.h header file (GH-19493)Victor Stinner-3/+2
Move PyRuntimeState from pycore_pystate.h to pycore_runtime.h. Remove _PyGILState_check_enabled macro: access directly _PyRuntime.gilstate.check_enabled.
2020-04-13bpo-40268: Add _PyInterpreterState_GetConfig() (GH-19492)Victor Stinner-1/+25
Don't access PyInterpreterState.config member directly anymore, but use new functions: * _PyInterpreterState_GetConfig() * _PyInterpreterState_SetConfig() * _Py_GetConfig()
2020-04-08bpo-40226: PyInterpreterState_Delete() deletes pending calls (GH-19436)Victor Stinner-1/+22
PyInterpreterState_New() is now responsible to create pending calls, PyInterpreterState_Delete() now deletes pending calls. * Rename _PyEval_InitThreads() to _PyEval_InitGIL() and rename _PyEval_InitGIL() to _PyEval_FiniGIL(). * _PyEval_InitState() and PyEval_FiniState() now create and delete pending calls. _PyEval_InitState() now returns -1 on memory allocation failure. * Add init_interp_create_gil() helper function: code shared by Py_NewInterpreter() and Py_InitializeFromConfig(). * init_interp_create_gil() now also calls _PyEval_FiniGIL(), _PyEval_InitGIL() and _PyGILState_Init() in subinterpreters, but these functions now do nothing when called from a subinterpreter.
2020-03-26bpo-38644: Make tstate more explicit inside pystate.c (GH-19182)Victor Stinner-47/+61
Fix PyInterpreterState_New(): Don't call PyErr_SetString() when there is no current Python thread state (if tstate is NULL).
2020-03-25bpo-39947: Add PyThreadState_GetID() function (GH-19163)Victor Stinner-0/+8
Add PyThreadState_GetID() function: get the unique identifier of a Python thread state.
2020-03-25bpo-39947: Add _PyThreadState_GetDict() function (GH-19160)Victor Stinner-9/+18
2020-03-25bpo-39882: Add _Py_FatalErrorFormat() function (GH-19157)Victor Stinner-12/+18
2020-03-24Use calloc-based functions, not malloc. (GH-19152)Andy Lester-2/+1
2020-03-24bpo-20526: Fix PyThreadState_Clear(): don't decref frame (GH-19120)Victor Stinner-2/+10
PyThreadState.frame is a borrowed reference, not a strong reference: PyThreadState_Clear() must not call Py_CLEAR(tstate->frame). Remove test_threading.test_warnings_at_exit(): we cannot warranty that the Python thread state of daemon threads is cleared in a reliable way during Python shutdown.
2020-03-20bpo-39946: Remove _PyThreadState_GetFrame (GH-19094)Victor Stinner-15/+1
Remove _PyRuntime.getframe hook and remove _PyThreadState_GetFrame macro which was an alias to _PyRuntime.getframe. They were only exposed by the internal C API. Remove also PyThreadFrameGetter type.
2020-03-20bpo-39947: Add PyThreadState_GetFrame() function (GH-19092)Victor Stinner-0/+8
Add PyThreadState_GetFrame() function: get the current frame of a Python thread state.
2020-03-20bpo-40010: COMPUTE_EVAL_BREAKER() checks for subinterpreter (GH-19087)Victor Stinner-6/+0
COMPUTE_EVAL_BREAKER() now also checks if the Python thread state belongs to the main interpreter. Don't break the evaluation loop if there are pending signals but the Python thread state it belongs to a subinterpeter. * Add _Py_IsMainThread() function. * Add _Py_ThreadCanHandleSignals() function.
2020-03-18bpo-39984: Pass tstate to _PyEval_SignalAsyncExc() (GH-19049)Victor Stinner-16/+19
_PyEval_SignalAsyncExc() and _PyEval_FiniThreads() now expect tstate, instead of ceval.
2020-03-18bpo-39984: _PyThreadState_DeleteCurrent() takes tstate (GH-19051)Victor Stinner-28/+32
* _PyThreadState_DeleteCurrent() now takes tstate rather than runtime. * Add ensure_tstate_not_null() helper to pystate.c. * Add _PyEval_ReleaseLock() function. * _PyThreadState_DeleteCurrent() now calls _PyEval_ReleaseLock(tstate) and frees PyThreadState memory after this call, not before. * PyGILState_Release(): rename "tcur" variable to "tstate".
2020-03-17bpo-39984: Add PyInterpreterState.ceval (GH-19047)Victor Stinner-1/+2
subinterpreters: Move _PyRuntimeState.ceval.tracing_possible to PyInterpreterState.ceval.tracing_possible: each interpreter now has its own variable. Changes: * Add _ceval_state structure. * Add PyInterpreterState.ceval field. * _PyEval_EvalFrameDefault(): add ceval2 variable (struct _ceval_state*). * Rename _PyEval_Initialize() to _PyEval_InitRuntimeState(). * Add _PyEval_InitState(). * Don't export internal _Py_FinishPendingCalls() and _PyEval_FiniThreads() functions anymore.
2020-03-13bpo-39947: Add PyThreadState_GetInterpreter() (GH-18981)Victor Stinner-0/+11
Add PyThreadState_GetInterpreter(tstate): get the interpreter of a Python thread state.
2020-03-13bpo-39947: Add PyInterpreterState_Get() function (GH-18979)Victor Stinner-3/+3
* Rename _PyInterpreterState_Get() to PyInterpreterState_Get() and move it the limited C API. * Add _PyInterpreterState_Get() alias to PyInterpreterState_Get() for backward compatibility with Python 3.8.
2020-03-12bpo-38500: Add _PyInterpreterState_SetEvalFrameFunc() (GH-17340)Victor Stinner-0/+14
PyInterpreterState.eval_frame function now requires a tstate (Python thread state) parameter. Add private functions to the C API to get and set the frame evaluation function: * Add tstate parameter to _PyFrameEvalFunction function type. * Add _PyInterpreterState_GetEvalFrameFunc() and _PyInterpreterState_SetEvalFrameFunc() functions. * Add tstate parameter to _PyEval_EvalFrameDefault().
2020-03-10bpo-38631: _PyGILState_Init() returns PyStatus (GH-18908)Victor Stinner-2/+3
_PyGILState_Init() now returns PyStatus rather than calling Py_FatalError() on failure.
2020-03-10bpo-39877: PyGILState_Ensure() don't call PyEval_InitThreads() (GH-18891)Victor Stinner-19/+13
PyGILState_Ensure() doesn't call PyEval_InitThreads() anymore when a new Python thread state is created. The GIL is created by Py_Initialize() since Python 3.7, it's not needed to call PyEval_InitThreads() explicitly. Add an assertion to ensure that the GIL is already created.
2020-03-09bpo-19466: Py_Finalize() clears daemon threads earlier (GH-18848)Victor Stinner-7/+12
Clear the frames of daemon threads earlier during the Python shutdown to call objects destructors. So "unclosed file" resource warnings are now emitted for daemon threads in a more reliable way. Cleanup _PyThreadState_DeleteExcept() code: rename "garbage" to "list".
2020-03-07bpo-39882: Py_FatalError() logs the function name (GH-18819)Victor Stinner-21/+21
The Py_FatalError() function is replaced with a macro which logs automatically the name of the current function, unless the Py_LIMITED_API macro is defined. Changes: * Add _Py_FatalErrorFunc() function. * Remove the function name from the message of Py_FatalError() calls which included the function name. * Update tests.
2020-03-07bpo-39877: _PyRuntimeState.finalizing becomes atomic (GH-18816)Victor Stinner-1/+1
Convert _PyRuntimeState.finalizing field to an atomic variable: * Rename it to _finalizing * Change its type to _Py_atomic_address * Add _PyRuntimeState_GetFinalizing() and _PyRuntimeState_SetFinalizing() functions * Remove _Py_CURRENTLY_FINALIZING() function: replace it with testing directly _PyRuntimeState_GetFinalizing() value Convert _PyRuntimeState_GetThreadState() to static inline function.
2020-03-02 bpo-39776: Lock ++interp->tstate_next_unique_id. (GH-18746) (#18746)Stefan Krah-2/+1
- Threads created by PyGILState_Ensure() could have a duplicate tstate->id.