diff options
author | Eric Snow <ericsnowcurrently@gmail.com> | 2017-05-23 04:36:03 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-23 04:36:03 (GMT) |
commit | 6b4be195cd8868b76eb6fbe166acc39beee8ce36 (patch) | |
tree | bbab44fad32c576b9eb7e4b83368e200adc33f00 /Python/sysmodule.c | |
parent | f9169ce6b48c7cc7cc62d9eb5e4ee1ac7066d14b (diff) | |
download | cpython-6b4be195cd8868b76eb6fbe166acc39beee8ce36.zip cpython-6b4be195cd8868b76eb6fbe166acc39beee8ce36.tar.gz cpython-6b4be195cd8868b76eb6fbe166acc39beee8ce36.tar.bz2 |
bpo-22257: Small changes for PEP 432. (#1728)
PEP 432 specifies a number of large changes to interpreter startup code, including exposing a cleaner C-API. The major changes depend on a number of smaller changes. This patch includes all those smaller changes.
Diffstat (limited to 'Python/sysmodule.c')
-rw-r--r-- | Python/sysmodule.c | 133 |
1 files changed, 91 insertions, 42 deletions
diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 2da7702..a7b7508 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -1900,16 +1900,7 @@ static struct PyModuleDef sysmodule = { NULL }; -PyObject * -_PySys_Init(void) -{ - PyObject *m, *sysdict, *version_info; - int res; - - m = PyModule_Create(&sysmodule); - if (m == NULL) - return NULL; - sysdict = PyModule_GetDict(m); +/* Updating the sys namespace, returning NULL pointer on error */ #define SET_SYS_FROM_STRING_BORROW(key, value) \ do { \ PyObject *v = (value); \ @@ -1932,6 +1923,17 @@ _PySys_Init(void) } \ } while (0) +PyObject * +_PySys_BeginInit(void) +{ + PyObject *m, *sysdict, *version_info; + int res; + + m = PyModule_Create(&sysmodule); + if (m == NULL) + return NULL; + sysdict = PyModule_GetDict(m); + /* Check that stdin is not a directory Using shell redirection, you can redirect stdin to a directory, crashing the Python interpreter. Catch this common mistake here @@ -1963,25 +1965,12 @@ _PySys_Init(void) SET_SYS_FROM_STRING("_git", Py_BuildValue("(szz)", "CPython", _Py_gitidentifier(), _Py_gitversion())); - SET_SYS_FROM_STRING("dont_write_bytecode", - PyBool_FromLong(Py_DontWriteBytecodeFlag)); SET_SYS_FROM_STRING("api_version", PyLong_FromLong(PYTHON_API_VERSION)); SET_SYS_FROM_STRING("copyright", PyUnicode_FromString(Py_GetCopyright())); SET_SYS_FROM_STRING("platform", PyUnicode_FromString(Py_GetPlatform())); - SET_SYS_FROM_STRING("executable", - PyUnicode_FromWideChar( - Py_GetProgramFullPath(), -1)); - SET_SYS_FROM_STRING("prefix", - PyUnicode_FromWideChar(Py_GetPrefix(), -1)); - SET_SYS_FROM_STRING("exec_prefix", - PyUnicode_FromWideChar(Py_GetExecPrefix(), -1)); - SET_SYS_FROM_STRING("base_prefix", - PyUnicode_FromWideChar(Py_GetPrefix(), -1)); - SET_SYS_FROM_STRING("base_exec_prefix", - PyUnicode_FromWideChar(Py_GetExecPrefix(), -1)); SET_SYS_FROM_STRING("maxsize", PyLong_FromSsize_t(PY_SSIZE_T_MAX)); SET_SYS_FROM_STRING("float_info", @@ -2017,17 +2006,6 @@ _PySys_Init(void) SET_SYS_FROM_STRING("abiflags", PyUnicode_FromString(ABIFLAGS)); #endif - if (warnoptions == NULL) { - warnoptions = PyList_New(0); - if (warnoptions == NULL) - return NULL; - } - else { - Py_INCREF(warnoptions); - } - SET_SYS_FROM_STRING_BORROW("warnoptions", warnoptions); - - SET_SYS_FROM_STRING_BORROW("_xoptions", get_xoptions()); /* version_info */ if (VersionInfoType.tp_name == NULL) { @@ -2052,13 +2030,8 @@ _PySys_Init(void) if (PyStructSequence_InitType2(&FlagsType, &flags_desc) < 0) return NULL; } + /* Set flags to their default values */ SET_SYS_FROM_STRING("flags", make_flags()); - /* prevent user from creating new instances */ - FlagsType.tp_init = NULL; - FlagsType.tp_new = NULL; - res = PyDict_DelItemString(FlagsType.tp_dict, "__new__"); - if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) - PyErr_Clear(); #if defined(MS_WINDOWS) /* getwindowsversion */ @@ -2095,13 +2068,89 @@ _PySys_Init(void) } } -#undef SET_SYS_FROM_STRING -#undef SET_SYS_FROM_STRING_BORROW if (PyErr_Occurred()) return NULL; return m; } +#undef SET_SYS_FROM_STRING +#undef SET_SYS_FROM_STRING_BORROW + +/* Updating the sys namespace, returning integer error codes */ +#define SET_SYS_FROM_STRING_BORROW_INT_RESULT(key, value) \ + do { \ + PyObject *v = (value); \ + if (v == NULL) \ + return -1; \ + res = PyDict_SetItemString(sysdict, key, v); \ + if (res < 0) { \ + return res; \ + } \ + } while (0) +#define SET_SYS_FROM_STRING_INT_RESULT(key, value) \ + do { \ + PyObject *v = (value); \ + if (v == NULL) \ + return -1; \ + res = PyDict_SetItemString(sysdict, key, v); \ + Py_DECREF(v); \ + if (res < 0) { \ + return res; \ + } \ + } while (0) + +int +_PySys_EndInit(PyObject *sysdict) +{ + int res; + + /* Set flags to their final values */ + SET_SYS_FROM_STRING_INT_RESULT("flags", make_flags()); + /* prevent user from creating new instances */ + FlagsType.tp_init = NULL; + FlagsType.tp_new = NULL; + res = PyDict_DelItemString(FlagsType.tp_dict, "__new__"); + if (res < 0) { + if (!PyErr_ExceptionMatches(PyExc_KeyError)) { + return res; + } + PyErr_Clear(); + } + + SET_SYS_FROM_STRING_INT_RESULT("dont_write_bytecode", + PyBool_FromLong(Py_DontWriteBytecodeFlag)); + SET_SYS_FROM_STRING_INT_RESULT("executable", + PyUnicode_FromWideChar( + Py_GetProgramFullPath(), -1)); + SET_SYS_FROM_STRING_INT_RESULT("prefix", + PyUnicode_FromWideChar(Py_GetPrefix(), -1)); + SET_SYS_FROM_STRING_INT_RESULT("exec_prefix", + PyUnicode_FromWideChar(Py_GetExecPrefix(), -1)); + SET_SYS_FROM_STRING_INT_RESULT("base_prefix", + PyUnicode_FromWideChar(Py_GetPrefix(), -1)); + SET_SYS_FROM_STRING_INT_RESULT("base_exec_prefix", + PyUnicode_FromWideChar(Py_GetExecPrefix(), -1)); + + if (warnoptions == NULL) { + warnoptions = PyList_New(0); + if (warnoptions == NULL) + return -1; + } + else { + Py_INCREF(warnoptions); + } + SET_SYS_FROM_STRING_BORROW_INT_RESULT("warnoptions", warnoptions); + + SET_SYS_FROM_STRING_BORROW_INT_RESULT("_xoptions", get_xoptions()); + + if (PyErr_Occurred()) + return -1; + return 0; +} + +#undef SET_SYS_FROM_STRING_INT_RESULT +#undef SET_SYS_FROM_STRING_BORROW_INT_RESULT + static PyObject * makepathobject(const wchar_t *path, wchar_t delim) { |