2727_Py_COMP_DIAG_PUSH
2828_Py_COMP_DIAG_IGNORE_DEPR_DECLS
2929
30+
31+ static void error (const char * msg )
32+ {
33+ fprintf (stderr , "ERROR: %s\n" , msg );
34+ fflush (stderr );
35+ }
36+
37+
3038static void _testembed_Py_Initialize (void )
3139{
3240 Py_SetProgramName (PROGRAM_NAME );
@@ -239,7 +247,7 @@ static void bpo20891_thread(void *lockp)
239247
240248 PyGILState_STATE state = PyGILState_Ensure ();
241249 if (!PyGILState_Check ()) {
242- fprintf ( stderr , "PyGILState_Check failed!" );
250+ error ( "PyGILState_Check failed!" );
243251 abort ();
244252 }
245253
@@ -259,15 +267,15 @@ static int test_bpo20891(void)
259267 crash. */
260268 PyThread_type_lock lock = PyThread_allocate_lock ();
261269 if (!lock ) {
262- fprintf ( stderr , "PyThread_allocate_lock failed!" );
270+ error ( "PyThread_allocate_lock failed!" );
263271 return 1 ;
264272 }
265273
266274 _testembed_Py_Initialize ();
267275
268276 unsigned long thrd = PyThread_start_new_thread (bpo20891_thread , & lock );
269277 if (thrd == PYTHREAD_INVALID_THREAD_ID ) {
270- fprintf ( stderr , "PyThread_start_new_thread failed!" );
278+ error ( "PyThread_start_new_thread failed!" );
271279 return 1 ;
272280 }
273281 PyThread_acquire_lock (lock , WAIT_LOCK );
@@ -1397,12 +1405,12 @@ static int test_init_setpath(void)
13971405{
13981406 char * env = getenv ("TESTPATH" );
13991407 if (!env ) {
1400- fprintf ( stderr , "missing TESTPATH env var\n " );
1408+ error ( "missing TESTPATH env var" );
14011409 return 1 ;
14021410 }
14031411 wchar_t * path = Py_DecodeLocale (env , NULL );
14041412 if (path == NULL ) {
1405- fprintf ( stderr , "failed to decode TESTPATH\n " );
1413+ error ( "failed to decode TESTPATH" );
14061414 return 1 ;
14071415 }
14081416 Py_SetPath (path );
@@ -1430,12 +1438,12 @@ static int test_init_setpath_config(void)
14301438
14311439 char * env = getenv ("TESTPATH" );
14321440 if (!env ) {
1433- fprintf ( stderr , "missing TESTPATH env var\n " );
1441+ error ( "missing TESTPATH env var" );
14341442 return 1 ;
14351443 }
14361444 wchar_t * path = Py_DecodeLocale (env , NULL );
14371445 if (path == NULL ) {
1438- fprintf ( stderr , "failed to decode TESTPATH\n " );
1446+ error ( "failed to decode TESTPATH" );
14391447 return 1 ;
14401448 }
14411449 Py_SetPath (path );
@@ -1459,12 +1467,12 @@ static int test_init_setpythonhome(void)
14591467{
14601468 char * env = getenv ("TESTHOME" );
14611469 if (!env ) {
1462- fprintf ( stderr , "missing TESTHOME env var\n " );
1470+ error ( "missing TESTHOME env var" );
14631471 return 1 ;
14641472 }
14651473 wchar_t * home = Py_DecodeLocale (env , NULL );
14661474 if (home == NULL ) {
1467- fprintf ( stderr , "failed to decode TESTHOME\n " );
1475+ error ( "failed to decode TESTHOME" );
14681476 return 1 ;
14691477 }
14701478 Py_SetPythonHome (home );
@@ -1726,6 +1734,48 @@ static int test_unicode_id_init(void)
17261734}
17271735
17281736
1737+ #ifndef MS_WINDOWS
1738+ #include "test_frozenmain.h" // M_test_frozenmain
1739+
1740+ static int test_frozenmain (void )
1741+ {
1742+ // Get "_frozen_importlib" and "_frozen_importlib_external"
1743+ // from PyImport_FrozenModules
1744+ const struct _frozen * importlib = NULL , * importlib_external = NULL ;
1745+ for (const struct _frozen * mod = PyImport_FrozenModules ; mod -> name != NULL ; mod ++ ) {
1746+ if (strcmp (mod -> name , "_frozen_importlib" ) == 0 ) {
1747+ importlib = mod ;
1748+ }
1749+ else if (strcmp (mod -> name , "_frozen_importlib_external" ) == 0 ) {
1750+ importlib_external = mod ;
1751+ }
1752+ }
1753+ if (importlib == NULL || importlib_external == NULL ) {
1754+ error ("cannot find frozen importlib and importlib_external" );
1755+ return 1 ;
1756+ }
1757+
1758+ static struct _frozen frozen_modules [4 ] = {
1759+ {0 , 0 , 0 }, // importlib
1760+ {0 , 0 , 0 }, // importlib_external
1761+ {"__main__" , M_test_frozenmain , sizeof (M_test_frozenmain )},
1762+ {0 , 0 , 0 } // sentinel
1763+ };
1764+ frozen_modules [0 ] = * importlib ;
1765+ frozen_modules [1 ] = * importlib_external ;
1766+
1767+ char * argv [] = {
1768+ "./argv0" ,
1769+ "-E" ,
1770+ "arg1" ,
1771+ "arg2" ,
1772+ };
1773+ PyImport_FrozenModules = frozen_modules ;
1774+ return Py_FrozenMain (Py_ARRAY_LENGTH (argv ), argv );
1775+ }
1776+ #endif // !MS_WINDOWS
1777+
1778+
17291779// List frozen modules.
17301780// Command used by Tools/scripts/generate_stdlib_module_names.py script.
17311781static int list_frozen (void )
@@ -1811,11 +1861,15 @@ static struct TestCase TestCases[] = {
18111861 {"test_audit_run_stdin" , test_audit_run_stdin },
18121862
18131863 {"test_unicode_id_init" , test_unicode_id_init },
1864+ #ifndef MS_WINDOWS
1865+ {"test_frozenmain" , test_frozenmain },
1866+ #endif
18141867
18151868 {"list_frozen" , list_frozen },
18161869 {NULL , NULL }
18171870};
18181871
1872+
18191873int main (int argc , char * argv [])
18201874{
18211875 if (argc > 1 ) {
0 commit comments