Skip to content

Commit c56d45e

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

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
@@ -374,7 +374,7 @@ def test_isinstance_string_types():
374374
# pybind11 isinstance
375375
assert m.isinstance_pybind11_bytes(actual_bytes)
376376
assert not m.isinstance_pybind11_bytes(actual_unicode)
377-
assert m.isinstance_pybind11_unicode(actual_bytes) # NOT like native
377+
assert not m.isinstance_pybind11_unicode(actual_bytes)
378378
assert m.isinstance_pybind11_unicode(actual_unicode)
379379

380380

@@ -386,7 +386,8 @@ def test_pass_actual_bytes_or_unicode_to_string_types():
386386
with pytest.raises(TypeError):
387387
m.pass_to_pybind11_bytes(actual_unicode) # NO implicit encode
388388

389-
assert m.pass_to_pybind11_unicode(actual_bytes) == 5 # implicit decode
389+
with pytest.raises(TypeError):
390+
m.pass_to_pybind11_unicode(actual_bytes) # NO implicit decode
390391
assert m.pass_to_pybind11_unicode(actual_unicode) == 3
391392

392393
assert m.pass_to_std_string(actual_bytes) == 5

0 commit comments

Comments
 (0)