bpo-38858: Add init_interp_main() subfunction (GH-17347) · python/cpython@b005136 (original) (raw)

`@@ -260,6 +260,7 @@ _Py_LegacyLocaleDetected(int warn)

`

260

260

`#endif

`

261

261

`}

`

262

262

``

``

263

`+

#ifndef MS_WINDOWS

`

263

264

`static const char *_C_LOCALE_WARNING =

`

264

265

`"Python runtime initialized with LC_CTYPE=C (a locale with default ASCII "

`

265

266

`"encoding), which may cause Unicode compatibility problems. Using C.UTF-8, "

`

`@@ -274,6 +275,7 @@ emit_stderr_warning_for_legacy_locale(_PyRuntimeState *runtime)

`

274

275

`PySys_FormatStderr("%s", _C_LOCALE_WARNING);

`

275

276

` }

`

276

277

`}

`

``

278

`+

#endif /* !defined(MS_WINDOWS) */

`

277

279

``

278

280

`typedef struct _CandidateLocale {

`

279

281

`const char locale_name; / The locale to try as a coercion target */

`

`@@ -896,123 +898,115 @@ pyinit_core(_PyRuntimeState *runtime,

`

896

898

` configuration. Example of bpo-34008: Py_Main() called after

`

897

899

` Py_Initialize(). */

`

898

900

`static PyStatus

`

899

``

`-

_Py_ReconfigureMainInterpreter(PyInterpreterState *interp)

`

``

901

`+

_Py_ReconfigureMainInterpreter(PyThreadState *tstate)

`

900

902

`{

`

901

``

`-

PyConfig *config = &interp->config;

`

``

903

`+

PyConfig *config = &tstate->interp->config;

`

902

904

``

903

905

`PyObject *argv = _PyWideStringList_AsList(&config->argv);

`

904

906

`if (argv == NULL) {

`

905

907

`return _PyStatus_NO_MEMORY(); \

`

906

908

` }

`

907

909

``

908

``

`-

int res = PyDict_SetItemString(interp->sysdict, "argv", argv);

`

``

910

`+

int res = PyDict_SetItemString(tstate->interp->sysdict, "argv", argv);

`

909

911

`Py_DECREF(argv);

`

910

912

`if (res < 0) {

`

911

913

`return _PyStatus_ERR("fail to set sys.argv");

`

912

914

` }

`

913

915

`return _PyStatus_OK();

`

914

916

`}

`

915

917

``

916

``

`-

/* Update interpreter state based on supplied configuration settings

`

917

``

`-

`

918

``

`-

`

919

``

`-

`

920

``

`-

`

921

``

`-

`

922

``

`-

`

923

``

`-

`

924

``

`-

`

925

``

`-

`

926

``

`-

*/

`

``

918

+

927

919

`static PyStatus

`

928

``

`-

pyinit_main(PyThreadState *tstate)

`

``

920

`+

init_interp_main(PyThreadState *tstate)

`

929

921

`{

`

930

``

`-

_PyRuntimeState *runtime = tstate->interp->runtime;

`

931

``

`-

if (!runtime->core_initialized) {

`

932

``

`-

return _PyStatus_ERR("runtime core not initialized");

`

933

``

`-

}

`

934

``

-

935

``

`-

/* Configure the main interpreter */

`

``

922

`+

PyStatus status;

`

``

923

`+

int is_main_interp = _Py_IsMainInterpreter(tstate);

`

936

924

`PyInterpreterState *interp = tstate->interp;

`

937

925

`PyConfig *config = &interp->config;

`

938

926

``

939

``

`-

if (runtime->initialized) {

`

940

``

`-

return _Py_ReconfigureMainInterpreter(interp);

`

941

``

`-

}

`

942

``

-

943

927

`if (!config->_install_importlib) {

`

944

928

`/* Special mode for freeze_importlib: run with no import system

`

945

929

` *

`

946

930

` * This means anything which needs support from extension modules

`

947

931

` * or pure Python code in the standard library won't work.

`

948

932

` */

`

949

``

`-

runtime->initialized = 1;

`

``

933

`+

if (is_main_interp) {

`

``

934

`+

interp->runtime->initialized = 1;

`

``

935

`+

}

`

950

936

`return _PyStatus_OK();

`

951

937

` }

`

952

938

``

953

``

`-

if (_PyTime_Init() < 0) {

`

954

``

`-

return _PyStatus_ERR("can't initialize time");

`

955

``

`-

}

`

``

939

`+

if (is_main_interp) {

`

``

940

`+

if (_PyTime_Init() < 0) {

`

``

941

`+

return _PyStatus_ERR("can't initialize time");

`

``

942

`+

}

`

956

943

``

957

``

`-

if (_PySys_InitMain(tstate) < 0) {

`

958

``

`-

return _PyStatus_ERR("can't finish initializing sys");

`

``

944

`+

if (_PySys_InitMain(tstate) < 0) {

`

``

945

`+

return _PyStatus_ERR("can't finish initializing sys");

`

``

946

`+

}

`

959

947

` }

`

960

948

``

961

``

`-

PyStatus status = init_importlib_external(tstate);

`

``

949

`+

status = init_importlib_external(tstate);

`

962

950

`if (_PyStatus_EXCEPTION(status)) {

`

963

951

`return status;

`

964

952

` }

`

965

953

``

966

``

`-

/* initialize the faulthandler module */

`

967

``

`-

status = _PyFaulthandler_Init(config->faulthandler);

`

968

``

`-

if (_PyStatus_EXCEPTION(status)) {

`

969

``

`-

return status;

`

``

954

`+

if (is_main_interp) {

`

``

955

`+

/* initialize the faulthandler module */

`

``

956

`+

status = _PyFaulthandler_Init(config->faulthandler);

`

``

957

`+

if (_PyStatus_EXCEPTION(status)) {

`

``

958

`+

return status;

`

``

959

`+

}

`

970

960

` }

`

971

961

``

972

962

`status = _PyUnicode_InitEncodings(tstate);

`

973

963

`if (_PyStatus_EXCEPTION(status)) {

`

974

964

`return status;

`

975

965

` }

`

976

966

``

977

``

`-

if (config->install_signal_handlers) {

`

978

``

`-

status = init_signals(tstate);

`

979

``

`-

if (_PyStatus_EXCEPTION(status)) {

`

980

``

`-

return status;

`

``

967

`+

if (is_main_interp) {

`

``

968

`+

if (config->install_signal_handlers) {

`

``

969

`+

status = init_signals(tstate);

`

``

970

`+

if (_PyStatus_EXCEPTION(status)) {

`

``

971

`+

return status;

`

``

972

`+

}

`

981

973

` }

`

982

``

`-

}

`

983

974

``

984

``

`-

if (_PyTraceMalloc_Init(config->tracemalloc) < 0) {

`

985

``

`-

return _PyStatus_ERR("can't initialize tracemalloc");

`

``

975

`+

if (_PyTraceMalloc_Init(config->tracemalloc) < 0) {

`

``

976

`+

return _PyStatus_ERR("can't initialize tracemalloc");

`

``

977

`+

}

`

986

978

` }

`

987

979

``

988

``

`-

status = add_main_module(interp);

`

``

980

`+

status = init_sys_streams(tstate);

`

989

981

`if (_PyStatus_EXCEPTION(status)) {

`

990

982

`return status;

`

991

983

` }

`

992

984

``

993

``

`-

status = init_sys_streams(tstate);

`

``

985

`+

status = init_set_builtins_open(tstate);

`

994

986

`if (_PyStatus_EXCEPTION(status)) {

`

995

987

`return status;

`

996

988

` }

`

997

989

``

998

``

`-

status = init_set_builtins_open(tstate);

`

``

990

`+

status = add_main_module(interp);

`

999

991

`if (_PyStatus_EXCEPTION(status)) {

`

1000

992

`return status;

`

1001

993

` }

`

1002

994

``

1003

``

`-

/* Initialize warnings. */

`

1004

``

`-

PyObject *warnoptions = PySys_GetObject("warnoptions");

`

1005

``

`-

if (warnoptions != NULL && PyList_Size(warnoptions) > 0)

`

1006

``

`-

{

`

1007

``

`-

PyObject *warnings_module = PyImport_ImportModule("warnings");

`

1008

``

`-

if (warnings_module == NULL) {

`

1009

``

`-

fprintf(stderr, "'import warnings' failed; traceback:\n");

`

1010

``

`-

_PyErr_Print(tstate);

`

``

995

`+

if (is_main_interp) {

`

``

996

`+

/* Initialize warnings. */

`

``

997

`+

PyObject *warnoptions = PySys_GetObject("warnoptions");

`

``

998

`+

if (warnoptions != NULL && PyList_Size(warnoptions) > 0)

`

``

999

`+

{

`

``

1000

`+

PyObject *warnings_module = PyImport_ImportModule("warnings");

`

``

1001

`+

if (warnings_module == NULL) {

`

``

1002

`+

fprintf(stderr, "'import warnings' failed; traceback:\n");

`

``

1003

`+

_PyErr_Print(tstate);

`

``

1004

`+

}

`

``

1005

`+

Py_XDECREF(warnings_module);

`

1011

1006

` }

`

1012

``

`-

Py_XDECREF(warnings_module);

`

1013

``

`-

}

`

1014

1007

``

1015

``

`-

runtime->initialized = 1;

`

``

1008

`+

interp->runtime->initialized = 1;

`

``

1009

`+

}

`

1016

1010

``

1017

1011

`if (config->site_import) {

`

1018

1012

`status = init_import_site();

`

`@@ -1021,14 +1015,47 @@ pyinit_main(PyThreadState *tstate)

`

1021

1015

` }

`

1022

1016

` }

`

1023

1017

``

``

1018

`+

if (is_main_interp) {

`

1024

1019

`#ifndef MS_WINDOWS

`

1025

``

`-

emit_stderr_warning_for_legacy_locale(runtime);

`

``

1020

`+

emit_stderr_warning_for_legacy_locale(interp->runtime);

`

1026

1021

`#endif

`

``

1022

`+

}

`

1027

1023

``

1028

1024

`return _PyStatus_OK();

`

1029

1025

`}

`

1030

1026

``

1031

1027

``

``

1028

`+

/* Update interpreter state based on supplied configuration settings

`

``

1029

`+

`

``

1030

`+

`

``

1031

`+

`

``

1032

`+

`

``

1033

`+

`

``

1034

`+

`

``

1035

`+

`

``

1036

`+

`

``

1037

`+

`

``

1038

`+

*/

`

``

1039

`+

static PyStatus

`

``

1040

`+

pyinit_main(PyThreadState *tstate)

`

``

1041

`+

{

`

``

1042

`+

PyInterpreterState *interp = tstate->interp;

`

``

1043

`+

if (!interp->runtime->core_initialized) {

`

``

1044

`+

return _PyStatus_ERR("runtime core not initialized");

`

``

1045

`+

}

`

``

1046

+

``

1047

`+

if (interp->runtime->initialized) {

`

``

1048

`+

return _Py_ReconfigureMainInterpreter(tstate);

`

``

1049

`+

}

`

``

1050

+

``

1051

`+

PyStatus status = init_interp_main(tstate);

`

``

1052

`+

if (_PyStatus_EXCEPTION(status)) {

`

``

1053

`+

return status;

`

``

1054

`+

}

`

``

1055

`+

return _PyStatus_OK();

`

``

1056

`+

}

`

``

1057

+

``

1058

+

1032

1059

`PyStatus

`

1033

1060

`_Py_InitializeMain(void)

`

1034

1061

`{

`

`@@ -1440,6 +1467,7 @@ Py_Finalize(void)

`

1440

1467

`Py_FinalizeEx();

`

1441

1468

`}

`

1442

1469

``

``

1470

+

1443

1471

`/* Create and initialize a new interpreter and thread, and return the

`

1444

1472

` new thread. This requires that Py_Initialize() has been called

`

1445

1473

` first.

`

`@@ -1499,7 +1527,7 @@ new_interpreter(PyThreadState **tstate_p)

`

1499

1527

``

1500

1528

`status = _PyConfig_Copy(&interp->config, config);

`

1501

1529

`if (_PyStatus_EXCEPTION(status)) {

`

1502

``

`-

return status;

`

``

1530

`+

goto done;

`

1503

1531

` }

`

1504

1532

`config = &interp->config;

`

1505

1533

``

`@@ -1508,109 +1536,87 @@ new_interpreter(PyThreadState **tstate_p)

`

1508

1536

`/* XXX The following is lax in error checking */

`

1509

1537

`PyObject *modules = PyDict_New();

`

1510

1538

`if (modules == NULL) {

`

1511

``

`-

return _PyStatus_ERR("can't make modules dictionary");

`

``

1539

`+

status = _PyStatus_ERR("can't make modules dictionary");

`

``

1540

`+

goto done;

`

1512

1541

` }

`

1513

1542

`interp->modules = modules;

`

1514

1543

``

1515

1544

`PyObject *sysmod = _PyImport_FindBuiltin(tstate, "sys");

`

1516

1545

`if (sysmod != NULL) {

`

1517

1546

`interp->sysdict = PyModule_GetDict(sysmod);

`

1518

1547

`if (interp->sysdict == NULL) {

`

1519

``

`-

goto handle_error;

`

``

1548

`+

goto handle_exc;

`

1520

1549

` }

`

1521

1550

`Py_INCREF(interp->sysdict);

`

1522

1551

`PyDict_SetItemString(interp->sysdict, "modules", modules);

`

1523

1552

`if (_PySys_InitMain(tstate) < 0) {

`

1524

``

`-

return _PyStatus_ERR("can't finish initializing sys");

`

``

1553

`+

status = _PyStatus_ERR("can't finish initializing sys");

`

``

1554

`+

goto done;

`

1525

1555

` }

`

1526

1556

` }

`

1527

1557

`else if (_PyErr_Occurred(tstate)) {

`

1528

``

`-

goto handle_error;

`

``

1558

`+

goto handle_exc;

`

1529

1559

` }

`

1530

1560

``

1531

1561

`PyObject *bimod = _PyImport_FindBuiltin(tstate, "builtins");

`

1532

1562

`if (bimod != NULL) {

`

1533

1563

`interp->builtins = PyModule_GetDict(bimod);

`

1534

1564

`if (interp->builtins == NULL)

`

1535

``

`-

goto handle_error;

`

``

1565

`+

goto handle_exc;

`

1536

1566

`Py_INCREF(interp->builtins);

`

1537

1567

` }

`

1538

1568

`else if (_PyErr_Occurred(tstate)) {

`

1539

``

`-

goto handle_error;

`

``

1569

`+

goto handle_exc;

`

1540

1570

` }

`

1541

1571

``

1542

1572

`if (bimod != NULL && sysmod != NULL) {

`

1543

1573

`status = _PyBuiltins_AddExceptions(bimod);

`

1544

1574

`if (_PyStatus_EXCEPTION(status)) {

`

1545

``

`-

return status;

`

``

1575

`+

goto done;

`

1546

1576

` }

`

1547

1577

``

1548

1578

`status = _PySys_SetPreliminaryStderr(interp->sysdict);

`

1549

1579

`if (_PyStatus_EXCEPTION(status)) {

`

1550

``

`-

return status;

`

``

1580

`+

goto done;

`

1551

1581

` }

`

1552

1582

``

1553

1583

`status = _PyImportHooks_Init(tstate);

`

1554

1584

`if (_PyStatus_EXCEPTION(status)) {

`

1555

``

`-

return status;

`

``

1585

`+

goto done;

`

1556

1586

` }

`

1557

1587

``

1558

1588

`status = init_importlib(tstate, sysmod);

`

1559

1589

`if (_PyStatus_EXCEPTION(status)) {

`

1560

``

`-

return status;

`

``

1590

`+

goto done;

`

1561

1591

` }

`

1562

1592

``

1563

``

`-

status = init_importlib_external(tstate);

`

``

1593

`+

status = init_interp_main(tstate);

`

1564

1594

`if (_PyStatus_EXCEPTION(status)) {

`

1565

``

`-

return status;

`

1566

``

`-

}

`

1567

``

-

1568

``

`-

status = _PyUnicode_InitEncodings(tstate);

`

1569

``

`-

if (_PyStatus_EXCEPTION(status)) {

`

1570

``

`-

return status;

`

1571

``

`-

}

`

1572

``

-

1573

``

`-

status = init_sys_streams(tstate);

`

1574

``

`-

if (_PyStatus_EXCEPTION(status)) {

`

1575

``

`-

return status;

`

1576

``

`-

}

`

1577

``

-

1578

``

`-

status = init_set_builtins_open(tstate);

`

1579

``

`-

if (_PyStatus_EXCEPTION(status)) {

`

1580

``

`-

return status;

`

1581

``

`-

}

`

1582

``

-

1583

``

`-

status = add_main_module(interp);

`

1584

``

`-

if (_PyStatus_EXCEPTION(status)) {

`

1585

``

`-

return status;

`

1586

``

`-

}

`

1587

``

-

1588

``

`-

if (config->site_import) {

`

1589

``

`-

status = init_import_site();

`

1590

``

`-

if (_PyStatus_EXCEPTION(status)) {

`

1591

``

`-

return status;

`

1592

``

`-

}

`

``

1595

`+

goto done;

`

1593

1596

` }

`

1594

1597

` }

`

1595

1598

``

1596

1599

`if (_PyErr_Occurred(tstate)) {

`

1597

``

`-

goto handle_error;

`

``

1600

`+

goto handle_exc;

`

1598

1601

` }

`

1599

1602

``

1600

1603

`*tstate_p = tstate;

`

1601

1604

`return _PyStatus_OK();

`

1602

1605

``

1603

``

`-

handle_error:

`

1604

``

`-

/* Oops, it didn't work. Undo it all. */

`

``

1606

`+

handle_exc:

`

``

1607

`+

status = _PyStatus_OK();

`

1605

1608

``

``

1609

`+

done:

`

``

1610

`+

*tstate_p = NULL;

`

``

1611

+

``

1612

`+

/* Oops, it didn't work. Undo it all. */

`

1606

1613

`PyErr_PrintEx(0);

`

1607

1614

`PyThreadState_Clear(tstate);

`

1608

1615

`PyThreadState_Delete(tstate);

`

1609

1616

`PyInterpreterState_Delete(interp);

`

1610

1617

`PyThreadState_Swap(save_tstate);

`

1611

1618

``

1612

``

`-

*tstate_p = NULL;

`

1613

``

`-

return _PyStatus_OK();

`

``

1619

`+

return status;

`

1614

1620

`}

`

1615

1621

``

1616

1622

`PyThreadState *

`