test_pytypes.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 from __future__ import division
3 import pytest
4 import sys
5 
6 import env # noqa: F401
7 
8 from pybind11_tests import pytypes as m
9 from pybind11_tests import debug_enabled
10 
11 
12 def test_int(doc):
13  assert doc(m.get_int) == "get_int() -> int"
14 
15 
16 def test_iterator(doc):
17  assert doc(m.get_iterator) == "get_iterator() -> Iterator"
18 
19 
20 def test_iterable(doc):
21  assert doc(m.get_iterable) == "get_iterable() -> Iterable"
22 
23 
24 def test_list(capture, doc):
25  with capture:
26  lst = m.get_list()
27  assert lst == ["inserted-0", "overwritten", "inserted-2"]
28 
29  lst.append("value2")
30  m.print_list(lst)
31  assert capture.unordered == """
32  Entry at position 0: value
33  list item 0: inserted-0
34  list item 1: overwritten
35  list item 2: inserted-2
36  list item 3: value2
37  """
38 
39  assert doc(m.get_list) == "get_list() -> list"
40  assert doc(m.print_list) == "print_list(arg0: list) -> None"
41 
42 
43 def test_none(capture, doc):
44  assert doc(m.get_none) == "get_none() -> None"
45  assert doc(m.print_none) == "print_none(arg0: None) -> None"
46 
47 
48 def test_set(capture, doc):
49  s = m.get_set()
50  assert s == {"key1", "key2", "key3"}
51 
52  with capture:
53  s.add("key4")
54  m.print_set(s)
55  assert capture.unordered == """
56  key: key1
57  key: key2
58  key: key3
59  key: key4
60  """
61 
62  assert not m.set_contains(set([]), 42)
63  assert m.set_contains({42}, 42)
64  assert m.set_contains({"foo"}, "foo")
65 
66  assert doc(m.get_list) == "get_list() -> list"
67  assert doc(m.print_list) == "print_list(arg0: list) -> None"
68 
69 
70 def test_dict(capture, doc):
71  d = m.get_dict()
72  assert d == {"key": "value"}
73 
74  with capture:
75  d["key2"] = "value2"
76  m.print_dict(d)
77  assert capture.unordered == """
78  key: key, value=value
79  key: key2, value=value2
80  """
81 
82  assert not m.dict_contains({}, 42)
83  assert m.dict_contains({42: None}, 42)
84  assert m.dict_contains({"foo": None}, "foo")
85 
86  assert doc(m.get_dict) == "get_dict() -> dict"
87  assert doc(m.print_dict) == "print_dict(arg0: dict) -> None"
88 
89  assert m.dict_keyword_constructor() == {"x": 1, "y": 2, "z": 3}
90 
91 
92 def test_str(doc):
93  assert m.str_from_string().encode().decode() == "baz"
94  assert m.str_from_bytes().encode().decode() == "boo"
95 
96  assert doc(m.str_from_bytes) == "str_from_bytes() -> str"
97 
98  class A(object):
99  def __str__(self):
100  return "this is a str"
101 
102  def __repr__(self):
103  return "this is a repr"
104 
105  assert m.str_from_object(A()) == "this is a str"
106  assert m.repr_from_object(A()) == "this is a repr"
107  assert m.str_from_handle(A()) == "this is a str"
108 
109  s1, s2 = m.str_format()
110  assert s1 == "1 + 2 = 3"
111  assert s1 == s2
112 
113  malformed_utf8 = b"\x80"
114  assert m.str_from_object(malformed_utf8) is malformed_utf8 # To be fixed; see #2380
115  if env.PY2:
116  # with pytest.raises(UnicodeDecodeError):
117  # m.str_from_object(malformed_utf8)
118  with pytest.raises(UnicodeDecodeError):
119  m.str_from_handle(malformed_utf8)
120  else:
121  # assert m.str_from_object(malformed_utf8) == "b'\\x80'"
122  assert m.str_from_handle(malformed_utf8) == "b'\\x80'"
123 
124 
125 def test_bytes(doc):
126  assert m.bytes_from_string().decode() == "foo"
127  assert m.bytes_from_str().decode() == "bar"
128 
129  assert doc(m.bytes_from_str) == "bytes_from_str() -> {}".format(
130  "str" if env.PY2 else "bytes"
131  )
132 
133 
134 def test_capsule(capture):
135  pytest.gc_collect()
136  with capture:
137  a = m.return_capsule_with_destructor()
138  del a
139  pytest.gc_collect()
140  assert capture.unordered == """
141  creating capsule
142  destructing capsule
143  """
144 
145  with capture:
146  a = m.return_capsule_with_destructor_2()
147  del a
148  pytest.gc_collect()
149  assert capture.unordered == """
150  creating capsule
151  destructing capsule: 1234
152  """
153 
154  with capture:
155  a = m.return_capsule_with_name_and_destructor()
156  del a
157  pytest.gc_collect()
158  assert capture.unordered == """
159  created capsule (1234, 'pointer type description')
160  destructing capsule (1234, 'pointer type description')
161  """
162 
163 
165  class SubTestObject:
166  attr_obj = 1
167  attr_char = 2
168 
169  class TestObject:
170  basic_attr = 1
171  begin_end = [1, 2, 3]
172  d = {"operator[object]": 1, "operator[char *]": 2}
173  sub = SubTestObject()
174 
175  def func(self, x, *args):
176  return self.basic_attr + x + sum(args)
177 
178  d = m.accessor_api(TestObject())
179  assert d["basic_attr"] == 1
180  assert d["begin_end"] == [1, 2, 3]
181  assert d["operator[object]"] == 1
182  assert d["operator[char *]"] == 2
183  assert d["attr(object)"] == 1
184  assert d["attr(char *)"] == 2
185  assert d["missing_attr_ptr"] == "raised"
186  assert d["missing_attr_chain"] == "raised"
187  assert d["is_none"] is False
188  assert d["operator()"] == 2
189  assert d["operator*"] == 7
190  assert d["implicit_list"] == [1, 2, 3]
191  assert all(x in TestObject.__dict__ for x in d["implicit_dict"])
192 
193  assert m.tuple_accessor(tuple()) == (0, 1, 2)
194 
195  d = m.accessor_assignment()
196  assert d["get"] == 0
197  assert d["deferred_get"] == 0
198  assert d["set"] == 1
199  assert d["deferred_set"] == 1
200  assert d["var"] == 99
201 
202 
204  """C++ default and converting constructors are equivalent to type calls in Python"""
205  types = [bytes, str, bool, int, float, tuple, list, dict, set]
206  expected = {t.__name__: t() for t in types}
207  if env.PY2:
208  # Note that bytes.__name__ == 'str' in Python 2.
209  # pybind11::str is unicode even under Python 2.
210  expected["bytes"] = bytes()
211  expected["str"] = unicode() # noqa: F821
212  assert m.default_constructors() == expected
213 
214  data = {
215  bytes: b'41', # Currently no supported or working conversions.
216  str: 42,
217  bool: "Not empty",
218  int: "42",
219  float: "+1e3",
220  tuple: range(3),
221  list: range(3),
222  dict: [("two", 2), ("one", 1), ("three", 3)],
223  set: [4, 4, 5, 6, 6, 6],
224  memoryview: b'abc'
225  }
226  inputs = {k.__name__: v for k, v in data.items()}
227  expected = {k.__name__: k(v) for k, v in data.items()}
228  if env.PY2: # Similar to the above. See comments above.
229  inputs["bytes"] = b'41'
230  inputs["str"] = 42
231  expected["bytes"] = b'41'
232  expected["str"] = u"42"
233 
234  assert m.converting_constructors(inputs) == expected
235  assert m.cast_functions(inputs) == expected
236 
237  # Converting constructors and cast functions should just reference rather
238  # than copy when no conversion is needed:
239  noconv1 = m.converting_constructors(expected)
240  for k in noconv1:
241  assert noconv1[k] is expected[k]
242 
243  noconv2 = m.cast_functions(expected)
244  for k in noconv2:
245  assert noconv2[k] is expected[k]
246 
247 
249  # specifically to exercise pybind11::str::raw_str
250  cvt = m.convert_to_pybind11_str
251  assert cvt(u"Str") == u"Str"
252  assert cvt(b'Bytes') == u"Bytes" if env.PY2 else "b'Bytes'"
253  assert cvt(None) == u"None"
254  assert cvt(False) == u"False"
255  assert cvt(True) == u"True"
256  assert cvt(42) == u"42"
257  assert cvt(2**65) == u"36893488147419103232"
258  assert cvt(-1.50) == u"-1.5"
259  assert cvt(()) == u"()"
260  assert cvt((18,)) == u"(18,)"
261  assert cvt([]) == u"[]"
262  assert cvt([28]) == u"[28]"
263  assert cvt({}) == u"{}"
264  assert cvt({3: 4}) == u"{3: 4}"
265  assert cvt(set()) == u"set([])" if env.PY2 else "set()"
266  assert cvt({3, 3}) == u"set([3])" if env.PY2 else "{3}"
267 
268  valid_orig = u"DZ"
269  valid_utf8 = valid_orig.encode("utf-8")
270  valid_cvt = cvt(valid_utf8)
271  assert type(valid_cvt) == bytes # Probably surprising.
272  assert valid_cvt == b'\xc7\xb1'
273 
274  malformed_utf8 = b'\x80'
275  malformed_cvt = cvt(malformed_utf8)
276  assert type(malformed_cvt) == bytes # Probably surprising.
277  assert malformed_cvt == b'\x80'
278 
279 
281  """Tests implicit casting when assigning or appending to dicts and lists."""
282  z = m.get_implicit_casting()
283  assert z['d'] == {
284  'char*_i1': 'abc', 'char*_i2': 'abc', 'char*_e': 'abc', 'char*_p': 'abc',
285  'str_i1': 'str', 'str_i2': 'str1', 'str_e': 'str2', 'str_p': 'str3',
286  'int_i1': 42, 'int_i2': 42, 'int_e': 43, 'int_p': 44
287  }
288  assert z['l'] == [3, 6, 9, 12, 15]
289 
290 
291 def test_print(capture):
292  with capture:
293  m.print_function()
294  assert capture == """
295  Hello, World!
296  1 2.0 three True -- multiple args
297  *args-and-a-custom-separator
298  no new line here -- next print
299  flush
300  py::print + str.format = this
301  """
302  assert capture.stderr == "this goes to stderr"
303 
304  with pytest.raises(RuntimeError) as excinfo:
305  m.print_failure()
306  assert str(excinfo.value) == "make_tuple(): unable to convert " + (
307  "argument of type 'UnregisteredType' to Python object"
308  if debug_enabled else
309  "arguments to Python object (compile in debug mode for details)"
310  )
311 
312 
313 def test_hash():
314  class Hashable(object):
315  def __init__(self, value):
316  self.value = value
317 
318  def __hash__(self):
319  return self.value
320 
321  class Unhashable(object):
322  __hash__ = None
323 
324  assert m.hash_function(Hashable(42)) == 42
325  with pytest.raises(TypeError):
326  m.hash_function(Unhashable())
327 
328 
330  for a, b in [(1, 1), (3, 5)]:
331  li = [a == b, a != b, a < b, a <= b, a > b, a >= b, a + b,
332  a - b, a * b, a / b, a | b, a & b, a ^ b, a >> b, a << b]
333  assert m.test_number_protocol(a, b) == li
334 
335 
337  li = list(range(100))
338  assert li[::2] == m.test_list_slicing(li)
339 
340 
342  # See issue #2361
343  assert m.issue2361_str_implicit_copy_none() == "None"
344  with pytest.raises(TypeError) as excinfo:
345  assert m.issue2361_dict_implicit_copy_none()
346  assert "'NoneType' object is not iterable" in str(excinfo.value)
347 
348 
349 @pytest.mark.parametrize('method, args, fmt, expected_view', [
350  (m.test_memoryview_object, (b'red',), 'B', b'red'),
351  (m.test_memoryview_buffer_info, (b'green',), 'B', b'green'),
352  (m.test_memoryview_from_buffer, (False,), 'h', [3, 1, 4, 1, 5]),
353  (m.test_memoryview_from_buffer, (True,), 'H', [2, 7, 1, 8]),
354  (m.test_memoryview_from_buffer_nativeformat, (), '@i', [4, 7, 5]),
355 ])
356 def test_memoryview(method, args, fmt, expected_view):
357  view = method(*args)
358  assert isinstance(view, memoryview)
359  assert view.format == fmt
360  if isinstance(expected_view, bytes) or not env.PY2:
361  view_as_list = list(view)
362  else:
363  # Using max to pick non-zero byte (big-endian vs little-endian).
364  view_as_list = [max([ord(c) for c in s]) for s in view]
365  assert view_as_list == list(expected_view)
366 
367 
368 @pytest.mark.xfail("env.PYPY", reason="getrefcount is not available")
369 @pytest.mark.parametrize('method', [
370  m.test_memoryview_object,
371  m.test_memoryview_buffer_info,
372 ])
374  buf = b'\x0a\x0b\x0c\x0d'
375  ref_before = sys.getrefcount(buf)
376  view = method(buf)
377  ref_after = sys.getrefcount(buf)
378  assert ref_before < ref_after
379  assert list(view) == list(buf)
380 
381 
383  view = m.test_memoryview_from_buffer_empty_shape()
384  assert isinstance(view, memoryview)
385  assert view.format == 'B'
386  if env.PY2:
387  # Python 2 behavior is weird, but Python 3 (the future) is fine.
388  # PyPy3 has <memoryview, while CPython 2 has <memory
389  assert bytes(view).startswith(b'<memory')
390  else:
391  assert bytes(view) == b''
392 
393 
395  with pytest.raises(RuntimeError):
396  m.test_memoryview_from_buffer_invalid_strides()
397 
398 
400  if env.PY2:
401  m.test_memoryview_from_buffer_nullptr()
402  else:
403  with pytest.raises(ValueError):
404  m.test_memoryview_from_buffer_nullptr()
405 
406 
407 @pytest.mark.skipif("env.PY2")
409  view = m.test_memoryview_from_memory()
410  assert isinstance(view, memoryview)
411  assert view.format == 'B'
412  assert bytes(view) == b'\xff\xe1\xab\x37'
#define max(a, b)
Definition: datatypes.h:20
def test_number_protocol()
def test_capsule(capture)
def test_memoryview_refcount(method)
Annotation for documentation.
Definition: attr.h:33
def test_dict(capture, doc)
Definition: test_pytypes.py:70
def test_iterator(doc)
Definition: test_pytypes.py:16
def test_print(capture)
def test_none(capture, doc)
Definition: test_pytypes.py:43
def test_pybind11_str_raw_str()
bool isinstance(handle obj)
Definition: pytypes.h:384
def test_test_memoryview_from_buffer_invalid_strides()
def test_test_memoryview_from_buffer_nullptr()
def test_constructors()
def test_list(capture, doc)
Definition: test_pytypes.py:24
def test_set(capture, doc)
Definition: test_pytypes.py:48
def test_int(doc)
Definition: test_pytypes.py:12
def test_memoryview_from_memory()
Definition: pytypes.h:928
def test_accessors()
def test_bytes(doc)
const mpreal sum(const mpreal tab[], const unsigned long int n, int &status, mp_rnd_t mode=mpreal::get_default_rnd())
Definition: mpreal.h:2381
Definition: pytypes.h:1301
int func(const int &a)
Definition: testDSF.cpp:225
def test_memoryview_from_buffer_empty_shape()
def test_issue2361()
def test_implicit_casting()
def test_str(doc)
Definition: test_pytypes.py:92
def test_iterable(doc)
Definition: test_pytypes.py:20
def test_memoryview(method, args, fmt, expected_view)
def test_list_slicing()
Definition: pytypes.h:1325
Definition: pytypes.h:995
Point2 t(10, 10)
Definition: pytypes.h:897


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:04