You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

701 lines
21 KiB

  1. #include <Python.h>
  2. #include "pythread.h"
  3. #include <inttypes.h>
  4. #include <stdio.h>
  5. #include <wchar.h>
  6. /*********************************************************
  7. * Embedded interpreter tests that need a custom exe
  8. *
  9. * Executed via 'EmbeddingTests' in Lib/test/test_capi.py
  10. *********************************************************/
  11. static void _testembed_Py_Initialize(void)
  12. {
  13. /* HACK: the "./" at front avoids a search along the PATH in
  14. Modules/getpath.c */
  15. Py_SetProgramName(L"./_testembed");
  16. Py_Initialize();
  17. }
  18. /*****************************************************
  19. * Test repeated initialisation and subinterpreters
  20. *****************************************************/
  21. static void print_subinterp(void)
  22. {
  23. /* Output information about the interpreter in the format
  24. expected in Lib/test/test_capi.py (test_subinterps). */
  25. PyThreadState *ts = PyThreadState_Get();
  26. PyInterpreterState *interp = ts->interp;
  27. int64_t id = PyInterpreterState_GetID(interp);
  28. printf("interp %" PRId64 " <0x%" PRIXPTR ">, thread state <0x%" PRIXPTR ">: ",
  29. id, (uintptr_t)interp, (uintptr_t)ts);
  30. fflush(stdout);
  31. PyRun_SimpleString(
  32. "import sys;"
  33. "print('id(modules) =', id(sys.modules));"
  34. "sys.stdout.flush()"
  35. );
  36. }
  37. static int test_repeated_init_and_subinterpreters(void)
  38. {
  39. PyThreadState *mainstate, *substate;
  40. PyGILState_STATE gilstate;
  41. int i, j;
  42. for (i=0; i<15; i++) {
  43. printf("--- Pass %d ---\n", i);
  44. _testembed_Py_Initialize();
  45. mainstate = PyThreadState_Get();
  46. PyEval_InitThreads();
  47. PyEval_ReleaseThread(mainstate);
  48. gilstate = PyGILState_Ensure();
  49. print_subinterp();
  50. PyThreadState_Swap(NULL);
  51. for (j=0; j<3; j++) {
  52. substate = Py_NewInterpreter();
  53. print_subinterp();
  54. Py_EndInterpreter(substate);
  55. }
  56. PyThreadState_Swap(mainstate);
  57. print_subinterp();
  58. PyGILState_Release(gilstate);
  59. PyEval_RestoreThread(mainstate);
  60. Py_Finalize();
  61. }
  62. return 0;
  63. }
  64. /*****************************************************
  65. * Test forcing a particular IO encoding
  66. *****************************************************/
  67. static void check_stdio_details(const char *encoding, const char * errors)
  68. {
  69. /* Output info for the test case to check */
  70. if (encoding) {
  71. printf("Expected encoding: %s\n", encoding);
  72. } else {
  73. printf("Expected encoding: default\n");
  74. }
  75. if (errors) {
  76. printf("Expected errors: %s\n", errors);
  77. } else {
  78. printf("Expected errors: default\n");
  79. }
  80. fflush(stdout);
  81. /* Force the given IO encoding */
  82. Py_SetStandardStreamEncoding(encoding, errors);
  83. _testembed_Py_Initialize();
  84. PyRun_SimpleString(
  85. "import sys;"
  86. "print('stdin: {0.encoding}:{0.errors}'.format(sys.stdin));"
  87. "print('stdout: {0.encoding}:{0.errors}'.format(sys.stdout));"
  88. "print('stderr: {0.encoding}:{0.errors}'.format(sys.stderr));"
  89. "sys.stdout.flush()"
  90. );
  91. Py_Finalize();
  92. }
  93. static int test_forced_io_encoding(void)
  94. {
  95. /* Check various combinations */
  96. printf("--- Use defaults ---\n");
  97. check_stdio_details(NULL, NULL);
  98. printf("--- Set errors only ---\n");
  99. check_stdio_details(NULL, "ignore");
  100. printf("--- Set encoding only ---\n");
  101. check_stdio_details("iso8859-1", NULL);
  102. printf("--- Set encoding and errors ---\n");
  103. check_stdio_details("iso8859-1", "replace");
  104. /* Check calling after initialization fails */
  105. Py_Initialize();
  106. if (Py_SetStandardStreamEncoding(NULL, NULL) == 0) {
  107. printf("Unexpected success calling Py_SetStandardStreamEncoding");
  108. }
  109. Py_Finalize();
  110. return 0;
  111. }
  112. /*********************************************************
  113. * Test parts of the C-API that work before initialization
  114. *********************************************************/
  115. /* The pre-initialization tests tend to break by segfaulting, so explicitly
  116. * flushed progress messages make the broken API easier to find when they fail.
  117. */
  118. #define _Py_EMBED_PREINIT_CHECK(msg) \
  119. do {printf(msg); fflush(stdout);} while (0);
  120. static int test_pre_initialization_api(void)
  121. {
  122. /* Leading "./" ensures getpath.c can still find the standard library */
  123. _Py_EMBED_PREINIT_CHECK("Checking Py_DecodeLocale\n");
  124. wchar_t *program = Py_DecodeLocale("./spam", NULL);
  125. if (program == NULL) {
  126. fprintf(stderr, "Fatal error: cannot decode program name\n");
  127. return 1;
  128. }
  129. _Py_EMBED_PREINIT_CHECK("Checking Py_SetProgramName\n");
  130. Py_SetProgramName(program);
  131. _Py_EMBED_PREINIT_CHECK("Initializing interpreter\n");
  132. Py_Initialize();
  133. _Py_EMBED_PREINIT_CHECK("Check sys module contents\n");
  134. PyRun_SimpleString("import sys; "
  135. "print('sys.executable:', sys.executable)");
  136. _Py_EMBED_PREINIT_CHECK("Finalizing interpreter\n");
  137. Py_Finalize();
  138. _Py_EMBED_PREINIT_CHECK("Freeing memory allocated by Py_DecodeLocale\n");
  139. PyMem_RawFree(program);
  140. return 0;
  141. }
  142. /* bpo-33042: Ensure embedding apps can predefine sys module options */
  143. static int test_pre_initialization_sys_options(void)
  144. {
  145. /* We allocate a couple of the options dynamically, and then delete
  146. * them before calling Py_Initialize. This ensures the interpreter isn't
  147. * relying on the caller to keep the passed in strings alive.
  148. */
  149. const wchar_t *static_warnoption = L"once";
  150. const wchar_t *static_xoption = L"also_not_an_option=2";
  151. size_t warnoption_len = wcslen(static_warnoption);
  152. size_t xoption_len = wcslen(static_xoption);
  153. wchar_t *dynamic_once_warnoption = \
  154. (wchar_t *) calloc(warnoption_len+1, sizeof(wchar_t));
  155. wchar_t *dynamic_xoption = \
  156. (wchar_t *) calloc(xoption_len+1, sizeof(wchar_t));
  157. wcsncpy(dynamic_once_warnoption, static_warnoption, warnoption_len+1);
  158. wcsncpy(dynamic_xoption, static_xoption, xoption_len+1);
  159. _Py_EMBED_PREINIT_CHECK("Checking PySys_AddWarnOption\n");
  160. PySys_AddWarnOption(L"default");
  161. _Py_EMBED_PREINIT_CHECK("Checking PySys_ResetWarnOptions\n");
  162. PySys_ResetWarnOptions();
  163. _Py_EMBED_PREINIT_CHECK("Checking PySys_AddWarnOption linked list\n");
  164. PySys_AddWarnOption(dynamic_once_warnoption);
  165. PySys_AddWarnOption(L"module");
  166. PySys_AddWarnOption(L"default");
  167. _Py_EMBED_PREINIT_CHECK("Checking PySys_AddXOption\n");
  168. PySys_AddXOption(L"not_an_option=1");
  169. PySys_AddXOption(dynamic_xoption);
  170. /* Delete the dynamic options early */
  171. free(dynamic_once_warnoption);
  172. dynamic_once_warnoption = NULL;
  173. free(dynamic_xoption);
  174. dynamic_xoption = NULL;
  175. _Py_EMBED_PREINIT_CHECK("Initializing interpreter\n");
  176. _testembed_Py_Initialize();
  177. _Py_EMBED_PREINIT_CHECK("Check sys module contents\n");
  178. PyRun_SimpleString("import sys; "
  179. "print('sys.warnoptions:', sys.warnoptions); "
  180. "print('sys._xoptions:', sys._xoptions); "
  181. "warnings = sys.modules['warnings']; "
  182. "latest_filters = [f[0] for f in warnings.filters[:3]]; "
  183. "print('warnings.filters[:3]:', latest_filters)");
  184. _Py_EMBED_PREINIT_CHECK("Finalizing interpreter\n");
  185. Py_Finalize();
  186. return 0;
  187. }
  188. /* bpo-20891: Avoid race condition when initialising the GIL */
  189. static void bpo20891_thread(void *lockp)
  190. {
  191. PyThread_type_lock lock = *((PyThread_type_lock*)lockp);
  192. PyGILState_STATE state = PyGILState_Ensure();
  193. if (!PyGILState_Check()) {
  194. fprintf(stderr, "PyGILState_Check failed!");
  195. abort();
  196. }
  197. PyGILState_Release(state);
  198. PyThread_release_lock(lock);
  199. PyThread_exit_thread();
  200. }
  201. static int test_bpo20891(void)
  202. {
  203. /* bpo-20891: Calling PyGILState_Ensure in a non-Python thread before
  204. calling PyEval_InitThreads() must not crash. PyGILState_Ensure() must
  205. call PyEval_InitThreads() for us in this case. */
  206. PyThread_type_lock lock = PyThread_allocate_lock();
  207. if (!lock) {
  208. fprintf(stderr, "PyThread_allocate_lock failed!");
  209. return 1;
  210. }
  211. _testembed_Py_Initialize();
  212. unsigned long thrd = PyThread_start_new_thread(bpo20891_thread, &lock);
  213. if (thrd == PYTHREAD_INVALID_THREAD_ID) {
  214. fprintf(stderr, "PyThread_start_new_thread failed!");
  215. return 1;
  216. }
  217. PyThread_acquire_lock(lock, WAIT_LOCK);
  218. Py_BEGIN_ALLOW_THREADS
  219. /* wait until the thread exit */
  220. PyThread_acquire_lock(lock, WAIT_LOCK);
  221. Py_END_ALLOW_THREADS
  222. PyThread_free_lock(lock);
  223. return 0;
  224. }
  225. static int test_initialize_twice(void)
  226. {
  227. _testembed_Py_Initialize();
  228. /* bpo-33932: Calling Py_Initialize() twice should do nothing
  229. * (and not crash!). */
  230. Py_Initialize();
  231. Py_Finalize();
  232. return 0;
  233. }
  234. static int test_initialize_pymain(void)
  235. {
  236. wchar_t *argv[] = {L"PYTHON", L"-c",
  237. L"import sys; print(f'Py_Main() after Py_Initialize: sys.argv={sys.argv}')",
  238. L"arg2"};
  239. _testembed_Py_Initialize();
  240. /* bpo-34008: Calling Py_Main() after Py_Initialize() must not crash */
  241. Py_Main(Py_ARRAY_LENGTH(argv), argv);
  242. Py_Finalize();
  243. return 0;
  244. }
  245. static void
  246. dump_config(void)
  247. {
  248. #define ASSERT_EQUAL(a, b) \
  249. if ((a) != (b)) { \
  250. printf("ERROR: %s != %s (%i != %i)\n", #a, #b, (a), (b)); \
  251. exit(1); \
  252. }
  253. #define ASSERT_STR_EQUAL(a, b) \
  254. if ((a) == NULL || (b == NULL) || wcscmp((a), (b)) != 0) { \
  255. printf("ERROR: %s != %s ('%ls' != '%ls')\n", #a, #b, (a), (b)); \
  256. exit(1); \
  257. }
  258. PyInterpreterState *interp = _PyInterpreterState_Get();
  259. _PyCoreConfig *config = &interp->core_config;
  260. printf("install_signal_handlers = %i\n", config->install_signal_handlers);
  261. printf("use_environment = %i\n", config->use_environment);
  262. ASSERT_EQUAL(config->use_environment, !Py_IgnoreEnvironmentFlag);
  263. printf("use_hash_seed = %i\n", config->use_hash_seed);
  264. printf("hash_seed = %lu\n", config->hash_seed);
  265. printf("allocator = %s\n", config->allocator);
  266. printf("dev_mode = %i\n", config->dev_mode);
  267. printf("faulthandler = %i\n", config->faulthandler);
  268. printf("tracemalloc = %i\n", config->tracemalloc);
  269. printf("import_time = %i\n", config->import_time);
  270. printf("show_ref_count = %i\n", config->show_ref_count);
  271. printf("show_alloc_count = %i\n", config->show_alloc_count);
  272. printf("dump_refs = %i\n", config->dump_refs);
  273. printf("malloc_stats = %i\n", config->malloc_stats);
  274. printf("filesystem_encoding = %s\n", config->filesystem_encoding);
  275. printf("filesystem_errors = %s\n", config->filesystem_errors);
  276. printf("coerce_c_locale = %i\n", config->coerce_c_locale);
  277. printf("coerce_c_locale_warn = %i\n", config->coerce_c_locale_warn);
  278. printf("utf8_mode = %i\n", config->utf8_mode);
  279. printf("pycache_prefix = %ls\n", config->pycache_prefix);
  280. printf("program_name = %ls\n", config->program_name);
  281. ASSERT_STR_EQUAL(config->program_name, Py_GetProgramName());
  282. printf("argc = %i\n", config->argc);
  283. printf("argv = [");
  284. for (int i=0; i < config->argc; i++) {
  285. if (i) {
  286. printf(", ");
  287. }
  288. printf("\"%ls\"", config->argv[i]);
  289. }
  290. printf("]\n");
  291. printf("program = %ls\n", config->program);
  292. /* FIXME: test xoptions */
  293. /* FIXME: test warnoptions */
  294. /* FIXME: test module_search_path_env */
  295. /* FIXME: test home */
  296. /* FIXME: test module_search_paths */
  297. /* FIXME: test executable */
  298. /* FIXME: test prefix */
  299. /* FIXME: test base_prefix */
  300. /* FIXME: test exec_prefix */
  301. /* FIXME: test base_exec_prefix */
  302. /* FIXME: test dll_path */
  303. printf("isolated = %i\n", config->isolated);
  304. ASSERT_EQUAL(config->isolated, Py_IsolatedFlag);
  305. printf("site_import = %i\n", config->site_import);
  306. printf("bytes_warning = %i\n", config->bytes_warning);
  307. printf("inspect = %i\n", config->inspect);
  308. printf("interactive = %i\n", config->interactive);
  309. printf("optimization_level = %i\n", config->optimization_level);
  310. printf("parser_debug = %i\n", config->parser_debug);
  311. printf("write_bytecode = %i\n", config->write_bytecode);
  312. printf("verbose = %i\n", config->verbose);
  313. ASSERT_EQUAL(config->verbose, Py_VerboseFlag);
  314. printf("quiet = %i\n", config->quiet);
  315. printf("user_site_directory = %i\n", config->user_site_directory);
  316. printf("buffered_stdio = %i\n", config->buffered_stdio);
  317. ASSERT_EQUAL(config->buffered_stdio, !Py_UnbufferedStdioFlag);
  318. printf("stdio_encoding = %s\n", config->stdio_encoding);
  319. printf("stdio_errors = %s\n", config->stdio_errors);
  320. /* FIXME: test legacy_windows_fs_encoding */
  321. /* FIXME: test legacy_windows_stdio */
  322. printf("_install_importlib = %i\n", config->_install_importlib);
  323. printf("_check_hash_pycs_mode = %s\n", config->_check_hash_pycs_mode);
  324. printf("_frozen = %i\n", config->_frozen);
  325. #undef ASSERT_EQUAL
  326. #undef ASSERT_STR_EQUAL
  327. }
  328. static int test_init_default_config(void)
  329. {
  330. _testembed_Py_Initialize();
  331. dump_config();
  332. Py_Finalize();
  333. return 0;
  334. }
  335. static int test_init_global_config(void)
  336. {
  337. /* FIXME: test Py_IgnoreEnvironmentFlag */
  338. putenv("PYTHONUTF8=0");
  339. Py_UTF8Mode = 1;
  340. /* Test initialization from global configuration variables (Py_xxx) */
  341. Py_SetProgramName(L"./globalvar");
  342. /* Py_IsolatedFlag is not tested */
  343. Py_NoSiteFlag = 1;
  344. Py_BytesWarningFlag = 1;
  345. putenv("PYTHONINSPECT=");
  346. Py_InspectFlag = 1;
  347. putenv("PYTHONOPTIMIZE=0");
  348. Py_InteractiveFlag = 1;
  349. putenv("PYTHONDEBUG=0");
  350. Py_OptimizeFlag = 2;
  351. /* Py_DebugFlag is not tested */
  352. putenv("PYTHONDONTWRITEBYTECODE=");
  353. Py_DontWriteBytecodeFlag = 1;
  354. putenv("PYTHONVERBOSE=0");
  355. Py_VerboseFlag = 1;
  356. Py_QuietFlag = 1;
  357. Py_NoUserSiteDirectory = 1;
  358. putenv("PYTHONUNBUFFERED=");
  359. Py_UnbufferedStdioFlag = 1;
  360. Py_FrozenFlag = 1;
  361. /* FIXME: test Py_LegacyWindowsFSEncodingFlag */
  362. /* FIXME: test Py_LegacyWindowsStdioFlag */
  363. Py_Initialize();
  364. dump_config();
  365. Py_Finalize();
  366. return 0;
  367. }
  368. static int test_init_from_config(void)
  369. {
  370. /* Test _Py_InitializeFromConfig() */
  371. _PyCoreConfig config = _PyCoreConfig_INIT;
  372. config.install_signal_handlers = 0;
  373. /* FIXME: test use_environment */
  374. putenv("PYTHONHASHSEED=42");
  375. config.use_hash_seed = 1;
  376. config.hash_seed = 123;
  377. putenv("PYTHONMALLOC=malloc");
  378. config.allocator = "malloc_debug";
  379. /* dev_mode=1 is tested in test_init_dev_mode() */
  380. putenv("PYTHONFAULTHANDLER=");
  381. config.faulthandler = 1;
  382. putenv("PYTHONTRACEMALLOC=0");
  383. config.tracemalloc = 2;
  384. putenv("PYTHONPROFILEIMPORTTIME=0");
  385. config.import_time = 1;
  386. config.show_ref_count = 1;
  387. config.show_alloc_count = 1;
  388. /* FIXME: test dump_refs: bpo-34223 */
  389. putenv("PYTHONMALLOCSTATS=0");
  390. config.malloc_stats = 1;
  391. /* FIXME: test coerce_c_locale and coerce_c_locale_warn */
  392. putenv("PYTHONUTF8=0");
  393. Py_UTF8Mode = 0;
  394. config.utf8_mode = 1;
  395. putenv("PYTHONPYCACHEPREFIX=env_pycache_prefix");
  396. config.pycache_prefix = L"conf_pycache_prefix";
  397. Py_SetProgramName(L"./globalvar");
  398. config.program_name = L"./conf_program_name";
  399. /* FIXME: test argc/argv */
  400. config.program = L"conf_program";
  401. /* FIXME: test xoptions */
  402. /* FIXME: test warnoptions */
  403. /* FIXME: test module_search_path_env */
  404. /* FIXME: test home */
  405. /* FIXME: test path config: module_search_path .. dll_path */
  406. putenv("PYTHONVERBOSE=0");
  407. Py_VerboseFlag = 0;
  408. config.verbose = 1;
  409. Py_NoSiteFlag = 0;
  410. config.site_import = 0;
  411. Py_BytesWarningFlag = 0;
  412. config.bytes_warning = 1;
  413. putenv("PYTHONINSPECT=");
  414. Py_InspectFlag = 0;
  415. config.inspect = 1;
  416. Py_InteractiveFlag = 0;
  417. config.interactive = 1;
  418. putenv("PYTHONOPTIMIZE=0");
  419. Py_OptimizeFlag = 1;
  420. config.optimization_level = 2;
  421. /* FIXME: test parser_debug */
  422. putenv("PYTHONDONTWRITEBYTECODE=");
  423. Py_DontWriteBytecodeFlag = 0;
  424. config.write_bytecode = 0;
  425. Py_QuietFlag = 0;
  426. config.quiet = 1;
  427. putenv("PYTHONUNBUFFERED=");
  428. Py_UnbufferedStdioFlag = 0;
  429. config.buffered_stdio = 0;
  430. putenv("PYTHONIOENCODING=cp424");
  431. Py_SetStandardStreamEncoding("ascii", "ignore");
  432. config.stdio_encoding = "iso8859-1";
  433. config.stdio_errors = "replace";
  434. putenv("PYTHONNOUSERSITE=");
  435. Py_NoUserSiteDirectory = 0;
  436. config.user_site_directory = 0;
  437. config._check_hash_pycs_mode = "always";
  438. Py_FrozenFlag = 0;
  439. config._frozen = 1;
  440. _PyInitError err = _Py_InitializeFromConfig(&config);
  441. /* Don't call _PyCoreConfig_Clear() since all strings are static */
  442. if (_Py_INIT_FAILED(err)) {
  443. _Py_FatalInitError(err);
  444. }
  445. dump_config();
  446. Py_Finalize();
  447. return 0;
  448. }
  449. static void test_init_env_putenvs(void)
  450. {
  451. putenv("PYTHONHASHSEED=42");
  452. putenv("PYTHONMALLOC=malloc_debug");
  453. putenv("PYTHONTRACEMALLOC=2");
  454. putenv("PYTHONPROFILEIMPORTTIME=1");
  455. putenv("PYTHONMALLOCSTATS=1");
  456. putenv("PYTHONUTF8=1");
  457. putenv("PYTHONVERBOSE=1");
  458. putenv("PYTHONINSPECT=1");
  459. putenv("PYTHONOPTIMIZE=2");
  460. putenv("PYTHONDONTWRITEBYTECODE=1");
  461. putenv("PYTHONUNBUFFERED=1");
  462. putenv("PYTHONPYCACHEPREFIX=env_pycache_prefix");
  463. putenv("PYTHONNOUSERSITE=1");
  464. putenv("PYTHONFAULTHANDLER=1");
  465. putenv("PYTHONDEVMODE=1");
  466. putenv("PYTHONIOENCODING=iso8859-1:replace");
  467. /* FIXME: test PYTHONWARNINGS */
  468. /* FIXME: test PYTHONEXECUTABLE */
  469. /* FIXME: test PYTHONHOME */
  470. /* FIXME: test PYTHONDEBUG */
  471. /* FIXME: test PYTHONDUMPREFS */
  472. /* FIXME: test PYTHONCOERCECLOCALE */
  473. /* FIXME: test PYTHONPATH */
  474. }
  475. static int test_init_env(void)
  476. {
  477. /* Test initialization from environment variables */
  478. Py_IgnoreEnvironmentFlag = 0;
  479. test_init_env_putenvs();
  480. _testembed_Py_Initialize();
  481. dump_config();
  482. Py_Finalize();
  483. return 0;
  484. }
  485. static int test_init_isolated(void)
  486. {
  487. /* Test _PyCoreConfig.isolated=1 */
  488. _PyCoreConfig config = _PyCoreConfig_INIT;
  489. /* Set coerce_c_locale and utf8_mode to not depend on the locale */
  490. config.coerce_c_locale = 0;
  491. config.utf8_mode = 0;
  492. /* Use path starting with "./" avoids a search along the PATH */
  493. config.program_name = L"./_testembed";
  494. Py_IsolatedFlag = 0;
  495. config.isolated = 1;
  496. test_init_env_putenvs();
  497. _PyInitError err = _Py_InitializeFromConfig(&config);
  498. if (_Py_INIT_FAILED(err)) {
  499. _Py_FatalInitError(err);
  500. }
  501. dump_config();
  502. Py_Finalize();
  503. return 0;
  504. }
  505. static int test_init_dev_mode(void)
  506. {
  507. _PyCoreConfig config = _PyCoreConfig_INIT;
  508. putenv("PYTHONFAULTHANDLER=");
  509. putenv("PYTHONMALLOC=");
  510. config.dev_mode = 1;
  511. config.program_name = L"./_testembed";
  512. _PyInitError err = _Py_InitializeFromConfig(&config);
  513. if (_Py_INIT_FAILED(err)) {
  514. _Py_FatalInitError(err);
  515. }
  516. dump_config();
  517. Py_Finalize();
  518. return 0;
  519. }
  520. /* *********************************************************
  521. * List of test cases and the function that implements it.
  522. *
  523. * Names are compared case-sensitively with the first
  524. * argument. If no match is found, or no first argument was
  525. * provided, the names of all test cases are printed and
  526. * the exit code will be -1.
  527. *
  528. * The int returned from test functions is used as the exit
  529. * code, and test_capi treats all non-zero exit codes as a
  530. * failed test.
  531. *********************************************************/
  532. struct TestCase
  533. {
  534. const char *name;
  535. int (*func)(void);
  536. };
  537. static struct TestCase TestCases[] = {
  538. { "forced_io_encoding", test_forced_io_encoding },
  539. { "repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters },
  540. { "pre_initialization_api", test_pre_initialization_api },
  541. { "pre_initialization_sys_options", test_pre_initialization_sys_options },
  542. { "bpo20891", test_bpo20891 },
  543. { "initialize_twice", test_initialize_twice },
  544. { "initialize_pymain", test_initialize_pymain },
  545. { "init_default_config", test_init_default_config },
  546. { "init_global_config", test_init_global_config },
  547. { "init_from_config", test_init_from_config },
  548. { "init_env", test_init_env },
  549. { "init_dev_mode", test_init_dev_mode },
  550. { "init_isolated", test_init_isolated },
  551. { NULL, NULL }
  552. };
  553. int main(int argc, char *argv[])
  554. {
  555. if (argc > 1) {
  556. for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
  557. if (strcmp(argv[1], tc->name) == 0)
  558. return (*tc->func)();
  559. }
  560. }
  561. /* No match found, or no test name provided, so display usage */
  562. printf("Python " PY_VERSION " _testembed executable for embedded interpreter tests\n"
  563. "Normally executed via 'EmbeddingTests' in Lib/test/test_embed.py\n\n"
  564. "Usage: %s TESTNAME\n\nAll available tests:\n", argv[0]);
  565. for (struct TestCase *tc = TestCases; tc && tc->name; tc++) {
  566. printf(" %s\n", tc->name);
  567. }
  568. /* Non-zero exit code will cause test_embed.py tests to fail.
  569. This is intentional. */
  570. return -1;
  571. }