@@ -120,14 +120,23 @@ def __repr__(self):
120
120
assert s1 == s2
121
121
122
122
malformed_utf8 = b"\x80 "
123
- assert m .str_from_object (malformed_utf8 ) is malformed_utf8 # To be fixed; see #2380
124
123
if env .PY2 :
125
- # with pytest.raises(UnicodeDecodeError):
126
- # m.str_from_object(malformed_utf8)
124
+ if hasattr (m , "has_str_non_permissive" ):
125
+ with pytest .raises (UnicodeDecodeError ):
126
+ m .str_from_object (malformed_utf8 )
127
+ else :
128
+ m .str_from_object (
129
+ malformed_utf8
130
+ ) is malformed_utf8 # To be fixed; see #2380
127
131
with pytest .raises (UnicodeDecodeError ):
128
132
m .str_from_handle (malformed_utf8 )
129
133
else :
130
- # assert m.str_from_object(malformed_utf8) == "b'\\x80'"
134
+ if hasattr (m , "has_str_non_permissive" ):
135
+ assert m .str_from_object (malformed_utf8 ) == "b'\\ x80'"
136
+ else :
137
+ assert (
138
+ m .str_from_object (malformed_utf8 ) is malformed_utf8
139
+ ) # To be fixed; see #2380
131
140
assert m .str_from_handle (malformed_utf8 ) == "b'\\ x80'"
132
141
133
142
@@ -301,13 +310,26 @@ def test_pybind11_str_raw_str():
301
310
valid_orig = u"DZ"
302
311
valid_utf8 = valid_orig .encode ("utf-8" )
303
312
valid_cvt = cvt (valid_utf8 )
304
- assert type (valid_cvt ) == bytes # Probably surprising.
305
- assert valid_cvt == b"\xc7 \xb1 "
313
+ if hasattr (m , "has_str_non_permissive" ):
314
+ assert type (valid_cvt ) is unicode if env .PY2 else str # noqa: F821
315
+ if env .PY2 :
316
+ assert valid_cvt == valid_orig
317
+ else :
318
+ assert valid_cvt == u"b'\\ xc7\\ xb1'"
319
+ else :
320
+ assert valid_cvt is valid_utf8
306
321
307
322
malformed_utf8 = b"\x80 "
308
- malformed_cvt = cvt (malformed_utf8 )
309
- assert type (malformed_cvt ) == bytes # Probably surprising.
310
- assert malformed_cvt == b"\x80 "
323
+ if hasattr (m , "has_str_non_permissive" ):
324
+ if env .PY2 :
325
+ with pytest .raises (UnicodeDecodeError ):
326
+ cvt (malformed_utf8 )
327
+ else :
328
+ malformed_cvt = cvt (malformed_utf8 )
329
+ assert type (malformed_cvt ) is unicode if env .PY2 else str # noqa: F821
330
+ assert malformed_cvt == u"b'\\ x80'"
331
+ else :
332
+ assert cvt (malformed_utf8 ) is malformed_utf8
311
333
312
334
313
335
def test_implicit_casting ():
@@ -486,3 +508,41 @@ def test_builtin_functions():
486
508
"object of type 'generator' has no len()" ,
487
509
"'generator' has no length" ,
488
510
] # PyPy
511
+
512
+
513
+ def test_isinstance_string_types ():
514
+ assert m .isinstance_pybind11_bytes (b"" )
515
+ assert not m .isinstance_pybind11_bytes (u"" )
516
+
517
+ assert m .isinstance_pybind11_str (u"" )
518
+ if hasattr (m , "has_str_non_permissive" ):
519
+ assert not m .isinstance_pybind11_str (b"" )
520
+ else :
521
+ assert m .isinstance_pybind11_str (b"" )
522
+
523
+
524
+ def test_pass_bytes_or_unicode_to_string_types ():
525
+ assert m .pass_to_pybind11_bytes (b"Bytes" ) == 5
526
+ with pytest .raises (TypeError ):
527
+ m .pass_to_pybind11_bytes (u"Str" )
528
+
529
+ if hasattr (m , "has_str_caster_no_implicit_decode" ):
530
+ with pytest .raises (TypeError ):
531
+ m .pass_to_pybind11_str (b"Bytes" )
532
+ else :
533
+ assert m .pass_to_pybind11_str (b"Bytes" ) == 5
534
+ assert m .pass_to_pybind11_str (u"Str" ) == 3
535
+
536
+ assert m .pass_to_std_string (b"Bytes" ) == 5
537
+ assert m .pass_to_std_string (u"Str" ) == 3
538
+
539
+ malformed_utf8 = b"\x80 "
540
+ if hasattr (m , "has_str_non_permissive" ):
541
+ if hasattr (m , "has_str_caster_no_implicit_decode" ):
542
+ with pytest .raises (TypeError ):
543
+ m .pass_to_pybind11_str (malformed_utf8 )
544
+ else :
545
+ with pytest .raises (UnicodeDecodeError ):
546
+ m .pass_to_pybind11_str (malformed_utf8 )
547
+ else :
548
+ assert m .pass_to_pybind11_str (malformed_utf8 ) == 1
0 commit comments