mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-22 21:25:13 +00:00
3f589379ec
This commit rewrites the examples that look for constructor/destructor calls to do so via static variable tracking rather than output parsing. The added ConstructorStats class provides methods to keep track of constructors and destructors, number of default/copy/move constructors, and number of copy/move assignments. It also provides a mechanism for storing values (e.g. for value construction), and then allows all of this to be checked at the end of a test by getting the statistics for a C++ (or python mapping) class. By not relying on the precise pattern of constructions/destructions, but rather simply ensuring that every construction is matched with a destruction on the same object, we ensure that everything that gets created also gets destroyed as expected. This replaces all of the various "std::cout << whatever" code in constructors/destructors with `print_created(this)`/`print_destroyed(this)`/etc. functions which provide similar output, but now has a unified format across the different examples, including a new ### prefix that makes mixed example output and lifecycle events easier to distinguish. With this change, relaxed mode is no longer needed, which enables testing for proper destruction under MSVC, and under any other compiler that generates code calling extra constructors, or optimizes away any constructors. GCC/clang are used as the baseline for move constructors; the tests are adapted to allow more move constructors to be evoked (but other types are constructors much have matching counts). This commit also disables output buffering of tests, as the buffering sometimes results in C++ output ending up in the middle of python output (or vice versa), depending on the OS/python version.
86 lines
2.7 KiB
Python
Executable File
86 lines
2.7 KiB
Python
Executable File
#!/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])
|