|
| 1 | +/* Smoke test for libpython linked into a program whose main() is not Python's. */ |
| 2 | + |
| 3 | +#include <Python.h> |
| 4 | +#include <stdio.h> |
| 5 | + |
| 6 | +// Imports come from the preloaded zip, len() goes through the call trampoline |
| 7 | +// and poll() through the syscall overrides. |
| 8 | +static const char *SCRIPT = |
| 9 | + "import json, select\n" |
| 10 | + "select.poll().poll(0)\n" |
| 11 | + "print(json.dumps({'embedded': len('ok')}))\n"; |
| 12 | + |
| 13 | +int main(void) |
| 14 | +{ |
| 15 | + PyStatus status; |
| 16 | + PyConfig config; |
| 17 | + |
| 18 | + PyConfig_InitIsolatedConfig(&config); |
| 19 | + config.write_bytecode = 0; |
| 20 | + config.module_search_paths_set = 1; |
| 21 | + status = PyWideStringList_Append(&config.module_search_paths, |
| 22 | + L"/lib/stdlib.zip"); |
| 23 | + if (PyStatus_Exception(status)) { |
| 24 | + goto exception; |
| 25 | + } |
| 26 | + status = PyConfig_SetBytesString(&config, &config.executable, "/embed"); |
| 27 | + if (PyStatus_Exception(status)) { |
| 28 | + goto exception; |
| 29 | + } |
| 30 | + status = Py_InitializeFromConfig(&config); |
| 31 | + if (PyStatus_Exception(status)) { |
| 32 | + goto exception; |
| 33 | + } |
| 34 | + PyConfig_Clear(&config); |
| 35 | + |
| 36 | + if (PyRun_SimpleString(SCRIPT) != 0) { |
| 37 | + puts("web_embed_test: script failed"); |
| 38 | + return 1; |
| 39 | + } |
| 40 | + if (Py_FinalizeEx() < 0) { |
| 41 | + puts("web_embed_test: Py_FinalizeEx failed"); |
| 42 | + return 1; |
| 43 | + } |
| 44 | + puts("web_embed_test: ok"); |
| 45 | + return 0; |
| 46 | + |
| 47 | +exception: |
| 48 | + PyConfig_Clear(&config); |
| 49 | + Py_ExitStatusException(status); |
| 50 | +} |
0 commit comments