Skip to content

Commit

Permalink
Cyber: Implement Python3 wrapper for cyber_timer.
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxq committed Nov 7, 2019
1 parent 7505a58 commit 7a1b7d5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
19 changes: 19 additions & 0 deletions cyber/py_wrapper/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,25 @@ cc_library(
],
)

cc_binary(
name = "_cyber_timer_py3.so",
linkshared = True,
deps = [
":py3_timer",
],
)

cc_library(
name = "py3_timer",
srcs = ["py_timer.cc"],
hdrs = ["py_timer.h"],
defines = ["PY_MAJOR_VERSION=3"],
deps = [
"//cyber:cyber_core",
"@python3",
],
)

cc_binary(
name = "_cyber_parameter.so",
linkshared = True,
Expand Down
24 changes: 22 additions & 2 deletions cyber/py_wrapper/py_timer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
#include <set>
#include <string>

#define PYOBJECT_NULL_STRING PyString_FromStringAndSize("", 0)
#if PY_MAJOR_VERSION >= 3
#define PyInt_AsLong PyLong_AsLong
#endif

template <typename T>
T PyObjectToPtr(PyObject* pyobj, const std::string& type_ptr) {
Expand Down Expand Up @@ -156,11 +158,29 @@ static PyMethodDef _cyber_timer_methods[] = {
{"PyTimer_stop", cyber_PyTimer_stop, METH_VARARGS, ""},
{"PyTimer_set_option", cyber_PyTimer_set_option, METH_VARARGS, ""},

{NULL, NULL, 0, NULL} /* sentinel */
{nullptr, nullptr, 0, nullptr} /* sentinel */
};

/// Init function of this module
#if PY_MAJOR_VERSION >= 3
PyMODINIT_FUNC PyInit__cyber_timer_py3(void) {
static struct PyModuleDef module_def = {
PyModuleDef_HEAD_INIT,
"_cyber_timer_py3", // Module name.
"CyberTimer module", // Module doc.
-1, // Module size.
_cyber_timer_methods, // Module methods.
nullptr,
nullptr,
nullptr,
nullptr,
};

return PyModule_Create(&module_def);
}
#else
PyMODINIT_FUNC init_cyber_timer(void) {
AINFO << "init _cyber_timer";
Py_InitModule("_cyber_timer", _cyber_timer_methods);
}
#endif

0 comments on commit 7a1b7d5

Please sign in to comment.