Skip to content

Commit 1bc3300

Browse files
authored
Remove python_compat.h (#417)
We don't need compatibility with older Python, so we can simplify the code and just move it into the C++ file where it is used. Signed-off-by: Chris Lalancette <[email protected]>
1 parent 27e73b3 commit 1bc3300

File tree

3 files changed

+47
-70
lines changed

3 files changed

+47
-70
lines changed

tf2_py/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ endif()
3636

3737
ament_python_install_package(${PROJECT_NAME})
3838

39-
include_directories(include)
40-
4139
function(set_properties _targetname _build_type)
4240
set_target_properties(${_targetname} PROPERTIES
4341
PREFIX ""

tf2_py/include/python_compat.h

Lines changed: 0 additions & 67 deletions
This file was deleted.

tf2_py/src/tf2_py.cpp

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include <python_compat.h>
21
#include <Python.h>
32

43
#include <tf2/buffer_core.h>
@@ -7,6 +6,53 @@
76
#include <string>
87
#include <vector>
98

9+
/// \brief Converts a C++ string into a Python string.
10+
/// \note The caller is responsible for decref'ing the returned object.
11+
/// \note If the return value is NULL then an exception is set.
12+
/// \return a new PyObject reference, or NULL
13+
inline PyObject * stringToPython(const std::string & input)
14+
{
15+
return PyUnicode_FromStringAndSize(input.c_str(), input.size());
16+
}
17+
18+
/// \brief Converts a C string into a Python string.
19+
/// \note The caller is responsible for decref'ing the returned object.
20+
/// \note If the return value is NULL then an exception is set.
21+
/// \return a new PyObject reference, or NULL
22+
inline PyObject * stringToPython(const char * input)
23+
{
24+
return PyUnicode_FromString(input);
25+
}
26+
27+
/// \brief Converts a Python string into a C++ string.
28+
/// \note The input PyObject is borrowed, and will not be decref'd.
29+
/// \note It's possible for this function to set an exception.
30+
/// If the returned string is empty, callers should check if an exception was
31+
/// set using PyErr_Ocurred().
32+
/// \return a new std::string instance
33+
inline std::string stringFromPython(PyObject * input)
34+
{
35+
Py_ssize_t size;
36+
const char * data;
37+
data = PyUnicode_AsUTF8AndSize(input, &size);
38+
return std::string(data, size);
39+
}
40+
41+
/// \brief Imports a python module by name.
42+
/// \note The caller is responsible for decref'ing the returned object.
43+
/// \note If the return value is NULL then an exception is set.
44+
/// \return a reference to the imported module.
45+
inline PyObject * pythonImport(const std::string & name)
46+
{
47+
PyObject * py_name = stringToPython(name);
48+
if (!py_name) {
49+
return nullptr;
50+
}
51+
PyObject * module = PyImport_Import(py_name);
52+
Py_XDECREF(py_name);
53+
return module;
54+
}
55+
1056
// Run x (a tf method, catching TF's exceptions and reraising them as Python exceptions)
1157
//
1258
#define WRAP(x) \

0 commit comments

Comments
 (0)