eval.c 651 B

12345678910111213141516171819202122232425
  1. /* https://cirosantilli.com/linux-kernel-module-cheat#embedding-python-in-another-application
  2. *
  3. * Adapted from: https://docs.python.org/3.7/extending/embedding.html#very-high-level-embedding
  4. */
  5. #define PY_SSIZE_T_CLEAN
  6. #include <Python.h>
  7. int main(int argc, char *argv[]) {
  8. (void)argc;
  9. wchar_t *program = Py_DecodeLocale(argv[0], NULL);
  10. if (program == NULL) {
  11. fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
  12. exit(1);
  13. }
  14. Py_SetProgramName(program);
  15. Py_Initialize();
  16. PyRun_SimpleString(argv[1]);
  17. if (Py_FinalizeEx() < 0) {
  18. exit(120);
  19. }
  20. PyMem_RawFree(program);
  21. return 0;
  22. }