4 from pybind11_tests
import builtin_casters
as m
5 from pybind11_tests
import UserType, IncType
9 assert m.string_roundtrip(
"const char *") ==
"const char *" 13 """Tests unicode conversion and error reporting.""" 14 assert m.good_utf8_string() ==
u"Say utf8β½ π π" 15 assert m.good_utf16_string() ==
u"bβ½ππz" 16 assert m.good_utf32_string() ==
u"aππβ½z" 17 assert m.good_wchar_string() ==
u"aβΈπz" 19 with pytest.raises(UnicodeDecodeError):
22 with pytest.raises(UnicodeDecodeError):
26 if hasattr(m,
"bad_utf32_string"):
27 with pytest.raises(UnicodeDecodeError):
29 if hasattr(m,
"bad_wchar_string"):
30 with pytest.raises(UnicodeDecodeError):
33 assert m.u8_Z() ==
'Z' 34 assert m.u8_eacute() ==
u'Γ©' 35 assert m.u16_ibang() ==
u'β½' 36 assert m.u32_mathbfA() ==
u'π' 37 assert m.wchar_heart() ==
u'β₯' 41 """Tests failures for passing invalid inputs to char-accepting functions""" 42 def toobig_message(r):
43 return "Character code point not in range({0:#x})".
format(r)
44 toolong_message =
"Expected a character, but multi-character string found" 46 assert m.ord_char(
u'a') == 0x61
47 assert m.ord_char_lv(
u'b') == 0x62
48 assert m.ord_char(
u'Γ©') == 0xE9
49 with pytest.raises(ValueError)
as excinfo:
50 assert m.ord_char(
u'Δ') == 0x100
51 assert str(excinfo.value) == toobig_message(0x100)
52 with pytest.raises(ValueError)
as excinfo:
53 assert m.ord_char(
u'ab')
54 assert str(excinfo.value) == toolong_message
56 assert m.ord_char16(
u'a') == 0x61
57 assert m.ord_char16(
u'Γ©') == 0xE9
58 assert m.ord_char16_lv(
u'Γͺ') == 0xEA
59 assert m.ord_char16(
u'Δ') == 0x100
60 assert m.ord_char16(
u'β½') == 0x203d
61 assert m.ord_char16(
u'β₯') == 0x2665
62 assert m.ord_char16_lv(
u'β‘') == 0x2661
63 with pytest.raises(ValueError)
as excinfo:
64 assert m.ord_char16(
u'π') == 0x1F382
65 assert str(excinfo.value) == toobig_message(0x10000)
66 with pytest.raises(ValueError)
as excinfo:
67 assert m.ord_char16(
u'aa')
68 assert str(excinfo.value) == toolong_message
70 assert m.ord_char32(
u'a') == 0x61
71 assert m.ord_char32(
u'Γ©') == 0xE9
72 assert m.ord_char32(
u'Δ') == 0x100
73 assert m.ord_char32(
u'β½') == 0x203d
74 assert m.ord_char32(
u'β₯') == 0x2665
75 assert m.ord_char32(
u'π') == 0x1F382
76 with pytest.raises(ValueError)
as excinfo:
77 assert m.ord_char32(
u'aa')
78 assert str(excinfo.value) == toolong_message
80 assert m.ord_wchar(
u'a') == 0x61
81 assert m.ord_wchar(
u'Γ©') == 0xE9
82 assert m.ord_wchar(
u'Δ') == 0x100
83 assert m.ord_wchar(
u'β½') == 0x203d
84 assert m.ord_wchar(
u'β₯') == 0x2665
86 with pytest.raises(ValueError)
as excinfo:
87 assert m.ord_wchar(
u'π') == 0x1F382
88 assert str(excinfo.value) == toobig_message(0x10000)
90 assert m.ord_wchar(
u'π') == 0x1F382
91 with pytest.raises(ValueError)
as excinfo:
92 assert m.ord_wchar(
u'aa')
93 assert str(excinfo.value) == toolong_message
97 """Tests the ability to pass bytes to C++ string-accepting functions. Note that this is 98 one-way: the only way to return bytes to Python is via the pybind11::bytes class.""" 101 byte = bytes
if sys.version_info[0] < 3
else str
103 assert m.strlen(byte(
"hi")) == 2
104 assert m.string_length(byte(
"world")) == 5
105 assert m.string_length(byte(
"a\x00b")) == 3
106 assert m.strlen(byte(
"a\x00b")) == 1
109 assert m.string_length(
u'π©'.encode(
"utf8")) == 4
112 @pytest.mark.skipif(
not hasattr(m,
"has_string_view"), reason=
"no <string_view>")
114 """Tests support for C++17 string_view arguments and return values""" 115 assert m.string_view_chars(
"Hi") == [72, 105]
116 assert m.string_view_chars(
"Hi π") == [72, 105, 32, 0xf0, 0x9f, 0x8e, 0x82]
117 assert m.string_view16_chars(
"Hi π") == [72, 105, 32, 0xd83c, 0xdf82]
118 assert m.string_view32_chars(
"Hi π") == [72, 105, 32, 127874]
120 assert m.string_view_return() ==
"utf8 secret π" 121 assert m.string_view16_return() ==
"utf16 secret π" 122 assert m.string_view32_return() ==
"utf32 secret π" 125 m.string_view_print(
"Hi")
126 m.string_view_print(
"utf8 π")
127 m.string_view16_print(
"utf16 π")
128 m.string_view32_print(
"utf32 π")
129 assert capture ==
""" 137 m.string_view_print(
"Hi, ascii")
138 m.string_view_print(
"Hi, utf8 π")
139 m.string_view16_print(
"Hi, utf16 π")
140 m.string_view32_print(
"Hi, utf32 π")
141 assert capture ==
""" 150 """Issue #929 - out-of-range integer values shouldn't be accepted""" 152 assert m.i32_str(-1) ==
"-1" 153 assert m.i64_str(-1) ==
"-1" 154 assert m.i32_str(2000000000) ==
"2000000000" 155 assert m.u32_str(2000000000) ==
"2000000000" 156 if sys.version_info < (3,):
157 assert m.i32_str(long(-1)) ==
"-1" 158 assert m.i64_str(long(-1)) ==
"-1" 159 assert m.i64_str(long(-999999999999)) ==
"-999999999999" 160 assert m.u64_str(long(999999999999)) ==
"999999999999" 162 assert m.i64_str(-999999999999) ==
"-999999999999" 163 assert m.u64_str(999999999999) ==
"999999999999" 165 with pytest.raises(TypeError)
as excinfo:
167 assert "incompatible function arguments" in str(excinfo.value)
168 with pytest.raises(TypeError)
as excinfo:
170 assert "incompatible function arguments" in str(excinfo.value)
171 with pytest.raises(TypeError)
as excinfo:
172 m.i32_str(-3000000000)
173 assert "incompatible function arguments" in str(excinfo.value)
174 with pytest.raises(TypeError)
as excinfo:
175 m.i32_str(3000000000)
176 assert "incompatible function arguments" in str(excinfo.value)
178 if sys.version_info < (3,):
179 with pytest.raises(TypeError)
as excinfo:
181 assert "incompatible function arguments" in str(excinfo.value)
182 with pytest.raises(TypeError)
as excinfo:
184 assert "incompatible function arguments" in str(excinfo.value)
188 """std::pair <-> tuple & std::tuple <-> tuple""" 189 assert m.pair_passthrough((
True,
"test")) == (
"test",
True)
190 assert m.tuple_passthrough((
True,
"test", 5)) == (5,
"test",
True)
192 assert m.pair_passthrough([
True,
"test"]) == (
"test",
True)
193 assert m.tuple_passthrough([
True,
"test", 5]) == (5,
"test",
True)
194 assert m.empty_tuple() == ()
196 assert doc(m.pair_passthrough) ==
""" 197 pair_passthrough(arg0: Tuple[bool, str]) -> Tuple[str, bool] 199 Return a pair in reversed order 201 assert doc(m.tuple_passthrough) ==
""" 202 tuple_passthrough(arg0: Tuple[bool, str, int]) -> Tuple[int, str, bool] 204 Return a triple in reversed order 207 assert m.rvalue_pair() == (
"rvalue",
"rvalue")
208 assert m.lvalue_pair() == (
"lvalue",
"lvalue")
209 assert m.rvalue_tuple() == (
"rvalue",
"rvalue",
"rvalue")
210 assert m.lvalue_tuple() == (
"lvalue",
"lvalue",
"lvalue")
211 assert m.rvalue_nested() == (
"rvalue", (
"rvalue", (
"rvalue",
"rvalue")))
212 assert m.lvalue_nested() == (
"lvalue", (
"lvalue", (
"lvalue",
"lvalue")))
216 """Casters produced with PYBIND11_TYPE_CASTER() should convert nullptr to None""" 217 assert m.return_none_string()
is None 218 assert m.return_none_char()
is None 219 assert m.return_none_bool()
is None 220 assert m.return_none_int()
is None 221 assert m.return_none_float()
is None 225 """None passed as various argument types should defer to other overloads""" 226 assert not m.defer_none_cstring(
"abc")
227 assert m.defer_none_cstring(
None)
228 assert not m.defer_none_custom(UserType())
229 assert m.defer_none_custom(
None)
230 assert m.nodefer_none_void(
None)
234 assert m.load_nullptr_t(
None)
is None 235 assert m.cast_nullptr_t()
is None 239 """std::reference_wrapper for builtin and user types""" 240 assert m.refwrap_builtin(42) == 420
241 assert m.refwrap_usertype(UserType(42)) == 42
243 with pytest.raises(TypeError)
as excinfo:
244 m.refwrap_builtin(
None)
245 assert "incompatible function arguments" in str(excinfo.value)
247 with pytest.raises(TypeError)
as excinfo:
248 m.refwrap_usertype(
None)
249 assert "incompatible function arguments" in str(excinfo.value)
251 a1 = m.refwrap_list(copy=
True)
252 a2 = m.refwrap_list(copy=
True)
253 assert [x.value
for x
in a1] == [2, 3]
254 assert [x.value
for x
in a2] == [2, 3]
255 assert not a1[0]
is a2[0]
and not a1[1]
is a2[1]
257 b1 = m.refwrap_list(copy=
False)
258 b2 = m.refwrap_list(copy=
False)
259 assert [x.value
for x
in b1] == [1, 2]
260 assert [x.value
for x
in b2] == [1, 2]
261 assert b1[0]
is b2[0]
and b1[1]
is b2[1]
263 assert m.refwrap_iiw(IncType(5)) == 5
264 assert m.refwrap_call_iiw(IncType(10), m.refwrap_iiw) == [10, 10, 10, 10]
268 """std::complex casts""" 269 assert m.complex_cast(1) ==
"1.0" 270 assert m.complex_cast(2j) ==
"(0.0, 2.0)" 274 """Test bool caster implicit conversions.""" 275 convert, noconvert = m.bool_passthrough, m.bool_passthrough_noconvert
277 def require_implicit(v):
278 pytest.raises(TypeError, noconvert, v)
281 pytest.raises(TypeError, convert, v)
284 assert convert(
True)
is True 285 assert convert(
False)
is False 286 assert noconvert(
True)
is True 287 assert noconvert(
False)
is False 290 require_implicit(
None)
291 assert convert(
None)
is False 294 def __init__(self, x):
297 def __nonzero__(self):
307 cant_convert(object())
311 require_implicit(
A(
True))
312 assert convert(
A(
True))
is True 313 assert convert(
A(
False))
is False 316 @pytest.requires_numpy
319 convert, noconvert = m.bool_passthrough, m.bool_passthrough_noconvert
322 assert convert(np.bool_(
True))
is True 323 assert convert(np.bool_(
False))
is False 324 assert noconvert(np.bool_(
True))
is True 325 assert noconvert(np.bool_(
False))
is False 329 """In Python 2, a C++ int should return a Python int rather than long 330 if possible: longs are not always accepted where ints are used (such 331 as the argument to sys.exit()). A C++ long long is always a Python 338 assert isinstance(m.longlong_cast(), must_be_long)
342 assert m.test_void_caster()
def test_string_view(capture)
def test_reference_wrapper()
object getattr(handle obj, handle name)
def test_builtins_cast_return_none()
def test_integer_casting()
auto format(const std::locale &loc, const CharT *fmt, const Streamable &tp) -> decltype(to_stream(std::declval< std::basic_ostream< CharT > &>(), fmt, tp), std::basic_string< CharT >
return isinstance(obj, type)
def test_single_char_arguments()
def test_unicode_conversion()
def test_bytes_to_string()
const detail::type_info * type
bool hasattr(handle obj, handle name)