_tangle.cxx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include <Python.h>
  2. #include <stdio.h>
  3. //-----------------------------------------------------------------------------
  4. static PyObject *hello_example(PyObject *self, PyObject *args)
  5. {
  6. // Unpack a string from the arguments
  7. const char *strArg;
  8. if (!PyArg_ParseTuple(args, "s", &strArg))
  9. return NULL;
  10. // Print message and return None
  11. PySys_WriteStdout("Hello, %s!\n", strArg);
  12. Py_RETURN_NONE;
  13. }
  14. //-----------------------------------------------------------------------------
  15. static PyObject *elevation_example(PyObject *self, PyObject *args)
  16. {
  17. // Return an integer
  18. return PyLong_FromLong(21463L);
  19. }
  20. //-----------------------------------------------------------------------------
  21. static PyMethodDef hello_methods[] = {
  22. {"hello",
  23. hello_example,
  24. METH_VARARGS,
  25. "Prints back 'Hello <param>', for example example: hello.hello('you')"},
  26. {"elevation",
  27. elevation_example,
  28. METH_VARARGS,
  29. "Returns elevation of Nevado Sajama."},
  30. {NULL, NULL, 0, NULL} /* Sentinel */
  31. };
  32. //-----------------------------------------------------------------------------
  33. #if PY_MAJOR_VERSION < 3
  34. PyMODINIT_FUNC init_tangle(void)
  35. {
  36. (void)Py_InitModule("_tangle", hello_methods);
  37. }
  38. #else /* PY_MAJOR_VERSION >= 3 */
  39. static struct PyModuleDef tangle_module_def = {
  40. PyModuleDef_HEAD_INIT,
  41. "_tangle",
  42. "Internal \"_tangle\" module",
  43. -1,
  44. hello_methods};
  45. PyMODINIT_FUNC PyInit__tangle(void)
  46. {
  47. return PyModule_Create(&tangle_module_def);
  48. }
  49. #endif /* PY_MAJOR_VERSION >= 3 */