@@ -347,3 +347,41 @@ def test_memoryview_from_memory():
347
347
assert isinstance (view , memoryview )
348
348
assert view .format == 'B'
349
349
assert bytes (view ) == b'\xff \xe1 \xab \x37 '
350
+
351
+
352
+ def test_isinstance_string_types ():
353
+ actual_bytes = b""
354
+ actual_unicode = u""
355
+ if str is bytes :
356
+ # Python 2: NOT same as native str, BUT same as pybind11::str
357
+ native_unicode_type = unicode # noqa: F821
358
+ else :
359
+ # Python 3: same as pybind11::str
360
+ native_unicode_type = str
361
+
362
+ # Native isinstance, for comparison with below.
363
+ assert isinstance (actual_bytes , bytes )
364
+ assert not isinstance (actual_unicode , bytes )
365
+ assert not isinstance (actual_bytes , native_unicode_type )
366
+ assert isinstance (actual_unicode , native_unicode_type )
367
+
368
+ # pybind11 isinstance
369
+ assert m .isinstance_pybind11_bytes (actual_bytes )
370
+ assert not m .isinstance_pybind11_bytes (actual_unicode )
371
+ assert m .isinstance_pybind11_unicode (actual_bytes ) # NOT like native
372
+ assert m .isinstance_pybind11_unicode (actual_unicode )
373
+
374
+
375
+ def test_pass_actual_bytes_or_unicode_to_string_types ():
376
+ actual_bytes = b"Bytes"
377
+ actual_unicode = u"Str"
378
+
379
+ assert m .pass_to_pybind11_bytes (actual_bytes ) == 5
380
+ with pytest .raises (TypeError ):
381
+ m .pass_to_pybind11_bytes (actual_unicode ) # NO implicit encode
382
+
383
+ assert m .pass_to_pybind11_unicode (actual_bytes ) == 5 # implicit decode
384
+ assert m .pass_to_pybind11_unicode (actual_unicode ) == 3
385
+
386
+ assert m .pass_to_std_string (actual_bytes ) == 5
387
+ assert m .pass_to_std_string (actual_unicode ) == 3
0 commit comments