Commit faec30c4 authored by Wenzel Jakob's avatar Wenzel Jakob Committed by GitHub
Browse files

Merge pull request #321 from dean0x7d/pytest

Port test suite to pytest
parents bf099587 99dbdc16
test_value 15 48
test_value 15 48
#!/usr/bin/env python
from __future__ import print_function
import sys, pydoc
sys.path.append('.')
import example
from example import ExamplePythonTypes
ExamplePythonTypes.value = 15
print(ExamplePythonTypes.value)
print(ExamplePythonTypes.value2)
try:
ExamplePythonTypes()
except Exception as e:
print(e)
try:
ExamplePythonTypes.value2 = 15
except Exception as e:
print(e)
instance = ExamplePythonTypes.new_instance()
dict_result = instance.get_dict()
dict_result['key2'] = 'value2'
instance.print_dict(dict_result)
dict_result = instance.get_dict_2()
dict_result['key2'] = 'value2'
instance.print_dict_2(dict_result)
set_result = instance.get_set()
set_result.add('key3')
instance.print_set(set_result)
set_result = instance.get_set2()
set_result.add('key3')
instance.print_set_2(set_result)
list_result = instance.get_list()
list_result.append('value2')
instance.print_list(list_result)
list_result = instance.get_list_2()
list_result.append('value2')
instance.print_list_2(list_result)
array_result = instance.get_array()
print(array_result)
instance.print_array(array_result)
try:
instance.throw_exception()
except Exception as e:
print(e)
print(instance.pair_passthrough((True, "test")))
print(instance.tuple_passthrough((True, "test", 5)))
print(pydoc.render_doc(ExamplePythonTypes, "Help on %s"))
print("__name__(example) = %s" % example.__name__)
print("__name__(example.ExamplePythonTypes) = %s" % ExamplePythonTypes.__name__)
print("__module__(example.ExamplePythonTypes) = %s" % ExamplePythonTypes.__module__)
print("__name__(example.ExamplePythonTypes.get_set) = %s" % ExamplePythonTypes.get_set.__name__)
print("__module__(example.ExamplePythonTypes.get_set) = %s" % ExamplePythonTypes.get_set.__module__)
print(instance.get_bytes_from_string().decode())
print(instance.get_bytes_from_str().decode())
print(instance.get_str_from_string().encode().decode())
print(instance.get_str_from_bytes().encode().decode())
class A(object):
__str__ = lambda _: 'this is a str'
__repr__ = lambda _: 'this is a repr'
instance.test_print(A())
from example import ConstructorStats
cstats = ConstructorStats.get(ExamplePythonTypes)
print("Instances not destroyed:", cstats.alive())
instance = None
print("Instances not destroyed:", cstats.alive())
15
5
example.ExamplePythonTypes: No constructor defined!
can't set attribute
### ExamplePythonTypes @ 0x1045b80 created via new_instance
key: key2, value=value2
key: key, value=value
key: key, value=value
key: key2, value=value2
key: key3
key: key2
key: key1
key: key1
key: key2
key: key3
Entry at positon 0: value
list item 0: overwritten
list item 1: value2
list item 0: value
list item 1: value2
[u'array entry 1', u'array entry 2']
array item 0: array entry 1
array item 1: array entry 2
This exception was intentionally thrown.
(u'test', True)
(5L, u'test', True)
Help on class ExamplePythonTypes in module example
class EExxaammpplleePPyytthhoonnTTyyppeess(__builtin__.object)
| Example 2 documentation
|
| Methods defined here:
|
| ____iinniitt____(...)
| x.__init__(...) initializes x; see help(type(x)) for signature
|
| ggeett__aarrrraayy(...)
|
| Signature : (example.ExamplePythonTypes) -> List[unicode[2]]
| Return a C++ array
|
| ggeett__ddiicctt(...)
| Signature : (example.ExamplePythonTypes) -> dict
|
| Return a Python dictionary
|
| ggeett__ddiicctt__22(...)
|
| Signature : (example.ExamplePythonTypes) -> Dict[unicode, unicode]
| Return a C++ dictionary
|
| ggeett__lliisstt(...)
| Signature : (example.ExamplePythonTypes) -> list
|
| Return a Python list
|
| ggeett__lliisstt__22(...)
|
| Signature : (example.ExamplePythonTypes) -> List[unicode]
| Return a C++ list
|
| ggeett__sseett(...)
| Signature : (example.ExamplePythonTypes) -> set
|
| Return a Python set
|
| ggeett__sseett22(...)
| Signature : (example.ExamplePythonTypes) -> set
|
| Return a C++ set
|
| ppaaiirr__ppaasssstthhrroouugghh(...)
|
| Signature : (example.ExamplePythonTypes, Tuple[bool, unicode]) -> Tuple[unicode, bool]
| Return a pair in reversed order
|
| pprriinntt__aarrrraayy(...)
|
| Signature : (example.ExamplePythonTypes, List[unicode[2]]) -> None
| Print entries of a C++ array
|
| pprriinntt__ddiicctt(...)
|
| Signature : (example.ExamplePythonTypes, dict) -> None
| Print entries of a Python dictionary
|
| pprriinntt__ddiicctt__22(...)
|
| Signature : (example.ExamplePythonTypes, Dict[unicode, unicode]) -> None
| Print entries of a C++ dictionary
|
| pprriinntt__lliisstt(...)
|
| Signature : (example.ExamplePythonTypes, list) -> None
| Print entries of a Python list
|
| pprriinntt__lliisstt__22(...)
|
| Signature : (example.ExamplePythonTypes, List[unicode]) -> None
| Print entries of a C++ list
|
| pprriinntt__sseett(...)
|
| Signature : (example.ExamplePythonTypes, set) -> None
| Print entries of a Python set
|
| pprriinntt__sseett__22(...)
|
| Signature : (example.ExamplePythonTypes, Set[unicode]) -> None
| Print entries of a C++ set
|
| tthhrrooww__eexxcceeppttiioonn(...)
|
| Signature : (example.ExamplePythonTypes) -> None
| Throw an exception
|
| ttuuppllee__ppaasssstthhrroouugghh(...)
|
| Signature : (example.ExamplePythonTypes, Tuple[bool, unicode, int]) -> Tuple[int, unicode, bool]
| Return a triple in reversed order
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| ____nneeww____ = <built-in method __new__ of example.ExamplePythonTypes__Meta object>
| T.__new__(S, ...) -> a new object with type S, a subtype of T
|
| nneeww__iinnssttaannccee = <built-in method new_instance of PyCapsule object>
| Signature : () -> example.ExamplePythonTypes
|
| Return an instance
__name__(example) = example
__name__(example.ExamplePythonTypes) = ExamplePythonTypes
__module__(example.ExamplePythonTypes) = example
__name__(example.ExamplePythonTypes.get_set) = get_set
__module__(example.ExamplePythonTypes.get_set) = example
foo
bar
baz
boo
this is a str
this is a repr
Instances not destroyed: 1
### ExamplePythonTypes @ 0x1045b80 destroyed
Instances not destroyed: 0
#!/usr/bin/env python
from __future__ import print_function
import sys
sys.path.append('.')
from example import Sequence, StringMap
s = Sequence(5)
print("s = " + str(s))
print("len(s) = " + str(len(s)))
print("s[0], s[3] = %f %f" % (s[0], s[3]))
print('12.34 in s: ' + str(12.34 in s))
s[0], s[3] = 12.34, 56.78
print('12.34 in s: ' + str(12.34 in s))
print("s[0], s[3] = %f %f" % (s[0], s[3]))
rev = reversed(s)
rev2 = s[::-1]
print("rev[0], rev[1], rev[2], rev[3], rev[4] = %f %f %f %f %f" % (rev[0], rev[1], rev[2], rev[3], rev[4]))
for i in rev:
print(i, end=' ')
print('')
for i in rev2:
print(i, end=' ')
print('')
print(rev == rev2)
rev[0::2] = Sequence([2.0, 2.0, 2.0])
for i in rev:
print(i, end=' ')
print('')
m = StringMap({ 'hi': 'bye', 'black': 'white' })
print(m['hi'])
print(len(m))
print(m['black'])
try:
print(m['orange'])
print('Error: should have thrown exception')
except KeyError:
pass
m['orange'] = 'banana'
print(m['orange'])
for k in m:
print("key = %s, value = %s" % (k, m[k]))
for k,v in m.items():
print("item: (%s, %s)" % (k,v))
from example import ConstructorStats
cstats = ConstructorStats.get(Sequence)
print("Instances not destroyed:", cstats.alive())
s = None
print("Instances not destroyed:", cstats.alive())
rev = None
print("Instances not destroyed:", cstats.alive())
rev2 = None
print("Instances not destroyed:", cstats.alive())
print("Constructor values:", cstats.values())
print("Default constructions:", cstats.default_constructions)
print("Copy constructions:", cstats.copy_constructions)
print("Move constructions:", cstats.move_constructions >= 1)
print("Copy assignments:", cstats.copy_assignments)
print("Move assignments:", cstats.move_assignments)
### Sequence @ 0x1535b00 created of size 5
s = <example.Sequence object at 0x7efc73cfa4e0>
len(s) = 5
s[0], s[3] = 0.000000 0.000000
12.34 in s: False
12.34 in s: True
s[0], s[3] = 12.340000 56.779999
### Sequence @ 0x7fff22a45068 created of size 5
### Sequence @ 0x1538b90 created via move constructor
### Sequence @ 0x7fff22a45068 destroyed
### Sequence @ 0x1538bf0 created of size 5
rev[0], rev[1], rev[2], rev[3], rev[4] = 0.000000 56.779999 0.000000 0.000000 12.340000
0.0 56.779998779296875 0.0 0.0 12.34000015258789
0.0 56.779998779296875 0.0 0.0 12.34000015258789
True
### Sequence @ 0x1b4d1f0 created of size 3 from std::vector
### Sequence @ 0x1b4d1f0 destroyed
2.0 56.779998779296875 2.0 0.0 2.0
bye
2
white
banana
key = orange, value = banana
key = hi, value = bye
key = black, value = white
item: (orange, banana)
item: (hi, bye)
item: (black, white)
Instances not destroyed: 3
### Sequence @ 0x1535b00 destroyed
Instances not destroyed: 2
### Sequence @ 0x1538b90 destroyed
Instances not destroyed: 1
### Sequence @ 0x1538bf0 destroyed
Instances not destroyed: 0
Constructor values: ['of size', '5', 'of size', '5', 'of size', '5', 'of size', '3', 'from std::vector']
Default constructions: 0
Copy constructions: 0
Move constructions: True
Copy assignments: 0
Move assignments: 0
#!/usr/bin/env python
from __future__ import print_function
import sys
sys.path.append('.')
from example import MyObject1
from example import MyObject2
from example import MyObject3
from example import make_object_1
from example import make_object_2
from example import make_myobject1_1
from example import make_myobject1_2
from example import make_myobject2_1
from example import make_myobject2_2
from example import make_myobject3_1
from example import make_myobject3_2
from example import print_object_1
from example import print_object_2
from example import print_object_3
from example import print_object_4
from example import print_myobject1_1
from example import print_myobject1_2
from example import print_myobject1_3
from example import print_myobject1_4
from example import print_myobject2_1
from example import print_myobject2_2
from example import print_myobject2_3
from example import print_myobject2_4
from example import print_myobject3_1
from example import print_myobject3_2
from example import print_myobject3_3
from example import print_myobject3_4
for o in [make_object_1(), make_object_2(), MyObject1(3)]:
print("Reference count = %i" % o.getRefCount())
print_object_1(o)
print_object_2(o)
print_object_3(o)
print_object_4(o)
for o in [make_myobject1_1(), make_myobject1_2(), MyObject1(6), 7]:
print(o)
if not isinstance(o, int):
print_object_1(o)
print_object_2(o)
print_object_3(o)
print_object_4(o)
print_myobject1_1(o)
print_myobject1_2(o)
print_myobject1_3(o)
print_myobject1_4(o)
for o in [MyObject2(8), make_myobject2_1(), make_myobject2_2()]:
print(o)
print_myobject2_1(o)
print_myobject2_2(o)
print_myobject2_3(o)
print_myobject2_4(o)
for o in [MyObject3(9), make_myobject3_1(), make_myobject3_2()]:
print(o)
print_myobject3_1(o)
print_myobject3_2(o)
print_myobject3_3(o)
print_myobject3_4(o)
from example import ConstructorStats, cstats_ref, Object
cstats = [ConstructorStats.get(Object), ConstructorStats.get(MyObject1),
ConstructorStats.get(MyObject2), ConstructorStats.get(MyObject3),
cstats_ref()]
print("Instances not destroyed:", [x.alive() for x in cstats])
o = None
print("Instances not destroyed:", [x.alive() for x in cstats])
print("Object value constructions:", [x.values() for x in cstats])
print("Default constructions:", [x.default_constructions for x in cstats])
print("Copy constructions:", [x.copy_constructions for x in cstats])
#print("Move constructions:", [x.move_constructions >= 0 for x in cstats]) # Doesn't invoke any
print("Copy assignments:", [x.copy_assignments for x in cstats])
print("Move assignments:", [x.move_assignments for x in cstats])
### Object @ 0xdeffd0 created via default constructor
### MyObject1 @ 0xdeffd0 created MyObject1[1]
### ref<MyObject1> @ 0x7f6a2e03c4a8 created from pointer 0xdeffd0
### Object @ 0xe43f50 created via default constructor
### MyObject1 @ 0xe43f50 created MyObject1[2]
### ref<Object> @ 0x7fff136845d0 created from pointer 0xe43f50
### ref<MyObject1> @ 0x7f6a2c32aad8 created via copy constructor with pointer 0xe43f50
### ref<Object> @ 0x7fff136845d0 destroyed
### Object @ 0xee8cf0 created via default constructor
### MyObject1 @ 0xee8cf0 created MyObject1[3]
### ref<MyObject1> @ 0x7f6a2c32ab08 created from pointer 0xee8cf0
Reference count = 1
MyObject1[1]
### ref<Object> @ 0x7fff136845c8 created via default constructor
### ref<Object> @ 0x7fff136845c8 assigned via copy assignment pointer 0xdeffd0
### ref<Object> @ 0x7fff136845a8 created via copy constructor with pointer 0xdeffd0
MyObject1[1]
### ref<Object> @ 0x7fff136845a8 destroyed
### ref<Object> @ 0x7fff136845c8 destroyed
### ref<Object> @ 0x7fff136845c8 created via default constructor
### ref<Object> @ 0x7fff136845c8 assigned via copy assignment pointer 0xdeffd0
MyObject1[1]
### ref<Object> @ 0x7fff136845c8 destroyed
### ref<Object> @ 0x7fff136845c8 created via default constructor
### ref<Object> @ 0x7fff136845c8 assigned via copy assignment pointer 0xdeffd0
MyObject1[1]
### ref<Object> @ 0x7fff136845c8 destroyed
Reference count = 1
MyObject1[2]
### ref<Object> @ 0x7fff136845c8 created via default constructor
### ref<Object> @ 0x7fff136845c8 assigned via copy assignment pointer 0xe43f50
### ref<Object> @ 0x7fff136845a8 created via copy constructor with pointer 0xe43f50
MyObject1[2]
### ref<Object> @ 0x7fff136845a8 destroyed
### ref<Object> @ 0x7fff136845c8 destroyed
### ref<Object> @ 0x7fff136845c8 created via default constructor
### ref<Object> @ 0x7fff136845c8 assigned via copy assignment pointer 0xe43f50
MyObject1[2]
### ref<Object> @ 0x7fff136845c8 destroyed
### ref<Object> @ 0x7fff136845c8 created via default constructor
### ref<Object> @ 0x7fff136845c8 assigned via copy assignment pointer 0xe43f50
MyObject1[2]
### ref<Object> @ 0x7fff136845c8 destroyed
Reference count = 1
MyObject1[3]
### ref<Object> @ 0x7fff136845c8 created via default constructor
### ref<Object> @ 0x7fff136845c8 assigned via copy assignment pointer 0xee8cf0
### ref<Object> @ 0x7fff136845a8 created via copy constructor with pointer 0xee8cf0
MyObject1[3]
### ref<Object> @ 0x7fff136845a8 destroyed
### ref<Object> @ 0x7fff136845c8 destroyed
### ref<Object> @ 0x7fff136845c8 created via default constructor
### ref<Object> @ 0x7fff136845c8 assigned via copy assignment pointer 0xee8cf0
MyObject1[3]
### ref<Object> @ 0x7fff136845c8 destroyed
### ref<Object> @ 0x7fff136845c8 created via default constructor
### ref<Object> @ 0x7fff136845c8 assigned via copy assignment pointer 0xee8cf0
MyObject1[3]
### ref<Object> @ 0x7fff136845c8 destroyed
### MyObject1 @ 0xe43f50 destroyed
### Object @ 0xe43f50 destroyed
### ref<MyObject1> @ 0x7f6a2c32aad8 destroyed
### MyObject1 @ 0xdeffd0 destroyed
### Object @ 0xdeffd0 destroyed
### ref<MyObject1> @ 0x7f6a2e03c4a8 destroyed
### Object @ 0xee8310 created via default constructor
### MyObject1 @ 0xee8310 created MyObject1[4]
### ref<MyObject1> @ 0x7f6a2e03c4a8 created from pointer 0xee8310
### Object @ 0xee8470 created via default constructor
### MyObject1 @ 0xee8470 created MyObject1[5]
### ref<MyObject1> @ 0x7fff136845d0 created from pointer 0xee8470
### ref<MyObject1> @ 0x7f6a2c32aad8 created via copy constructor with pointer 0xee8470
### ref<MyObject1> @ 0x7fff136845d0 destroyed
### Object @ 0xee95a0 created via default constructor
### MyObject1 @ 0xee95a0 created MyObject1[6]
### ref<MyObject1> @ 0x7f6a2c32ab38 created from pointer 0xee95a0
### MyObject1 @ 0xee8cf0 destroyed
### Object @ 0xee8cf0 destroyed
### ref<MyObject1> @ 0x7f6a2c32ab08 destroyed
<example.MyObject1 object at 0x7f6a2e03c480>
MyObject1[4]
### ref<Object> @ 0x7fff136845c8 created via default constructor
### ref<Object> @ 0x7fff136845c8 assigned via copy assignment pointer 0xee8310
### ref<Object> @ 0x7fff136845a8 created via copy constructor with pointer 0xee8310
MyObject1[4]
### ref<Object> @ 0x7fff136845a8 destroyed
### ref<Object> @ 0x7fff136845c8 destroyed
### ref<Object> @ 0x7fff136845c8 created via default constructor
### ref<Object> @ 0x7fff136845c8 assigned via copy assignment pointer 0xee8310
MyObject1[4]
### ref<Object> @ 0x7fff136845c8 destroyed
### ref<Object> @ 0x7fff136845c8 created via default constructor
### ref<Object> @ 0x7fff136845c8 assigned via copy assignment pointer 0xee8310
MyObject1[4]
### ref<Object> @ 0x7fff136845c8 destroyed
MyObject1[4]
### ref<MyObject1> @ 0x7fff136845c8 created via default constructor
### ref<MyObject1> @ 0x7fff136845c8 assigned via copy assignment pointer 0xee8310
### ref<MyObject1> @ 0x7fff136845a8 created via copy constructor with pointer 0xee8310
MyObject1[4]
### ref<MyObject1> @ 0x7fff136845a8 destroyed
### ref<MyObject1> @ 0x7fff136845c8 destroyed
### ref<MyObject1> @ 0x7fff136845c8 created via default constructor
### ref<MyObject1> @ 0x7fff136845c8 assigned via copy assignment pointer 0xee8310
MyObject1[4]
### ref<MyObject1> @ 0x7fff136845c8 destroyed
### ref<MyObject1> @ 0x7fff136845c8 created via default constructor
### ref<MyObject1> @ 0x7fff136845c8 assigned via copy assignment pointer 0xee8310
MyObject1[4]
### ref<MyObject1> @ 0x7fff136845c8 destroyed
<example.MyObject1 object at 0x7f6a2c32aab0>
MyObject1[5]
### ref<Object> @ 0x7fff136845c8 created via default constructor
### ref<Object> @ 0x7fff136845c8 assigned via copy assignment pointer 0xee8470
### ref<Object> @ 0x7fff136845a8 created via copy constructor with pointer 0xee8470
MyObject1[5]
### ref<Object> @ 0x7fff136845a8 destroyed
### ref<Object> @ 0x7fff136845c8 destroyed
### ref<Object> @ 0x7fff136845c8 created via default constructor
### ref<Object> @ 0x7fff136845c8 assigned via copy assignment pointer 0xee8470
MyObject1[5]
### ref<Object> @ 0x7fff136845c8 destroyed
### ref<Object> @ 0x7fff136845c8 created via default constructor
### ref<Object> @ 0x7fff136845c8 assigned via copy assignment pointer 0xee8470
MyObject1[5]
### ref<Object> @ 0x7fff136845c8 destroyed
MyObject1[5]
### ref<MyObject1> @ 0x7fff136845c8 created via default constructor
### ref<MyObject1> @ 0x7fff136845c8 assigned via copy assignment pointer 0xee8470
### ref<MyObject1> @ 0x7fff136845a8 created via copy constructor with pointer 0xee8470
MyObject1[5]
### ref<MyObject1> @ 0x7fff136845a8 destroyed
### ref<MyObject1> @ 0x7fff136845c8 destroyed
### ref<MyObject1> @ 0x7fff136845c8 created via default constructor
### ref<MyObject1> @ 0x7fff136845c8 assigned via copy assignment pointer 0xee8470
MyObject1[5]
### ref<MyObject1> @ 0x7fff136845c8 destroyed
### ref<MyObject1> @ 0x7fff136845c8 created via default constructor
### ref<MyObject1> @ 0x7fff136845c8 assigned via copy assignment pointer 0xee8470
MyObject1[5]
### ref<MyObject1> @ 0x7fff136845c8 destroyed
<example.MyObject1 object at 0x7f6a2c32ab10>
MyObject1[6]
### ref<Object> @ 0x7fff136845c8 created via default constructor
### ref<Object> @ 0x7fff136845c8 assigned via copy assignment pointer 0xee95a0
### ref<Object> @ 0x7fff136845a8 created via copy constructor with pointer 0xee95a0
MyObject1[6]
### ref<Object> @ 0x7fff136845a8 destroyed
### ref<Object> @ 0x7fff136845c8 destroyed
### ref<Object> @ 0x7fff136845c8 created via default constructor
### ref<Object> @ 0x7fff136845c8 assigned via copy assignment pointer 0xee95a0
MyObject1[6]
### ref<Object> @ 0x7fff136845c8 destroyed
### ref<Object> @ 0x7fff136845c8 created via default constructor
### ref<Object> @ 0x7fff136845c8 assigned via copy assignment pointer 0xee95a0
MyObject1[6]
### ref<Object> @ 0x7fff136845c8 destroyed
MyObject1[6]
### ref<MyObject1> @ 0x7fff136845c8 created via default constructor
### ref<MyObject1> @ 0x7fff136845c8 assigned via copy assignment pointer 0xee95a0
### ref<MyObject1> @ 0x7fff136845a8 created via copy constructor with pointer 0xee95a0
MyObject1[6]
### ref<MyObject1> @ 0x7fff136845a8 destroyed
### ref<MyObject1> @ 0x7fff136845c8 destroyed
### ref<MyObject1> @ 0x7fff136845c8 created via default constructor
### ref<MyObject1> @ 0x7fff136845c8 assigned via copy assignment pointer 0xee95a0
MyObject1[6]
### ref<MyObject1> @ 0x7fff136845c8 destroyed
### ref<MyObject1> @ 0x7fff136845c8 created via default constructor
### ref<MyObject1> @ 0x7fff136845c8 assigned via copy assignment pointer 0xee95a0
MyObject1[6]
### ref<MyObject1> @ 0x7fff136845c8 destroyed
7
### Object @ 0xee97f0 created via default constructor
### MyObject1 @ 0xee97f0 created MyObject1[7]
### ref<MyObject1> @ 0x7f6a2c32ab08 created from pointer 0xee97f0
MyObject1[7]
### MyObject1 @ 0xee97f0 destroyed
### Object @ 0xee97f0 destroyed
### ref<MyObject1> @ 0x7f6a2c32ab08 destroyed
### ref<MyObject1> @ 0x7fff136845c8 created via default constructor
### Object @ 0xee99e0 created via default constructor
### MyObject1 @ 0xee99e0 created MyObject1[7]
### ref<MyObject1> @ 0x7f6a2c32ab08 created from pointer 0xee99e0
### ref<MyObject1> @ 0x7fff136845c8 assigned via copy assignment pointer 0xee99e0
### ref<MyObject1> @ 0x7fff136845a8 created via copy constructor with pointer 0xee99e0
MyObject1[7]
### ref<MyObject1> @ 0x7fff136845a8 destroyed
### ref<MyObject1> @ 0x7fff136845c8 destroyed
### MyObject1 @ 0xee99e0 destroyed
### Object @ 0xee99e0 destroyed
### ref<MyObject1> @ 0x7f6a2c32ab08 destroyed
### ref<MyObject1> @ 0x7fff136845c8 created via default constructor
### Object @ 0xee97f0 created via default constructor
### MyObject1 @ 0xee97f0 created MyObject1[7]
### ref<MyObject1> @ 0x7f6a2c32ab08 created from pointer 0xee97f0
### ref<MyObject1> @ 0x7fff136845c8 assigned via copy assignment pointer 0xee97f0
MyObject1[7]
### ref<MyObject1> @ 0x7fff136845c8 destroyed
### MyObject1 @ 0xee97f0 destroyed
### Object @ 0xee97f0 destroyed
### ref<MyObject1> @ 0x7f6a2c32ab08 destroyed
### ref<MyObject1> @ 0x7fff136845c8 created via default constructor
### Object @ 0xee99e0 created via default constructor
### MyObject1 @ 0xee99e0 created MyObject1[7]
### ref<MyObject1> @ 0x7f6a2c32ab08 created from pointer 0xee99e0
### ref<MyObject1> @ 0x7fff136845c8 assigned via copy assignment pointer 0xee99e0
MyObject1[7]
### ref<MyObject1> @ 0x7fff136845c8 destroyed
### MyObject1 @ 0xee99e0 destroyed
### Object @ 0xee99e0 destroyed
### ref<MyObject1> @ 0x7f6a2c32ab08 destroyed
### MyObject1 @ 0xee95a0 destroyed
### Object @ 0xee95a0 destroyed
### ref<MyObject1> @ 0x7f6a2c32ab38 destroyed
### MyObject1 @ 0xee8470 destroyed
### Object @ 0xee8470 destroyed
### ref<MyObject1> @ 0x7f6a2c32aad8 destroyed
### MyObject1 @ 0xee8310 destroyed
### Object @ 0xee8310 destroyed
### ref<MyObject1> @ 0x7f6a2e03c4a8 destroyed
### MyObject2 @ 0xe43f50 created MyObject2[8]
### MyObject2 @ 0xee95a0 created MyObject2[6]
### MyObject2 @ 0xee95d0 created MyObject2[7]
<example.MyObject2 object at 0x7f6a2dfc8768>
MyObject2[8]
MyObject2[8]
MyObject2[8]
MyObject2[8]
<example.MyObject2 object at 0x7f6a2dfc86c0>
MyObject2[6]
MyObject2[6]
MyObject2[6]
MyObject2[6]
<example.MyObject2 object at 0x7f6a2c32d030>
MyObject2[7]
MyObject2[7]
MyObject2[7]
MyObject2[7]
### MyObject2 @ 0xee95a0 destroyed
### MyObject2 @ 0xe43f50 destroyed
### MyObject3 @ 0xee9ac0 created MyObject3[9]
### MyObject3 @ 0xe43f90 created MyObject3[8]
### MyObject3 @ 0xeea7d0 created MyObject3[9]
### MyObject2 @ 0xee95d0 destroyed
<example.MyObject3 object at 0x7f6a2dfc8768>
MyObject3[9]
MyObject3[9]
MyObject3[9]
MyObject3[9]
<example.MyObject3 object at 0x7f6a2dfc86c0>
MyObject3[8]
MyObject3[8]
MyObject3[8]
MyObject3[8]
<example.MyObject3 object at 0x7f6a2c32d068>
MyObject3[9]
MyObject3[9]
MyObject3[9]
MyObject3[9]
### MyObject3 @ 0xe43f90 destroyed
### MyObject3 @ 0xee9ac0 destroyed
Instances not destroyed: [0, 0, 0, 1, 0]
### MyObject3 @ 0xeea7d0 destroyed
Instances not destroyed: [0, 0, 0, 0, 0]
Object value constructions: [[], ['MyObject1[1]', 'MyObject1[2]', 'MyObject1[3]', 'MyObject1[4]', 'MyObject1[5]', 'MyObject1[6]', 'MyObject1[7]', 'MyObject1[7]', 'MyObject1[7]', 'MyObject1[7]'], ['MyObject2[8]', 'MyObject2[6]', 'MyObject2[7]'], ['MyObject3[9]', 'MyObject3[8]', 'MyObject3[9]'], ['from pointer', 'from pointer', 'from pointer', 'from pointer', 'from pointer', 'from pointer', 'from pointer', 'from pointer', 'from pointer', 'from pointer']]
Default constructions: [10, 0, 0, 0, 30]
Copy constructions: [0, 0, 0, 0, 12]
Copy assignments: [0, 0, 0, 0, 30]
Move assignments: [0, 0, 0, 0, 0]
#!/usr/bin/env python
from __future__ import print_function
from example import VectorInt, El, VectorEl, VectorVectorEl, VectorBool
v_int = VectorInt([0, 0])
print(len(v_int))
print(bool(v_int))
v_int2 = VectorInt([0, 0])
print(v_int == v_int2)
v_int2[1] = 1
print(v_int != v_int2)
v_int2.append(2)
v_int2.append(3)
v_int2.insert(0, 1)
v_int2.insert(0, 2)
v_int2.insert(0, 3)
print(v_int2)
v_int.append(99)
v_int2[2:-2] = v_int
print(v_int2)
del v_int2[1:3]
print(v_int2)
del v_int2[0]
print(v_int2)
v_a = VectorEl()
v_a.append(El(1))
v_a.append(El(2))
print(v_a)
vv_a = VectorVectorEl()
vv_a.append(v_a)
vv_b = vv_a[0]
print(vv_b)
vv_c = VectorBool()
for i in range(10):
vv_c.append(i % 2 == 0)
for i in range(10):
if vv_c[i] != (i % 2 == 0):
print("Error!")
print(vv_c)
2
True
True
True
VectorInt[3, 2, 1, 0, 1, 2, 3]
VectorInt[3, 2, 0, 0, 99, 2, 3]
VectorInt[3, 0, 99, 2, 3]
VectorInt[0, 99, 2, 3]
VectorEl[El{1}, El{2}]
VectorEl[El{1}, El{2}]
VectorBool[1, 0, 1, 0, 1, 0, 1, 0, 1, 0]
#!/usr/bin/env python
from __future__ import print_function
import sys
sys.path.append('.')
from example import ExampleVirt, runExampleVirt, runExampleVirtVirtual, runExampleVirtBool
from example import A_Repeat, B_Repeat, C_Repeat, D_Repeat, A_Tpl, B_Tpl, C_Tpl, D_Tpl
from example import NCVirt, NonCopyable, Movable
class ExtendedExampleVirt(ExampleVirt):
def __init__(self, state):
super(ExtendedExampleVirt, self).__init__(state + 1)
self.data = "Hello world"
def run(self, value):
print('ExtendedExampleVirt::run(%i), calling parent..' % value)
return super(ExtendedExampleVirt, self).run(value + 1)
def run_bool(self):
print('ExtendedExampleVirt::run_bool()')
return False
def pure_virtual(self):
print('ExtendedExampleVirt::pure_virtual(): %s' % self.data)
ex12 = ExampleVirt(10)
print(runExampleVirt(ex12, 20))
try:
runExampleVirtVirtual(ex12)
except Exception as e:
print("Caught expected exception: " + str(e))
ex12p = ExtendedExampleVirt(10)
print(runExampleVirt(ex12p, 20))
print(runExampleVirtBool(ex12p))
runExampleVirtVirtual(ex12p)
class VI_AR(A_Repeat):
def unlucky_number(self):
return 99
class VI_AT(A_Tpl):
def unlucky_number(self):
return 999
class VI_CR(C_Repeat):
def lucky_number(self):
return C_Repeat.lucky_number(self) + 1.25
class VI_CT(C_Tpl):
pass
class VI_CCR(VI_CR):
def lucky_number(self):
return VI_CR.lucky_number(self) * 10
class VI_CCT(VI_CT):
def lucky_number(self):
return VI_CT.lucky_number(self) * 1000
class VI_DR(D_Repeat):
def unlucky_number(self):
return 123
def lucky_number(self):
return 42.0
class VI_DT(D_Tpl):
def say_something(self, times):
print("VI_DT says:" + (' quack' * times))
def unlucky_number(self):
return 1234
def lucky_number(self):
return -4.25
classes = [
# A_Repeat, A_Tpl, # abstract (they have a pure virtual unlucky_number)
VI_AR, VI_AT,
B_Repeat, B_Tpl,
C_Repeat, C_Tpl,
VI_CR, VI_CT, VI_CCR, VI_CCT,
D_Repeat, D_Tpl, VI_DR, VI_DT
]
for cl in classes:
print("\n%s:" % cl.__name__)
obj = cl()
obj.say_something(3)
print("Unlucky = %d" % obj.unlucky_number())
if hasattr(obj, "lucky_number"):
print("Lucky = %.2f" % obj.lucky_number())
class NCVirtExt(NCVirt):
def get_noncopyable(self, a, b):
# Constructs and returns a new instance:
nc = NonCopyable(a*a, b*b)
return nc
def get_movable(self, a, b):
# Return a referenced copy
self.movable = Movable(a, b)
return self.movable
class NCVirtExt2(NCVirt):
def get_noncopyable(self, a, b):
# Keep a reference: this is going to throw an exception
self.nc = NonCopyable(a, b)
return self.nc
def get_movable(self, a, b):
# Return a new instance without storing it
return Movable(a, b)
ncv1 = NCVirtExt()
print("2^2 * 3^2 =")
ncv1.print_nc(2, 3)
print("4 + 5 =")
ncv1.print_movable(4, 5)
ncv2 = NCVirtExt2()
print("7 + 7 =")
ncv2.print_movable(7, 7)
try:
ncv2.print_nc(9, 9)
print("Something's wrong: exception not raised!")
except RuntimeError as e:
# Don't print the exception message here because it differs under debug/non-debug mode
print("Caught expected exception")
from example import ConstructorStats
del ex12
del ex12p
del obj
del ncv1
del ncv2
cstats = [ConstructorStats.get(ExampleVirt), ConstructorStats.get(NonCopyable), ConstructorStats.get(Movable)]
print("Instances not destroyed:", [x.alive() for x in cstats])
print("Constructor values:", [x.values() for x in cstats])
print("Copy constructions:", [x.copy_constructions for x in cstats])
print("Move constructions:", [cstats[i].move_constructions >= 1 for i in range(1, len(cstats))])
### ExampleVirt @ 0x2073a90 created 10
Original implementation of ExampleVirt::run(state=10, value=20)
30
Caught expected exception: Tried to call pure virtual function "ExampleVirt::pure_virtual"
### ExampleVirt @ 0x2076a00 created 11
ExtendedExampleVirt::run(20), calling parent..
Original implementation of ExampleVirt::run(state=11, value=21)
32
ExtendedExampleVirt::run_bool()
False
ExtendedExampleVirt::pure_virtual(): Hello world
VI_AR:
hihihi
Unlucky = 99
VI_AT:
hihihi
Unlucky = 999
B_Repeat:
B says hi 3 times
Unlucky = 13
Lucky = 7.00
B_Tpl:
B says hi 3 times
Unlucky = 13
Lucky = 7.00
C_Repeat:
B says hi 3 times
Unlucky = 4444
Lucky = 888.00
C_Tpl:
B says hi 3 times
Unlucky = 4444
Lucky = 888.00
VI_CR:
B says hi 3 times
Unlucky = 4444
Lucky = 889.25
VI_CT:
B says hi 3 times
Unlucky = 4444
Lucky = 888.00
VI_CCR:
B says hi 3 times
Unlucky = 4444
Lucky = 8892.50
VI_CCT:
B says hi 3 times
Unlucky = 4444
Lucky = 888000.00
D_Repeat:
B says hi 3 times
Unlucky = 4444
Lucky = 888.00
D_Tpl:
B says hi 3 times
Unlucky = 4444
Lucky = 888.00
VI_DR:
B says hi 3 times
Unlucky = 123
Lucky = 42.00
VI_DT:
VI_DT says: quack quack quack
Unlucky = 1234
Lucky = -4.25
2^2 * 3^2 =
### NonCopyable @ 0x207df10 created 4 9
### NonCopyable @ 0x7ffcfe866228 created via move constructor
### NonCopyable @ 0x207df10 destroyed
36
### NonCopyable @ 0x7ffcfe866228 destroyed
4 + 5 =
### Movable @ 0x207e230 created 4 5
### Movable @ 0x7ffcfe86624c created via copy constructor
9
### Movable @ 0x7ffcfe86624c destroyed
7 + 7 =
### Movable @ 0x20259e0 created 7 7
### Movable @ 0x7ffcfe86624c created via move constructor
### Movable @ 0x20259e0 destroyed
14
### Movable @ 0x7ffcfe86624c destroyed
### NonCopyable @ 0x2025a00 created 9 9
Caught expected exception
### ExampleVirt @ 0x2073a90 destroyed
### ExampleVirt @ 0x2076a00 destroyed
### Movable @ 0x207e230 destroyed
### NonCopyable @ 0x2025a00 destroyed
Instances not destroyed: [0, 0, 0]
Constructor values: [['10', '11'], ['4', '9', '9', '9'], ['4', '5', '7', '7']]
Copy constructions: [0, 0, 1]
Move constructions: [True, True]
#!/usr/bin/env python
from __future__ import print_function
import sys
sys.path.append('.')
from example.issues import print_cchar, print_char
from example.issues import DispatchIssue, dispatch_issue_go
from example.issues import Placeholder, return_vec_of_reference_wrapper
from example.issues import iterator_passthrough
from example.issues import ElementList, ElementA, print_element
from example.issues import expect_float, expect_int
from example.issues import A, call_f
from example.issues import StrIssue
from example.issues import NestA, NestB, NestC, print_NestA, print_NestB, print_NestC
import gc
print_cchar("const char *")
print_char('c')
class PyClass1(DispatchIssue):
def dispatch(self):
print("Yay..")
class PyClass2(DispatchIssue):
def dispatch(self):
try:
super(PyClass2, self).dispatch()
except Exception as e:
print("Failed as expected: " + str(e))
p = PyClass1()
dispatch_issue_go(p)
b = PyClass2()
dispatch_issue_go(b)
print(return_vec_of_reference_wrapper(Placeholder(4)))
print(list(iterator_passthrough(iter([3, 5, 7, 9, 11, 13, 15]))))
el = ElementList()
for i in range(10):
el.add(ElementA(i))
gc.collect()
for i, v in enumerate(el.get()):
print("%i==%i, " % (i, v.value()), end='')
print()
try:
print_element(None)
except Exception as e:
print("Failed as expected: " + str(e))
try:
print(expect_int(5.2))
except Exception as e:
print("Failed as expected: " + str(e))
print(expect_float(12))
class B(A):
def __init__(self):
super(B, self).__init__()
def f(self):
print("In python f()")
print("C++ version")
a = A()
call_f(a)
print("Python version")
b = B()
call_f(b)
print(StrIssue(3))
try:
print(StrIssue("no", "such", "constructor"))
except TypeError as e:
print("Failed as expected: " + str(e))
a = NestA()
b = NestB()
c = NestC()
a += 10
b.a += 100
c.b.a += 1000
b -= 1
c.b -= 3
c *= 7
print_NestA(a)
print_NestA(b.a)
print_NestA(c.b.a)
print_NestB(b)
print_NestB(c.b)
print_NestC(c)
abase = a.as_base()
print(abase.value)
a.as_base().value += 44
print(abase.value)
print(c.b.a.as_base().value)
c.b.a.as_base().value += 44
print(c.b.a.as_base().value)
del c
gc.collect()
del a # Should't delete while abase is still alive
gc.collect()
print(abase.value)
del abase
gc.collect()
const char *
c
Failed as expected: Tried to call pure virtual function "Base::dispatch"
Yay..
[Placeholder[1], Placeholder[2], Placeholder[3], Placeholder[4]]
[3, 5, 7, 9, 11, 13, 15]
0==0, 1==1, 2==2, 3==3, 4==4, 5==5, 6==6, 7==7, 8==8, 9==9,
Failed as expected: Incompatible function arguments. The following argument types are supported:
1. (arg0: example.issues.ElementA) -> None
Invoked with: None
Failed as expected: Incompatible function arguments. The following argument types are supported:
1. (arg0: int) -> int
Invoked with: 5.2
12.0
C++ version
A.f()
Python version
PyA.PyA()
PyA.f()
In python f()
StrIssue.__str__ called
StrIssue[3]
Failed as expected: Incompatible constructor arguments. The following argument types are supported:
1. example.issues.StrIssue(arg0: int)
2. example.issues.StrIssue()
Invoked with: no, such, constructor
### NestABase @ 0x15eb630 created via default constructor
### NestA @ 0x15eb630 created via default constructor
### NestABase @ 0x1704000 created via default constructor
### NestA @ 0x1704000 created via default constructor
### NestB @ 0x1704000 created via default constructor
### NestABase @ 0x1633110 created via default constructor
### NestA @ 0x1633110 created via default constructor
### NestB @ 0x1633110 created via default constructor
### NestC @ 0x1633110 created via default constructor
13
103
1003
3
1
35
-2
42
-2
42
### NestC @ 0x1633110 destroyed
### NestB @ 0x1633110 destroyed
### NestA @ 0x1633110 destroyed
### NestABase @ 0x1633110 destroyed
42
### NestA @ 0x15eb630 destroyed
### NestABase @ 0x15eb630 destroyed
### NestB @ 0x1704000 destroyed
### NestA @ 0x1704000 destroyed
### NestABase @ 0x1704000 destroyed
import sys
import os
import re
import subprocess
import difflib
remove_unicode_marker = re.compile(r'u(\'[^\']*\')')
remove_long_marker = re.compile(r'([0-9])L')
remove_hex = re.compile(r'0x[0-9a-fA-F]+')
shorten_floats = re.compile(r'([1-9][0-9]*\.[0-9]{4})[0-9]*')
def sanitize(lines):
lines = lines.split('\n')
for i in range(len(lines)):
line = lines[i]
if line.startswith(" |"):
line = ""
if line.startswith("### "):
# Constructor/destructor output. Useful for example, but unreliable across compilers;
# testing of proper construction/destruction occurs with ConstructorStats mechanism instead
line = ""
line = remove_unicode_marker.sub(r'\1', line)
line = remove_long_marker.sub(r'\1', line)
line = remove_hex.sub(r'0', line)
line = shorten_floats.sub(r'\1', line)
line = line.replace('__builtin__', 'builtins')
line = line.replace('example.', '')
line = line.replace('unicode', 'str')
line = line.replace('ExampleWithEnum.EMode', 'EMode')
line = line.replace('example.EMode', 'EMode')
line = line.replace('method of builtins.PyCapsule instance', '')
line = line.strip()
lines[i] = line
return '\n'.join(sorted([l for l in lines if l != ""]))
path = os.path.dirname(__file__)
if path != '':
os.chdir(path)
if len(sys.argv) < 2:
print("Syntax: %s <test name>" % sys.argv[0])
exit(0)
name = sys.argv[1]
try:
output_bytes = subprocess.check_output([sys.executable, "-u", name + ".py"],
stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as exc:
print('Test `{}` failed:\n{}\n'.format(name, '-' * 50))
print(exc.output.decode())
print('-' * 50)
sys.exit(1)
output = sanitize(output_bytes.decode('utf-8'))
reference = sanitize(open(name + '.ref', 'r').read())
if output == reference:
print('Test "%s" succeeded.' % name)
exit(0)
else:
print('Test "%s" FAILED!' % name)
print('--- output')
print('+++ reference')
print(''.join(difflib.ndiff(output.splitlines(True),
reference.splitlines(True))))
exit(-1)
#!/usr/bin/env python #!/usr/bin/env python
# Setup script for PyPI; use CMakeFile.txt to build the example application # Setup script for PyPI; use CMakeFile.txt to build extension modules
from setuptools import setup from setuptools import setup
from pybind11 import __version__ from pybind11 import __version__
......
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting tests build type to MinSizeRel as none was specified")
set(CMAKE_BUILD_TYPE MinSizeRel CACHE STRING "Choose the type of build" FORCE)
endif()
set(PYBIND11_TEST_FILES
test_buffers.cpp
test_callbacks.cpp
test_constants_and_functions.cpp
test_enum.cpp
test_eval.cpp
test_exceptions.cpp
test_inheritance.cpp
test_issues.cpp
test_keep_alive.cpp
test_kwargs_and_defaults.cpp
test_methods_and_attributes.cpp
test_modules.cpp
test_numpy_dtypes.cpp
test_numpy_vectorize.cpp
test_opaque_types.cpp
test_operator_overloading.cpp
test_pickling.cpp
test_python_types.cpp
test_sequences_and_iterators.cpp
test_smart_ptr.cpp
test_stl_binders.cpp
test_virtual_functions.cpp
)
# Check if Eigen is available
find_package(Eigen3 QUIET)
if(EIGEN3_FOUND)
list(APPEND PYBIND11_TEST_FILES test_eigen.cpp)
message(STATUS "Building tests with Eigen v${EIGEN3_VERSION}")
else()
message(STATUS "Building tests WITHOUT Eigen")
endif()
# Create the binding library
pybind11_add_module(pybind11_tests pybind11_tests.cpp ${PYBIND11_TEST_FILES})
pybind11_enable_warnings(pybind11_tests)
if(EIGEN3_FOUND)
target_include_directories(pybind11_tests PRIVATE ${EIGEN3_INCLUDE_DIR})
target_compile_definitions(pybind11_tests PRIVATE -DPYBIND11_TEST_EIGEN)
endif()
set(testdir ${PROJECT_SOURCE_DIR}/tests)
# Always write the output file directly into the 'tests' directory (even on MSVC)
if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
set_target_properties(pybind11_tests PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${testdir})
foreach(config ${CMAKE_CONFIGURATION_TYPES})
string(TOUPPER ${config} config)
set_target_properties(pybind11_tests PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config} ${testdir})
endforeach()
endif()
# Make sure pytest is found or try to install it if it's not found
macro(pybind11_execute_python)
execute_process(COMMAND ${PYTHON_EXECUTABLE} -m ${ARGN} OUTPUT_QUIET ERROR_QUIET
RESULT_VARIABLE pybind11_execute_python_error)
endmacro()
if(NOT PYBIND11_PYTEST_FOUND)
pybind11_execute_python(pytest --version --noconftest)
if(pybind11_execute_python_error)
message(STATUS "Installing pytest using pip")
pybind11_execute_python(pip install pytest)
if(pybind11_execute_python_error)
message(STATUS "Installing pytest using pip --user (fallback)")
pybind11_execute_python(pip install --user pytest)
endif()
pybind11_execute_python(pytest --version --noconftest)
if(pybind11_execute_python_error)
message(FATAL_ERROR "Running the tests requires pytest. Please install it manually.")
endif()
endif()
set(PYBIND11_PYTEST_FOUND TRUE CACHE INTERAL "")
endif()
# A single command to compile and run the tests
add_custom_target(pytest COMMAND ${PYTHON_EXECUTABLE} -m pytest -rws
DEPENDS pybind11_tests WORKING_DIRECTORY ${testdir})
"""pytest configuration
Extends output capture as needed by pybind11: ignore constructors, optional unordered lines.
Adds docstring and exceptions message sanitizers: ignore Python 2 vs 3 differences.
"""
import pytest
import textwrap
import difflib
import re
import os
import sys
import contextlib
_unicode_marker = re.compile(r'u(\'[^\']*\')')
_long_marker = re.compile(r'([0-9])L')
_hexadecimal = re.compile(r'0x[0-9a-fA-F]+')
def _strip_and_dedent(s):
"""For triple-quote strings"""
return textwrap.dedent(s.lstrip('\n').rstrip())
def _split_and_sort(s):
"""For output which does not require specific line order"""
return sorted(_strip_and_dedent(s).splitlines())
def _make_explanation(a, b):
"""Explanation for a failed assert -- the a and b arguments are List[str]"""
return ["--- actual / +++ expected"] + [line.strip('\n') for line in difflib.ndiff(a, b)]
class Output(object):
"""Basic output post-processing and comparison"""
def __init__(self, string):
self.string = string
self.explanation = []
def __str__(self):
return self.string
def __eq__(self, other):
# Ignore constructor/destructor output which is prefixed with "###"
a = [line for line in self.string.strip().splitlines() if not line.startswith("###")]
b = _strip_and_dedent(other).splitlines()
if a == b:
return True
else:
self.explanation = _make_explanation(a, b)
return False
class Unordered(Output):
"""Custom comparison for output without strict line ordering"""
def __eq__(self, other):
a = _split_and_sort(self.string)
b = _split_and_sort(other)
if a == b:
return True
else:
self.explanation = _make_explanation(a, b)
return False
class Capture(object):
def __init__(self, capfd):
self.capfd = capfd
self.out = ""
def _flush_stdout(self):
sys.stdout.flush()
os.fsync(sys.stdout.fileno()) # make sure C++ output is also read
return self.capfd.readouterr()[0]
def __enter__(self):
self._flush_stdout()
return self
def __exit__(self, *_):
self.out = self._flush_stdout()
def __eq__(self, other):
a = Output(self.out)
b = other
if a == b:
return True
else:
self.explanation = a.explanation
return False
def __str__(self):
return self.out
def __contains__(self, item):
return item in self.out
@property
def unordered(self):
return Unordered(self.out)
@pytest.fixture
def capture(capfd):
"""Extended `capfd` with context manager and custom equality operators"""
return Capture(capfd)
class SanitizedString(object):
def __init__(self, sanitizer):
self.sanitizer = sanitizer
self.string = ""
self.explanation = []
def __call__(self, thing):
self.string = self.sanitizer(thing)
return self
def __eq__(self, other):
a = self.string
b = _strip_and_dedent(other)
if a == b:
return True
else:
self.explanation = _make_explanation(a.splitlines(), b.splitlines())
return False
def _sanitize_general(s):
s = s.strip()
s = s.replace("pybind11_tests.", "m.")
s = s.replace("unicode", "str")
s = _long_marker.sub(r"\1", s)
s = _unicode_marker.sub(r"\1", s)
return s
def _sanitize_docstring(thing):
s = thing.__doc__
s = _sanitize_general(s)
return s
@pytest.fixture
def doc():
"""Sanitize docstrings and add custom failure explanation"""
return SanitizedString(_sanitize_docstring)
def _sanitize_message(thing):
s = str(thing)
s = _sanitize_general(s)
s = _hexadecimal.sub("0", s)
return s
@pytest.fixture
def msg():
"""Sanitize messages and add custom failure explanation"""
return SanitizedString(_sanitize_message)
# noinspection PyUnusedLocal
def pytest_assertrepr_compare(op, left, right):
"""Hook to insert custom failure explanation"""
if hasattr(left, 'explanation'):
return left.explanation
@contextlib.contextmanager
def suppress(exception):
"""Suppress the desired exception"""
try:
yield
except exception:
pass
def pytest_namespace():
"""Add import suppression and test requirements to `pytest` namespace"""
try:
import numpy as np
except ImportError:
np = None
try:
import scipy
except ImportError:
scipy = None
try:
from pybind11_tests import have_eigen
except ImportError:
have_eigen = False
skipif = pytest.mark.skipif
return {
'suppress': suppress,
'requires_numpy': skipif(not np, reason="numpy is not installed"),
'requires_scipy': skipif(not np, reason="scipy is not installed"),
'requires_eigen_and_numpy': skipif(not have_eigen or not np,
reason="eigen and/or numpy are not installed"),
'requires_eigen_and_scipy': skipif(not have_eigen or not scipy,
reason="eigen and/or scipy are not installed"),
}
#pragma once #pragma once
/* /*
example/constructor-stats.h -- framework for printing and tracking object tests/constructor_stats.h -- framework for printing and tracking object
instance lifetimes in example/test code. instance lifetimes in example/test code.
Copyright (c) 2016 Jason Rhinelander <jason@imaginary.ca> Copyright (c) 2016 Jason Rhinelander <jason@imaginary.ca>
...@@ -64,7 +64,7 @@ inspection/testing in python) by using the functions with `print_` replaced with ...@@ -64,7 +64,7 @@ inspection/testing in python) by using the functions with `print_` replaced with
*/ */
#include "example.h" #include "pybind11_tests.h"
#include <unordered_map> #include <unordered_map>
#include <list> #include <list>
#include <typeindex> #include <typeindex>
...@@ -117,9 +117,12 @@ public: ...@@ -117,9 +117,12 @@ public:
_values.push_back(oss.str()); _values.push_back(oss.str());
value(std::forward<Tmore>(args)...); value(std::forward<Tmore>(args)...);
} }
// Move out stored values
py::list values() { py::list values() {
py::list l; py::list l;
for (const auto &v : _values) l.append(py::cast(v)); for (const auto &v : _values) l.append(py::cast(v));
_values.clear();
return l; return l;
} }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
#define __OBJECT_H #define __OBJECT_H
#include <atomic> #include <atomic>
#include "constructor-stats.h" #include "constructor_stats.h"
/// Reference counted object base class /// Reference counted object base class
class Object { class Object {
......
/* /*
example/example.cpp -- pybind example plugin tests/pybind11_tests.cpp -- pybind example plugin
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch> Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
...@@ -7,8 +7,8 @@ ...@@ -7,8 +7,8 @@
BSD-style license that can be found in the LICENSE file. BSD-style license that can be found in the LICENSE file.
*/ */
#include "example.h" #include "pybind11_tests.h"
#include "constructor-stats.h" #include "constructor_stats.h"
void init_ex_methods_and_attributes(py::module &); void init_ex_methods_and_attributes(py::module &);
void init_ex_python_types(py::module &); void init_ex_python_types(py::module &);
...@@ -30,6 +30,7 @@ void init_ex_stl_binder_vector(py::module &); ...@@ -30,6 +30,7 @@ void init_ex_stl_binder_vector(py::module &);
void init_ex_eval(py::module &); void init_ex_eval(py::module &);
void init_ex_custom_exceptions(py::module &); void init_ex_custom_exceptions(py::module &);
void init_ex_numpy_dtypes(py::module &); void init_ex_numpy_dtypes(py::module &);
void init_ex_enum(py::module &);
void init_issues(py::module &); void init_issues(py::module &);
#if defined(PYBIND11_TEST_EIGEN) #if defined(PYBIND11_TEST_EIGEN)
...@@ -49,8 +50,8 @@ void bind_ConstructorStats(py::module &m) { ...@@ -49,8 +50,8 @@ void bind_ConstructorStats(py::module &m) {
; ;
} }
PYBIND11_PLUGIN(example) { PYBIND11_PLUGIN(pybind11_tests) {
py::module m("example", "pybind example plugin"); py::module m("pybind11_tests", "pybind example plugin");
bind_ConstructorStats(m); bind_ConstructorStats(m);
...@@ -74,11 +75,15 @@ PYBIND11_PLUGIN(example) { ...@@ -74,11 +75,15 @@ PYBIND11_PLUGIN(example) {
init_ex_eval(m); init_ex_eval(m);
init_ex_custom_exceptions(m); init_ex_custom_exceptions(m);
init_ex_numpy_dtypes(m); init_ex_numpy_dtypes(m);
init_ex_enum(m);
init_issues(m); init_issues(m);
#if defined(PYBIND11_TEST_EIGEN) #if defined(PYBIND11_TEST_EIGEN)
init_eigen(m); init_eigen(m);
#endif m.attr("have_eigen") = py::cast(true);
#else
m.attr("have_eigen") = py::cast(false);
#endif
return m.ptr(); return m.ptr();
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment