File tree 3 files changed +26
-1
lines changed
3 files changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -943,6 +943,10 @@ class str : public object {
943
943
};
944
944
// / @} pytypes
945
945
946
+ #if PY_MAJOR_VERSION >= 3
947
+ template <> inline bool isinstance<str>(handle obj) { return PyUnicode_Check (obj.ptr ()); }
948
+ #endif
949
+
946
950
inline namespace literals {
947
951
/* * \rst
948
952
String literal version of `str`
Original file line number Diff line number Diff line change @@ -370,4 +370,7 @@ TEST_SUBMODULE(pytypes, m) {
370
370
371
371
m.def (" isinstance_str" , [](py::object o) { return py::isinstance<py::str>(o); });
372
372
m.def (" isinstance_bytes" , [](py::object o) { return py::isinstance<py::bytes>(o); });
373
+
374
+ m.def (" pass_to_std_string" , [](std::string s) { return s.size (); });
375
+ m.def (" pass_to_py_str" , [](py::str s) { return py::len (s); });
373
376
}
Original file line number Diff line number Diff line change @@ -362,5 +362,23 @@ def test_isinstance_string_types():
362
362
else :
363
363
assert not isinstance (actual_bytes , str )
364
364
assert isinstance (actual_unicode , str )
365
- assert m .isinstance_str (actual_bytes ) # REALLY BAD for Python 3?
365
+ assert not m .isinstance_str (actual_bytes )
366
366
assert m .isinstance_str (actual_unicode )
367
+
368
+
369
+ def test_pass_str_or_bytes_to_std_string ():
370
+ actual_bytes = b"bytes"
371
+ actual_unicode = u"str"
372
+ assert m .pass_to_std_string (actual_unicode ) == 3
373
+ assert m .pass_to_std_string (actual_bytes ) == 5
374
+ assert m .pass_to_py_str (actual_unicode ) == 3
375
+ if str is bytes : # Python 2
376
+ m .pass_to_py_str (actual_bytes ) == 5
377
+ else :
378
+ with pytest .raises (TypeError ) as excinfo :
379
+ m .pass_to_py_str (actual_bytes )
380
+ assert str (excinfo .value ) == """\
381
+ pass_to_py_str(): incompatible function arguments. The following argument types are supported:
382
+ 1. (arg0: str) -> int
383
+
384
+ Invoked with: b'bytes'"""
You can’t perform that action at this time.
0 commit comments