@@ -64,11 +64,23 @@ static void error(const char *msg)
6464}
6565
6666
67+ static void error_fmt (const char * format , ...)
68+ {
69+ va_list vargs ;
70+ va_start (vargs , format );
71+ fprintf (stderr , "ERROR: " );
72+ vfprintf (stderr , format , vargs );
73+ fprintf (stderr , "\n" );
74+ va_end (vargs );
75+ fflush (stderr );
76+ }
77+
78+
6779static wchar_t * py_getenv (const char * name )
6880{
6981 const char * env = getenv (name );
7082 if (env == NULL ) {
71- fprintf ( stderr , "ERROR: need %s env var\n " , name );
83+ error_fmt ( " need %s env var" , name );
7284 return NULL ;
7385 }
7486
@@ -81,6 +93,24 @@ static wchar_t* py_getenv(const char *name)
8193}
8294
8395
96+ static wchar_t * get_cmdline_arg (const char * arg_name )
97+ {
98+ if (main_argc < 3 ) {
99+ const char * test = main_argv [1 ];
100+ fprintf (stderr , "usage: %s %s %s\n" , PROGRAM , test , arg_name );
101+ return NULL ;
102+ }
103+ const char * arg = main_argv [2 ];
104+
105+ wchar_t * result = Py_DecodeLocale (arg , NULL );
106+ if (result == NULL ) {
107+ error_fmt ("failed to decode %s command line argument" , arg_name );
108+ return NULL ;
109+ }
110+ return result ;
111+ }
112+
113+
84114static void config_set_string (PyConfig * config , wchar_t * * config_str , const wchar_t * str )
85115{
86116 PyStatus status = PyConfig_SetString (config , config_str , str );
@@ -2003,8 +2033,7 @@ static int test_init_run_main_exitcode(Py_ssize_t argc, wchar_t * const *argv)
20032033
20042034 int exitcode = Py_RunMain ();
20052035 if (exitcode != 123 ) {
2006- fprintf (stderr , "ERROR: Py_RunMain() returned %i, expected 123\n" ,
2007- exitcode );
2036+ error_fmt ("Py_RunMain() returned %i, expected 123" , exitcode );
20082037 return 1 ;
20092038 }
20102039
@@ -2014,7 +2043,7 @@ static int test_init_run_main_exitcode(Py_ssize_t argc, wchar_t * const *argv)
20142043
20152044static int test_init_run_main_script_exitcode (void )
20162045{
2017- wchar_t * filename = py_getenv ( "TEST_SCRIPT " );
2046+ wchar_t * filename = get_cmdline_arg ( "FILENAME " );
20182047 if (filename == NULL ) {
20192048 return 1 ;
20202049 }
@@ -2029,7 +2058,7 @@ static int test_init_run_main_script_exitcode(void)
20292058
20302059static int test_init_run_main_module_exitcode (void )
20312060{
2032- wchar_t * module = py_getenv ( "TEST_MODULE " );
2061+ wchar_t * module = get_cmdline_arg ( "MODULE " );
20332062 if (module == NULL ) {
20342063 return 1 ;
20352064 }
@@ -2051,7 +2080,7 @@ static int test_init_run_main_interactive_exitcode(void)
20512080
20522081static int test_init_run_main_code_exitcode (void )
20532082{
2054- wchar_t * code = py_getenv ( "TEST_CODE " );
2083+ wchar_t * code = get_cmdline_arg ( "CODE " );
20552084 if (code == NULL ) {
20562085 return 1 ;
20572086 }
0 commit comments