@@ -67,44 +67,69 @@ static PyObject* pyrun_file(FILE *fp, PyObject *filename, int start,
6767 PyCompilerFlags * flags );
6868
6969
70- /* Parse input from a file and execute it */
7170int
72- PyRun_AnyFileExFlags (FILE * fp , const char * filename , int closeit ,
71+ _PyRun_AnyFileObject (FILE * fp , PyObject * filename , int closeit ,
7372 PyCompilerFlags * flags )
7473{
75- if (filename == NULL )
76- filename = "???" ;
77- if (Py_FdIsInteractive (fp , filename )) {
78- int err = PyRun_InteractiveLoopFlags (fp , filename , flags );
79- if (closeit )
74+ int decref_filename = 0 ;
75+ if (filename == NULL ) {
76+ filename = PyUnicode_FromString ("???" );
77+ if (filename == NULL ) {
78+ PyErr_Print ();
79+ return -1 ;
80+ }
81+ decref_filename = 1 ;
82+ }
83+
84+ int res ;
85+ if (_Py_FdIsInteractive (fp , filename )) {
86+ res = _PyRun_InteractiveLoopObject (fp , filename , flags );
87+ if (closeit ) {
8088 fclose (fp );
81- return err ;
89+ }
90+ }
91+ else {
92+ res = _PyRun_SimpleFileObject (fp , filename , closeit , flags );
93+ }
94+
95+ if (decref_filename ) {
96+ Py_DECREF (filename );
8297 }
83- else
84- return PyRun_SimpleFileExFlags (fp , filename , closeit , flags );
98+ return res ;
8599}
86100
101+
102+ /* Parse input from a file and execute it */
87103int
88- PyRun_InteractiveLoopFlags (FILE * fp , const char * filename_str , PyCompilerFlags * flags )
104+ PyRun_AnyFileExFlags (FILE * fp , const char * filename , int closeit ,
105+ PyCompilerFlags * flags )
89106{
90- PyObject * filename , * v ;
91- int ret , err ;
92- PyCompilerFlags local_flags = _PyCompilerFlags_INIT ;
93- int nomem_count = 0 ;
94- #ifdef Py_REF_DEBUG
95- int show_ref_count = _Py_GetConfig ()-> show_ref_count ;
96- #endif
97-
98- filename = PyUnicode_DecodeFSDefault (filename_str );
99- if (filename == NULL ) {
100- PyErr_Print ();
101- return -1 ;
107+ PyObject * filename_obj ;
108+ if (filename != NULL ) {
109+ filename_obj = PyUnicode_DecodeFSDefault (filename );
110+ if (filename_obj == NULL ) {
111+ PyErr_Print ();
112+ return -1 ;
113+ }
102114 }
115+ else {
116+ filename_obj = NULL ;
117+ }
118+ int res = _PyRun_AnyFileObject (fp , filename_obj , closeit , flags );
119+ Py_XDECREF (filename_obj );
120+ return res ;
121+ }
103122
123+
124+ int
125+ _PyRun_InteractiveLoopObject (FILE * fp , PyObject * filename , PyCompilerFlags * flags )
126+ {
127+ PyCompilerFlags local_flags = _PyCompilerFlags_INIT ;
104128 if (flags == NULL ) {
105129 flags = & local_flags ;
106130 }
107- v = _PySys_GetObjectId (& PyId_ps1 );
131+
132+ PyObject * v = _PySys_GetObjectId (& PyId_ps1 );
108133 if (v == NULL ) {
109134 _PySys_SetObjectId (& PyId_ps1 , v = PyUnicode_FromString (">>> " ));
110135 Py_XDECREF (v );
@@ -114,7 +139,13 @@ PyRun_InteractiveLoopFlags(FILE *fp, const char *filename_str, PyCompilerFlags *
114139 _PySys_SetObjectId (& PyId_ps2 , v = PyUnicode_FromString ("... " ));
115140 Py_XDECREF (v );
116141 }
117- err = 0 ;
142+
143+ #ifdef Py_REF_DEBUG
144+ int show_ref_count = _Py_GetConfig ()-> show_ref_count ;
145+ #endif
146+ int err = 0 ;
147+ int ret ;
148+ int nomem_count = 0 ;
118149 do {
119150 ret = PyRun_InteractiveOneObjectEx (fp , filename , flags );
120151 if (ret == -1 && PyErr_Occurred ()) {
@@ -141,10 +172,26 @@ PyRun_InteractiveLoopFlags(FILE *fp, const char *filename_str, PyCompilerFlags *
141172 }
142173#endif
143174 } while (ret != E_EOF );
144- Py_DECREF (filename );
145175 return err ;
146176}
147177
178+
179+ int
180+ PyRun_InteractiveLoopFlags (FILE * fp , const char * filename , PyCompilerFlags * flags )
181+ {
182+ PyObject * filename_obj = PyUnicode_DecodeFSDefault (filename );
183+ if (filename_obj == NULL ) {
184+ PyErr_Print ();
185+ return -1 ;
186+ }
187+
188+ int err = _PyRun_InteractiveLoopObject (fp , filename_obj , flags );
189+ Py_DECREF (filename_obj );
190+ return err ;
191+
192+ }
193+
194+
148195/* A PyRun_InteractiveOneObject() auxiliary function that does not print the
149196 * error on failure. */
150197static int
0 commit comments