mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-26 15:12:01 +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.
42 lines
1.1 KiB
Python
Executable File
42 lines
1.1 KiB
Python
Executable File
#!/usr/bin/env python
|
|
from __future__ import print_function
|
|
import sys
|
|
sys.path.append('.')
|
|
|
|
from example import Vector2, Vector
|
|
|
|
v1 = Vector2(1, 2)
|
|
v2 = Vector(3, -1)
|
|
|
|
print("v1 = " + str(v1))
|
|
print("v2 = " + str(v2))
|
|
print("v1+v2 = " + str(v1+v2))
|
|
print("v1-v2 = " + str(v1-v2))
|
|
print("v1-8 = " + str(v1-8))
|
|
print("v1+8 = " + str(v1+8))
|
|
print("v1*8 = " + str(v1*8))
|
|
print("v1/8 = " + str(v1/8))
|
|
print("8-v1 = " + str(8-v1))
|
|
print("8+v1 = " + str(8+v1))
|
|
print("8*v1 = " + str(8*v1))
|
|
print("8/v1 = " + str(8/v1))
|
|
|
|
v1 += v2
|
|
v1 *= 2
|
|
|
|
print("(v1+v2)*2 = " + str(v1))
|
|
|
|
from example import ConstructorStats
|
|
cstats = ConstructorStats.get(Vector2)
|
|
print("Instances not destroyed:", cstats.alive())
|
|
v1 = None
|
|
print("Instances not destroyed:", cstats.alive())
|
|
v2 = 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 >= 10)
|
|
print("Copy assignments:", cstats.copy_assignments)
|
|
print("Move assignments:", cstats.move_assignments)
|