/quasiquotes/c/_loader.c

https://github.com/llllllllll/quasiquotes · C · 55 lines · 49 code · 6 blank · 0 comment · 3 complexity · 1ea9406f43add32788c65334c8369617 MD5 · raw file

  1. #include <Python.h>
  2. #include <dlfcn.h>
  3. static PyObject *
  4. create_callable(PyObject *self, PyObject *args, PyObject *kwargs)
  5. {
  6. char* keywords[] = {"filename", NULL};
  7. char *filename;
  8. void *sohandle;
  9. PyMethodDef *qq_methoddef;
  10. if (!(PyArg_ParseTupleAndKeywords(args,
  11. kwargs,
  12. "s:create_callable",
  13. keywords,
  14. &filename))) {
  15. return NULL;
  16. }
  17. if (!(sohandle = dlopen(filename, RTLD_LAZY))) {
  18. PyErr_SetString(PyExc_OSError, dlerror());
  19. return NULL;
  20. }
  21. if (!(qq_methoddef = dlsym(sohandle, "__qq_methoddef"))) {
  22. PyErr_SetString(PyExc_OSError, dlerror());
  23. return NULL;
  24. }
  25. return PyCFunction_NewEx(qq_methoddef, NULL, NULL);
  26. }
  27. static PyMethodDef methods[] = {
  28. {"create_callable",
  29. (PyCFunction) create_callable,
  30. METH_VARARGS | METH_KEYWORDS,
  31. ""},
  32. {NULL},
  33. };
  34. static struct PyModuleDef module = {
  35. PyModuleDef_HEAD_INIT,
  36. "quasiquotes.c._loader",
  37. "",
  38. -1,
  39. methods,
  40. NULL,
  41. NULL,
  42. NULL,
  43. NULL
  44. };
  45. PyMODINIT_FUNC
  46. PyInit__loader(void)
  47. {
  48. return PyModule_Create(&module);
  49. }