27
27
_Py_COMP_DIAG_PUSH
28
28
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
29
29
30
+
31
+ static void error (const char * msg )
32
+ {
33
+ fprintf (stderr , "ERROR: %s\n" , msg );
34
+ fflush (stderr );
35
+ }
36
+
37
+
30
38
static void _testembed_Py_Initialize (void )
31
39
{
32
40
Py_SetProgramName (PROGRAM_NAME );
@@ -239,7 +247,7 @@ static void bpo20891_thread(void *lockp)
239
247
240
248
PyGILState_STATE state = PyGILState_Ensure ();
241
249
if (!PyGILState_Check ()) {
242
- fprintf ( stderr , "PyGILState_Check failed!" );
250
+ error ( "PyGILState_Check failed!" );
243
251
abort ();
244
252
}
245
253
@@ -259,15 +267,15 @@ static int test_bpo20891(void)
259
267
crash. */
260
268
PyThread_type_lock lock = PyThread_allocate_lock ();
261
269
if (!lock ) {
262
- fprintf ( stderr , "PyThread_allocate_lock failed!" );
270
+ error ( "PyThread_allocate_lock failed!" );
263
271
return 1 ;
264
272
}
265
273
266
274
_testembed_Py_Initialize ();
267
275
268
276
unsigned long thrd = PyThread_start_new_thread (bpo20891_thread , & lock );
269
277
if (thrd == PYTHREAD_INVALID_THREAD_ID ) {
270
- fprintf ( stderr , "PyThread_start_new_thread failed!" );
278
+ error ( "PyThread_start_new_thread failed!" );
271
279
return 1 ;
272
280
}
273
281
PyThread_acquire_lock (lock , WAIT_LOCK );
@@ -1397,12 +1405,12 @@ static int test_init_setpath(void)
1397
1405
{
1398
1406
char * env = getenv ("TESTPATH" );
1399
1407
if (!env ) {
1400
- fprintf ( stderr , "missing TESTPATH env var\n " );
1408
+ error ( "missing TESTPATH env var" );
1401
1409
return 1 ;
1402
1410
}
1403
1411
wchar_t * path = Py_DecodeLocale (env , NULL );
1404
1412
if (path == NULL ) {
1405
- fprintf ( stderr , "failed to decode TESTPATH\n " );
1413
+ error ( "failed to decode TESTPATH" );
1406
1414
return 1 ;
1407
1415
}
1408
1416
Py_SetPath (path );
@@ -1430,12 +1438,12 @@ static int test_init_setpath_config(void)
1430
1438
1431
1439
char * env = getenv ("TESTPATH" );
1432
1440
if (!env ) {
1433
- fprintf ( stderr , "missing TESTPATH env var\n " );
1441
+ error ( "missing TESTPATH env var" );
1434
1442
return 1 ;
1435
1443
}
1436
1444
wchar_t * path = Py_DecodeLocale (env , NULL );
1437
1445
if (path == NULL ) {
1438
- fprintf ( stderr , "failed to decode TESTPATH\n " );
1446
+ error ( "failed to decode TESTPATH" );
1439
1447
return 1 ;
1440
1448
}
1441
1449
Py_SetPath (path );
@@ -1459,12 +1467,12 @@ static int test_init_setpythonhome(void)
1459
1467
{
1460
1468
char * env = getenv ("TESTHOME" );
1461
1469
if (!env ) {
1462
- fprintf ( stderr , "missing TESTHOME env var\n " );
1470
+ error ( "missing TESTHOME env var" );
1463
1471
return 1 ;
1464
1472
}
1465
1473
wchar_t * home = Py_DecodeLocale (env , NULL );
1466
1474
if (home == NULL ) {
1467
- fprintf ( stderr , "failed to decode TESTHOME\n " );
1475
+ error ( "failed to decode TESTHOME" );
1468
1476
return 1 ;
1469
1477
}
1470
1478
Py_SetPythonHome (home );
@@ -1726,6 +1734,48 @@ static int test_unicode_id_init(void)
1726
1734
}
1727
1735
1728
1736
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
+
1729
1779
// List frozen modules.
1730
1780
// Command used by Tools/scripts/generate_stdlib_module_names.py script.
1731
1781
static int list_frozen (void )
@@ -1811,11 +1861,15 @@ static struct TestCase TestCases[] = {
1811
1861
{"test_audit_run_stdin" , test_audit_run_stdin },
1812
1862
1813
1863
{"test_unicode_id_init" , test_unicode_id_init },
1864
+ #ifndef MS_WINDOWS
1865
+ {"test_frozenmain" , test_frozenmain },
1866
+ #endif
1814
1867
1815
1868
{"list_frozen" , list_frozen },
1816
1869
{NULL , NULL }
1817
1870
};
1818
1871
1872
+
1819
1873
int main (int argc , char * argv [])
1820
1874
{
1821
1875
if (argc > 1 ) {
0 commit comments