2424#undef PyRun_InteractiveLoop
2525
2626
27- // Some PyRun functions crash if start is invalid,
28- // so validate the start argument.
29- static int
30- check_start (int start )
31- {
32- if (start == Py_single_input || start == Py_file_input
33- || start == Py_eval_input || start == Py_func_type_input )
34- {
35- return 0 ;
36- }
37- PyErr_SetString (PyExc_ValueError , "invalid start argument" );
38- return -1 ;
39- }
40-
41-
4227// Test PyRun_String()
4328static PyObject *
4429run_string (PyObject * mod , PyObject * args )
4530{
4631 const char * str ;
47- int start = 0 ;
32+ int start ;
4833 PyObject * globals = NULL ;
4934 PyObject * locals = NULL ;
5035
51- if (!PyArg_ParseTuple (args , "y|iOO" , & str , & start , & globals , & locals )) {
52- return NULL ;
53- }
54- if (check_start (start ) < 0 ) {
36+ if (!PyArg_ParseTuple (args , "yi|OO" , & str , & start , & globals , & locals )) {
5537 return NULL ;
5638 }
5739 NULLABLE (globals );
@@ -79,9 +61,6 @@ run_stringflags(PyObject *mod, PyObject *args)
7961 & cf_flags , & cf_feature_version )) {
8062 return NULL ;
8163 }
82- if (check_start (start ) < 0 ) {
83- return NULL ;
84- }
8564 NULLABLE (globals );
8665 NULLABLE (locals );
8766 if (cf_flags || cf_feature_version ) {
@@ -481,9 +460,6 @@ run_file(PyObject *mod, PyObject *args)
481460 & globals , & locals )) {
482461 return NULL ;
483462 }
484- if (check_start (start ) < 0 ) {
485- return NULL ;
486- }
487463 NULLABLE (globals );
488464 NULLABLE (locals );
489465
@@ -518,9 +494,6 @@ run_fileex(PyObject *mod, PyObject *args)
518494 & closeit )) {
519495 return NULL ;
520496 }
521- if (check_start (start ) < 0 ) {
522- return NULL ;
523- }
524497 NULLABLE (globals );
525498 NULLABLE (locals );
526499
@@ -560,9 +533,6 @@ run_fileflags(PyObject *mod, PyObject *args)
560533 & cf_flags , & cf_feature_version )) {
561534 return NULL ;
562535 }
563- if (check_start (start ) < 0 ) {
564- return NULL ;
565- }
566536 NULLABLE (globals );
567537 NULLABLE (locals );
568538 if (cf_flags || cf_feature_version ) {
@@ -606,9 +576,6 @@ run_fileexflags(PyObject *mod, PyObject *args)
606576 & closeit , & cf_flags , & cf_feature_version )) {
607577 return NULL ;
608578 }
609- if (check_start (start ) < 0 ) {
610- return NULL ;
611- }
612579 NULLABLE (globals );
613580 NULLABLE (locals );
614581 if (cf_flags || cf_feature_version ) {
@@ -733,6 +700,86 @@ run_interactiveloopflags(PyObject *mod, PyObject *args)
733700}
734701
735702
703+ // Test Py_CompileStringFlags()
704+ static PyObject *
705+ run_compilestringflags (PyObject * mod , PyObject * args )
706+ {
707+ const char * str ;
708+ const char * filename ;
709+ int start ;
710+ PyCompilerFlags flags = _PyCompilerFlags_INIT ;
711+ PyCompilerFlags * pflags = NULL ;
712+ int cf_flags = 0 ;
713+ int cf_feature_version = 0 ;
714+
715+ if (!PyArg_ParseTuple (args , "yyi|ii" , & str , & filename , & start ,
716+ & cf_flags , & cf_feature_version )) {
717+ return NULL ;
718+ }
719+ if (cf_flags || cf_feature_version ) {
720+ flags .cf_flags = cf_flags ;
721+ flags .cf_feature_version = cf_feature_version ;
722+ pflags = & flags ;
723+ }
724+
725+ return Py_CompileStringFlags (str , filename , start , pflags );
726+ }
727+
728+
729+ // Test Py_CompileStringExFlags()
730+ static PyObject *
731+ run_compilestringexflags (PyObject * mod , PyObject * args )
732+ {
733+ const char * str ;
734+ const char * filename ;
735+ int start ;
736+ PyCompilerFlags flags = _PyCompilerFlags_INIT ;
737+ PyCompilerFlags * pflags = NULL ;
738+ int cf_flags = 0 ;
739+ int cf_feature_version = 0 ;
740+ int optimize = -1 ;
741+
742+ if (!PyArg_ParseTuple (args , "yyi|iii" , & str , & filename , & start ,
743+ & cf_flags , & cf_feature_version , & optimize )) {
744+ return NULL ;
745+ }
746+ if (cf_flags || cf_feature_version ) {
747+ flags .cf_flags = cf_flags ;
748+ flags .cf_feature_version = cf_feature_version ;
749+ pflags = & flags ;
750+ }
751+
752+ return Py_CompileStringExFlags (str , filename , start , pflags , optimize );
753+ }
754+
755+
756+ // Test Py_CompileStringObject()
757+ static PyObject *
758+ run_compilestringobject (PyObject * mod , PyObject * args )
759+ {
760+ const char * str ;
761+ PyObject * filename ;
762+ int start ;
763+ PyCompilerFlags flags = _PyCompilerFlags_INIT ;
764+ PyCompilerFlags * pflags = NULL ;
765+ int cf_flags = 0 ;
766+ int cf_feature_version = 0 ;
767+ int optimize = -1 ;
768+
769+ if (!PyArg_ParseTuple (args , "yOi|iii" , & str , & filename , & start ,
770+ & cf_flags , & cf_feature_version , & optimize )) {
771+ return NULL ;
772+ }
773+ if (cf_flags || cf_feature_version ) {
774+ flags .cf_flags = cf_flags ;
775+ flags .cf_feature_version = cf_feature_version ;
776+ pflags = & flags ;
777+ }
778+
779+ return Py_CompileStringObject (str , filename , start , pflags , optimize );
780+ }
781+
782+
736783static PyMethodDef test_methods [] = {
737784 {"run_string" , run_string , METH_VARARGS },
738785 {"run_stringflags" , run_stringflags , METH_VARARGS },
@@ -754,6 +801,9 @@ static PyMethodDef test_methods[] = {
754801 {"run_simplestringflags" , run_simplestringflags , METH_VARARGS },
755802 {"run_interactiveloop" , run_interactiveloop , METH_VARARGS },
756803 {"run_interactiveloopflags" , run_interactiveloopflags , METH_VARARGS },
804+ {"run_compilestringflags" , run_compilestringflags , METH_VARARGS },
805+ {"run_compilestringexflags" , run_compilestringexflags , METH_VARARGS },
806+ {"run_compilestringobject" , run_compilestringobject , METH_VARARGS },
757807 {NULL },
758808};
759809
@@ -763,5 +813,11 @@ _PyTestCapi_Init_Run(PyObject *mod)
763813 if (PyModule_AddFunctions (mod , test_methods ) < 0 ) {
764814 return -1 ;
765815 }
816+ if (PyModule_AddIntMacro (mod , PyCF_ONLY_AST ) < 0 ) {
817+ return -1 ;
818+ }
819+ if (PyModule_AddIntMacro (mod , PyCF_IGNORE_COOKIE ) < 0 ) {
820+ return -1 ;
821+ }
766822 return 0 ;
767823}
0 commit comments