@@ -64,6 +64,7 @@ extern grammar _PyParser_Grammar; /* From graminit.c */
6464/* Forward declarations */
6565static PyStatus add_main_module (PyInterpreterState * interp );
6666static PyStatus init_import_site (void );
67+ static PyStatus init_set_builtins_open (PyThreadState * tstate );
6768static PyStatus init_sys_streams (PyThreadState * tstate );
6869static PyStatus init_signals (PyThreadState * tstate );
6970static void call_py_exitfuncs (PyThreadState * tstate );
@@ -994,6 +995,11 @@ pyinit_main(PyThreadState *tstate)
994995 return status ;
995996 }
996997
998+ status = init_set_builtins_open (tstate );
999+ if (_PyStatus_EXCEPTION (status )) {
1000+ return status ;
1001+ }
1002+
9971003 /* Initialize warnings. */
9981004 PyObject * warnoptions = PySys_GetObject ("warnoptions" );
9991005 if (warnoptions != NULL && PyList_Size (warnoptions ) > 0 )
@@ -1569,6 +1575,11 @@ new_interpreter(PyThreadState **tstate_p)
15691575 return status ;
15701576 }
15711577
1578+ status = init_set_builtins_open (tstate );
1579+ if (_PyStatus_EXCEPTION (status )) {
1580+ return status ;
1581+ }
1582+
15721583 status = add_main_module (interp );
15731584 if (_PyStatus_EXCEPTION (status )) {
15741585 return status ;
@@ -1891,12 +1902,49 @@ create_stdio(const PyConfig *config, PyObject* io,
18911902 return NULL ;
18921903}
18931904
1894- /* Initialize sys.stdin, stdout, stderr and builtins.open */
1905+ /* Set builtins.open to io.OpenWrapper */
18951906static PyStatus
1896- init_sys_streams (PyThreadState * tstate )
1907+ init_set_builtins_open (PyThreadState * tstate )
18971908{
18981909 PyObject * iomod = NULL , * wrapper ;
18991910 PyObject * bimod = NULL ;
1911+ PyStatus res = _PyStatus_OK ();
1912+
1913+ if (!(iomod = PyImport_ImportModule ("io" ))) {
1914+ goto error ;
1915+ }
1916+
1917+ if (!(bimod = PyImport_ImportModule ("builtins" ))) {
1918+ goto error ;
1919+ }
1920+
1921+ if (!(wrapper = PyObject_GetAttrString (iomod , "OpenWrapper" ))) {
1922+ goto error ;
1923+ }
1924+
1925+ /* Set builtins.open */
1926+ if (PyObject_SetAttrString (bimod , "open" , wrapper ) == -1 ) {
1927+ Py_DECREF (wrapper );
1928+ goto error ;
1929+ }
1930+ Py_DECREF (wrapper );
1931+ goto done ;
1932+
1933+ error :
1934+ res = _PyStatus_ERR ("can't initialize io.open" );
1935+
1936+ done :
1937+ Py_XDECREF (bimod );
1938+ Py_XDECREF (iomod );
1939+ return res ;
1940+ }
1941+
1942+
1943+ /* Initialize sys.stdin, stdout, stderr and builtins.open */
1944+ static PyStatus
1945+ init_sys_streams (PyThreadState * tstate )
1946+ {
1947+ PyObject * iomod = NULL ;
19001948 PyObject * m ;
19011949 PyObject * std = NULL ;
19021950 int fd ;
@@ -1929,23 +1977,9 @@ init_sys_streams(PyThreadState *tstate)
19291977 }
19301978 Py_DECREF (m );
19311979
1932- if (!(bimod = PyImport_ImportModule ("builtins" ))) {
1933- goto error ;
1934- }
1935-
19361980 if (!(iomod = PyImport_ImportModule ("io" ))) {
19371981 goto error ;
19381982 }
1939- if (!(wrapper = PyObject_GetAttrString (iomod , "OpenWrapper" ))) {
1940- goto error ;
1941- }
1942-
1943- /* Set builtins.open */
1944- if (PyObject_SetAttrString (bimod , "open" , wrapper ) == -1 ) {
1945- Py_DECREF (wrapper );
1946- goto error ;
1947- }
1948- Py_DECREF (wrapper );
19491983
19501984 /* Set sys.stdin */
19511985 fd = fileno (stdin );
@@ -2013,8 +2047,6 @@ init_sys_streams(PyThreadState *tstate)
20132047
20142048done :
20152049 _Py_ClearStandardStreamEncoding ();
2016-
2017- Py_XDECREF (bimod );
20182050 Py_XDECREF (iomod );
20192051 return res ;
20202052}
0 commit comments