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""" 51 def toobig_message(r):
52 return "Character code point not in range({0:#x})".format(r)
53 toolong_message =
"Expected a character, but multi-character string found" 55 assert m.ord_char(
u'a') == 0x61
56 assert m.ord_char_lv(
u'b') == 0x62
57 assert m.ord_char(
u'Γ©') == 0xE9
58 with pytest.raises(ValueError)
as excinfo:
59 assert m.ord_char(
u'Δ') == 0x100
60 assert str(excinfo.value) == toobig_message(0x100)
61 with pytest.raises(ValueError)
as excinfo:
62 assert m.ord_char(
u'ab')
63 assert str(excinfo.value) == toolong_message
65 assert m.ord_char16(
u'a') == 0x61
66 assert m.ord_char16(
u'Γ©') == 0xE9
67 assert m.ord_char16_lv(
u'Γͺ') == 0xEA
68 assert m.ord_char16(
u'Δ') == 0x100
69 assert m.ord_char16(
u'β½') == 0x203d
70 assert m.ord_char16(
u'β₯') == 0x2665
71 assert m.ord_char16_lv(
u'β‘') == 0x2661
72 with pytest.raises(ValueError)
as excinfo:
73 assert m.ord_char16(
u'π') == 0x1F382
74 assert str(excinfo.value) == toobig_message(0x10000)
75 with pytest.raises(ValueError)
as excinfo:
76 assert m.ord_char16(
u'aa')
77 assert str(excinfo.value) == toolong_message
79 assert m.ord_char32(
u'a') == 0x61
80 assert m.ord_char32(
u'Γ©') == 0xE9
81 assert m.ord_char32(
u'Δ') == 0x100
82 assert m.ord_char32(
u'β½') == 0x203d
83 assert m.ord_char32(
u'β₯') == 0x2665
84 assert m.ord_char32(
u'π') == 0x1F382
85 with pytest.raises(ValueError)
as excinfo:
86 assert m.ord_char32(
u'aa')
87 assert str(excinfo.value) == toolong_message
89 assert m.ord_wchar(
u'a') == 0x61
90 assert m.ord_wchar(
u'Γ©') == 0xE9
91 assert m.ord_wchar(
u'Δ') == 0x100
92 assert m.ord_wchar(
u'β½') == 0x203d
93 assert m.ord_wchar(
u'β₯') == 0x2665
95 with pytest.raises(ValueError)
as excinfo:
96 assert m.ord_wchar(
u'π') == 0x1F382
97 assert str(excinfo.value) == toobig_message(0x10000)
99 assert m.ord_wchar(
u'π') == 0x1F382
100 with pytest.raises(ValueError)
as excinfo:
101 assert m.ord_wchar(
u'aa')
102 assert str(excinfo.value) == toolong_message
105 assert m.ord_char8(
u'a') == 0x61
106 assert m.ord_char8_lv(
u'b') == 0x62
107 assert m.ord_char8(
u'Γ©') == 0xE9
108 with pytest.raises(ValueError)
as excinfo:
109 assert m.ord_char8(
u'Δ') == 0x100
110 assert str(excinfo.value) == toobig_message(0x100)
111 with pytest.raises(ValueError)
as excinfo:
112 assert m.ord_char8(
u'ab')
113 assert str(excinfo.value) == toolong_message
117 """Tests the ability to pass bytes to C++ string-accepting functions. Note that this is 118 one-way: the only way to return bytes to Python is via the pybind11::bytes class.""" 122 b = s
if env.PY2
else s.encode(
"utf8")
126 assert m.strlen(to_bytes(
"hi")) == 2
127 assert m.string_length(to_bytes(
"world")) == 5
128 assert m.string_length(to_bytes(
"a\x00b")) == 3
129 assert m.strlen(to_bytes(
"a\x00b")) == 1
132 assert m.string_length(
u'π©'.encode(
"utf8")) == 4
135 @pytest.mark.skipif(
not hasattr(m,
"has_string_view"), reason=
"no <string_view>")
137 """Tests support for C++17 string_view arguments and return values""" 138 assert m.string_view_chars(
"Hi") == [72, 105]
139 assert m.string_view_chars(
"Hi π") == [72, 105, 32, 0xf0, 0x9f, 0x8e, 0x82]
140 assert m.string_view16_chars(
u"Hi π") == [72, 105, 32, 0xd83c, 0xdf82]
141 assert m.string_view32_chars(
u"Hi π") == [72, 105, 32, 127874]
143 assert m.string_view8_chars(
"Hi") == [72, 105]
144 assert m.string_view8_chars(
u"Hi π") == [72, 105, 32, 0xf0, 0x9f, 0x8e, 0x82]
146 assert m.string_view_return() ==
u"utf8 secret π" 147 assert m.string_view16_return() ==
u"utf16 secret π" 148 assert m.string_view32_return() ==
u"utf32 secret π" 150 assert m.string_view8_return() ==
u"utf8 secret π" 153 m.string_view_print(
"Hi")
154 m.string_view_print(
"utf8 π")
155 m.string_view16_print(
u"utf16 π")
156 m.string_view32_print(
u"utf32 π")
157 assert capture ==
u""" 165 m.string_view8_print(
"Hi")
166 m.string_view8_print(
u"utf8 π")
167 assert capture ==
u""" 173 m.string_view_print(
"Hi, ascii")
174 m.string_view_print(
"Hi, utf8 π")
175 m.string_view16_print(
u"Hi, utf16 π")
176 m.string_view32_print(
u"Hi, utf32 π")
177 assert capture ==
u""" 185 m.string_view8_print(
"Hi, ascii")
186 m.string_view8_print(
u"Hi, utf8 π")
187 assert capture ==
u""" 194 """Issue #929 - out-of-range integer values shouldn't be accepted""" 195 assert m.i32_str(-1) ==
"-1" 196 assert m.i64_str(-1) ==
"-1" 197 assert m.i32_str(2000000000) ==
"2000000000" 198 assert m.u32_str(2000000000) ==
"2000000000" 200 assert m.i32_str(long(-1)) ==
"-1" 201 assert m.i64_str(long(-1)) ==
"-1" 202 assert m.i64_str(long(-999999999999)) ==
"-999999999999" 203 assert m.u64_str(long(999999999999)) ==
"999999999999" 205 assert m.i64_str(-999999999999) ==
"-999999999999" 206 assert m.u64_str(999999999999) ==
"999999999999" 208 with pytest.raises(TypeError)
as excinfo:
210 assert "incompatible function arguments" in str(excinfo.value)
211 with pytest.raises(TypeError)
as excinfo:
213 assert "incompatible function arguments" in str(excinfo.value)
214 with pytest.raises(TypeError)
as excinfo:
215 m.i32_str(-3000000000)
216 assert "incompatible function arguments" in str(excinfo.value)
217 with pytest.raises(TypeError)
as excinfo:
218 m.i32_str(3000000000)
219 assert "incompatible function arguments" in str(excinfo.value)
222 with pytest.raises(TypeError)
as excinfo:
224 assert "incompatible function arguments" in str(excinfo.value)
225 with pytest.raises(TypeError)
as excinfo:
227 assert "incompatible function arguments" in str(excinfo.value)
231 """std::pair <-> tuple & std::tuple <-> tuple""" 232 assert m.pair_passthrough((
True,
"test")) == (
"test",
True)
233 assert m.tuple_passthrough((
True,
"test", 5)) == (5,
"test",
True)
235 assert m.pair_passthrough([
True,
"test"]) == (
"test",
True)
236 assert m.tuple_passthrough([
True,
"test", 5]) == (5,
"test",
True)
237 assert m.empty_tuple() == ()
239 assert doc(m.pair_passthrough) ==
""" 240 pair_passthrough(arg0: Tuple[bool, str]) -> Tuple[str, bool] 242 Return a pair in reversed order 244 assert doc(m.tuple_passthrough) ==
""" 245 tuple_passthrough(arg0: Tuple[bool, str, int]) -> Tuple[int, str, bool] 247 Return a triple in reversed order 250 assert m.rvalue_pair() == (
"rvalue",
"rvalue")
251 assert m.lvalue_pair() == (
"lvalue",
"lvalue")
252 assert m.rvalue_tuple() == (
"rvalue",
"rvalue",
"rvalue")
253 assert m.lvalue_tuple() == (
"lvalue",
"lvalue",
"lvalue")
254 assert m.rvalue_nested() == (
"rvalue", (
"rvalue", (
"rvalue",
"rvalue")))
255 assert m.lvalue_nested() == (
"lvalue", (
"lvalue", (
"lvalue",
"lvalue")))
257 assert m.int_string_pair() == (2,
"items")
261 """Casters produced with PYBIND11_TYPE_CASTER() should convert nullptr to None""" 262 assert m.return_none_string()
is None 263 assert m.return_none_char()
is None 264 assert m.return_none_bool()
is None 265 assert m.return_none_int()
is None 266 assert m.return_none_float()
is None 267 assert m.return_none_pair()
is None 271 """None passed as various argument types should defer to other overloads""" 272 assert not m.defer_none_cstring(
"abc")
273 assert m.defer_none_cstring(
None)
274 assert not m.defer_none_custom(UserType())
275 assert m.defer_none_custom(
None)
276 assert m.nodefer_none_void(
None)
280 assert m.load_nullptr_t(
None)
is None 281 assert m.cast_nullptr_t()
is None 285 """std::reference_wrapper for builtin and user types""" 286 assert m.refwrap_builtin(42) == 420
287 assert m.refwrap_usertype(UserType(42)) == 42
289 with pytest.raises(TypeError)
as excinfo:
290 m.refwrap_builtin(
None)
291 assert "incompatible function arguments" in str(excinfo.value)
293 with pytest.raises(TypeError)
as excinfo:
294 m.refwrap_usertype(
None)
295 assert "incompatible function arguments" in str(excinfo.value)
297 a1 = m.refwrap_list(copy=
True)
298 a2 = m.refwrap_list(copy=
True)
299 assert [x.value
for x
in a1] == [2, 3]
300 assert [x.value
for x
in a2] == [2, 3]
301 assert not a1[0]
is a2[0]
and not a1[1]
is a2[1]
303 b1 = m.refwrap_list(copy=
False)
304 b2 = m.refwrap_list(copy=
False)
305 assert [x.value
for x
in b1] == [1, 2]
306 assert [x.value
for x
in b2] == [1, 2]
307 assert b1[0]
is b2[0]
and b1[1]
is b2[1]
309 assert m.refwrap_iiw(IncType(5)) == 5
310 assert m.refwrap_call_iiw(IncType(10), m.refwrap_iiw) == [10, 10, 10, 10]
314 """std::complex casts""" 315 assert m.complex_cast(1) ==
"1.0" 316 assert m.complex_cast(2j) ==
"(0.0, 2.0)" 320 """Test bool caster implicit conversions.""" 321 convert, noconvert = m.bool_passthrough, m.bool_passthrough_noconvert
323 def require_implicit(v):
324 pytest.raises(TypeError, noconvert, v)
327 pytest.raises(TypeError, convert, v)
332 assert noconvert(
True)
is True 333 assert noconvert(
False)
is False 336 require_implicit(
None)
340 def __init__(self, x):
343 def __nonzero__(self):
357 require_implicit(
A(
True))
363 np = pytest.importorskip(
"numpy")
365 convert, noconvert = m.bool_passthrough, m.bool_passthrough_noconvert
368 pytest.raises(TypeError, convert, v)
371 assert convert(np.bool_(
True))
is True 372 assert convert(np.bool_(
False))
is False 373 assert noconvert(np.bool_(
True))
is True 374 assert noconvert(np.bool_(
False))
is False 375 cant_convert(np.zeros(2, dtype=
'int'))
379 """In Python 2, a C++ int should return a Python int rather than long 380 if possible: longs are not always accepted where ints are used (such 381 as the argument to sys.exit()). A C++ long long is always a Python 388 assert isinstance(m.longlong_cast(), must_be_long)
392 assert m.test_void_caster()
def test_string_view(capture)
bool hasattr(handle obj, handle name)
def test_reference_wrapper()
def test_builtins_cast_return_none()
Annotation for documentation.
def test_integer_casting()
bool isinstance(handle obj)
bool convert(const int &y)
def test_single_char_arguments()
def test_unicode_conversion()
def test_bytes_to_string()
object getattr(handle obj, handle name)