blob: 39ff0977c8fdbf7ae39c97d24b82ed08324301f8 [file] [log] [blame]
Antoine Pitrou8e605772011-04-25 21:21:07 +02001#include <Python.h>
2#include <stdio.h>
3
Nick Coghlan7d270ee2013-10-17 22:35:35 +10004/*********************************************************
5 * Embedded interpreter tests that need a custom exe
6 *
7 * Executed via 'EmbeddingTests' in Lib/test/test_capi.py
8 *********************************************************/
9
10static void _testembed_Py_Initialize(void)
11{
12 /* HACK: the "./" at front avoids a search along the PATH in
13 Modules/getpath.c */
14 Py_SetProgramName(L"./_testembed");
15 Py_Initialize();
16}
17
18
19/*****************************************************
20 * Test repeated initalisation and subinterpreters
21 *****************************************************/
22
23static void print_subinterp(void)
Antoine Pitrou8e605772011-04-25 21:21:07 +020024{
25 /* Just output some debug stuff */
26 PyThreadState *ts = PyThreadState_Get();
27 printf("interp %p, thread state %p: ", ts->interp, ts);
28 fflush(stdout);
29 PyRun_SimpleString(
30 "import sys;"
31 "print('id(modules) =', id(sys.modules));"
32 "sys.stdout.flush()"
33 );
34}
35
Nick Coghlan7d270ee2013-10-17 22:35:35 +100036static void test_repeated_init_and_subinterpreters(void)
Antoine Pitrou8e605772011-04-25 21:21:07 +020037{
38 PyThreadState *mainstate, *substate;
Victor Stinner66299a42011-04-26 23:37:02 +020039#ifdef WITH_THREAD
Antoine Pitrou8e605772011-04-25 21:21:07 +020040 PyGILState_STATE gilstate;
Victor Stinner66299a42011-04-26 23:37:02 +020041#endif
Antoine Pitrou8e605772011-04-25 21:21:07 +020042 int i, j;
43
44 for (i=0; i<3; i++) {
45 printf("--- Pass %d ---\n", i);
Nick Coghlan7d270ee2013-10-17 22:35:35 +100046 _testembed_Py_Initialize();
Antoine Pitrou8e605772011-04-25 21:21:07 +020047 mainstate = PyThreadState_Get();
48
Victor Stinner66299a42011-04-26 23:37:02 +020049#ifdef WITH_THREAD
Antoine Pitrou8e605772011-04-25 21:21:07 +020050 PyEval_InitThreads();
51 PyEval_ReleaseThread(mainstate);
52
53 gilstate = PyGILState_Ensure();
Victor Stinner66299a42011-04-26 23:37:02 +020054#endif
Antoine Pitrou8e605772011-04-25 21:21:07 +020055 print_subinterp();
56 PyThreadState_Swap(NULL);
57
58 for (j=0; j<3; j++) {
59 substate = Py_NewInterpreter();
60 print_subinterp();
61 Py_EndInterpreter(substate);
62 }
63
64 PyThreadState_Swap(mainstate);
65 print_subinterp();
Victor Stinner66299a42011-04-26 23:37:02 +020066#ifdef WITH_THREAD
Antoine Pitrou8e605772011-04-25 21:21:07 +020067 PyGILState_Release(gilstate);
Victor Stinner66299a42011-04-26 23:37:02 +020068#endif
Antoine Pitrou8e605772011-04-25 21:21:07 +020069
70 PyEval_RestoreThread(mainstate);
71 Py_Finalize();
72 }
Nick Coghlan7d270ee2013-10-17 22:35:35 +100073}
74
75/*****************************************************
76 * Test forcing a particular IO encoding
77 *****************************************************/
78
79static void check_stdio_details(const char *encoding, const char * errors)
80{
81 /* Output info for the test case to check */
82 if (encoding) {
83 printf("Expected encoding: %s\n", encoding);
84 } else {
85 printf("Expected encoding: default\n");
86 }
87 if (errors) {
88 printf("Expected errors: %s\n", errors);
89 } else {
90 printf("Expected errors: default\n");
91 }
92 fflush(stdout);
93 /* Force the given IO encoding */
94 Py_SetStandardStreamEncoding(encoding, errors);
95 _testembed_Py_Initialize();
96 PyRun_SimpleString(
97 "import sys;"
98 "print('stdin: {0.encoding}:{0.errors}'.format(sys.stdin));"
99 "print('stdout: {0.encoding}:{0.errors}'.format(sys.stdout));"
100 "print('stderr: {0.encoding}:{0.errors}'.format(sys.stderr));"
101 "sys.stdout.flush()"
102 );
103 Py_Finalize();
104}
105
106static void test_forced_io_encoding(void)
107{
108 /* Check various combinations */
109 printf("--- Use defaults ---\n");
110 check_stdio_details(NULL, NULL);
111 printf("--- Set errors only ---\n");
Victor Stinnerb2bef622014-03-18 02:38:12 +0100112 check_stdio_details(NULL, "ignore");
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000113 printf("--- Set encoding only ---\n");
114 check_stdio_details("latin-1", NULL);
115 printf("--- Set encoding and errors ---\n");
Victor Stinnerb2bef622014-03-18 02:38:12 +0100116 check_stdio_details("latin-1", "replace");
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000117
118 /* Check calling after initialization fails */
119 Py_Initialize();
120
121 if (Py_SetStandardStreamEncoding(NULL, NULL) == 0) {
122 printf("Unexpected success calling Py_SetStandardStreamEncoding");
123 }
124 Py_Finalize();
125}
126
127/* Different embedding tests */
128int main(int argc, char *argv[])
129{
130
131 /* TODO: Check the argument string to allow for more test cases */
132 if (argc > 1) {
133 /* For now: assume "forced_io_encoding */
134 test_forced_io_encoding();
135 } else {
136 /* Run the original embedding test case by default */
137 test_repeated_init_and_subinterpreters();
138 }
Antoine Pitrou8e605772011-04-25 21:21:07 +0200139 return 0;
140}