6 from pybind11_tests
import builtin_casters
as m
7 from pybind11_tests
import UserType, IncType
11 assert m.string_roundtrip(
"const char *") ==
"const char *"
15 """Tests unicode conversion and error reporting."""
16 assert m.good_utf8_string() ==
u"Say utf8β½ π π"
17 assert m.good_utf16_string() ==
u"bβ½ππz"
18 assert m.good_utf32_string() ==
u"aππβ½z"
19 assert m.good_wchar_string() ==
u"aβΈπz"
21 assert m.good_utf8_u8string() ==
u"Say utf8β½ π π"
23 with pytest.raises(UnicodeDecodeError):
26 with pytest.raises(UnicodeDecodeError):
30 if hasattr(m,
"bad_utf32_string"):
31 with pytest.raises(UnicodeDecodeError):
33 if hasattr(m,
"bad_wchar_string"):
34 with pytest.raises(UnicodeDecodeError):
37 with pytest.raises(UnicodeDecodeError):
40 assert m.u8_Z() ==
"Z"
41 assert m.u8_eacute() ==
u"Γ©"
42 assert m.u16_ibang() ==
u"β½"
43 assert m.u32_mathbfA() ==
u"π"
44 assert m.wchar_heart() ==
u"β₯"
46 assert m.u8_char8_Z() ==
"Z"
50 """Tests failures for passing invalid inputs to char-accepting functions"""
52 def toobig_message(r):
53 return "Character code point not in range({0:#x})".format(r)
55 toolong_message =
"Expected a character, but multi-character string found"
57 assert m.ord_char(
u"a") == 0x61
58 assert m.ord_char_lv(
u"b") == 0x62
60 m.ord_char(
u"Γ©") == 0xE9
62 with pytest.raises(ValueError)
as excinfo:
63 assert m.ord_char(
u"Δ") == 0x100
64 assert str(excinfo.value) == toobig_message(0x100)
65 with pytest.raises(ValueError)
as excinfo:
66 assert m.ord_char(
u"ab")
67 assert str(excinfo.value) == toolong_message
69 assert m.ord_char16(
u"a") == 0x61
70 assert m.ord_char16(
u"Γ©") == 0xE9
71 assert m.ord_char16_lv(
u"Γͺ") == 0xEA
72 assert m.ord_char16(
u"Δ") == 0x100
73 assert m.ord_char16(
u"β½") == 0x203D
74 assert m.ord_char16(
u"β₯") == 0x2665
75 assert m.ord_char16_lv(
u"β‘") == 0x2661
76 with pytest.raises(ValueError)
as excinfo:
77 assert m.ord_char16(
u"π") == 0x1F382
78 assert str(excinfo.value) == toobig_message(0x10000)
79 with pytest.raises(ValueError)
as excinfo:
80 assert m.ord_char16(
u"aa")
81 assert str(excinfo.value) == toolong_message
83 assert m.ord_char32(
u"a") == 0x61
84 assert m.ord_char32(
u"Γ©") == 0xE9
85 assert m.ord_char32(
u"Δ") == 0x100
86 assert m.ord_char32(
u"β½") == 0x203D
87 assert m.ord_char32(
u"β₯") == 0x2665
88 assert m.ord_char32(
u"π") == 0x1F382
89 with pytest.raises(ValueError)
as excinfo:
90 assert m.ord_char32(
u"aa")
91 assert str(excinfo.value) == toolong_message
93 assert m.ord_wchar(
u"a") == 0x61
94 assert m.ord_wchar(
u"Γ©") == 0xE9
95 assert m.ord_wchar(
u"Δ") == 0x100
96 assert m.ord_wchar(
u"β½") == 0x203D
97 assert m.ord_wchar(
u"β₯") == 0x2665
99 with pytest.raises(ValueError)
as excinfo:
100 assert m.ord_wchar(
u"π") == 0x1F382
101 assert str(excinfo.value) == toobig_message(0x10000)
103 assert m.ord_wchar(
u"π") == 0x1F382
104 with pytest.raises(ValueError)
as excinfo:
105 assert m.ord_wchar(
u"aa")
106 assert str(excinfo.value) == toolong_message
109 assert m.ord_char8(
u"a") == 0x61
110 assert m.ord_char8_lv(
u"b") == 0x62
112 m.ord_char8(
u"Γ©") == 0xE9
114 with pytest.raises(ValueError)
as excinfo:
115 assert m.ord_char8(
u"Δ") == 0x100
116 assert str(excinfo.value) == toobig_message(0x100)
117 with pytest.raises(ValueError)
as excinfo:
118 assert m.ord_char8(
u"ab")
119 assert str(excinfo.value) == toolong_message
123 """Tests the ability to pass bytes to C++ string-accepting functions. Note that this is
124 one-way: the only way to return bytes to Python is via the pybind11::bytes class."""
128 b = s
if env.PY2
else s.encode(
"utf8")
132 assert m.strlen(to_bytes(
"hi")) == 2
133 assert m.string_length(to_bytes(
"world")) == 5
134 assert m.string_length(to_bytes(
"a\x00b")) == 3
135 assert m.strlen(to_bytes(
"a\x00b")) == 1
138 assert m.string_length(
u"π©".encode(
"utf8")) == 4
141 @pytest.mark.skipif(
not hasattr(m,
"has_string_view"), reason=
"no <string_view>")
143 """Tests support for C++17 string_view arguments and return values"""
144 assert m.string_view_chars(
"Hi") == [72, 105]
145 assert m.string_view_chars(
"Hi π") == [72, 105, 32, 0xF0, 0x9F, 0x8E, 0x82]
146 assert m.string_view16_chars(
u"Hi π") == [72, 105, 32, 0xD83C, 0xDF82]
147 assert m.string_view32_chars(
u"Hi π") == [72, 105, 32, 127874]
149 assert m.string_view8_chars(
"Hi") == [72, 105]
150 assert m.string_view8_chars(
u"Hi π") == [72, 105, 32, 0xF0, 0x9F, 0x8E, 0x82]
152 assert m.string_view_return() ==
u"utf8 secret π"
153 assert m.string_view16_return() ==
u"utf16 secret π"
154 assert m.string_view32_return() ==
u"utf32 secret π"
156 assert m.string_view8_return() ==
u"utf8 secret π"
159 m.string_view_print(
"Hi")
160 m.string_view_print(
"utf8 π")
161 m.string_view16_print(
u"utf16 π")
162 m.string_view32_print(
u"utf32 π")
174 m.string_view8_print(
"Hi")
175 m.string_view8_print(
u"utf8 π")
185 m.string_view_print(
"Hi, ascii")
186 m.string_view_print(
"Hi, utf8 π")
187 m.string_view16_print(
u"Hi, utf16 π")
188 m.string_view32_print(
u"Hi, utf32 π")
200 m.string_view8_print(
"Hi, ascii")
201 m.string_view8_print(
u"Hi, utf8 π")
212 """Issue #929 - out-of-range integer values shouldn't be accepted"""
213 assert m.i32_str(-1) ==
"-1"
214 assert m.i64_str(-1) ==
"-1"
215 assert m.i32_str(2000000000) ==
"2000000000"
216 assert m.u32_str(2000000000) ==
"2000000000"
218 assert m.i32_str(long(-1)) ==
"-1"
219 assert m.i64_str(long(-1)) ==
"-1"
221 m.i64_str(long(-999999999999))
225 m.u64_str(long(999999999999))
229 assert m.i64_str(-999999999999) ==
"-999999999999"
230 assert m.u64_str(999999999999) ==
"999999999999"
232 with pytest.raises(TypeError)
as excinfo:
234 assert "incompatible function arguments" in str(excinfo.value)
235 with pytest.raises(TypeError)
as excinfo:
237 assert "incompatible function arguments" in str(excinfo.value)
238 with pytest.raises(TypeError)
as excinfo:
239 m.i32_str(-3000000000)
240 assert "incompatible function arguments" in str(excinfo.value)
241 with pytest.raises(TypeError)
as excinfo:
242 m.i32_str(3000000000)
243 assert "incompatible function arguments" in str(excinfo.value)
246 with pytest.raises(TypeError)
as excinfo:
248 assert "incompatible function arguments" in str(excinfo.value)
249 with pytest.raises(TypeError)
as excinfo:
251 assert "incompatible function arguments" in str(excinfo.value)
270 class IntAndIndex(
object):
277 class RaisingTypeErrorOnIndex(
object):
284 class RaisingValueErrorOnIndex(
object):
291 convert, noconvert = m.int_passthrough, m.int_passthrough_noconvert
293 def requires_conversion(v):
294 pytest.raises(TypeError, noconvert, v)
297 pytest.raises(TypeError, convert, v)
299 assert convert(7) == 7
300 assert noconvert(7) == 7
301 cant_convert(3.14159)
303 if (3, 8) <= env.PY < (3, 10):
304 with pytest.deprecated_call():
305 assert convert(Int()) == 42
307 assert convert(Int()) == 42
308 requires_conversion(Int())
309 cant_convert(NotInt())
310 cant_convert(Float())
314 assert convert(Index()) == 42
315 assert noconvert(Index()) == 42
316 assert convert(IntAndIndex()) == 0
317 assert noconvert(IntAndIndex()) == 0
318 assert convert(RaisingTypeErrorOnIndex()) == 42
319 requires_conversion(RaisingTypeErrorOnIndex())
320 assert convert(RaisingValueErrorOnIndex()) == 42
321 requires_conversion(RaisingValueErrorOnIndex())
325 np = pytest.importorskip(
"numpy")
327 convert, noconvert = m.int_passthrough, m.int_passthrough_noconvert
329 def require_implicit(v):
330 pytest.raises(TypeError, noconvert, v)
333 assert convert(np.intc(42)) == 42
334 assert noconvert(np.intc(42)) == 42
338 if (3, 8) <= env.PY < (3, 10):
339 with pytest.deprecated_call():
340 assert convert(np.float32(3.14159)) == 3
342 assert convert(np.float32(3.14159)) == 3
343 require_implicit(np.float32(3.14159))
347 """std::pair <-> tuple & std::tuple <-> tuple"""
348 assert m.pair_passthrough((
True,
"test")) == (
"test",
True)
349 assert m.tuple_passthrough((
True,
"test", 5)) == (5,
"test",
True)
351 assert m.pair_passthrough([
True,
"test"]) == (
"test",
True)
352 assert m.tuple_passthrough([
True,
"test", 5]) == (5,
"test",
True)
353 assert m.empty_tuple() == ()
356 doc(m.pair_passthrough)
358 pair_passthrough(arg0: Tuple[bool, str]) -> Tuple[str, bool]
360 Return a pair in reversed order
364 doc(m.tuple_passthrough)
366 tuple_passthrough(arg0: Tuple[bool, str, int]) -> Tuple[int, str, bool]
368 Return a triple in reversed order
372 assert m.rvalue_pair() == (
"rvalue",
"rvalue")
373 assert m.lvalue_pair() == (
"lvalue",
"lvalue")
374 assert m.rvalue_tuple() == (
"rvalue",
"rvalue",
"rvalue")
375 assert m.lvalue_tuple() == (
"lvalue",
"lvalue",
"lvalue")
376 assert m.rvalue_nested() == (
"rvalue", (
"rvalue", (
"rvalue",
"rvalue")))
377 assert m.lvalue_nested() == (
"lvalue", (
"lvalue", (
"lvalue",
"lvalue")))
379 assert m.int_string_pair() == (2,
"items")
383 """Casters produced with PYBIND11_TYPE_CASTER() should convert nullptr to None"""
384 assert m.return_none_string()
is None
385 assert m.return_none_char()
is None
386 assert m.return_none_bool()
is None
387 assert m.return_none_int()
is None
388 assert m.return_none_float()
is None
389 assert m.return_none_pair()
is None
393 """None passed as various argument types should defer to other overloads"""
394 assert not m.defer_none_cstring(
"abc")
395 assert m.defer_none_cstring(
None)
396 assert not m.defer_none_custom(UserType())
397 assert m.defer_none_custom(
None)
398 assert m.nodefer_none_void(
None)
402 assert m.load_nullptr_t(
None)
is None
403 assert m.cast_nullptr_t()
is None
407 """std::reference_wrapper for builtin and user types"""
408 assert m.refwrap_builtin(42) == 420
409 assert m.refwrap_usertype(UserType(42)) == 42
410 assert m.refwrap_usertype_const(UserType(42)) == 42
412 with pytest.raises(TypeError)
as excinfo:
413 m.refwrap_builtin(
None)
414 assert "incompatible function arguments" in str(excinfo.value)
416 with pytest.raises(TypeError)
as excinfo:
417 m.refwrap_usertype(
None)
418 assert "incompatible function arguments" in str(excinfo.value)
420 assert m.refwrap_lvalue().value == 1
421 assert m.refwrap_lvalue_const().value == 1
423 a1 = m.refwrap_list(copy=
True)
424 a2 = m.refwrap_list(copy=
True)
425 assert [x.value
for x
in a1] == [2, 3]
426 assert [x.value
for x
in a2] == [2, 3]
427 assert not a1[0]
is a2[0]
and not a1[1]
is a2[1]
429 b1 = m.refwrap_list(copy=
False)
430 b2 = m.refwrap_list(copy=
False)
431 assert [x.value
for x
in b1] == [1, 2]
432 assert [x.value
for x
in b2] == [1, 2]
433 assert b1[0]
is b2[0]
and b1[1]
is b2[1]
435 assert m.refwrap_iiw(IncType(5)) == 5
436 assert m.refwrap_call_iiw(IncType(10), m.refwrap_iiw) == [10, 10, 10, 10]
440 """std::complex casts"""
441 assert m.complex_cast(1) ==
"1.0"
442 assert m.complex_cast(2j) ==
"(0.0, 2.0)"
446 """Test bool caster implicit conversions."""
447 convert, noconvert = m.bool_passthrough, m.bool_passthrough_noconvert
449 def require_implicit(v):
450 pytest.raises(TypeError, noconvert, v)
453 pytest.raises(TypeError, convert, v)
456 assert convert(
True)
is True
457 assert convert(
False)
is False
458 assert noconvert(
True)
is True
459 assert noconvert(
False)
is False
462 require_implicit(
None)
463 assert convert(
None)
is False
466 def __init__(self, x):
469 def __nonzero__(self):
483 require_implicit(
A(
True))
484 assert convert(
A(
True))
is True
485 assert convert(
A(
False))
is False
489 np = pytest.importorskip(
"numpy")
491 convert, noconvert = m.bool_passthrough, m.bool_passthrough_noconvert
494 pytest.raises(TypeError, convert, v)
497 assert convert(np.bool_(
True))
is True
498 assert convert(np.bool_(
False))
is False
499 assert noconvert(np.bool_(
True))
is True
500 assert noconvert(np.bool_(
False))
is False
501 cant_convert(np.zeros(2, dtype=
"int"))
505 """In Python 2, a C++ int should return a Python int rather than long
506 if possible: longs are not always accepted where ints are used (such
507 as the argument to sys.exit()). A C++ long long is always a Python
515 assert isinstance(m.longlong_cast(), must_be_long)
519 assert m.test_void_caster()
523 """Verifies that const-ref is propagated through type_caster cast_op.
524 The returned ConstRefCasted type is a mimimal type that is constructed to
525 reference the casting mode used.
528 assert m.takes(x) == 1
529 assert m.takes_move(x) == 1
531 assert m.takes_ptr(x) == 3
532 assert m.takes_ref(x) == 2
533 assert m.takes_ref_wrap(x) == 2
535 assert m.takes_const_ptr(x) == 5
536 assert m.takes_const_ref(x) == 4
537 assert m.takes_const_ref_wrap(x) == 4