1 from __future__
import division
5 from pybind11_tests
import pytypes
as m
6 from pybind11_tests
import debug_enabled
12 assert lst == [
"inserted-0",
"overwritten",
"inserted-2"]
16 assert capture.unordered ==
""" 17 Entry at position 0: value 18 list item 0: inserted-0 19 list item 1: overwritten 20 list item 2: inserted-2 24 assert doc(m.get_list) ==
"get_list() -> list" 25 assert doc(m.print_list) ==
"print_list(arg0: list) -> None" 30 assert s == {
"key1",
"key2",
"key3"}
35 assert capture.unordered ==
""" 42 assert not m.set_contains(set([]), 42)
43 assert m.set_contains({42}, 42)
44 assert m.set_contains({
"foo"},
"foo")
46 assert doc(m.get_list) ==
"get_list() -> list" 47 assert doc(m.print_list) ==
"print_list(arg0: list) -> None" 52 assert d == {
"key":
"value"}
57 assert capture.unordered ==
""" 59 key: key2, value=value2 62 assert not m.dict_contains({}, 42)
63 assert m.dict_contains({42:
None}, 42)
64 assert m.dict_contains({
"foo":
None},
"foo")
66 assert doc(m.get_dict) ==
"get_dict() -> dict" 67 assert doc(m.print_dict) ==
"print_dict(arg0: dict) -> None" 69 assert m.dict_keyword_constructor() == {
"x": 1,
"y": 2,
"z": 3}
73 assert m.str_from_string().encode().decode() ==
"baz" 74 assert m.str_from_bytes().encode().decode() ==
"boo" 76 assert doc(m.str_from_bytes) ==
"str_from_bytes() -> str" 80 return "this is a str" 83 return "this is a repr" 85 assert m.str_from_object(
A()) ==
"this is a str" 86 assert m.repr_from_object(
A()) ==
"this is a repr" 88 s1, s2 = m.str_format()
89 assert s1 ==
"1 + 2 = 3" 94 assert m.bytes_from_string().decode() ==
"foo" 95 assert m.bytes_from_str().decode() ==
"bar" 97 assert doc(m.bytes_from_str) ==
"bytes_from_str() -> {}".
format(
98 "bytes" if sys.version_info[0] == 3
else "str" 105 a = m.return_capsule_with_destructor()
108 assert capture.unordered ==
""" 114 a = m.return_capsule_with_destructor_2()
117 assert capture.unordered ==
""" 119 destructing capsule: 1234 123 a = m.return_capsule_with_name_and_destructor()
126 assert capture.unordered ==
""" 127 created capsule (1234, 'pointer type description') 128 destructing capsule (1234, 'pointer type description') 139 begin_end = [1, 2, 3]
140 d = {
"operator[object]": 1,
"operator[char *]": 2}
141 sub = SubTestObject()
143 def func(self, x, *args):
144 return self.basic_attr + x + sum(args)
146 d = m.accessor_api(TestObject())
147 assert d[
"basic_attr"] == 1
148 assert d[
"begin_end"] == [1, 2, 3]
149 assert d[
"operator[object]"] == 1
150 assert d[
"operator[char *]"] == 2
151 assert d[
"attr(object)"] == 1
152 assert d[
"attr(char *)"] == 2
153 assert d[
"missing_attr_ptr"] ==
"raised" 154 assert d[
"missing_attr_chain"] ==
"raised" 155 assert d[
"is_none"]
is False 156 assert d[
"operator()"] == 2
157 assert d[
"operator*"] == 7
158 assert d[
"implicit_list"] == [1, 2, 3]
159 assert all(x
in TestObject.__dict__
for x
in d[
"implicit_dict"])
161 assert m.tuple_accessor(tuple()) == (0, 1, 2)
163 d = m.accessor_assignment()
165 assert d[
"deferred_get"] == 0
167 assert d[
"deferred_set"] == 1
168 assert d[
"var"] == 99
172 """C++ default and converting constructors are equivalent to type calls in Python""" 173 types = [str, bool, int, float, tuple, list, dict, set]
174 expected = {t.__name__: t()
for t
in types}
175 assert m.default_constructors() == expected
184 dict: [(
"two", 2), (
"one", 1), (
"three", 3)],
185 set: [4, 4, 5, 6, 6, 6],
188 inputs = {k.__name__: v
for k, v
in data.items()}
189 expected = {k.__name__: k(v)
for k, v
in data.items()}
191 assert m.converting_constructors(inputs) == expected
192 assert m.cast_functions(inputs) == expected
196 noconv1 = m.converting_constructors(expected)
198 assert noconv1[k]
is expected[k]
200 noconv2 = m.cast_functions(expected)
202 assert noconv2[k]
is expected[k]
206 """Tests implicit casting when assigning or appending to dicts and lists.""" 207 z = m.get_implicit_casting()
209 'char*_i1':
'abc',
'char*_i2':
'abc',
'char*_e':
'abc',
'char*_p':
'abc',
210 'str_i1':
'str',
'str_i2':
'str1',
'str_e':
'str2',
'str_p':
'str3',
211 'int_i1': 42,
'int_i2': 42,
'int_e': 43,
'int_p': 44
213 assert z[
'l'] == [3, 6, 9, 12, 15]
219 assert capture ==
""" 221 1 2.0 three True -- multiple args 222 *args-and-a-custom-separator 223 no new line here -- next print 225 py::print + str.format = this 227 assert capture.stderr ==
"this goes to stderr" 229 with pytest.raises(RuntimeError)
as excinfo:
231 assert str(excinfo.value) ==
"make_tuple(): unable to convert " + (
232 "argument of type 'UnregisteredType' to Python object" 233 if debug_enabled
else 234 "arguments to Python object (compile in debug mode for details)" 239 class Hashable(object):
240 def __init__(self, value):
246 class Unhashable(object):
249 assert m.hash_function(Hashable(42)) == 42
250 with pytest.raises(TypeError):
251 m.hash_function(Unhashable())
255 for a, b
in [(1, 1), (3, 5)]:
256 li = [a == b, a != b, a < b, a <= b, a > b, a >= b, a + b,
257 a - b, a * b, a / b, a | b, a & b, a ^ b, a >> b, a << b]
258 assert m.test_number_protocol(a, b) == li
262 li = list(
range(100))
263 assert li[::2] == m.test_list_slicing(li)
def test_number_protocol()
def test_capsule(capture)
def test_dict(capture, doc)
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 >
def test_list(capture, doc)
def test_set(capture, doc)
def test_implicit_casting()