@@ -120,23 +120,17 @@ def __repr__(self):
120120 assert s1 == s2
121121
122122 malformed_utf8 = b"\x80 "
123+ if hasattr (m , "PYBIND11_STR_LEGACY_PERMISSIVE" ):
124+ assert m .str_from_object (malformed_utf8 ) is malformed_utf8
125+ elif env .PY2 :
126+ with pytest .raises (UnicodeDecodeError ):
127+ m .str_from_object (malformed_utf8 )
128+ else :
129+ assert m .str_from_object (malformed_utf8 ) == "b'\\ x80'"
123130 if env .PY2 :
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
131131 with pytest .raises (UnicodeDecodeError ):
132132 m .str_from_handle (malformed_utf8 )
133133 else :
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
140134 assert m .str_from_handle (malformed_utf8 ) == "b'\\ x80'"
141135
142136
@@ -310,26 +304,26 @@ def test_pybind11_str_raw_str():
310304 valid_orig = u"DZ"
311305 valid_utf8 = valid_orig .encode ("utf-8" )
312306 valid_cvt = cvt (valid_utf8 )
313- if hasattr (m , "has_str_non_permissive" ):
307+ if hasattr (m , "PYBIND11_STR_LEGACY_PERMISSIVE" ):
308+ assert valid_cvt is valid_utf8
309+ else :
314310 assert type (valid_cvt ) is unicode if env .PY2 else str # noqa: F821
315311 if env .PY2 :
316312 assert valid_cvt == valid_orig
317313 else :
318- assert valid_cvt == u"b'\\ xc7\\ xb1'"
319- else :
320- assert valid_cvt is valid_utf8
314+ assert valid_cvt == "b'\\ xc7\\ xb1'"
321315
322316 malformed_utf8 = b"\x80 "
323- if hasattr (m , "has_str_non_permissive" ):
317+ if hasattr (m , "PYBIND11_STR_LEGACY_PERMISSIVE" ):
318+ assert cvt (malformed_utf8 ) is malformed_utf8
319+ else :
324320 if env .PY2 :
325321 with pytest .raises (UnicodeDecodeError ):
326322 cvt (malformed_utf8 )
327323 else :
328324 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
325+ assert type (malformed_cvt ) is str
326+ assert malformed_cvt == "b'\\ x80'"
333327
334328
335329def test_implicit_casting ():
@@ -515,34 +509,33 @@ def test_isinstance_string_types():
515509 assert not m .isinstance_pybind11_bytes (u"" )
516510
517511 assert m .isinstance_pybind11_str (u"" )
518- if hasattr (m , "has_str_non_permissive" ):
519- assert not m .isinstance_pybind11_str (b"" )
520- else :
512+ if hasattr (m , "PYBIND11_STR_LEGACY_PERMISSIVE" ):
521513 assert m .isinstance_pybind11_str (b"" )
514+ else :
515+ assert not m .isinstance_pybind11_str (b"" )
522516
523517
524518def test_pass_bytes_or_unicode_to_string_types ():
525519 assert m .pass_to_pybind11_bytes (b"Bytes" ) == 5
526520 with pytest .raises (TypeError ):
527521 m .pass_to_pybind11_bytes (u"Str" )
528522
529- if hasattr (m , "has_str_caster_no_implicit_decode" ):
523+ if hasattr (m , "PYBIND11_STR_LEGACY_PERMISSIVE" ) or env .PY2 :
524+ assert m .pass_to_pybind11_str (b"Bytes" ) == 5
525+ else :
530526 with pytest .raises (TypeError ):
531527 m .pass_to_pybind11_str (b"Bytes" )
532- else :
533- assert m .pass_to_pybind11_str (b"Bytes" ) == 5
534528 assert m .pass_to_pybind11_str (u"Str" ) == 3
535529
536530 assert m .pass_to_std_string (b"Bytes" ) == 5
537531 assert m .pass_to_std_string (u"Str" ) == 3
538532
539533 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 :
534+ if hasattr (m , "PYBIND11_STR_LEGACY_PERMISSIVE" ):
548535 assert m .pass_to_pybind11_str (malformed_utf8 ) == 1
536+ elif env .PY2 :
537+ with pytest .raises (UnicodeDecodeError ):
538+ m .pass_to_pybind11_str (malformed_utf8 )
539+ else :
540+ with pytest .raises (TypeError ):
541+ m .pass_to_pybind11_str (malformed_utf8 )
0 commit comments