stub.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include <Python.h>
  2. #include <dlfcn.h>
  3. #include "emacs-module.h"
  4. int emacs_module_init_py(void *runtime);
  5. void PyInit_emacspy(void);
  6. void init_interpreter_list(void);
  7. int emacs_module_init(struct emacs_runtime *runtime) {
  8. // https://www.gnu.org/software/emacs/manual/html_node/elisp/Module-Initialization.html#index-emacs_005fmodule_005finit-1
  9. if ((long unsigned int)runtime->size < sizeof(*runtime)) {
  10. fprintf(stderr, "%s\n",
  11. "ERROR: emacs_module_init: emacspy was compiled for newer version of Emacs.");
  12. return 1;
  13. }
  14. if (Py_IsInitialized()) {
  15. fprintf(stderr, "%s\n",
  16. "ERROR: emacs_module_init: emacspy is already loaded.");
  17. return 2;
  18. }
  19. dlopen(LIBPYTHON_NAME, RTLD_LAZY | RTLD_GLOBAL);
  20. PyConfig config;
  21. PyStatus status;
  22. PyConfig_InitPythonConfig(&config);
  23. status = PyConfig_SetString(&config, &config.home, BASE_PREFIX);
  24. if (PyStatus_Exception(status)) {
  25. return 3;
  26. }
  27. status = Py_InitializeFromConfig(&config);
  28. if (PyStatus_Exception(status)) {
  29. return 4;
  30. }
  31. init_interpreter_list();
  32. PyInit_emacspy();
  33. int result = emacs_module_init_py(runtime);
  34. PyEval_SaveThread();
  35. return result;
  36. }