@@ -179,90 +179,118 @@ PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flag
179
179
}
180
180
181
181
182
- /* A PyRun_InteractiveOneObject() auxiliary function that does not print the
183
- * error on failure. */
182
+ // Call _PyParser_ASTFromFile() with sys.stdin.encoding, sys.ps1 and sys.ps2
184
183
static int
185
- PyRun_InteractiveOneObjectEx (FILE * fp , PyObject * filename ,
186
- PyCompilerFlags * flags )
184
+ pyrun_one_parse_ast (FILE * fp , PyObject * filename ,
185
+ PyCompilerFlags * flags , PyArena * arena , mod_ty * pmod )
187
186
{
188
- PyObject * m , * d , * v , * w , * oenc = NULL ;
189
- mod_ty mod ;
190
- PyArena * arena ;
191
- const char * ps1 = "" , * ps2 = "" , * enc = NULL ;
192
- int errcode = 0 ;
193
187
PyThreadState * tstate = _PyThreadState_GET ();
194
188
189
+ // Get sys.stdin.encoding (as UTF-8)
190
+ PyObject * attr ; // borrowed ref
191
+ PyObject * encoding_obj = NULL ;
192
+ const char * encoding = NULL ;
195
193
if (fp == stdin ) {
196
- /* Fetch encoding from sys.stdin if possible. */
197
- v = _PySys_GetAttr (tstate , & _Py_ID (stdin ));
198
- if (v && v != Py_None ) {
199
- oenc = PyObject_GetAttr (v , & _Py_ID (encoding ));
200
- if (oenc )
201
- enc = PyUnicode_AsUTF8 (oenc );
202
- if (!enc )
203
- PyErr_Clear ();
194
+ attr = _PySys_GetAttr (tstate , & _Py_ID (stdin ));
195
+ if (attr && attr != Py_None ) {
196
+ encoding_obj = PyObject_GetAttr (attr , & _Py_ID (encoding ));
197
+ if (encoding_obj ) {
198
+ encoding = PyUnicode_AsUTF8 (encoding_obj );
199
+ if (!encoding ) {
200
+ PyErr_Clear ();
201
+ }
202
+ }
204
203
}
205
204
}
206
- v = _PySys_GetAttr (tstate , & _Py_ID (ps1 ));
207
- if (v != NULL ) {
208
- v = PyObject_Str (v );
209
- if (v == NULL )
205
+
206
+ // Get sys.ps1 (as UTF-8)
207
+ attr = _PySys_GetAttr (tstate , & _Py_ID (ps1 ));
208
+ PyObject * ps1_obj = NULL ;
209
+ const char * ps1 = "" ;
210
+ if (attr != NULL ) {
211
+ ps1_obj = PyObject_Str (attr );
212
+ if (ps1_obj == NULL ) {
210
213
PyErr_Clear ();
211
- else if (PyUnicode_Check (v )) {
212
- ps1 = PyUnicode_AsUTF8 (v );
214
+ }
215
+ else if (PyUnicode_Check (ps1_obj )) {
216
+ ps1 = PyUnicode_AsUTF8 (ps1_obj );
213
217
if (ps1 == NULL ) {
214
218
PyErr_Clear ();
215
219
ps1 = "" ;
216
220
}
217
221
}
218
222
}
219
- w = _PySys_GetAttr (tstate , & _Py_ID (ps2 ));
220
- if (w != NULL ) {
221
- w = PyObject_Str (w );
222
- if (w == NULL )
223
+
224
+ // Get sys.ps2 (as UTF-8)
225
+ attr = _PySys_GetAttr (tstate , & _Py_ID (ps2 ));
226
+ PyObject * ps2_obj = NULL ;
227
+ const char * ps2 = "" ;
228
+ if (attr != NULL ) {
229
+ ps2_obj = PyObject_Str (attr );
230
+ if (ps2_obj == NULL ) {
223
231
PyErr_Clear ();
224
- else if (PyUnicode_Check (w )) {
225
- ps2 = PyUnicode_AsUTF8 (w );
232
+ }
233
+ else if (PyUnicode_Check (ps2_obj )) {
234
+ ps2 = PyUnicode_AsUTF8 (ps2_obj );
226
235
if (ps2 == NULL ) {
227
236
PyErr_Clear ();
228
237
ps2 = "" ;
229
238
}
230
239
}
231
240
}
232
- arena = _PyArena_New ();
233
- if (arena == NULL ) {
234
- Py_XDECREF (v );
235
- Py_XDECREF (w );
236
- Py_XDECREF (oenc );
237
- return -1 ;
238
- }
239
-
240
- mod = _PyParser_ASTFromFile (fp , filename , enc , Py_single_input ,
241
- ps1 , ps2 , flags , & errcode , arena );
242
241
243
- Py_XDECREF (v );
244
- Py_XDECREF (w );
245
- Py_XDECREF (oenc );
246
- if (mod == NULL ) {
247
- _PyArena_Free (arena );
242
+ int errcode = 0 ;
243
+ * pmod = _PyParser_ASTFromFile (fp , filename , encoding ,
244
+ Py_single_input , ps1 , ps2 ,
245
+ flags , & errcode , arena );
246
+ Py_XDECREF (ps1_obj );
247
+ Py_XDECREF (ps2_obj );
248
+ Py_XDECREF (encoding_obj );
249
+
250
+ if (* pmod == NULL ) {
248
251
if (errcode == E_EOF ) {
249
252
PyErr_Clear ();
250
253
return E_EOF ;
251
254
}
252
255
return -1 ;
253
256
}
254
- m = PyImport_AddModuleObject (& _Py_ID (__main__ ));
255
- if (m == NULL ) {
257
+ return 0 ;
258
+ }
259
+
260
+
261
+ /* A PyRun_InteractiveOneObject() auxiliary function that does not print the
262
+ * error on failure. */
263
+ static int
264
+ PyRun_InteractiveOneObjectEx (FILE * fp , PyObject * filename ,
265
+ PyCompilerFlags * flags )
266
+ {
267
+ PyArena * arena = _PyArena_New ();
268
+ if (arena == NULL ) {
269
+ return -1 ;
270
+ }
271
+
272
+ mod_ty mod ;
273
+ int parse_res = pyrun_one_parse_ast (fp , filename , flags , arena , & mod );
274
+ if (parse_res != 0 ) {
275
+ _PyArena_Free (arena );
276
+ return parse_res ;
277
+ }
278
+
279
+ PyObject * module = Py_XNewRef (PyImport_AddModuleObject (& _Py_ID (__main__ )));
280
+ if (module == NULL ) {
256
281
_PyArena_Free (arena );
257
282
return -1 ;
258
283
}
259
- d = PyModule_GetDict (m );
260
- v = run_mod (mod , filename , d , d , flags , arena );
284
+ PyObject * mod_dict = PyModule_GetDict (module ); // borrowed ref
285
+
286
+ PyObject * res = run_mod (mod , filename , mod_dict , mod_dict , flags , arena );
261
287
_PyArena_Free (arena );
262
- if (v == NULL ) {
288
+ Py_DECREF (module );
289
+ if (res == NULL ) {
263
290
return -1 ;
264
291
}
265
- Py_DECREF (v );
292
+ Py_DECREF (res );
293
+
266
294
flush_io ();
267
295
return 0 ;
268
296
}
@@ -376,22 +404,22 @@ int
376
404
_PyRun_SimpleFileObject (FILE * fp , PyObject * filename , int closeit ,
377
405
PyCompilerFlags * flags )
378
406
{
379
- PyObject * m , * d , * v ;
380
- int set_file_name = 0 , ret = -1 ;
407
+ int ret = -1 ;
381
408
382
- m = PyImport_AddModule ("__main__" );
383
- if (m == NULL )
409
+ PyObject * module = Py_XNewRef ( PyImport_AddModule ("__main__" ) );
410
+ if (module == NULL )
384
411
return -1 ;
385
- Py_INCREF (m );
386
- d = PyModule_GetDict (m );
387
- if (_PyDict_GetItemStringWithError (d , "__file__" ) == NULL ) {
412
+ PyObject * dict = PyModule_GetDict (module ); // borrowed ref
413
+
414
+ int set_file_name = 0 ;
415
+ if (_PyDict_GetItemStringWithError (dict , "__file__" ) == NULL ) {
388
416
if (PyErr_Occurred ()) {
389
417
goto done ;
390
418
}
391
- if (PyDict_SetItemString (d , "__file__" , filename ) < 0 ) {
419
+ if (PyDict_SetItemString (dict , "__file__" , filename ) < 0 ) {
392
420
goto done ;
393
421
}
394
- if (PyDict_SetItemString (d , "__cached__" , Py_None ) < 0 ) {
422
+ if (PyDict_SetItemString (dict , "__cached__" , Py_None ) < 0 ) {
395
423
goto done ;
396
424
}
397
425
set_file_name = 1 ;
@@ -402,6 +430,7 @@ _PyRun_SimpleFileObject(FILE *fp, PyObject *filename, int closeit,
402
430
goto done ;
403
431
}
404
432
433
+ PyObject * v ;
405
434
if (pyc ) {
406
435
FILE * pyc_fp ;
407
436
/* Try to run a pyc file. First, re-open in binary */
@@ -415,42 +444,43 @@ _PyRun_SimpleFileObject(FILE *fp, PyObject *filename, int closeit,
415
444
goto done ;
416
445
}
417
446
418
- if (set_main_loader (d , filename , "SourcelessFileLoader" ) < 0 ) {
447
+ if (set_main_loader (dict , filename , "SourcelessFileLoader" ) < 0 ) {
419
448
fprintf (stderr , "python: failed to set __main__.__loader__\n" );
420
449
ret = -1 ;
421
450
fclose (pyc_fp );
422
451
goto done ;
423
452
}
424
- v = run_pyc_file (pyc_fp , d , d , flags );
453
+ v = run_pyc_file (pyc_fp , dict , dict , flags );
425
454
} else {
426
455
/* When running from stdin, leave __main__.__loader__ alone */
427
456
if (PyUnicode_CompareWithASCIIString (filename , "<stdin>" ) != 0 &&
428
- set_main_loader (d , filename , "SourceFileLoader" ) < 0 ) {
457
+ set_main_loader (dict , filename , "SourceFileLoader" ) < 0 ) {
429
458
fprintf (stderr , "python: failed to set __main__.__loader__\n" );
430
459
ret = -1 ;
431
460
goto done ;
432
461
}
433
- v = pyrun_file (fp , filename , Py_file_input , d , d ,
462
+ v = pyrun_file (fp , filename , Py_file_input , dict , dict ,
434
463
closeit , flags );
435
464
}
436
465
flush_io ();
437
466
if (v == NULL ) {
438
- Py_CLEAR (m );
467
+ Py_CLEAR (module );
439
468
PyErr_Print ();
440
469
goto done ;
441
470
}
442
471
Py_DECREF (v );
443
472
ret = 0 ;
473
+
444
474
done :
445
475
if (set_file_name ) {
446
- if (PyDict_DelItemString (d , "__file__" )) {
476
+ if (PyDict_DelItemString (dict , "__file__" )) {
447
477
PyErr_Clear ();
448
478
}
449
- if (PyDict_DelItemString (d , "__cached__" )) {
479
+ if (PyDict_DelItemString (dict , "__cached__" )) {
450
480
PyErr_Clear ();
451
481
}
452
482
}
453
- Py_XDECREF (m );
483
+ Py_XDECREF (module );
454
484
return ret ;
455
485
}
456
486
@@ -472,17 +502,21 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
472
502
int
473
503
PyRun_SimpleStringFlags (const char * command , PyCompilerFlags * flags )
474
504
{
475
- PyObject * m , * d , * v ;
476
- m = PyImport_AddModule ("__main__" );
477
- if (m == NULL )
505
+ PyObject * module = Py_XNewRef (PyImport_AddModule ("__main__" ));
506
+ if (module == NULL ) {
478
507
return -1 ;
479
- d = PyModule_GetDict (m );
480
- v = PyRun_StringFlags (command , Py_file_input , d , d , flags );
481
- if (v == NULL ) {
508
+ }
509
+ PyObject * dict = PyModule_GetDict (module ); // borrowed ref
510
+
511
+ PyObject * res = PyRun_StringFlags (command , Py_file_input ,
512
+ dict , dict , flags );
513
+ Py_DECREF (module );
514
+ if (res == NULL ) {
482
515
PyErr_Print ();
483
516
return -1 ;
484
517
}
485
- Py_DECREF (v );
518
+
519
+ Py_DECREF (res );
486
520
return 0 ;
487
521
}
488
522
0 commit comments