Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 1 | #include <Python.h> |
Victor Stinner | b4d1e1f | 2017-11-30 22:05:00 +0100 | [diff] [blame] | 2 | #include "pythread.h" |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 3 | #include <inttypes.h> |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 4 | #include <stdio.h> |
| 5 | |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 6 | /********************************************************* |
| 7 | * Embedded interpreter tests that need a custom exe |
| 8 | * |
| 9 | * Executed via 'EmbeddingTests' in Lib/test/test_capi.py |
| 10 | *********************************************************/ |
| 11 | |
| 12 | static void _testembed_Py_Initialize(void) |
| 13 | { |
| 14 | /* HACK: the "./" at front avoids a search along the PATH in |
| 15 | Modules/getpath.c */ |
| 16 | Py_SetProgramName(L"./_testembed"); |
| 17 | Py_Initialize(); |
| 18 | } |
| 19 | |
| 20 | |
| 21 | /***************************************************** |
Martin Panter | 8f26565 | 2016-04-19 04:03:41 +0000 | [diff] [blame] | 22 | * Test repeated initialisation and subinterpreters |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 23 | *****************************************************/ |
| 24 | |
| 25 | static void print_subinterp(void) |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 26 | { |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 27 | /* Output information about the interpreter in the format |
| 28 | expected in Lib/test/test_capi.py (test_subinterps). */ |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 29 | PyThreadState *ts = PyThreadState_Get(); |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 30 | PyInterpreterState *interp = ts->interp; |
| 31 | int64_t id = PyInterpreterState_GetID(interp); |
Eric Snow | d1c3c13 | 2017-05-24 17:19:47 -0700 | [diff] [blame] | 32 | printf("interp %" PRId64 " <0x%" PRIXPTR ">, thread state <0x%" PRIXPTR ">: ", |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 33 | id, (uintptr_t)interp, (uintptr_t)ts); |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 34 | fflush(stdout); |
| 35 | PyRun_SimpleString( |
| 36 | "import sys;" |
| 37 | "print('id(modules) =', id(sys.modules));" |
| 38 | "sys.stdout.flush()" |
| 39 | ); |
| 40 | } |
| 41 | |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 42 | static int test_repeated_init_and_subinterpreters(void) |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 43 | { |
| 44 | PyThreadState *mainstate, *substate; |
| 45 | PyGILState_STATE gilstate; |
| 46 | int i, j; |
| 47 | |
Ned Deily | 939231b | 2016-08-16 00:17:42 -0400 | [diff] [blame] | 48 | for (i=0; i<15; i++) { |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 49 | printf("--- Pass %d ---\n", i); |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 50 | _testembed_Py_Initialize(); |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 51 | mainstate = PyThreadState_Get(); |
| 52 | |
| 53 | PyEval_InitThreads(); |
| 54 | PyEval_ReleaseThread(mainstate); |
| 55 | |
| 56 | gilstate = PyGILState_Ensure(); |
| 57 | print_subinterp(); |
| 58 | PyThreadState_Swap(NULL); |
| 59 | |
| 60 | for (j=0; j<3; j++) { |
| 61 | substate = Py_NewInterpreter(); |
| 62 | print_subinterp(); |
| 63 | Py_EndInterpreter(substate); |
| 64 | } |
| 65 | |
| 66 | PyThreadState_Swap(mainstate); |
| 67 | print_subinterp(); |
| 68 | PyGILState_Release(gilstate); |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 69 | |
| 70 | PyEval_RestoreThread(mainstate); |
| 71 | Py_Finalize(); |
| 72 | } |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 73 | return 0; |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /***************************************************** |
| 77 | * Test forcing a particular IO encoding |
| 78 | *****************************************************/ |
| 79 | |
| 80 | static void check_stdio_details(const char *encoding, const char * errors) |
| 81 | { |
| 82 | /* Output info for the test case to check */ |
| 83 | if (encoding) { |
| 84 | printf("Expected encoding: %s\n", encoding); |
| 85 | } else { |
| 86 | printf("Expected encoding: default\n"); |
| 87 | } |
| 88 | if (errors) { |
| 89 | printf("Expected errors: %s\n", errors); |
| 90 | } else { |
| 91 | printf("Expected errors: default\n"); |
| 92 | } |
| 93 | fflush(stdout); |
| 94 | /* Force the given IO encoding */ |
| 95 | Py_SetStandardStreamEncoding(encoding, errors); |
| 96 | _testembed_Py_Initialize(); |
| 97 | PyRun_SimpleString( |
| 98 | "import sys;" |
| 99 | "print('stdin: {0.encoding}:{0.errors}'.format(sys.stdin));" |
| 100 | "print('stdout: {0.encoding}:{0.errors}'.format(sys.stdout));" |
| 101 | "print('stderr: {0.encoding}:{0.errors}'.format(sys.stderr));" |
| 102 | "sys.stdout.flush()" |
| 103 | ); |
| 104 | Py_Finalize(); |
| 105 | } |
| 106 | |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 107 | static int test_forced_io_encoding(void) |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 108 | { |
| 109 | /* Check various combinations */ |
| 110 | printf("--- Use defaults ---\n"); |
| 111 | check_stdio_details(NULL, NULL); |
| 112 | printf("--- Set errors only ---\n"); |
Victor Stinner | b2bef62 | 2014-03-18 02:38:12 +0100 | [diff] [blame] | 113 | check_stdio_details(NULL, "ignore"); |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 114 | printf("--- Set encoding only ---\n"); |
| 115 | check_stdio_details("latin-1", NULL); |
| 116 | printf("--- Set encoding and errors ---\n"); |
Victor Stinner | b2bef62 | 2014-03-18 02:38:12 +0100 | [diff] [blame] | 117 | check_stdio_details("latin-1", "replace"); |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 118 | |
| 119 | /* Check calling after initialization fails */ |
| 120 | Py_Initialize(); |
| 121 | |
| 122 | if (Py_SetStandardStreamEncoding(NULL, NULL) == 0) { |
| 123 | printf("Unexpected success calling Py_SetStandardStreamEncoding"); |
| 124 | } |
| 125 | Py_Finalize(); |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 126 | return 0; |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 127 | } |
| 128 | |
Victor Stinner | 9e87e77 | 2017-11-24 12:09:24 +0100 | [diff] [blame] | 129 | /********************************************************* |
| 130 | * Test parts of the C-API that work before initialization |
| 131 | *********************************************************/ |
| 132 | |
| 133 | static int test_pre_initialization_api(void) |
| 134 | { |
Nick Coghlan | 4274609 | 2017-11-26 14:19:13 +1000 | [diff] [blame] | 135 | /* Leading "./" ensures getpath.c can still find the standard library */ |
| 136 | wchar_t *program = Py_DecodeLocale("./spam", NULL); |
Victor Stinner | 9e87e77 | 2017-11-24 12:09:24 +0100 | [diff] [blame] | 137 | if (program == NULL) { |
| 138 | fprintf(stderr, "Fatal error: cannot decode program name\n"); |
| 139 | return 1; |
| 140 | } |
| 141 | Py_SetProgramName(program); |
| 142 | |
| 143 | Py_Initialize(); |
| 144 | Py_Finalize(); |
| 145 | |
| 146 | PyMem_RawFree(program); |
| 147 | return 0; |
| 148 | } |
| 149 | |
Victor Stinner | b4d1e1f | 2017-11-30 22:05:00 +0100 | [diff] [blame] | 150 | static void bpo20891_thread(void *lockp) |
| 151 | { |
| 152 | PyThread_type_lock lock = *((PyThread_type_lock*)lockp); |
| 153 | |
| 154 | PyGILState_STATE state = PyGILState_Ensure(); |
| 155 | if (!PyGILState_Check()) { |
| 156 | fprintf(stderr, "PyGILState_Check failed!"); |
| 157 | abort(); |
| 158 | } |
| 159 | |
| 160 | PyGILState_Release(state); |
| 161 | |
| 162 | PyThread_release_lock(lock); |
| 163 | |
| 164 | PyThread_exit_thread(); |
| 165 | } |
| 166 | |
| 167 | static int test_bpo20891(void) |
| 168 | { |
| 169 | /* bpo-20891: Calling PyGILState_Ensure in a non-Python thread before |
| 170 | calling PyEval_InitThreads() must not crash. PyGILState_Ensure() must |
| 171 | call PyEval_InitThreads() for us in this case. */ |
| 172 | PyThread_type_lock lock = PyThread_allocate_lock(); |
| 173 | if (!lock) { |
| 174 | fprintf(stderr, "PyThread_allocate_lock failed!"); |
| 175 | return 1; |
| 176 | } |
| 177 | |
| 178 | _testembed_Py_Initialize(); |
| 179 | |
| 180 | unsigned long thrd = PyThread_start_new_thread(bpo20891_thread, &lock); |
| 181 | if (thrd == PYTHREAD_INVALID_THREAD_ID) { |
| 182 | fprintf(stderr, "PyThread_start_new_thread failed!"); |
| 183 | return 1; |
| 184 | } |
| 185 | PyThread_acquire_lock(lock, WAIT_LOCK); |
| 186 | |
| 187 | Py_BEGIN_ALLOW_THREADS |
| 188 | /* wait until the thread exit */ |
| 189 | PyThread_acquire_lock(lock, WAIT_LOCK); |
| 190 | Py_END_ALLOW_THREADS |
| 191 | |
| 192 | PyThread_free_lock(lock); |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
Victor Stinner | 9e87e77 | 2017-11-24 12:09:24 +0100 | [diff] [blame] | 197 | |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 198 | /* ********************************************************* |
| 199 | * List of test cases and the function that implements it. |
Serhiy Storchaka | 13ad3b7 | 2017-09-14 09:38:36 +0300 | [diff] [blame] | 200 | * |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 201 | * Names are compared case-sensitively with the first |
| 202 | * argument. If no match is found, or no first argument was |
| 203 | * provided, the names of all test cases are printed and |
| 204 | * the exit code will be -1. |
| 205 | * |
| 206 | * The int returned from test functions is used as the exit |
| 207 | * code, and test_capi treats all non-zero exit codes as a |
Serhiy Storchaka | 13ad3b7 | 2017-09-14 09:38:36 +0300 | [diff] [blame] | 208 | * failed test. |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 209 | *********************************************************/ |
| 210 | struct TestCase |
| 211 | { |
| 212 | const char *name; |
| 213 | int (*func)(void); |
| 214 | }; |
| 215 | |
| 216 | static struct TestCase TestCases[] = { |
| 217 | { "forced_io_encoding", test_forced_io_encoding }, |
| 218 | { "repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters }, |
Victor Stinner | 9e87e77 | 2017-11-24 12:09:24 +0100 | [diff] [blame] | 219 | { "pre_initialization_api", test_pre_initialization_api }, |
Victor Stinner | b4d1e1f | 2017-11-30 22:05:00 +0100 | [diff] [blame] | 220 | { "bpo20891", test_bpo20891 }, |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 221 | { NULL, NULL } |
| 222 | }; |
| 223 | |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 224 | int main(int argc, char *argv[]) |
| 225 | { |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 226 | if (argc > 1) { |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 227 | for (struct TestCase *tc = TestCases; tc && tc->name; tc++) { |
| 228 | if (strcmp(argv[1], tc->name) == 0) |
| 229 | return (*tc->func)(); |
| 230 | } |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 231 | } |
Steve Dower | ea74f0c | 2017-01-01 20:25:03 -0800 | [diff] [blame] | 232 | |
| 233 | /* No match found, or no test name provided, so display usage */ |
| 234 | printf("Python " PY_VERSION " _testembed executable for embedded interpreter tests\n" |
| 235 | "Normally executed via 'EmbeddingTests' in Lib/test/test_capi.py\n\n" |
| 236 | "Usage: %s TESTNAME\n\nAll available tests:\n", argv[0]); |
| 237 | for (struct TestCase *tc = TestCases; tc && tc->name; tc++) { |
| 238 | printf(" %s\n", tc->name); |
| 239 | } |
| 240 | |
| 241 | /* Non-zero exit code will cause test_capi.py tests to fail. |
| 242 | This is intentional. */ |
| 243 | return -1; |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 244 | } |