Skip to content

Commit 4dcb691

Browse files
committed
Make py::repr() a free function
1 parent 458f7b4 commit 4dcb691

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

include/pybind11/pytypes.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ class object_api : public pyobject_tag {
7070
bool is_none() const { return derived().ptr() == Py_None; }
7171
PYBIND11_DEPRECATED("Instead of obj.str(), use py::str(obj)")
7272
pybind11::str str() const;
73-
pybind11::str repr() const;
7473

7574
int ref_count() const { return static_cast<int>(Py_REFCNT(derived().ptr())); }
7675
handle get_type() const;
@@ -833,6 +832,17 @@ inline size_t len(handle h) {
833832
return (size_t) result;
834833
}
835834

835+
inline str repr(handle h) {
836+
PyObject *str_value = PyObject_Repr(h.ptr());
837+
if (!str_value) throw error_already_set();
838+
#if PY_MAJOR_VERSION < 3
839+
PyObject *unicode = PyUnicode_FromEncodedObject(str_value, "utf-8", nullptr);
840+
Py_XDECREF(str_value); str_value = unicode;
841+
if (!str_value) throw error_already_set();
842+
#endif
843+
return {str_value, false};
844+
}
845+
836846
NAMESPACE_BEGIN(detail)
837847
template <typename D> iterator object_api<D>::begin() const { return {PyObject_GetIter(derived().ptr()), false}; }
838848
template <typename D> iterator object_api<D>::end() const { return {nullptr, false}; }
@@ -848,16 +858,6 @@ template <typename D> template <typename T> bool object_api<D>::contains(T &&key
848858
template <typename D>
849859
pybind11::str object_api<D>::str() const { return pybind11::str(derived()); }
850860

851-
template <typename D>
852-
pybind11::str object_api<D>::repr() const {
853-
PyObject *str_value = PyObject_Repr(derived().ptr());
854-
#if PY_MAJOR_VERSION < 3
855-
PyObject *unicode = PyUnicode_FromEncodedObject(str_value, "utf-8", nullptr);
856-
Py_XDECREF(str_value); str_value = unicode;
857-
#endif
858-
return {str_value, false};
859-
}
860-
861861
template <typename D>
862862
handle object_api<D>::get_type() const { return (PyObject *) Py_TYPE(derived().ptr()); }
863863

tests/test_python_types.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class ExamplePythonTypes {
154154

155155
void test_print(const py::object& obj) {
156156
py::print(py::str(obj));
157-
py::print(obj.repr());
157+
py::print(py::repr(obj));
158158
}
159159

160160
static int value;

0 commit comments

Comments
 (0)