@@ -104,11 +104,28 @@ def __repr__(self):
104
104
105
105
assert m .str_from_object (A ()) == "this is a str"
106
106
assert m .repr_from_object (A ()) == "this is a repr"
107
+ assert m .str_from_handle (A ()) == "this is a str"
107
108
108
109
s1 , s2 = m .str_format ()
109
110
assert s1 == "1 + 2 = 3"
110
111
assert s1 == s2
111
112
113
+ malformed_utf8 = b"\x80 "
114
+ if env .PY2 :
115
+ if hasattr (m , "has_str_non_permissive" ):
116
+ with pytest .raises (UnicodeDecodeError ):
117
+ m .str_from_object (malformed_utf8 )
118
+ else :
119
+ m .str_from_object (malformed_utf8 ) is malformed_utf8
120
+ with pytest .raises (UnicodeDecodeError ):
121
+ m .str_from_handle (malformed_utf8 )
122
+ else :
123
+ if hasattr (m , "has_str_non_permissive" ):
124
+ assert m .str_from_object (malformed_utf8 ) == "b'\\ x80'"
125
+ else :
126
+ assert m .str_from_object (malformed_utf8 ) is malformed_utf8
127
+ assert m .str_from_handle (malformed_utf8 ) == "b'\\ x80'"
128
+
112
129
113
130
def test_bytes (doc ):
114
131
assert m .bytes_from_string ().decode () == "foo"
@@ -256,13 +273,26 @@ def test_pybind11_str_raw_str():
256
273
valid_orig = u"DZ"
257
274
valid_utf8 = valid_orig .encode ("utf-8" )
258
275
valid_cvt = cvt (valid_utf8 )
259
- assert type (valid_cvt ) == bytes # Probably surprising.
260
- assert valid_cvt == b'\xc7 \xb1 '
276
+ if hasattr (m , "has_str_non_permissive" ):
277
+ assert type (valid_cvt ) is unicode if env .PY2 else str # noqa: F821
278
+ if env .PY2 :
279
+ assert valid_cvt == valid_orig
280
+ else :
281
+ assert valid_cvt == u"b'\\ xc7\\ xb1'"
282
+ else :
283
+ assert valid_cvt is valid_utf8
261
284
262
285
malformed_utf8 = b'\x80 '
263
- malformed_cvt = cvt (malformed_utf8 )
264
- assert type (malformed_cvt ) == bytes # Probably surprising.
265
- assert malformed_cvt == b'\x80 '
286
+ if hasattr (m , "has_str_non_permissive" ):
287
+ if env .PY2 :
288
+ with pytest .raises (UnicodeDecodeError ):
289
+ cvt (malformed_utf8 )
290
+ else :
291
+ malformed_cvt = cvt (malformed_utf8 )
292
+ assert type (malformed_cvt ) is unicode if env .PY2 else str # noqa: F821
293
+ assert malformed_cvt == u"b'\\ x80'"
294
+ else :
295
+ assert cvt (malformed_utf8 ) is malformed_utf8
266
296
267
297
268
298
def test_implicit_casting ():
@@ -398,3 +428,41 @@ def test_memoryview_from_memory():
398
428
assert isinstance (view , memoryview )
399
429
assert view .format == 'B'
400
430
assert bytes (view ) == b'\xff \xe1 \xab \x37 '
431
+
432
+
433
+ def test_isinstance_string_types ():
434
+ assert m .isinstance_pybind11_bytes (b"" )
435
+ assert not m .isinstance_pybind11_bytes (u"" )
436
+
437
+ assert m .isinstance_pybind11_str (u"" )
438
+ if hasattr (m , "has_str_non_permissive" ):
439
+ assert not m .isinstance_pybind11_str (b"" )
440
+ else :
441
+ assert m .isinstance_pybind11_str (b"" )
442
+
443
+
444
+ def test_pass_bytes_or_unicode_to_string_types ():
445
+ assert m .pass_to_pybind11_bytes (b"Bytes" ) == 5
446
+ with pytest .raises (TypeError ):
447
+ m .pass_to_pybind11_bytes (u"Str" )
448
+
449
+ if hasattr (m , "has_str_caster_no_implicit_decode" ):
450
+ with pytest .raises (TypeError ):
451
+ m .pass_to_pybind11_str (b"Bytes" )
452
+ else :
453
+ assert m .pass_to_pybind11_str (b"Bytes" ) == 5
454
+ assert m .pass_to_pybind11_str (u"Str" ) == 3
455
+
456
+ assert m .pass_to_std_string (b"Bytes" ) == 5
457
+ assert m .pass_to_std_string (u"Str" ) == 3
458
+
459
+ malformed_utf8 = b"\x80 "
460
+ if hasattr (m , "has_str_non_permissive" ):
461
+ if hasattr (m , "has_str_caster_no_implicit_decode" ):
462
+ with pytest .raises (TypeError ):
463
+ m .pass_to_pybind11_str (malformed_utf8 )
464
+ else :
465
+ with pytest .raises (UnicodeDecodeError ):
466
+ m .pass_to_pybind11_str (malformed_utf8 )
467
+ else :
468
+ assert m .pass_to_pybind11_str (malformed_utf8 ) == 1
0 commit comments