summaryrefslogtreecommitdiffstats
path: root/Python/pylifecycle.c
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@redhat.com>2019-01-22 16:39:03 (GMT)
committerGitHub <noreply@github.com>2019-01-22 16:39:03 (GMT)
commitbf4ac2d2fd520c61306b2676db488adab9b5d8c5 (patch)
tree36b7680e9ac88256ba0f3beeb834c677a20914f8 /Python/pylifecycle.c
parent35ca1820e19f81f69073f294503cdcd708fe490f (diff)
downloadcpython-bf4ac2d2fd520c61306b2676db488adab9b5d8c5.zip
cpython-bf4ac2d2fd520c61306b2676db488adab9b5d8c5.tar.gz
cpython-bf4ac2d2fd520c61306b2676db488adab9b5d8c5.tar.bz2
bpo-35713: Rework Python initialization (GH-11647)
* The PyByteArray_Init() and PyByteArray_Fini() functions have been removed. They did nothing since Python 2.7.4 and Python 3.2.0, were excluded from the limited API (stable ABI), and were not documented. * Move "_PyXXX_Init()" and "_PyXXX_Fini()" declarations from Include/cpython/pylifecycle.h to Include/internal/pycore_pylifecycle.h. Replace "PyAPI_FUNC(TYPE)" with "extern TYPE". * _PyExc_Init() now returns an error on failure rather than calling Py_FatalError(). Move macros inside _PyExc_Init() and undefine them when done. Rewrite macros to make them look more like statement: add ";" when using them, add "do { ... } while (0)". * _PyUnicode_Init() now returns a _PyInitError error rather than call Py_FatalError(). * Move stdin check from _PySys_BeginInit() to init_sys_streams(). * _Py_ReadyTypes() now returns a _PyInitError error rather than calling Py_FatalError().
Diffstat (limited to 'Python/pylifecycle.c')
-rw-r--r--Python/pylifecycle.c35
1 files changed, 26 insertions, 9 deletions
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index 37ecc51..f0e00ea 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -608,9 +608,6 @@ _Py_InitializeCore_impl(PyInterpreterState **interp_p,
if (!_PyLong_Init())
return _Py_INIT_ERR("can't init longs");
- if (!PyByteArray_Init())
- return _Py_INIT_ERR("can't init bytearray");
-
if (!_PyFloat_Init())
return _Py_INIT_ERR("can't init float");
@@ -634,9 +631,10 @@ _Py_InitializeCore_impl(PyInterpreterState **interp_p,
PyDict_SetItemString(interp->sysdict, "modules", modules);
_PyImport_FixupBuiltin(sysmod, "sys", modules);
- /* Init Unicode implementation; relies on the codec registry */
- if (_PyUnicode_Init() < 0)
- return _Py_INIT_ERR("can't initialize unicode");
+ err = _PyUnicode_Init();
+ if (_Py_INIT_FAILED(err)) {
+ return err;
+ }
if (_PyStructSequence_Init() < 0)
return _Py_INIT_ERR("can't initialize structseq");
@@ -651,7 +649,10 @@ _Py_InitializeCore_impl(PyInterpreterState **interp_p,
Py_INCREF(interp->builtins);
/* initialize builtin exceptions */
- _PyExc_Init(bimod);
+ err = _PyExc_Init(bimod);
+ if (_Py_INIT_FAILED(err)) {
+ return err;
+ }
/* Set up a preliminary stderr printer until we have enough
infrastructure for the io module in place. */
@@ -1146,7 +1147,6 @@ Py_FinalizeEx(void)
PyList_Fini();
PySet_Fini();
PyBytes_Fini();
- PyByteArray_Fini();
PyLong_Fini();
PyFloat_Fini();
PyDict_Fini();
@@ -1302,7 +1302,10 @@ new_interpreter(PyThreadState **tstate_p)
}
/* initialize builtin exceptions */
- _PyExc_Init(bimod);
+ err = _PyExc_Init(bimod);
+ if (_Py_INIT_FAILED(err)) {
+ return err;
+ }
if (bimod != NULL && sysmod != NULL) {
PyObject *pstderr;
@@ -1682,6 +1685,20 @@ init_sys_streams(PyInterpreterState *interp)
_PyInitError res = _Py_INIT_OK();
_PyCoreConfig *config = &interp->core_config;
+ /* 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
+ and output a useful error message. Note that under MS Windows,
+ the shell already prevents that. */
+#ifndef MS_WINDOWS
+ struct _Py_stat_struct sb;
+ if (_Py_fstat_noraise(fileno(stdin), &sb) == 0 &&
+ S_ISDIR(sb.st_mode)) {
+ return _Py_INIT_USER_ERR("<stdin> is a directory, "
+ "cannot continue");
+ }
+#endif
+
char *codec_name = get_codec_name(config->stdio_encoding);
if (codec_name == NULL) {
return _Py_INIT_ERR("failed to get the Python codec name "