@@ -466,14 +466,15 @@ builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
466466 char * str ;
467467 char * filename ;
468468 char * startstr ;
469- int start ;
469+ int mode = -1 ;
470470 int dont_inherit = 0 ;
471471 int supplied_flags = 0 ;
472472 PyCompilerFlags cf ;
473473 PyObject * result = NULL , * cmd , * tmp = NULL ;
474474 Py_ssize_t length ;
475475 static char * kwlist [] = {"source" , "filename" , "mode" , "flags" ,
476476 "dont_inherit" , NULL };
477+ int start [] = {Py_file_input , Py_eval_input , Py_single_input };
477478
478479 if (!PyArg_ParseTupleAndKeywords (args , kwds , "Oss|ii:compile" ,
479480 kwlist , & cmd , & filename , & startstr ,
@@ -495,6 +496,18 @@ builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
495496 PyEval_MergeCompilerFlags (& cf );
496497 }
497498
499+ if (strcmp (startstr , "exec" ) == 0 )
500+ mode = 0 ;
501+ else if (strcmp (startstr , "eval" ) == 0 )
502+ mode = 1 ;
503+ else if (strcmp (startstr , "single" ) == 0 )
504+ mode = 2 ;
505+ else {
506+ PyErr_SetString (PyExc_ValueError ,
507+ "compile() arg 3 must be 'exec', 'eval' or 'single'" );
508+ return NULL ;
509+ }
510+
498511 if (PyAST_Check (cmd )) {
499512 if (supplied_flags & PyCF_ONLY_AST ) {
500513 Py_INCREF (cmd );
@@ -505,7 +518,7 @@ builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
505518 mod_ty mod ;
506519
507520 arena = PyArena_New ();
508- mod = PyAST_obj2mod (cmd , arena );
521+ mod = PyAST_obj2mod (cmd , arena , mode );
509522 if (mod == NULL ) {
510523 PyArena_Free (arena );
511524 return NULL ;
@@ -526,19 +539,6 @@ builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
526539 cf .cf_flags |= PyCF_SOURCE_IS_UTF8 ;
527540 }
528541#endif
529- /* XXX: is it possible to pass start to the PyAST_ branch? */
530- if (strcmp (startstr , "exec" ) == 0 )
531- start = Py_file_input ;
532- else if (strcmp (startstr , "eval" ) == 0 )
533- start = Py_eval_input ;
534- else if (strcmp (startstr , "single" ) == 0 )
535- start = Py_single_input ;
536- else {
537- PyErr_SetString (PyExc_ValueError ,
538- "compile() arg 3 must be 'exec'"
539- "or 'eval' or 'single'" );
540- goto cleanup ;
541- }
542542
543543 if (PyObject_AsReadBuffer (cmd , (const void * * )& str , & length ))
544544 goto cleanup ;
@@ -547,7 +547,7 @@ builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
547547 "compile() expected string without null bytes" );
548548 goto cleanup ;
549549 }
550- result = Py_CompileStringFlags (str , filename , start , & cf );
550+ result = Py_CompileStringFlags (str , filename , start [ mode ] , & cf );
551551cleanup :
552552 Py_XDECREF (tmp );
553553 return result ;
0 commit comments