module.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package api
  2. // Here we add our "py_" functions to the module
  3. // Note that this file could potentially be combined with "py_.go", but we preferred separation
  4. // For that reason we must forward-declare the "py_" functions in the cgo preamble
  5. import (
  6. "github.com/tliron/py4go"
  7. )
  8. /*
  9. #cgo pkg-config: python3-embed
  10. #define PY_SSIZE_T_CLEAN
  11. #include <Python.h>
  12. PyObject *py_api_sayGoodbye(PyObject *self, PyObject *unused);
  13. PyObject *py_api_concat(PyObject *self, PyObject *args);
  14. PyObject *py_api_concat_fast(PyObject *self, PyObject **args, Py_ssize_t nargs);
  15. */
  16. import "C"
  17. func CreateModule() (*python.Reference, error) {
  18. if module, err := python.CreateModule("api"); err == nil {
  19. if err := module.AddModuleCFunctionNoArgs("say_goodbye", C.py_api_sayGoodbye); err != nil {
  20. module.Release()
  21. return nil, err
  22. }
  23. if err := module.AddModuleCFunctionArgs("concat", C.py_api_concat); err != nil {
  24. module.Release()
  25. return nil, err
  26. }
  27. if err := module.AddModuleCFunctionFastArgs("concat_fast", C.py_api_concat_fast); err != nil {
  28. module.Release()
  29. return nil, err
  30. }
  31. return module, nil
  32. } else {
  33. return nil, err
  34. }
  35. }