Skip to content

gh-76785: Improved Subinterpreters Compatibility with 3.12 (2/2) #126707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use the init funcs in _PyXI_Init().
  • Loading branch information
ericsnowcurrently committed Nov 11, 2024
commit cf4801668cfeae7311e9ee4c28d2152fee7f01b2
4 changes: 2 additions & 2 deletions Include/internal/pycore_crossinterp.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ extern void _PyXI_FiniTypes(PyInterpreterState *interp);

int _Py_xi_global_state_init(_PyXI_global_state_t *);
void _Py_xi_global_state_fini(_PyXI_global_state_t *);
int _Py_xi_state_init(_PyXI_state_t *);
void _Py_xi_state_fini(_PyXI_state_t *);
int _Py_xi_state_init(_PyXI_state_t *, PyInterpreterState *);
void _Py_xi_state_fini(_PyXI_state_t *, PyInterpreterState *);


/***************************/
Expand Down
141 changes: 74 additions & 67 deletions Python/crossinterp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1788,33 +1788,87 @@ _PyXI_Exit(_PyXI_session *session)
/* runtime lifecycle */
/*********************/

PyStatus
_PyXI_Init(PyInterpreterState *interp)
int
_Py_xi_global_state_init(_PyXI_global_state_t *state)
{
_PyXI_state_t *state = _PyXI_GET_STATE(interp);
if (state == NULL) {
PyErr_PrintEx(0);
return _PyStatus_ERR(
"failed to get interpreter's cross-interpreter state");
assert(state != NULL);
xid_lookup_init(&state->data_lookup);
return 0;
}

void
_Py_xi_global_state_fini(_PyXI_global_state_t *state)
{
assert(state != NULL);
xid_lookup_fini(&state->data_lookup);
}

int
_Py_xi_state_init(_PyXI_state_t *state, PyInterpreterState *interp)
{
assert(state != NULL);
assert(interp == NULL || state == _PyXI_GET_STATE(interp));

xid_lookup_init(&state->data_lookup);

// Initialize exceptions.
if (interp != NULL) {
if (init_static_exctypes(&state->exceptions, interp) < 0) {
fini_heap_exctypes(&state->exceptions);
return -1;
}
}
if (init_heap_exctypes(&state->exceptions) < 0) {
return -1;
}

return 0;
}

void
_Py_xi_state_fini(_PyXI_state_t *state, PyInterpreterState *interp)
{
assert(state != NULL);
assert(interp == NULL || state == _PyXI_GET_STATE(interp));

fini_heap_exctypes(&state->exceptions);
if (interp != NULL) {
fini_static_exctypes(&state->exceptions, interp);
}

// Initialize the XID lookup state (e.g. registry).
xid_lookup_fini(&state->data_lookup);
}


PyStatus
_PyXI_Init(PyInterpreterState *interp)
{
if (_Py_IsMainInterpreter(interp)) {
_PyXI_global_state_t *global_state = _PyXI_GET_GLOBAL_STATE(interp);
if (global_state == NULL) {
PyErr_PrintEx(0);
return _PyStatus_ERR(
"failed to get global cross-interpreter state");
}
xid_lookup_init(&global_state->data_lookup);
if (_Py_xi_global_state_init(global_state) < 0) {
PyErr_PrintEx(0);
return _PyStatus_ERR(
"failed to initialize global cross-interpreter state");
}
}
xid_lookup_init(&state->data_lookup);

// Initialize exceptions.(heap types).
// See _PyXI_InitTypes() for the static types.
if (init_heap_exctypes(&_PyXI_GET_STATE(interp)->exceptions) < 0) {
_PyXI_state_t *state = _PyXI_GET_STATE(interp);
if (state == NULL) {
PyErr_PrintEx(0);
return _PyStatus_ERR("failed to initialize exceptions");
return _PyStatus_ERR(
"failed to get interpreter's cross-interpreter state");
}
// The static types were already initialized in _PyXI_InitTypes(),
// so we pass in NULL here to avoid initializing them again.
if (_Py_xi_state_init(state, NULL) < 0) {
PyErr_PrintEx(0);
return _PyStatus_ERR(
"failed to initialize interpreter's cross-interpreter state");
}

return _PyStatus_OK();
Expand All @@ -1827,24 +1881,19 @@ void
_PyXI_Fini(PyInterpreterState *interp)
{
_PyXI_state_t *state = _PyXI_GET_STATE(interp);
assert(state != NULL);
#ifndef NDEBUG
if (state == NULL) {
PyErr_PrintEx(0);
return;
}
#endif
// The static types will be finalized soon in _PyXI_FiniTypes(),
// so we pass in NULL here to avoid finalizing them right now.
_Py_xi_state_fini(state, NULL);

// Finalize exceptions (heap types).
// See _PyXI_FiniTypes() for the static types.
fini_heap_exctypes(&_PyXI_GET_STATE(interp)->exceptions);

// Finalize the XID lookup state (e.g. registry).
xid_lookup_fini(&state->data_lookup);
if (_Py_IsMainInterpreter(interp)) {
_PyXI_global_state_t *global_state = _PyXI_GET_GLOBAL_STATE(interp);
assert(global_state != NULL);
xid_lookup_fini(&global_state->data_lookup);
_Py_xi_global_state_fini(global_state);
}
}

Expand All @@ -1853,7 +1902,8 @@ _PyXI_InitTypes(PyInterpreterState *interp)
{
if (init_static_exctypes(&_PyXI_GET_STATE(interp)->exceptions, interp) < 0) {
PyErr_PrintEx(0);
return _PyStatus_ERR("failed to initialize an exception type");
return _PyStatus_ERR(
"failed to initialize the cross-interpreter exception types");
}
// We would initialize heap types here too but that leads to ref leaks.
// Instead, we intialize them in _PyXI_Init().
Expand All @@ -1868,49 +1918,6 @@ _PyXI_FiniTypes(PyInterpreterState *interp)
fini_static_exctypes(&_PyXI_GET_STATE(interp)->exceptions, interp);
}

int
_Py_xi_global_state_init(_PyXI_global_state_t *state)
{
xid_lookup_init(&state->data_lookup);
return 0;
}

void
_Py_xi_global_state_fini(_PyXI_global_state_t *state)
{
xid_lookup_fini(&state->data_lookup);
}

int
_Py_xi_state_init(_PyXI_state_t *state)
{
PyInterpreterState *interp = _PyInterpreterState_GET();

xid_lookup_init(&state->data_lookup);

// Initialize exceptions.
if (init_exceptions(interp) < 0) {
return -1;
}
if (_init_not_shareable_error_type(state) < 0) {
return -1;
}
return 0;
}

void
_Py_xi_state_fini(_PyXI_state_t *state)
{
PyInterpreterState *interp = _PyInterpreterState_GET();

// Finalize exceptions.
_fini_not_shareable_error_type(state);
fini_exceptions(interp);

// Finalize the XID lookup state (e.g. registry).
xid_lookup_fini(&state->data_lookup);
}


/*************/
/* other API */
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy