Skip to content

Commit c78cc6a

Browse files
committed
adding isinstance<str> specialization to match native behavior, fixing up tests
1 parent d50fcad commit c78cc6a

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

include/pybind11/pytypes.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,8 @@ inline namespace literals {
950950
inline str operator"" _s(const char *s, size_t size) { return {s, size}; }
951951
}
952952

953+
template <> inline bool isinstance<str>(handle obj) { return PyUnicode_Check(obj.ptr()); }
954+
953955
/// \addtogroup pytypes
954956
/// @{
955957
class bytes : public object {
@@ -1012,6 +1014,12 @@ inline str::str(const bytes& b) {
10121014
m_ptr = obj.release().ptr();
10131015
}
10141016

1017+
#if PY_MAJOR_VERSION < 3
1018+
using native_str = bytes;
1019+
#else
1020+
using native_str = str;
1021+
#endif
1022+
10151023
/// \addtogroup pytypes
10161024
/// @{
10171025
class none : public object {

include/pybind11/stl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ template <typename Type, typename Value> struct list_caster {
144144
using value_conv = make_caster<Value>;
145145

146146
bool load(handle src, bool convert) {
147-
if (!isinstance<sequence>(src) || isinstance<str>(src))
147+
if (!isinstance<sequence>(src) || isinstance<str>(src) || isinstance<native_str>(src))
148148
return false;
149149
auto s = reinterpret_borrow<sequence>(src);
150150
value.clear();

tests/test_eval.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ def test_evals(capture):
1818
@pytest.unsupported_on_pypy3
1919
def test_eval_file():
2020
filename = os.path.join(os.path.dirname(__file__), "test_eval_call.py")
21+
if isinstance(filename, bytes): # true for Python 2 only
22+
filename = filename.decode() # effectively six.ensure_text()
2123
assert m.test_eval_file(filename)
2224

2325
assert m.test_eval_file_failure()

tests/test_pytypes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ def test_isinstance_string_types():
367367
# pybind11 isinstance
368368
assert m.isinstance_pybind11_bytes(actual_bytes)
369369
assert not m.isinstance_pybind11_bytes(actual_unicode)
370-
assert m.isinstance_pybind11_unicode(actual_bytes) # NOT like native
370+
assert not m.isinstance_pybind11_unicode(actual_bytes)
371371
assert m.isinstance_pybind11_unicode(actual_unicode)
372372

373373

@@ -379,7 +379,8 @@ def test_pass_actual_bytes_or_unicode_to_string_types():
379379
with pytest.raises(TypeError):
380380
m.pass_to_pybind11_bytes(actual_unicode) # NO implicit encode
381381

382-
assert m.pass_to_pybind11_unicode(actual_bytes) == 5 # implicit decode
382+
with pytest.raises(TypeError):
383+
m.pass_to_pybind11_unicode(actual_bytes) # NO implicit decode
383384
assert m.pass_to_pybind11_unicode(actual_unicode) == 3
384385

385386
assert m.pass_to_std_string(actual_bytes) == 5

0 commit comments

Comments
 (0)