File tree Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -367,4 +367,7 @@ TEST_SUBMODULE(pytypes, m) {
367
367
buf, static_cast <ssize_t >(strlen (buf)));
368
368
});
369
369
#endif
370
+
371
+ m.def (" isinstance_str" , [](py::object o) { return py::isinstance<py::str>(o); });
372
+ m.def (" isinstance_bytes" , [](py::object o) { return py::isinstance<py::bytes>(o); });
370
373
}
Original file line number Diff line number Diff line change @@ -346,3 +346,21 @@ def test_memoryview_from_memory():
346
346
assert isinstance (view , memoryview )
347
347
assert view .format == 'B'
348
348
assert bytes (view ) == b'\xff \xe1 \xab \x37 '
349
+
350
+
351
+ def test_isinstance_string_types ():
352
+ actual_bytes = b""
353
+ # Requires Python 2 or >= 3.3 (https://www.python.org/dev/peps/pep-0414/):
354
+ actual_unicode = u""
355
+ assert m .isinstance_bytes (actual_bytes )
356
+ assert not m .isinstance_bytes (actual_unicode )
357
+ if str is bytes : # Python 2
358
+ assert isinstance (actual_bytes , str )
359
+ assert not isinstance (actual_unicode , str )
360
+ assert m .isinstance_str (actual_bytes ) # BAD? (but matches native isinstance for Python 2)
361
+ assert m .isinstance_str (actual_unicode ) # DESIRED for Python 2? (does not match native isinstance for Python 2)
362
+ else :
363
+ assert not isinstance (actual_bytes , str )
364
+ assert isinstance (actual_unicode , str )
365
+ assert m .isinstance_str (actual_bytes ) # REALLY BAD for Python 3?
366
+ assert m .isinstance_str (actual_unicode )
You can’t perform that action at this time.
0 commit comments