Skip to content

Commit

Permalink
adding isinstance<str> specialization to match native behavior, fixin…
Browse files Browse the repository at this point in the history
…g up tests
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Jul 22, 2020
1 parent 1aec645 commit e20b54d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
8 changes: 8 additions & 0 deletions include/pybind11/pytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,8 @@ inline namespace literals {
inline str operator"" _s(const char *s, size_t size) { return {s, size}; }
}

template <> inline bool isinstance<str>(handle obj) { return PyUnicode_Check(obj.ptr()); }

/// \addtogroup pytypes
/// @{
class bytes : public object {
Expand Down Expand Up @@ -1012,6 +1014,12 @@ inline str::str(const bytes& b) {
m_ptr = obj.release().ptr();
}

#if PY_MAJOR_VERSION < 3
using native_str = bytes;
#else
using native_str = str;
#endif

/// \addtogroup pytypes
/// @{
class none : public object {
Expand Down
2 changes: 1 addition & 1 deletion include/pybind11/stl.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ template <typename Type, typename Value> struct list_caster {
using value_conv = make_caster<Value>;

bool load(handle src, bool convert) {
if (!isinstance<sequence>(src) || isinstance<str>(src))
if (!isinstance<sequence>(src) || isinstance<str>(src) || isinstance<native_str>(src))
return false;
auto s = reinterpret_borrow<sequence>(src);
value.clear();
Expand Down
2 changes: 2 additions & 0 deletions tests/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def test_evals(capture):
@pytest.unsupported_on_pypy3
def test_eval_file():
filename = os.path.join(os.path.dirname(__file__), "test_eval_call.py")
if isinstance(filename, bytes): # true for Python 2 only
filename = filename.decode() # effectively six.ensure_text()
assert m.test_eval_file(filename)

assert m.test_eval_file_failure()
5 changes: 3 additions & 2 deletions tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ def test_isinstance_string_types():
# pybind11 isinstance
assert m.isinstance_pybind11_bytes(actual_bytes)
assert not m.isinstance_pybind11_bytes(actual_unicode)
assert m.isinstance_pybind11_unicode(actual_bytes) # NOT like native
assert not m.isinstance_pybind11_unicode(actual_bytes)
assert m.isinstance_pybind11_unicode(actual_unicode)


Expand All @@ -379,7 +379,8 @@ def test_pass_actual_bytes_or_unicode_to_string_types():
with pytest.raises(TypeError) as excinfo:
m.pass_to_pybind11_bytes(actual_unicode) # NO implicit encode

assert m.pass_to_pybind11_unicode(actual_bytes) == 5 # implicit decode
with pytest.raises(TypeError) as excinfo:
m.pass_to_pybind11_unicode(actual_bytes) # NO implicit decode
assert m.pass_to_pybind11_unicode(actual_unicode) == 3

assert m.pass_to_std_string(actual_bytes) == 5
Expand Down

0 comments on commit e20b54d

Please sign in to comment.