@@ -35,6 +35,8 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3535#include "opcode.h"
3636#include "bltinmodule.h"
3737#include "traceback.h"
38+ #include "graminit.h"
39+ #include "pythonrun.h"
3840
3941/* Turn this on if your compiler chokes on the big switch: */
4042/* #define CASE_TOO_BIG 1 /**/
@@ -91,6 +93,7 @@ static object *build_class PROTO((object *, object *, object *));
9193static void locals_2_fast PROTO ((frameobject * , int ) );
9294static void fast_2_locals PROTO ((frameobject * ) );
9395static int access_statement PROTO ((object * , object * , frameobject * ) );
96+ static int exec_statement PROTO ((object * , object * , object * ) );
9497
9598
9699/* Pointer to current frame, used to link new frames to */
@@ -712,6 +715,22 @@ eval_code(co, globals, locals, owner, arg)
712715 retval = POP ();
713716 why = WHY_RETURN ;
714717 break ;
718+
719+ case LOAD_GLOBALS :
720+ v = f -> f_locals ;
721+ INCREF (v );
722+ PUSH (v );
723+ break ;
724+
725+ case EXEC_STMT :
726+ w = POP ();
727+ v = POP ();
728+ u = POP ();
729+ err = exec_statement (u , v , w );
730+ DECREF (u );
731+ DECREF (v );
732+ DECREF (w );
733+ break ;
715734
716735 case BUILD_FUNCTION :
717736 v = POP ();
@@ -2489,3 +2508,62 @@ access_statement(name, vmode, f)
24892508 }
24902509 return ret ;
24912510}
2511+
2512+ static int
2513+ exec_statement (prog , globals , locals )
2514+ object * prog ;
2515+ object * globals ;
2516+ object * locals ;
2517+ {
2518+ char * s ;
2519+ int n ;
2520+
2521+ if (is_tupleobject (prog ) && globals == None && locals == None &&
2522+ ((n = gettuplesize (prog )) == 2 || n == 3 )) {
2523+ /* Backward compatibility hack */
2524+ globals = gettupleitem (prog , 1 );
2525+ if (n == 3 )
2526+ locals = gettupleitem (prog , 2 );
2527+ prog = gettupleitem (prog , 0 );
2528+ }
2529+ if (globals == None ) {
2530+ globals = getglobals ();
2531+ if (locals == None )
2532+ locals = getlocals ();
2533+ }
2534+ else if (locals == None )
2535+ locals = globals ;
2536+ if (!is_stringobject (prog ) &&
2537+ !is_codeobject (prog ) &&
2538+ !is_fileobject (prog )) {
2539+ err_setstr (TypeError ,
2540+ "exec 1st arg must be string, code or file object" );
2541+ return -1 ;
2542+ }
2543+ if (!is_dictobject (globals ) || !is_dictobject (locals )) {
2544+ err_setstr (TypeError ,
2545+ "exec 2nd/3rd args must be dict or None" );
2546+ return -1 ;
2547+ }
2548+ if (is_codeobject (prog )) {
2549+ if (eval_code ((codeobject * ) prog , globals , locals ,
2550+ (object * )NULL , (object * )NULL ) == NULL )
2551+ return -1 ;
2552+ return 0 ;
2553+ }
2554+ if (is_fileobject (prog )) {
2555+ FILE * fp = getfilefile (prog );
2556+ char * name = getstringvalue (getfilename (prog ));
2557+ if (run_file (fp , name , file_input , globals , locals ) == NULL )
2558+ return -1 ;
2559+ return 0 ;
2560+ }
2561+ s = getstringvalue (prog );
2562+ if (strlen (s ) != getstringsize (prog )) {
2563+ err_setstr (ValueError , "embedded '\\0' in exec string" );
2564+ return -1 ;
2565+ }
2566+ if (run_string (s , file_input , globals , locals ) == NULL )
2567+ return -1 ;
2568+ return 0 ;
2569+ }
0 commit comments