Skip to content

Commit 5e76fc6

Browse files
committed
pybind11::str::raw_str simplification (for Python 2)
For Python 2, convert directly to unicode (instead of converting to str first, followed by encoding). Simpler code, and more obviously equivalent to `unicode(...)` in the interpreter.
1 parent 3e448c0 commit 5e76fc6

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

include/pybind11/pytypes.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -930,11 +930,10 @@ class str : public object {
930930
private:
931931
/// Return string representation -- always returns a new reference, even if already a str
932932
static PyObject *raw_str(PyObject *op) {
933-
PyObject *str_value = PyObject_Str(op);
934-
if (!str_value) throw error_already_set();
935933
#if PY_MAJOR_VERSION < 3
936-
PyObject *unicode = PyUnicode_FromEncodedObject(str_value, "utf-8", nullptr);
937-
Py_XDECREF(str_value); str_value = unicode;
934+
PyObject *str_value = PyObject_Unicode(op);
935+
#else
936+
PyObject *str_value = PyObject_Str(op);
938937
#endif
939938
return str_value;
940939
}

0 commit comments

Comments
 (0)