Skip to content

Commit

Permalink
Backing out changes originating from pybind#2392.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Sep 10, 2020
1 parent c7ba900 commit 6f270a3
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 20 deletions.
4 changes: 2 additions & 2 deletions include/pybind11/pytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ class str : public object {
Return a string representation of the object. This is analogous to
the ``str()`` function in Python.
\endrst */
explicit str(handle h) : object(raw_str(h.ptr()), stolen_t{}) { if (!m_ptr) throw error_already_set(); }
explicit str(handle h) : object(raw_str(h.ptr()), stolen_t{}) { }

operator std::string() const {
object temp = *this;
Expand All @@ -950,8 +950,8 @@ class str : public object {
/// Return string representation -- always returns a new reference, even if already a str
static PyObject *raw_str(PyObject *op) {
PyObject *str_value = PyObject_Str(op);
#if PY_MAJOR_VERSION < 3
if (!str_value) throw error_already_set();
#if PY_MAJOR_VERSION < 3
PyObject *unicode = PyUnicode_FromEncodedObject(str_value, "utf-8", nullptr);
Py_XDECREF(str_value); str_value = unicode;
#endif
Expand Down
1 change: 0 additions & 1 deletion tests/test_pytypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ TEST_SUBMODULE(pytypes, m) {
m.def("str_from_bytes", []() { return py::str(py::bytes("boo", 3)); });
m.def("str_from_object", [](const py::object& obj) { return py::str(obj); });
m.def("repr_from_object", [](const py::object& obj) { return py::repr(obj); });
m.def("str_from_handle", [](py::handle h) { return py::str(h); });

m.def("str_format", []() {
auto s1 = "{} + {} = {}"_s.format(1, 2, 3);
Expand Down
17 changes: 0 additions & 17 deletions tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,28 +104,11 @@ def __repr__(self):

assert m.str_from_object(A()) == "this is a str"
assert m.repr_from_object(A()) == "this is a repr"
assert m.str_from_handle(A()) == "this is a str"

s1, s2 = m.str_format()
assert s1 == "1 + 2 = 3"
assert s1 == s2

malformed_utf8 = b"\x80"
if env.PY2:
if hasattr(m, "has_str_non_permissive"):
with pytest.raises(UnicodeDecodeError):
m.str_from_object(malformed_utf8)
else:
m.str_from_object(malformed_utf8) is malformed_utf8
with pytest.raises(UnicodeDecodeError):
m.str_from_handle(malformed_utf8)
else:
if hasattr(m, "has_str_non_permissive"):
assert m.str_from_object(malformed_utf8) == "b'\\x80'"
else:
assert m.str_from_object(malformed_utf8) is malformed_utf8
assert m.str_from_handle(malformed_utf8) == "b'\\x80'"


def test_bytes(doc):
assert m.bytes_from_string().decode() == "foo"
Expand Down

0 comments on commit 6f270a3

Please sign in to comment.