Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ab67281

Browse filesBrowse files
authored
bpo-35713: Reorganize sys module initialization (GH-11658)
* Rename _PySys_BeginInit() to _PySys_InitCore(). * Rename _PySys_EndInit() to _PySys_InitMain(). * Add _PySys_Create(). It calls _PySys_InitCore() which becomes private. * Add _PySys_SetPreliminaryStderr(). * Rename _Py_ReadyTypes() to _PyTypes_Init(). * Misc code cleanup.
1 parent cda73a5 commit ab67281
Copy full SHA for ab67281

File tree

Expand file treeCollapse file tree

4 files changed

+96
-75
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+96
-75
lines changed

‎Include/internal/pycore_pylifecycle.h

Copy file name to clipboardExpand all lines: Include/internal/pycore_pylifecycle.h
+6-3Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,19 @@ extern int _PyLong_Init(void);
2727
extern _PyInitError _PyFaulthandler_Init(int enable);
2828
extern int _PyTraceMalloc_Init(int enable);
2929
extern PyObject * _PyBuiltin_Init(void);
30-
extern _PyInitError _PySys_BeginInit(PyObject **sysmod);
31-
extern int _PySys_EndInit(PyObject *sysdict, PyInterpreterState *interp);
30+
extern _PyInitError _PySys_Create(
31+
PyInterpreterState *interp,
32+
PyObject **sysmod_p);
33+
extern _PyInitError _PySys_SetPreliminaryStderr(PyObject *sysdict);
34+
extern int _PySys_InitMain(PyInterpreterState *interp);
3235
extern _PyInitError _PyImport_Init(PyInterpreterState *interp);
3336
extern _PyInitError _PyExc_Init(void);
3437
extern _PyInitError _PyBuiltins_AddExceptions(PyObject * bltinmod);
3538
extern _PyInitError _PyImportHooks_Init(void);
3639
extern int _PyFloat_Init(void);
3740
extern _PyInitError _Py_HashRandomization_Init(const _PyCoreConfig *);
3841

39-
extern _PyInitError _Py_ReadyTypes(void);
42+
extern _PyInitError _PyTypes_Init(void);
4043

4144
/* Various internal finalizers */
4245

‎Objects/object.c

Copy file name to clipboardExpand all lines: Objects/object.c
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1717,7 +1717,7 @@ PyObject _Py_NotImplementedStruct = {
17171717
};
17181718

17191719
_PyInitError
1720-
_Py_ReadyTypes(void)
1720+
_PyTypes_Init(void)
17211721
{
17221722
#define INIT_TYPE(TYPE, NAME) \
17231723
do { \

‎Python/pylifecycle.c

Copy file name to clipboardExpand all lines: Python/pylifecycle.c
+9-52Lines changed: 9 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ pycore_create_interpreter(const _PyCoreConfig *core_config,
589589
static _PyInitError
590590
pycore_init_types(void)
591591
{
592-
_PyInitError err = _Py_ReadyTypes();
592+
_PyInitError err = _PyTypes_Init();
593593
if (_Py_INIT_FAILED(err)) {
594594
return err;
595595
}
@@ -623,46 +623,6 @@ pycore_init_types(void)
623623
}
624624

625625

626-
static _PyInitError
627-
pycore_init_sys(PyInterpreterState *interp, PyObject **sysmod_p)
628-
{
629-
PyObject *modules = PyDict_New();
630-
if (modules == NULL)
631-
return _Py_INIT_ERR("can't make modules dictionary");
632-
interp->modules = modules;
633-
634-
PyObject *sysmod;
635-
_PyInitError err = _PySys_BeginInit(&sysmod);
636-
if (_Py_INIT_FAILED(err)) {
637-
return err;
638-
}
639-
*sysmod_p = sysmod;
640-
641-
interp->sysdict = PyModule_GetDict(sysmod);
642-
if (interp->sysdict == NULL) {
643-
return _Py_INIT_ERR("can't initialize sys dict");
644-
}
645-
646-
Py_INCREF(interp->sysdict);
647-
PyDict_SetItemString(interp->sysdict, "modules", modules);
648-
_PyImport_FixupBuiltin(sysmod, "sys", modules);
649-
650-
/* Set up a preliminary stderr printer until we have enough
651-
infrastructure for the io module in place.
652-
653-
Use UTF-8/surrogateescape and ignore EAGAIN errors. */
654-
PyObject *pstderr = PyFile_NewStdPrinter(fileno(stderr));
655-
if (pstderr == NULL) {
656-
return _Py_INIT_ERR("can't set preliminary stderr");
657-
}
658-
_PySys_SetObjectId(&PyId_stderr, pstderr);
659-
PySys_SetObject("__stderr__", pstderr);
660-
Py_DECREF(pstderr);
661-
662-
return _Py_INIT_OK();
663-
}
664-
665-
666626
static _PyInitError
667627
pycore_init_builtins(PyInterpreterState *interp)
668628
{
@@ -746,7 +706,7 @@ _Py_InitializeCore_impl(PyInterpreterState **interp_p,
746706
}
747707

748708
PyObject *sysmod;
749-
err = pycore_init_sys(interp, &sysmod);
709+
err = _PySys_Create(interp, &sysmod);
750710
if (_Py_INIT_FAILED(err)) {
751711
return err;
752712
}
@@ -887,7 +847,7 @@ _Py_InitializeMainInterpreter(PyInterpreterState *interp,
887847
return _Py_INIT_ERR("can't initialize time");
888848
}
889849

890-
if (_PySys_EndInit(interp->sysdict, interp) < 0) {
850+
if (_PySys_InitMain(interp) < 0) {
891851
return _Py_INIT_ERR("can't finish initializing sys");
892852
}
893853

@@ -1376,7 +1336,9 @@ new_interpreter(PyThreadState **tstate_p)
13761336
goto handle_error;
13771337
Py_INCREF(interp->sysdict);
13781338
PyDict_SetItemString(interp->sysdict, "modules", modules);
1379-
_PySys_EndInit(interp->sysdict, interp);
1339+
if (_PySys_InitMain(interp) < 0) {
1340+
return _Py_INIT_ERR("can't finish initializing sys");
1341+
}
13801342
}
13811343
else if (PyErr_Occurred()) {
13821344
goto handle_error;
@@ -1399,15 +1361,10 @@ new_interpreter(PyThreadState **tstate_p)
13991361
return err;
14001362
}
14011363

1402-
/* Set up a preliminary stderr printer until we have enough
1403-
infrastructure for the io module in place. */
1404-
PyObject *pstderr = PyFile_NewStdPrinter(fileno(stderr));
1405-
if (pstderr == NULL) {
1406-
return _Py_INIT_ERR("can't set preliminary stderr");
1364+
err = _PySys_SetPreliminaryStderr(interp->sysdict);
1365+
if (_Py_INIT_FAILED(err)) {
1366+
return err;
14071367
}
1408-
_PySys_SetObjectId(&PyId_stderr, pstderr);
1409-
PySys_SetObject("__stderr__", pstderr);
1410-
Py_DECREF(pstderr);
14111368

14121369
err = _PyImportHooks_Init();
14131370
if (_Py_INIT_FAILED(err)) {

‎Python/sysmodule.c

Copy file name to clipboardExpand all lines: Python/sysmodule.c
+80-19Lines changed: 80 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2368,19 +2368,12 @@ static struct PyModuleDef sysmodule = {
23682368
} \
23692369
} while (0)
23702370

2371-
2372-
_PyInitError
2373-
_PySys_BeginInit(PyObject **sysmod)
2371+
static _PyInitError
2372+
_PySys_InitCore(PyObject *sysdict)
23742373
{
2375-
PyObject *m, *sysdict, *version_info;
2374+
PyObject *version_info;
23762375
int res;
23772376

2378-
m = _PyModule_CreateInitialized(&sysmodule, PYTHON_API_VERSION);
2379-
if (m == NULL) {
2380-
return _Py_INIT_ERR("failed to create a module object");
2381-
}
2382-
sysdict = PyModule_GetDict(m);
2383-
23842377
/* stdin/stdout/stderr are set in pylifecycle.c */
23852378

23862379
SET_SYS_FROM_STRING_BORROW("__displayhook__",
@@ -2508,9 +2501,6 @@ _PySys_BeginInit(PyObject **sysmod)
25082501
if (PyErr_Occurred()) {
25092502
goto err_occurred;
25102503
}
2511-
2512-
*sysmod = m;
2513-
25142504
return _Py_INIT_OK();
25152505

25162506
type_init_failed:
@@ -2536,8 +2526,9 @@ _PySys_BeginInit(PyObject **sysmod)
25362526
} while (0)
25372527

25382528
int
2539-
_PySys_EndInit(PyObject *sysdict, PyInterpreterState *interp)
2529+
_PySys_InitMain(PyInterpreterState *interp)
25402530
{
2531+
PyObject *sysdict = interp->sysdict;
25412532
const _PyCoreConfig *core_config = &interp->core_config;
25422533
const _PyMainInterpreterConfig *config = &interp->config;
25432534
int res;
@@ -2552,17 +2543,16 @@ _PySys_EndInit(PyObject *sysdict, PyInterpreterState *interp)
25522543

25532544
#define COPY_LIST(KEY, ATTR) \
25542545
do { \
2555-
assert(PyList_Check(config->ATTR)); \
2556-
PyObject *list = PyList_GetSlice(config->ATTR, \
2557-
0, PyList_GET_SIZE(config->ATTR)); \
2546+
assert(PyList_Check(ATTR)); \
2547+
PyObject *list = PyList_GetSlice(ATTR, 0, PyList_GET_SIZE(ATTR)); \
25582548
if (list == NULL) { \
25592549
return -1; \
25602550
} \
25612551
SET_SYS_FROM_STRING_BORROW(KEY, list); \
25622552
Py_DECREF(list); \
25632553
} while (0)
25642554

2565-
COPY_LIST("path", module_search_path);
2555+
COPY_LIST("path", config->module_search_path);
25662556

25672557
SET_SYS_FROM_STRING_BORROW("executable", config->executable);
25682558
SET_SYS_FROM_STRING_BORROW("prefix", config->prefix);
@@ -2580,7 +2570,7 @@ _PySys_EndInit(PyObject *sysdict, PyInterpreterState *interp)
25802570
SET_SYS_FROM_STRING_BORROW("argv", config->argv);
25812571
}
25822572
if (config->warnoptions != NULL) {
2583-
COPY_LIST("warnoptions", warnoptions);
2573+
COPY_LIST("warnoptions", config->warnoptions);
25842574
}
25852575
if (config->xoptions != NULL) {
25862576
PyObject *dict = PyDict_Copy(config->xoptions);
@@ -2631,6 +2621,77 @@ _PySys_EndInit(PyObject *sysdict, PyInterpreterState *interp)
26312621
#undef SET_SYS_FROM_STRING_BORROW
26322622
#undef SET_SYS_FROM_STRING_INT_RESULT
26332623

2624+
2625+
/* Set up a preliminary stderr printer until we have enough
2626+
infrastructure for the io module in place.
2627+
2628+
Use UTF-8/surrogateescape and ignore EAGAIN errors. */
2629+
_PyInitError
2630+
_PySys_SetPreliminaryStderr(PyObject *sysdict)
2631+
{
2632+
PyObject *pstderr = PyFile_NewStdPrinter(fileno(stderr));
2633+
if (pstderr == NULL) {
2634+
goto error;
2635+
}
2636+
if (_PyDict_SetItemId(sysdict, &PyId_stderr, pstderr) < 0) {
2637+
goto error;
2638+
}
2639+
if (PyDict_SetItemString(sysdict, "__stderr__", pstderr) < 0) {
2640+
goto error;
2641+
}
2642+
Py_DECREF(pstderr);
2643+
return _Py_INIT_OK();
2644+
2645+
error:
2646+
Py_XDECREF(pstderr);
2647+
return _Py_INIT_ERR("can't set preliminary stderr");
2648+
}
2649+
2650+
2651+
/* Create sys module without all attributes: _PySys_InitMain() should be called
2652+
later to add remaining attributes. */
2653+
_PyInitError
2654+
_PySys_Create(PyInterpreterState *interp, PyObject **sysmod_p)
2655+
{
2656+
PyObject *modules = PyDict_New();
2657+
if (modules == NULL) {
2658+
return _Py_INIT_ERR("can't make modules dictionary");
2659+
}
2660+
interp->modules = modules;
2661+
2662+
PyObject *sysmod = _PyModule_CreateInitialized(&sysmodule, PYTHON_API_VERSION);
2663+
if (sysmod == NULL) {
2664+
return _Py_INIT_ERR("failed to create a module object");
2665+
}
2666+
2667+
PyObject *sysdict = PyModule_GetDict(sysmod);
2668+
if (sysdict == NULL) {
2669+
return _Py_INIT_ERR("can't initialize sys dict");
2670+
}
2671+
Py_INCREF(sysdict);
2672+
interp->sysdict = sysdict;
2673+
2674+
if (PyDict_SetItemString(sysdict, "modules", interp->modules) < 0) {
2675+
return _Py_INIT_ERR("can't initialize sys module");
2676+
}
2677+
2678+
_PyInitError err = _PySys_SetPreliminaryStderr(sysdict);
2679+
if (_Py_INIT_FAILED(err)) {
2680+
return err;
2681+
}
2682+
2683+
err = _PySys_InitCore(sysdict);
2684+
if (_Py_INIT_FAILED(err)) {
2685+
return err;
2686+
}
2687+
2688+
_PyImport_FixupBuiltin(sysmod, "sys", interp->modules);
2689+
2690+
*sysmod_p = sysmod;
2691+
return _Py_INIT_OK();
2692+
}
2693+
2694+
26342695
static PyObject *
26352696
makepathobject(const wchar_t *path, wchar_t delim)
26362697
{

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.