@@ -346,3 +346,41 @@ 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
+ actual_unicode = u""
354
+ if str is bytes :
355
+ # Python 2: NOT same as native str, BUT same as pybind11::str
356
+ native_unicode_type = unicode # noqa: F821
357
+ else :
358
+ # Python 3: same as pybind11::str
359
+ native_unicode_type = str
360
+
361
+ # Native isinstance, for comparison with below.
362
+ assert isinstance (actual_bytes , bytes )
363
+ assert not isinstance (actual_unicode , bytes )
364
+ assert not isinstance (actual_bytes , native_unicode_type )
365
+ assert isinstance (actual_unicode , native_unicode_type )
366
+
367
+ # pybind11 isinstance
368
+ assert m .isinstance_pybind11_bytes (actual_bytes )
369
+ assert not m .isinstance_pybind11_bytes (actual_unicode )
370
+ assert m .isinstance_pybind11_unicode (actual_bytes ) # NOT like native
371
+ assert m .isinstance_pybind11_unicode (actual_unicode )
372
+
373
+
374
+ def test_pass_actual_bytes_or_unicode_to_string_types ():
375
+ actual_bytes = b"Bytes"
376
+ actual_unicode = u"Str"
377
+
378
+ assert m .pass_to_pybind11_bytes (actual_bytes ) == 5
379
+ with pytest .raises (TypeError ):
380
+ m .pass_to_pybind11_bytes (actual_unicode ) # NO implicit encode
381
+
382
+ assert m .pass_to_pybind11_unicode (actual_bytes ) == 5 # implicit decode
383
+ assert m .pass_to_pybind11_unicode (actual_unicode ) == 3
384
+
385
+ assert m .pass_to_std_string (actual_bytes ) == 5
386
+ assert m .pass_to_std_string (actual_unicode ) == 3
0 commit comments