Skip to content

Commit 070e7d5

Browse files
ncoghlanericsnowcurrently
authored andcommitted
Split up _PySys_Init().
1 parent ee647de commit 070e7d5

File tree

3 files changed

+96
-44
lines changed

3 files changed

+96
-44
lines changed

Include/pylifecycle.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ PyAPI_FUNC(const char *) _Py_gitversion(void);
7777
/* Internal -- various one-time initializations */
7878
#ifndef Py_LIMITED_API
7979
PyAPI_FUNC(PyObject *) _PyBuiltin_Init(void);
80-
PyAPI_FUNC(PyObject *) _PySys_Init(void);
80+
PyAPI_FUNC(PyObject *) _PySys_BeginInit(void);
81+
PyAPI_FUNC(int) _PySys_EndInit(PyObject *sysdict);
8182
PyAPI_FUNC(void) _PyImport_Init(void);
8283
PyAPI_FUNC(void) _PyExc_Init(PyObject * bltinmod);
8384
PyAPI_FUNC(void) _PyImportHooks_Init(void);

Python/pylifecycle.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,13 +404,15 @@ _Py_InitializeEx_Private(int install_sigs, int install_importlib)
404404
/* initialize builtin exceptions */
405405
_PyExc_Init(bimod);
406406

407-
sysmod = _PySys_Init();
407+
sysmod = _PySys_BeginInit();
408408
if (sysmod == NULL)
409409
Py_FatalError("Py_Initialize: can't initialize sys");
410410
interp->sysdict = PyModule_GetDict(sysmod);
411411
if (interp->sysdict == NULL)
412412
Py_FatalError("Py_Initialize: can't initialize sys dict");
413413
Py_INCREF(interp->sysdict);
414+
if (_PySys_EndInit(interp->sysdict) < 0)
415+
Py_FatalError("Py_Initialize: can't initialize sys");
414416
_PyImport_FixupBuiltin(sysmod, "sys");
415417
PySys_SetPath(Py_GetPath());
416418
PyDict_SetItemString(interp->sysdict, "modules",

Python/sysmodule.c

Lines changed: 91 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1900,16 +1900,7 @@ static struct PyModuleDef sysmodule = {
19001900
NULL
19011901
};
19021902

1903-
PyObject *
1904-
_PySys_Init(void)
1905-
{
1906-
PyObject *m, *sysdict, *version_info;
1907-
int res;
1908-
1909-
m = PyModule_Create(&sysmodule);
1910-
if (m == NULL)
1911-
return NULL;
1912-
sysdict = PyModule_GetDict(m);
1903+
/* Updating the sys namespace, returning NULL pointer on error */
19131904
#define SET_SYS_FROM_STRING_BORROW(key, value) \
19141905
do { \
19151906
PyObject *v = (value); \
@@ -1932,6 +1923,17 @@ _PySys_Init(void)
19321923
} \
19331924
} while (0)
19341925

1926+
PyObject *
1927+
_PySys_BeginInit(void)
1928+
{
1929+
PyObject *m, *sysdict, *version_info;
1930+
int res;
1931+
1932+
m = PyModule_Create(&sysmodule);
1933+
if (m == NULL)
1934+
return NULL;
1935+
sysdict = PyModule_GetDict(m);
1936+
19351937
/* Check that stdin is not a directory
19361938
Using shell redirection, you can redirect stdin to a directory,
19371939
crashing the Python interpreter. Catch this common mistake here
@@ -1963,25 +1965,12 @@ _PySys_Init(void)
19631965
SET_SYS_FROM_STRING("_git",
19641966
Py_BuildValue("(szz)", "CPython", _Py_gitidentifier(),
19651967
_Py_gitversion()));
1966-
SET_SYS_FROM_STRING("dont_write_bytecode",
1967-
PyBool_FromLong(Py_DontWriteBytecodeFlag));
19681968
SET_SYS_FROM_STRING("api_version",
19691969
PyLong_FromLong(PYTHON_API_VERSION));
19701970
SET_SYS_FROM_STRING("copyright",
19711971
PyUnicode_FromString(Py_GetCopyright()));
19721972
SET_SYS_FROM_STRING("platform",
19731973
PyUnicode_FromString(Py_GetPlatform()));
1974-
SET_SYS_FROM_STRING("executable",
1975-
PyUnicode_FromWideChar(
1976-
Py_GetProgramFullPath(), -1));
1977-
SET_SYS_FROM_STRING("prefix",
1978-
PyUnicode_FromWideChar(Py_GetPrefix(), -1));
1979-
SET_SYS_FROM_STRING("exec_prefix",
1980-
PyUnicode_FromWideChar(Py_GetExecPrefix(), -1));
1981-
SET_SYS_FROM_STRING("base_prefix",
1982-
PyUnicode_FromWideChar(Py_GetPrefix(), -1));
1983-
SET_SYS_FROM_STRING("base_exec_prefix",
1984-
PyUnicode_FromWideChar(Py_GetExecPrefix(), -1));
19851974
SET_SYS_FROM_STRING("maxsize",
19861975
PyLong_FromSsize_t(PY_SSIZE_T_MAX));
19871976
SET_SYS_FROM_STRING("float_info",
@@ -2017,17 +2006,6 @@ _PySys_Init(void)
20172006
SET_SYS_FROM_STRING("abiflags",
20182007
PyUnicode_FromString(ABIFLAGS));
20192008
#endif
2020-
if (warnoptions == NULL) {
2021-
warnoptions = PyList_New(0);
2022-
if (warnoptions == NULL)
2023-
return NULL;
2024-
}
2025-
else {
2026-
Py_INCREF(warnoptions);
2027-
}
2028-
SET_SYS_FROM_STRING_BORROW("warnoptions", warnoptions);
2029-
2030-
SET_SYS_FROM_STRING_BORROW("_xoptions", get_xoptions());
20312009

20322010
/* version_info */
20332011
if (VersionInfoType.tp_name == NULL) {
@@ -2052,13 +2030,8 @@ _PySys_Init(void)
20522030
if (PyStructSequence_InitType2(&FlagsType, &flags_desc) < 0)
20532031
return NULL;
20542032
}
2033+
/* Set flags to their default values */
20552034
SET_SYS_FROM_STRING("flags", make_flags());
2056-
/* prevent user from creating new instances */
2057-
FlagsType.tp_init = NULL;
2058-
FlagsType.tp_new = NULL;
2059-
res = PyDict_DelItemString(FlagsType.tp_dict, "__new__");
2060-
if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
2061-
PyErr_Clear();
20622035

20632036
#if defined(MS_WINDOWS)
20642037
/* getwindowsversion */
@@ -2095,13 +2068,89 @@ _PySys_Init(void)
20952068
}
20962069
}
20972070

2098-
#undef SET_SYS_FROM_STRING
2099-
#undef SET_SYS_FROM_STRING_BORROW
21002071
if (PyErr_Occurred())
21012072
return NULL;
21022073
return m;
21032074
}
21042075

2076+
#undef SET_SYS_FROM_STRING
2077+
#undef SET_SYS_FROM_STRING_BORROW
2078+
2079+
/* Updating the sys namespace, returning integer error codes */
2080+
#define SET_SYS_FROM_STRING_BORROW_INT_RESULT(key, value) \
2081+
do { \
2082+
PyObject *v = (value); \
2083+
if (v == NULL) \
2084+
return -1; \
2085+
res = PyDict_SetItemString(sysdict, key, v); \
2086+
if (res < 0) { \
2087+
return res; \
2088+
} \
2089+
} while (0)
2090+
#define SET_SYS_FROM_STRING_INT_RESULT(key, value) \
2091+
do { \
2092+
PyObject *v = (value); \
2093+
if (v == NULL) \
2094+
return -1; \
2095+
res = PyDict_SetItemString(sysdict, key, v); \
2096+
Py_DECREF(v); \
2097+
if (res < 0) { \
2098+
return res; \
2099+
} \
2100+
} while (0)
2101+
2102+
int
2103+
_PySys_EndInit(PyObject *sysdict)
2104+
{
2105+
int res;
2106+
2107+
/* Set flags to their final values */
2108+
SET_SYS_FROM_STRING_INT_RESULT("flags", make_flags());
2109+
/* prevent user from creating new instances */
2110+
FlagsType.tp_init = NULL;
2111+
FlagsType.tp_new = NULL;
2112+
res = PyDict_DelItemString(FlagsType.tp_dict, "__new__");
2113+
if (res < 0) {
2114+
if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2115+
return res;
2116+
}
2117+
PyErr_Clear();
2118+
}
2119+
2120+
SET_SYS_FROM_STRING_INT_RESULT("dont_write_bytecode",
2121+
PyBool_FromLong(Py_DontWriteBytecodeFlag));
2122+
SET_SYS_FROM_STRING_INT_RESULT("executable",
2123+
PyUnicode_FromWideChar(
2124+
Py_GetProgramFullPath(), -1));
2125+
SET_SYS_FROM_STRING_INT_RESULT("prefix",
2126+
PyUnicode_FromWideChar(Py_GetPrefix(), -1));
2127+
SET_SYS_FROM_STRING_INT_RESULT("exec_prefix",
2128+
PyUnicode_FromWideChar(Py_GetExecPrefix(), -1));
2129+
SET_SYS_FROM_STRING_INT_RESULT("base_prefix",
2130+
PyUnicode_FromWideChar(Py_GetPrefix(), -1));
2131+
SET_SYS_FROM_STRING_INT_RESULT("base_exec_prefix",
2132+
PyUnicode_FromWideChar(Py_GetExecPrefix(), -1));
2133+
2134+
if (warnoptions == NULL) {
2135+
warnoptions = PyList_New(0);
2136+
if (warnoptions == NULL)
2137+
return -1;
2138+
}
2139+
else {
2140+
Py_INCREF(warnoptions);
2141+
}
2142+
SET_SYS_FROM_STRING_BORROW_INT_RESULT("warnoptions", warnoptions);
2143+
2144+
SET_SYS_FROM_STRING_BORROW_INT_RESULT("_xoptions", get_xoptions());
2145+
2146+
if (PyErr_Occurred())
2147+
return -1;
2148+
return 0;
2149+
}
2150+
2151+
#undef SET_SYS_FROM_STRING_INT_RESULT
2152+
#undef SET_SYS_FROM_STRING_BORROW_INT_RESULT
2153+
21052154
static PyObject *
21062155
makepathobject(const wchar_t *path, wchar_t delim)
21072156
{

0 commit comments

Comments
 (0)