@@ -458,95 +458,15 @@ set_main_loader(PyObject *d, PyObject *filename, const char *loader_name)
458458}
459459
460460
461- int
462- _PyRun_SimpleFileObject (FILE * fp , PyObject * filename , int closeit ,
463- PyCompilerFlags * flags )
464- {
465- int ret = -1 ;
466-
467- PyObject * main_module = PyImport_AddModuleRef ("__main__" );
468- if (main_module == NULL )
469- return -1 ;
470- PyObject * dict = PyModule_GetDict (main_module ); // borrowed ref
471-
472- int set_file_name = 0 ;
473- int has_file = PyDict_ContainsString (dict , "__file__" );
474- if (has_file < 0 ) {
475- goto done ;
476- }
477- if (!has_file ) {
478- if (PyDict_SetItemString (dict , "__file__" , filename ) < 0 ) {
479- goto done ;
480- }
481- set_file_name = 1 ;
482- }
483-
484- int pyc = maybe_pyc_file (fp , filename , closeit );
485- if (pyc < 0 ) {
486- goto done ;
487- }
488-
489- PyObject * v ;
490- if (pyc ) {
491- FILE * pyc_fp ;
492- /* Try to run a pyc file. First, re-open in binary */
493- if (closeit ) {
494- fclose (fp );
495- }
496-
497- pyc_fp = Py_fopen (filename , "rb" );
498- if (pyc_fp == NULL ) {
499- fprintf (stderr , "python: Can't reopen .pyc file\n" );
500- goto done ;
501- }
502-
503- if (set_main_loader (dict , filename , "SourcelessFileLoader" ) < 0 ) {
504- fprintf (stderr , "python: failed to set __main__.__loader__\n" );
505- ret = -1 ;
506- fclose (pyc_fp );
507- goto done ;
508- }
509- v = run_pyc_file (pyc_fp , dict , dict , flags );
510- } else {
511- /* When running from stdin, leave __main__.__loader__ alone */
512- if ((!PyUnicode_Check (filename ) || !PyUnicode_EqualToUTF8 (filename , "<stdin>" )) &&
513- set_main_loader (dict , filename , "SourceFileLoader" ) < 0 ) {
514- fprintf (stderr , "python: failed to set __main__.__loader__\n" );
515- ret = -1 ;
516- goto done ;
517- }
518- v = pyrun_file (fp , filename , Py_file_input , dict , dict ,
519- closeit , flags );
520- }
521- flush_io ();
522- if (v == NULL ) {
523- Py_CLEAR (main_module );
524- PyErr_Print ();
525- goto done ;
526- }
527- Py_DECREF (v );
528- ret = 0 ;
529-
530- done :
531- if (set_file_name ) {
532- if (PyDict_PopString (dict , "__file__" , NULL ) < 0 ) {
533- PyErr_Print ();
534- }
535- }
536- Py_XDECREF (main_module );
537- return ret ;
538- }
539-
540-
541- /* Variant of _PyRun_SimpleFileObject that returns the result object
542- instead of calling PyErr_Print() on failure. The caller (typically
543- pymain_run_file_obj in Modules/main.c) should handle the error
544- with _Py_HandleSystemExitAndKeyboardInterrupt or pymain_exit_err_print. */
461+ /* Run a simple file object. Returns the result PyObject* on success,
462+ or NULL on failure. Does NOT call PyErr_Print(); the caller must
463+ handle the error (e.g. with PyErr_Print() or
464+ _Py_HandleSystemExitAndKeyboardInterrupt()). */
545465PyObject *
546- _PyRun_SimpleFileObjectEx (FILE * fp , PyObject * filename , int closeit ,
547- PyCompilerFlags * flags )
466+ _PyRun_SimpleFileObjectNoPrint (FILE * fp , PyObject * filename , int closeit ,
467+ PyCompilerFlags * flags )
548468{
549- PyObject * v = NULL ;
469+ PyObject * result = NULL ;
550470
551471 PyObject * main_module = PyImport_AddModuleRef ("__main__" );
552472 if (main_module == NULL )
@@ -589,22 +509,22 @@ _PyRun_SimpleFileObjectEx(FILE *fp, PyObject *filename, int closeit,
589509 fclose (pyc_fp );
590510 goto done ;
591511 }
592- v = run_pyc_file (pyc_fp , dict , dict , flags );
512+ result = run_pyc_file (pyc_fp , dict , dict , flags );
593513 } else {
594514 /* When running from stdin, leave __main__.__loader__ alone */
595515 if ((!PyUnicode_Check (filename ) || !PyUnicode_EqualToUTF8 (filename , "<stdin>" )) &&
596516 set_main_loader (dict , filename , "SourceFileLoader" ) < 0 ) {
597517 fprintf (stderr , "python: failed to set __main__.__loader__\n" );
598518 goto done ;
599519 }
600- v = pyrun_file (fp , filename , Py_file_input , dict , dict ,
601- closeit , flags );
520+ result = pyrun_file (fp , filename , Py_file_input , dict , dict ,
521+ closeit , flags );
602522 }
603523 flush_io ();
604524
605525done :
606526 if (set_file_name ) {
607- if (v == NULL ) {
527+ if (result == NULL ) {
608528 // Main code failed: save the exception before cleanup
609529 // so PyDict_PopString doesn't overwrite it
610530 PyObject * saved_exc = PyErr_GetRaisedException ();
@@ -622,7 +542,22 @@ _PyRun_SimpleFileObjectEx(FILE *fp, PyObject *filename, int closeit,
622542 }
623543 }
624544 Py_XDECREF (main_module );
625- return v ;
545+ return result ;
546+ }
547+
548+
549+ int
550+ _PyRun_SimpleFileObject (FILE * fp , PyObject * filename , int closeit ,
551+ PyCompilerFlags * flags )
552+ {
553+ PyObject * result = _PyRun_SimpleFileObjectNoPrint (fp , filename , closeit ,
554+ flags );
555+ if (result == NULL ) {
556+ PyErr_Print ();
557+ return -1 ;
558+ }
559+ Py_DECREF (result );
560+ return 0 ;
626561}
627562
628563
@@ -640,61 +575,49 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
640575}
641576
642577
643- int
644- _PyRun_SimpleStringFlagsWithName (const char * command , const char * name , PyCompilerFlags * flags ) {
645- PyObject * main_module = PyImport_AddModuleRef ("__main__" );
646- if (main_module == NULL ) {
647- return -1 ;
648- }
649- PyObject * dict = PyModule_GetDict (main_module ); // borrowed ref
650-
651- PyObject * res = NULL ;
652- if (name == NULL ) {
653- res = PyRun_StringFlags (command , Py_file_input , dict , dict , flags );
654- } else {
655- PyObject * the_name = PyUnicode_FromString (name );
656- if (!the_name ) {
657- PyErr_Print ();
658- Py_DECREF (main_module );
659- return -1 ;
660- }
661- res = _PyRun_StringFlagsWithName (command , the_name , Py_file_input , dict , dict , flags , 0 );
662- Py_DECREF (the_name );
663- }
664- Py_DECREF (main_module );
665- if (res == NULL ) {
666- PyErr_Print ();
667- return -1 ;
668- }
669-
670- Py_DECREF (res );
671- return 0 ;
672- }
673-
578+ /* Run a simple string. Returns the result PyObject* on success,
579+ or NULL on failure. Does NOT call PyErr_Print(); the caller must
580+ handle the error (e.g. with PyErr_Print() or
581+ _Py_HandleSystemExitAndKeyboardInterrupt()). */
674582PyObject *
675- _PyRun_SimpleStringFlagsEx (const char * command , const char * name , PyCompilerFlags * flags )
583+ _PyRun_SimpleStringFlagsNoPrint (const char * command , const char * name ,
584+ PyCompilerFlags * flags )
676585{
677586 PyObject * main_module = PyImport_AddModuleRef ("__main__" );
678587 if (main_module == NULL ) {
679588 return NULL ;
680589 }
681590 PyObject * dict = PyModule_GetDict (main_module ); // borrowed ref
682591
683- PyObject * res = NULL ;
592+ PyObject * result = NULL ;
684593 if (name == NULL ) {
685- res = PyRun_StringFlags (command , Py_file_input , dict , dict , flags );
594+ result = PyRun_StringFlags (command , Py_file_input , dict , dict , flags );
686595 } else {
687596 PyObject * the_name = PyUnicode_FromString (name );
688597 if (!the_name ) {
689598 Py_DECREF (main_module );
690599 return NULL ;
691600 }
692- res = _PyRun_StringFlagsWithName (command , the_name , Py_file_input ,
693- dict , dict , flags , 0 );
601+ result = _PyRun_StringFlagsWithName (command , the_name , Py_file_input ,
602+ dict , dict , flags , 0 );
694603 Py_DECREF (the_name );
695604 }
696605 Py_DECREF (main_module );
697- return res ;
606+ return result ;
607+ }
608+
609+
610+ int
611+ _PyRun_SimpleStringFlagsWithName (const char * command , const char * name ,
612+ PyCompilerFlags * flags )
613+ {
614+ PyObject * result = _PyRun_SimpleStringFlagsNoPrint (command , name , flags );
615+ if (result == NULL ) {
616+ PyErr_Print ();
617+ return -1 ;
618+ }
619+ Py_DECREF (result );
620+ return 0 ;
698621}
699622
700623
0 commit comments