@@ -1741,6 +1741,16 @@ static int initconfig_getint(PyInitConfig *config, const char *name)
17411741 return (int )value ;
17421742}
17431743
1744+ static void initconfig_error (PyInitConfig * config )
1745+ {
1746+ const char * err_msg ;
1747+ int res = PyInitConfig_GetError (config , & err_msg );
1748+ assert (res == 1 );
1749+
1750+ printf ("Python init failed: %s\n" , err_msg );
1751+ PyInitConfig_Free (config );
1752+ }
1753+
17441754
17451755static int test_initconfig_api (void )
17461756{
@@ -1795,12 +1805,8 @@ static int test_initconfig_api(void)
17951805 return 0 ;
17961806
17971807error :
1798- {
1799- const char * err_msg ;
1800- (void )PyInitConfig_GetError (config , & err_msg );
1801- printf ("Python init failed: %s\n" , err_msg );
1802- exit (1 );
1803- }
1808+ initconfig_error (config );
1809+ return 1 ;
18041810}
18051811
18061812
@@ -1953,12 +1959,8 @@ static int test_initconfig_module(void)
19531959 return 0 ;
19541960
19551961error :
1956- {
1957- const char * err_msg ;
1958- (void )PyInitConfig_GetError (config , & err_msg );
1959- printf ("Python init failed: %s\n" , err_msg );
1960- exit (1 );
1961- }
1962+ initconfig_error (config );
1963+ return 1 ;
19621964}
19631965
19641966
@@ -2170,6 +2172,86 @@ static int test_init_in_background_thread(void)
21702172}
21712173
21722174
2175+ static PyStatus init_callback (void * arg )
2176+ {
2177+ const char * msg = (const char * )arg ;
2178+ fprintf (stderr , "%s\n" , msg );
2179+
2180+ // Write sorted(sys.modules) to sys.stderr
2181+ PyObject * modules = PySys_GetAttrString ("modules" );
2182+ if (modules == NULL ) {
2183+ return PyStatus_Error ("failed to get sys.modules" );
2184+ }
2185+
2186+ PyObject * builtins = PyEval_GetBuiltins (); // borrowed ref
2187+ if (builtins == NULL ) {
2188+ Py_DECREF (modules );
2189+ return PyStatus_Error ("failed to get builtins" );
2190+ }
2191+
2192+ PyObject * sorted ;
2193+ if (PyDict_GetItemStringRef (builtins , "sorted" , & sorted ) <= 0 ) {
2194+ Py_DECREF (modules );
2195+ return PyStatus_Error ("failed to get sorted" );
2196+ }
2197+
2198+ PyObject * names = PyObject_CallOneArg (sorted , modules );
2199+ Py_DECREF (modules );
2200+ if (names == NULL ) {
2201+ return PyStatus_Error ("sorted failed" );
2202+ }
2203+
2204+ PySys_FormatStderr ("sys.modules: %R\n" , names );
2205+ Py_DECREF (names );
2206+
2207+ // Write sys.meta_path to sys.stderr
2208+ const char * code = (
2209+ "import sys; "
2210+ "print(f\"sys.meta_path: {sys.meta_path}\", file=sys.stderr)" );
2211+ if (PyRun_SimpleString (code ) < 0 ) {
2212+ return PyStatus_Error ("PyRun_SimpleString failed" );
2213+ }
2214+
2215+ return PyStatus_Ok ();
2216+ }
2217+
2218+
2219+ static int test_init_callback (void )
2220+ {
2221+ PyInitConfig * config = PyInitConfig_Create ();
2222+ if (config == NULL ) {
2223+ printf ("Init allocation error\n" );
2224+ return 1 ;
2225+ }
2226+
2227+ if (PyInitConfig_SetStr (config , "program_name" , PROGRAM_NAME_UTF8 ) < 0 ) {
2228+ goto error ;
2229+ }
2230+
2231+ const char * ignored_msg = "ignored_msg" ;
2232+ if (PyInitConfig_SetInitCallback (config , init_callback , (void * )ignored_msg ) < 0 ) {
2233+ goto error ;
2234+ }
2235+
2236+ // PyInitConfig_SetInitCallback() can be called more than once, but the
2237+ // previous callback and callback argument are overridden.
2238+ const char * msg = "Hello Callback!" ;
2239+ if (PyInitConfig_SetInitCallback (config , init_callback , (void * )msg ) < 0 ) {
2240+ goto error ;
2241+ }
2242+
2243+ if (Py_InitializeFromInitConfig (config ) < 0 ) {
2244+ goto error ;
2245+ }
2246+ PyInitConfig_Free (config );
2247+ return 0 ;
2248+
2249+ error :
2250+ initconfig_error (config );
2251+ return 1 ;
2252+ }
2253+
2254+
21732255#ifndef MS_WINDOWS
21742256#include "test_frozenmain.h" // M_test_frozenmain
21752257
@@ -2658,6 +2740,7 @@ static struct TestCase TestCases[] = {
26582740 {"test_init_use_frozen_modules" , test_init_use_frozen_modules },
26592741 {"test_init_main_interpreter_settings" , test_init_main_interpreter_settings },
26602742 {"test_init_in_background_thread" , test_init_in_background_thread },
2743+ {"test_init_callback" , test_init_callback },
26612744
26622745 // Audit
26632746 {"test_open_code_hook" , test_open_code_hook },
0 commit comments