summaryrefslogtreecommitdiff
path: root/ext/pybind11/tests
diff options
context:
space:
mode:
authorJason Lowe-Power <jason@lowepower.com>2017-11-17 17:02:05 -0800
committerJason Lowe-Power <jason@lowepower.com>2017-12-14 00:27:59 +0000
commitf07d5069d86e31ecf195664850f79fb00c445bd3 (patch)
treef54ac06896fa828f873d199a0e9b25bd94911c79 /ext/pybind11/tests
parent3f64b374c49491f18dc2ca538ed8c8597e4aac83 (diff)
downloadgem5-f07d5069d86e31ecf195664850f79fb00c445bd3.tar.xz
ext: Upgrade PyBind11 to version 2.2.1
This upgrade is necessary for pybind to build with GCC 7.2. We still need to add the patch for stl.h. MSC_FULL_VER change is no longer needed. See https://gem5-review.googlesource.com/c/public/gem5/+/2230 Change-Id: I806729217d022070583994c2dfcaa74476aef30f Signed-off-by: Jason Lowe-Power <jason@lowepower.com> Reviewed-on: https://gem5-review.googlesource.com/5801 Reviewed-by: Andreas Sandberg <andreas.sandberg@arm.com> Maintainer: Andreas Sandberg <andreas.sandberg@arm.com>
Diffstat (limited to 'ext/pybind11/tests')
-rw-r--r--ext/pybind11/tests/CMakeLists.txt160
-rw-r--r--ext/pybind11/tests/conftest.py4
-rw-r--r--ext/pybind11/tests/constructor_stats.h2
-rw-r--r--ext/pybind11/tests/local_bindings.h64
-rw-r--r--ext/pybind11/tests/pybind11_cross_module_tests.cpp123
-rw-r--r--ext/pybind11/tests/pybind11_tests.cpp49
-rw-r--r--ext/pybind11/tests/pybind11_tests.h59
-rw-r--r--ext/pybind11/tests/pytest.ini8
-rw-r--r--ext/pybind11/tests/test_alias_initialization.cpp62
-rw-r--r--ext/pybind11/tests/test_alias_initialization.py80
-rw-r--r--ext/pybind11/tests/test_buffers.cpp196
-rw-r--r--ext/pybind11/tests/test_buffers.py55
-rw-r--r--ext/pybind11/tests/test_builtin_casters.cpp156
-rw-r--r--ext/pybind11/tests/test_builtin_casters.py322
-rw-r--r--ext/pybind11/tests/test_call_policies.cpp98
-rw-r--r--ext/pybind11/tests/test_call_policies.py187
-rw-r--r--ext/pybind11/tests/test_callbacks.cpp167
-rw-r--r--ext/pybind11/tests/test_callbacks.py83
-rw-r--r--ext/pybind11/tests/test_chrono.cpp72
-rw-r--r--ext/pybind11/tests/test_chrono.py41
-rw-r--r--ext/pybind11/tests/test_class.cpp357
-rw-r--r--ext/pybind11/tests/test_class.py235
-rw-r--r--ext/pybind11/tests/test_class_args.cpp68
-rw-r--r--ext/pybind11/tests/test_class_args.py8
-rw-r--r--ext/pybind11/tests/test_cmake_build/CMakeLists.txt58
-rw-r--r--ext/pybind11/tests/test_cmake_build/embed.cpp21
-rw-r--r--ext/pybind11/tests/test_cmake_build/installed_embed/CMakeLists.txt15
-rw-r--r--ext/pybind11/tests/test_cmake_build/main.cpp6
-rw-r--r--ext/pybind11/tests/test_cmake_build/subdirectory_embed/CMakeLists.txt25
-rw-r--r--ext/pybind11/tests/test_constants_and_functions.cpp25
-rw-r--r--ext/pybind11/tests/test_constants_and_functions.py38
-rw-r--r--ext/pybind11/tests/test_copy_move.cpp213
-rw-r--r--ext/pybind11/tests/test_copy_move.py112
-rw-r--r--ext/pybind11/tests/test_copy_move_policies.cpp41
-rw-r--r--ext/pybind11/tests/test_copy_move_policies.py15
-rw-r--r--ext/pybind11/tests/test_docstring_options.cpp17
-rw-r--r--ext/pybind11/tests/test_docstring_options.py34
-rw-r--r--ext/pybind11/tests/test_eigen.cpp73
-rw-r--r--ext/pybind11/tests/test_eigen.py443
-rw-r--r--ext/pybind11/tests/test_embed/CMakeLists.txt34
-rw-r--r--ext/pybind11/tests/test_embed/catch.cpp16
-rw-r--r--ext/pybind11/tests/test_embed/test_interpreter.cpp269
-rw-r--r--ext/pybind11/tests/test_embed/test_interpreter.py9
-rw-r--r--ext/pybind11/tests/test_enum.cpp73
-rw-r--r--ext/pybind11/tests/test_enum.py118
-rw-r--r--ext/pybind11/tests/test_eval.cpp28
-rw-r--r--ext/pybind11/tests/test_eval.py16
-rw-r--r--ext/pybind11/tests/test_exceptions.cpp69
-rw-r--r--ext/pybind11/tests/test_exceptions.py130
-rw-r--r--ext/pybind11/tests/test_factory_constructors.cpp337
-rw-r--r--ext/pybind11/tests/test_factory_constructors.py459
-rw-r--r--ext/pybind11/tests/test_inheritance.cpp123
-rw-r--r--ext/pybind11/tests/test_inheritance.py78
-rw-r--r--ext/pybind11/tests/test_iostream.cpp73
-rw-r--r--ext/pybind11/tests/test_iostream.py203
-rw-r--r--ext/pybind11/tests/test_issues.cpp400
-rw-r--r--ext/pybind11/tests/test_issues.py251
-rw-r--r--ext/pybind11/tests/test_keep_alive.cpp40
-rw-r--r--ext/pybind11/tests/test_keep_alive.py97
-rw-r--r--ext/pybind11/tests/test_kwargs_and_defaults.cpp124
-rw-r--r--ext/pybind11/tests/test_kwargs_and_defaults.py71
-rw-r--r--ext/pybind11/tests/test_local_bindings.cpp101
-rw-r--r--ext/pybind11/tests/test_local_bindings.py226
-rw-r--r--ext/pybind11/tests/test_methods_and_attributes.cpp281
-rw-r--r--ext/pybind11/tests/test_methods_and_attributes.py305
-rw-r--r--ext/pybind11/tests/test_modules.cpp104
-rw-r--r--ext/pybind11/tests/test_modules.py34
-rw-r--r--ext/pybind11/tests/test_multiple_inheritance.cpp208
-rw-r--r--ext/pybind11/tests/test_multiple_inheritance.py296
-rw-r--r--ext/pybind11/tests/test_numpy_array.cpp140
-rw-r--r--ext/pybind11/tests/test_numpy_array.py327
-rw-r--r--ext/pybind11/tests/test_numpy_dtypes.cpp364
-rw-r--r--ext/pybind11/tests/test_numpy_dtypes.py154
-rw-r--r--ext/pybind11/tests/test_numpy_vectorize.cpp49
-rw-r--r--ext/pybind11/tests/test_numpy_vectorize.py73
-rw-r--r--ext/pybind11/tests/test_opaque_types.cpp17
-rw-r--r--ext/pybind11/tests/test_opaque_types.py38
-rw-r--r--ext/pybind11/tests/test_operator_overloading.cpp102
-rw-r--r--ext/pybind11/tests/test_operator_overloading.py80
-rw-r--r--ext/pybind11/tests/test_pickling.cpp97
-rw-r--r--ext/pybind11/tests/test_pickling.py17
-rw-r--r--ext/pybind11/tests/test_python_types.cpp495
-rw-r--r--ext/pybind11/tests/test_python_types.py535
-rw-r--r--ext/pybind11/tests/test_pytypes.cpp272
-rw-r--r--ext/pybind11/tests/test_pytypes.py240
-rw-r--r--ext/pybind11/tests/test_sequences_and_iterators.cpp334
-rw-r--r--ext/pybind11/tests/test_sequences_and_iterators.py87
-rw-r--r--ext/pybind11/tests/test_smart_ptr.cpp374
-rw-r--r--ext/pybind11/tests/test_smart_ptr.py136
-rw-r--r--ext/pybind11/tests/test_stl.cpp238
-rw-r--r--ext/pybind11/tests/test_stl.py200
-rw-r--r--ext/pybind11/tests/test_stl_binders.cpp75
-rw-r--r--ext/pybind11/tests/test_stl_binders.py139
-rw-r--r--ext/pybind11/tests/test_virtual_functions.cpp191
-rw-r--r--ext/pybind11/tests/test_virtual_functions.py270
95 files changed, 8613 insertions, 4796 deletions
diff --git a/ext/pybind11/tests/CMakeLists.txt b/ext/pybind11/tests/CMakeLists.txt
index 11be49e53..25e06662c 100644
--- a/ext/pybind11/tests/CMakeLists.txt
+++ b/ext/pybind11/tests/CMakeLists.txt
@@ -12,7 +12,7 @@ option(PYBIND11_WERROR "Report all warnings as errors" OFF)
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
# We're being loaded directly, i.e. not via add_subdirectory, so make this
# work as its own project and load the pybind11Config to get the tools we need
- project(pybind11_tests)
+ project(pybind11_tests CXX)
find_package(pybind11 REQUIRED CONFIG)
endif()
@@ -26,22 +26,23 @@ endif()
# Full set of test files (you can override these; see below)
set(PYBIND11_TEST_FILES
- test_alias_initialization.cpp
test_buffers.cpp
+ test_builtin_casters.cpp
+ test_call_policies.cpp
test_callbacks.cpp
test_chrono.cpp
- test_class_args.cpp
+ test_class.cpp
test_constants_and_functions.cpp
- test_copy_move_policies.cpp
+ test_copy_move.cpp
test_docstring_options.cpp
test_eigen.cpp
test_enum.cpp
test_eval.cpp
test_exceptions.cpp
- test_inheritance.cpp
- test_issues.cpp
- test_keep_alive.cpp
+ test_factory_constructors.cpp
+ test_iostream.cpp
test_kwargs_and_defaults.cpp
+ test_local_bindings.cpp
test_methods_and_attributes.cpp
test_modules.cpp
test_multiple_inheritance.cpp
@@ -51,15 +52,16 @@ set(PYBIND11_TEST_FILES
test_opaque_types.cpp
test_operator_overloading.cpp
test_pickling.cpp
- test_python_types.cpp
+ test_pytypes.cpp
test_sequences_and_iterators.cpp
test_smart_ptr.cpp
+ test_stl.cpp
test_stl_binders.cpp
test_virtual_functions.cpp
)
# Invoking cmake with something like:
-# cmake -DPYBIND11_TEST_OVERRIDE="test_issues.cpp;test_picking.cpp" ..
+# cmake -DPYBIND11_TEST_OVERRIDE="test_callbacks.cpp;test_picking.cpp" ..
# lets you override the tests that get compiled and run. You can restore to all tests with:
# cmake -DPYBIND11_TEST_OVERRIDE= ..
if (PYBIND11_TEST_OVERRIDE)
@@ -68,6 +70,16 @@ endif()
string(REPLACE ".cpp" ".py" PYBIND11_PYTEST_FILES "${PYBIND11_TEST_FILES}")
+# Contains the set of test files that require pybind11_cross_module_tests to be
+# built; if none of these are built (i.e. because TEST_OVERRIDE is used and
+# doesn't include them) the second module doesn't get built.
+set(PYBIND11_CROSS_MODULE_TESTS
+ test_exceptions.py
+ test_local_bindings.py
+ test_stl.py
+ test_stl_binders.py
+)
+
# Check if Eigen is available; if not, remove from PYBIND11_TEST_FILES (but
# keep it in PYBIND11_PYTEST_FILES, so that we get the "eigen is not installed"
# skip message).
@@ -104,6 +116,9 @@ if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
endif()
endif()
+# Optional dependency for some tests (boost::variant is only supported with version >= 1.56)
+find_package(Boost 1.56)
+
# Compile with compiler warnings turned on
function(pybind11_enable_warnings target_name)
if(MSVC)
@@ -121,32 +136,55 @@ function(pybind11_enable_warnings target_name)
endif()
endfunction()
+set(test_targets pybind11_tests)
+
+# Build pybind11_cross_module_tests if any test_whatever.py are being built that require it
+foreach(t ${PYBIND11_CROSS_MODULE_TESTS})
+ list(FIND PYBIND11_PYTEST_FILES ${t} i)
+ if (i GREATER -1)
+ list(APPEND test_targets pybind11_cross_module_tests)
+ break()
+ endif()
+endforeach()
-# Create the binding library
-pybind11_add_module(pybind11_tests THIN_LTO pybind11_tests.cpp
- ${PYBIND11_TEST_FILES} ${PYBIND11_HEADERS})
+set(testdir ${CMAKE_CURRENT_SOURCE_DIR})
+foreach(target ${test_targets})
+ set(test_files ${PYBIND11_TEST_FILES})
+ if(NOT target STREQUAL "pybind11_tests")
+ set(test_files "")
+ endif()
-pybind11_enable_warnings(pybind11_tests)
+ # Create the binding library
+ pybind11_add_module(${target} THIN_LTO ${target}.cpp ${test_files} ${PYBIND11_HEADERS})
+ pybind11_enable_warnings(${target})
-if(EIGEN3_FOUND)
- if (PYBIND11_EIGEN_VIA_TARGET)
- target_link_libraries(pybind11_tests PRIVATE Eigen3::Eigen)
- else()
- target_include_directories(pybind11_tests PRIVATE ${EIGEN3_INCLUDE_DIR})
+ if(MSVC)
+ target_compile_options(${target} PRIVATE /utf-8)
endif()
- target_compile_definitions(pybind11_tests PRIVATE -DPYBIND11_TEST_EIGEN)
-endif()
-set(testdir ${CMAKE_CURRENT_SOURCE_DIR})
+ if(EIGEN3_FOUND)
+ if (PYBIND11_EIGEN_VIA_TARGET)
+ target_link_libraries(${target} PRIVATE Eigen3::Eigen)
+ else()
+ target_include_directories(${target} PRIVATE ${EIGEN3_INCLUDE_DIR})
+ endif()
+ target_compile_definitions(${target} PRIVATE -DPYBIND11_TEST_EIGEN)
+ endif()
-# 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()
+ if(Boost_FOUND)
+ target_include_directories(${target} PRIVATE ${Boost_INCLUDE_DIRS})
+ target_compile_definitions(${target} PRIVATE -DPYBIND11_TEST_BOOST)
+ endif()
+
+ # Always write the output file directly into the 'tests' directory (even on MSVC)
+ if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
+ set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${testdir})
+ foreach(config ${CMAKE_CONFIGURATION_TYPES})
+ string(TOUPPER ${config} config)
+ set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config} ${testdir})
+ endforeach()
+ endif()
+endforeach()
# Make sure pytest is found or produce a fatal error
if(NOT PYBIND11_PYTEST_FOUND)
@@ -162,9 +200,15 @@ if(NOT PYBIND11_PYTEST_FOUND)
set(PYBIND11_PYTEST_FOUND TRUE CACHE INTERNAL "")
endif()
+if(CMAKE_VERSION VERSION_LESS 3.2)
+ set(PYBIND11_USES_TERMINAL "")
+else()
+ set(PYBIND11_USES_TERMINAL "USES_TERMINAL")
+endif()
+
# A single command to compile and run the tests
add_custom_target(pytest COMMAND ${PYTHON_EXECUTABLE} -m pytest ${PYBIND11_PYTEST_FILES}
- DEPENDS pybind11_tests WORKING_DIRECTORY ${testdir})
+ DEPENDS ${test_targets} WORKING_DIRECTORY ${testdir} ${PYBIND11_USES_TERMINAL})
if(PYBIND11_TEST_OVERRIDE)
add_custom_command(TARGET pytest POST_BUILD
@@ -180,59 +224,13 @@ if (NOT PROJECT_NAME STREQUAL "pybind11")
return()
endif()
-# Add a post-build comment to show the .so size and, if a previous size, compare it:
+# Add a post-build comment to show the primary test suite .so size and, if a previous size, compare it:
add_custom_command(TARGET pybind11_tests POST_BUILD
COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/libsize.py
$<TARGET_FILE:pybind11_tests> ${CMAKE_CURRENT_BINARY_DIR}/sosize-$<TARGET_FILE_NAME:pybind11_tests>.txt)
-# Test CMake build using functions and targets from subdirectory or installed location
-add_custom_target(test_cmake_build)
-if(NOT CMAKE_VERSION VERSION_LESS 3.1)
- # 3.0 needed for interface library for subdirectory_target/installed_target
- # 3.1 needed for cmake -E env for testing
-
- include(CMakeParseArguments)
- function(pybind11_add_build_test name)
- cmake_parse_arguments(ARG "INSTALL" "" "" ${ARGN})
-
- set(build_options "-DCMAKE_PREFIX_PATH=${PROJECT_BINARY_DIR}/mock_install"
- "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
- "-DPYTHON_EXECUTABLE=${PYTHON_EXECUTABLE}"
- "-DPYBIND11_CPP_STANDARD=${PYBIND11_CPP_STANDARD}")
- if(NOT ARG_INSTALL)
- list(APPEND build_options "-DPYBIND11_PROJECT_DIR=${PROJECT_SOURCE_DIR}")
- endif()
+# Test embedding the interpreter. Provides the `cpptest` target.
+add_subdirectory(test_embed)
- add_custom_target(test_${name} ${CMAKE_CTEST_COMMAND}
- --quiet --output-log test_cmake_build/${name}.log
- --build-and-test "${CMAKE_CURRENT_SOURCE_DIR}/test_cmake_build/${name}"
- "${CMAKE_CURRENT_BINARY_DIR}/test_cmake_build/${name}"
- --build-config Release
- --build-noclean
- --build-generator ${CMAKE_GENERATOR}
- $<$<BOOL:${CMAKE_GENERATOR_PLATFORM}>:--build-generator-platform> ${CMAKE_GENERATOR_PLATFORM}
- --build-makeprogram ${CMAKE_MAKE_PROGRAM}
- --build-target check
- --build-options ${build_options}
- )
- if(ARG_INSTALL)
- add_dependencies(test_${name} mock_install)
- endif()
- add_dependencies(test_cmake_build test_${name})
- endfunction()
-
- pybind11_add_build_test(subdirectory_function)
- pybind11_add_build_test(subdirectory_target)
-
- if(PYBIND11_INSTALL)
- add_custom_target(mock_install ${CMAKE_COMMAND}
- "-DCMAKE_INSTALL_PREFIX=${PROJECT_BINARY_DIR}/mock_install"
- -P "${PROJECT_BINARY_DIR}/cmake_install.cmake"
- )
-
- pybind11_add_build_test(installed_function INSTALL)
- pybind11_add_build_test(installed_target INSTALL)
- endif()
-endif()
-
-add_dependencies(check test_cmake_build)
+# Test CMake build using functions and targets from subdirectory or installed location
+add_subdirectory(test_cmake_build)
diff --git a/ext/pybind11/tests/conftest.py b/ext/pybind11/tests/conftest.py
index 5b08004e3..f4c228260 100644
--- a/ext/pybind11/tests/conftest.py
+++ b/ext/pybind11/tests/conftest.py
@@ -196,7 +196,7 @@ def pytest_namespace():
except ImportError:
scipy = None
try:
- from pybind11_tests import have_eigen
+ from pybind11_tests.eigen import have_eigen
except ImportError:
have_eigen = False
pypy = platform.python_implementation() == "PyPy"
@@ -211,6 +211,8 @@ def pytest_namespace():
'requires_eigen_and_scipy': skipif(not have_eigen or not scipy,
reason="eigen and/or scipy are not installed"),
'unsupported_on_pypy': skipif(pypy, reason="unsupported on PyPy"),
+ 'unsupported_on_py2': skipif(sys.version_info.major < 3,
+ reason="unsupported on Python 2.x"),
'gc_collect': gc_collect
}
diff --git a/ext/pybind11/tests/constructor_stats.h b/ext/pybind11/tests/constructor_stats.h
index f66ff71df..babded032 100644
--- a/ext/pybind11/tests/constructor_stats.h
+++ b/ext/pybind11/tests/constructor_stats.h
@@ -169,7 +169,7 @@ public:
auto &internals = py::detail::get_internals();
const std::type_index *t1 = nullptr, *t2 = nullptr;
try {
- auto *type_info = internals.registered_types_py.at(class_.ptr());
+ auto *type_info = internals.registered_types_py.at((PyTypeObject *) class_.ptr()).at(0);
for (auto &p : internals.registered_types_cpp) {
if (p.second == type_info) {
if (t1) {
diff --git a/ext/pybind11/tests/local_bindings.h b/ext/pybind11/tests/local_bindings.h
new file mode 100644
index 000000000..b6afb8086
--- /dev/null
+++ b/ext/pybind11/tests/local_bindings.h
@@ -0,0 +1,64 @@
+#pragma once
+#include "pybind11_tests.h"
+
+/// Simple class used to test py::local:
+template <int> class LocalBase {
+public:
+ LocalBase(int i) : i(i) { }
+ int i = -1;
+};
+
+/// Registered with py::module_local in both main and secondary modules:
+using LocalType = LocalBase<0>;
+/// Registered without py::module_local in both modules:
+using NonLocalType = LocalBase<1>;
+/// A second non-local type (for stl_bind tests):
+using NonLocal2 = LocalBase<2>;
+/// Tests within-module, different-compilation-unit local definition conflict:
+using LocalExternal = LocalBase<3>;
+/// Mixed: registered local first, then global
+using MixedLocalGlobal = LocalBase<4>;
+/// Mixed: global first, then local
+using MixedGlobalLocal = LocalBase<5>;
+
+/// Registered with py::module_local only in the secondary module:
+using ExternalType1 = LocalBase<6>;
+using ExternalType2 = LocalBase<7>;
+
+using LocalVec = std::vector<LocalType>;
+using LocalVec2 = std::vector<NonLocal2>;
+using LocalMap = std::unordered_map<std::string, LocalType>;
+using NonLocalVec = std::vector<NonLocalType>;
+using NonLocalVec2 = std::vector<NonLocal2>;
+using NonLocalMap = std::unordered_map<std::string, NonLocalType>;
+using NonLocalMap2 = std::unordered_map<std::string, uint8_t>;
+
+PYBIND11_MAKE_OPAQUE(LocalVec);
+PYBIND11_MAKE_OPAQUE(LocalVec2);
+PYBIND11_MAKE_OPAQUE(LocalMap);
+PYBIND11_MAKE_OPAQUE(NonLocalVec);
+//PYBIND11_MAKE_OPAQUE(NonLocalVec2); // same type as LocalVec2
+PYBIND11_MAKE_OPAQUE(NonLocalMap);
+PYBIND11_MAKE_OPAQUE(NonLocalMap2);
+
+
+// Simple bindings (used with the above):
+template <typename T, int Adjust = 0, typename... Args>
+py::class_<T> bind_local(Args && ...args) {
+ return py::class_<T>(std::forward<Args>(args)...)
+ .def(py::init<int>())
+ .def("get", [](T &i) { return i.i + Adjust; });
+};
+
+// Simulate a foreign library base class (to match the example in the docs):
+namespace pets {
+class Pet {
+public:
+ Pet(std::string name) : name_(name) {}
+ std::string name_;
+ const std::string &name() { return name_; }
+};
+}
+
+struct MixGL { int i; MixGL(int i) : i{i} {} };
+struct MixGL2 { int i; MixGL2(int i) : i{i} {} };
diff --git a/ext/pybind11/tests/pybind11_cross_module_tests.cpp b/ext/pybind11/tests/pybind11_cross_module_tests.cpp
new file mode 100644
index 000000000..f705e3106
--- /dev/null
+++ b/ext/pybind11/tests/pybind11_cross_module_tests.cpp
@@ -0,0 +1,123 @@
+/*
+ tests/pybind11_cross_module_tests.cpp -- contains tests that require multiple modules
+
+ Copyright (c) 2017 Jason Rhinelander <jason@imaginary.ca>
+
+ All rights reserved. Use of this source code is governed by a
+ BSD-style license that can be found in the LICENSE file.
+*/
+
+#include "pybind11_tests.h"
+#include "local_bindings.h"
+#include <pybind11/stl_bind.h>
+#include <numeric>
+
+PYBIND11_MODULE(pybind11_cross_module_tests, m) {
+ m.doc() = "pybind11 cross-module test module";
+
+ // test_local_bindings.py tests:
+ //
+ // Definitions here are tested by importing both this module and the
+ // relevant pybind11_tests submodule from a test_whatever.py
+
+ // test_load_external
+ bind_local<ExternalType1>(m, "ExternalType1", py::module_local());
+ bind_local<ExternalType2>(m, "ExternalType2", py::module_local());
+
+ // test_exceptions.py
+ m.def("raise_runtime_error", []() { PyErr_SetString(PyExc_RuntimeError, "My runtime error"); throw py::error_already_set(); });
+ m.def("raise_value_error", []() { PyErr_SetString(PyExc_ValueError, "My value error"); throw py::error_already_set(); });
+ m.def("throw_pybind_value_error", []() { throw py::value_error("pybind11 value error"); });
+ m.def("throw_pybind_type_error", []() { throw py::type_error("pybind11 type error"); });
+ m.def("throw_stop_iteration", []() { throw py::stop_iteration(); });
+
+ // test_local_bindings.py
+ // Local to both:
+ bind_local<LocalType, 1>(m, "LocalType", py::module_local())
+ .def("get2", [](LocalType &t) { return t.i + 2; })
+ ;
+
+ // Can only be called with our python type:
+ m.def("local_value", [](LocalType &l) { return l.i; });
+
+ // test_nonlocal_failure
+ // This registration will fail (global registration when LocalFail is already registered
+ // globally in the main test module):
+ m.def("register_nonlocal", [m]() {
+ bind_local<NonLocalType, 0>(m, "NonLocalType");
+ });
+
+ // test_stl_bind_local
+ // stl_bind.h binders defaults to py::module_local if the types are local or converting:
+ py::bind_vector<LocalVec>(m, "LocalVec");
+ py::bind_map<LocalMap>(m, "LocalMap");
+
+ // test_stl_bind_global
+ // and global if the type (or one of the types, for the map) is global (so these will fail,
+ // assuming pybind11_tests is already loaded):
+ m.def("register_nonlocal_vec", [m]() {
+ py::bind_vector<NonLocalVec>(m, "NonLocalVec");
+ });
+ m.def("register_nonlocal_map", [m]() {
+ py::bind_map<NonLocalMap>(m, "NonLocalMap");
+ });
+ // The default can, however, be overridden to global using `py::module_local()` or
+ // `py::module_local(false)`.
+ // Explicitly made local:
+ py::bind_vector<NonLocalVec2>(m, "NonLocalVec2", py::module_local());
+ // Explicitly made global (and so will fail to bind):
+ m.def("register_nonlocal_map2", [m]() {
+ py::bind_map<NonLocalMap2>(m, "NonLocalMap2", py::module_local(false));
+ });
+
+ // test_mixed_local_global
+ // We try this both with the global type registered first and vice versa (the order shouldn't
+ // matter).
+ m.def("register_mixed_global_local", [m]() {
+ bind_local<MixedGlobalLocal, 200>(m, "MixedGlobalLocal", py::module_local());
+ });
+ m.def("register_mixed_local_global", [m]() {
+ bind_local<MixedLocalGlobal, 2000>(m, "MixedLocalGlobal", py::module_local(false));
+ });
+ m.def("get_mixed_gl", [](int i) { return MixedGlobalLocal(i); });
+ m.def("get_mixed_lg", [](int i) { return MixedLocalGlobal(i); });
+
+ // test_internal_locals_differ
+ m.def("local_cpp_types_addr", []() { return (uintptr_t) &py::detail::registered_local_types_cpp(); });
+
+ // test_stl_caster_vs_stl_bind
+ py::bind_vector<std::vector<int>>(m, "VectorInt");
+
+ m.def("load_vector_via_binding", [](std::vector<int> &v) {
+ return std::accumulate(v.begin(), v.end(), 0);
+ });
+
+ // test_cross_module_calls
+ m.def("return_self", [](LocalVec *v) { return v; });
+ m.def("return_copy", [](const LocalVec &v) { return LocalVec(v); });
+
+ class Dog : public pets::Pet { public: Dog(std::string name) : Pet(name) {}; };
+ py::class_<pets::Pet>(m, "Pet", py::module_local())
+ .def("name", &pets::Pet::name);
+ // Binding for local extending class:
+ py::class_<Dog, pets::Pet>(m, "Dog")
+ .def(py::init<std::string>());
+ m.def("pet_name", [](pets::Pet &p) { return p.name(); });
+
+ py::class_<MixGL>(m, "MixGL", py::module_local()).def(py::init<int>());
+ m.def("get_gl_value", [](MixGL &o) { return o.i + 100; });
+
+ py::class_<MixGL2>(m, "MixGL2", py::module_local()).def(py::init<int>());
+
+ // test_vector_bool
+ // We can't test both stl.h and stl_bind.h conversions of `std::vector<bool>` within
+ // the same module (it would be an ODR violation). Therefore `bind_vector` of `bool`
+ // is defined here and tested in `test_stl_binders.py`.
+ py::bind_vector<std::vector<bool>>(m, "VectorBool");
+
+ // test_missing_header_message
+ // The main module already includes stl.h, but we need to test the error message
+ // which appears when this header is missing.
+ m.def("missing_header_arg", [](std::vector<float>) { });
+ m.def("missing_header_return", []() { return std::vector<float>(); });
+}
diff --git a/ext/pybind11/tests/pybind11_tests.cpp b/ext/pybind11/tests/pybind11_tests.cpp
index 1d805d75b..bc7d2c3e7 100644
--- a/ext/pybind11/tests/pybind11_tests.cpp
+++ b/ext/pybind11/tests/pybind11_tests.cpp
@@ -10,6 +10,9 @@
#include "pybind11_tests.h"
#include "constructor_stats.h"
+#include <functional>
+#include <list>
+
/*
For testing purposes, we define a static global variable here in a function that each individual
test .cpp calls with its initialization lambda. It's convenient here because we can just not
@@ -28,8 +31,15 @@ std::list<std::function<void(py::module &)>> &initializers() {
return inits;
}
-test_initializer::test_initializer(std::function<void(py::module &)> initializer) {
- initializers().push_back(std::move(initializer));
+test_initializer::test_initializer(Initializer init) {
+ initializers().push_back(init);
+}
+
+test_initializer::test_initializer(const char *submodule_name, Initializer init) {
+ initializers().push_back([=](py::module &parent) {
+ auto m = parent.def_submodule(submodule_name);
+ init(m);
+ });
}
void bind_ConstructorStats(py::module &m) {
@@ -41,18 +51,43 @@ void bind_ConstructorStats(py::module &m) {
.def_readwrite("move_assignments", &ConstructorStats::move_assignments)
.def_readwrite("copy_constructions", &ConstructorStats::copy_constructions)
.def_readwrite("move_constructions", &ConstructorStats::move_constructions)
- .def_static("get", (ConstructorStats &(*)(py::object)) &ConstructorStats::get, py::return_value_policy::reference_internal);
+ .def_static("get", (ConstructorStats &(*)(py::object)) &ConstructorStats::get, py::return_value_policy::reference_internal)
+
+ // Not exactly ConstructorStats, but related: expose the internal pybind number of registered instances
+ // to allow instance cleanup checks (invokes a GC first)
+ .def_static("detail_reg_inst", []() {
+ ConstructorStats::gc();
+ return py::detail::get_internals().registered_instances.size();
+ })
+ ;
}
-PYBIND11_PLUGIN(pybind11_tests) {
- py::module m("pybind11_tests", "pybind testing plugin");
+PYBIND11_MODULE(pybind11_tests, m) {
+ m.doc() = "pybind11 test module";
bind_ConstructorStats(m);
+#if !defined(NDEBUG)
+ m.attr("debug_enabled") = true;
+#else
+ m.attr("debug_enabled") = false;
+#endif
+
+ py::class_<UserType>(m, "UserType", "A `py::class_` type for testing")
+ .def(py::init<>())
+ .def(py::init<int>())
+ .def("get_value", &UserType::value, "Get value using a method")
+ .def("set_value", &UserType::set, "Set value using a method")
+ .def_property("value", &UserType::value, &UserType::set, "Get/set value using a property")
+ .def("__repr__", [](const UserType& u) { return "UserType({})"_s.format(u.value()); });
+
+ py::class_<IncType, UserType>(m, "IncType")
+ .def(py::init<>())
+ .def(py::init<int>())
+ .def("__repr__", [](const IncType& u) { return "IncType({})"_s.format(u.value()); });
+
for (const auto &initializer : initializers())
initializer(m);
if (!py::hasattr(m, "have_eigen")) m.attr("have_eigen") = false;
-
- return m.ptr();
}
diff --git a/ext/pybind11/tests/pybind11_tests.h b/ext/pybind11/tests/pybind11_tests.h
index c11b687b2..90963a5de 100644
--- a/ext/pybind11/tests/pybind11_tests.h
+++ b/ext/pybind11/tests/pybind11_tests.h
@@ -1,12 +1,65 @@
#pragma once
#include <pybind11/pybind11.h>
-#include <functional>
-#include <list>
+
+#if defined(_MSC_VER) && _MSC_VER < 1910
+// We get some really long type names here which causes MSVC 2015 to emit warnings
+# pragma warning(disable: 4503) // warning C4503: decorated name length exceeded, name was truncated
+#endif
namespace py = pybind11;
using namespace pybind11::literals;
class test_initializer {
+ using Initializer = void (*)(py::module &);
+
+public:
+ test_initializer(Initializer init);
+ test_initializer(const char *submodule_name, Initializer init);
+};
+
+#define TEST_SUBMODULE(name, variable) \
+ void test_submodule_##name(py::module &); \
+ test_initializer name(#name, test_submodule_##name); \
+ void test_submodule_##name(py::module &variable)
+
+
+/// Dummy type which is not exported anywhere -- something to trigger a conversion error
+struct UnregisteredType { };
+
+/// A user-defined type which is exported and can be used by any test
+class UserType {
+public:
+ UserType() = default;
+ UserType(int i) : i(i) { }
+
+ int value() const { return i; }
+ void set(int set) { i = set; }
+
+private:
+ int i = -1;
+};
+
+/// Like UserType, but increments `value` on copy for quick reference vs. copy tests
+class IncType : public UserType {
+public:
+ using UserType::UserType;
+ IncType() = default;
+ IncType(const IncType &other) : IncType(other.value() + 1) { }
+ IncType(IncType &&) = delete;
+ IncType &operator=(const IncType &) = delete;
+ IncType &operator=(IncType &&) = delete;
+};
+
+/// Custom cast-only type that casts to a string "rvalue" or "lvalue" depending on the cast context.
+/// Used to test recursive casters (e.g. std::tuple, stl containers).
+struct RValueCaster {};
+NAMESPACE_BEGIN(pybind11)
+NAMESPACE_BEGIN(detail)
+template<> class type_caster<RValueCaster> {
public:
- test_initializer(std::function<void(py::module &)> initializer);
+ PYBIND11_TYPE_CASTER(RValueCaster, _("RValueCaster"));
+ static handle cast(RValueCaster &&, return_value_policy, handle) { return py::str("rvalue").release(); }
+ static handle cast(const RValueCaster &, return_value_policy, handle) { return py::str("lvalue").release(); }
};
+NAMESPACE_END(detail)
+NAMESPACE_END(pybind11)
diff --git a/ext/pybind11/tests/pytest.ini b/ext/pybind11/tests/pytest.ini
index 401cbe0ad..1e44f0a05 100644
--- a/ext/pybind11/tests/pytest.ini
+++ b/ext/pybind11/tests/pytest.ini
@@ -1,7 +1,15 @@
[pytest]
minversion = 3.0
+norecursedirs = test_cmake_build test_embed
addopts =
# show summary of skipped tests
-rs
# capture only Python print and C++ py::print, but not C output (low-level Python errors)
--capture=sys
+filterwarnings =
+ # make warnings into errors but ignore certain third-party extension issues
+ error
+ # importing scipy submodules on some version of Python
+ ignore::ImportWarning
+ # bogus numpy ABI warning (see numpy/#432)
+ ignore:.*numpy.dtype size changed.*:RuntimeWarning
diff --git a/ext/pybind11/tests/test_alias_initialization.cpp b/ext/pybind11/tests/test_alias_initialization.cpp
deleted file mode 100644
index 48e595695..000000000
--- a/ext/pybind11/tests/test_alias_initialization.cpp
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- tests/test_alias_initialization.cpp -- test cases and example of different trampoline
- initialization modes
-
- Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>, Jason Rhinelander <jason@imaginary.ca>
-
- All rights reserved. Use of this source code is governed by a
- BSD-style license that can be found in the LICENSE file.
-*/
-
-#include "pybind11_tests.h"
-
-test_initializer alias_initialization([](py::module &m) {
- // don't invoke Python dispatch classes by default when instantiating C++ classes that were not
- // extended on the Python side
- struct A {
- virtual ~A() {}
- virtual void f() { py::print("A.f()"); }
- };
-
- struct PyA : A {
- PyA() { py::print("PyA.PyA()"); }
- ~PyA() { py::print("PyA.~PyA()"); }
-
- void f() override {
- py::print("PyA.f()");
- PYBIND11_OVERLOAD(void, A, f);
- }
- };
-
- auto call_f = [](A *a) { a->f(); };
-
- py::class_<A, PyA>(m, "A")
- .def(py::init<>())
- .def("f", &A::f);
-
- m.def("call_f", call_f);
-
-
- // ... unless we explicitly request it, as in this example:
- struct A2 {
- virtual ~A2() {}
- virtual void f() { py::print("A2.f()"); }
- };
-
- struct PyA2 : A2 {
- PyA2() { py::print("PyA2.PyA2()"); }
- ~PyA2() { py::print("PyA2.~PyA2()"); }
- void f() override {
- py::print("PyA2.f()");
- PYBIND11_OVERLOAD(void, A2, f);
- }
- };
-
- py::class_<A2, PyA2>(m, "A2")
- .def(py::init_alias<>())
- .def("f", &A2::f);
-
- m.def("call_f", [](A2 *a2) { a2->f(); });
-
-});
-
diff --git a/ext/pybind11/tests/test_alias_initialization.py b/ext/pybind11/tests/test_alias_initialization.py
deleted file mode 100644
index fb90cfc7b..000000000
--- a/ext/pybind11/tests/test_alias_initialization.py
+++ /dev/null
@@ -1,80 +0,0 @@
-import pytest
-
-
-def test_alias_delay_initialization1(capture):
- """
- A only initializes its trampoline class when we inherit from it; if we just
- create and use an A instance directly, the trampoline initialization is
- bypassed and we only initialize an A() instead (for performance reasons).
- """
- from pybind11_tests import A, call_f
-
- class B(A):
- def __init__(self):
- super(B, self).__init__()
-
- def f(self):
- print("In python f()")
-
- # C++ version
- with capture:
- a = A()
- call_f(a)
- del a
- pytest.gc_collect()
- assert capture == "A.f()"
-
- # Python version
- with capture:
- b = B()
- call_f(b)
- del b
- pytest.gc_collect()
- assert capture == """
- PyA.PyA()
- PyA.f()
- In python f()
- PyA.~PyA()
- """
-
-
-def test_alias_delay_initialization2(capture):
- """A2, unlike the above, is configured to always initialize the alias; while
- the extra initialization and extra class layer has small virtual dispatch
- performance penalty, it also allows us to do more things with the trampoline
- class such as defining local variables and performing construction/destruction.
- """
- from pybind11_tests import A2, call_f
-
- class B2(A2):
- def __init__(self):
- super(B2, self).__init__()
-
- def f(self):
- print("In python B2.f()")
-
- # No python subclass version
- with capture:
- a2 = A2()
- call_f(a2)
- del a2
- pytest.gc_collect()
- assert capture == """
- PyA2.PyA2()
- PyA2.f()
- A2.f()
- PyA2.~PyA2()
- """
-
- # Python subclass version
- with capture:
- b2 = B2()
- call_f(b2)
- del b2
- pytest.gc_collect()
- assert capture == """
- PyA2.PyA2()
- PyA2.f()
- In python B2.f()
- PyA2.~PyA2()
- """
diff --git a/ext/pybind11/tests/test_buffers.cpp b/ext/pybind11/tests/test_buffers.cpp
index 057250d29..5be717730 100644
--- a/ext/pybind11/tests/test_buffers.cpp
+++ b/ext/pybind11/tests/test_buffers.cpp
@@ -10,93 +10,94 @@
#include "pybind11_tests.h"
#include "constructor_stats.h"
-class Matrix {
-public:
- Matrix(size_t rows, size_t cols) : m_rows(rows), m_cols(cols) {
- print_created(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
- m_data = new float[rows*cols];
- memset(m_data, 0, sizeof(float) * rows * cols);
- }
-
- Matrix(const Matrix &s) : m_rows(s.m_rows), m_cols(s.m_cols) {
- print_copy_created(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
- m_data = new float[m_rows * m_cols];
- memcpy(m_data, s.m_data, sizeof(float) * m_rows * m_cols);
- }
-
- Matrix(Matrix &&s) : m_rows(s.m_rows), m_cols(s.m_cols), m_data(s.m_data) {
- print_move_created(this);
- s.m_rows = 0;
- s.m_cols = 0;
- s.m_data = nullptr;
- }
-
- ~Matrix() {
- print_destroyed(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
- delete[] m_data;
- }
-
- Matrix &operator=(const Matrix &s) {
- print_copy_assigned(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
- delete[] m_data;
- m_rows = s.m_rows;
- m_cols = s.m_cols;
- m_data = new float[m_rows * m_cols];
- memcpy(m_data, s.m_data, sizeof(float) * m_rows * m_cols);
- return *this;
- }
-
- Matrix &operator=(Matrix &&s) {
- print_move_assigned(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
- if (&s != this) {
- delete[] m_data;
- m_rows = s.m_rows; m_cols = s.m_cols; m_data = s.m_data;
- s.m_rows = 0; s.m_cols = 0; s.m_data = nullptr;
+TEST_SUBMODULE(buffers, m) {
+ // test_from_python / test_to_python:
+ class Matrix {
+ public:
+ Matrix(ssize_t rows, ssize_t cols) : m_rows(rows), m_cols(cols) {
+ print_created(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
+ m_data = new float[(size_t) (rows*cols)];
+ memset(m_data, 0, sizeof(float) * (size_t) (rows * cols));
+ }
+
+ Matrix(const Matrix &s) : m_rows(s.m_rows), m_cols(s.m_cols) {
+ print_copy_created(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
+ m_data = new float[(size_t) (m_rows * m_cols)];
+ memcpy(m_data, s.m_data, sizeof(float) * (size_t) (m_rows * m_cols));
}
- return *this;
- }
- float operator()(size_t i, size_t j) const {
- return m_data[i*m_cols + j];
- }
+ Matrix(Matrix &&s) : m_rows(s.m_rows), m_cols(s.m_cols), m_data(s.m_data) {
+ print_move_created(this);
+ s.m_rows = 0;
+ s.m_cols = 0;
+ s.m_data = nullptr;
+ }
+
+ ~Matrix() {
+ print_destroyed(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
+ delete[] m_data;
+ }
- float &operator()(size_t i, size_t j) {
- return m_data[i*m_cols + j];
- }
+ Matrix &operator=(const Matrix &s) {
+ print_copy_assigned(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
+ delete[] m_data;
+ m_rows = s.m_rows;
+ m_cols = s.m_cols;
+ m_data = new float[(size_t) (m_rows * m_cols)];
+ memcpy(m_data, s.m_data, sizeof(float) * (size_t) (m_rows * m_cols));
+ return *this;
+ }
- float *data() { return m_data; }
+ Matrix &operator=(Matrix &&s) {
+ print_move_assigned(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
+ if (&s != this) {
+ delete[] m_data;
+ m_rows = s.m_rows; m_cols = s.m_cols; m_data = s.m_data;
+ s.m_rows = 0; s.m_cols = 0; s.m_data = nullptr;
+ }
+ return *this;
+ }
- size_t rows() const { return m_rows; }
- size_t cols() const { return m_cols; }
-private:
- size_t m_rows;
- size_t m_cols;
- float *m_data;
-};
+ float operator()(ssize_t i, ssize_t j) const {
+ return m_data[(size_t) (i*m_cols + j)];
+ }
-test_initializer buffers([](py::module &m) {
- py::class_<Matrix> mtx(m, "Matrix", py::buffer_protocol());
+ float &operator()(ssize_t i, ssize_t j) {
+ return m_data[(size_t) (i*m_cols + j)];
+ }
- mtx.def(py::init<size_t, size_t>())
+ float *data() { return m_data; }
+
+ ssize_t rows() const { return m_rows; }
+ ssize_t cols() const { return m_cols; }
+ private:
+ ssize_t m_rows;
+ ssize_t m_cols;
+ float *m_data;
+ };
+ py::class_<Matrix>(m, "Matrix", py::buffer_protocol())
+ .def(py::init<ssize_t, ssize_t>())
/// Construct from a buffer
- .def("__init__", [](Matrix &v, py::buffer b) {
+ .def(py::init([](py::buffer b) {
py::buffer_info info = b.request();
if (info.format != py::format_descriptor<float>::format() || info.ndim != 2)
throw std::runtime_error("Incompatible buffer format!");
- new (&v) Matrix(info.shape[0], info.shape[1]);
- memcpy(v.data(), info.ptr, sizeof(float) * v.rows() * v.cols());
- })
+
+ auto v = new Matrix(info.shape[0], info.shape[1]);
+ memcpy(v->data(), info.ptr, sizeof(float) * (size_t) (v->rows() * v->cols()));
+ return v;
+ }))
.def("rows", &Matrix::rows)
.def("cols", &Matrix::cols)
/// Bare bones interface
- .def("__getitem__", [](const Matrix &m, std::pair<size_t, size_t> i) {
+ .def("__getitem__", [](const Matrix &m, std::pair<ssize_t, ssize_t> i) {
if (i.first >= m.rows() || i.second >= m.cols())
throw py::index_error();
return m(i.first, i.second);
})
- .def("__setitem__", [](Matrix &m, std::pair<size_t, size_t> i, float v) {
+ .def("__setitem__", [](Matrix &m, std::pair<ssize_t, ssize_t> i, float v) {
if (i.first >= m.rows() || i.second >= m.cols())
throw py::index_error();
m(i.first, i.second) = v;
@@ -105,13 +106,64 @@ test_initializer buffers([](py::module &m) {
.def_buffer([](Matrix &m) -> py::buffer_info {
return py::buffer_info(
m.data(), /* Pointer to buffer */
- sizeof(float), /* Size of one scalar */
- py::format_descriptor<float>::format(), /* Python struct-style format descriptor */
- 2, /* Number of dimensions */
{ m.rows(), m.cols() }, /* Buffer dimensions */
- { sizeof(float) * m.rows(), /* Strides (in bytes) for each index */
+ { sizeof(float) * size_t(m.rows()), /* Strides (in bytes) for each index */
sizeof(float) }
);
})
;
-});
+
+
+ // test_inherited_protocol
+ class SquareMatrix : public Matrix {
+ public:
+ SquareMatrix(ssize_t n) : Matrix(n, n) { }
+ };
+ // Derived classes inherit the buffer protocol and the buffer access function
+ py::class_<SquareMatrix, Matrix>(m, "SquareMatrix")
+ .def(py::init<ssize_t>());
+
+
+ // test_pointer_to_member_fn
+ // Tests that passing a pointer to member to the base class works in
+ // the derived class.
+ struct Buffer {
+ int32_t value = 0;
+
+ py::buffer_info get_buffer_info() {
+ return py::buffer_info(&value, sizeof(value),
+ py::format_descriptor<int32_t>::format(), 1);
+ }
+ };
+ py::class_<Buffer>(m, "Buffer", py::buffer_protocol())
+ .def(py::init<>())
+ .def_readwrite("value", &Buffer::value)
+ .def_buffer(&Buffer::get_buffer_info);
+
+
+ class ConstBuffer {
+ std::unique_ptr<int32_t> value;
+
+ public:
+ int32_t get_value() const { return *value; }
+ void set_value(int32_t v) { *value = v; }
+
+ py::buffer_info get_buffer_info() const {
+ return py::buffer_info(value.get(), sizeof(*value),
+ py::format_descriptor<int32_t>::format(), 1);
+ }
+
+ ConstBuffer() : value(new int32_t{0}) { };
+ };
+ py::class_<ConstBuffer>(m, "ConstBuffer", py::buffer_protocol())
+ .def(py::init<>())
+ .def_property("value", &ConstBuffer::get_value, &ConstBuffer::set_value)
+ .def_buffer(&ConstBuffer::get_buffer_info);
+
+ struct DerivedBuffer : public Buffer { };
+ py::class_<DerivedBuffer>(m, "DerivedBuffer", py::buffer_protocol())
+ .def(py::init<>())
+ .def_readwrite("value", (int32_t DerivedBuffer::*) &DerivedBuffer::value)
+ .def_buffer(&DerivedBuffer::get_buffer_info);
+
+}
diff --git a/ext/pybind11/tests/test_buffers.py b/ext/pybind11/tests/test_buffers.py
index 24843d95a..c348be5dd 100644
--- a/ext/pybind11/tests/test_buffers.py
+++ b/ext/pybind11/tests/test_buffers.py
@@ -1,5 +1,7 @@
+import struct
import pytest
-from pybind11_tests import Matrix, ConstructorStats
+from pybind11_tests import buffers as m
+from pybind11_tests import ConstructorStats
pytestmark = pytest.requires_numpy
@@ -9,17 +11,17 @@ with pytest.suppress(ImportError):
def test_from_python():
with pytest.raises(RuntimeError) as excinfo:
- Matrix(np.array([1, 2, 3])) # trying to assign a 1D array
+ m.Matrix(np.array([1, 2, 3])) # trying to assign a 1D array
assert str(excinfo.value) == "Incompatible buffer format!"
m3 = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32)
- m4 = Matrix(m3)
+ m4 = m.Matrix(m3)
for i in range(m4.rows()):
for j in range(m4.cols()):
assert m3[i, j] == m4[i, j]
- cstats = ConstructorStats.get(Matrix)
+ cstats = ConstructorStats.get(m.Matrix)
assert cstats.alive() == 1
del m3, m4
assert cstats.alive() == 0
@@ -34,25 +36,26 @@ def test_from_python():
# https://bitbucket.org/pypy/pypy/issues/2444
@pytest.unsupported_on_pypy
def test_to_python():
- m = Matrix(5, 5)
+ mat = m.Matrix(5, 5)
+ assert memoryview(mat).shape == (5, 5)
- assert m[2, 3] == 0
- m[2, 3] = 4
- assert m[2, 3] == 4
+ assert mat[2, 3] == 0
+ mat[2, 3] = 4
+ assert mat[2, 3] == 4
- m2 = np.array(m, copy=False)
- assert m2.shape == (5, 5)
- assert abs(m2).sum() == 4
- assert m2[2, 3] == 4
- m2[2, 3] = 5
- assert m2[2, 3] == 5
+ mat2 = np.array(mat, copy=False)
+ assert mat2.shape == (5, 5)
+ assert abs(mat2).sum() == 4
+ assert mat2[2, 3] == 4
+ mat2[2, 3] = 5
+ assert mat2[2, 3] == 5
- cstats = ConstructorStats.get(Matrix)
+ cstats = ConstructorStats.get(m.Matrix)
assert cstats.alive() == 1
- del m
+ del mat
pytest.gc_collect()
assert cstats.alive() == 1
- del m2 # holds an m reference
+ del mat2 # holds a mat reference
pytest.gc_collect()
assert cstats.alive() == 0
assert cstats.values() == ["5x5 matrix"]
@@ -60,3 +63,21 @@ def test_to_python():
# assert cstats.move_constructions >= 0 # Don't invoke any
assert cstats.copy_assignments == 0
assert cstats.move_assignments == 0
+
+
+@pytest.unsupported_on_pypy
+def test_inherited_protocol():
+ """SquareMatrix is derived from Matrix and inherits the buffer protocol"""
+
+ matrix = m.SquareMatrix(5)
+ assert memoryview(matrix).shape == (5, 5)
+ assert np.asarray(matrix).shape == (5, 5)
+
+
+@pytest.unsupported_on_pypy
+def test_pointer_to_member_fn():
+ for cls in [m.Buffer, m.ConstBuffer, m.DerivedBuffer]:
+ buf = cls()
+ buf.value = 0x12345678
+ value = struct.unpack('i', bytearray(buf))[0]
+ assert value == 0x12345678
diff --git a/ext/pybind11/tests/test_builtin_casters.cpp b/ext/pybind11/tests/test_builtin_casters.cpp
new file mode 100644
index 000000000..b73e96ea5
--- /dev/null
+++ b/ext/pybind11/tests/test_builtin_casters.cpp
@@ -0,0 +1,156 @@
+/*
+ tests/test_builtin_casters.cpp -- Casters available without any additional headers
+
+ Copyright (c) 2017 Wenzel Jakob <wenzel.jakob@epfl.ch>
+
+ All rights reserved. Use of this source code is governed by a
+ BSD-style license that can be found in the LICENSE file.
+*/
+
+#include "pybind11_tests.h"
+#include <pybind11/complex.h>
+
+#if defined(_MSC_VER)
+# pragma warning(push)
+# pragma warning(disable: 4127) // warning C4127: Conditional expression is constant
+#endif
+
+TEST_SUBMODULE(builtin_casters, m) {
+ // test_simple_string
+ m.def("string_roundtrip", [](const char *s) { return s; });
+
+ // test_unicode_conversion
+ // Some test characters in utf16 and utf32 encodings. The last one (the 𝐀) contains a null byte
+ char32_t a32 = 0x61 /*a*/, z32 = 0x7a /*z*/, ib32 = 0x203d /*‽*/, cake32 = 0x1f382 /*🎂*/, mathbfA32 = 0x1d400 /*𝐀*/;
+ char16_t b16 = 0x62 /*b*/, z16 = 0x7a, ib16 = 0x203d, cake16_1 = 0xd83c, cake16_2 = 0xdf82, mathbfA16_1 = 0xd835, mathbfA16_2 = 0xdc00;
+ std::wstring wstr;
+ wstr.push_back(0x61); // a
+ wstr.push_back(0x2e18); // ⸘
+ if (sizeof(wchar_t) == 2) { wstr.push_back(mathbfA16_1); wstr.push_back(mathbfA16_2); } // 𝐀, utf16
+ else { wstr.push_back((wchar_t) mathbfA32); } // 𝐀, utf32
+ wstr.push_back(0x7a); // z
+
+ m.def("good_utf8_string", []() { return std::string(u8"Say utf8\u203d \U0001f382 \U0001d400"); }); // Say utf8‽ 🎂 𝐀
+ m.def("good_utf16_string", [=]() { return std::u16string({ b16, ib16, cake16_1, cake16_2, mathbfA16_1, mathbfA16_2, z16 }); }); // b‽🎂𝐀z
+ m.def("good_utf32_string", [=]() { return std::u32string({ a32, mathbfA32, cake32, ib32, z32 }); }); // a𝐀🎂‽z
+ m.def("good_wchar_string", [=]() { return wstr; }); // a‽𝐀z
+ m.def("bad_utf8_string", []() { return std::string("abc\xd0" "def"); });
+ m.def("bad_utf16_string", [=]() { return std::u16string({ b16, char16_t(0xd800), z16 }); });
+ // Under Python 2.7, invalid unicode UTF-32 characters don't appear to trigger UnicodeDecodeError
+ if (PY_MAJOR_VERSION >= 3)
+ m.def("bad_utf32_string", [=]() { return std::u32string({ a32, char32_t(0xd800), z32 }); });
+ if (PY_MAJOR_VERSION >= 3 || sizeof(wchar_t) == 2)
+ m.def("bad_wchar_string", [=]() { return std::wstring({ wchar_t(0x61), wchar_t(0xd800) }); });
+ m.def("u8_Z", []() -> char { return 'Z'; });
+ m.def("u8_eacute", []() -> char { return '\xe9'; });
+ m.def("u16_ibang", [=]() -> char16_t { return ib16; });
+ m.def("u32_mathbfA", [=]() -> char32_t { return mathbfA32; });
+ m.def("wchar_heart", []() -> wchar_t { return 0x2665; });
+
+ // test_single_char_arguments
+ m.attr("wchar_size") = py::cast(sizeof(wchar_t));
+ m.def("ord_char", [](char c) -> int { return static_cast<unsigned char>(c); });
+ m.def("ord_char16", [](char16_t c) -> uint16_t { return c; });
+ m.def("ord_char32", [](char32_t c) -> uint32_t { return c; });
+ m.def("ord_wchar", [](wchar_t c) -> int { return c; });
+
+ // test_bytes_to_string
+ m.def("strlen", [](char *s) { return strlen(s); });
+ m.def("string_length", [](std::string s) { return s.length(); });
+
+ // test_string_view
+#ifdef PYBIND11_HAS_STRING_VIEW
+ m.attr("has_string_view") = true;
+ m.def("string_view_print", [](std::string_view s) { py::print(s, s.size()); });
+ m.def("string_view16_print", [](std::u16string_view s) { py::print(s, s.size()); });
+ m.def("string_view32_print", [](std::u32string_view s) { py::print(s, s.size()); });
+ m.def("string_view_chars", [](std::string_view s) { py::list l; for (auto c : s) l.append((std::uint8_t) c); return l; });
+ m.def("string_view16_chars", [](std::u16string_view s) { py::list l; for (auto c : s) l.append((int) c); return l; });
+ m.def("string_view32_chars", [](std::u32string_view s) { py::list l; for (auto c : s) l.append((int) c); return l; });
+ m.def("string_view_return", []() { return std::string_view(u8"utf8 secret \U0001f382"); });
+ m.def("string_view16_return", []() { return std::u16string_view(u"utf16 secret \U0001f382"); });
+ m.def("string_view32_return", []() { return std::u32string_view(U"utf32 secret \U0001f382"); });
+#endif
+
+ // test_integer_casting
+ m.def("i32_str", [](std::int32_t v) { return std::to_string(v); });
+ m.def("u32_str", [](std::uint32_t v) { return std::to_string(v); });
+ m.def("i64_str", [](std::int64_t v) { return std::to_string(v); });
+ m.def("u64_str", [](std::uint64_t v) { return std::to_string(v); });
+
+ // test_tuple
+ m.def("pair_passthrough", [](std::pair<bool, std::string> input) {
+ return std::make_pair(input.second, input.first);
+ }, "Return a pair in reversed order");
+ m.def("tuple_passthrough", [](std::tuple<bool, std::string, int> input) {
+ return std::make_tuple(std::get<2>(input), std::get<1>(input), std::get<0>(input));
+ }, "Return a triple in reversed order");
+ m.def("empty_tuple", []() { return std::tuple<>(); });
+ static std::pair<RValueCaster, RValueCaster> lvpair;
+ static std::tuple<RValueCaster, RValueCaster, RValueCaster> lvtuple;
+ static std::pair<RValueCaster, std::tuple<RValueCaster, std::pair<RValueCaster, RValueCaster>>> lvnested;
+ m.def("rvalue_pair", []() { return std::make_pair(RValueCaster{}, RValueCaster{}); });
+ m.def("lvalue_pair", []() -> const decltype(lvpair) & { return lvpair; });
+ m.def("rvalue_tuple", []() { return std::make_tuple(RValueCaster{}, RValueCaster{}, RValueCaster{}); });
+ m.def("lvalue_tuple", []() -> const decltype(lvtuple) & { return lvtuple; });
+ m.def("rvalue_nested", []() {
+ return std::make_pair(RValueCaster{}, std::make_tuple(RValueCaster{}, std::make_pair(RValueCaster{}, RValueCaster{}))); });
+ m.def("lvalue_nested", []() -> const decltype(lvnested) & { return lvnested; });
+
+ // test_builtins_cast_return_none
+ m.def("return_none_string", []() -> std::string * { return nullptr; });
+ m.def("return_none_char", []() -> const char * { return nullptr; });
+ m.def("return_none_bool", []() -> bool * { return nullptr; });
+ m.def("return_none_int", []() -> int * { return nullptr; });
+ m.def("return_none_float", []() -> float * { return nullptr; });
+
+ // test_none_deferred
+ m.def("defer_none_cstring", [](char *) { return false; });
+ m.def("defer_none_cstring", [](py::none) { return true; });
+ m.def("defer_none_custom", [](UserType *) { return false; });
+ m.def("defer_none_custom", [](py::none) { return true; });
+ m.def("nodefer_none_void", [](void *) { return true; });
+ m.def("nodefer_none_void", [](py::none) { return false; });
+
+ // test_void_caster
+ m.def("load_nullptr_t", [](std::nullptr_t) {}); // not useful, but it should still compile
+ m.def("cast_nullptr_t", []() { return std::nullptr_t{}; });
+
+ // test_bool_caster
+ m.def("bool_passthrough", [](bool arg) { return arg; });
+ m.def("bool_passthrough_noconvert", [](bool arg) { return arg; }, py::arg().noconvert());
+
+ // test_reference_wrapper
+ m.def("refwrap_builtin", [](std::reference_wrapper<int> p) { return 10 * p.get(); });
+ m.def("refwrap_usertype", [](std::reference_wrapper<UserType> p) { return p.get().value(); });
+ // Not currently supported (std::pair caster has return-by-value cast operator);
+ // triggers static_assert failure.
+ //m.def("refwrap_pair", [](std::reference_wrapper<std::pair<int, int>>) { });
+
+ m.def("refwrap_list", [](bool copy) {
+ static IncType x1(1), x2(2);
+ py::list l;
+ for (auto &f : {std::ref(x1), std::ref(x2)}) {
+ l.append(py::cast(f, copy ? py::return_value_policy::copy
+ : py::return_value_policy::reference));
+ }
+ return l;
+ }, "copy"_a);
+
+ m.def("refwrap_iiw", [](const IncType &w) { return w.value(); });
+ m.def("refwrap_call_iiw", [](IncType &w, py::function f) {
+ py::list l;
+ l.append(f(std::ref(w)));
+ l.append(f(std::cref(w)));
+ IncType x(w.value());
+ l.append(f(std::ref(x)));
+ IncType y(w.value());
+ auto r3 = std::ref(y);
+ l.append(f(r3));
+ return l;
+ });
+
+ // test_complex
+ m.def("complex_cast", [](float x) { return "{}"_s.format(x); });
+ m.def("complex_cast", [](std::complex<float> x) { return "({}, {})"_s.format(x.real(), x.imag()); });
+}
diff --git a/ext/pybind11/tests/test_builtin_casters.py b/ext/pybind11/tests/test_builtin_casters.py
new file mode 100644
index 000000000..bc094a381
--- /dev/null
+++ b/ext/pybind11/tests/test_builtin_casters.py
@@ -0,0 +1,322 @@
+# Python < 3 needs this: coding=utf-8
+import pytest
+
+from pybind11_tests import builtin_casters as m
+from pybind11_tests import UserType, IncType
+
+
+def test_simple_string():
+ assert m.string_roundtrip("const char *") == "const char *"
+
+
+def test_unicode_conversion():
+ """Tests unicode conversion and error reporting."""
+ assert m.good_utf8_string() == u"Say utf8‽ 🎂 𝐀"
+ assert m.good_utf16_string() == u"b‽🎂𝐀z"
+ assert m.good_utf32_string() == u"a𝐀🎂‽z"
+ assert m.good_wchar_string() == u"a⸘𝐀z"
+
+ with pytest.raises(UnicodeDecodeError):
+ m.bad_utf8_string()
+
+ with pytest.raises(UnicodeDecodeError):
+ m.bad_utf16_string()
+
+ # These are provided only if they actually fail (they don't when 32-bit and under Python 2.7)
+ if hasattr(m, "bad_utf32_string"):
+ with pytest.raises(UnicodeDecodeError):
+ m.bad_utf32_string()
+ if hasattr(m, "bad_wchar_string"):
+ with pytest.raises(UnicodeDecodeError):
+ m.bad_wchar_string()
+
+ assert m.u8_Z() == 'Z'
+ assert m.u8_eacute() == u'é'
+ assert m.u16_ibang() == u'‽'
+ assert m.u32_mathbfA() == u'𝐀'
+ assert m.wchar_heart() == u'♥'
+
+
+def test_single_char_arguments():
+ """Tests failures for passing invalid inputs to char-accepting functions"""
+ def toobig_message(r):
+ return "Character code point not in range({0:#x})".format(r)
+ toolong_message = "Expected a character, but multi-character string found"
+
+ assert m.ord_char(u'a') == 0x61 # simple ASCII
+ assert m.ord_char(u'é') == 0xE9 # requires 2 bytes in utf-8, but can be stuffed in a char
+ with pytest.raises(ValueError) as excinfo:
+ assert m.ord_char(u'Ā') == 0x100 # requires 2 bytes, doesn't fit in a char
+ assert str(excinfo.value) == toobig_message(0x100)
+ with pytest.raises(ValueError) as excinfo:
+ assert m.ord_char(u'ab')
+ assert str(excinfo.value) == toolong_message
+
+ assert m.ord_char16(u'a') == 0x61
+ assert m.ord_char16(u'é') == 0xE9
+ assert m.ord_char16(u'Ā') == 0x100
+ assert m.ord_char16(u'‽') == 0x203d
+ assert m.ord_char16(u'♥') == 0x2665
+ with pytest.raises(ValueError) as excinfo:
+ assert m.ord_char16(u'🎂') == 0x1F382 # requires surrogate pair
+ assert str(excinfo.value) == toobig_message(0x10000)
+ with pytest.raises(ValueError) as excinfo:
+ assert m.ord_char16(u'aa')
+ assert str(excinfo.value) == toolong_message
+
+ assert m.ord_char32(u'a') == 0x61
+ assert m.ord_char32(u'é') == 0xE9
+ assert m.ord_char32(u'Ā') == 0x100
+ assert m.ord_char32(u'‽') == 0x203d
+ assert m.ord_char32(u'♥') == 0x2665
+ assert m.ord_char32(u'🎂') == 0x1F382
+ with pytest.raises(ValueError) as excinfo:
+ assert m.ord_char32(u'aa')
+ assert str(excinfo.value) == toolong_message
+
+ assert m.ord_wchar(u'a') == 0x61
+ assert m.ord_wchar(u'é') == 0xE9
+ assert m.ord_wchar(u'Ā') == 0x100
+ assert m.ord_wchar(u'‽') == 0x203d
+ assert m.ord_wchar(u'♥') == 0x2665
+ if m.wchar_size == 2:
+ with pytest.raises(ValueError) as excinfo:
+ assert m.ord_wchar(u'🎂') == 0x1F382 # requires surrogate pair
+ assert str(excinfo.value) == toobig_message(0x10000)
+ else:
+ assert m.ord_wchar(u'🎂') == 0x1F382
+ with pytest.raises(ValueError) as excinfo:
+ assert m.ord_wchar(u'aa')
+ assert str(excinfo.value) == toolong_message
+
+
+def test_bytes_to_string():
+ """Tests the ability to pass bytes to C++ string-accepting functions. Note that this is
+ one-way: the only way to return bytes to Python is via the pybind11::bytes class."""
+ # Issue #816
+ import sys
+ byte = bytes if sys.version_info[0] < 3 else str
+
+ assert m.strlen(byte("hi")) == 2
+ assert m.string_length(byte("world")) == 5
+ assert m.string_length(byte("a\x00b")) == 3
+ assert m.strlen(byte("a\x00b")) == 1 # C-string limitation
+
+ # passing in a utf8 encoded string should work
+ assert m.string_length(u'💩'.encode("utf8")) == 4
+
+
+@pytest.mark.skipif(not hasattr(m, "has_string_view"), reason="no <string_view>")
+def test_string_view(capture):
+ """Tests support for C++17 string_view arguments and return values"""
+ assert m.string_view_chars("Hi") == [72, 105]
+ assert m.string_view_chars("Hi 🎂") == [72, 105, 32, 0xf0, 0x9f, 0x8e, 0x82]
+ assert m.string_view16_chars("Hi 🎂") == [72, 105, 32, 0xd83c, 0xdf82]
+ assert m.string_view32_chars("Hi 🎂") == [72, 105, 32, 127874]
+
+ assert m.string_view_return() == "utf8 secret 🎂"
+ assert m.string_view16_return() == "utf16 secret 🎂"
+ assert m.string_view32_return() == "utf32 secret 🎂"
+
+ with capture:
+ m.string_view_print("Hi")
+ m.string_view_print("utf8 🎂")
+ m.string_view16_print("utf16 🎂")
+ m.string_view32_print("utf32 🎂")
+ assert capture == """
+ Hi 2
+ utf8 🎂 9
+ utf16 🎂 8
+ utf32 🎂 7
+ """
+
+ with capture:
+ m.string_view_print("Hi, ascii")
+ m.string_view_print("Hi, utf8 🎂")
+ m.string_view16_print("Hi, utf16 🎂")
+ m.string_view32_print("Hi, utf32 🎂")
+ assert capture == """
+ Hi, ascii 9
+ Hi, utf8 🎂 13
+ Hi, utf16 🎂 12
+ Hi, utf32 🎂 11
+ """
+
+
+def test_integer_casting():
+ """Issue #929 - out-of-range integer values shouldn't be accepted"""
+ import sys
+ assert m.i32_str(-1) == "-1"
+ assert m.i64_str(-1) == "-1"
+ assert m.i32_str(2000000000) == "2000000000"
+ assert m.u32_str(2000000000) == "2000000000"
+ if sys.version_info < (3,):
+ assert m.i32_str(long(-1)) == "-1" # noqa: F821 undefined name 'long'
+ assert m.i64_str(long(-1)) == "-1" # noqa: F821 undefined name 'long'
+ assert m.i64_str(long(-999999999999)) == "-999999999999" # noqa: F821 undefined name
+ assert m.u64_str(long(999999999999)) == "999999999999" # noqa: F821 undefined name 'long'
+ else:
+ assert m.i64_str(-999999999999) == "-999999999999"
+ assert m.u64_str(999999999999) == "999999999999"
+
+ with pytest.raises(TypeError) as excinfo:
+ m.u32_str(-1)
+ assert "incompatible function arguments" in str(excinfo.value)
+ with pytest.raises(TypeError) as excinfo:
+ m.u64_str(-1)
+ assert "incompatible function arguments" in str(excinfo.value)
+ with pytest.raises(TypeError) as excinfo:
+ m.i32_str(-3000000000)
+ assert "incompatible function arguments" in str(excinfo.value)
+ with pytest.raises(TypeError) as excinfo:
+ m.i32_str(3000000000)
+ assert "incompatible function arguments" in str(excinfo.value)
+
+ if sys.version_info < (3,):
+ with pytest.raises(TypeError) as excinfo:
+ m.u32_str(long(-1)) # noqa: F821 undefined name 'long'
+ assert "incompatible function arguments" in str(excinfo.value)
+ with pytest.raises(TypeError) as excinfo:
+ m.u64_str(long(-1)) # noqa: F821 undefined name 'long'
+ assert "incompatible function arguments" in str(excinfo.value)
+
+
+def test_tuple(doc):
+ """std::pair <-> tuple & std::tuple <-> tuple"""
+ assert m.pair_passthrough((True, "test")) == ("test", True)
+ assert m.tuple_passthrough((True, "test", 5)) == (5, "test", True)
+ # Any sequence can be cast to a std::pair or std::tuple
+ assert m.pair_passthrough([True, "test"]) == ("test", True)
+ assert m.tuple_passthrough([True, "test", 5]) == (5, "test", True)
+ assert m.empty_tuple() == ()
+
+ assert doc(m.pair_passthrough) == """
+ pair_passthrough(arg0: Tuple[bool, str]) -> Tuple[str, bool]
+
+ Return a pair in reversed order
+ """
+ assert doc(m.tuple_passthrough) == """
+ tuple_passthrough(arg0: Tuple[bool, str, int]) -> Tuple[int, str, bool]
+
+ Return a triple in reversed order
+ """
+
+ assert m.rvalue_pair() == ("rvalue", "rvalue")
+ assert m.lvalue_pair() == ("lvalue", "lvalue")
+ assert m.rvalue_tuple() == ("rvalue", "rvalue", "rvalue")
+ assert m.lvalue_tuple() == ("lvalue", "lvalue", "lvalue")
+ assert m.rvalue_nested() == ("rvalue", ("rvalue", ("rvalue", "rvalue")))
+ assert m.lvalue_nested() == ("lvalue", ("lvalue", ("lvalue", "lvalue")))
+
+
+def test_builtins_cast_return_none():
+ """Casters produced with PYBIND11_TYPE_CASTER() should convert nullptr to None"""
+ assert m.return_none_string() is None
+ assert m.return_none_char() is None
+ assert m.return_none_bool() is None
+ assert m.return_none_int() is None
+ assert m.return_none_float() is None
+
+
+def test_none_deferred():
+ """None passed as various argument types should defer to other overloads"""
+ assert not m.defer_none_cstring("abc")
+ assert m.defer_none_cstring(None)
+ assert not m.defer_none_custom(UserType())
+ assert m.defer_none_custom(None)
+ assert m.nodefer_none_void(None)
+
+
+def test_void_caster():
+ assert m.load_nullptr_t(None) is None
+ assert m.cast_nullptr_t() is None
+
+
+def test_reference_wrapper():
+ """std::reference_wrapper for builtin and user types"""
+ assert m.refwrap_builtin(42) == 420
+ assert m.refwrap_usertype(UserType(42)) == 42
+
+ with pytest.raises(TypeError) as excinfo:
+ m.refwrap_builtin(None)
+ assert "incompatible function arguments" in str(excinfo.value)
+
+ with pytest.raises(TypeError) as excinfo:
+ m.refwrap_usertype(None)
+ assert "incompatible function arguments" in str(excinfo.value)
+
+ a1 = m.refwrap_list(copy=True)
+ a2 = m.refwrap_list(copy=True)
+ assert [x.value for x in a1] == [2, 3]
+ assert [x.value for x in a2] == [2, 3]
+ assert not a1[0] is a2[0] and not a1[1] is a2[1]
+
+ b1 = m.refwrap_list(copy=False)
+ b2 = m.refwrap_list(copy=False)
+ assert [x.value for x in b1] == [1, 2]
+ assert [x.value for x in b2] == [1, 2]
+ assert b1[0] is b2[0] and b1[1] is b2[1]
+
+ assert m.refwrap_iiw(IncType(5)) == 5
+ assert m.refwrap_call_iiw(IncType(10), m.refwrap_iiw) == [10, 10, 10, 10]
+
+
+def test_complex_cast():
+ """std::complex casts"""
+ assert m.complex_cast(1) == "1.0"
+ assert m.complex_cast(2j) == "(0.0, 2.0)"
+
+
+def test_bool_caster():
+ """Test bool caster implicit conversions."""
+ convert, noconvert = m.bool_passthrough, m.bool_passthrough_noconvert
+
+ def require_implicit(v):
+ pytest.raises(TypeError, noconvert, v)
+
+ def cant_convert(v):
+ pytest.raises(TypeError, convert, v)
+
+ # straight up bool
+ assert convert(True) is True
+ assert convert(False) is False
+ assert noconvert(True) is True
+ assert noconvert(False) is False
+
+ # None requires implicit conversion
+ require_implicit(None)
+ assert convert(None) is False
+
+ class A(object):
+ def __init__(self, x):
+ self.x = x
+
+ def __nonzero__(self):
+ return self.x
+
+ def __bool__(self):
+ return self.x
+
+ class B(object):
+ pass
+
+ # Arbitrary objects are not accepted
+ cant_convert(object())
+ cant_convert(B())
+
+ # Objects with __nonzero__ / __bool__ defined can be converted
+ require_implicit(A(True))
+ assert convert(A(True)) is True
+ assert convert(A(False)) is False
+
+
+@pytest.requires_numpy
+def test_numpy_bool():
+ import numpy as np
+ convert, noconvert = m.bool_passthrough, m.bool_passthrough_noconvert
+
+ # np.bool_ is not considered implicit
+ assert convert(np.bool_(True)) is True
+ assert convert(np.bool_(False)) is False
+ assert noconvert(np.bool_(True)) is True
+ assert noconvert(np.bool_(False)) is False
diff --git a/ext/pybind11/tests/test_call_policies.cpp b/ext/pybind11/tests/test_call_policies.cpp
new file mode 100644
index 000000000..8642188f9
--- /dev/null
+++ b/ext/pybind11/tests/test_call_policies.cpp
@@ -0,0 +1,98 @@
+/*
+ tests/test_call_policies.cpp -- keep_alive and call_guard
+
+ Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
+
+ All rights reserved. Use of this source code is governed by a
+ BSD-style license that can be found in the LICENSE file.
+*/
+
+#include "pybind11_tests.h"
+
+struct CustomGuard {
+ static bool enabled;
+
+ CustomGuard() { enabled = true; }
+ ~CustomGuard() { enabled = false; }
+
+ static const char *report_status() { return enabled ? "guarded" : "unguarded"; }
+};
+bool CustomGuard::enabled = false;
+
+struct DependentGuard {
+ static bool enabled;
+
+ DependentGuard() { enabled = CustomGuard::enabled; }
+ ~DependentGuard() { enabled = false; }
+
+ static const char *report_status() { return enabled ? "guarded" : "unguarded"; }
+};
+bool DependentGuard::enabled = false;
+
+TEST_SUBMODULE(call_policies, m) {
+ // Parent/Child are used in:
+ // test_keep_alive_argument, test_keep_alive_return_value, test_alive_gc_derived,
+ // test_alive_gc_multi_derived, test_return_none, test_keep_alive_constructor
+ class Child {
+ public:
+ Child() { py::print("Allocating child."); }
+ ~Child() { py::print("Releasing child."); }
+ };
+ py::class_<Child>(m, "Child")
+ .def(py::init<>());
+
+ class Parent {
+ public:
+ Parent() { py::print("Allocating parent."); }
+ ~Parent() { py::print("Releasing parent."); }
+ void addChild(Child *) { }
+ Child *returnChild() { return new Child(); }
+ Child *returnNullChild() { return nullptr; }
+ };
+ py::class_<Parent>(m, "Parent")
+ .def(py::init<>())
+ .def(py::init([](Child *) { return new Parent(); }), py::keep_alive<1, 2>())
+ .def("addChild", &Parent::addChild)
+ .def("addChildKeepAlive", &Parent::addChild, py::keep_alive<1, 2>())
+ .def("returnChild", &Parent::returnChild)
+ .def("returnChildKeepAlive", &Parent::returnChild, py::keep_alive<1, 0>())
+ .def("returnNullChildKeepAliveChild", &Parent::returnNullChild, py::keep_alive<1, 0>())
+ .def("returnNullChildKeepAliveParent", &Parent::returnNullChild, py::keep_alive<0, 1>());
+
+#if !defined(PYPY_VERSION)
+ // test_alive_gc
+ class ParentGC : public Parent {
+ public:
+ using Parent::Parent;
+ };
+ py::class_<ParentGC, Parent>(m, "ParentGC", py::dynamic_attr())
+ .def(py::init<>());
+#endif
+
+ // test_call_guard
+ m.def("unguarded_call", &CustomGuard::report_status);
+ m.def("guarded_call", &CustomGuard::report_status, py::call_guard<CustomGuard>());
+
+ m.def("multiple_guards_correct_order", []() {
+ return CustomGuard::report_status() + std::string(" & ") + DependentGuard::report_status();
+ }, py::call_guard<CustomGuard, DependentGuard>());
+
+ m.def("multiple_guards_wrong_order", []() {
+ return DependentGuard::report_status() + std::string(" & ") + CustomGuard::report_status();
+ }, py::call_guard<DependentGuard, CustomGuard>());
+
+#if defined(WITH_THREAD) && !defined(PYPY_VERSION)
+ // `py::call_guard<py::gil_scoped_release>()` should work in PyPy as well,
+ // but it's unclear how to test it without `PyGILState_GetThisThreadState`.
+ auto report_gil_status = []() {
+ auto is_gil_held = false;
+ if (auto tstate = py::detail::get_thread_state_unchecked())
+ is_gil_held = (tstate == PyGILState_GetThisThreadState());
+
+ return is_gil_held ? "GIL held" : "GIL released";
+ };
+
+ m.def("with_gil", report_gil_status);
+ m.def("without_gil", report_gil_status, py::call_guard<py::gil_scoped_release>());
+#endif
+}
diff --git a/ext/pybind11/tests/test_call_policies.py b/ext/pybind11/tests/test_call_policies.py
new file mode 100644
index 000000000..7c835599c
--- /dev/null
+++ b/ext/pybind11/tests/test_call_policies.py
@@ -0,0 +1,187 @@
+import pytest
+from pybind11_tests import call_policies as m
+from pybind11_tests import ConstructorStats
+
+
+def test_keep_alive_argument(capture):
+ n_inst = ConstructorStats.detail_reg_inst()
+ with capture:
+ p = m.Parent()
+ assert capture == "Allocating parent."
+ with capture:
+ p.addChild(m.Child())
+ assert ConstructorStats.detail_reg_inst() == n_inst + 1
+ assert capture == """
+ Allocating child.
+ Releasing child.
+ """
+ with capture:
+ del p
+ assert ConstructorStats.detail_reg_inst() == n_inst
+ assert capture == "Releasing parent."
+
+ with capture:
+ p = m.Parent()
+ assert capture == "Allocating parent."
+ with capture:
+ p.addChildKeepAlive(m.Child())
+ assert ConstructorStats.detail_reg_inst() == n_inst + 2
+ assert capture == "Allocating child."
+ with capture:
+ del p
+ assert ConstructorStats.detail_reg_inst() == n_inst
+ assert capture == """
+ Releasing parent.
+ Releasing child.
+ """
+
+
+def test_keep_alive_return_value(capture):
+ n_inst = ConstructorStats.detail_reg_inst()
+ with capture:
+ p = m.Parent()
+ assert capture == "Allocating parent."
+ with capture:
+ p.returnChild()
+ assert ConstructorStats.detail_reg_inst() == n_inst + 1
+ assert capture == """
+ Allocating child.
+ Releasing child.
+ """
+ with capture:
+ del p
+ assert ConstructorStats.detail_reg_inst() == n_inst
+ assert capture == "Releasing parent."
+
+ with capture:
+ p = m.Parent()
+ assert capture == "Allocating parent."
+ with capture:
+ p.returnChildKeepAlive()
+ assert ConstructorStats.detail_reg_inst() == n_inst + 2
+ assert capture == "Allocating child."
+ with capture:
+ del p
+ assert ConstructorStats.detail_reg_inst() == n_inst
+ assert capture == """
+ Releasing parent.
+ Releasing child.
+ """
+
+
+# https://bitbucket.org/pypy/pypy/issues/2447
+@pytest.unsupported_on_pypy
+def test_alive_gc(capture):
+ n_inst = ConstructorStats.detail_reg_inst()
+ p = m.ParentGC()
+ p.addChildKeepAlive(m.Child())
+ assert ConstructorStats.detail_reg_inst() == n_inst + 2
+ lst = [p]
+ lst.append(lst) # creates a circular reference
+ with capture:
+ del p, lst
+ assert ConstructorStats.detail_reg_inst() == n_inst
+ assert capture == """
+ Releasing parent.
+ Releasing child.
+ """
+
+
+def test_alive_gc_derived(capture):
+ class Derived(m.Parent):
+ pass
+
+ n_inst = ConstructorStats.detail_reg_inst()
+ p = Derived()
+ p.addChildKeepAlive(m.Child())
+ assert ConstructorStats.detail_reg_inst() == n_inst + 2
+ lst = [p]
+ lst.append(lst) # creates a circular reference
+ with capture:
+ del p, lst
+ assert ConstructorStats.detail_reg_inst() == n_inst
+ assert capture == """
+ Releasing parent.
+ Releasing child.
+ """
+
+
+def test_alive_gc_multi_derived(capture):
+ class Derived(m.Parent, m.Child):
+ def __init__(self):
+ m.Parent.__init__(self)
+ m.Child.__init__(self)
+
+ n_inst = ConstructorStats.detail_reg_inst()
+ p = Derived()
+ p.addChildKeepAlive(m.Child())
+ # +3 rather than +2 because Derived corresponds to two registered instances
+ assert ConstructorStats.detail_reg_inst() == n_inst + 3
+ lst = [p]
+ lst.append(lst) # creates a circular reference
+ with capture:
+ del p, lst
+ assert ConstructorStats.detail_reg_inst() == n_inst
+ assert capture == """
+ Releasing parent.
+ Releasing child.
+ Releasing child.
+ """
+
+
+def test_return_none(capture):
+ n_inst = ConstructorStats.detail_reg_inst()
+ with capture:
+ p = m.Parent()
+ assert capture == "Allocating parent."
+ with capture:
+ p.returnNullChildKeepAliveChild()
+ assert ConstructorStats.detail_reg_inst() == n_inst + 1
+ assert capture == ""
+ with capture:
+ del p
+ assert ConstructorStats.detail_reg_inst() == n_inst
+ assert capture == "Releasing parent."
+
+ with capture:
+ p = m.Parent()
+ assert capture == "Allocating parent."
+ with capture:
+ p.returnNullChildKeepAliveParent()
+ assert ConstructorStats.detail_reg_inst() == n_inst + 1
+ assert capture == ""
+ with capture:
+ del p
+ assert ConstructorStats.detail_reg_inst() == n_inst
+ assert capture == "Releasing parent."
+
+
+def test_keep_alive_constructor(capture):
+ n_inst = ConstructorStats.detail_reg_inst()
+
+ with capture:
+ p = m.Parent(m.Child())
+ assert ConstructorStats.detail_reg_inst() == n_inst + 2
+ assert capture == """
+ Allocating child.
+ Allocating parent.
+ """
+ with capture:
+ del p
+ assert ConstructorStats.detail_reg_inst() == n_inst
+ assert capture == """
+ Releasing parent.
+ Releasing child.
+ """
+
+
+def test_call_guard():
+ assert m.unguarded_call() == "unguarded"
+ assert m.guarded_call() == "guarded"
+
+ assert m.multiple_guards_correct_order() == "guarded & guarded"
+ assert m.multiple_guards_wrong_order() == "unguarded & guarded"
+
+ if hasattr(m, "with_gil"):
+ assert m.with_gil() == "GIL held"
+ assert m.without_gil() == "GIL released"
diff --git a/ext/pybind11/tests/test_callbacks.cpp b/ext/pybind11/tests/test_callbacks.cpp
index f89cc1c79..273eacc30 100644
--- a/ext/pybind11/tests/test_callbacks.cpp
+++ b/ext/pybind11/tests/test_callbacks.cpp
@@ -12,97 +12,20 @@
#include <pybind11/functional.h>
-py::object test_callback1(py::object func) {
- return func();
-}
-
-py::tuple test_callback2(py::object func) {
- return func("Hello", 'x', true, 5);
-}
-
-std::string test_callback3(const std::function<int(int)> &func) {
- return "func(43) = " + std::to_string(func(43));
-}
-
-std::function<int(int)> test_callback4() {
- return [](int i) { return i+1; };
-}
-
-py::cpp_function test_callback5() {
- return py::cpp_function([](int i) { return i+1; },
- py::arg("number"));
-}
-
int dummy_function(int i) { return i + 1; }
-int dummy_function2(int i, int j) { return i + j; }
-std::function<int(int)> roundtrip(std::function<int(int)> f, bool expect_none = false) {
- if (expect_none && f) {
- throw std::runtime_error("Expected None to be converted to empty std::function");
- }
- return f;
-}
-std::string test_dummy_function(const std::function<int(int)> &f) {
- using fn_type = int (*)(int);
- auto result = f.target<fn_type>();
- if (!result) {
- auto r = f(1);
- return "can't convert to function pointer: eval(1) = " + std::to_string(r);
- } else if (*result == dummy_function) {
- auto r = (*result)(1);
- return "matches dummy_function: eval(1) = " + std::to_string(r);
- } else {
- return "argument does NOT match dummy_function. This should never happen!";
- }
-}
+TEST_SUBMODULE(callbacks, m) {
+ // test_callbacks, test_function_signatures
+ m.def("test_callback1", [](py::object func) { return func(); });
+ m.def("test_callback2", [](py::object func) { return func("Hello", 'x', true, 5); });
+ m.def("test_callback3", [](const std::function<int(int)> &func) {
+ return "func(43) = " + std::to_string(func(43)); });
+ m.def("test_callback4", []() -> std::function<int(int)> { return [](int i) { return i+1; }; });
+ m.def("test_callback5", []() {
+ return py::cpp_function([](int i) { return i+1; }, py::arg("number"));
+ });
-struct Payload {
- Payload() {
- print_default_created(this);
- }
- ~Payload() {
- print_destroyed(this);
- }
- Payload(const Payload &) {
- print_copy_created(this);
- }
- Payload(Payload &&) {
- print_move_created(this);
- }
-};
-
-/// Something to trigger a conversion error
-struct Unregistered {};
-
-class AbstractBase {
-public:
- virtual unsigned int func() = 0;
-};
-
-void func_accepting_func_accepting_base(std::function<double(AbstractBase&)>) { }
-
-struct MovableObject {
- bool valid = true;
-
- MovableObject() = default;
- MovableObject(const MovableObject &) = default;
- MovableObject &operator=(const MovableObject &) = default;
- MovableObject(MovableObject &&o) : valid(o.valid) { o.valid = false; }
- MovableObject &operator=(MovableObject &&o) {
- valid = o.valid;
- o.valid = false;
- return *this;
- }
-};
-
-test_initializer callbacks([](py::module &m) {
- m.def("test_callback1", &test_callback1);
- m.def("test_callback2", &test_callback2);
- m.def("test_callback3", &test_callback3);
- m.def("test_callback4", &test_callback4);
- m.def("test_callback5", &test_callback5);
-
- // Test keyword args and generalized unpacking
+ // test_keyword_args_and_generalized_unpacking
m.def("test_tuple_unpacking", [](py::function f) {
auto t1 = py::make_tuple(2, 3);
auto t2 = py::make_tuple(5, 6);
@@ -144,13 +67,22 @@ test_initializer callbacks([](py::module &m) {
});
m.def("test_arg_conversion_error1", [](py::function f) {
- f(234, Unregistered(), "kw"_a=567);
+ f(234, UnregisteredType(), "kw"_a=567);
});
m.def("test_arg_conversion_error2", [](py::function f) {
- f(234, "expected_name"_a=Unregistered(), "kw"_a=567);
+ f(234, "expected_name"_a=UnregisteredType(), "kw"_a=567);
});
+ // test_lambda_closure_cleanup
+ struct Payload {
+ Payload() { print_default_created(this); }
+ ~Payload() { print_destroyed(this); }
+ Payload(const Payload &) { print_copy_created(this); }
+ Payload(Payload &&) { print_move_created(this); }
+ };
+ // Export the payload constructor statistics for testing purposes:
+ m.def("payload_cstats", &ConstructorStats::get<Payload>);
/* Test cleanup of lambda closure */
m.def("test_cleanup", []() -> std::function<void(void)> {
Payload p;
@@ -161,22 +93,57 @@ test_initializer callbacks([](py::module &m) {
};
});
+ // test_cpp_function_roundtrip
/* Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer */
m.def("dummy_function", &dummy_function);
- m.def("dummy_function2", &dummy_function2);
- m.def("roundtrip", &roundtrip, py::arg("f"), py::arg("expect_none")=false);
- m.def("test_dummy_function", &test_dummy_function);
- // Export the payload constructor statistics for testing purposes:
- m.def("payload_cstats", &ConstructorStats::get<Payload>);
-
- m.def("func_accepting_func_accepting_base",
- func_accepting_func_accepting_base);
+ m.def("dummy_function2", [](int i, int j) { return i + j; });
+ m.def("roundtrip", [](std::function<int(int)> f, bool expect_none = false) {
+ if (expect_none && f)
+ throw std::runtime_error("Expected None to be converted to empty std::function");
+ return f;
+ }, py::arg("f"), py::arg("expect_none")=false);
+ m.def("test_dummy_function", [](const std::function<int(int)> &f) -> std::string {
+ using fn_type = int (*)(int);
+ auto result = f.target<fn_type>();
+ if (!result) {
+ auto r = f(1);
+ return "can't convert to function pointer: eval(1) = " + std::to_string(r);
+ } else if (*result == dummy_function) {
+ auto r = (*result)(1);
+ return "matches dummy_function: eval(1) = " + std::to_string(r);
+ } else {
+ return "argument does NOT match dummy_function. This should never happen!";
+ }
+ });
+ class AbstractBase { public: virtual unsigned int func() = 0; };
+ m.def("func_accepting_func_accepting_base", [](std::function<double(AbstractBase&)>) { });
+
+ struct MovableObject {
+ bool valid = true;
+
+ MovableObject() = default;
+ MovableObject(const MovableObject &) = default;
+ MovableObject &operator=(const MovableObject &) = default;
+ MovableObject(MovableObject &&o) : valid(o.valid) { o.valid = false; }
+ MovableObject &operator=(MovableObject &&o) {
+ valid = o.valid;
+ o.valid = false;
+ return *this;
+ }
+ };
py::class_<MovableObject>(m, "MovableObject");
+ // test_movable_object
m.def("callback_with_movable", [](std::function<void(MovableObject &)> f) {
auto x = MovableObject();
f(x); // lvalue reference shouldn't move out object
return x.valid; // must still return `true`
- });
-});
+ });
+
+ // test_bound_method_callback
+ struct CppBoundMethodTest {};
+ py::class_<CppBoundMethodTest>(m, "CppBoundMethodTest")
+ .def(py::init<>())
+ .def("triple", [](CppBoundMethodTest &, int val) { return 3 * val; });
+}
diff --git a/ext/pybind11/tests/test_callbacks.py b/ext/pybind11/tests/test_callbacks.py
index f94e7b64c..93c42c22b 100644
--- a/ext/pybind11/tests/test_callbacks.py
+++ b/ext/pybind11/tests/test_callbacks.py
@@ -1,10 +1,9 @@
import pytest
+from pybind11_tests import callbacks as m
def test_callbacks():
from functools import partial
- from pybind11_tests import (test_callback1, test_callback2, test_callback3,
- test_callback4, test_callback5)
def func1():
return "func1"
@@ -15,58 +14,65 @@ def test_callbacks():
def func3(a):
return "func3({})".format(a)
- assert test_callback1(func1) == "func1"
- assert test_callback2(func2) == ("func2", "Hello", "x", True, 5)
- assert test_callback1(partial(func2, 1, 2, 3, 4)) == ("func2", 1, 2, 3, 4)
- assert test_callback1(partial(func3, "partial")) == "func3(partial)"
- assert test_callback3(lambda i: i + 1) == "func(43) = 44"
+ assert m.test_callback1(func1) == "func1"
+ assert m.test_callback2(func2) == ("func2", "Hello", "x", True, 5)
+ assert m.test_callback1(partial(func2, 1, 2, 3, 4)) == ("func2", 1, 2, 3, 4)
+ assert m.test_callback1(partial(func3, "partial")) == "func3(partial)"
+ assert m.test_callback3(lambda i: i + 1) == "func(43) = 44"
- f = test_callback4()
+ f = m.test_callback4()
assert f(43) == 44
- f = test_callback5()
+ f = m.test_callback5()
assert f(number=43) == 44
+def test_bound_method_callback():
+ # Bound Python method:
+ class MyClass:
+ def double(self, val):
+ return 2 * val
+
+ z = MyClass()
+ assert m.test_callback3(z.double) == "func(43) = 86"
+
+ z = m.CppBoundMethodTest()
+ assert m.test_callback3(z.triple) == "func(43) = 129"
+
+
def test_keyword_args_and_generalized_unpacking():
- from pybind11_tests import (test_tuple_unpacking, test_dict_unpacking, test_keyword_args,
- test_unpacking_and_keywords1, test_unpacking_and_keywords2,
- test_unpacking_error1, test_unpacking_error2,
- test_arg_conversion_error1, test_arg_conversion_error2)
def f(*args, **kwargs):
return args, kwargs
- assert test_tuple_unpacking(f) == (("positional", 1, 2, 3, 4, 5, 6), {})
- assert test_dict_unpacking(f) == (("positional", 1), {"key": "value", "a": 1, "b": 2})
- assert test_keyword_args(f) == ((), {"x": 10, "y": 20})
- assert test_unpacking_and_keywords1(f) == ((1, 2), {"c": 3, "d": 4})
- assert test_unpacking_and_keywords2(f) == (
+ assert m.test_tuple_unpacking(f) == (("positional", 1, 2, 3, 4, 5, 6), {})
+ assert m.test_dict_unpacking(f) == (("positional", 1), {"key": "value", "a": 1, "b": 2})
+ assert m.test_keyword_args(f) == ((), {"x": 10, "y": 20})
+ assert m.test_unpacking_and_keywords1(f) == ((1, 2), {"c": 3, "d": 4})
+ assert m.test_unpacking_and_keywords2(f) == (
("positional", 1, 2, 3, 4, 5),
{"key": "value", "a": 1, "b": 2, "c": 3, "d": 4, "e": 5}
)
with pytest.raises(TypeError) as excinfo:
- test_unpacking_error1(f)
+ m.test_unpacking_error1(f)
assert "Got multiple values for keyword argument" in str(excinfo.value)
with pytest.raises(TypeError) as excinfo:
- test_unpacking_error2(f)
+ m.test_unpacking_error2(f)
assert "Got multiple values for keyword argument" in str(excinfo.value)
with pytest.raises(RuntimeError) as excinfo:
- test_arg_conversion_error1(f)
+ m.test_arg_conversion_error1(f)
assert "Unable to convert call argument" in str(excinfo.value)
with pytest.raises(RuntimeError) as excinfo:
- test_arg_conversion_error2(f)
+ m.test_arg_conversion_error2(f)
assert "Unable to convert call argument" in str(excinfo.value)
def test_lambda_closure_cleanup():
- from pybind11_tests import test_cleanup, payload_cstats
-
- test_cleanup()
- cstats = payload_cstats()
+ m.test_cleanup()
+ cstats = m.payload_cstats()
assert cstats.alive() == 0
assert cstats.copy_constructions == 1
assert cstats.move_constructions >= 1
@@ -74,31 +80,28 @@ def test_lambda_closure_cleanup():
def test_cpp_function_roundtrip():
"""Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer"""
- from pybind11_tests import dummy_function, dummy_function2, test_dummy_function, roundtrip
- assert test_dummy_function(dummy_function) == "matches dummy_function: eval(1) = 2"
- assert test_dummy_function(roundtrip(dummy_function)) == "matches dummy_function: eval(1) = 2"
- assert roundtrip(None, expect_none=True) is None
- assert test_dummy_function(lambda x: x + 2) == "can't convert to function pointer: eval(1) = 3"
+ assert m.test_dummy_function(m.dummy_function) == "matches dummy_function: eval(1) = 2"
+ assert (m.test_dummy_function(m.roundtrip(m.dummy_function)) ==
+ "matches dummy_function: eval(1) = 2")
+ assert m.roundtrip(None, expect_none=True) is None
+ assert (m.test_dummy_function(lambda x: x + 2) ==
+ "can't convert to function pointer: eval(1) = 3")
with pytest.raises(TypeError) as excinfo:
- test_dummy_function(dummy_function2)
+ m.test_dummy_function(m.dummy_function2)
assert "incompatible function arguments" in str(excinfo.value)
with pytest.raises(TypeError) as excinfo:
- test_dummy_function(lambda x, y: x + y)
+ m.test_dummy_function(lambda x, y: x + y)
assert any(s in str(excinfo.value) for s in ("missing 1 required positional argument",
"takes exactly 2 arguments"))
def test_function_signatures(doc):
- from pybind11_tests import test_callback3, test_callback4
-
- assert doc(test_callback3) == "test_callback3(arg0: Callable[[int], int]) -> str"
- assert doc(test_callback4) == "test_callback4() -> Callable[[int], int]"
+ assert doc(m.test_callback3) == "test_callback3(arg0: Callable[[int], int]) -> str"
+ assert doc(m.test_callback4) == "test_callback4() -> Callable[[int], int]"
def test_movable_object():
- from pybind11_tests import callback_with_movable
-
- assert callback_with_movable(lambda _: None) is True
+ assert m.callback_with_movable(lambda _: None) is True
diff --git a/ext/pybind11/tests/test_chrono.cpp b/ext/pybind11/tests/test_chrono.cpp
index fcc1b6185..195a93bba 100644
--- a/ext/pybind11/tests/test_chrono.cpp
+++ b/ext/pybind11/tests/test_chrono.cpp
@@ -8,58 +8,40 @@
BSD-style license that can be found in the LICENSE file.
*/
-
#include "pybind11_tests.h"
-#include "constructor_stats.h"
#include <pybind11/chrono.h>
-// Return the current time off the wall clock
-std::chrono::system_clock::time_point test_chrono1() {
- return std::chrono::system_clock::now();
-}
+TEST_SUBMODULE(chrono, m) {
+ using system_time = std::chrono::system_clock::time_point;
+ using steady_time = std::chrono::steady_clock::time_point;
+ // test_chrono_system_clock
+ // Return the current time off the wall clock
+ m.def("test_chrono1", []() { return std::chrono::system_clock::now(); });
-// Round trip the passed in system clock time
-std::chrono::system_clock::time_point test_chrono2(std::chrono::system_clock::time_point t) {
- return t;
-}
-
-// Round trip the passed in duration
-std::chrono::system_clock::duration test_chrono3(std::chrono::system_clock::duration d) {
- return d;
-}
+ // test_chrono_system_clock_roundtrip
+ // Round trip the passed in system clock time
+ m.def("test_chrono2", [](system_time t) { return t; });
-// Difference between two passed in time_points
-std::chrono::system_clock::duration test_chrono4(std::chrono::system_clock::time_point a, std::chrono::system_clock::time_point b) {
- return a - b;
-}
+ // test_chrono_duration_roundtrip
+ // Round trip the passed in duration
+ m.def("test_chrono3", [](std::chrono::system_clock::duration d) { return d; });
-// Return the current time off the steady_clock
-std::chrono::steady_clock::time_point test_chrono5() {
- return std::chrono::steady_clock::now();
-}
+ // test_chrono_duration_subtraction_equivalence
+ // Difference between two passed in time_points
+ m.def("test_chrono4", [](system_time a, system_time b) { return a - b; });
-// Round trip a steady clock timepoint
-std::chrono::steady_clock::time_point test_chrono6(std::chrono::steady_clock::time_point t) {
- return t;
-}
+ // test_chrono_steady_clock
+ // Return the current time off the steady_clock
+ m.def("test_chrono5", []() { return std::chrono::steady_clock::now(); });
-// Roundtrip a duration in microseconds from a float argument
-std::chrono::microseconds test_chrono7(std::chrono::microseconds t) {
- return t;
-}
+ // test_chrono_steady_clock_roundtrip
+ // Round trip a steady clock timepoint
+ m.def("test_chrono6", [](steady_time t) { return t; });
-// Float durations (issue #719)
-std::chrono::duration<double> test_chrono_float_diff(std::chrono::duration<float> a, std::chrono::duration<float> b) {
- return a - b;
+ // test_floating_point_duration
+ // Roundtrip a duration in microseconds from a float argument
+ m.def("test_chrono7", [](std::chrono::microseconds t) { return t; });
+ // Float durations (issue #719)
+ m.def("test_chrono_float_diff", [](std::chrono::duration<float> a, std::chrono::duration<float> b) {
+ return a - b; });
}
-
-test_initializer chrono([] (py::module &m) {
- m.def("test_chrono1", &test_chrono1);
- m.def("test_chrono2", &test_chrono2);
- m.def("test_chrono3", &test_chrono3);
- m.def("test_chrono4", &test_chrono4);
- m.def("test_chrono5", &test_chrono5);
- m.def("test_chrono6", &test_chrono6);
- m.def("test_chrono7", &test_chrono7);
- m.def("test_chrono_float_diff", &test_chrono_float_diff);
-});
diff --git a/ext/pybind11/tests/test_chrono.py b/ext/pybind11/tests/test_chrono.py
index 55094edbf..2b75bd191 100644
--- a/ext/pybind11/tests/test_chrono.py
+++ b/ext/pybind11/tests/test_chrono.py
@@ -1,11 +1,11 @@
+from pybind11_tests import chrono as m
+import datetime
def test_chrono_system_clock():
- from pybind11_tests import test_chrono1
- import datetime
# Get the time from both c++ and datetime
- date1 = test_chrono1()
+ date1 = m.test_chrono1()
date2 = datetime.datetime.today()
# The returned value should be a datetime
@@ -25,13 +25,10 @@ def test_chrono_system_clock():
def test_chrono_system_clock_roundtrip():
- from pybind11_tests import test_chrono2
- import datetime
-
date1 = datetime.datetime.today()
# Roundtrip the time
- date2 = test_chrono2(date1)
+ date2 = m.test_chrono2(date1)
# The returned value should be a datetime
assert isinstance(date2, datetime.datetime)
@@ -44,8 +41,6 @@ def test_chrono_system_clock_roundtrip():
def test_chrono_duration_roundtrip():
- from pybind11_tests import test_chrono3
- import datetime
# Get the difference between two times (a timedelta)
date1 = datetime.datetime.today()
@@ -55,7 +50,7 @@ def test_chrono_duration_roundtrip():
# Make sure this is a timedelta
assert isinstance(diff, datetime.timedelta)
- cpp_diff = test_chrono3(diff)
+ cpp_diff = m.test_chrono3(diff)
assert cpp_diff.days == diff.days
assert cpp_diff.seconds == diff.seconds
@@ -63,14 +58,12 @@ def test_chrono_duration_roundtrip():
def test_chrono_duration_subtraction_equivalence():
- from pybind11_tests import test_chrono4
- import datetime
date1 = datetime.datetime.today()
date2 = datetime.datetime.today()
diff = date2 - date1
- cpp_diff = test_chrono4(date2, date1)
+ cpp_diff = m.test_chrono4(date2, date1)
assert cpp_diff.days == diff.days
assert cpp_diff.seconds == diff.seconds
@@ -78,22 +71,13 @@ def test_chrono_duration_subtraction_equivalence():
def test_chrono_steady_clock():
- from pybind11_tests import test_chrono5
- import datetime
-
- time1 = test_chrono5()
- time2 = test_chrono5()
-
+ time1 = m.test_chrono5()
assert isinstance(time1, datetime.timedelta)
- assert isinstance(time2, datetime.timedelta)
def test_chrono_steady_clock_roundtrip():
- from pybind11_tests import test_chrono6
- import datetime
-
time1 = datetime.timedelta(days=10, seconds=10, microseconds=100)
- time2 = test_chrono6(time1)
+ time2 = m.test_chrono6(time1)
assert isinstance(time2, datetime.timedelta)
@@ -104,17 +88,14 @@ def test_chrono_steady_clock_roundtrip():
def test_floating_point_duration():
- from pybind11_tests import test_chrono7, test_chrono_float_diff
- import datetime
-
- # Test using 35.525123 seconds as an example floating point number in seconds
- time = test_chrono7(35.525123)
+ # Test using a floating point number in seconds
+ time = m.test_chrono7(35.525123)
assert isinstance(time, datetime.timedelta)
assert time.seconds == 35
assert 525122 <= time.microseconds <= 525123
- diff = test_chrono_float_diff(43.789012, 1.123456)
+ diff = m.test_chrono_float_diff(43.789012, 1.123456)
assert diff.seconds == 42
assert 665556 <= diff.microseconds <= 665557
diff --git a/ext/pybind11/tests/test_class.cpp b/ext/pybind11/tests/test_class.cpp
new file mode 100644
index 000000000..222190617
--- /dev/null
+++ b/ext/pybind11/tests/test_class.cpp
@@ -0,0 +1,357 @@
+/*
+ tests/test_class.cpp -- test py::class_ definitions and basic functionality
+
+ Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
+
+ All rights reserved. Use of this source code is governed by a
+ BSD-style license that can be found in the LICENSE file.
+*/
+
+#include "pybind11_tests.h"
+#include "constructor_stats.h"
+#include "local_bindings.h"
+
+TEST_SUBMODULE(class_, m) {
+ // test_instance
+ struct NoConstructor {
+ static NoConstructor *new_instance() {
+ auto *ptr = new NoConstructor();
+ print_created(ptr, "via new_instance");
+ return ptr;
+ }
+ ~NoConstructor() { print_destroyed(this); }
+ };
+
+ py::class_<NoConstructor>(m, "NoConstructor")
+ .def_static("new_instance", &NoConstructor::new_instance, "Return an instance");
+
+ // test_inheritance
+ class Pet {
+ public:
+ Pet(const std::string &name, const std::string &species)
+ : m_name(name), m_species(species) {}
+ std::string name() const { return m_name; }
+ std::string species() const { return m_species; }
+ private:
+ std::string m_name;
+ std::string m_species;
+ };
+
+ class Dog : public Pet {
+ public:
+ Dog(const std::string &name) : Pet(name, "dog") {}
+ std::string bark() const { return "Woof!"; }
+ };
+
+ class Rabbit : public Pet {
+ public:
+ Rabbit(const std::string &name) : Pet(name, "parrot") {}
+ };
+
+ class Hamster : public Pet {
+ public:
+ Hamster(const std::string &name) : Pet(name, "rodent") {}
+ };
+
+ class Chimera : public Pet {
+ Chimera() : Pet("Kimmy", "chimera") {}
+ };
+
+ py::class_<Pet> pet_class(m, "Pet");
+ pet_class
+ .def(py::init<std::string, std::string>())
+ .def("name", &Pet::name)
+ .def("species", &Pet::species);
+
+ /* One way of declaring a subclass relationship: reference parent's class_ object */
+ py::class_<Dog>(m, "Dog", pet_class)
+ .def(py::init<std::string>());
+
+ /* Another way of declaring a subclass relationship: reference parent's C++ type */
+ py::class_<Rabbit, Pet>(m, "Rabbit")
+ .def(py::init<std::string>());
+
+ /* And another: list parent in class template arguments */
+ py::class_<Hamster, Pet>(m, "Hamster")
+ .def(py::init<std::string>());
+
+ /* Constructors are not inherited by default */
+ py::class_<Chimera, Pet>(m, "Chimera");
+
+ m.def("pet_name_species", [](const Pet &pet) { return pet.name() + " is a " + pet.species(); });
+ m.def("dog_bark", [](const Dog &dog) { return dog.bark(); });
+
+ // test_automatic_upcasting
+ struct BaseClass { virtual ~BaseClass() {} };
+ struct DerivedClass1 : BaseClass { };
+ struct DerivedClass2 : BaseClass { };
+
+ py::class_<BaseClass>(m, "BaseClass").def(py::init<>());
+ py::class_<DerivedClass1>(m, "DerivedClass1").def(py::init<>());
+ py::class_<DerivedClass2>(m, "DerivedClass2").def(py::init<>());
+
+ m.def("return_class_1", []() -> BaseClass* { return new DerivedClass1(); });
+ m.def("return_class_2", []() -> BaseClass* { return new DerivedClass2(); });
+ m.def("return_class_n", [](int n) -> BaseClass* {
+ if (n == 1) return new DerivedClass1();
+ if (n == 2) return new DerivedClass2();
+ return new BaseClass();
+ });
+ m.def("return_none", []() -> BaseClass* { return nullptr; });
+
+ // test_isinstance
+ m.def("check_instances", [](py::list l) {
+ return py::make_tuple(
+ py::isinstance<py::tuple>(l[0]),
+ py::isinstance<py::dict>(l[1]),
+ py::isinstance<Pet>(l[2]),
+ py::isinstance<Pet>(l[3]),
+ py::isinstance<Dog>(l[4]),
+ py::isinstance<Rabbit>(l[5]),
+ py::isinstance<UnregisteredType>(l[6])
+ );
+ });
+
+ // test_mismatched_holder
+ struct MismatchBase1 { };
+ struct MismatchDerived1 : MismatchBase1 { };
+
+ struct MismatchBase2 { };
+ struct MismatchDerived2 : MismatchBase2 { };
+
+ m.def("mismatched_holder_1", []() {
+ auto mod = py::module::import("__main__");
+ py::class_<MismatchBase1, std::shared_ptr<MismatchBase1>>(mod, "MismatchBase1");
+ py::class_<MismatchDerived1, MismatchBase1>(mod, "MismatchDerived1");
+ });
+ m.def("mismatched_holder_2", []() {
+ auto mod = py::module::import("__main__");
+ py::class_<MismatchBase2>(mod, "MismatchBase2");
+ py::class_<MismatchDerived2, std::shared_ptr<MismatchDerived2>,
+ MismatchBase2>(mod, "MismatchDerived2");
+ });
+
+ // test_override_static
+ // #511: problem with inheritance + overwritten def_static
+ struct MyBase {
+ static std::unique_ptr<MyBase> make() {
+ return std::unique_ptr<MyBase>(new MyBase());
+ }
+ };
+
+ struct MyDerived : MyBase {
+ static std::unique_ptr<MyDerived> make() {
+ return std::unique_ptr<MyDerived>(new MyDerived());
+ }
+ };
+
+ py::class_<MyBase>(m, "MyBase")
+ .def_static("make", &MyBase::make);
+
+ py::class_<MyDerived, MyBase>(m, "MyDerived")
+ .def_static("make", &MyDerived::make)
+ .def_static("make2", &MyDerived::make);
+
+ // test_implicit_conversion_life_support
+ struct ConvertibleFromUserType {
+ int i;
+
+ ConvertibleFromUserType(UserType u) : i(u.value()) { }
+ };
+
+ py::class_<ConvertibleFromUserType>(m, "AcceptsUserType")
+ .def(py::init<UserType>());
+ py::implicitly_convertible<UserType, ConvertibleFromUserType>();
+
+ m.def("implicitly_convert_argument", [](const ConvertibleFromUserType &r) { return r.i; });
+ m.def("implicitly_convert_variable", [](py::object o) {
+ // `o` is `UserType` and `r` is a reference to a temporary created by implicit
+ // conversion. This is valid when called inside a bound function because the temp
+ // object is attached to the same life support system as the arguments.
+ const auto &r = o.cast<const ConvertibleFromUserType &>();
+ return r.i;
+ });
+ m.add_object("implicitly_convert_variable_fail", [&] {
+ auto f = [](PyObject *, PyObject *args) -> PyObject * {
+ auto o = py::reinterpret_borrow<py::tuple>(args)[0];
+ try { // It should fail here because there is no life support.
+ o.cast<const ConvertibleFromUserType &>();
+ } catch (const py::cast_error &e) {
+ return py::str(e.what()).release().ptr();
+ }
+ return py::str().release().ptr();
+ };
+
+ auto def = new PyMethodDef{"f", f, METH_VARARGS, nullptr};
+ return py::reinterpret_steal<py::object>(PyCFunction_NewEx(def, nullptr, m.ptr()));
+ }());
+
+ // test_operator_new_delete
+ struct HasOpNewDel {
+ std::uint64_t i;
+ static void *operator new(size_t s) { py::print("A new", s); return ::operator new(s); }
+ static void *operator new(size_t s, void *ptr) { py::print("A placement-new", s); return ptr; }
+ static void operator delete(void *p) { py::print("A delete"); return ::operator delete(p); }
+ };
+ struct HasOpNewDelSize {
+ std::uint32_t i;
+ static void *operator new(size_t s) { py::print("B new", s); return ::operator new(s); }
+ static void *operator new(size_t s, void *ptr) { py::print("B placement-new", s); return ptr; }
+ static void operator delete(void *p, size_t s) { py::print("B delete", s); return ::operator delete(p); }
+ };
+ struct AliasedHasOpNewDelSize {
+ std::uint64_t i;
+ static void *operator new(size_t s) { py::print("C new", s); return ::operator new(s); }
+ static void *operator new(size_t s, void *ptr) { py::print("C placement-new", s); return ptr; }
+ static void operator delete(void *p, size_t s) { py::print("C delete", s); return ::operator delete(p); }
+ virtual ~AliasedHasOpNewDelSize() = default;
+ };
+ struct PyAliasedHasOpNewDelSize : AliasedHasOpNewDelSize {
+ PyAliasedHasOpNewDelSize() = default;
+ PyAliasedHasOpNewDelSize(int) { }
+ std::uint64_t j;
+ };
+ struct HasOpNewDelBoth {
+ std::uint32_t i[8];
+ static void *operator new(size_t s) { py::print("D new", s); return ::operator new(s); }
+ static void *operator new(size_t s, void *ptr) { py::print("D placement-new", s); return ptr; }
+ static void operator delete(void *p) { py::print("D delete"); return ::operator delete(p); }
+ static void operator delete(void *p, size_t s) { py::print("D wrong delete", s); return ::operator delete(p); }
+ };
+ py::class_<HasOpNewDel>(m, "HasOpNewDel").def(py::init<>());
+ py::class_<HasOpNewDelSize>(m, "HasOpNewDelSize").def(py::init<>());
+ py::class_<HasOpNewDelBoth>(m, "HasOpNewDelBoth").def(py::init<>());
+ py::class_<AliasedHasOpNewDelSize, PyAliasedHasOpNewDelSize> aliased(m, "AliasedHasOpNewDelSize");
+ aliased.def(py::init<>());
+ aliased.attr("size_noalias") = py::int_(sizeof(AliasedHasOpNewDelSize));
+ aliased.attr("size_alias") = py::int_(sizeof(PyAliasedHasOpNewDelSize));
+
+ // This test is actually part of test_local_bindings (test_duplicate_local), but we need a
+ // definition in a different compilation unit within the same module:
+ bind_local<LocalExternal, 17>(m, "LocalExternal", py::module_local());
+
+ // test_bind_protected_functions
+ class ProtectedA {
+ protected:
+ int foo() const { return value; }
+
+ private:
+ int value = 42;
+ };
+
+ class PublicistA : public ProtectedA {
+ public:
+ using ProtectedA::foo;
+ };
+
+ py::class_<ProtectedA>(m, "ProtectedA")
+ .def(py::init<>())
+#if !defined(_MSC_VER) || _MSC_VER >= 1910
+ .def("foo", &PublicistA::foo);
+#else
+ .def("foo", static_cast<int (ProtectedA::*)() const>(&PublicistA::foo));
+#endif
+
+ class ProtectedB {
+ public:
+ virtual ~ProtectedB() = default;
+
+ protected:
+ virtual int foo() const { return value; }
+
+ private:
+ int value = 42;
+ };
+
+ class TrampolineB : public ProtectedB {
+ public:
+ int foo() const override { PYBIND11_OVERLOAD(int, ProtectedB, foo, ); }
+ };
+
+ class PublicistB : public ProtectedB {
+ public:
+ using ProtectedB::foo;
+ };
+
+ py::class_<ProtectedB, TrampolineB>(m, "ProtectedB")
+ .def(py::init<>())
+#if !defined(_MSC_VER) || _MSC_VER >= 1910
+ .def("foo", &PublicistB::foo);
+#else
+ .def("foo", static_cast<int (ProtectedB::*)() const>(&PublicistB::foo));
+#endif
+
+ // test_brace_initialization
+ struct BraceInitialization {
+ int field1;
+ std::string field2;
+ };
+
+ py::class_<BraceInitialization>(m, "BraceInitialization")
+ .def(py::init<int, const std::string &>())
+ .def_readwrite("field1", &BraceInitialization::field1)
+ .def_readwrite("field2", &BraceInitialization::field2);
+
+ // test_reentrant_implicit_conversion_failure
+ // #1035: issue with runaway reentrant implicit conversion
+ struct BogusImplicitConversion {
+ BogusImplicitConversion(const BogusImplicitConversion &) { }
+ };
+
+ py::class_<BogusImplicitConversion>(m, "BogusImplicitConversion")
+ .def(py::init<const BogusImplicitConversion &>());
+
+ py::implicitly_convertible<int, BogusImplicitConversion>();
+}
+
+template <int N> class BreaksBase { public: virtual ~BreaksBase() = default; };
+template <int N> class BreaksTramp : public BreaksBase<N> {};
+// These should all compile just fine:
+typedef py::class_<BreaksBase<1>, std::unique_ptr<BreaksBase<1>>, BreaksTramp<1>> DoesntBreak1;
+typedef py::class_<BreaksBase<2>, BreaksTramp<2>, std::unique_ptr<BreaksBase<2>>> DoesntBreak2;
+typedef py::class_<BreaksBase<3>, std::unique_ptr<BreaksBase<3>>> DoesntBreak3;
+typedef py::class_<BreaksBase<4>, BreaksTramp<4>> DoesntBreak4;
+typedef py::class_<BreaksBase<5>> DoesntBreak5;
+typedef py::class_<BreaksBase<6>, std::shared_ptr<BreaksBase<6>>, BreaksTramp<6>> DoesntBreak6;
+typedef py::class_<BreaksBase<7>, BreaksTramp<7>, std::shared_ptr<BreaksBase<7>>> DoesntBreak7;
+typedef py::class_<BreaksBase<8>, std::shared_ptr<BreaksBase<8>>> DoesntBreak8;
+#define CHECK_BASE(N) static_assert(std::is_same<typename DoesntBreak##N::type, BreaksBase<N>>::value, \
+ "DoesntBreak" #N " has wrong type!")
+CHECK_BASE(1); CHECK_BASE(2); CHECK_BASE(3); CHECK_BASE(4); CHECK_BASE(5); CHECK_BASE(6); CHECK_BASE(7); CHECK_BASE(8);
+#define CHECK_ALIAS(N) static_assert(DoesntBreak##N::has_alias && std::is_same<typename DoesntBreak##N::type_alias, BreaksTramp<N>>::value, \
+ "DoesntBreak" #N " has wrong type_alias!")
+#define CHECK_NOALIAS(N) static_assert(!DoesntBreak##N::has_alias && std::is_void<typename DoesntBreak##N::type_alias>::value, \
+ "DoesntBreak" #N " has type alias, but shouldn't!")
+CHECK_ALIAS(1); CHECK_ALIAS(2); CHECK_NOALIAS(3); CHECK_ALIAS(4); CHECK_NOALIAS(5); CHECK_ALIAS(6); CHECK_ALIAS(7); CHECK_NOALIAS(8);
+#define CHECK_HOLDER(N, TYPE) static_assert(std::is_same<typename DoesntBreak##N::holder_type, std::TYPE##_ptr<BreaksBase<N>>>::value, \
+ "DoesntBreak" #N " has wrong holder_type!")
+CHECK_HOLDER(1, unique); CHECK_HOLDER(2, unique); CHECK_HOLDER(3, unique); CHECK_HOLDER(4, unique); CHECK_HOLDER(5, unique);
+CHECK_HOLDER(6, shared); CHECK_HOLDER(7, shared); CHECK_HOLDER(8, shared);
+
+// There's no nice way to test that these fail because they fail to compile; leave them here,
+// though, so that they can be manually tested by uncommenting them (and seeing that compilation
+// failures occurs).
+
+// We have to actually look into the type: the typedef alone isn't enough to instantiate the type:
+#define CHECK_BROKEN(N) static_assert(std::is_same<typename Breaks##N::type, BreaksBase<-N>>::value, \
+ "Breaks1 has wrong type!");
+
+//// Two holder classes:
+//typedef py::class_<BreaksBase<-1>, std::unique_ptr<BreaksBase<-1>>, std::unique_ptr<BreaksBase<-1>>> Breaks1;
+//CHECK_BROKEN(1);
+//// Two aliases:
+//typedef py::class_<BreaksBase<-2>, BreaksTramp<-2>, BreaksTramp<-2>> Breaks2;
+//CHECK_BROKEN(2);
+//// Holder + 2 aliases
+//typedef py::class_<BreaksBase<-3>, std::unique_ptr<BreaksBase<-3>>, BreaksTramp<-3>, BreaksTramp<-3>> Breaks3;
+//CHECK_BROKEN(3);
+//// Alias + 2 holders
+//typedef py::class_<BreaksBase<-4>, std::unique_ptr<BreaksBase<-4>>, BreaksTramp<-4>, std::shared_ptr<BreaksBase<-4>>> Breaks4;
+//CHECK_BROKEN(4);
+//// Invalid option (not a subclass or holder)
+//typedef py::class_<BreaksBase<-5>, BreaksTramp<-4>> Breaks5;
+//CHECK_BROKEN(5);
+//// Invalid option: multiple inheritance not supported:
+//template <> struct BreaksBase<-8> : BreaksBase<-6>, BreaksBase<-7> {};
+//typedef py::class_<BreaksBase<-8>, BreaksBase<-6>, BreaksBase<-7>> Breaks8;
+//CHECK_BROKEN(8);
diff --git a/ext/pybind11/tests/test_class.py b/ext/pybind11/tests/test_class.py
new file mode 100644
index 000000000..412d6798e
--- /dev/null
+++ b/ext/pybind11/tests/test_class.py
@@ -0,0 +1,235 @@
+import pytest
+
+from pybind11_tests import class_ as m
+from pybind11_tests import UserType, ConstructorStats
+
+
+def test_repr():
+ # In Python 3.3+, repr() accesses __qualname__
+ assert "pybind11_type" in repr(type(UserType))
+ assert "UserType" in repr(UserType)
+
+
+def test_instance(msg):
+ with pytest.raises(TypeError) as excinfo:
+ m.NoConstructor()
+ assert msg(excinfo.value) == "m.class_.NoConstructor: No constructor defined!"
+
+ instance = m.NoConstructor.new_instance()
+
+ cstats = ConstructorStats.get(m.NoConstructor)
+ assert cstats.alive() == 1
+ del instance
+ assert cstats.alive() == 0
+
+
+def test_docstrings(doc):
+ assert doc(UserType) == "A `py::class_` type for testing"
+ assert UserType.__name__ == "UserType"
+ assert UserType.__module__ == "pybind11_tests"
+ assert UserType.get_value.__name__ == "get_value"
+ assert UserType.get_value.__module__ == "pybind11_tests"
+
+ assert doc(UserType.get_value) == """
+ get_value(self: m.UserType) -> int
+
+ Get value using a method
+ """
+ assert doc(UserType.value) == "Get/set value using a property"
+
+ assert doc(m.NoConstructor.new_instance) == """
+ new_instance() -> m.class_.NoConstructor
+
+ Return an instance
+ """
+
+
+def test_inheritance(msg):
+ roger = m.Rabbit('Rabbit')
+ assert roger.name() + " is a " + roger.species() == "Rabbit is a parrot"
+ assert m.pet_name_species(roger) == "Rabbit is a parrot"
+
+ polly = m.Pet('Polly', 'parrot')
+ assert polly.name() + " is a " + polly.species() == "Polly is a parrot"
+ assert m.pet_name_species(polly) == "Polly is a parrot"
+
+ molly = m.Dog('Molly')
+ assert molly.name() + " is a " + molly.species() == "Molly is a dog"
+ assert m.pet_name_species(molly) == "Molly is a dog"
+
+ fred = m.Hamster('Fred')
+ assert fred.name() + " is a " + fred.species() == "Fred is a rodent"
+
+ assert m.dog_bark(molly) == "Woof!"
+
+ with pytest.raises(TypeError) as excinfo:
+ m.dog_bark(polly)
+ assert msg(excinfo.value) == """
+ dog_bark(): incompatible function arguments. The following argument types are supported:
+ 1. (arg0: m.class_.Dog) -> str
+
+ Invoked with: <m.class_.Pet object at 0>
+ """
+
+ with pytest.raises(TypeError) as excinfo:
+ m.Chimera("lion", "goat")
+ assert "No constructor defined!" in str(excinfo.value)
+
+
+def test_automatic_upcasting():
+ assert type(m.return_class_1()).__name__ == "DerivedClass1"
+ assert type(m.return_class_2()).__name__ == "DerivedClass2"
+ assert type(m.return_none()).__name__ == "NoneType"
+ # Repeat these a few times in a random order to ensure no invalid caching is applied
+ assert type(m.return_class_n(1)).__name__ == "DerivedClass1"
+ assert type(m.return_class_n(2)).__name__ == "DerivedClass2"
+ assert type(m.return_class_n(0)).__name__ == "BaseClass"
+ assert type(m.return_class_n(2)).__name__ == "DerivedClass2"
+ assert type(m.return_class_n(2)).__name__ == "DerivedClass2"
+ assert type(m.return_class_n(0)).__name__ == "BaseClass"
+ assert type(m.return_class_n(1)).__name__ == "DerivedClass1"
+
+
+def test_isinstance():
+ objects = [tuple(), dict(), m.Pet("Polly", "parrot")] + [m.Dog("Molly")] * 4
+ expected = (True, True, True, True, True, False, False)
+ assert m.check_instances(objects) == expected
+
+
+def test_mismatched_holder():
+ import re
+
+ with pytest.raises(RuntimeError) as excinfo:
+ m.mismatched_holder_1()
+ assert re.match('generic_type: type ".*MismatchDerived1" does not have a non-default '
+ 'holder type while its base ".*MismatchBase1" does', str(excinfo.value))
+
+ with pytest.raises(RuntimeError) as excinfo:
+ m.mismatched_holder_2()
+ assert re.match('generic_type: type ".*MismatchDerived2" has a non-default holder type '
+ 'while its base ".*MismatchBase2" does not', str(excinfo.value))
+
+
+def test_override_static():
+ """#511: problem with inheritance + overwritten def_static"""
+ b = m.MyBase.make()
+ d1 = m.MyDerived.make2()
+ d2 = m.MyDerived.make()
+
+ assert isinstance(b, m.MyBase)
+ assert isinstance(d1, m.MyDerived)
+ assert isinstance(d2, m.MyDerived)
+
+
+def test_implicit_conversion_life_support():
+ """Ensure the lifetime of temporary objects created for implicit conversions"""
+ assert m.implicitly_convert_argument(UserType(5)) == 5
+ assert m.implicitly_convert_variable(UserType(5)) == 5
+
+ assert "outside a bound function" in m.implicitly_convert_variable_fail(UserType(5))
+
+
+def test_operator_new_delete(capture):
+ """Tests that class-specific operator new/delete functions are invoked"""
+
+ class SubAliased(m.AliasedHasOpNewDelSize):
+ pass
+
+ with capture:
+ a = m.HasOpNewDel()
+ b = m.HasOpNewDelSize()
+ d = m.HasOpNewDelBoth()
+ assert capture == """
+ A new 8
+ B new 4
+ D new 32
+ """
+ sz_alias = str(m.AliasedHasOpNewDelSize.size_alias)
+ sz_noalias = str(m.AliasedHasOpNewDelSize.size_noalias)
+ with capture:
+ c = m.AliasedHasOpNewDelSize()
+ c2 = SubAliased()
+ assert capture == (
+ "C new " + sz_noalias + "\n" +
+ "C new " + sz_alias + "\n"
+ )
+
+ with capture:
+ del a
+ pytest.gc_collect()
+ del b
+ pytest.gc_collect()
+ del d
+ pytest.gc_collect()
+ assert capture == """
+ A delete
+ B delete 4
+ D delete
+ """
+
+ with capture:
+ del c
+ pytest.gc_collect()
+ del c2
+ pytest.gc_collect()
+ assert capture == (
+ "C delete " + sz_noalias + "\n" +
+ "C delete " + sz_alias + "\n"
+ )
+
+
+def test_bind_protected_functions():
+ """Expose protected member functions to Python using a helper class"""
+ a = m.ProtectedA()
+ assert a.foo() == 42
+
+ b = m.ProtectedB()
+ assert b.foo() == 42
+
+ class C(m.ProtectedB):
+ def __init__(self):
+ m.ProtectedB.__init__(self)
+
+ def foo(self):
+ return 0
+
+ c = C()
+ assert c.foo() == 0
+
+
+def test_brace_initialization():
+ """ Tests that simple POD classes can be constructed using C++11 brace initialization """
+ a = m.BraceInitialization(123, "test")
+ assert a.field1 == 123
+ assert a.field2 == "test"
+
+
+@pytest.unsupported_on_pypy
+def test_class_refcount():
+ """Instances must correctly increase/decrease the reference count of their types (#1029)"""
+ from sys import getrefcount
+
+ class PyDog(m.Dog):
+ pass
+
+ for cls in m.Dog, PyDog:
+ refcount_1 = getrefcount(cls)
+ molly = [cls("Molly") for _ in range(10)]
+ refcount_2 = getrefcount(cls)
+
+ del molly
+ pytest.gc_collect()
+ refcount_3 = getrefcount(cls)
+
+ assert refcount_1 == refcount_3
+ assert refcount_2 > refcount_1
+
+
+def test_reentrant_implicit_conversion_failure(msg):
+ # ensure that there is no runaway reentrant implicit conversion (#1035)
+ with pytest.raises(TypeError) as excinfo:
+ m.BogusImplicitConversion(0)
+ assert msg(excinfo.value) == '''__init__(): incompatible constructor arguments. The following argument types are supported:
+ 1. m.class_.BogusImplicitConversion(arg0: m.class_.BogusImplicitConversion)
+
+Invoked with: 0'''
diff --git a/ext/pybind11/tests/test_class_args.cpp b/ext/pybind11/tests/test_class_args.cpp
deleted file mode 100644
index e18b39db2..000000000
--- a/ext/pybind11/tests/test_class_args.cpp
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- tests/test_class_args.cpp -- tests that various way of defining a class work
-
- Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
-
- All rights reserved. Use of this source code is governed by a
- BSD-style license that can be found in the LICENSE file.
-*/
-
-#include "pybind11_tests.h"
-
-
-template <int N> class BreaksBase {};
-template <int N> class BreaksTramp : public BreaksBase<N> {};
-// These should all compile just fine:
-typedef py::class_<BreaksBase<1>, std::unique_ptr<BreaksBase<1>>, BreaksTramp<1>> DoesntBreak1;
-typedef py::class_<BreaksBase<2>, BreaksTramp<2>, std::unique_ptr<BreaksBase<2>>> DoesntBreak2;
-typedef py::class_<BreaksBase<3>, std::unique_ptr<BreaksBase<3>>> DoesntBreak3;
-typedef py::class_<BreaksBase<4>, BreaksTramp<4>> DoesntBreak4;
-typedef py::class_<BreaksBase<5>> DoesntBreak5;
-typedef py::class_<BreaksBase<6>, std::shared_ptr<BreaksBase<6>>, BreaksTramp<6>> DoesntBreak6;
-typedef py::class_<BreaksBase<7>, BreaksTramp<7>, std::shared_ptr<BreaksBase<7>>> DoesntBreak7;
-typedef py::class_<BreaksBase<8>, std::shared_ptr<BreaksBase<8>>> DoesntBreak8;
-#define CHECK_BASE(N) static_assert(std::is_same<typename DoesntBreak##N::type, BreaksBase<N>>::value, \
- "DoesntBreak" #N " has wrong type!")
-CHECK_BASE(1); CHECK_BASE(2); CHECK_BASE(3); CHECK_BASE(4); CHECK_BASE(5); CHECK_BASE(6); CHECK_BASE(7); CHECK_BASE(8);
-#define CHECK_ALIAS(N) static_assert(DoesntBreak##N::has_alias && std::is_same<typename DoesntBreak##N::type_alias, BreaksTramp<N>>::value, \
- "DoesntBreak" #N " has wrong type_alias!")
-#define CHECK_NOALIAS(N) static_assert(!DoesntBreak##N::has_alias && std::is_void<typename DoesntBreak##N::type_alias>::value, \
- "DoesntBreak" #N " has type alias, but shouldn't!")
-CHECK_ALIAS(1); CHECK_ALIAS(2); CHECK_NOALIAS(3); CHECK_ALIAS(4); CHECK_NOALIAS(5); CHECK_ALIAS(6); CHECK_ALIAS(7); CHECK_NOALIAS(8);
-#define CHECK_HOLDER(N, TYPE) static_assert(std::is_same<typename DoesntBreak##N::holder_type, std::TYPE##_ptr<BreaksBase<N>>>::value, \
- "DoesntBreak" #N " has wrong holder_type!")
-CHECK_HOLDER(1, unique); CHECK_HOLDER(2, unique); CHECK_HOLDER(3, unique); CHECK_HOLDER(4, unique); CHECK_HOLDER(5, unique);
-CHECK_HOLDER(6, shared); CHECK_HOLDER(7, shared); CHECK_HOLDER(8, shared);
-
-// There's no nice way to test that these fail because they fail to compile; leave them here,
-// though, so that they can be manually tested by uncommenting them (and seeing that compilation
-// failures occurs).
-
-// We have to actually look into the type: the typedef alone isn't enough to instantiate the type:
-#define CHECK_BROKEN(N) static_assert(std::is_same<typename Breaks##N::type, BreaksBase<-N>>::value, \
- "Breaks1 has wrong type!");
-
-//// Two holder classes:
-//typedef py::class_<BreaksBase<-1>, std::unique_ptr<BreaksBase<-1>>, std::unique_ptr<BreaksBase<-1>>> Breaks1;
-//CHECK_BROKEN(1);
-//// Two aliases:
-//typedef py::class_<BreaksBase<-2>, BreaksTramp<-2>, BreaksTramp<-2>> Breaks2;
-//CHECK_BROKEN(2);
-//// Holder + 2 aliases
-//typedef py::class_<BreaksBase<-3>, std::unique_ptr<BreaksBase<-3>>, BreaksTramp<-3>, BreaksTramp<-3>> Breaks3;
-//CHECK_BROKEN(3);
-//// Alias + 2 holders
-//typedef py::class_<BreaksBase<-4>, std::unique_ptr<BreaksBase<-4>>, BreaksTramp<-4>, std::shared_ptr<BreaksBase<-4>>> Breaks4;
-//CHECK_BROKEN(4);
-//// Invalid option (not a subclass or holder)
-//typedef py::class_<BreaksBase<-5>, BreaksTramp<-4>> Breaks5;
-//CHECK_BROKEN(5);
-//// Invalid option: multiple inheritance not supported:
-//template <> struct BreaksBase<-8> : BreaksBase<-6>, BreaksBase<-7> {};
-//typedef py::class_<BreaksBase<-8>, BreaksBase<-6>, BreaksBase<-7>> Breaks8;
-//CHECK_BROKEN(8);
-
-test_initializer class_args([](py::module &m) {
- // Just test that this compiled okay
- m.def("class_args_noop", []() {});
-});
diff --git a/ext/pybind11/tests/test_class_args.py b/ext/pybind11/tests/test_class_args.py
deleted file mode 100644
index 40cbcec9f..000000000
--- a/ext/pybind11/tests/test_class_args.py
+++ /dev/null
@@ -1,8 +0,0 @@
-
-
-def test_class_args():
- """There's basically nothing to test here; just make sure the code compiled
- and declared its definition
- """
- from pybind11_tests import class_args_noop
- class_args_noop()
diff --git a/ext/pybind11/tests/test_cmake_build/CMakeLists.txt b/ext/pybind11/tests/test_cmake_build/CMakeLists.txt
new file mode 100644
index 000000000..c9b5fcb2e
--- /dev/null
+++ b/ext/pybind11/tests/test_cmake_build/CMakeLists.txt
@@ -0,0 +1,58 @@
+add_custom_target(test_cmake_build)
+
+if(CMAKE_VERSION VERSION_LESS 3.1)
+ # 3.0 needed for interface library for subdirectory_target/installed_target
+ # 3.1 needed for cmake -E env for testing
+ return()
+endif()
+
+include(CMakeParseArguments)
+function(pybind11_add_build_test name)
+ cmake_parse_arguments(ARG "INSTALL" "" "" ${ARGN})
+
+ set(build_options "-DCMAKE_PREFIX_PATH=${PROJECT_BINARY_DIR}/mock_install"
+ "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
+ "-DPYTHON_EXECUTABLE:FILEPATH=${PYTHON_EXECUTABLE}"
+ "-DPYBIND11_CPP_STANDARD=${PYBIND11_CPP_STANDARD}")
+ if(NOT ARG_INSTALL)
+ list(APPEND build_options "-DPYBIND11_PROJECT_DIR=${PROJECT_SOURCE_DIR}")
+ endif()
+
+ add_custom_target(test_${name} ${CMAKE_CTEST_COMMAND}
+ --quiet --output-log ${name}.log
+ --build-and-test "${CMAKE_CURRENT_SOURCE_DIR}/${name}"
+ "${CMAKE_CURRENT_BINARY_DIR}/${name}"
+ --build-config Release
+ --build-noclean
+ --build-generator ${CMAKE_GENERATOR}
+ $<$<BOOL:${CMAKE_GENERATOR_PLATFORM}>:--build-generator-platform> ${CMAKE_GENERATOR_PLATFORM}
+ --build-makeprogram ${CMAKE_MAKE_PROGRAM}
+ --build-target check
+ --build-options ${build_options}
+ )
+ if(ARG_INSTALL)
+ add_dependencies(test_${name} mock_install)
+ endif()
+ add_dependencies(test_cmake_build test_${name})
+endfunction()
+
+pybind11_add_build_test(subdirectory_function)
+pybind11_add_build_test(subdirectory_target)
+if(NOT ${PYTHON_MODULE_EXTENSION} MATCHES "pypy")
+ pybind11_add_build_test(subdirectory_embed)
+endif()
+
+if(PYBIND11_INSTALL)
+ add_custom_target(mock_install ${CMAKE_COMMAND}
+ "-DCMAKE_INSTALL_PREFIX=${PROJECT_BINARY_DIR}/mock_install"
+ -P "${PROJECT_BINARY_DIR}/cmake_install.cmake"
+ )
+
+ pybind11_add_build_test(installed_function INSTALL)
+ pybind11_add_build_test(installed_target INSTALL)
+ if(NOT ${PYTHON_MODULE_EXTENSION} MATCHES "pypy")
+ pybind11_add_build_test(installed_embed INSTALL)
+ endif()
+endif()
+
+add_dependencies(check test_cmake_build)
diff --git a/ext/pybind11/tests/test_cmake_build/embed.cpp b/ext/pybind11/tests/test_cmake_build/embed.cpp
new file mode 100644
index 000000000..b9581d2fd
--- /dev/null
+++ b/ext/pybind11/tests/test_cmake_build/embed.cpp
@@ -0,0 +1,21 @@
+#include <pybind11/embed.h>
+namespace py = pybind11;
+
+PYBIND11_EMBEDDED_MODULE(test_cmake_build, m) {
+ m.def("add", [](int i, int j) { return i + j; });
+}
+
+int main(int argc, char *argv[]) {
+ if (argc != 2)
+ throw std::runtime_error("Expected test.py file as the first argument");
+ auto test_py_file = argv[1];
+
+ py::scoped_interpreter guard{};
+
+ auto m = py::module::import("test_cmake_build");
+ if (m.attr("add")(1, 2).cast<int>() != 3)
+ throw std::runtime_error("embed.cpp failed");
+
+ py::module::import("sys").attr("argv") = py::make_tuple("test.py", "embed.cpp");
+ py::eval_file(test_py_file, py::globals());
+}
diff --git a/ext/pybind11/tests/test_cmake_build/installed_embed/CMakeLists.txt b/ext/pybind11/tests/test_cmake_build/installed_embed/CMakeLists.txt
new file mode 100644
index 000000000..f7fc09c21
--- /dev/null
+++ b/ext/pybind11/tests/test_cmake_build/installed_embed/CMakeLists.txt
@@ -0,0 +1,15 @@
+cmake_minimum_required(VERSION 3.0)
+project(test_installed_embed CXX)
+
+set(CMAKE_MODULE_PATH "")
+find_package(pybind11 CONFIG REQUIRED)
+message(STATUS "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIRS}")
+
+add_executable(test_cmake_build ../embed.cpp)
+target_link_libraries(test_cmake_build PRIVATE pybind11::embed)
+
+# Do not treat includes from IMPORTED target as SYSTEM (Python headers in pybind11::embed).
+# This may be needed to resolve header conflicts, e.g. between Python release and debug headers.
+set_target_properties(test_cmake_build PROPERTIES NO_SYSTEM_FROM_IMPORTED ON)
+
+add_custom_target(check $<TARGET_FILE:test_cmake_build> ${PROJECT_SOURCE_DIR}/../test.py)
diff --git a/ext/pybind11/tests/test_cmake_build/main.cpp b/ext/pybind11/tests/test_cmake_build/main.cpp
index e0f5b69c9..e30f2c4b9 100644
--- a/ext/pybind11/tests/test_cmake_build/main.cpp
+++ b/ext/pybind11/tests/test_cmake_build/main.cpp
@@ -1,10 +1,6 @@
#include <pybind11/pybind11.h>
namespace py = pybind11;
-PYBIND11_PLUGIN(test_cmake_build) {
- py::module m("test_cmake_build");
-
+PYBIND11_MODULE(test_cmake_build, m) {
m.def("add", [](int i, int j) { return i + j; });
-
- return m.ptr();
}
diff --git a/ext/pybind11/tests/test_cmake_build/subdirectory_embed/CMakeLists.txt b/ext/pybind11/tests/test_cmake_build/subdirectory_embed/CMakeLists.txt
new file mode 100644
index 000000000..88ba60dd5
--- /dev/null
+++ b/ext/pybind11/tests/test_cmake_build/subdirectory_embed/CMakeLists.txt
@@ -0,0 +1,25 @@
+cmake_minimum_required(VERSION 3.0)
+project(test_subdirectory_embed CXX)
+
+set(PYBIND11_INSTALL ON CACHE BOOL "")
+set(PYBIND11_EXPORT_NAME test_export)
+
+add_subdirectory(${PYBIND11_PROJECT_DIR} pybind11)
+
+# Test basic target functionality
+add_executable(test_cmake_build ../embed.cpp)
+target_link_libraries(test_cmake_build PRIVATE pybind11::embed)
+
+add_custom_target(check $<TARGET_FILE:test_cmake_build> ${PROJECT_SOURCE_DIR}/../test.py)
+
+# Test custom export group -- PYBIND11_EXPORT_NAME
+add_library(test_embed_lib ../embed.cpp)
+target_link_libraries(test_embed_lib PRIVATE pybind11::embed)
+
+install(TARGETS test_embed_lib
+ EXPORT test_export
+ ARCHIVE DESTINATION bin
+ LIBRARY DESTINATION lib
+ RUNTIME DESTINATION lib)
+install(EXPORT test_export
+ DESTINATION lib/cmake/test_export/test_export-Targets.cmake)
diff --git a/ext/pybind11/tests/test_constants_and_functions.cpp b/ext/pybind11/tests/test_constants_and_functions.cpp
index 653bdf6b6..8c9ef7f67 100644
--- a/ext/pybind11/tests/test_constants_and_functions.cpp
+++ b/ext/pybind11/tests/test_constants_and_functions.cpp
@@ -23,6 +23,8 @@ std::string test_function3(int i) {
return "test_function(" + std::to_string(i) + ")";
}
+py::str test_function4() { return "test_function()"; }
+py::str test_function4(char *) { return "test_function(char *)"; }
py::str test_function4(int, float) { return "test_function(int, float)"; }
py::str test_function4(float, int) { return "test_function(float, int)"; }
@@ -61,17 +63,23 @@ struct C {
}
-test_initializer constants_and_functions([](py::module &m) {
+TEST_SUBMODULE(constants_and_functions, m) {
+ // test_constants
m.attr("some_constant") = py::int_(14);
+ // test_function_overloading
m.def("test_function", &test_function1);
m.def("test_function", &test_function2);
m.def("test_function", &test_function3);
#if defined(PYBIND11_OVERLOAD_CAST)
+ m.def("test_function", py::overload_cast<>(&test_function4));
+ m.def("test_function", py::overload_cast<char *>(&test_function4));
m.def("test_function", py::overload_cast<int, float>(&test_function4));
m.def("test_function", py::overload_cast<float, int>(&test_function4));
#else
+ m.def("test_function", static_cast<py::str (*)()>(&test_function4));
+ m.def("test_function", static_cast<py::str (*)(char *)>(&test_function4));
m.def("test_function", static_cast<py::str (*)(int, float)>(&test_function4));
m.def("test_function", static_cast<py::str (*)(float, int)>(&test_function4));
#endif
@@ -81,12 +89,13 @@ test_initializer constants_and_functions([](py::module &m) {
.value("ESecondEntry", ESecondEntry)
.export_values();
+ // test_bytes
m.def("return_bytes", &return_bytes);
m.def("print_bytes", &print_bytes);
+ // test_exception_specifiers
using namespace test_exc_sp;
- py::module m2 = m.def_submodule("exc_sp");
- py::class_<C>(m2, "C")
+ py::class_<C>(m, "C")
.def(py::init<>())
.def("m1", &C::m1)
.def("m2", &C::m2)
@@ -97,8 +106,8 @@ test_initializer constants_and_functions([](py::module &m) {
.def("m7", &C::m7)
.def("m8", &C::m8)
;
- m2.def("f1", f1);
- m2.def("f2", f2);
- m2.def("f3", f3);
- m2.def("f4", f4);
-});
+ m.def("f1", f1);
+ m.def("f2", f2);
+ m.def("f3", f3);
+ m.def("f4", f4);
+}
diff --git a/ext/pybind11/tests/test_constants_and_functions.py b/ext/pybind11/tests/test_constants_and_functions.py
index 2a570d2e5..472682d61 100644
--- a/ext/pybind11/tests/test_constants_and_functions.py
+++ b/ext/pybind11/tests/test_constants_and_functions.py
@@ -1,33 +1,29 @@
+from pybind11_tests import constants_and_functions as m
def test_constants():
- from pybind11_tests import some_constant
-
- assert some_constant == 14
+ assert m.some_constant == 14
def test_function_overloading():
- from pybind11_tests import MyEnum, test_function
-
- assert test_function() == "test_function()"
- assert test_function(7) == "test_function(7)"
- assert test_function(MyEnum.EFirstEntry) == "test_function(enum=1)"
- assert test_function(MyEnum.ESecondEntry) == "test_function(enum=2)"
+ assert m.test_function() == "test_function()"
+ assert m.test_function(7) == "test_function(7)"
+ assert m.test_function(m.MyEnum.EFirstEntry) == "test_function(enum=1)"
+ assert m.test_function(m.MyEnum.ESecondEntry) == "test_function(enum=2)"
- assert test_function(1, 1.0) == "test_function(int, float)"
- assert test_function(2.0, 2) == "test_function(float, int)"
+ assert m.test_function() == "test_function()"
+ assert m.test_function("abcd") == "test_function(char *)"
+ assert m.test_function(1, 1.0) == "test_function(int, float)"
+ assert m.test_function(1, 1.0) == "test_function(int, float)"
+ assert m.test_function(2.0, 2) == "test_function(float, int)"
def test_bytes():
- from pybind11_tests import return_bytes, print_bytes
-
- assert print_bytes(return_bytes()) == "bytes[1 0 2 0]"
+ assert m.print_bytes(m.return_bytes()) == "bytes[1 0 2 0]"
def test_exception_specifiers():
- from pybind11_tests.exc_sp import C, f1, f2, f3, f4
-
- c = C()
+ c = m.C()
assert c.m1(2) == 1
assert c.m2(3) == 1
assert c.m3(5) == 2
@@ -37,7 +33,7 @@ def test_exception_specifiers():
assert c.m7(20) == 13
assert c.m8(29) == 21
- assert f1(33) == 34
- assert f2(53) == 55
- assert f3(86) == 89
- assert f4(140) == 144
+ assert m.f1(33) == 34
+ assert m.f2(53) == 55
+ assert m.f3(86) == 89
+ assert m.f4(140) == 144
diff --git a/ext/pybind11/tests/test_copy_move.cpp b/ext/pybind11/tests/test_copy_move.cpp
new file mode 100644
index 000000000..94113e3af
--- /dev/null
+++ b/ext/pybind11/tests/test_copy_move.cpp
@@ -0,0 +1,213 @@
+/*
+ tests/test_copy_move_policies.cpp -- 'copy' and 'move' return value policies
+ and related tests
+
+ Copyright (c) 2016 Ben North <ben@redfrontdoor.org>
+
+ All rights reserved. Use of this source code is governed by a
+ BSD-style license that can be found in the LICENSE file.
+*/
+
+#include "pybind11_tests.h"
+#include "constructor_stats.h"
+#include <pybind11/stl.h>
+
+template <typename derived>
+struct empty {
+ static const derived& get_one() { return instance_; }
+ static derived instance_;
+};
+
+struct lacking_copy_ctor : public empty<lacking_copy_ctor> {
+ lacking_copy_ctor() {}
+ lacking_copy_ctor(const lacking_copy_ctor& other) = delete;
+};
+
+template <> lacking_copy_ctor empty<lacking_copy_ctor>::instance_ = {};
+
+struct lacking_move_ctor : public empty<lacking_move_ctor> {
+ lacking_move_ctor() {}
+ lacking_move_ctor(const lacking_move_ctor& other) = delete;
+ lacking_move_ctor(lacking_move_ctor&& other) = delete;
+};
+
+template <> lacking_move_ctor empty<lacking_move_ctor>::instance_ = {};
+
+/* Custom type caster move/copy test classes */
+class MoveOnlyInt {
+public:
+ MoveOnlyInt() { print_default_created(this); }
+ MoveOnlyInt(int v) : value{std::move(v)} { print_created(this, value); }
+ MoveOnlyInt(MoveOnlyInt &&m) { print_move_created(this, m.value); std::swap(value, m.value); }
+ MoveOnlyInt &operator=(MoveOnlyInt &&m) { print_move_assigned(this, m.value); std::swap(value, m.value); return *this; }
+ MoveOnlyInt(const MoveOnlyInt &) = delete;
+ MoveOnlyInt &operator=(const MoveOnlyInt &) = delete;
+ ~MoveOnlyInt() { print_destroyed(this); }
+
+ int value;
+};
+class MoveOrCopyInt {
+public:
+ MoveOrCopyInt() { print_default_created(this); }
+ MoveOrCopyInt(int v) : value{std::move(v)} { print_created(this, value); }
+ MoveOrCopyInt(MoveOrCopyInt &&m) { print_move_created(this, m.value); std::swap(value, m.value); }
+ MoveOrCopyInt &operator=(MoveOrCopyInt &&m) { print_move_assigned(this, m.value); std::swap(value, m.value); return *this; }
+ MoveOrCopyInt(const MoveOrCopyInt &c) { print_copy_created(this, c.value); value = c.value; }
+ MoveOrCopyInt &operator=(const MoveOrCopyInt &c) { print_copy_assigned(this, c.value); value = c.value; return *this; }
+ ~MoveOrCopyInt() { print_destroyed(this); }
+
+ int value;
+};
+class CopyOnlyInt {
+public:
+ CopyOnlyInt() { print_default_created(this); }
+ CopyOnlyInt(int v) : value{std::move(v)} { print_created(this, value); }
+ CopyOnlyInt(const CopyOnlyInt &c) { print_copy_created(this, c.value); value = c.value; }
+ CopyOnlyInt &operator=(const CopyOnlyInt &c) { print_copy_assigned(this, c.value); value = c.value; return *this; }
+ ~CopyOnlyInt() { print_destroyed(this); }
+
+ int value;
+};
+NAMESPACE_BEGIN(pybind11)
+NAMESPACE_BEGIN(detail)
+template <> struct type_caster<MoveOnlyInt> {
+ PYBIND11_TYPE_CASTER(MoveOnlyInt, _("MoveOnlyInt"));
+ bool load(handle src, bool) { value = MoveOnlyInt(src.cast<int>()); return true; }
+ static handle cast(const MoveOnlyInt &m, return_value_policy r, handle p) { return pybind11::cast(m.value, r, p); }
+};
+
+template <> struct type_caster<MoveOrCopyInt> {
+ PYBIND11_TYPE_CASTER(MoveOrCopyInt, _("MoveOrCopyInt"));
+ bool load(handle src, bool) { value = MoveOrCopyInt(src.cast<int>()); return true; }
+ static handle cast(const MoveOrCopyInt &m, return_value_policy r, handle p) { return pybind11::cast(m.value, r, p); }
+};
+
+template <> struct type_caster<CopyOnlyInt> {
+protected:
+ CopyOnlyInt value;
+public:
+ static PYBIND11_DESCR name() { return _("CopyOnlyInt"); }
+ bool load(handle src, bool) { value = CopyOnlyInt(src.cast<int>()); return true; }
+ static handle cast(const CopyOnlyInt &m, return_value_policy r, handle p) { return pybind11::cast(m.value, r, p); }
+ static handle cast(const CopyOnlyInt *src, return_value_policy policy, handle parent) {
+ if (!src) return none().release();
+ return cast(*src, policy, parent);
+ }
+ operator CopyOnlyInt*() { return &value; }
+ operator CopyOnlyInt&() { return value; }
+ template <typename T> using cast_op_type = pybind11::detail::cast_op_type<T>;
+};
+NAMESPACE_END(detail)
+NAMESPACE_END(pybind11)
+
+TEST_SUBMODULE(copy_move_policies, m) {
+ // test_lacking_copy_ctor
+ py::class_<lacking_copy_ctor>(m, "lacking_copy_ctor")
+ .def_static("get_one", &lacking_copy_ctor::get_one,
+ py::return_value_policy::copy);
+ // test_lacking_move_ctor
+ py::class_<lacking_move_ctor>(m, "lacking_move_ctor")
+ .def_static("get_one", &lacking_move_ctor::get_one,
+ py::return_value_policy::move);
+
+ // test_move_and_copy_casts
+ m.def("move_and_copy_casts", [](py::object o) {
+ int r = 0;
+ r += py::cast<MoveOrCopyInt>(o).value; /* moves */
+ r += py::cast<MoveOnlyInt>(o).value; /* moves */
+ r += py::cast<CopyOnlyInt>(o).value; /* copies */
+ MoveOrCopyInt m1(py::cast<MoveOrCopyInt>(o)); /* moves */
+ MoveOnlyInt m2(py::cast<MoveOnlyInt>(o)); /* moves */
+ CopyOnlyInt m3(py::cast<CopyOnlyInt>(o)); /* copies */
+ r += m1.value + m2.value + m3.value;
+
+ return r;
+ });
+
+ // test_move_and_copy_loads
+ m.def("move_only", [](MoveOnlyInt m) { return m.value; });
+ m.def("move_or_copy", [](MoveOrCopyInt m) { return m.value; });
+ m.def("copy_only", [](CopyOnlyInt m) { return m.value; });
+ m.def("move_pair", [](std::pair<MoveOnlyInt, MoveOrCopyInt> p) {
+ return p.first.value + p.second.value;
+ });
+ m.def("move_tuple", [](std::tuple<MoveOnlyInt, MoveOrCopyInt, MoveOnlyInt> t) {
+ return std::get<0>(t).value + std::get<1>(t).value + std::get<2>(t).value;
+ });
+ m.def("copy_tuple", [](std::tuple<CopyOnlyInt, CopyOnlyInt> t) {
+ return std::get<0>(t).value + std::get<1>(t).value;
+ });
+ m.def("move_copy_nested", [](std::pair<MoveOnlyInt, std::pair<std::tuple<MoveOrCopyInt, CopyOnlyInt, std::tuple<MoveOnlyInt>>, MoveOrCopyInt>> x) {
+ return x.first.value + std::get<0>(x.second.first).value + std::get<1>(x.second.first).value +
+ std::get<0>(std::get<2>(x.second.first)).value + x.second.second.value;
+ });
+ m.def("move_and_copy_cstats", []() {
+ ConstructorStats::gc();
+ // Reset counts to 0 so that previous tests don't affect later ones:
+ auto &mc = ConstructorStats::get<MoveOrCopyInt>();
+ mc.move_assignments = mc.move_constructions = mc.copy_assignments = mc.copy_constructions = 0;
+ auto &mo = ConstructorStats::get<MoveOnlyInt>();
+ mo.move_assignments = mo.move_constructions = mo.copy_assignments = mo.copy_constructions = 0;
+ auto &co = ConstructorStats::get<CopyOnlyInt>();
+ co.move_assignments = co.move_constructions = co.copy_assignments = co.copy_constructions = 0;
+ py::dict d;
+ d["MoveOrCopyInt"] = py::cast(mc, py::return_value_policy::reference);
+ d["MoveOnlyInt"] = py::cast(mo, py::return_value_policy::reference);
+ d["CopyOnlyInt"] = py::cast(co, py::return_value_policy::reference);
+ return d;
+ });
+#ifdef PYBIND11_HAS_OPTIONAL
+ // test_move_and_copy_load_optional
+ m.attr("has_optional") = true;
+ m.def("move_optional", [](std::optional<MoveOnlyInt> o) {
+ return o->value;
+ });
+ m.def("move_or_copy_optional", [](std::optional<MoveOrCopyInt> o) {
+ return o->value;
+ });
+ m.def("copy_optional", [](std::optional<CopyOnlyInt> o) {
+ return o->value;
+ });
+ m.def("move_optional_tuple", [](std::optional<std::tuple<MoveOrCopyInt, MoveOnlyInt, CopyOnlyInt>> x) {
+ return std::get<0>(*x).value + std::get<1>(*x).value + std::get<2>(*x).value;
+ });
+#else
+ m.attr("has_optional") = false;
+#endif
+
+ // #70 compilation issue if operator new is not public
+ struct PrivateOpNew {
+ int value = 1;
+ private:
+#if defined(_MSC_VER)
+# pragma warning(disable: 4822) // warning C4822: local class member function does not have a body
+#endif
+ void *operator new(size_t bytes);
+ };
+ py::class_<PrivateOpNew>(m, "PrivateOpNew").def_readonly("value", &PrivateOpNew::value);
+ m.def("private_op_new_value", []() { return PrivateOpNew(); });
+ m.def("private_op_new_reference", []() -> const PrivateOpNew & {
+ static PrivateOpNew x{};
+ return x;
+ }, py::return_value_policy::reference);
+
+ // test_move_fallback
+ // #389: rvp::move should fall-through to copy on non-movable objects
+ struct MoveIssue1 {
+ int v;
+ MoveIssue1(int v) : v{v} {}
+ MoveIssue1(const MoveIssue1 &c) = default;
+ MoveIssue1(MoveIssue1 &&) = delete;
+ };
+ py::class_<MoveIssue1>(m, "MoveIssue1").def(py::init<int>()).def_readwrite("value", &MoveIssue1::v);
+
+ struct MoveIssue2 {
+ int v;
+ MoveIssue2(int v) : v{v} {}
+ MoveIssue2(MoveIssue2 &&) = default;
+ };
+ py::class_<MoveIssue2>(m, "MoveIssue2").def(py::init<int>()).def_readwrite("value", &MoveIssue2::v);
+
+ m.def("get_moveissue1", [](int i) { return new MoveIssue1(i); }, py::return_value_policy::move);
+ m.def("get_moveissue2", [](int i) { return MoveIssue2(i); }, py::return_value_policy::move);
+}
diff --git a/ext/pybind11/tests/test_copy_move.py b/ext/pybind11/tests/test_copy_move.py
new file mode 100644
index 000000000..aff2d99f2
--- /dev/null
+++ b/ext/pybind11/tests/test_copy_move.py
@@ -0,0 +1,112 @@
+import pytest
+from pybind11_tests import copy_move_policies as m
+
+
+def test_lacking_copy_ctor():
+ with pytest.raises(RuntimeError) as excinfo:
+ m.lacking_copy_ctor.get_one()
+ assert "the object is non-copyable!" in str(excinfo.value)
+
+
+def test_lacking_move_ctor():
+ with pytest.raises(RuntimeError) as excinfo:
+ m.lacking_move_ctor.get_one()
+ assert "the object is neither movable nor copyable!" in str(excinfo.value)
+
+
+def test_move_and_copy_casts():
+ """Cast some values in C++ via custom type casters and count the number of moves/copies."""
+
+ cstats = m.move_and_copy_cstats()
+ c_m, c_mc, c_c = cstats["MoveOnlyInt"], cstats["MoveOrCopyInt"], cstats["CopyOnlyInt"]
+
+ # The type move constructions/assignments below each get incremented: the move assignment comes
+ # from the type_caster load; the move construction happens when extracting that via a cast or
+ # loading into an argument.
+ assert m.move_and_copy_casts(3) == 18
+ assert c_m.copy_assignments + c_m.copy_constructions == 0
+ assert c_m.move_assignments == 2
+ assert c_m.move_constructions >= 2
+ assert c_mc.alive() == 0
+ assert c_mc.copy_assignments + c_mc.copy_constructions == 0
+ assert c_mc.move_assignments == 2
+ assert c_mc.move_constructions >= 2
+ assert c_c.alive() == 0
+ assert c_c.copy_assignments == 2
+ assert c_c.copy_constructions >= 2
+ assert c_m.alive() + c_mc.alive() + c_c.alive() == 0
+
+
+def test_move_and_copy_loads():
+ """Call some functions that load arguments via custom type casters and count the number of
+ moves/copies."""
+
+ cstats = m.move_and_copy_cstats()
+ c_m, c_mc, c_c = cstats["MoveOnlyInt"], cstats["MoveOrCopyInt"], cstats["CopyOnlyInt"]
+
+ assert m.move_only(10) == 10 # 1 move, c_m
+ assert m.move_or_copy(11) == 11 # 1 move, c_mc
+ assert m.copy_only(12) == 12 # 1 copy, c_c
+ assert m.move_pair((13, 14)) == 27 # 1 c_m move, 1 c_mc move
+ assert m.move_tuple((15, 16, 17)) == 48 # 2 c_m moves, 1 c_mc move
+ assert m.copy_tuple((18, 19)) == 37 # 2 c_c copies
+ # Direct constructions: 2 c_m moves, 2 c_mc moves, 1 c_c copy
+ # Extra moves/copies when moving pairs/tuples: 3 c_m, 3 c_mc, 2 c_c
+ assert m.move_copy_nested((1, ((2, 3, (4,)), 5))) == 15
+
+ assert c_m.copy_assignments + c_m.copy_constructions == 0
+ assert c_m.move_assignments == 6
+ assert c_m.move_constructions == 9
+ assert c_mc.copy_assignments + c_mc.copy_constructions == 0
+ assert c_mc.move_assignments == 5
+ assert c_mc.move_constructions == 8
+ assert c_c.copy_assignments == 4
+ assert c_c.copy_constructions == 6
+ assert c_m.alive() + c_mc.alive() + c_c.alive() == 0
+
+
+@pytest.mark.skipif(not m.has_optional, reason='no <optional>')
+def test_move_and_copy_load_optional():
+ """Tests move/copy loads of std::optional arguments"""
+
+ cstats = m.move_and_copy_cstats()
+ c_m, c_mc, c_c = cstats["MoveOnlyInt"], cstats["MoveOrCopyInt"], cstats["CopyOnlyInt"]
+
+ # The extra move/copy constructions below come from the std::optional move (which has to move
+ # its arguments):
+ assert m.move_optional(10) == 10 # c_m: 1 move assign, 2 move construct
+ assert m.move_or_copy_optional(11) == 11 # c_mc: 1 move assign, 2 move construct
+ assert m.copy_optional(12) == 12 # c_c: 1 copy assign, 2 copy construct
+ # 1 move assign + move construct moves each of c_m, c_mc, 1 c_c copy
+ # +1 move/copy construct each from moving the tuple
+ # +1 move/copy construct each from moving the optional (which moves the tuple again)
+ assert m.move_optional_tuple((3, 4, 5)) == 12
+
+ assert c_m.copy_assignments + c_m.copy_constructions == 0
+ assert c_m.move_assignments == 2
+ assert c_m.move_constructions == 5
+ assert c_mc.copy_assignments + c_mc.copy_constructions == 0
+ assert c_mc.move_assignments == 2
+ assert c_mc.move_constructions == 5
+ assert c_c.copy_assignments == 2
+ assert c_c.copy_constructions == 5
+ assert c_m.alive() + c_mc.alive() + c_c.alive() == 0
+
+
+def test_private_op_new():
+ """An object with a private `operator new` cannot be returned by value"""
+
+ with pytest.raises(RuntimeError) as excinfo:
+ m.private_op_new_value()
+ assert "the object is neither movable nor copyable" in str(excinfo.value)
+
+ assert m.private_op_new_reference().value == 1
+
+
+def test_move_fallback():
+ """#389: rvp::move should fall-through to copy on non-movable objects"""
+
+ m2 = m.get_moveissue2(2)
+ assert m2.value == 2
+ m1 = m.get_moveissue1(1)
+ assert m1.value == 1
diff --git a/ext/pybind11/tests/test_copy_move_policies.cpp b/ext/pybind11/tests/test_copy_move_policies.cpp
deleted file mode 100644
index 6f7907c1f..000000000
--- a/ext/pybind11/tests/test_copy_move_policies.cpp
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- tests/test_copy_move_policies.cpp -- 'copy' and 'move'
- return value policies
-
- Copyright (c) 2016 Ben North <ben@redfrontdoor.org>
-
- All rights reserved. Use of this source code is governed by a
- BSD-style license that can be found in the LICENSE file.
-*/
-
-#include "pybind11_tests.h"
-
-template <typename derived>
-struct empty {
- static const derived& get_one() { return instance_; }
- static derived instance_;
-};
-
-struct lacking_copy_ctor : public empty<lacking_copy_ctor> {
- lacking_copy_ctor() {}
- lacking_copy_ctor(const lacking_copy_ctor& other) = delete;
-};
-
-template <> lacking_copy_ctor empty<lacking_copy_ctor>::instance_ = {};
-
-struct lacking_move_ctor : public empty<lacking_move_ctor> {
- lacking_move_ctor() {}
- lacking_move_ctor(const lacking_move_ctor& other) = delete;
- lacking_move_ctor(lacking_move_ctor&& other) = delete;
-};
-
-template <> lacking_move_ctor empty<lacking_move_ctor>::instance_ = {};
-
-test_initializer copy_move_policies([](py::module &m) {
- py::class_<lacking_copy_ctor>(m, "lacking_copy_ctor")
- .def_static("get_one", &lacking_copy_ctor::get_one,
- py::return_value_policy::copy);
- py::class_<lacking_move_ctor>(m, "lacking_move_ctor")
- .def_static("get_one", &lacking_move_ctor::get_one,
- py::return_value_policy::move);
-});
diff --git a/ext/pybind11/tests/test_copy_move_policies.py b/ext/pybind11/tests/test_copy_move_policies.py
deleted file mode 100644
index edcf38075..000000000
--- a/ext/pybind11/tests/test_copy_move_policies.py
+++ /dev/null
@@ -1,15 +0,0 @@
-import pytest
-
-
-def test_lacking_copy_ctor():
- from pybind11_tests import lacking_copy_ctor
- with pytest.raises(RuntimeError) as excinfo:
- lacking_copy_ctor.get_one()
- assert "the object is non-copyable!" in str(excinfo.value)
-
-
-def test_lacking_move_ctor():
- from pybind11_tests import lacking_move_ctor
- with pytest.raises(RuntimeError) as excinfo:
- lacking_move_ctor.get_one()
- assert "the object is neither movable nor copyable!" in str(excinfo.value)
diff --git a/ext/pybind11/tests/test_docstring_options.cpp b/ext/pybind11/tests/test_docstring_options.cpp
index 9a9297cc3..8c8f79fd5 100644
--- a/ext/pybind11/tests/test_docstring_options.cpp
+++ b/ext/pybind11/tests/test_docstring_options.cpp
@@ -9,14 +9,8 @@
#include "pybind11_tests.h"
-struct DocstringTestFoo {
- int value;
- void setValue(int v) { value = v; }
- int getValue() const { return value; }
-};
-
-test_initializer docstring_generation([](py::module &m) {
-
+TEST_SUBMODULE(docstring_options, m) {
+ // test_docstring_options
{
py::options options;
options.disable_function_signatures();
@@ -55,8 +49,13 @@ test_initializer docstring_generation([](py::module &m) {
py::options options;
options.disable_user_defined_docstrings();
+ struct DocstringTestFoo {
+ int value;
+ void setValue(int v) { value = v; }
+ int getValue() const { return value; }
+ };
py::class_<DocstringTestFoo>(m, "DocstringTestFoo", "This is a class docstring")
.def_property("value_prop", &DocstringTestFoo::getValue, &DocstringTestFoo::setValue, "This is a property docstring")
;
}
-});
+}
diff --git a/ext/pybind11/tests/test_docstring_options.py b/ext/pybind11/tests/test_docstring_options.py
index 5e40f6868..0dbca609e 100644
--- a/ext/pybind11/tests/test_docstring_options.py
+++ b/ext/pybind11/tests/test_docstring_options.py
@@ -1,42 +1,38 @@
+from pybind11_tests import docstring_options as m
def test_docstring_options():
- from pybind11_tests import (test_function1, test_function2, test_function3,
- test_function4, test_function5, test_function6,
- test_function7, DocstringTestFoo,
- test_overloaded1, test_overloaded2, test_overloaded3)
-
# options.disable_function_signatures()
- assert not test_function1.__doc__
+ assert not m.test_function1.__doc__
- assert test_function2.__doc__ == "A custom docstring"
+ assert m.test_function2.__doc__ == "A custom docstring"
# docstring specified on just the first overload definition:
- assert test_overloaded1.__doc__ == "Overload docstring"
+ assert m.test_overloaded1.__doc__ == "Overload docstring"
# docstring on both overloads:
- assert test_overloaded2.__doc__ == "overload docstring 1\noverload docstring 2"
+ assert m.test_overloaded2.__doc__ == "overload docstring 1\noverload docstring 2"
# docstring on only second overload:
- assert test_overloaded3.__doc__ == "Overload docstr"
+ assert m.test_overloaded3.__doc__ == "Overload docstr"
# options.enable_function_signatures()
- assert test_function3.__doc__ .startswith("test_function3(a: int, b: int) -> None")
+ assert m.test_function3.__doc__ .startswith("test_function3(a: int, b: int) -> None")
- assert test_function4.__doc__ .startswith("test_function4(a: int, b: int) -> None")
- assert test_function4.__doc__ .endswith("A custom docstring\n")
+ assert m.test_function4.__doc__ .startswith("test_function4(a: int, b: int) -> None")
+ assert m.test_function4.__doc__ .endswith("A custom docstring\n")
# options.disable_function_signatures()
# options.disable_user_defined_docstrings()
- assert not test_function5.__doc__
+ assert not m.test_function5.__doc__
# nested options.enable_user_defined_docstrings()
- assert test_function6.__doc__ == "A custom docstring"
+ assert m.test_function6.__doc__ == "A custom docstring"
# RAII destructor
- assert test_function7.__doc__ .startswith("test_function7(a: int, b: int) -> None")
- assert test_function7.__doc__ .endswith("A custom docstring\n")
+ assert m.test_function7.__doc__ .startswith("test_function7(a: int, b: int) -> None")
+ assert m.test_function7.__doc__ .endswith("A custom docstring\n")
# Suppression of user-defined docstrings for non-function objects
- assert not DocstringTestFoo.__doc__
- assert not DocstringTestFoo.value_prop.__doc__
+ assert not m.DocstringTestFoo.__doc__
+ assert not m.DocstringTestFoo.value_prop.__doc__
diff --git a/ext/pybind11/tests/test_eigen.cpp b/ext/pybind11/tests/test_eigen.cpp
index f2ec8fd2e..17b156ce4 100644
--- a/ext/pybind11/tests/test_eigen.cpp
+++ b/ext/pybind11/tests/test_eigen.cpp
@@ -10,6 +10,7 @@
#include "pybind11_tests.h"
#include "constructor_stats.h"
#include <pybind11/eigen.h>
+#include <pybind11/stl.h>
#include <Eigen/Cholesky>
using MatrixXdR = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
@@ -69,20 +70,21 @@ struct CustomOperatorNew {
EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
};
-test_initializer eigen([](py::module &m) {
- typedef Eigen::Matrix<float, 5, 6, Eigen::RowMajor> FixedMatrixR;
- typedef Eigen::Matrix<float, 5, 6> FixedMatrixC;
- typedef Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> DenseMatrixR;
- typedef Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> DenseMatrixC;
- typedef Eigen::Matrix<float, 4, Eigen::Dynamic> FourRowMatrixC;
- typedef Eigen::Matrix<float, Eigen::Dynamic, 4> FourColMatrixC;
- typedef Eigen::Matrix<float, 4, Eigen::Dynamic> FourRowMatrixR;
- typedef Eigen::Matrix<float, Eigen::Dynamic, 4> FourColMatrixR;
- typedef Eigen::SparseMatrix<float, Eigen::RowMajor> SparseMatrixR;
- typedef Eigen::SparseMatrix<float> SparseMatrixC;
+TEST_SUBMODULE(eigen, m) {
+ using FixedMatrixR = Eigen::Matrix<float, 5, 6, Eigen::RowMajor>;
+ using FixedMatrixC = Eigen::Matrix<float, 5, 6>;
+ using DenseMatrixR = Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
+ using DenseMatrixC = Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic>;
+ using FourRowMatrixC = Eigen::Matrix<float, 4, Eigen::Dynamic>;
+ using FourColMatrixC = Eigen::Matrix<float, Eigen::Dynamic, 4>;
+ using FourRowMatrixR = Eigen::Matrix<float, 4, Eigen::Dynamic>;
+ using FourColMatrixR = Eigen::Matrix<float, Eigen::Dynamic, 4>;
+ using SparseMatrixR = Eigen::SparseMatrix<float, Eigen::RowMajor>;
+ using SparseMatrixC = Eigen::SparseMatrix<float>;
m.attr("have_eigen") = true;
+ // various tests
m.def("double_col", [](const Eigen::VectorXf &x) -> Eigen::VectorXf { return 2.0f * x; });
m.def("double_row", [](const Eigen::RowVectorXf &x) -> Eigen::RowVectorXf { return 2.0f * x; });
m.def("double_complex", [](const Eigen::VectorXcf &x) -> Eigen::VectorXcf { return 2.0f * x; });
@@ -91,12 +93,14 @@ test_initializer eigen([](py::module &m) {
m.def("double_mat_cm", [](Eigen::MatrixXf x) -> Eigen::MatrixXf { return 2.0f * x; });
m.def("double_mat_rm", [](DenseMatrixR x) -> DenseMatrixR { return 2.0f * x; });
+ // test_eigen_ref_to_python
// Different ways of passing via Eigen::Ref; the first and second are the Eigen-recommended
m.def("cholesky1", [](Eigen::Ref<MatrixXdR> x) -> Eigen::MatrixXd { return x.llt().matrixL(); });
m.def("cholesky2", [](const Eigen::Ref<const MatrixXdR> &x) -> Eigen::MatrixXd { return x.llt().matrixL(); });
m.def("cholesky3", [](const Eigen::Ref<MatrixXdR> &x) -> Eigen::MatrixXd { return x.llt().matrixL(); });
m.def("cholesky4", [](Eigen::Ref<const MatrixXdR> x) -> Eigen::MatrixXd { return x.llt().matrixL(); });
+ // test_eigen_ref_mutators
// Mutators: these add some value to the given element using Eigen, but Eigen should be mapping into
// the numpy array data and so the result should show up there. There are three versions: one that
// works on a contiguous-row matrix (numpy's default), one for a contiguous-column matrix, and one
@@ -121,19 +125,6 @@ test_initializer eigen([](py::module &m) {
// The same references, but non-mutable (numpy maps into eigen variables, but is !writeable)
m.def("get_cm_const_ref", []() { return Eigen::Ref<const Eigen::MatrixXd>(get_cm()); });
m.def("get_rm_const_ref", []() { return Eigen::Ref<const MatrixXdR>(get_rm()); });
- // Just the corners (via a Map instead of a Ref):
- m.def("get_cm_corners", []() {
- auto &x = get_cm();
- return py::EigenDMap<Eigen::Matrix2d>(
- x.data(),
- py::EigenDStride(x.outerStride() * (x.rows() - 1), x.innerStride() * (x.cols() - 1)));
- });
- m.def("get_cm_corners_const", []() {
- const auto &x = get_cm();
- return py::EigenDMap<const Eigen::Matrix2d>(
- x.data(),
- py::EigenDStride(x.outerStride() * (x.rows() - 1), x.innerStride() * (x.cols() - 1)));
- });
m.def("reset_refs", reset_refs); // Restores get_{cm,rm}_ref to original values
@@ -173,6 +164,7 @@ test_initializer eigen([](py::module &m) {
return x.block(start_row, start_col, block_rows, block_cols);
});
+ // test_eigen_return_references, test_eigen_keepalive
// return value referencing/copying tests:
class ReturnTester {
Eigen::MatrixXd mat = create();
@@ -219,6 +211,7 @@ test_initializer eigen([](py::module &m) {
.def("corners_const", &ReturnTester::cornersConst, rvp::reference_internal)
;
+ // test_special_matrix_objects
// Returns a DiagonalMatrix with diagonal (1,2,3,...)
m.def("incr_diag", [](int k) {
Eigen::DiagonalMatrix<int, Eigen::Dynamic> m(k);
@@ -243,27 +236,33 @@ test_initializer eigen([](py::module &m) {
0, 0, 0, 0, 0, 11,
0, 0, 14, 0, 8, 11;
+ // test_fixed, and various other tests
m.def("fixed_r", [mat]() -> FixedMatrixR { return FixedMatrixR(mat); });
m.def("fixed_r_const", [mat]() -> const FixedMatrixR { return FixedMatrixR(mat); });
m.def("fixed_c", [mat]() -> FixedMatrixC { return FixedMatrixC(mat); });
m.def("fixed_copy_r", [](const FixedMatrixR &m) -> FixedMatrixR { return m; });
m.def("fixed_copy_c", [](const FixedMatrixC &m) -> FixedMatrixC { return m; });
+ // test_mutator_descriptors
m.def("fixed_mutator_r", [](Eigen::Ref<FixedMatrixR>) {});
m.def("fixed_mutator_c", [](Eigen::Ref<FixedMatrixC>) {});
m.def("fixed_mutator_a", [](py::EigenDRef<FixedMatrixC>) {});
+ // test_dense
m.def("dense_r", [mat]() -> DenseMatrixR { return DenseMatrixR(mat); });
m.def("dense_c", [mat]() -> DenseMatrixC { return DenseMatrixC(mat); });
m.def("dense_copy_r", [](const DenseMatrixR &m) -> DenseMatrixR { return m; });
m.def("dense_copy_c", [](const DenseMatrixC &m) -> DenseMatrixC { return m; });
+ // test_sparse, test_sparse_signature
m.def("sparse_r", [mat]() -> SparseMatrixR { return Eigen::SparseView<Eigen::MatrixXf>(mat); });
m.def("sparse_c", [mat]() -> SparseMatrixC { return Eigen::SparseView<Eigen::MatrixXf>(mat); });
m.def("sparse_copy_r", [](const SparseMatrixR &m) -> SparseMatrixR { return m; });
m.def("sparse_copy_c", [](const SparseMatrixC &m) -> SparseMatrixC { return m; });
+ // test_partially_fixed
m.def("partial_copy_four_rm_r", [](const FourRowMatrixR &m) -> FourRowMatrixR { return m; });
m.def("partial_copy_four_rm_c", [](const FourColMatrixR &m) -> FourColMatrixR { return m; });
m.def("partial_copy_four_cm_r", [](const FourRowMatrixC &m) -> FourRowMatrixC { return m; });
m.def("partial_copy_four_cm_c", [](const FourColMatrixC &m) -> FourColMatrixC { return m; });
+ // test_cpp_casting
// Test that we can cast a numpy object to a Eigen::MatrixXd explicitly
m.def("cpp_copy", [](py::handle m) { return m.cast<Eigen::MatrixXd>()(1, 0); });
m.def("cpp_ref_c", [](py::handle m) { return m.cast<Eigen::Ref<Eigen::MatrixXd>>()(1, 0); });
@@ -271,6 +270,7 @@ test_initializer eigen([](py::module &m) {
m.def("cpp_ref_any", [](py::handle m) { return m.cast<py::EigenDRef<Eigen::MatrixXd>>()(1, 0); });
+ // test_nocopy_wrapper
// Test that we can prevent copying into an argument that would normally copy: First a version
// that would allow copying (if types or strides don't match) for comparison:
m.def("get_elem", &get_elem);
@@ -281,14 +281,37 @@ test_initializer eigen([](py::module &m) {
m.def("get_elem_rm_nocopy", [](Eigen::Ref<const Eigen::Matrix<long, -1, -1, Eigen::RowMajor>> &m) -> long { return m(2, 1); },
py::arg().noconvert());
+ // test_issue738
// Issue #738: 1xN or Nx1 2D matrices were neither accepted nor properly copied with an
// incompatible stride value on the length-1 dimension--but that should be allowed (without
// requiring a copy!) because the stride value can be safely ignored on a size-1 dimension.
m.def("iss738_f1", &adjust_matrix<const Eigen::Ref<const Eigen::MatrixXd> &>, py::arg().noconvert());
m.def("iss738_f2", &adjust_matrix<const Eigen::Ref<const Eigen::Matrix<double, -1, -1, Eigen::RowMajor>> &>, py::arg().noconvert());
+ // test_named_arguments
+ // Make sure named arguments are working properly:
+ m.def("matrix_multiply", [](const py::EigenDRef<const Eigen::MatrixXd> A, const py::EigenDRef<const Eigen::MatrixXd> B)
+ -> Eigen::MatrixXd {
+ if (A.cols() != B.rows()) throw std::domain_error("Nonconformable matrices!");
+ return A * B;
+ }, py::arg("A"), py::arg("B"));
+
+ // test_custom_operator_new
py::class_<CustomOperatorNew>(m, "CustomOperatorNew")
.def(py::init<>())
.def_readonly("a", &CustomOperatorNew::a)
.def_readonly("b", &CustomOperatorNew::b);
-});
+
+ // test_eigen_ref_life_support
+ // In case of a failure (the caster's temp array does not live long enough), creating
+ // a new array (np.ones(10)) increases the chances that the temp array will be garbage
+ // collected and/or that its memory will be overridden with different values.
+ m.def("get_elem_direct", [](Eigen::Ref<const Eigen::VectorXd> v) {
+ py::module::import("numpy").attr("ones")(10);
+ return v(5);
+ });
+ m.def("get_elem_indirect", [](std::vector<Eigen::Ref<const Eigen::VectorXd>> v) {
+ py::module::import("numpy").attr("ones")(10);
+ return v[0](5);
+ });
+}
diff --git a/ext/pybind11/tests/test_eigen.py b/ext/pybind11/tests/test_eigen.py
index df08edf3d..4ac8cbf5d 100644
--- a/ext/pybind11/tests/test_eigen.py
+++ b/ext/pybind11/tests/test_eigen.py
@@ -1,8 +1,10 @@
import pytest
+from pybind11_tests import ConstructorStats
pytestmark = pytest.requires_eigen_and_numpy
with pytest.suppress(ImportError):
+ from pybind11_tests import eigen as m
import numpy as np
ref = np.array([[ 0., 3, 0, 0, 0, 11],
@@ -21,156 +23,190 @@ def assert_sparse_equal_ref(sparse_mat):
def test_fixed():
- from pybind11_tests import fixed_r, fixed_c, fixed_copy_r, fixed_copy_c
-
- assert_equal_ref(fixed_c())
- assert_equal_ref(fixed_r())
- assert_equal_ref(fixed_copy_r(fixed_r()))
- assert_equal_ref(fixed_copy_c(fixed_c()))
- assert_equal_ref(fixed_copy_r(fixed_c()))
- assert_equal_ref(fixed_copy_c(fixed_r()))
+ assert_equal_ref(m.fixed_c())
+ assert_equal_ref(m.fixed_r())
+ assert_equal_ref(m.fixed_copy_r(m.fixed_r()))
+ assert_equal_ref(m.fixed_copy_c(m.fixed_c()))
+ assert_equal_ref(m.fixed_copy_r(m.fixed_c()))
+ assert_equal_ref(m.fixed_copy_c(m.fixed_r()))
def test_dense():
- from pybind11_tests import dense_r, dense_c, dense_copy_r, dense_copy_c
-
- assert_equal_ref(dense_r())
- assert_equal_ref(dense_c())
- assert_equal_ref(dense_copy_r(dense_r()))
- assert_equal_ref(dense_copy_c(dense_c()))
- assert_equal_ref(dense_copy_r(dense_c()))
- assert_equal_ref(dense_copy_c(dense_r()))
+ assert_equal_ref(m.dense_r())
+ assert_equal_ref(m.dense_c())
+ assert_equal_ref(m.dense_copy_r(m.dense_r()))
+ assert_equal_ref(m.dense_copy_c(m.dense_c()))
+ assert_equal_ref(m.dense_copy_r(m.dense_c()))
+ assert_equal_ref(m.dense_copy_c(m.dense_r()))
def test_partially_fixed():
- from pybind11_tests import (partial_copy_four_rm_r, partial_copy_four_rm_c,
- partial_copy_four_cm_r, partial_copy_four_cm_c)
-
ref2 = np.array([[0., 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]])
- np.testing.assert_array_equal(partial_copy_four_rm_r(ref2), ref2)
- np.testing.assert_array_equal(partial_copy_four_rm_c(ref2), ref2)
- np.testing.assert_array_equal(partial_copy_four_rm_r(ref2[:, 1]), ref2[:, [1]])
- np.testing.assert_array_equal(partial_copy_four_rm_c(ref2[0, :]), ref2[[0], :])
- np.testing.assert_array_equal(partial_copy_four_rm_r(ref2[:, (0, 2)]), ref2[:, (0, 2)])
+ np.testing.assert_array_equal(m.partial_copy_four_rm_r(ref2), ref2)
+ np.testing.assert_array_equal(m.partial_copy_four_rm_c(ref2), ref2)
+ np.testing.assert_array_equal(m.partial_copy_four_rm_r(ref2[:, 1]), ref2[:, [1]])
+ np.testing.assert_array_equal(m.partial_copy_four_rm_c(ref2[0, :]), ref2[[0], :])
+ np.testing.assert_array_equal(m.partial_copy_four_rm_r(ref2[:, (0, 2)]), ref2[:, (0, 2)])
np.testing.assert_array_equal(
- partial_copy_four_rm_c(ref2[(3, 1, 2), :]), ref2[(3, 1, 2), :])
+ m.partial_copy_four_rm_c(ref2[(3, 1, 2), :]), ref2[(3, 1, 2), :])
- np.testing.assert_array_equal(partial_copy_four_cm_r(ref2), ref2)
- np.testing.assert_array_equal(partial_copy_four_cm_c(ref2), ref2)
- np.testing.assert_array_equal(partial_copy_four_cm_r(ref2[:, 1]), ref2[:, [1]])
- np.testing.assert_array_equal(partial_copy_four_cm_c(ref2[0, :]), ref2[[0], :])
- np.testing.assert_array_equal(partial_copy_four_cm_r(ref2[:, (0, 2)]), ref2[:, (0, 2)])
+ np.testing.assert_array_equal(m.partial_copy_four_cm_r(ref2), ref2)
+ np.testing.assert_array_equal(m.partial_copy_four_cm_c(ref2), ref2)
+ np.testing.assert_array_equal(m.partial_copy_four_cm_r(ref2[:, 1]), ref2[:, [1]])
+ np.testing.assert_array_equal(m.partial_copy_four_cm_c(ref2[0, :]), ref2[[0], :])
+ np.testing.assert_array_equal(m.partial_copy_four_cm_r(ref2[:, (0, 2)]), ref2[:, (0, 2)])
np.testing.assert_array_equal(
- partial_copy_four_cm_c(ref2[(3, 1, 2), :]), ref2[(3, 1, 2), :])
+ m.partial_copy_four_cm_c(ref2[(3, 1, 2), :]), ref2[(3, 1, 2), :])
+
+ # TypeError should be raise for a shape mismatch
+ functions = [m.partial_copy_four_rm_r, m.partial_copy_four_rm_c,
+ m.partial_copy_four_cm_r, m.partial_copy_four_cm_c]
+ matrix_with_wrong_shape = [[1, 2],
+ [3, 4]]
+ for f in functions:
+ with pytest.raises(TypeError) as excinfo:
+ f(matrix_with_wrong_shape)
+ assert "incompatible function arguments" in str(excinfo.value)
def test_mutator_descriptors():
- from pybind11_tests import fixed_mutator_r, fixed_mutator_c, fixed_mutator_a
zr = np.arange(30, dtype='float32').reshape(5, 6) # row-major
zc = zr.reshape(6, 5).transpose() # column-major
- fixed_mutator_r(zr)
- fixed_mutator_c(zc)
- fixed_mutator_a(zr)
- fixed_mutator_a(zc)
+ m.fixed_mutator_r(zr)
+ m.fixed_mutator_c(zc)
+ m.fixed_mutator_a(zr)
+ m.fixed_mutator_a(zc)
with pytest.raises(TypeError) as excinfo:
- fixed_mutator_r(zc)
- assert ('(numpy.ndarray[float32[5, 6], flags.writeable, flags.c_contiguous]) -> arg0: None'
+ m.fixed_mutator_r(zc)
+ assert ('(arg0: numpy.ndarray[float32[5, 6], flags.writeable, flags.c_contiguous]) -> None'
in str(excinfo.value))
with pytest.raises(TypeError) as excinfo:
- fixed_mutator_c(zr)
- assert ('(numpy.ndarray[float32[5, 6], flags.writeable, flags.f_contiguous]) -> arg0: None'
+ m.fixed_mutator_c(zr)
+ assert ('(arg0: numpy.ndarray[float32[5, 6], flags.writeable, flags.f_contiguous]) -> None'
in str(excinfo.value))
with pytest.raises(TypeError) as excinfo:
- fixed_mutator_a(np.array([[1, 2], [3, 4]], dtype='float32'))
- assert ('(numpy.ndarray[float32[5, 6], flags.writeable]) -> arg0: None'
+ m.fixed_mutator_a(np.array([[1, 2], [3, 4]], dtype='float32'))
+ assert ('(arg0: numpy.ndarray[float32[5, 6], flags.writeable]) -> None'
in str(excinfo.value))
zr.flags.writeable = False
with pytest.raises(TypeError):
- fixed_mutator_r(zr)
+ m.fixed_mutator_r(zr)
with pytest.raises(TypeError):
- fixed_mutator_a(zr)
+ m.fixed_mutator_a(zr)
def test_cpp_casting():
- from pybind11_tests import (cpp_copy, cpp_ref_c, cpp_ref_r, cpp_ref_any,
- fixed_r, fixed_c, get_cm_ref, get_rm_ref, ReturnTester)
- assert cpp_copy(fixed_r()) == 22.
- assert cpp_copy(fixed_c()) == 22.
+ assert m.cpp_copy(m.fixed_r()) == 22.
+ assert m.cpp_copy(m.fixed_c()) == 22.
z = np.array([[5., 6], [7, 8]])
- assert cpp_copy(z) == 7.
- assert cpp_copy(get_cm_ref()) == 21.
- assert cpp_copy(get_rm_ref()) == 21.
- assert cpp_ref_c(get_cm_ref()) == 21.
- assert cpp_ref_r(get_rm_ref()) == 21.
+ assert m.cpp_copy(z) == 7.
+ assert m.cpp_copy(m.get_cm_ref()) == 21.
+ assert m.cpp_copy(m.get_rm_ref()) == 21.
+ assert m.cpp_ref_c(m.get_cm_ref()) == 21.
+ assert m.cpp_ref_r(m.get_rm_ref()) == 21.
with pytest.raises(RuntimeError) as excinfo:
- # Can't reference fixed_c: it contains floats, cpp_ref_any wants doubles
- cpp_ref_any(fixed_c())
+ # Can't reference m.fixed_c: it contains floats, m.cpp_ref_any wants doubles
+ m.cpp_ref_any(m.fixed_c())
assert 'Unable to cast Python instance' in str(excinfo.value)
with pytest.raises(RuntimeError) as excinfo:
- # Can't reference fixed_r: it contains floats, cpp_ref_any wants doubles
- cpp_ref_any(fixed_r())
+ # Can't reference m.fixed_r: it contains floats, m.cpp_ref_any wants doubles
+ m.cpp_ref_any(m.fixed_r())
assert 'Unable to cast Python instance' in str(excinfo.value)
- assert cpp_ref_any(ReturnTester.create()) == 1.
+ assert m.cpp_ref_any(m.ReturnTester.create()) == 1.
- assert cpp_ref_any(get_cm_ref()) == 21.
- assert cpp_ref_any(get_cm_ref()) == 21.
+ assert m.cpp_ref_any(m.get_cm_ref()) == 21.
+ assert m.cpp_ref_any(m.get_cm_ref()) == 21.
def test_pass_readonly_array():
- from pybind11_tests import fixed_copy_r, fixed_r, fixed_r_const
z = np.full((5, 6), 42.0)
z.flags.writeable = False
- np.testing.assert_array_equal(z, fixed_copy_r(z))
- np.testing.assert_array_equal(fixed_r_const(), fixed_r())
- assert not fixed_r_const().flags.writeable
- np.testing.assert_array_equal(fixed_copy_r(fixed_r_const()), fixed_r_const())
+ np.testing.assert_array_equal(z, m.fixed_copy_r(z))
+ np.testing.assert_array_equal(m.fixed_r_const(), m.fixed_r())
+ assert not m.fixed_r_const().flags.writeable
+ np.testing.assert_array_equal(m.fixed_copy_r(m.fixed_r_const()), m.fixed_r_const())
def test_nonunit_stride_from_python():
- from pybind11_tests import (
- double_row, double_col, double_complex, double_mat_cm, double_mat_rm,
- double_threec, double_threer)
-
counting_mat = np.arange(9.0, dtype=np.float32).reshape((3, 3))
second_row = counting_mat[1, :]
second_col = counting_mat[:, 1]
- np.testing.assert_array_equal(double_row(second_row), 2.0 * second_row)
- np.testing.assert_array_equal(double_col(second_row), 2.0 * second_row)
- np.testing.assert_array_equal(double_complex(second_row), 2.0 * second_row)
- np.testing.assert_array_equal(double_row(second_col), 2.0 * second_col)
- np.testing.assert_array_equal(double_col(second_col), 2.0 * second_col)
- np.testing.assert_array_equal(double_complex(second_col), 2.0 * second_col)
+ np.testing.assert_array_equal(m.double_row(second_row), 2.0 * second_row)
+ np.testing.assert_array_equal(m.double_col(second_row), 2.0 * second_row)
+ np.testing.assert_array_equal(m.double_complex(second_row), 2.0 * second_row)
+ np.testing.assert_array_equal(m.double_row(second_col), 2.0 * second_col)
+ np.testing.assert_array_equal(m.double_col(second_col), 2.0 * second_col)
+ np.testing.assert_array_equal(m.double_complex(second_col), 2.0 * second_col)
counting_3d = np.arange(27.0, dtype=np.float32).reshape((3, 3, 3))
slices = [counting_3d[0, :, :], counting_3d[:, 0, :], counting_3d[:, :, 0]]
for slice_idx, ref_mat in enumerate(slices):
- np.testing.assert_array_equal(double_mat_cm(ref_mat), 2.0 * ref_mat)
- np.testing.assert_array_equal(double_mat_rm(ref_mat), 2.0 * ref_mat)
+ np.testing.assert_array_equal(m.double_mat_cm(ref_mat), 2.0 * ref_mat)
+ np.testing.assert_array_equal(m.double_mat_rm(ref_mat), 2.0 * ref_mat)
# Mutator:
- double_threer(second_row)
- double_threec(second_col)
+ m.double_threer(second_row)
+ m.double_threec(second_col)
np.testing.assert_array_equal(counting_mat, [[0., 2, 2], [6, 16, 10], [6, 14, 8]])
-def test_nonunit_stride_to_python():
- from pybind11_tests import diagonal, diagonal_1, diagonal_n, block
+def test_negative_stride_from_python(msg):
+ """Eigen doesn't support (as of yet) negative strides. When a function takes an Eigen matrix by
+ copy or const reference, we can pass a numpy array that has negative strides. Otherwise, an
+ exception will be thrown as Eigen will not be able to map the numpy array."""
+
+ counting_mat = np.arange(9.0, dtype=np.float32).reshape((3, 3))
+ counting_mat = counting_mat[::-1, ::-1]
+ second_row = counting_mat[1, :]
+ second_col = counting_mat[:, 1]
+ np.testing.assert_array_equal(m.double_row(second_row), 2.0 * second_row)
+ np.testing.assert_array_equal(m.double_col(second_row), 2.0 * second_row)
+ np.testing.assert_array_equal(m.double_complex(second_row), 2.0 * second_row)
+ np.testing.assert_array_equal(m.double_row(second_col), 2.0 * second_col)
+ np.testing.assert_array_equal(m.double_col(second_col), 2.0 * second_col)
+ np.testing.assert_array_equal(m.double_complex(second_col), 2.0 * second_col)
+
+ counting_3d = np.arange(27.0, dtype=np.float32).reshape((3, 3, 3))
+ counting_3d = counting_3d[::-1, ::-1, ::-1]
+ slices = [counting_3d[0, :, :], counting_3d[:, 0, :], counting_3d[:, :, 0]]
+ for slice_idx, ref_mat in enumerate(slices):
+ np.testing.assert_array_equal(m.double_mat_cm(ref_mat), 2.0 * ref_mat)
+ np.testing.assert_array_equal(m.double_mat_rm(ref_mat), 2.0 * ref_mat)
+
+ # Mutator:
+ with pytest.raises(TypeError) as excinfo:
+ m.double_threer(second_row)
+ assert msg(excinfo.value) == """
+ double_threer(): incompatible function arguments. The following argument types are supported:
+ 1. (arg0: numpy.ndarray[float32[1, 3], flags.writeable]) -> None
+
+ Invoked with: array([ 5., 4., 3.], dtype=float32)
+ """ # noqa: E501 line too long
+
+ with pytest.raises(TypeError) as excinfo:
+ m.double_threec(second_col)
+ assert msg(excinfo.value) == """
+ double_threec(): incompatible function arguments. The following argument types are supported:
+ 1. (arg0: numpy.ndarray[float32[3, 1], flags.writeable]) -> None
+
+ Invoked with: array([ 7., 4., 1.], dtype=float32)
+ """ # noqa: E501 line too long
+
- assert np.all(diagonal(ref) == ref.diagonal())
- assert np.all(diagonal_1(ref) == ref.diagonal(1))
+def test_nonunit_stride_to_python():
+ assert np.all(m.diagonal(ref) == ref.diagonal())
+ assert np.all(m.diagonal_1(ref) == ref.diagonal(1))
for i in range(-5, 7):
- assert np.all(diagonal_n(ref, i) == ref.diagonal(i)), "diagonal_n({})".format(i)
+ assert np.all(m.diagonal_n(ref, i) == ref.diagonal(i)), "m.diagonal_n({})".format(i)
- assert np.all(block(ref, 2, 1, 3, 3) == ref[2:5, 1:4])
- assert np.all(block(ref, 1, 4, 4, 2) == ref[1:, 4:])
- assert np.all(block(ref, 1, 4, 3, 2) == ref[1:4, 4:])
+ assert np.all(m.block(ref, 2, 1, 3, 3) == ref[2:5, 1:4])
+ assert np.all(m.block(ref, 1, 4, 4, 2) == ref[1:, 4:])
+ assert np.all(m.block(ref, 1, 4, 3, 2) == ref[1:4, 4:])
def test_eigen_ref_to_python():
- from pybind11_tests import cholesky1, cholesky2, cholesky3, cholesky4
-
- chols = [cholesky1, cholesky2, cholesky3, cholesky4]
+ chols = [m.cholesky1, m.cholesky2, m.cholesky3, m.cholesky4]
for i, chol in enumerate(chols, start=1):
mymat = chol(np.array([[1., 2, 4], [2, 13, 23], [4, 23, 77]]))
assert np.all(mymat == np.array([[1, 0, 0], [2, 3, 0], [4, 5, 6]])), "cholesky{}".format(i)
@@ -189,9 +225,9 @@ def array_copy_but_one(a, r, c, v):
def test_eigen_return_references():
"""Tests various ways of returning references and non-referencing copies"""
- from pybind11_tests import ReturnTester
+
master = np.ones((10, 10))
- a = ReturnTester()
+ a = m.ReturnTester()
a_get1 = a.get()
assert not a_get1.flags.owndata and a_get1.flags.writeable
assign_both(a_get1, master, 3, 3, 5)
@@ -301,7 +337,6 @@ def test_eigen_return_references():
def assert_keeps_alive(cl, method, *args):
- from pybind11_tests import ConstructorStats
cstats = ConstructorStats.get(cl)
start_with = cstats.alive()
a = cl()
@@ -317,10 +352,8 @@ def assert_keeps_alive(cl, method, *args):
def test_eigen_keepalive():
- from pybind11_tests import ReturnTester, ConstructorStats
- a = ReturnTester()
-
- cstats = ConstructorStats.get(ReturnTester)
+ a = m.ReturnTester()
+ cstats = ConstructorStats.get(m.ReturnTester)
assert cstats.alive() == 1
unsafe = [a.ref(), a.ref_const(), a.block(1, 2, 3, 4)]
copies = [a.copy_get(), a.copy_view(), a.copy_ref(), a.copy_ref_const(),
@@ -330,43 +363,43 @@ def test_eigen_keepalive():
del unsafe
del copies
- for meth in [ReturnTester.get, ReturnTester.get_ptr, ReturnTester.view,
- ReturnTester.view_ptr, ReturnTester.ref_safe, ReturnTester.ref_const_safe,
- ReturnTester.corners, ReturnTester.corners_const]:
- assert_keeps_alive(ReturnTester, meth)
+ for meth in [m.ReturnTester.get, m.ReturnTester.get_ptr, m.ReturnTester.view,
+ m.ReturnTester.view_ptr, m.ReturnTester.ref_safe, m.ReturnTester.ref_const_safe,
+ m.ReturnTester.corners, m.ReturnTester.corners_const]:
+ assert_keeps_alive(m.ReturnTester, meth)
- for meth in [ReturnTester.block_safe, ReturnTester.block_const]:
- assert_keeps_alive(ReturnTester, meth, 4, 3, 2, 1)
+ for meth in [m.ReturnTester.block_safe, m.ReturnTester.block_const]:
+ assert_keeps_alive(m.ReturnTester, meth, 4, 3, 2, 1)
def test_eigen_ref_mutators():
- """Tests whether Eigen can mutate numpy values"""
- from pybind11_tests import add_rm, add_cm, add_any, add1, add2
+ """Tests Eigen's ability to mutate numpy values"""
+
orig = np.array([[1., 2, 3], [4, 5, 6], [7, 8, 9]])
zr = np.array(orig)
zc = np.array(orig, order='F')
- add_rm(zr, 1, 0, 100)
+ m.add_rm(zr, 1, 0, 100)
assert np.all(zr == np.array([[1., 2, 3], [104, 5, 6], [7, 8, 9]]))
- add_cm(zc, 1, 0, 200)
+ m.add_cm(zc, 1, 0, 200)
assert np.all(zc == np.array([[1., 2, 3], [204, 5, 6], [7, 8, 9]]))
- add_any(zr, 1, 0, 20)
+ m.add_any(zr, 1, 0, 20)
assert np.all(zr == np.array([[1., 2, 3], [124, 5, 6], [7, 8, 9]]))
- add_any(zc, 1, 0, 10)
+ m.add_any(zc, 1, 0, 10)
assert np.all(zc == np.array([[1., 2, 3], [214, 5, 6], [7, 8, 9]]))
# Can't reference a col-major array with a row-major Ref, and vice versa:
with pytest.raises(TypeError):
- add_rm(zc, 1, 0, 1)
+ m.add_rm(zc, 1, 0, 1)
with pytest.raises(TypeError):
- add_cm(zr, 1, 0, 1)
+ m.add_cm(zr, 1, 0, 1)
# Overloads:
- add1(zr, 1, 0, -100)
- add2(zr, 1, 0, -20)
+ m.add1(zr, 1, 0, -100)
+ m.add2(zr, 1, 0, -20)
assert np.all(zr == orig)
- add1(zc, 1, 0, -200)
- add2(zc, 1, 0, -10)
+ m.add1(zc, 1, 0, -200)
+ m.add2(zc, 1, 0, -10)
assert np.all(zc == orig)
# a non-contiguous slice (this won't work on either the row- or
@@ -378,15 +411,15 @@ def test_eigen_ref_mutators():
assert np.all(cornersc == np.array([[1., 3], [7, 9]]))
with pytest.raises(TypeError):
- add_rm(cornersr, 0, 1, 25)
+ m.add_rm(cornersr, 0, 1, 25)
with pytest.raises(TypeError):
- add_cm(cornersr, 0, 1, 25)
+ m.add_cm(cornersr, 0, 1, 25)
with pytest.raises(TypeError):
- add_rm(cornersc, 0, 1, 25)
+ m.add_rm(cornersc, 0, 1, 25)
with pytest.raises(TypeError):
- add_cm(cornersc, 0, 1, 25)
- add_any(cornersr, 0, 1, 25)
- add_any(cornersc, 0, 1, 44)
+ m.add_cm(cornersc, 0, 1, 25)
+ m.add_any(cornersr, 0, 1, 25)
+ m.add_any(cornersc, 0, 1, 44)
assert np.all(zr == np.array([[1., 2, 28], [4, 5, 6], [7, 8, 9]]))
assert np.all(zc == np.array([[1., 2, 47], [4, 5, 6], [7, 8, 9]]))
@@ -394,30 +427,29 @@ def test_eigen_ref_mutators():
zro = zr[0:4, 0:4]
zro.flags.writeable = False
with pytest.raises(TypeError):
- add_rm(zro, 0, 0, 0)
+ m.add_rm(zro, 0, 0, 0)
with pytest.raises(TypeError):
- add_any(zro, 0, 0, 0)
+ m.add_any(zro, 0, 0, 0)
with pytest.raises(TypeError):
- add1(zro, 0, 0, 0)
+ m.add1(zro, 0, 0, 0)
with pytest.raises(TypeError):
- add2(zro, 0, 0, 0)
+ m.add2(zro, 0, 0, 0)
# integer array shouldn't be passable to a double-matrix-accepting mutating func:
zi = np.array([[1, 2], [3, 4]])
with pytest.raises(TypeError):
- add_rm(zi)
+ m.add_rm(zi)
def test_numpy_ref_mutators():
"""Tests numpy mutating Eigen matrices (for returned Eigen::Ref<...>s)"""
- from pybind11_tests import (
- get_cm_ref, get_cm_const_ref, get_rm_ref, get_rm_const_ref, reset_refs)
- reset_refs() # In case another test already changed it
- zc = get_cm_ref()
- zcro = get_cm_const_ref()
- zr = get_rm_ref()
- zrro = get_rm_const_ref()
+ m.reset_refs() # In case another test already changed it
+
+ zc = m.get_cm_ref()
+ zcro = m.get_cm_const_ref()
+ zr = m.get_rm_ref()
+ zrro = m.get_rm_const_ref()
assert [zc[1, 2], zcro[1, 2], zr[1, 2], zrro[1, 2]] == [23] * 4
@@ -431,12 +463,12 @@ def test_numpy_ref_mutators():
# We should have just changed zc, of course, but also zcro and the original eigen matrix
assert np.all(zc == expect)
assert np.all(zcro == expect)
- assert np.all(get_cm_ref() == expect)
+ assert np.all(m.get_cm_ref() == expect)
zr[1, 2] = 99
assert np.all(zr == expect)
assert np.all(zrro == expect)
- assert np.all(get_rm_ref() == expect)
+ assert np.all(m.get_rm_ref() == expect)
# Make sure the readonly ones are numpy-readonly:
with pytest.raises(ValueError):
@@ -446,7 +478,7 @@ def test_numpy_ref_mutators():
# We should be able to explicitly copy like this (and since we're copying,
# the const should drop away)
- y1 = np.array(get_cm_const_ref())
+ y1 = np.array(m.get_cm_const_ref())
assert y1.flags.owndata and y1.flags.writeable
# We should get copies of the eigen data, which was modified above:
@@ -458,19 +490,18 @@ def test_numpy_ref_mutators():
def test_both_ref_mutators():
"""Tests a complex chain of nested eigen/numpy references"""
- from pybind11_tests import (
- incr_matrix, get_cm_ref, incr_matrix_any, even_cols, even_rows, reset_refs)
- reset_refs() # In case another test already changed it
- z = get_cm_ref() # numpy -> eigen
+ m.reset_refs() # In case another test already changed it
+
+ z = m.get_cm_ref() # numpy -> eigen
z[0, 2] -= 3
- z2 = incr_matrix(z, 1) # numpy -> eigen -> numpy -> eigen
+ z2 = m.incr_matrix(z, 1) # numpy -> eigen -> numpy -> eigen
z2[1, 1] += 6
- z3 = incr_matrix(z, 2) # (numpy -> eigen)^3
+ z3 = m.incr_matrix(z, 2) # (numpy -> eigen)^3
z3[2, 2] += -5
- z4 = incr_matrix(z, 3) # (numpy -> eigen)^4
+ z4 = m.incr_matrix(z, 3) # (numpy -> eigen)^4
z4[1, 1] -= 1
- z5 = incr_matrix(z, 4) # (numpy -> eigen)^5
+ z5 = m.incr_matrix(z, 4) # (numpy -> eigen)^5
z5[0, 0] = 0
assert np.all(z == z2)
assert np.all(z == z3)
@@ -480,11 +511,11 @@ def test_both_ref_mutators():
assert np.all(z == expect)
y = np.array(range(100), dtype='float64').reshape(10, 10)
- y2 = incr_matrix_any(y, 10) # np -> eigen -> np
- y3 = incr_matrix_any(y2[0::2, 0::2], -33) # np -> eigen -> np slice -> np -> eigen -> np
- y4 = even_rows(y3) # numpy -> eigen slice -> (... y3)
- y5 = even_cols(y4) # numpy -> eigen slice -> (... y4)
- y6 = incr_matrix_any(y5, 1000) # numpy -> eigen -> (... y5)
+ y2 = m.incr_matrix_any(y, 10) # np -> eigen -> np
+ y3 = m.incr_matrix_any(y2[0::2, 0::2], -33) # np -> eigen -> np slice -> np -> eigen -> np
+ y4 = m.even_rows(y3) # numpy -> eigen slice -> (... y3)
+ y5 = m.even_cols(y4) # numpy -> eigen slice -> (... y4)
+ y6 = m.incr_matrix_any(y5, 1000) # numpy -> eigen -> (... y5)
# Apply same mutations using just numpy:
yexpect = np.array(range(100), dtype='float64').reshape(10, 10)
@@ -500,7 +531,6 @@ def test_both_ref_mutators():
def test_nocopy_wrapper():
- from pybind11_tests import get_elem, get_elem_nocopy, get_elem_rm_nocopy
# get_elem requires a column-contiguous matrix reference, but should be
# callable with other types of matrix (via copying):
int_matrix_colmajor = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], order='F')
@@ -509,46 +539,58 @@ def test_nocopy_wrapper():
dbl_matrix_rowmajor = np.array(int_matrix_rowmajor, dtype='double', order='C', copy=True)
# All should be callable via get_elem:
- assert get_elem(int_matrix_colmajor) == 8
- assert get_elem(dbl_matrix_colmajor) == 8
- assert get_elem(int_matrix_rowmajor) == 8
- assert get_elem(dbl_matrix_rowmajor) == 8
+ assert m.get_elem(int_matrix_colmajor) == 8
+ assert m.get_elem(dbl_matrix_colmajor) == 8
+ assert m.get_elem(int_matrix_rowmajor) == 8
+ assert m.get_elem(dbl_matrix_rowmajor) == 8
- # All but the second should fail with get_elem_nocopy:
+ # All but the second should fail with m.get_elem_nocopy:
with pytest.raises(TypeError) as excinfo:
- get_elem_nocopy(int_matrix_colmajor)
+ m.get_elem_nocopy(int_matrix_colmajor)
assert ('get_elem_nocopy(): incompatible function arguments.' in str(excinfo.value) and
', flags.f_contiguous' in str(excinfo.value))
- assert get_elem_nocopy(dbl_matrix_colmajor) == 8
+ assert m.get_elem_nocopy(dbl_matrix_colmajor) == 8
with pytest.raises(TypeError) as excinfo:
- get_elem_nocopy(int_matrix_rowmajor)
+ m.get_elem_nocopy(int_matrix_rowmajor)
assert ('get_elem_nocopy(): incompatible function arguments.' in str(excinfo.value) and
', flags.f_contiguous' in str(excinfo.value))
with pytest.raises(TypeError) as excinfo:
- get_elem_nocopy(dbl_matrix_rowmajor)
+ m.get_elem_nocopy(dbl_matrix_rowmajor)
assert ('get_elem_nocopy(): incompatible function arguments.' in str(excinfo.value) and
', flags.f_contiguous' in str(excinfo.value))
# For the row-major test, we take a long matrix in row-major, so only the third is allowed:
with pytest.raises(TypeError) as excinfo:
- get_elem_rm_nocopy(int_matrix_colmajor)
+ m.get_elem_rm_nocopy(int_matrix_colmajor)
assert ('get_elem_rm_nocopy(): incompatible function arguments.' in str(excinfo.value) and
', flags.c_contiguous' in str(excinfo.value))
with pytest.raises(TypeError) as excinfo:
- get_elem_rm_nocopy(dbl_matrix_colmajor)
+ m.get_elem_rm_nocopy(dbl_matrix_colmajor)
assert ('get_elem_rm_nocopy(): incompatible function arguments.' in str(excinfo.value) and
', flags.c_contiguous' in str(excinfo.value))
- assert get_elem_rm_nocopy(int_matrix_rowmajor) == 8
+ assert m.get_elem_rm_nocopy(int_matrix_rowmajor) == 8
with pytest.raises(TypeError) as excinfo:
- get_elem_rm_nocopy(dbl_matrix_rowmajor)
+ m.get_elem_rm_nocopy(dbl_matrix_rowmajor)
assert ('get_elem_rm_nocopy(): incompatible function arguments.' in str(excinfo.value) and
', flags.c_contiguous' in str(excinfo.value))
-def test_special_matrix_objects():
- from pybind11_tests import incr_diag, symmetric_upper, symmetric_lower
+def test_eigen_ref_life_support():
+ """Ensure the lifetime of temporary arrays created by the `Ref` caster
+
+ The `Ref` caster sometimes creates a copy which needs to stay alive. This needs to
+ happen both for directs casts (just the array) or indirectly (e.g. list of arrays).
+ """
+
+ a = np.full(shape=10, fill_value=8, dtype=np.int8)
+ assert m.get_elem_direct(a) == 8
+
+ list_of_a = [a]
+ assert m.get_elem_indirect(list_of_a) == 8
- assert np.all(incr_diag(7) == np.diag([1., 2, 3, 4, 5, 6, 7]))
+
+def test_special_matrix_objects():
+ assert np.all(m.incr_diag(7) == np.diag([1., 2, 3, 4, 5, 6, 7]))
asymm = np.array([[ 1., 2, 3, 4],
[ 5, 6, 7, 8],
@@ -561,66 +603,79 @@ def test_special_matrix_objects():
symm_lower[i, j] = symm_lower[j, i]
symm_upper[j, i] = symm_upper[i, j]
- assert np.all(symmetric_lower(asymm) == symm_lower)
- assert np.all(symmetric_upper(asymm) == symm_upper)
+ assert np.all(m.symmetric_lower(asymm) == symm_lower)
+ assert np.all(m.symmetric_upper(asymm) == symm_upper)
def test_dense_signature(doc):
- from pybind11_tests import double_col, double_row, double_complex, double_mat_rm
-
- assert doc(double_col) == """
+ assert doc(m.double_col) == """
double_col(arg0: numpy.ndarray[float32[m, 1]]) -> numpy.ndarray[float32[m, 1]]
"""
- assert doc(double_row) == """
+ assert doc(m.double_row) == """
double_row(arg0: numpy.ndarray[float32[1, n]]) -> numpy.ndarray[float32[1, n]]
"""
- assert doc(double_complex) == """
+ assert doc(m.double_complex) == """
double_complex(arg0: numpy.ndarray[complex64[m, 1]]) -> numpy.ndarray[complex64[m, 1]]
"""
- assert doc(double_mat_rm) == """
+ assert doc(m.double_mat_rm) == """
double_mat_rm(arg0: numpy.ndarray[float32[m, n]]) -> numpy.ndarray[float32[m, n]]
"""
+def test_named_arguments():
+ a = np.array([[1.0, 2], [3, 4], [5, 6]])
+ b = np.ones((2, 1))
+
+ assert np.all(m.matrix_multiply(a, b) == np.array([[3.], [7], [11]]))
+ assert np.all(m.matrix_multiply(A=a, B=b) == np.array([[3.], [7], [11]]))
+ assert np.all(m.matrix_multiply(B=b, A=a) == np.array([[3.], [7], [11]]))
+
+ with pytest.raises(ValueError) as excinfo:
+ m.matrix_multiply(b, a)
+ assert str(excinfo.value) == 'Nonconformable matrices!'
+
+ with pytest.raises(ValueError) as excinfo:
+ m.matrix_multiply(A=b, B=a)
+ assert str(excinfo.value) == 'Nonconformable matrices!'
+
+ with pytest.raises(ValueError) as excinfo:
+ m.matrix_multiply(B=a, A=b)
+ assert str(excinfo.value) == 'Nonconformable matrices!'
+
+
@pytest.requires_eigen_and_scipy
def test_sparse():
- from pybind11_tests import sparse_r, sparse_c, sparse_copy_r, sparse_copy_c
-
- assert_sparse_equal_ref(sparse_r())
- assert_sparse_equal_ref(sparse_c())
- assert_sparse_equal_ref(sparse_copy_r(sparse_r()))
- assert_sparse_equal_ref(sparse_copy_c(sparse_c()))
- assert_sparse_equal_ref(sparse_copy_r(sparse_c()))
- assert_sparse_equal_ref(sparse_copy_c(sparse_r()))
+ assert_sparse_equal_ref(m.sparse_r())
+ assert_sparse_equal_ref(m.sparse_c())
+ assert_sparse_equal_ref(m.sparse_copy_r(m.sparse_r()))
+ assert_sparse_equal_ref(m.sparse_copy_c(m.sparse_c()))
+ assert_sparse_equal_ref(m.sparse_copy_r(m.sparse_c()))
+ assert_sparse_equal_ref(m.sparse_copy_c(m.sparse_r()))
@pytest.requires_eigen_and_scipy
def test_sparse_signature(doc):
- from pybind11_tests import sparse_copy_r, sparse_copy_c
-
- assert doc(sparse_copy_r) == """
+ assert doc(m.sparse_copy_r) == """
sparse_copy_r(arg0: scipy.sparse.csr_matrix[float32]) -> scipy.sparse.csr_matrix[float32]
""" # noqa: E501 line too long
- assert doc(sparse_copy_c) == """
+ assert doc(m.sparse_copy_c) == """
sparse_copy_c(arg0: scipy.sparse.csc_matrix[float32]) -> scipy.sparse.csc_matrix[float32]
""" # noqa: E501 line too long
def test_issue738():
- from pybind11_tests import iss738_f1, iss738_f2
-
- assert np.all(iss738_f1(np.array([[1., 2, 3]])) == np.array([[1., 102, 203]]))
- assert np.all(iss738_f1(np.array([[1.], [2], [3]])) == np.array([[1.], [12], [23]]))
+ """Ignore strides on a length-1 dimension (even if they would be incompatible length > 1)"""
+ assert np.all(m.iss738_f1(np.array([[1., 2, 3]])) == np.array([[1., 102, 203]]))
+ assert np.all(m.iss738_f1(np.array([[1.], [2], [3]])) == np.array([[1.], [12], [23]]))
- assert np.all(iss738_f2(np.array([[1., 2, 3]])) == np.array([[1., 102, 203]]))
- assert np.all(iss738_f2(np.array([[1.], [2], [3]])) == np.array([[1.], [12], [23]]))
+ assert np.all(m.iss738_f2(np.array([[1., 2, 3]])) == np.array([[1., 102, 203]]))
+ assert np.all(m.iss738_f2(np.array([[1.], [2], [3]])) == np.array([[1.], [12], [23]]))
def test_custom_operator_new():
"""Using Eigen types as member variables requires a class-specific
operator new with proper alignment"""
- from pybind11_tests import CustomOperatorNew
- o = CustomOperatorNew()
+ o = m.CustomOperatorNew()
np.testing.assert_allclose(o.a, 0.0)
np.testing.assert_allclose(o.b.diagonal(), 1.0)
diff --git a/ext/pybind11/tests/test_embed/CMakeLists.txt b/ext/pybind11/tests/test_embed/CMakeLists.txt
new file mode 100644
index 000000000..0a43e0e22
--- /dev/null
+++ b/ext/pybind11/tests/test_embed/CMakeLists.txt
@@ -0,0 +1,34 @@
+if(${PYTHON_MODULE_EXTENSION} MATCHES "pypy")
+ add_custom_target(cpptest) # Dummy target on PyPy. Embedding is not supported.
+ set(_suppress_unused_variable_warning "${DOWNLOAD_CATCH}")
+ return()
+endif()
+
+find_package(Catch 1.9.3)
+if(NOT CATCH_FOUND)
+ message(STATUS "Catch not detected. Interpreter tests will be skipped. Install Catch headers"
+ " manually or use `cmake -DDOWNLOAD_CATCH=1` to fetch them automatically.")
+ return()
+endif()
+
+add_executable(test_embed
+ catch.cpp
+ test_interpreter.cpp
+)
+target_include_directories(test_embed PRIVATE ${CATCH_INCLUDE_DIR})
+pybind11_enable_warnings(test_embed)
+
+if(NOT CMAKE_VERSION VERSION_LESS 3.0)
+ target_link_libraries(test_embed PRIVATE pybind11::embed)
+else()
+ target_include_directories(test_embed PRIVATE ${PYBIND11_INCLUDE_DIR} ${PYTHON_INCLUDE_DIRS})
+ target_compile_options(test_embed PRIVATE ${PYBIND11_CPP_STANDARD})
+ target_link_libraries(test_embed PRIVATE ${PYTHON_LIBRARIES})
+endif()
+
+find_package(Threads REQUIRED)
+target_link_libraries(test_embed PUBLIC ${CMAKE_THREAD_LIBS_INIT})
+
+add_custom_target(cpptest COMMAND $<TARGET_FILE:test_embed>
+ WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
+add_dependencies(check cpptest)
diff --git a/ext/pybind11/tests/test_embed/catch.cpp b/ext/pybind11/tests/test_embed/catch.cpp
new file mode 100644
index 000000000..cface485d
--- /dev/null
+++ b/ext/pybind11/tests/test_embed/catch.cpp
@@ -0,0 +1,16 @@
+// The Catch implementation is compiled here. This is a standalone
+// translation unit to avoid recompiling it for every test change.
+
+#include <pybind11/embed.h>
+
+#define CATCH_CONFIG_RUNNER
+#include <catch.hpp>
+
+namespace py = pybind11;
+
+int main(int argc, const char *argv[]) {
+ py::scoped_interpreter guard{};
+ auto result = Catch::Session().run(argc, argv);
+
+ return result < 0xff ? result : 0xff;
+}
diff --git a/ext/pybind11/tests/test_embed/test_interpreter.cpp b/ext/pybind11/tests/test_embed/test_interpreter.cpp
new file mode 100644
index 000000000..6b5f051f2
--- /dev/null
+++ b/ext/pybind11/tests/test_embed/test_interpreter.cpp
@@ -0,0 +1,269 @@
+#include <pybind11/embed.h>
+#include <catch.hpp>
+
+#include <thread>
+#include <fstream>
+#include <functional>
+
+namespace py = pybind11;
+using namespace py::literals;
+
+class Widget {
+public:
+ Widget(std::string message) : message(message) { }
+ virtual ~Widget() = default;
+
+ std::string the_message() const { return message; }
+ virtual int the_answer() const = 0;
+
+private:
+ std::string message;
+};
+
+class PyWidget final : public Widget {
+ using Widget::Widget;
+
+ int the_answer() const override { PYBIND11_OVERLOAD_PURE(int, Widget, the_answer); }
+};
+
+PYBIND11_EMBEDDED_MODULE(widget_module, m) {
+ py::class_<Widget, PyWidget>(m, "Widget")
+ .def(py::init<std::string>())
+ .def_property_readonly("the_message", &Widget::the_message);
+
+ m.def("add", [](int i, int j) { return i + j; });
+}
+
+PYBIND11_EMBEDDED_MODULE(throw_exception, ) {
+ throw std::runtime_error("C++ Error");
+}
+
+PYBIND11_EMBEDDED_MODULE(throw_error_already_set, ) {
+ auto d = py::dict();
+ d["missing"].cast<py::object>();
+}
+
+TEST_CASE("Pass classes and data between modules defined in C++ and Python") {
+ auto module = py::module::import("test_interpreter");
+ REQUIRE(py::hasattr(module, "DerivedWidget"));
+
+ auto locals = py::dict("hello"_a="Hello, World!", "x"_a=5, **module.attr("__dict__"));
+ py::exec(R"(
+ widget = DerivedWidget("{} - {}".format(hello, x))
+ message = widget.the_message
+ )", py::globals(), locals);
+ REQUIRE(locals["message"].cast<std::string>() == "Hello, World! - 5");
+
+ auto py_widget = module.attr("DerivedWidget")("The question");
+ auto message = py_widget.attr("the_message");
+ REQUIRE(message.cast<std::string>() == "The question");
+
+ const auto &cpp_widget = py_widget.cast<const Widget &>();
+ REQUIRE(cpp_widget.the_answer() == 42);
+}
+
+TEST_CASE("Import error handling") {
+ REQUIRE_NOTHROW(py::module::import("widget_module"));
+ REQUIRE_THROWS_WITH(py::module::import("throw_exception"),
+ "ImportError: C++ Error");
+ REQUIRE_THROWS_WITH(py::module::import("throw_error_already_set"),
+ Catch::Contains("ImportError: KeyError"));
+}
+
+TEST_CASE("There can be only one interpreter") {
+ static_assert(std::is_move_constructible<py::scoped_interpreter>::value, "");
+ static_assert(!std::is_move_assignable<py::scoped_interpreter>::value, "");
+ static_assert(!std::is_copy_constructible<py::scoped_interpreter>::value, "");
+ static_assert(!std::is_copy_assignable<py::scoped_interpreter>::value, "");
+
+ REQUIRE_THROWS_WITH(py::initialize_interpreter(), "The interpreter is already running");
+ REQUIRE_THROWS_WITH(py::scoped_interpreter(), "The interpreter is already running");
+
+ py::finalize_interpreter();
+ REQUIRE_NOTHROW(py::scoped_interpreter());
+ {
+ auto pyi1 = py::scoped_interpreter();
+ auto pyi2 = std::move(pyi1);
+ }
+ py::initialize_interpreter();
+}
+
+bool has_pybind11_internals_builtin() {
+ auto builtins = py::handle(PyEval_GetBuiltins());
+ return builtins.contains(PYBIND11_INTERNALS_ID);
+};
+
+bool has_pybind11_internals_static() {
+ return py::detail::get_internals_ptr() != nullptr;
+}
+
+TEST_CASE("Restart the interpreter") {
+ // Verify pre-restart state.
+ REQUIRE(py::module::import("widget_module").attr("add")(1, 2).cast<int>() == 3);
+ REQUIRE(has_pybind11_internals_builtin());
+ REQUIRE(has_pybind11_internals_static());
+
+ // Restart the interpreter.
+ py::finalize_interpreter();
+ REQUIRE(Py_IsInitialized() == 0);
+
+ py::initialize_interpreter();
+ REQUIRE(Py_IsInitialized() == 1);
+
+ // Internals are deleted after a restart.
+ REQUIRE_FALSE(has_pybind11_internals_builtin());
+ REQUIRE_FALSE(has_pybind11_internals_static());
+ pybind11::detail::get_internals();
+ REQUIRE(has_pybind11_internals_builtin());
+ REQUIRE(has_pybind11_internals_static());
+
+ // Make sure that an interpreter with no get_internals() created until finalize still gets the
+ // internals destroyed
+ py::finalize_interpreter();
+ py::initialize_interpreter();
+ bool ran = false;
+ py::module::import("__main__").attr("internals_destroy_test") =
+ py::capsule(&ran, [](void *ran) { py::detail::get_internals(); *static_cast<bool *>(ran) = true; });
+ REQUIRE_FALSE(has_pybind11_internals_builtin());
+ REQUIRE_FALSE(has_pybind11_internals_static());
+ REQUIRE_FALSE(ran);
+ py::finalize_interpreter();
+ REQUIRE(ran);
+ py::initialize_interpreter();
+ REQUIRE_FALSE(has_pybind11_internals_builtin());
+ REQUIRE_FALSE(has_pybind11_internals_static());
+
+ // C++ modules can be reloaded.
+ auto cpp_module = py::module::import("widget_module");
+ REQUIRE(cpp_module.attr("add")(1, 2).cast<int>() == 3);
+
+ // C++ type information is reloaded and can be used in python modules.
+ auto py_module = py::module::import("test_interpreter");
+ auto py_widget = py_module.attr("DerivedWidget")("Hello after restart");
+ REQUIRE(py_widget.attr("the_message").cast<std::string>() == "Hello after restart");
+}
+
+TEST_CASE("Subinterpreter") {
+ // Add tags to the modules in the main interpreter and test the basics.
+ py::module::import("__main__").attr("main_tag") = "main interpreter";
+ {
+ auto m = py::module::import("widget_module");
+ m.attr("extension_module_tag") = "added to module in main interpreter";
+
+ REQUIRE(m.attr("add")(1, 2).cast<int>() == 3);
+ }
+ REQUIRE(has_pybind11_internals_builtin());
+ REQUIRE(has_pybind11_internals_static());
+
+ /// Create and switch to a subinterpreter.
+ auto main_tstate = PyThreadState_Get();
+ auto sub_tstate = Py_NewInterpreter();
+
+ // Subinterpreters get their own copy of builtins. detail::get_internals() still
+ // works by returning from the static variable, i.e. all interpreters share a single
+ // global pybind11::internals;
+ REQUIRE_FALSE(has_pybind11_internals_builtin());
+ REQUIRE(has_pybind11_internals_static());
+
+ // Modules tags should be gone.
+ REQUIRE_FALSE(py::hasattr(py::module::import("__main__"), "tag"));
+ {
+ auto m = py::module::import("widget_module");
+ REQUIRE_FALSE(py::hasattr(m, "extension_module_tag"));
+
+ // Function bindings should still work.
+ REQUIRE(m.attr("add")(1, 2).cast<int>() == 3);
+ }
+
+ // Restore main interpreter.
+ Py_EndInterpreter(sub_tstate);
+ PyThreadState_Swap(main_tstate);
+
+ REQUIRE(py::hasattr(py::module::import("__main__"), "main_tag"));
+ REQUIRE(py::hasattr(py::module::import("widget_module"), "extension_module_tag"));
+}
+
+TEST_CASE("Execution frame") {
+ // When the interpreter is embedded, there is no execution frame, but `py::exec`
+ // should still function by using reasonable globals: `__main__.__dict__`.
+ py::exec("var = dict(number=42)");
+ REQUIRE(py::globals()["var"]["number"].cast<int>() == 42);
+}
+
+TEST_CASE("Threads") {
+ // Restart interpreter to ensure threads are not initialized
+ py::finalize_interpreter();
+ py::initialize_interpreter();
+ REQUIRE_FALSE(has_pybind11_internals_static());
+
+ constexpr auto num_threads = 10;
+ auto locals = py::dict("count"_a=0);
+
+ {
+ py::gil_scoped_release gil_release{};
+ REQUIRE(has_pybind11_internals_static());
+
+ auto threads = std::vector<std::thread>();
+ for (auto i = 0; i < num_threads; ++i) {
+ threads.emplace_back([&]() {
+ py::gil_scoped_acquire gil{};
+ locals["count"] = locals["count"].cast<int>() + 1;
+ });
+ }
+
+ for (auto &thread : threads) {
+ thread.join();
+ }
+ }
+
+ REQUIRE(locals["count"].cast<int>() == num_threads);
+}
+
+// Scope exit utility https://stackoverflow.com/a/36644501/7255855
+struct scope_exit {
+ std::function<void()> f_;
+ explicit scope_exit(std::function<void()> f) noexcept : f_(std::move(f)) {}
+ ~scope_exit() { if (f_) f_(); }
+};
+
+TEST_CASE("Reload module from file") {
+ // Disable generation of cached bytecode (.pyc files) for this test, otherwise
+ // Python might pick up an old version from the cache instead of the new versions
+ // of the .py files generated below
+ auto sys = py::module::import("sys");
+ bool dont_write_bytecode = sys.attr("dont_write_bytecode").cast<bool>();
+ sys.attr("dont_write_bytecode") = true;
+ // Reset the value at scope exit
+ scope_exit reset_dont_write_bytecode([&]() {
+ sys.attr("dont_write_bytecode") = dont_write_bytecode;
+ });
+
+ std::string module_name = "test_module_reload";
+ std::string module_file = module_name + ".py";
+
+ // Create the module .py file
+ std::ofstream test_module(module_file);
+ test_module << "def test():\n";
+ test_module << " return 1\n";
+ test_module.close();
+ // Delete the file at scope exit
+ scope_exit delete_module_file([&]() {
+ std::remove(module_file.c_str());
+ });
+
+ // Import the module from file
+ auto module = py::module::import(module_name.c_str());
+ int result = module.attr("test")().cast<int>();
+ REQUIRE(result == 1);
+
+ // Update the module .py file with a small change
+ test_module.open(module_file);
+ test_module << "def test():\n";
+ test_module << " return 2\n";
+ test_module.close();
+
+ // Reload the module
+ module.reload();
+ result = module.attr("test")().cast<int>();
+ REQUIRE(result == 2);
+}
diff --git a/ext/pybind11/tests/test_embed/test_interpreter.py b/ext/pybind11/tests/test_embed/test_interpreter.py
new file mode 100644
index 000000000..26a047921
--- /dev/null
+++ b/ext/pybind11/tests/test_embed/test_interpreter.py
@@ -0,0 +1,9 @@
+from widget_module import Widget
+
+
+class DerivedWidget(Widget):
+ def __init__(self, message):
+ super(DerivedWidget, self).__init__(message)
+
+ def the_answer(self):
+ return 42
diff --git a/ext/pybind11/tests/test_enum.cpp b/ext/pybind11/tests/test_enum.cpp
index 09f334cdb..49f31ba1f 100644
--- a/ext/pybind11/tests/test_enum.cpp
+++ b/ext/pybind11/tests/test_enum.cpp
@@ -9,60 +9,63 @@
#include "pybind11_tests.h"
-enum UnscopedEnum {
- EOne = 1,
- ETwo
-};
-
-enum class ScopedEnum {
- Two = 2,
- Three
-};
-
-enum Flags {
- Read = 4,
- Write = 2,
- Execute = 1
-};
-
-class ClassWithUnscopedEnum {
-public:
- enum EMode {
- EFirstMode = 1,
- ESecondMode
+TEST_SUBMODULE(enums, m) {
+ // test_unscoped_enum
+ enum UnscopedEnum {
+ EOne = 1,
+ ETwo
};
-
- static EMode test_function(EMode mode) {
- return mode;
- }
-};
-
-std::string test_scoped_enum(ScopedEnum z) {
- return "ScopedEnum::" + std::string(z == ScopedEnum::Two ? "Two" : "Three");
-}
-
-test_initializer enums([](py::module &m) {
- m.def("test_scoped_enum", &test_scoped_enum);
-
py::enum_<UnscopedEnum>(m, "UnscopedEnum", py::arithmetic())
.value("EOne", EOne)
.value("ETwo", ETwo)
.export_values();
+ // test_scoped_enum
+ enum class ScopedEnum {
+ Two = 2,
+ Three
+ };
py::enum_<ScopedEnum>(m, "ScopedEnum", py::arithmetic())
.value("Two", ScopedEnum::Two)
.value("Three", ScopedEnum::Three);
+ m.def("test_scoped_enum", [](ScopedEnum z) {
+ return "ScopedEnum::" + std::string(z == ScopedEnum::Two ? "Two" : "Three");
+ });
+
+ // test_binary_operators
+ enum Flags {
+ Read = 4,
+ Write = 2,
+ Execute = 1
+ };
py::enum_<Flags>(m, "Flags", py::arithmetic())
.value("Read", Flags::Read)
.value("Write", Flags::Write)
.value("Execute", Flags::Execute)
.export_values();
+ // test_implicit_conversion
+ class ClassWithUnscopedEnum {
+ public:
+ enum EMode {
+ EFirstMode = 1,
+ ESecondMode
+ };
+
+ static EMode test_function(EMode mode) {
+ return mode;
+ }
+ };
py::class_<ClassWithUnscopedEnum> exenum_class(m, "ClassWithUnscopedEnum");
exenum_class.def_static("test_function", &ClassWithUnscopedEnum::test_function);
py::enum_<ClassWithUnscopedEnum::EMode>(exenum_class, "EMode")
.value("EFirstMode", ClassWithUnscopedEnum::EFirstMode)
.value("ESecondMode", ClassWithUnscopedEnum::ESecondMode)
.export_values();
-});
+
+ // test_enum_to_int
+ m.def("test_enum_to_int", [](int) { });
+ m.def("test_enum_to_uint", [](uint32_t) { });
+ m.def("test_enum_to_long_long", [](long long) { });
+}
diff --git a/ext/pybind11/tests/test_enum.py b/ext/pybind11/tests/test_enum.py
index ba7e22ab0..d8eff5278 100644
--- a/ext/pybind11/tests/test_enum.py
+++ b/ext/pybind11/tests/test_enum.py
@@ -1,51 +1,50 @@
import pytest
+from pybind11_tests import enums as m
def test_unscoped_enum():
- from pybind11_tests import UnscopedEnum, EOne
-
- assert str(UnscopedEnum.EOne) == "UnscopedEnum.EOne"
- assert str(UnscopedEnum.ETwo) == "UnscopedEnum.ETwo"
- assert str(EOne) == "UnscopedEnum.EOne"
+ assert str(m.UnscopedEnum.EOne) == "UnscopedEnum.EOne"
+ assert str(m.UnscopedEnum.ETwo) == "UnscopedEnum.ETwo"
+ assert str(m.EOne) == "UnscopedEnum.EOne"
# __members__ property
- assert UnscopedEnum.__members__ == {"EOne": UnscopedEnum.EOne, "ETwo": UnscopedEnum.ETwo}
+ assert m.UnscopedEnum.__members__ == \
+ {"EOne": m.UnscopedEnum.EOne, "ETwo": m.UnscopedEnum.ETwo}
# __members__ readonly
with pytest.raises(AttributeError):
- UnscopedEnum.__members__ = {}
+ m.UnscopedEnum.__members__ = {}
# __members__ returns a copy
- foo = UnscopedEnum.__members__
+ foo = m.UnscopedEnum.__members__
foo["bar"] = "baz"
- assert UnscopedEnum.__members__ == {"EOne": UnscopedEnum.EOne, "ETwo": UnscopedEnum.ETwo}
+ assert m.UnscopedEnum.__members__ == \
+ {"EOne": m.UnscopedEnum.EOne, "ETwo": m.UnscopedEnum.ETwo}
# no TypeError exception for unscoped enum ==/!= int comparisons
- y = UnscopedEnum.ETwo
+ y = m.UnscopedEnum.ETwo
assert y == 2
assert y != 3
- assert int(UnscopedEnum.ETwo) == 2
- assert str(UnscopedEnum(2)) == "UnscopedEnum.ETwo"
+ assert int(m.UnscopedEnum.ETwo) == 2
+ assert str(m.UnscopedEnum(2)) == "UnscopedEnum.ETwo"
# order
- assert UnscopedEnum.EOne < UnscopedEnum.ETwo
- assert UnscopedEnum.EOne < 2
- assert UnscopedEnum.ETwo > UnscopedEnum.EOne
- assert UnscopedEnum.ETwo > 1
- assert UnscopedEnum.ETwo <= 2
- assert UnscopedEnum.ETwo >= 2
- assert UnscopedEnum.EOne <= UnscopedEnum.ETwo
- assert UnscopedEnum.EOne <= 2
- assert UnscopedEnum.ETwo >= UnscopedEnum.EOne
- assert UnscopedEnum.ETwo >= 1
- assert not (UnscopedEnum.ETwo < UnscopedEnum.EOne)
- assert not (2 < UnscopedEnum.EOne)
+ assert m.UnscopedEnum.EOne < m.UnscopedEnum.ETwo
+ assert m.UnscopedEnum.EOne < 2
+ assert m.UnscopedEnum.ETwo > m.UnscopedEnum.EOne
+ assert m.UnscopedEnum.ETwo > 1
+ assert m.UnscopedEnum.ETwo <= 2
+ assert m.UnscopedEnum.ETwo >= 2
+ assert m.UnscopedEnum.EOne <= m.UnscopedEnum.ETwo
+ assert m.UnscopedEnum.EOne <= 2
+ assert m.UnscopedEnum.ETwo >= m.UnscopedEnum.EOne
+ assert m.UnscopedEnum.ETwo >= 1
+ assert not (m.UnscopedEnum.ETwo < m.UnscopedEnum.EOne)
+ assert not (2 < m.UnscopedEnum.EOne)
def test_scoped_enum():
- from pybind11_tests import ScopedEnum, test_scoped_enum
-
- assert test_scoped_enum(ScopedEnum.Three) == "ScopedEnum::Three"
- z = ScopedEnum.Two
- assert test_scoped_enum(z) == "ScopedEnum::Two"
+ assert m.test_scoped_enum(m.ScopedEnum.Three) == "ScopedEnum::Three"
+ z = m.ScopedEnum.Two
+ assert m.test_scoped_enum(z) == "ScopedEnum::Two"
# expected TypeError exceptions for scoped enum ==/!= int comparisons
with pytest.raises(TypeError):
@@ -54,23 +53,21 @@ def test_scoped_enum():
assert z != 3
# order
- assert ScopedEnum.Two < ScopedEnum.Three
- assert ScopedEnum.Three > ScopedEnum.Two
- assert ScopedEnum.Two <= ScopedEnum.Three
- assert ScopedEnum.Two <= ScopedEnum.Two
- assert ScopedEnum.Two >= ScopedEnum.Two
- assert ScopedEnum.Three >= ScopedEnum.Two
+ assert m.ScopedEnum.Two < m.ScopedEnum.Three
+ assert m.ScopedEnum.Three > m.ScopedEnum.Two
+ assert m.ScopedEnum.Two <= m.ScopedEnum.Three
+ assert m.ScopedEnum.Two <= m.ScopedEnum.Two
+ assert m.ScopedEnum.Two >= m.ScopedEnum.Two
+ assert m.ScopedEnum.Three >= m.ScopedEnum.Two
def test_implicit_conversion():
- from pybind11_tests import ClassWithUnscopedEnum
-
- assert str(ClassWithUnscopedEnum.EMode.EFirstMode) == "EMode.EFirstMode"
- assert str(ClassWithUnscopedEnum.EFirstMode) == "EMode.EFirstMode"
+ assert str(m.ClassWithUnscopedEnum.EMode.EFirstMode) == "EMode.EFirstMode"
+ assert str(m.ClassWithUnscopedEnum.EFirstMode) == "EMode.EFirstMode"
- f = ClassWithUnscopedEnum.test_function
- first = ClassWithUnscopedEnum.EFirstMode
- second = ClassWithUnscopedEnum.ESecondMode
+ f = m.ClassWithUnscopedEnum.test_function
+ first = m.ClassWithUnscopedEnum.EFirstMode
+ second = m.ClassWithUnscopedEnum.ESecondMode
assert f(first) == 1
@@ -95,23 +92,30 @@ def test_implicit_conversion():
def test_binary_operators():
- from pybind11_tests import Flags
-
- assert int(Flags.Read) == 4
- assert int(Flags.Write) == 2
- assert int(Flags.Execute) == 1
- assert int(Flags.Read | Flags.Write | Flags.Execute) == 7
- assert int(Flags.Read | Flags.Write) == 6
- assert int(Flags.Read | Flags.Execute) == 5
- assert int(Flags.Write | Flags.Execute) == 3
- assert int(Flags.Write | 1) == 3
-
- state = Flags.Read | Flags.Write
- assert (state & Flags.Read) != 0
- assert (state & Flags.Write) != 0
- assert (state & Flags.Execute) == 0
+ assert int(m.Flags.Read) == 4
+ assert int(m.Flags.Write) == 2
+ assert int(m.Flags.Execute) == 1
+ assert int(m.Flags.Read | m.Flags.Write | m.Flags.Execute) == 7
+ assert int(m.Flags.Read | m.Flags.Write) == 6
+ assert int(m.Flags.Read | m.Flags.Execute) == 5
+ assert int(m.Flags.Write | m.Flags.Execute) == 3
+ assert int(m.Flags.Write | 1) == 3
+
+ state = m.Flags.Read | m.Flags.Write
+ assert (state & m.Flags.Read) != 0
+ assert (state & m.Flags.Write) != 0
+ assert (state & m.Flags.Execute) == 0
assert (state & 1) == 0
state2 = ~state
assert state2 == -7
assert int(state ^ state2) == -1
+
+
+def test_enum_to_int():
+ m.test_enum_to_int(m.Flags.Read)
+ m.test_enum_to_int(m.ClassWithUnscopedEnum.EMode.EFirstMode)
+ m.test_enum_to_uint(m.Flags.Read)
+ m.test_enum_to_uint(m.ClassWithUnscopedEnum.EMode.EFirstMode)
+ m.test_enum_to_long_long(m.Flags.Read)
+ m.test_enum_to_long_long(m.ClassWithUnscopedEnum.EMode.EFirstMode)
diff --git a/ext/pybind11/tests/test_eval.cpp b/ext/pybind11/tests/test_eval.cpp
index ed4c226fe..e09482191 100644
--- a/ext/pybind11/tests/test_eval.cpp
+++ b/ext/pybind11/tests/test_eval.cpp
@@ -11,7 +11,9 @@
#include <pybind11/eval.h>
#include "pybind11_tests.h"
-test_initializer eval([](py::module &m) {
+TEST_SUBMODULE(eval_, m) {
+ // test_evals
+
auto global = py::dict(py::module::import("__main__").attr("__dict__"));
m.def("test_eval_statements", [global]() {
@@ -20,14 +22,24 @@ test_initializer eval([](py::module &m) {
return 42;
});
- auto result = py::eval<py::eval_statements>(
- "print('Hello World!');\n"
- "x = call_test();",
+ // Regular string literal
+ py::exec(
+ "message = 'Hello World!'\n"
+ "x = call_test()",
global, local
);
+
+ // Multi-line raw string literal
+ py::exec(R"(
+ if x == 42:
+ print(message)
+ else:
+ raise RuntimeError
+ )", global, local
+ );
auto x = local["x"].cast<int>();
- return result == py::none() && x == 42;
+ return x == 42;
});
m.def("test_eval", [global]() {
@@ -45,7 +57,7 @@ test_initializer eval([](py::module &m) {
auto result = py::eval<py::eval_single_statement>("x = call_test()", py::dict(), local);
auto x = local["x"].cast<int>();
- return result == py::none() && x == 42;
+ return result.is_none() && x == 42;
});
m.def("test_eval_file", [global](py::str filename) {
@@ -56,7 +68,7 @@ test_initializer eval([](py::module &m) {
local["call_test2"] = py::cpp_function([&](int value) { val_out = value; });
auto result = py::eval_file(filename, global, local);
- return val_out == 43 && result == py::none();
+ return val_out == 43 && result.is_none();
});
m.def("test_eval_failure", []() {
@@ -76,4 +88,4 @@ test_initializer eval([](py::module &m) {
}
return false;
});
-});
+}
diff --git a/ext/pybind11/tests/test_eval.py b/ext/pybind11/tests/test_eval.py
index 8715dbadb..bda4ef6bf 100644
--- a/ext/pybind11/tests/test_eval.py
+++ b/ext/pybind11/tests/test_eval.py
@@ -1,19 +1,17 @@
import os
+from pybind11_tests import eval_ as m
def test_evals(capture):
- from pybind11_tests import (test_eval_statements, test_eval, test_eval_single_statement,
- test_eval_file, test_eval_failure, test_eval_file_failure)
-
with capture:
- assert test_eval_statements()
+ assert m.test_eval_statements()
assert capture == "Hello World!"
- assert test_eval()
- assert test_eval_single_statement()
+ assert m.test_eval()
+ assert m.test_eval_single_statement()
filename = os.path.join(os.path.dirname(__file__), "test_eval_call.py")
- assert test_eval_file(filename)
+ assert m.test_eval_file(filename)
- assert test_eval_failure()
- assert test_eval_file_failure()
+ assert m.test_eval_failure()
+ assert m.test_eval_file_failure()
diff --git a/ext/pybind11/tests/test_exceptions.cpp b/ext/pybind11/tests/test_exceptions.cpp
index 706b500f2..ae28abb48 100644
--- a/ext/pybind11/tests/test_exceptions.cpp
+++ b/ext/pybind11/tests/test_exceptions.cpp
@@ -58,34 +58,6 @@ class MyException5_1 : public MyException5 {
using MyException5::MyException5;
};
-void throws1() {
- throw MyException("this error should go to a custom type");
-}
-
-void throws2() {
- throw MyException2("this error should go to a standard Python exception");
-}
-
-void throws3() {
- throw MyException3("this error cannot be translated");
-}
-
-void throws4() {
- throw MyException4("this error is rethrown");
-}
-
-void throws5() {
- throw MyException5("this is a helper-defined translated exception");
-}
-
-void throws5_1() {
- throw MyException5_1("MyException5 subclass");
-}
-
-void throws_logic_error() {
- throw std::logic_error("this error should fall through to the standard handler");
-}
-
struct PythonCallInDestructor {
PythonCallInDestructor(const py::dict &d) : d(d) {}
~PythonCallInDestructor() { d["good"] = true; }
@@ -93,7 +65,11 @@ struct PythonCallInDestructor {
py::dict d;
};
-test_initializer custom_exceptions([](py::module &m) {
+TEST_SUBMODULE(exceptions, m) {
+ m.def("throw_std_exception", []() {
+ throw std::runtime_error("This exception was intentionally thrown.");
+ });
+
// make a new custom exception and use it as a translation target
static py::exception<MyException> ex(m, "MyException");
py::register_exception_translator([](std::exception_ptr p) {
@@ -133,13 +109,20 @@ test_initializer custom_exceptions([](py::module &m) {
// A slightly more complicated one that declares MyException5_1 as a subclass of MyException5
py::register_exception<MyException5_1>(m, "MyException5_1", ex5.ptr());
- m.def("throws1", &throws1);
- m.def("throws2", &throws2);
- m.def("throws3", &throws3);
- m.def("throws4", &throws4);
- m.def("throws5", &throws5);
- m.def("throws5_1", &throws5_1);
- m.def("throws_logic_error", &throws_logic_error);
+ m.def("throws1", []() { throw MyException("this error should go to a custom type"); });
+ m.def("throws2", []() { throw MyException2("this error should go to a standard Python exception"); });
+ m.def("throws3", []() { throw MyException3("this error cannot be translated"); });
+ m.def("throws4", []() { throw MyException4("this error is rethrown"); });
+ m.def("throws5", []() { throw MyException5("this is a helper-defined translated exception"); });
+ m.def("throws5_1", []() { throw MyException5_1("MyException5 subclass"); });
+ m.def("throws_logic_error", []() { throw std::logic_error("this error should fall through to the standard handler"); });
+ m.def("exception_matches", []() {
+ py::dict foo;
+ try { foo["bar"]; }
+ catch (py::error_already_set& ex) {
+ if (!ex.matches(PyExc_KeyError)) throw;
+ }
+ });
m.def("throw_already_set", [](bool err) {
if (err)
@@ -170,4 +153,16 @@ test_initializer custom_exceptions([](py::module &m) {
}
return false;
});
-});
+
+ // test_nested_throws
+ m.def("try_catch", [m](py::object exc_type, py::function f, py::args args) {
+ try { f(*args); }
+ catch (py::error_already_set &ex) {
+ if (ex.matches(exc_type))
+ py::print(ex.what());
+ else
+ throw;
+ }
+ });
+
+}
diff --git a/ext/pybind11/tests/test_exceptions.py b/ext/pybind11/tests/test_exceptions.py
index 0025e4eb6..8d37c09b8 100644
--- a/ext/pybind11/tests/test_exceptions.py
+++ b/ext/pybind11/tests/test_exceptions.py
@@ -1,74 +1,144 @@
import pytest
+from pybind11_tests import exceptions as m
+import pybind11_cross_module_tests as cm
-def test_error_already_set(msg):
- from pybind11_tests import throw_already_set
+def test_std_exception(msg):
with pytest.raises(RuntimeError) as excinfo:
- throw_already_set(False)
+ m.throw_std_exception()
+ assert msg(excinfo.value) == "This exception was intentionally thrown."
+
+
+def test_error_already_set(msg):
+ with pytest.raises(RuntimeError) as excinfo:
+ m.throw_already_set(False)
assert msg(excinfo.value) == "Unknown internal error occurred"
with pytest.raises(ValueError) as excinfo:
- throw_already_set(True)
+ m.throw_already_set(True)
assert msg(excinfo.value) == "foo"
-def test_python_call_in_catch():
- from pybind11_tests import python_call_in_destructor
+def test_cross_module_exceptions():
+ with pytest.raises(RuntimeError) as excinfo:
+ cm.raise_runtime_error()
+ assert str(excinfo.value) == "My runtime error"
+
+ with pytest.raises(ValueError) as excinfo:
+ cm.raise_value_error()
+ assert str(excinfo.value) == "My value error"
+
+ with pytest.raises(ValueError) as excinfo:
+ cm.throw_pybind_value_error()
+ assert str(excinfo.value) == "pybind11 value error"
+
+ with pytest.raises(TypeError) as excinfo:
+ cm.throw_pybind_type_error()
+ assert str(excinfo.value) == "pybind11 type error"
+
+ with pytest.raises(StopIteration) as excinfo:
+ cm.throw_stop_iteration()
+
+def test_python_call_in_catch():
d = {}
- assert python_call_in_destructor(d) is True
+ assert m.python_call_in_destructor(d) is True
assert d["good"] is True
-def test_custom(msg):
- from pybind11_tests import (MyException, MyException5, MyException5_1,
- throws1, throws2, throws3, throws4, throws5, throws5_1,
- throws_logic_error)
+def test_exception_matches():
+ m.exception_matches()
- # Can we catch a MyException?"
- with pytest.raises(MyException) as excinfo:
- throws1()
+
+def test_custom(msg):
+ # Can we catch a MyException?
+ with pytest.raises(m.MyException) as excinfo:
+ m.throws1()
assert msg(excinfo.value) == "this error should go to a custom type"
# Can we translate to standard Python exceptions?
with pytest.raises(RuntimeError) as excinfo:
- throws2()
+ m.throws2()
assert msg(excinfo.value) == "this error should go to a standard Python exception"
# Can we handle unknown exceptions?
with pytest.raises(RuntimeError) as excinfo:
- throws3()
+ m.throws3()
assert msg(excinfo.value) == "Caught an unknown exception!"
# Can we delegate to another handler by rethrowing?
- with pytest.raises(MyException) as excinfo:
- throws4()
+ with pytest.raises(m.MyException) as excinfo:
+ m.throws4()
assert msg(excinfo.value) == "this error is rethrown"
- # "Can we fall-through to the default handler?"
+ # Can we fall-through to the default handler?
with pytest.raises(RuntimeError) as excinfo:
- throws_logic_error()
+ m.throws_logic_error()
assert msg(excinfo.value) == "this error should fall through to the standard handler"
# Can we handle a helper-declared exception?
- with pytest.raises(MyException5) as excinfo:
- throws5()
+ with pytest.raises(m.MyException5) as excinfo:
+ m.throws5()
assert msg(excinfo.value) == "this is a helper-defined translated exception"
# Exception subclassing:
- with pytest.raises(MyException5) as excinfo:
- throws5_1()
+ with pytest.raises(m.MyException5) as excinfo:
+ m.throws5_1()
assert msg(excinfo.value) == "MyException5 subclass"
- assert isinstance(excinfo.value, MyException5_1)
+ assert isinstance(excinfo.value, m.MyException5_1)
- with pytest.raises(MyException5_1) as excinfo:
- throws5_1()
+ with pytest.raises(m.MyException5_1) as excinfo:
+ m.throws5_1()
assert msg(excinfo.value) == "MyException5 subclass"
- with pytest.raises(MyException5) as excinfo:
+ with pytest.raises(m.MyException5) as excinfo:
try:
- throws5()
- except MyException5_1:
+ m.throws5()
+ except m.MyException5_1:
raise RuntimeError("Exception error: caught child from parent")
assert msg(excinfo.value) == "this is a helper-defined translated exception"
+
+
+def test_nested_throws(capture):
+ """Tests nested (e.g. C++ -> Python -> C++) exception handling"""
+
+ def throw_myex():
+ raise m.MyException("nested error")
+
+ def throw_myex5():
+ raise m.MyException5("nested error 5")
+
+ # In the comments below, the exception is caught in the first step, thrown in the last step
+
+ # C++ -> Python
+ with capture:
+ m.try_catch(m.MyException5, throw_myex5)
+ assert str(capture).startswith("MyException5: nested error 5")
+
+ # Python -> C++ -> Python
+ with pytest.raises(m.MyException) as excinfo:
+ m.try_catch(m.MyException5, throw_myex)
+ assert str(excinfo.value) == "nested error"
+
+ def pycatch(exctype, f, *args):
+ try:
+ f(*args)
+ except m.MyException as e:
+ print(e)
+
+ # C++ -> Python -> C++ -> Python
+ with capture:
+ m.try_catch(
+ m.MyException5, pycatch, m.MyException, m.try_catch, m.MyException, throw_myex5)
+ assert str(capture).startswith("MyException5: nested error 5")
+
+ # C++ -> Python -> C++
+ with capture:
+ m.try_catch(m.MyException, pycatch, m.MyException5, m.throws4)
+ assert capture == "this error is rethrown"
+
+ # Python -> C++ -> Python -> C++
+ with pytest.raises(m.MyException5) as excinfo:
+ m.try_catch(m.MyException, pycatch, m.MyException, m.throws5)
+ assert str(excinfo.value) == "this is a helper-defined translated exception"
diff --git a/ext/pybind11/tests/test_factory_constructors.cpp b/ext/pybind11/tests/test_factory_constructors.cpp
new file mode 100644
index 000000000..fb33377b2
--- /dev/null
+++ b/ext/pybind11/tests/test_factory_constructors.cpp
@@ -0,0 +1,337 @@
+/*
+ tests/test_factory_constructors.cpp -- tests construction from a factory function
+ via py::init_factory()
+
+ Copyright (c) 2017 Jason Rhinelander <jason@imaginary.ca>
+
+ All rights reserved. Use of this source code is governed by a
+ BSD-style license that can be found in the LICENSE file.
+*/
+
+#include "pybind11_tests.h"
+#include "constructor_stats.h"
+#include <cmath>
+
+// Classes for testing python construction via C++ factory function:
+// Not publically constructible, copyable, or movable:
+class TestFactory1 {
+ friend class TestFactoryHelper;
+ TestFactory1() : value("(empty)") { print_default_created(this); }
+ TestFactory1(int v) : value(std::to_string(v)) { print_created(this, value); }
+ TestFactory1(std::string v) : value(std::move(v)) { print_created(this, value); }
+ TestFactory1(TestFactory1 &&) = delete;
+ TestFactory1(const TestFactory1 &) = delete;
+ TestFactory1 &operator=(TestFactory1 &&) = delete;
+ TestFactory1 &operator=(const TestFactory1 &) = delete;
+public:
+ std::string value;
+ ~TestFactory1() { print_destroyed(this); }
+};
+// Non-public construction, but moveable:
+class TestFactory2 {
+ friend class TestFactoryHelper;
+ TestFactory2() : value("(empty2)") { print_default_created(this); }
+ TestFactory2(int v) : value(std::to_string(v)) { print_created(this, value); }
+ TestFactory2(std::string v) : value(std::move(v)) { print_created(this, value); }
+public:
+ TestFactory2(TestFactory2 &&m) { value = std::move(m.value); print_move_created(this); }
+ TestFactory2 &operator=(TestFactory2 &&m) { value = std::move(m.value); print_move_assigned(this); return *this; }
+ std::string value;
+ ~TestFactory2() { print_destroyed(this); }
+};
+// Mixed direct/factory construction:
+class TestFactory3 {
+protected:
+ friend class TestFactoryHelper;
+ TestFactory3() : value("(empty3)") { print_default_created(this); }
+ TestFactory3(int v) : value(std::to_string(v)) { print_created(this, value); }
+public:
+ TestFactory3(std::string v) : value(std::move(v)) { print_created(this, value); }
+ TestFactory3(TestFactory3 &&m) { value = std::move(m.value); print_move_created(this); }
+ TestFactory3 &operator=(TestFactory3 &&m) { value = std::move(m.value); print_move_assigned(this); return *this; }
+ std::string value;
+ virtual ~TestFactory3() { print_destroyed(this); }
+};
+// Inheritance test
+class TestFactory4 : public TestFactory3 {
+public:
+ TestFactory4() : TestFactory3() { print_default_created(this); }
+ TestFactory4(int v) : TestFactory3(v) { print_created(this, v); }
+ virtual ~TestFactory4() { print_destroyed(this); }
+};
+// Another class for an invalid downcast test
+class TestFactory5 : public TestFactory3 {
+public:
+ TestFactory5(int i) : TestFactory3(i) { print_created(this, i); }
+ virtual ~TestFactory5() { print_destroyed(this); }
+};
+
+class TestFactory6 {
+protected:
+ int value;
+ bool alias = false;
+public:
+ TestFactory6(int i) : value{i} { print_created(this, i); }
+ TestFactory6(TestFactory6 &&f) { print_move_created(this); value = f.value; alias = f.alias; }
+ TestFactory6(const TestFactory6 &f) { print_copy_created(this); value = f.value; alias = f.alias; }
+ virtual ~TestFactory6() { print_destroyed(this); }
+ virtual int get() { return value; }
+ bool has_alias() { return alias; }
+};
+class PyTF6 : public TestFactory6 {
+public:
+ // Special constructor that allows the factory to construct a PyTF6 from a TestFactory6 only
+ // when an alias is needed:
+ PyTF6(TestFactory6 &&base) : TestFactory6(std::move(base)) { alias = true; print_created(this, "move", value); }
+ PyTF6(int i) : TestFactory6(i) { alias = true; print_created(this, i); }
+ PyTF6(PyTF6 &&f) : TestFactory6(std::move(f)) { print_move_created(this); }
+ PyTF6(const PyTF6 &f) : TestFactory6(f) { print_copy_created(this); }
+ PyTF6(std::string s) : TestFactory6((int) s.size()) { alias = true; print_created(this, s); }
+ virtual ~PyTF6() { print_destroyed(this); }
+ int get() override { PYBIND11_OVERLOAD(int, TestFactory6, get, /*no args*/); }
+};
+
+class TestFactory7 {
+protected:
+ int value;
+ bool alias = false;
+public:
+ TestFactory7(int i) : value{i} { print_created(this, i); }
+ TestFactory7(TestFactory7 &&f) { print_move_created(this); value = f.value; alias = f.alias; }
+ TestFactory7(const TestFactory7 &f) { print_copy_created(this); value = f.value; alias = f.alias; }
+ virtual ~TestFactory7() { print_destroyed(this); }
+ virtual int get() { return value; }
+ bool has_alias() { return alias; }
+};
+class PyTF7 : public TestFactory7 {
+public:
+ PyTF7(int i) : TestFactory7(i) { alias = true; print_created(this, i); }
+ PyTF7(PyTF7 &&f) : TestFactory7(std::move(f)) { print_move_created(this); }
+ PyTF7(const PyTF7 &f) : TestFactory7(f) { print_copy_created(this); }
+ virtual ~PyTF7() { print_destroyed(this); }
+ int get() override { PYBIND11_OVERLOAD(int, TestFactory7, get, /*no args*/); }
+};
+
+
+class TestFactoryHelper {
+public:
+ // Non-movable, non-copyable type:
+ // Return via pointer:
+ static TestFactory1 *construct1() { return new TestFactory1(); }
+ // Holder:
+ static std::unique_ptr<TestFactory1> construct1(int a) { return std::unique_ptr<TestFactory1>(new TestFactory1(a)); }
+ // pointer again
+ static TestFactory1 *construct1_string(std::string a) { return new TestFactory1(a); }
+
+ // Moveable type:
+ // pointer:
+ static TestFactory2 *construct2() { return new TestFactory2(); }
+ // holder:
+ static std::unique_ptr<TestFactory2> construct2(int a) { return std::unique_ptr<TestFactory2>(new TestFactory2(a)); }
+ // by value moving:
+ static TestFactory2 construct2(std::string a) { return TestFactory2(a); }
+
+ // shared_ptr holder type:
+ // pointer:
+ static TestFactory3 *construct3() { return new TestFactory3(); }
+ // holder:
+ static std::shared_ptr<TestFactory3> construct3(int a) { return std::shared_ptr<TestFactory3>(new TestFactory3(a)); }
+};
+
+TEST_SUBMODULE(factory_constructors, m) {
+
+ // Define various trivial types to allow simpler overload resolution:
+ py::module m_tag = m.def_submodule("tag");
+#define MAKE_TAG_TYPE(Name) \
+ struct Name##_tag {}; \
+ py::class_<Name##_tag>(m_tag, #Name "_tag").def(py::init<>()); \
+ m_tag.attr(#Name) = py::cast(Name##_tag{})
+ MAKE_TAG_TYPE(pointer);
+ MAKE_TAG_TYPE(unique_ptr);
+ MAKE_TAG_TYPE(move);
+ MAKE_TAG_TYPE(shared_ptr);
+ MAKE_TAG_TYPE(derived);
+ MAKE_TAG_TYPE(TF4);
+ MAKE_TAG_TYPE(TF5);
+ MAKE_TAG_TYPE(null_ptr);
+ MAKE_TAG_TYPE(base);
+ MAKE_TAG_TYPE(invalid_base);
+ MAKE_TAG_TYPE(alias);
+ MAKE_TAG_TYPE(unaliasable);
+ MAKE_TAG_TYPE(mixed);
+
+ // test_init_factory_basic, test_bad_type
+ py::class_<TestFactory1>(m, "TestFactory1")
+ .def(py::init([](unique_ptr_tag, int v) { return TestFactoryHelper::construct1(v); }))
+ .def(py::init(&TestFactoryHelper::construct1_string)) // raw function pointer
+ .def(py::init([](pointer_tag) { return TestFactoryHelper::construct1(); }))
+ .def(py::init([](py::handle, int v, py::handle) { return TestFactoryHelper::construct1(v); }))
+ .def_readwrite("value", &TestFactory1::value)
+ ;
+ py::class_<TestFactory2>(m, "TestFactory2")
+ .def(py::init([](pointer_tag, int v) { return TestFactoryHelper::construct2(v); }))
+ .def(py::init([](unique_ptr_tag, std::string v) { return TestFactoryHelper::construct2(v); }))
+ .def(py::init([](move_tag) { return TestFactoryHelper::construct2(); }))
+ .def_readwrite("value", &TestFactory2::value)
+ ;
+
+ // Stateful & reused:
+ int c = 1;
+ auto c4a = [c](pointer_tag, TF4_tag, int a) { (void) c; return new TestFactory4(a);};
+
+ // test_init_factory_basic, test_init_factory_casting
+ py::class_<TestFactory3, std::shared_ptr<TestFactory3>>(m, "TestFactory3")
+ .def(py::init([](pointer_tag, int v) { return TestFactoryHelper::construct3(v); }))
+ .def(py::init([](shared_ptr_tag) { return TestFactoryHelper::construct3(); }))
+ .def("__init__", [](TestFactory3 &self, std::string v) { new (&self) TestFactory3(v); }) // placement-new ctor
+
+ // factories returning a derived type:
+ .def(py::init(c4a)) // derived ptr
+ .def(py::init([](pointer_tag, TF5_tag, int a) { return new TestFactory5(a); }))
+ // derived shared ptr:
+ .def(py::init([](shared_ptr_tag, TF4_tag, int a) { return std::make_shared<TestFactory4>(a); }))
+ .def(py::init([](shared_ptr_tag, TF5_tag, int a) { return std::make_shared<TestFactory5>(a); }))
+
+ // Returns nullptr:
+ .def(py::init([](null_ptr_tag) { return (TestFactory3 *) nullptr; }))
+
+ .def_readwrite("value", &TestFactory3::value)
+ ;
+
+ // test_init_factory_casting
+ py::class_<TestFactory4, TestFactory3, std::shared_ptr<TestFactory4>>(m, "TestFactory4")
+ .def(py::init(c4a)) // pointer
+ ;
+
+ // Doesn't need to be registered, but registering makes getting ConstructorStats easier:
+ py::class_<TestFactory5, TestFactory3, std::shared_ptr<TestFactory5>>(m, "TestFactory5");
+
+ // test_init_factory_alias
+ // Alias testing
+ py::class_<TestFactory6, PyTF6>(m, "TestFactory6")
+ .def(py::init([](base_tag, int i) { return TestFactory6(i); }))
+ .def(py::init([](alias_tag, int i) { return PyTF6(i); }))
+ .def(py::init([](alias_tag, std::string s) { return PyTF6(s); }))
+ .def(py::init([](alias_tag, pointer_tag, int i) { return new PyTF6(i); }))
+ .def(py::init([](base_tag, pointer_tag, int i) { return new TestFactory6(i); }))
+ .def(py::init([](base_tag, alias_tag, pointer_tag, int i) { return (TestFactory6 *) new PyTF6(i); }))
+
+ .def("get", &TestFactory6::get)
+ .def("has_alias", &TestFactory6::has_alias)
+
+ .def_static("get_cstats", &ConstructorStats::get<TestFactory6>, py::return_value_policy::reference)
+ .def_static("get_alias_cstats", &ConstructorStats::get<PyTF6>, py::return_value_policy::reference)
+ ;
+
+ // test_init_factory_dual
+ // Separate alias constructor testing
+ py::class_<TestFactory7, PyTF7, std::shared_ptr<TestFactory7>>(m, "TestFactory7")
+ .def(py::init(
+ [](int i) { return TestFactory7(i); },
+ [](int i) { return PyTF7(i); }))
+ .def(py::init(
+ [](pointer_tag, int i) { return new TestFactory7(i); },
+ [](pointer_tag, int i) { return new PyTF7(i); }))
+ .def(py::init(
+ [](mixed_tag, int i) { return new TestFactory7(i); },
+ [](mixed_tag, int i) { return PyTF7(i); }))
+ .def(py::init(
+ [](mixed_tag, std::string s) { return TestFactory7((int) s.size()); },
+ [](mixed_tag, std::string s) { return new PyTF7((int) s.size()); }))
+ .def(py::init(
+ [](base_tag, pointer_tag, int i) { return new TestFactory7(i); },
+ [](base_tag, pointer_tag, int i) { return (TestFactory7 *) new PyTF7(i); }))
+ .def(py::init(
+ [](alias_tag, pointer_tag, int i) { return new PyTF7(i); },
+ [](alias_tag, pointer_tag, int i) { return new PyTF7(10*i); }))
+ .def(py::init(
+ [](shared_ptr_tag, base_tag, int i) { return std::make_shared<TestFactory7>(i); },
+ [](shared_ptr_tag, base_tag, int i) { auto *p = new PyTF7(i); return std::shared_ptr<TestFactory7>(p); }))
+ .def(py::init(
+ [](shared_ptr_tag, invalid_base_tag, int i) { return std::make_shared<TestFactory7>(i); },
+ [](shared_ptr_tag, invalid_base_tag, int i) { return std::make_shared<TestFactory7>(i); })) // <-- invalid alias factory
+
+ .def("get", &TestFactory7::get)
+ .def("has_alias", &TestFactory7::has_alias)
+
+ .def_static("get_cstats", &ConstructorStats::get<TestFactory7>, py::return_value_policy::reference)
+ .def_static("get_alias_cstats", &ConstructorStats::get<PyTF7>, py::return_value_policy::reference)
+ ;
+
+ // test_placement_new_alternative
+ // Class with a custom new operator but *without* a placement new operator (issue #948)
+ class NoPlacementNew {
+ public:
+ NoPlacementNew(int i) : i(i) { }
+ static void *operator new(std::size_t s) {
+ auto *p = ::operator new(s);
+ py::print("operator new called, returning", reinterpret_cast<uintptr_t>(p));
+ return p;
+ }
+ static void operator delete(void *p) {
+ py::print("operator delete called on", reinterpret_cast<uintptr_t>(p));
+ ::operator delete(p);
+ }
+ int i;
+ };
+ // As of 2.2, `py::init<args>` no longer requires placement new
+ py::class_<NoPlacementNew>(m, "NoPlacementNew")
+ .def(py::init<int>())
+ .def(py::init([]() { return new NoPlacementNew(100); }))
+ .def_readwrite("i", &NoPlacementNew::i)
+ ;
+
+
+ // test_reallocations
+ // Class that has verbose operator_new/operator_delete calls
+ struct NoisyAlloc {
+ NoisyAlloc(int i) { py::print(py::str("NoisyAlloc(int {})").format(i)); }
+ NoisyAlloc(double d) { py::print(py::str("NoisyAlloc(double {})").format(d)); }
+ ~NoisyAlloc() { py::print("~NoisyAlloc()"); }
+
+ static void *operator new(size_t s) { py::print("noisy new"); return ::operator new(s); }
+ static void *operator new(size_t, void *p) { py::print("noisy placement new"); return p; }
+ static void operator delete(void *p, size_t) { py::print("noisy delete"); ::operator delete(p); }
+ static void operator delete(void *, void *) { py::print("noisy placement delete"); }
+#if defined(_MSC_VER) && _MSC_VER < 1910
+ // MSVC 2015 bug: the above "noisy delete" isn't invoked (fixed in MSVC 2017)
+ static void operator delete(void *p) { py::print("noisy delete"); ::operator delete(p); }
+#endif
+ };
+ py::class_<NoisyAlloc>(m, "NoisyAlloc")
+ // Since these overloads have the same number of arguments, the dispatcher will try each of
+ // them until the arguments convert. Thus we can get a pre-allocation here when passing a
+ // single non-integer:
+ .def("__init__", [](NoisyAlloc *a, int i) { new (a) NoisyAlloc(i); }) // Regular constructor, runs first, requires preallocation
+ .def(py::init([](double d) { return new NoisyAlloc(d); }))
+
+ // The two-argument version: first the factory pointer overload.
+ .def(py::init([](int i, int) { return new NoisyAlloc(i); }))
+ // Return-by-value:
+ .def(py::init([](double d, int) { return NoisyAlloc(d); }))
+ // Old-style placement new init; requires preallocation
+ .def("__init__", [](NoisyAlloc &a, double d, double) { new (&a) NoisyAlloc(d); })
+ // Requires deallocation of previous overload preallocated value:
+ .def(py::init([](int i, double) { return new NoisyAlloc(i); }))
+ // Regular again: requires yet another preallocation
+ .def("__init__", [](NoisyAlloc &a, int i, std::string) { new (&a) NoisyAlloc(i); })
+ ;
+
+
+
+
+ // static_assert testing (the following def's should all fail with appropriate compilation errors):
+#if 0
+ struct BadF1Base {};
+ struct BadF1 : BadF1Base {};
+ struct PyBadF1 : BadF1 {};
+ py::class_<BadF1, PyBadF1, std::shared_ptr<BadF1>> bf1(m, "BadF1");
+ // wrapped factory function must return a compatible pointer, holder, or value
+ bf1.def(py::init([]() { return 3; }));
+ // incompatible factory function pointer return type
+ bf1.def(py::init([]() { static int three = 3; return &three; }));
+ // incompatible factory function std::shared_ptr<T> return type: cannot convert shared_ptr<T> to holder
+ // (non-polymorphic base)
+ bf1.def(py::init([]() { return std::shared_ptr<BadF1Base>(new BadF1()); }));
+#endif
+}
diff --git a/ext/pybind11/tests/test_factory_constructors.py b/ext/pybind11/tests/test_factory_constructors.py
new file mode 100644
index 000000000..78a3910ad
--- /dev/null
+++ b/ext/pybind11/tests/test_factory_constructors.py
@@ -0,0 +1,459 @@
+import pytest
+import re
+
+from pybind11_tests import factory_constructors as m
+from pybind11_tests.factory_constructors import tag
+from pybind11_tests import ConstructorStats
+
+
+def test_init_factory_basic():
+ """Tests py::init_factory() wrapper around various ways of returning the object"""
+
+ cstats = [ConstructorStats.get(c) for c in [m.TestFactory1, m.TestFactory2, m.TestFactory3]]
+ cstats[0].alive() # force gc
+ n_inst = ConstructorStats.detail_reg_inst()
+
+ x1 = m.TestFactory1(tag.unique_ptr, 3)
+ assert x1.value == "3"
+ y1 = m.TestFactory1(tag.pointer)
+ assert y1.value == "(empty)"
+ z1 = m.TestFactory1("hi!")
+ assert z1.value == "hi!"
+
+ assert ConstructorStats.detail_reg_inst() == n_inst + 3
+
+ x2 = m.TestFactory2(tag.move)
+ assert x2.value == "(empty2)"
+ y2 = m.TestFactory2(tag.pointer, 7)
+ assert y2.value == "7"
+ z2 = m.TestFactory2(tag.unique_ptr, "hi again")
+ assert z2.value == "hi again"
+
+ assert ConstructorStats.detail_reg_inst() == n_inst + 6
+
+ x3 = m.TestFactory3(tag.shared_ptr)
+ assert x3.value == "(empty3)"
+ y3 = m.TestFactory3(tag.pointer, 42)
+ assert y3.value == "42"
+ z3 = m.TestFactory3("bye")
+ assert z3.value == "bye"
+
+ with pytest.raises(TypeError) as excinfo:
+ m.TestFactory3(tag.null_ptr)
+ assert str(excinfo.value) == "pybind11::init(): factory function returned nullptr"
+
+ assert [i.alive() for i in cstats] == [3, 3, 3]
+ assert ConstructorStats.detail_reg_inst() == n_inst + 9
+
+ del x1, y2, y3, z3
+ assert [i.alive() for i in cstats] == [2, 2, 1]
+ assert ConstructorStats.detail_reg_inst() == n_inst + 5
+ del x2, x3, y1, z1, z2
+ assert [i.alive() for i in cstats] == [0, 0, 0]
+ assert ConstructorStats.detail_reg_inst() == n_inst
+
+ assert [i.values() for i in cstats] == [
+ ["3", "hi!"],
+ ["7", "hi again"],
+ ["42", "bye"]
+ ]
+ assert [i.default_constructions for i in cstats] == [1, 1, 1]
+
+
+def test_init_factory_signature(msg):
+ with pytest.raises(TypeError) as excinfo:
+ m.TestFactory1("invalid", "constructor", "arguments")
+ assert msg(excinfo.value) == """
+ __init__(): incompatible constructor arguments. The following argument types are supported:
+ 1. m.factory_constructors.TestFactory1(arg0: m.factory_constructors.tag.unique_ptr_tag, arg1: int)
+ 2. m.factory_constructors.TestFactory1(arg0: str)
+ 3. m.factory_constructors.TestFactory1(arg0: m.factory_constructors.tag.pointer_tag)
+ 4. m.factory_constructors.TestFactory1(arg0: handle, arg1: int, arg2: handle)
+
+ Invoked with: 'invalid', 'constructor', 'arguments'
+ """ # noqa: E501 line too long
+
+ assert msg(m.TestFactory1.__init__.__doc__) == """
+ __init__(*args, **kwargs)
+ Overloaded function.
+
+ 1. __init__(self: m.factory_constructors.TestFactory1, arg0: m.factory_constructors.tag.unique_ptr_tag, arg1: int) -> None
+
+ 2. __init__(self: m.factory_constructors.TestFactory1, arg0: str) -> None
+
+ 3. __init__(self: m.factory_constructors.TestFactory1, arg0: m.factory_constructors.tag.pointer_tag) -> None
+
+ 4. __init__(self: m.factory_constructors.TestFactory1, arg0: handle, arg1: int, arg2: handle) -> None
+ """ # noqa: E501 line too long
+
+
+def test_init_factory_casting():
+ """Tests py::init_factory() wrapper with various upcasting and downcasting returns"""
+
+ cstats = [ConstructorStats.get(c) for c in [m.TestFactory3, m.TestFactory4, m.TestFactory5]]
+ cstats[0].alive() # force gc
+ n_inst = ConstructorStats.detail_reg_inst()
+
+ # Construction from derived references:
+ a = m.TestFactory3(tag.pointer, tag.TF4, 4)
+ assert a.value == "4"
+ b = m.TestFactory3(tag.shared_ptr, tag.TF4, 5)
+ assert b.value == "5"
+ c = m.TestFactory3(tag.pointer, tag.TF5, 6)
+ assert c.value == "6"
+ d = m.TestFactory3(tag.shared_ptr, tag.TF5, 7)
+ assert d.value == "7"
+
+ assert ConstructorStats.detail_reg_inst() == n_inst + 4
+
+ # Shared a lambda with TF3:
+ e = m.TestFactory4(tag.pointer, tag.TF4, 8)
+ assert e.value == "8"
+
+ assert ConstructorStats.detail_reg_inst() == n_inst + 5
+ assert [i.alive() for i in cstats] == [5, 3, 2]
+
+ del a
+ assert [i.alive() for i in cstats] == [4, 2, 2]
+ assert ConstructorStats.detail_reg_inst() == n_inst + 4
+
+ del b, c, e
+ assert [i.alive() for i in cstats] == [1, 0, 1]
+ assert ConstructorStats.detail_reg_inst() == n_inst + 1
+
+ del d
+ assert [i.alive() for i in cstats] == [0, 0, 0]
+ assert ConstructorStats.detail_reg_inst() == n_inst
+
+ assert [i.values() for i in cstats] == [
+ ["4", "5", "6", "7", "8"],
+ ["4", "5", "8"],
+ ["6", "7"]
+ ]
+
+
+def test_init_factory_alias():
+ """Tests py::init_factory() wrapper with value conversions and alias types"""
+
+ cstats = [m.TestFactory6.get_cstats(), m.TestFactory6.get_alias_cstats()]
+ cstats[0].alive() # force gc
+ n_inst = ConstructorStats.detail_reg_inst()
+
+ a = m.TestFactory6(tag.base, 1)
+ assert a.get() == 1
+ assert not a.has_alias()
+ b = m.TestFactory6(tag.alias, "hi there")
+ assert b.get() == 8
+ assert b.has_alias()
+ c = m.TestFactory6(tag.alias, 3)
+ assert c.get() == 3
+ assert c.has_alias()
+ d = m.TestFactory6(tag.alias, tag.pointer, 4)
+ assert d.get() == 4
+ assert d.has_alias()
+ e = m.TestFactory6(tag.base, tag.pointer, 5)
+ assert e.get() == 5
+ assert not e.has_alias()
+ f = m.TestFactory6(tag.base, tag.alias, tag.pointer, 6)
+ assert f.get() == 6
+ assert f.has_alias()
+
+ assert ConstructorStats.detail_reg_inst() == n_inst + 6
+ assert [i.alive() for i in cstats] == [6, 4]
+
+ del a, b, e
+ assert [i.alive() for i in cstats] == [3, 3]
+ assert ConstructorStats.detail_reg_inst() == n_inst + 3
+ del f, c, d
+ assert [i.alive() for i in cstats] == [0, 0]
+ assert ConstructorStats.detail_reg_inst() == n_inst
+
+ class MyTest(m.TestFactory6):
+ def __init__(self, *args):
+ m.TestFactory6.__init__(self, *args)
+
+ def get(self):
+ return -5 + m.TestFactory6.get(self)
+
+ # Return Class by value, moved into new alias:
+ z = MyTest(tag.base, 123)
+ assert z.get() == 118
+ assert z.has_alias()
+
+ # Return alias by value, moved into new alias:
+ y = MyTest(tag.alias, "why hello!")
+ assert y.get() == 5
+ assert y.has_alias()
+
+ # Return Class by pointer, moved into new alias then original destroyed:
+ x = MyTest(tag.base, tag.pointer, 47)
+ assert x.get() == 42
+ assert x.has_alias()
+
+ assert ConstructorStats.detail_reg_inst() == n_inst + 3
+ assert [i.alive() for i in cstats] == [3, 3]
+ del x, y, z
+ assert [i.alive() for i in cstats] == [0, 0]
+ assert ConstructorStats.detail_reg_inst() == n_inst
+
+ assert [i.values() for i in cstats] == [
+ ["1", "8", "3", "4", "5", "6", "123", "10", "47"],
+ ["hi there", "3", "4", "6", "move", "123", "why hello!", "move", "47"]
+ ]
+
+
+def test_init_factory_dual():
+ """Tests init factory functions with dual main/alias factory functions"""
+ from pybind11_tests.factory_constructors import TestFactory7
+
+ cstats = [TestFactory7.get_cstats(), TestFactory7.get_alias_cstats()]
+ cstats[0].alive() # force gc
+ n_inst = ConstructorStats.detail_reg_inst()
+
+ class PythFactory7(TestFactory7):
+ def get(self):
+ return 100 + TestFactory7.get(self)
+
+ a1 = TestFactory7(1)
+ a2 = PythFactory7(2)
+ assert a1.get() == 1
+ assert a2.get() == 102
+ assert not a1.has_alias()
+ assert a2.has_alias()
+
+ b1 = TestFactory7(tag.pointer, 3)
+ b2 = PythFactory7(tag.pointer, 4)
+ assert b1.get() == 3
+ assert b2.get() == 104
+ assert not b1.has_alias()
+ assert b2.has_alias()
+
+ c1 = TestFactory7(tag.mixed, 5)
+ c2 = PythFactory7(tag.mixed, 6)
+ assert c1.get() == 5
+ assert c2.get() == 106
+ assert not c1.has_alias()
+ assert c2.has_alias()
+
+ d1 = TestFactory7(tag.base, tag.pointer, 7)
+ d2 = PythFactory7(tag.base, tag.pointer, 8)
+ assert d1.get() == 7
+ assert d2.get() == 108
+ assert not d1.has_alias()
+ assert d2.has_alias()
+
+ # Both return an alias; the second multiplies the value by 10:
+ e1 = TestFactory7(tag.alias, tag.pointer, 9)
+ e2 = PythFactory7(tag.alias, tag.pointer, 10)
+ assert e1.get() == 9
+ assert e2.get() == 200
+ assert e1.has_alias()
+ assert e2.has_alias()
+
+ f1 = TestFactory7(tag.shared_ptr, tag.base, 11)
+ f2 = PythFactory7(tag.shared_ptr, tag.base, 12)
+ assert f1.get() == 11
+ assert f2.get() == 112
+ assert not f1.has_alias()
+ assert f2.has_alias()
+
+ g1 = TestFactory7(tag.shared_ptr, tag.invalid_base, 13)
+ assert g1.get() == 13
+ assert not g1.has_alias()
+ with pytest.raises(TypeError) as excinfo:
+ PythFactory7(tag.shared_ptr, tag.invalid_base, 14)
+ assert (str(excinfo.value) ==
+ "pybind11::init(): construction failed: returned holder-wrapped instance is not an "
+ "alias instance")
+
+ assert [i.alive() for i in cstats] == [13, 7]
+ assert ConstructorStats.detail_reg_inst() == n_inst + 13
+
+ del a1, a2, b1, d1, e1, e2
+ assert [i.alive() for i in cstats] == [7, 4]
+ assert ConstructorStats.detail_reg_inst() == n_inst + 7
+ del b2, c1, c2, d2, f1, f2, g1
+ assert [i.alive() for i in cstats] == [0, 0]
+ assert ConstructorStats.detail_reg_inst() == n_inst
+
+ assert [i.values() for i in cstats] == [
+ ["1", "2", "3", "4", "5", "6", "7", "8", "9", "100", "11", "12", "13", "14"],
+ ["2", "4", "6", "8", "9", "100", "12"]
+ ]
+
+
+def test_no_placement_new(capture):
+ """Prior to 2.2, `py::init<...>` relied on the type supporting placement
+ new; this tests a class without placement new support."""
+ with capture:
+ a = m.NoPlacementNew(123)
+
+ found = re.search(r'^operator new called, returning (\d+)\n$', str(capture))
+ assert found
+ assert a.i == 123
+ with capture:
+ del a
+ pytest.gc_collect()
+ assert capture == "operator delete called on " + found.group(1)
+
+ with capture:
+ b = m.NoPlacementNew()
+
+ found = re.search(r'^operator new called, returning (\d+)\n$', str(capture))
+ assert found
+ assert b.i == 100
+ with capture:
+ del b
+ pytest.gc_collect()
+ assert capture == "operator delete called on " + found.group(1)
+
+
+def test_multiple_inheritance():
+ class MITest(m.TestFactory1, m.TestFactory2):
+ def __init__(self):
+ m.TestFactory1.__init__(self, tag.unique_ptr, 33)
+ m.TestFactory2.__init__(self, tag.move)
+
+ a = MITest()
+ assert m.TestFactory1.value.fget(a) == "33"
+ assert m.TestFactory2.value.fget(a) == "(empty2)"
+
+
+def create_and_destroy(*args):
+ a = m.NoisyAlloc(*args)
+ print("---")
+ del a
+ pytest.gc_collect()
+
+
+def strip_comments(s):
+ return re.sub(r'\s+#.*', '', s)
+
+
+def test_reallocations(capture, msg):
+ """When the constructor is overloaded, previous overloads can require a preallocated value.
+ This test makes sure that such preallocated values only happen when they might be necessary,
+ and that they are deallocated properly"""
+
+ pytest.gc_collect()
+
+ with capture:
+ create_and_destroy(1)
+ assert msg(capture) == """
+ noisy new
+ noisy placement new
+ NoisyAlloc(int 1)
+ ---
+ ~NoisyAlloc()
+ noisy delete
+ """
+ with capture:
+ create_and_destroy(1.5)
+ assert msg(capture) == strip_comments("""
+ noisy new # allocation required to attempt first overload
+ noisy delete # have to dealloc before considering factory init overload
+ noisy new # pointer factory calling "new", part 1: allocation
+ NoisyAlloc(double 1.5) # ... part two, invoking constructor
+ ---
+ ~NoisyAlloc() # Destructor
+ noisy delete # operator delete
+ """)
+
+ with capture:
+ create_and_destroy(2, 3)
+ assert msg(capture) == strip_comments("""
+ noisy new # pointer factory calling "new", allocation
+ NoisyAlloc(int 2) # constructor
+ ---
+ ~NoisyAlloc() # Destructor
+ noisy delete # operator delete
+ """)
+
+ with capture:
+ create_and_destroy(2.5, 3)
+ assert msg(capture) == strip_comments("""
+ NoisyAlloc(double 2.5) # construction (local func variable: operator_new not called)
+ noisy new # return-by-value "new" part 1: allocation
+ ~NoisyAlloc() # moved-away local func variable destruction
+ ---
+ ~NoisyAlloc() # Destructor
+ noisy delete # operator delete
+ """)
+
+ with capture:
+ create_and_destroy(3.5, 4.5)
+ assert msg(capture) == strip_comments("""
+ noisy new # preallocation needed before invoking placement-new overload
+ noisy placement new # Placement new
+ NoisyAlloc(double 3.5) # construction
+ ---
+ ~NoisyAlloc() # Destructor
+ noisy delete # operator delete
+ """)
+
+ with capture:
+ create_and_destroy(4, 0.5)
+ assert msg(capture) == strip_comments("""
+ noisy new # preallocation needed before invoking placement-new overload
+ noisy delete # deallocation of preallocated storage
+ noisy new # Factory pointer allocation
+ NoisyAlloc(int 4) # factory pointer construction
+ ---
+ ~NoisyAlloc() # Destructor
+ noisy delete # operator delete
+ """)
+
+ with capture:
+ create_and_destroy(5, "hi")
+ assert msg(capture) == strip_comments("""
+ noisy new # preallocation needed before invoking first placement new
+ noisy delete # delete before considering new-style constructor
+ noisy new # preallocation for second placement new
+ noisy placement new # Placement new in the second placement new overload
+ NoisyAlloc(int 5) # construction
+ ---
+ ~NoisyAlloc() # Destructor
+ noisy delete # operator delete
+ """)
+
+
+@pytest.unsupported_on_py2
+def test_invalid_self():
+ """Tests invocation of the pybind-registered base class with an invalid `self` argument. You
+ can only actually do this on Python 3: Python 2 raises an exception itself if you try."""
+ class NotPybindDerived(object):
+ pass
+
+ # Attempts to initialize with an invalid type passed as `self`:
+ class BrokenTF1(m.TestFactory1):
+ def __init__(self, bad):
+ if bad == 1:
+ a = m.TestFactory2(tag.pointer, 1)
+ m.TestFactory1.__init__(a, tag.pointer)
+ elif bad == 2:
+ a = NotPybindDerived()
+ m.TestFactory1.__init__(a, tag.pointer)
+
+ # Same as above, but for a class with an alias:
+ class BrokenTF6(m.TestFactory6):
+ def __init__(self, bad):
+ if bad == 1:
+ a = m.TestFactory2(tag.pointer, 1)
+ m.TestFactory6.__init__(a, tag.base, 1)
+ elif bad == 2:
+ a = m.TestFactory2(tag.pointer, 1)
+ m.TestFactory6.__init__(a, tag.alias, 1)
+ elif bad == 3:
+ m.TestFactory6.__init__(NotPybindDerived.__new__(NotPybindDerived), tag.base, 1)
+ elif bad == 4:
+ m.TestFactory6.__init__(NotPybindDerived.__new__(NotPybindDerived), tag.alias, 1)
+
+ for arg in (1, 2):
+ with pytest.raises(TypeError) as excinfo:
+ BrokenTF1(arg)
+ assert str(excinfo.value) == "__init__(self, ...) called with invalid `self` argument"
+
+ for arg in (1, 2, 3, 4):
+ with pytest.raises(TypeError) as excinfo:
+ BrokenTF6(arg)
+ assert str(excinfo.value) == "__init__(self, ...) called with invalid `self` argument"
diff --git a/ext/pybind11/tests/test_inheritance.cpp b/ext/pybind11/tests/test_inheritance.cpp
deleted file mode 100644
index c19f58dc2..000000000
--- a/ext/pybind11/tests/test_inheritance.cpp
+++ /dev/null
@@ -1,123 +0,0 @@
-/*
- tests/test_inheritance.cpp -- inheritance, automatic upcasting for polymorphic types
-
- Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
-
- All rights reserved. Use of this source code is governed by a
- BSD-style license that can be found in the LICENSE file.
-*/
-
-#include "pybind11_tests.h"
-
-class Pet {
-public:
- Pet(const std::string &name, const std::string &species)
- : m_name(name), m_species(species) {}
- std::string name() const { return m_name; }
- std::string species() const { return m_species; }
-private:
- std::string m_name;
- std::string m_species;
-};
-
-class Dog : public Pet {
-public:
- Dog(const std::string &name) : Pet(name, "dog") {}
- std::string bark() const { return "Woof!"; }
-};
-
-class Rabbit : public Pet {
-public:
- Rabbit(const std::string &name) : Pet(name, "parrot") {}
-};
-
-class Hamster : public Pet {
-public:
- Hamster(const std::string &name) : Pet(name, "rodent") {}
-};
-
-class Chimera : public Pet {
- Chimera() : Pet("Kimmy", "chimera") {}
-};
-
-std::string pet_name_species(const Pet &pet) {
- return pet.name() + " is a " + pet.species();
-}
-
-std::string dog_bark(const Dog &dog) {
- return dog.bark();
-}
-
-
-struct BaseClass { virtual ~BaseClass() {} };
-struct DerivedClass1 : BaseClass { };
-struct DerivedClass2 : BaseClass { };
-
-struct MismatchBase1 { };
-struct MismatchDerived1 : MismatchBase1 { };
-
-struct MismatchBase2 { };
-struct MismatchDerived2 : MismatchBase2 { };
-
-test_initializer inheritance([](py::module &m) {
- py::class_<Pet> pet_class(m, "Pet");
- pet_class
- .def(py::init<std::string, std::string>())
- .def("name", &Pet::name)
- .def("species", &Pet::species);
-
- /* One way of declaring a subclass relationship: reference parent's class_ object */
- py::class_<Dog>(m, "Dog", pet_class)
- .def(py::init<std::string>());
-
- /* Another way of declaring a subclass relationship: reference parent's C++ type */
- py::class_<Rabbit, Pet>(m, "Rabbit")
- .def(py::init<std::string>());
-
- /* And another: list parent in class template arguments */
- py::class_<Hamster, Pet>(m, "Hamster")
- .def(py::init<std::string>());
-
- py::class_<Chimera, Pet>(m, "Chimera");
-
- m.def("pet_name_species", pet_name_species);
- m.def("dog_bark", dog_bark);
-
- py::class_<BaseClass>(m, "BaseClass").def(py::init<>());
- py::class_<DerivedClass1>(m, "DerivedClass1").def(py::init<>());
- py::class_<DerivedClass2>(m, "DerivedClass2").def(py::init<>());
-
- m.def("return_class_1", []() -> BaseClass* { return new DerivedClass1(); });
- m.def("return_class_2", []() -> BaseClass* { return new DerivedClass2(); });
- m.def("return_class_n", [](int n) -> BaseClass* {
- if (n == 1) return new DerivedClass1();
- if (n == 2) return new DerivedClass2();
- return new BaseClass();
- });
- m.def("return_none", []() -> BaseClass* { return nullptr; });
-
- m.def("test_isinstance", [](py::list l) {
- struct Unregistered { }; // checks missing type_info code path
-
- return py::make_tuple(
- py::isinstance<py::tuple>(l[0]),
- py::isinstance<py::dict>(l[1]),
- py::isinstance<Pet>(l[2]),
- py::isinstance<Pet>(l[3]),
- py::isinstance<Dog>(l[4]),
- py::isinstance<Rabbit>(l[5]),
- py::isinstance<Unregistered>(l[6])
- );
- });
-
- m.def("test_mismatched_holder_type_1", []() {
- auto m = py::module::import("__main__");
- py::class_<MismatchBase1, std::shared_ptr<MismatchBase1>>(m, "MismatchBase1");
- py::class_<MismatchDerived1, MismatchBase1>(m, "MismatchDerived1");
- });
- m.def("test_mismatched_holder_type_2", []() {
- auto m = py::module::import("__main__");
- py::class_<MismatchBase2>(m, "MismatchBase2");
- py::class_<MismatchDerived2, std::shared_ptr<MismatchDerived2>, MismatchBase2>(m, "MismatchDerived2");
- });
-});
diff --git a/ext/pybind11/tests/test_inheritance.py b/ext/pybind11/tests/test_inheritance.py
deleted file mode 100644
index d1f537d1d..000000000
--- a/ext/pybind11/tests/test_inheritance.py
+++ /dev/null
@@ -1,78 +0,0 @@
-import pytest
-
-
-def test_inheritance(msg):
- from pybind11_tests import Pet, Dog, Rabbit, Hamster, Chimera, dog_bark, pet_name_species
-
- roger = Rabbit('Rabbit')
- assert roger.name() + " is a " + roger.species() == "Rabbit is a parrot"
- assert pet_name_species(roger) == "Rabbit is a parrot"
-
- polly = Pet('Polly', 'parrot')
- assert polly.name() + " is a " + polly.species() == "Polly is a parrot"
- assert pet_name_species(polly) == "Polly is a parrot"
-
- molly = Dog('Molly')
- assert molly.name() + " is a " + molly.species() == "Molly is a dog"
- assert pet_name_species(molly) == "Molly is a dog"
-
- fred = Hamster('Fred')
- assert fred.name() + " is a " + fred.species() == "Fred is a rodent"
-
- assert dog_bark(molly) == "Woof!"
-
- with pytest.raises(TypeError) as excinfo:
- dog_bark(polly)
- assert msg(excinfo.value) == """
- dog_bark(): incompatible function arguments. The following argument types are supported:
- 1. (arg0: m.Dog) -> str
-
- Invoked with: <m.Pet object at 0>
- """
-
- with pytest.raises(TypeError) as excinfo:
- Chimera("lion", "goat")
- assert "No constructor defined!" in str(excinfo.value)
-
-
-def test_automatic_upcasting():
- from pybind11_tests import return_class_1, return_class_2, return_class_n, return_none
-
- assert type(return_class_1()).__name__ == "DerivedClass1"
- assert type(return_class_2()).__name__ == "DerivedClass2"
- assert type(return_none()).__name__ == "NoneType"
- # Repeat these a few times in a random order to ensure no invalid caching
- # is applied
- assert type(return_class_n(1)).__name__ == "DerivedClass1"
- assert type(return_class_n(2)).__name__ == "DerivedClass2"
- assert type(return_class_n(0)).__name__ == "BaseClass"
- assert type(return_class_n(2)).__name__ == "DerivedClass2"
- assert type(return_class_n(2)).__name__ == "DerivedClass2"
- assert type(return_class_n(0)).__name__ == "BaseClass"
- assert type(return_class_n(1)).__name__ == "DerivedClass1"
-
-
-def test_isinstance():
- from pybind11_tests import test_isinstance, Pet, Dog
-
- objects = [tuple(), dict(), Pet("Polly", "parrot")] + [Dog("Molly")] * 4
- expected = (True, True, True, True, True, False, False)
- assert test_isinstance(objects) == expected
-
-
-def test_holder():
- from pybind11_tests import test_mismatched_holder_type_1, test_mismatched_holder_type_2
-
- with pytest.raises(RuntimeError) as excinfo:
- test_mismatched_holder_type_1()
-
- assert str(excinfo.value) == ("generic_type: type \"MismatchDerived1\" does not have "
- "a non-default holder type while its base "
- "\"MismatchBase1\" does")
-
- with pytest.raises(RuntimeError) as excinfo:
- test_mismatched_holder_type_2()
-
- assert str(excinfo.value) == ("generic_type: type \"MismatchDerived2\" has a "
- "non-default holder type while its base "
- "\"MismatchBase2\" does not")
diff --git a/ext/pybind11/tests/test_iostream.cpp b/ext/pybind11/tests/test_iostream.cpp
new file mode 100644
index 000000000..e67f88af5
--- /dev/null
+++ b/ext/pybind11/tests/test_iostream.cpp
@@ -0,0 +1,73 @@
+/*
+ tests/test_iostream.cpp -- Usage of scoped_output_redirect
+
+ Copyright (c) 2017 Henry F. Schreiner
+
+ All rights reserved. Use of this source code is governed by a
+ BSD-style license that can be found in the LICENSE file.
+*/
+
+
+#include <pybind11/iostream.h>
+#include "pybind11_tests.h"
+#include <iostream>
+
+
+void noisy_function(std::string msg, bool flush) {
+
+ std::cout << msg;
+ if (flush)
+ std::cout << std::flush;
+}
+
+void noisy_funct_dual(std::string msg, std::string emsg) {
+ std::cout << msg;
+ std::cerr << emsg;
+}
+
+TEST_SUBMODULE(iostream, m) {
+
+ add_ostream_redirect(m);
+
+ // test_evals
+
+ m.def("captured_output_default", [](std::string msg) {
+ py::scoped_ostream_redirect redir;
+ std::cout << msg << std::flush;
+ });
+
+ m.def("captured_output", [](std::string msg) {
+ py::scoped_ostream_redirect redir(std::cout, py::module::import("sys").attr("stdout"));
+ std::cout << msg << std::flush;
+ });
+
+ m.def("guard_output", &noisy_function,
+ py::call_guard<py::scoped_ostream_redirect>(),
+ py::arg("msg"), py::arg("flush")=true);
+
+ m.def("captured_err", [](std::string msg) {
+ py::scoped_ostream_redirect redir(std::cerr, py::module::import("sys").attr("stderr"));
+ std::cerr << msg << std::flush;
+ });
+
+ m.def("noisy_function", &noisy_function, py::arg("msg"), py::arg("flush") = true);
+
+ m.def("dual_guard", &noisy_funct_dual,
+ py::call_guard<py::scoped_ostream_redirect, py::scoped_estream_redirect>(),
+ py::arg("msg"), py::arg("emsg"));
+
+ m.def("raw_output", [](std::string msg) {
+ std::cout << msg << std::flush;
+ });
+
+ m.def("raw_err", [](std::string msg) {
+ std::cerr << msg << std::flush;
+ });
+
+ m.def("captured_dual", [](std::string msg, std::string emsg) {
+ py::scoped_ostream_redirect redirout(std::cout, py::module::import("sys").attr("stdout"));
+ py::scoped_ostream_redirect redirerr(std::cerr, py::module::import("sys").attr("stderr"));
+ std::cout << msg << std::flush;
+ std::cerr << emsg << std::flush;
+ });
+}
diff --git a/ext/pybind11/tests/test_iostream.py b/ext/pybind11/tests/test_iostream.py
new file mode 100644
index 000000000..3364849a4
--- /dev/null
+++ b/ext/pybind11/tests/test_iostream.py
@@ -0,0 +1,203 @@
+from pybind11_tests import iostream as m
+import sys
+
+from contextlib import contextmanager
+
+try:
+ # Python 3
+ from io import StringIO
+except ImportError:
+ # Python 2
+ try:
+ from cStringIO import StringIO
+ except ImportError:
+ from StringIO import StringIO
+
+try:
+ # Python 3.4
+ from contextlib import redirect_stdout
+except ImportError:
+ @contextmanager
+ def redirect_stdout(target):
+ original = sys.stdout
+ sys.stdout = target
+ yield
+ sys.stdout = original
+
+try:
+ # Python 3.5
+ from contextlib import redirect_stderr
+except ImportError:
+ @contextmanager
+ def redirect_stderr(target):
+ original = sys.stderr
+ sys.stderr = target
+ yield
+ sys.stderr = original
+
+
+def test_captured(capsys):
+ msg = "I've been redirected to Python, I hope!"
+ m.captured_output(msg)
+ stdout, stderr = capsys.readouterr()
+ assert stdout == msg
+ assert stderr == ''
+
+ m.captured_output_default(msg)
+ stdout, stderr = capsys.readouterr()
+ assert stdout == msg
+ assert stderr == ''
+
+ m.captured_err(msg)
+ stdout, stderr = capsys.readouterr()
+ assert stdout == ''
+ assert stderr == msg
+
+
+def test_guard_capture(capsys):
+ msg = "I've been redirected to Python, I hope!"
+ m.guard_output(msg)
+ stdout, stderr = capsys.readouterr()
+ assert stdout == msg
+ assert stderr == ''
+
+
+def test_series_captured(capture):
+ with capture:
+ m.captured_output("a")
+ m.captured_output("b")
+ assert capture == "ab"
+
+
+def test_flush(capfd):
+ msg = "(not flushed)"
+ msg2 = "(flushed)"
+
+ with m.ostream_redirect():
+ m.noisy_function(msg, flush=False)
+ stdout, stderr = capfd.readouterr()
+ assert stdout == ''
+
+ m.noisy_function(msg2, flush=True)
+ stdout, stderr = capfd.readouterr()
+ assert stdout == msg + msg2
+
+ m.noisy_function(msg, flush=False)
+
+ stdout, stderr = capfd.readouterr()
+ assert stdout == msg
+
+
+def test_not_captured(capfd):
+ msg = "Something that should not show up in log"
+ stream = StringIO()
+ with redirect_stdout(stream):
+ m.raw_output(msg)
+ stdout, stderr = capfd.readouterr()
+ assert stdout == msg
+ assert stderr == ''
+ assert stream.getvalue() == ''
+
+ stream = StringIO()
+ with redirect_stdout(stream):
+ m.captured_output(msg)
+ stdout, stderr = capfd.readouterr()
+ assert stdout == ''
+ assert stderr == ''
+ assert stream.getvalue() == msg
+
+
+def test_err(capfd):
+ msg = "Something that should not show up in log"
+ stream = StringIO()
+ with redirect_stderr(stream):
+ m.raw_err(msg)
+ stdout, stderr = capfd.readouterr()
+ assert stdout == ''
+ assert stderr == msg
+ assert stream.getvalue() == ''
+
+ stream = StringIO()
+ with redirect_stderr(stream):
+ m.captured_err(msg)
+ stdout, stderr = capfd.readouterr()
+ assert stdout == ''
+ assert stderr == ''
+ assert stream.getvalue() == msg
+
+
+def test_multi_captured(capfd):
+ stream = StringIO()
+ with redirect_stdout(stream):
+ m.captured_output("a")
+ m.raw_output("b")
+ m.captured_output("c")
+ m.raw_output("d")
+ stdout, stderr = capfd.readouterr()
+ assert stdout == 'bd'
+ assert stream.getvalue() == 'ac'
+
+
+def test_dual(capsys):
+ m.captured_dual("a", "b")
+ stdout, stderr = capsys.readouterr()
+ assert stdout == "a"
+ assert stderr == "b"
+
+
+def test_redirect(capfd):
+ msg = "Should not be in log!"
+ stream = StringIO()
+ with redirect_stdout(stream):
+ m.raw_output(msg)
+ stdout, stderr = capfd.readouterr()
+ assert stdout == msg
+ assert stream.getvalue() == ''
+
+ stream = StringIO()
+ with redirect_stdout(stream):
+ with m.ostream_redirect():
+ m.raw_output(msg)
+ stdout, stderr = capfd.readouterr()
+ assert stdout == ''
+ assert stream.getvalue() == msg
+
+ stream = StringIO()
+ with redirect_stdout(stream):
+ m.raw_output(msg)
+ stdout, stderr = capfd.readouterr()
+ assert stdout == msg
+ assert stream.getvalue() == ''
+
+
+def test_redirect_err(capfd):
+ msg = "StdOut"
+ msg2 = "StdErr"
+
+ stream = StringIO()
+ with redirect_stderr(stream):
+ with m.ostream_redirect(stdout=False):
+ m.raw_output(msg)
+ m.raw_err(msg2)
+ stdout, stderr = capfd.readouterr()
+ assert stdout == msg
+ assert stderr == ''
+ assert stream.getvalue() == msg2
+
+
+def test_redirect_both(capfd):
+ msg = "StdOut"
+ msg2 = "StdErr"
+
+ stream = StringIO()
+ stream2 = StringIO()
+ with redirect_stdout(stream):
+ with redirect_stderr(stream2):
+ with m.ostream_redirect():
+ m.raw_output(msg)
+ m.raw_err(msg2)
+ stdout, stderr = capfd.readouterr()
+ assert stdout == ''
+ assert stderr == ''
+ assert stream.getvalue() == msg
+ assert stream2.getvalue() == msg2
diff --git a/ext/pybind11/tests/test_issues.cpp b/ext/pybind11/tests/test_issues.cpp
deleted file mode 100644
index 7da370045..000000000
--- a/ext/pybind11/tests/test_issues.cpp
+++ /dev/null
@@ -1,400 +0,0 @@
-/*
- tests/test_issues.cpp -- collection of testcases for miscellaneous issues
-
- Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
-
- All rights reserved. Use of this source code is governed by a
- BSD-style license that can be found in the LICENSE file.
-*/
-
-#include "pybind11_tests.h"
-#include "constructor_stats.h"
-#include <pybind11/stl.h>
-#include <pybind11/operators.h>
-#include <pybind11/complex.h>
-
-#define TRACKERS(CLASS) CLASS() { print_default_created(this); } ~CLASS() { print_destroyed(this); }
-struct NestABase { int value = -2; TRACKERS(NestABase) };
-struct NestA : NestABase { int value = 3; NestA& operator+=(int i) { value += i; return *this; } TRACKERS(NestA) };
-struct NestB { NestA a; int value = 4; NestB& operator-=(int i) { value -= i; return *this; } TRACKERS(NestB) };
-struct NestC { NestB b; int value = 5; NestC& operator*=(int i) { value *= i; return *this; } TRACKERS(NestC) };
-
-/// #393
-class OpTest1 {};
-class OpTest2 {};
-
-OpTest1 operator+(const OpTest1 &, const OpTest1 &) {
- py::print("Add OpTest1 with OpTest1");
- return OpTest1();
-}
-OpTest2 operator+(const OpTest2 &, const OpTest2 &) {
- py::print("Add OpTest2 with OpTest2");
- return OpTest2();
-}
-OpTest2 operator+(const OpTest2 &, const OpTest1 &) {
- py::print("Add OpTest2 with OpTest1");
- return OpTest2();
-}
-
-// #461
-class Dupe1 {
-public:
- Dupe1(int v) : v_{v} {}
- int get_value() const { return v_; }
-private:
- int v_;
-};
-class Dupe2 {};
-class Dupe3 {};
-class DupeException : public std::runtime_error {};
-
-// #478
-template <typename T> class custom_unique_ptr {
-public:
- custom_unique_ptr() { print_default_created(this); }
- custom_unique_ptr(T *ptr) : _ptr{ptr} { print_created(this, ptr); }
- custom_unique_ptr(custom_unique_ptr<T> &&move) : _ptr{move._ptr} { move._ptr = nullptr; print_move_created(this); }
- custom_unique_ptr &operator=(custom_unique_ptr<T> &&move) { print_move_assigned(this); if (_ptr) destruct_ptr(); _ptr = move._ptr; move._ptr = nullptr; return *this; }
- custom_unique_ptr(const custom_unique_ptr<T> &) = delete;
- void operator=(const custom_unique_ptr<T> &copy) = delete;
- ~custom_unique_ptr() { print_destroyed(this); if (_ptr) destruct_ptr(); }
-private:
- T *_ptr = nullptr;
- void destruct_ptr() { delete _ptr; }
-};
-PYBIND11_DECLARE_HOLDER_TYPE(T, custom_unique_ptr<T>);
-
-/// Issue #528: templated constructor
-struct TplConstrClass {
- template <typename T> TplConstrClass(const T &arg) : str{arg} {}
- std::string str;
- bool operator==(const TplConstrClass &t) const { return t.str == str; }
-};
-namespace std {
-template <> struct hash<TplConstrClass> { size_t operator()(const TplConstrClass &t) const { return std::hash<std::string>()(t.str); } };
-}
-
-void init_issues(py::module &m) {
- py::module m2 = m.def_submodule("issues");
-
-#if !defined(_MSC_VER)
- // Visual Studio 2015 currently cannot compile this test
- // (see the comment in type_caster_base::make_copy_constructor)
- // #70 compilation issue if operator new is not public
- class NonConstructible { private: void *operator new(size_t bytes) throw(); };
- py::class_<NonConstructible>(m, "Foo");
- m2.def("getstmt", []() -> NonConstructible * { return nullptr; },
- py::return_value_policy::reference);
-#endif
-
- // #137: const char* isn't handled properly
- m2.def("print_cchar", [](const char *s) { return std::string(s); });
-
- // #150: char bindings broken
- m2.def("print_char", [](char c) { return std::string(1, c); });
-
- // #159: virtual function dispatch has problems with similar-named functions
- struct Base { virtual std::string dispatch() const {
- /* for some reason MSVC2015 can't compile this if the function is pure virtual */
- return {};
- }; };
-
- struct DispatchIssue : Base {
- virtual std::string dispatch() const {
- PYBIND11_OVERLOAD_PURE(std::string, Base, dispatch, /* no arguments */);
- }
- };
-
- py::class_<Base, DispatchIssue>(m2, "DispatchIssue")
- .def(py::init<>())
- .def("dispatch", &Base::dispatch);
-
- m2.def("dispatch_issue_go", [](const Base * b) { return b->dispatch(); });
-
- struct Placeholder { int i; Placeholder(int i) : i(i) { } };
-
- py::class_<Placeholder>(m2, "Placeholder")
- .def(py::init<int>())
- .def("__repr__", [](const Placeholder &p) { return "Placeholder[" + std::to_string(p.i) + "]"; });
-
- // #171: Can't return reference wrappers (or STL datastructures containing them)
- m2.def("return_vec_of_reference_wrapper", [](std::reference_wrapper<Placeholder> p4) {
- Placeholder *p1 = new Placeholder{1};
- Placeholder *p2 = new Placeholder{2};
- Placeholder *p3 = new Placeholder{3};
- std::vector<std::reference_wrapper<Placeholder>> v;
- v.push_back(std::ref(*p1));
- v.push_back(std::ref(*p2));
- v.push_back(std::ref(*p3));
- v.push_back(p4);
- return v;
- });
-
- // #181: iterator passthrough did not compile
- m2.def("iterator_passthrough", [](py::iterator s) -> py::iterator {
- return py::make_iterator(std::begin(s), std::end(s));
- });
-
- // #187: issue involving std::shared_ptr<> return value policy & garbage collection
- struct ElementBase { virtual void foo() { } /* Force creation of virtual table */ };
- struct ElementA : ElementBase {
- ElementA(int v) : v(v) { }
- int value() { return v; }
- int v;
- };
-
- struct ElementList {
- void add(std::shared_ptr<ElementBase> e) { l.push_back(e); }
- std::vector<std::shared_ptr<ElementBase>> l;
- };
-
- py::class_<ElementBase, std::shared_ptr<ElementBase>> (m2, "ElementBase");
-
- py::class_<ElementA, ElementBase, std::shared_ptr<ElementA>>(m2, "ElementA")
- .def(py::init<int>())
- .def("value", &ElementA::value);
-
- py::class_<ElementList, std::shared_ptr<ElementList>>(m2, "ElementList")
- .def(py::init<>())
- .def("add", &ElementList::add)
- .def("get", [](ElementList &el) {
- py::list list;
- for (auto &e : el.l)
- list.append(py::cast(e));
- return list;
- });
-
- // (no id): should not be able to pass 'None' to a reference argument
- m2.def("get_element", [](ElementA &el) { return el.value(); });
-
- // (no id): don't cast doubles to ints
- m2.def("expect_float", [](float f) { return f; });
- m2.def("expect_int", [](int i) { return i; });
-
- try {
- py::class_<Placeholder>(m2, "Placeholder");
- throw std::logic_error("Expected an exception!");
- } catch (std::runtime_error &) {
- /* All good */
- }
-
- // Issue #283: __str__ called on uninitialized instance when constructor arguments invalid
- class StrIssue {
- public:
- StrIssue(int i) : val{i} {}
- StrIssue() : StrIssue(-1) {}
- int value() const { return val; }
- private:
- int val;
- };
- py::class_<StrIssue> si(m2, "StrIssue");
- si .def(py::init<int>())
- .def(py::init<>())
- .def("__str__", [](const StrIssue &si) { return "StrIssue[" + std::to_string(si.value()) + "]"; })
- ;
-
- // Issue #328: first member in a class can't be used in operators
- py::class_<NestABase>(m2, "NestABase").def(py::init<>()).def_readwrite("value", &NestABase::value);
- py::class_<NestA>(m2, "NestA").def(py::init<>()).def(py::self += int())
- .def("as_base", [](NestA &a) -> NestABase& { return (NestABase&) a; }, py::return_value_policy::reference_internal);
- py::class_<NestB>(m2, "NestB").def(py::init<>()).def(py::self -= int()).def_readwrite("a", &NestB::a);
- py::class_<NestC>(m2, "NestC").def(py::init<>()).def(py::self *= int()).def_readwrite("b", &NestC::b);
- m2.def("get_NestA", [](const NestA &a) { return a.value; });
- m2.def("get_NestB", [](const NestB &b) { return b.value; });
- m2.def("get_NestC", [](const NestC &c) { return c.value; });
-
- // Issue 389: r_v_p::move should fall-through to copy on non-movable objects
- class MoveIssue1 {
- public:
- MoveIssue1(int v) : v{v} {}
- MoveIssue1(const MoveIssue1 &c) { v = c.v; }
- MoveIssue1(MoveIssue1 &&) = delete;
- int v;
- };
- class MoveIssue2 {
- public:
- MoveIssue2(int v) : v{v} {}
- MoveIssue2(MoveIssue2 &&) = default;
- int v;
- };
- py::class_<MoveIssue1>(m2, "MoveIssue1").def(py::init<int>()).def_readwrite("value", &MoveIssue1::v);
- py::class_<MoveIssue2>(m2, "MoveIssue2").def(py::init<int>()).def_readwrite("value", &MoveIssue2::v);
- m2.def("get_moveissue1", [](int i) -> MoveIssue1 * { return new MoveIssue1(i); }, py::return_value_policy::move);
- m2.def("get_moveissue2", [](int i) { return MoveIssue2(i); }, py::return_value_policy::move);
-
- // Issues 392/397: overridding reference-returning functions
- class OverrideTest {
- public:
- struct A { std::string value = "hi"; };
- std::string v;
- A a;
- explicit OverrideTest(const std::string &v) : v{v} {}
- virtual std::string str_value() { return v; }
- virtual std::string &str_ref() { return v; }
- virtual A A_value() { return a; }
- virtual A &A_ref() { return a; }
- };
- class PyOverrideTest : public OverrideTest {
- public:
- using OverrideTest::OverrideTest;
- std::string str_value() override { PYBIND11_OVERLOAD(std::string, OverrideTest, str_value); }
- // Not allowed (uncommenting should hit a static_assert failure): we can't get a reference
- // to a python numeric value, since we only copy values in the numeric type caster:
-// std::string &str_ref() override { PYBIND11_OVERLOAD(std::string &, OverrideTest, str_ref); }
- // But we can work around it like this:
- private:
- std::string _tmp;
- std::string str_ref_helper() { PYBIND11_OVERLOAD(std::string, OverrideTest, str_ref); }
- public:
- std::string &str_ref() override { return _tmp = str_ref_helper(); }
-
- A A_value() override { PYBIND11_OVERLOAD(A, OverrideTest, A_value); }
- A &A_ref() override { PYBIND11_OVERLOAD(A &, OverrideTest, A_ref); }
- };
- py::class_<OverrideTest::A>(m2, "OverrideTest_A")
- .def_readwrite("value", &OverrideTest::A::value);
- py::class_<OverrideTest, PyOverrideTest>(m2, "OverrideTest")
- .def(py::init<const std::string &>())
- .def("str_value", &OverrideTest::str_value)
-// .def("str_ref", &OverrideTest::str_ref)
- .def("A_value", &OverrideTest::A_value)
- .def("A_ref", &OverrideTest::A_ref);
-
- /// Issue 393: need to return NotSupported to ensure correct arithmetic operator behavior
- py::class_<OpTest1>(m2, "OpTest1")
- .def(py::init<>())
- .def(py::self + py::self);
-
- py::class_<OpTest2>(m2, "OpTest2")
- .def(py::init<>())
- .def(py::self + py::self)
- .def("__add__", [](const OpTest2& c2, const OpTest1& c1) { return c2 + c1; })
- .def("__radd__", [](const OpTest2& c2, const OpTest1& c1) { return c2 + c1; });
-
- // Issue 388: Can't make iterators via make_iterator() with different r/v policies
- static std::vector<int> list = { 1, 2, 3 };
- m2.def("make_iterator_1", []() { return py::make_iterator<py::return_value_policy::copy>(list); });
- m2.def("make_iterator_2", []() { return py::make_iterator<py::return_value_policy::automatic>(list); });
-
- static std::vector<std::string> nothrows;
- // Issue 461: registering two things with the same name:
- py::class_<Dupe1>(m2, "Dupe1")
- .def("get_value", &Dupe1::get_value)
- ;
- m2.def("dupe1_factory", [](int v) { return new Dupe1(v); });
-
- py::class_<Dupe2>(m2, "Dupe2");
- py::exception<DupeException>(m2, "DupeException");
-
- try {
- m2.def("Dupe1", [](int v) { return new Dupe1(v); });
- nothrows.emplace_back("Dupe1");
- }
- catch (std::runtime_error &) {}
- try {
- py::class_<Dupe3>(m2, "dupe1_factory");
- nothrows.emplace_back("dupe1_factory");
- }
- catch (std::runtime_error &) {}
- try {
- py::exception<Dupe3>(m2, "Dupe2");
- nothrows.emplace_back("Dupe2");
- }
- catch (std::runtime_error &) {}
- try {
- m2.def("DupeException", []() { return 30; });
- nothrows.emplace_back("DupeException1");
- }
- catch (std::runtime_error &) {}
- try {
- py::class_<DupeException>(m2, "DupeException");
- nothrows.emplace_back("DupeException2");
- }
- catch (std::runtime_error &) {}
- m2.def("dupe_exception_failures", []() {
- py::list l;
- for (auto &e : nothrows) l.append(py::cast(e));
- return l;
- });
-
- /// Issue #471: shared pointer instance not dellocated
- class SharedChild : public std::enable_shared_from_this<SharedChild> {
- public:
- SharedChild() { print_created(this); }
- ~SharedChild() { print_destroyed(this); }
- };
-
- class SharedParent {
- public:
- SharedParent() : child(std::make_shared<SharedChild>()) { }
- const SharedChild &get_child() const { return *child; }
-
- private:
- std::shared_ptr<SharedChild> child;
- };
-
- py::class_<SharedChild, std::shared_ptr<SharedChild>>(m, "SharedChild");
- py::class_<SharedParent, std::shared_ptr<SharedParent>>(m, "SharedParent")
- .def(py::init<>())
- .def("get_child", &SharedParent::get_child, py::return_value_policy::reference);
-
- /// Issue/PR #478: unique ptrs constructed and freed without destruction
- class SpecialHolderObj {
- public:
- int val = 0;
- SpecialHolderObj *ch = nullptr;
- SpecialHolderObj(int v, bool make_child = true) : val{v}, ch{make_child ? new SpecialHolderObj(val+1, false) : nullptr}
- { print_created(this, val); }
- ~SpecialHolderObj() { delete ch; print_destroyed(this); }
- SpecialHolderObj *child() { return ch; }
- };
-
- py::class_<SpecialHolderObj, custom_unique_ptr<SpecialHolderObj>>(m, "SpecialHolderObj")
- .def(py::init<int>())
- .def("child", &SpecialHolderObj::child, pybind11::return_value_policy::reference_internal)
- .def_readwrite("val", &SpecialHolderObj::val)
- .def_static("holder_cstats", &ConstructorStats::get<custom_unique_ptr<SpecialHolderObj>>,
- py::return_value_policy::reference);
-
- /// Issue #484: number conversion generates unhandled exceptions
- m2.def("test_complex", [](float x) { py::print("{}"_s.format(x)); });
- m2.def("test_complex", [](std::complex<float> x) { py::print("({}, {})"_s.format(x.real(), x.imag())); });
-
- /// Issue #511: problem with inheritance + overwritten def_static
- struct MyBase {
- static std::unique_ptr<MyBase> make() {
- return std::unique_ptr<MyBase>(new MyBase());
- }
- };
-
- struct MyDerived : MyBase {
- static std::unique_ptr<MyDerived> make() {
- return std::unique_ptr<MyDerived>(new MyDerived());
- }
- };
-
- py::class_<MyBase>(m2, "MyBase")
- .def_static("make", &MyBase::make);
-
- py::class_<MyDerived, MyBase>(m2, "MyDerived")
- .def_static("make", &MyDerived::make)
- .def_static("make2", &MyDerived::make);
-
- py::dict d;
- std::string bar = "bar";
- d["str"] = bar;
- d["num"] = 3.7;
-
- /// Issue #528: templated constructor
- m2.def("tpl_constr_vector", [](std::vector<TplConstrClass> &) {});
- m2.def("tpl_constr_map", [](std::unordered_map<TplConstrClass, TplConstrClass> &) {});
- m2.def("tpl_constr_set", [](std::unordered_set<TplConstrClass> &) {});
-#if defined(PYBIND11_HAS_OPTIONAL)
- m2.def("tpl_constr_optional", [](std::optional<TplConstrClass> &) {});
-#elif defined(PYBIND11_HAS_EXP_OPTIONAL)
- m2.def("tpl_constr_optional", [](std::experimental::optional<TplConstrClass> &) {});
-#endif
-}
-
-// MSVC workaround: trying to use a lambda here crashes MSVC
-test_initializer issues(&init_issues);
diff --git a/ext/pybind11/tests/test_issues.py b/ext/pybind11/tests/test_issues.py
deleted file mode 100644
index e60b5ca90..000000000
--- a/ext/pybind11/tests/test_issues.py
+++ /dev/null
@@ -1,251 +0,0 @@
-import pytest
-from pybind11_tests import ConstructorStats
-
-
-def test_regressions():
- from pybind11_tests.issues import print_cchar, print_char
-
- # #137: const char* isn't handled properly
- assert print_cchar("const char *") == "const char *"
- # #150: char bindings broken
- assert print_char("c") == "c"
-
-
-def test_dispatch_issue(msg):
- """#159: virtual function dispatch has problems with similar-named functions"""
- from pybind11_tests.issues import DispatchIssue, dispatch_issue_go
-
- class PyClass1(DispatchIssue):
- def dispatch(self):
- return "Yay.."
-
- class PyClass2(DispatchIssue):
- def dispatch(self):
- with pytest.raises(RuntimeError) as excinfo:
- super(PyClass2, self).dispatch()
- assert msg(excinfo.value) == 'Tried to call pure virtual function "Base::dispatch"'
-
- p = PyClass1()
- return dispatch_issue_go(p)
-
- b = PyClass2()
- assert dispatch_issue_go(b) == "Yay.."
-
-
-def test_reference_wrapper():
- """#171: Can't return reference wrappers (or STL data structures containing them)"""
- from pybind11_tests.issues import Placeholder, return_vec_of_reference_wrapper
-
- assert str(return_vec_of_reference_wrapper(Placeholder(4))) == \
- "[Placeholder[1], Placeholder[2], Placeholder[3], Placeholder[4]]"
-
-
-def test_iterator_passthrough():
- """#181: iterator passthrough did not compile"""
- from pybind11_tests.issues import iterator_passthrough
-
- assert list(iterator_passthrough(iter([3, 5, 7, 9, 11, 13, 15]))) == [3, 5, 7, 9, 11, 13, 15]
-
-
-def test_shared_ptr_gc():
- """// #187: issue involving std::shared_ptr<> return value policy & garbage collection"""
- from pybind11_tests.issues import ElementList, ElementA
-
- el = ElementList()
- for i in range(10):
- el.add(ElementA(i))
- pytest.gc_collect()
- for i, v in enumerate(el.get()):
- assert i == v.value()
-
-
-def test_no_id(msg):
- from pybind11_tests.issues import get_element, expect_float, expect_int
-
- with pytest.raises(TypeError) as excinfo:
- get_element(None)
- assert msg(excinfo.value) == """
- get_element(): incompatible function arguments. The following argument types are supported:
- 1. (arg0: m.issues.ElementA) -> int
-
- Invoked with: None
- """
-
- with pytest.raises(TypeError) as excinfo:
- expect_int(5.2)
- assert msg(excinfo.value) == """
- expect_int(): incompatible function arguments. The following argument types are supported:
- 1. (arg0: int) -> int
-
- Invoked with: 5.2
- """
- assert expect_float(12) == 12
-
-
-def test_str_issue(msg):
- """Issue #283: __str__ called on uninitialized instance when constructor arguments invalid"""
- from pybind11_tests.issues import StrIssue
-
- assert str(StrIssue(3)) == "StrIssue[3]"
-
- with pytest.raises(TypeError) as excinfo:
- str(StrIssue("no", "such", "constructor"))
- assert msg(excinfo.value) == """
- __init__(): incompatible constructor arguments. The following argument types are supported:
- 1. m.issues.StrIssue(arg0: int)
- 2. m.issues.StrIssue()
-
- Invoked with: 'no', 'such', 'constructor'
- """
-
-
-def test_nested():
- """ #328: first member in a class can't be used in operators"""
- from pybind11_tests.issues import NestA, NestB, NestC, get_NestA, get_NestB, get_NestC
-
- a = NestA()
- b = NestB()
- c = NestC()
-
- a += 10
- assert get_NestA(a) == 13
- b.a += 100
- assert get_NestA(b.a) == 103
- c.b.a += 1000
- assert get_NestA(c.b.a) == 1003
- b -= 1
- assert get_NestB(b) == 3
- c.b -= 3
- assert get_NestB(c.b) == 1
- c *= 7
- assert get_NestC(c) == 35
-
- abase = a.as_base()
- assert abase.value == -2
- a.as_base().value += 44
- assert abase.value == 42
- assert c.b.a.as_base().value == -2
- c.b.a.as_base().value += 44
- assert c.b.a.as_base().value == 42
-
- del c
- pytest.gc_collect()
- del a # Should't delete while abase is still alive
- pytest.gc_collect()
-
- assert abase.value == 42
- del abase, b
- pytest.gc_collect()
-
-
-def test_move_fallback():
- from pybind11_tests.issues import get_moveissue1, get_moveissue2
- m2 = get_moveissue2(2)
- assert m2.value == 2
- m1 = get_moveissue1(1)
- assert m1.value == 1
-
-
-def test_override_ref():
- from pybind11_tests.issues import OverrideTest
- o = OverrideTest("asdf")
-
- # Not allowed (see associated .cpp comment)
- # i = o.str_ref()
- # assert o.str_ref() == "asdf"
- assert o.str_value() == "asdf"
-
- assert o.A_value().value == "hi"
- a = o.A_ref()
- assert a.value == "hi"
- a.value = "bye"
- assert a.value == "bye"
-
-
-def test_operators_notimplemented(capture):
- from pybind11_tests.issues import OpTest1, OpTest2
- with capture:
- c1, c2 = OpTest1(), OpTest2()
- c1 + c1
- c2 + c2
- c2 + c1
- c1 + c2
- assert capture == """
- Add OpTest1 with OpTest1
- Add OpTest2 with OpTest2
- Add OpTest2 with OpTest1
- Add OpTest2 with OpTest1
- """
-
-
-def test_iterator_rvpolicy():
- """ Issue 388: Can't make iterators via make_iterator() with different r/v policies """
- from pybind11_tests.issues import make_iterator_1
- from pybind11_tests.issues import make_iterator_2
-
- assert list(make_iterator_1()) == [1, 2, 3]
- assert list(make_iterator_2()) == [1, 2, 3]
- assert not isinstance(make_iterator_1(), type(make_iterator_2()))
-
-
-def test_dupe_assignment():
- """ Issue 461: overwriting a class with a function """
- from pybind11_tests.issues import dupe_exception_failures
- assert dupe_exception_failures() == []
-
-
-def test_enable_shared_from_this_with_reference_rvp():
- """ Issue #471: shared pointer instance not dellocated """
- from pybind11_tests import SharedParent, SharedChild
-
- parent = SharedParent()
- child = parent.get_child()
-
- cstats = ConstructorStats.get(SharedChild)
- assert cstats.alive() == 1
- del child, parent
- assert cstats.alive() == 0
-
-
-def test_non_destructed_holders():
- """ Issue #478: unique ptrs constructed and freed without destruction """
- from pybind11_tests import SpecialHolderObj
-
- a = SpecialHolderObj(123)
- b = a.child()
-
- assert a.val == 123
- assert b.val == 124
-
- cstats = SpecialHolderObj.holder_cstats()
- assert cstats.alive() == 1
- del b
- assert cstats.alive() == 1
- del a
- assert cstats.alive() == 0
-
-
-def test_complex_cast(capture):
- """ Issue #484: number conversion generates unhandled exceptions """
- from pybind11_tests.issues import test_complex
-
- with capture:
- test_complex(1)
- test_complex(2j)
-
- assert capture == """
- 1.0
- (0.0, 2.0)
- """
-
-
-def test_inheritance_override_def_static():
- from pybind11_tests.issues import MyBase, MyDerived
-
- b = MyBase.make()
- d1 = MyDerived.make2()
- d2 = MyDerived.make()
-
- assert isinstance(b, MyBase)
- assert isinstance(d1, MyDerived)
- assert isinstance(d2, MyDerived)
diff --git a/ext/pybind11/tests/test_keep_alive.cpp b/ext/pybind11/tests/test_keep_alive.cpp
deleted file mode 100644
index cd62a02e8..000000000
--- a/ext/pybind11/tests/test_keep_alive.cpp
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- tests/test_keep_alive.cpp -- keep_alive modifier (pybind11's version
- of Boost.Python's with_custodian_and_ward / with_custodian_and_ward_postcall)
-
- Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
-
- All rights reserved. Use of this source code is governed by a
- BSD-style license that can be found in the LICENSE file.
-*/
-
-#include "pybind11_tests.h"
-
-class Child {
-public:
- Child() { py::print("Allocating child."); }
- ~Child() { py::print("Releasing child."); }
-};
-
-class Parent {
-public:
- Parent() { py::print("Allocating parent."); }
- ~Parent() { py::print("Releasing parent."); }
- void addChild(Child *) { }
- Child *returnChild() { return new Child(); }
- Child *returnNullChild() { return nullptr; }
-};
-
-test_initializer keep_alive([](py::module &m) {
- py::class_<Parent>(m, "Parent")
- .def(py::init<>())
- .def("addChild", &Parent::addChild)
- .def("addChildKeepAlive", &Parent::addChild, py::keep_alive<1, 2>())
- .def("returnChild", &Parent::returnChild)
- .def("returnChildKeepAlive", &Parent::returnChild, py::keep_alive<1, 0>())
- .def("returnNullChildKeepAliveChild", &Parent::returnNullChild, py::keep_alive<1, 0>())
- .def("returnNullChildKeepAliveParent", &Parent::returnNullChild, py::keep_alive<0, 1>());
-
- py::class_<Child>(m, "Child")
- .def(py::init<>());
-});
diff --git a/ext/pybind11/tests/test_keep_alive.py b/ext/pybind11/tests/test_keep_alive.py
deleted file mode 100644
index bfd7d40c3..000000000
--- a/ext/pybind11/tests/test_keep_alive.py
+++ /dev/null
@@ -1,97 +0,0 @@
-import pytest
-
-
-def test_keep_alive_argument(capture):
- from pybind11_tests import Parent, Child
-
- with capture:
- p = Parent()
- assert capture == "Allocating parent."
- with capture:
- p.addChild(Child())
- pytest.gc_collect()
- assert capture == """
- Allocating child.
- Releasing child.
- """
- with capture:
- del p
- pytest.gc_collect()
- assert capture == "Releasing parent."
-
- with capture:
- p = Parent()
- assert capture == "Allocating parent."
- with capture:
- p.addChildKeepAlive(Child())
- pytest.gc_collect()
- assert capture == "Allocating child."
- with capture:
- del p
- pytest.gc_collect()
- assert capture == """
- Releasing parent.
- Releasing child.
- """
-
-
-def test_keep_alive_return_value(capture):
- from pybind11_tests import Parent
-
- with capture:
- p = Parent()
- assert capture == "Allocating parent."
- with capture:
- p.returnChild()
- pytest.gc_collect()
- assert capture == """
- Allocating child.
- Releasing child.
- """
- with capture:
- del p
- pytest.gc_collect()
- assert capture == "Releasing parent."
-
- with capture:
- p = Parent()
- assert capture == "Allocating parent."
- with capture:
- p.returnChildKeepAlive()
- pytest.gc_collect()
- assert capture == "Allocating child."
- with capture:
- del p
- pytest.gc_collect()
- assert capture == """
- Releasing parent.
- Releasing child.
- """
-
-
-def test_return_none(capture):
- from pybind11_tests import Parent
-
- with capture:
- p = Parent()
- assert capture == "Allocating parent."
- with capture:
- p.returnNullChildKeepAliveChild()
- pytest.gc_collect()
- assert capture == ""
- with capture:
- del p
- pytest.gc_collect()
- assert capture == "Releasing parent."
-
- with capture:
- p = Parent()
- assert capture == "Allocating parent."
- with capture:
- p.returnNullChildKeepAliveParent()
- pytest.gc_collect()
- assert capture == ""
- with capture:
- del p
- pytest.gc_collect()
- assert capture == "Releasing parent."
diff --git a/ext/pybind11/tests/test_kwargs_and_defaults.cpp b/ext/pybind11/tests/test_kwargs_and_defaults.cpp
index 3180123df..165f8017e 100644
--- a/ext/pybind11/tests/test_kwargs_and_defaults.cpp
+++ b/ext/pybind11/tests/test_kwargs_and_defaults.cpp
@@ -10,84 +10,62 @@
#include "pybind11_tests.h"
#include <pybind11/stl.h>
-std::string kw_func(int x, int y) { return "x=" + std::to_string(x) + ", y=" + std::to_string(y); }
+TEST_SUBMODULE(kwargs_and_defaults, m) {
+ auto kw_func = [](int x, int y) { return "x=" + std::to_string(x) + ", y=" + std::to_string(y); };
-std::string kw_func4(const std::vector<int> &entries) {
- std::string ret = "{";
- for (int i : entries)
- ret += std::to_string(i) + " ";
- ret.back() = '}';
- return ret;
-}
-
-py::tuple args_function(py::args args) {
- return args;
-}
-
-py::tuple args_kwargs_function(py::args args, py::kwargs kwargs) {
- return py::make_tuple(args, kwargs);
-}
-
-py::tuple mixed_plus_args(int i, double j, py::args args) {
- return py::make_tuple(i, j, args);
-}
-
-py::tuple mixed_plus_kwargs(int i, double j, py::kwargs kwargs) {
- return py::make_tuple(i, j, kwargs);
-}
-
-py::tuple mixed_plus_args_kwargs(int i, double j, py::args args, py::kwargs kwargs) {
- return py::make_tuple(i, j, args, kwargs);
-}
-
-// pybind11 won't allow these to be bound: args and kwargs, if present, must be at the end.
-void bad_args1(py::args, int) {}
-void bad_args2(py::kwargs, int) {}
-void bad_args3(py::kwargs, py::args) {}
-void bad_args4(py::args, int, py::kwargs) {}
-void bad_args5(py::args, py::kwargs, int) {}
-void bad_args6(py::args, py::args) {}
-void bad_args7(py::kwargs, py::kwargs) {}
-
-struct KWClass {
- void foo(int, float) {}
-};
-
-test_initializer arg_keywords_and_defaults([](py::module &m) {
- m.def("kw_func0", &kw_func);
- m.def("kw_func1", &kw_func, py::arg("x"), py::arg("y"));
- m.def("kw_func2", &kw_func, py::arg("x") = 100, py::arg("y") = 200);
+ // test_named_arguments
+ m.def("kw_func0", kw_func);
+ m.def("kw_func1", kw_func, py::arg("x"), py::arg("y"));
+ m.def("kw_func2", kw_func, py::arg("x") = 100, py::arg("y") = 200);
m.def("kw_func3", [](const char *) { }, py::arg("data") = std::string("Hello world!"));
/* A fancier default argument */
- std::vector<int> list;
- list.push_back(13);
- list.push_back(17);
- m.def("kw_func4", &kw_func4, py::arg("myList") = list);
-
- m.def("args_function", &args_function);
- m.def("args_kwargs_function", &args_kwargs_function);
-
- m.def("kw_func_udl", &kw_func, "x"_a, "y"_a=300);
- m.def("kw_func_udl_z", &kw_func, "x"_a, "y"_a=0);
+ std::vector<int> list{{13, 17}};
+ m.def("kw_func4", [](const std::vector<int> &entries) {
+ std::string ret = "{";
+ for (int i : entries)
+ ret += std::to_string(i) + " ";
+ ret.back() = '}';
+ return ret;
+ }, py::arg("myList") = list);
+
+ m.def("kw_func_udl", kw_func, "x"_a, "y"_a=300);
+ m.def("kw_func_udl_z", kw_func, "x"_a, "y"_a=0);
+
+ // test_args_and_kwargs
+ m.def("args_function", [](py::args args) -> py::tuple { return args; });
+ m.def("args_kwargs_function", [](py::args args, py::kwargs kwargs) {
+ return py::make_tuple(args, kwargs);
+ });
+
+ // test_mixed_args_and_kwargs
+ m.def("mixed_plus_args", [](int i, double j, py::args args) {
+ return py::make_tuple(i, j, args);
+ });
+ m.def("mixed_plus_kwargs", [](int i, double j, py::kwargs kwargs) {
+ return py::make_tuple(i, j, kwargs);
+ });
+ auto mixed_plus_both = [](int i, double j, py::args args, py::kwargs kwargs) {
+ return py::make_tuple(i, j, args, kwargs);
+ };
+ m.def("mixed_plus_args_kwargs", mixed_plus_both);
+
+ m.def("mixed_plus_args_kwargs_defaults", mixed_plus_both,
+ py::arg("i") = 1, py::arg("j") = 3.14159);
+ // pybind11 won't allow these to be bound: args and kwargs, if present, must be at the end.
+ // Uncomment these to test that the static_assert is indeed working:
+// m.def("bad_args1", [](py::args, int) {});
+// m.def("bad_args2", [](py::kwargs, int) {});
+// m.def("bad_args3", [](py::kwargs, py::args) {});
+// m.def("bad_args4", [](py::args, int, py::kwargs) {});
+// m.def("bad_args5", [](py::args, py::kwargs, int) {});
+// m.def("bad_args6", [](py::args, py::args) {});
+// m.def("bad_args7", [](py::kwargs, py::kwargs) {});
+
+ // test_function_signatures (along with most of the above)
+ struct KWClass { void foo(int, float) {} };
py::class_<KWClass>(m, "KWClass")
.def("foo0", &KWClass::foo)
.def("foo1", &KWClass::foo, "x"_a, "y"_a);
-
- m.def("mixed_plus_args", &mixed_plus_args);
- m.def("mixed_plus_kwargs", &mixed_plus_kwargs);
- m.def("mixed_plus_args_kwargs", &mixed_plus_args_kwargs);
-
- m.def("mixed_plus_args_kwargs_defaults", &mixed_plus_args_kwargs,
- py::arg("i") = 1, py::arg("j") = 3.14159);
-
- // Uncomment these to test that the static_assert is indeed working:
-// m.def("bad_args1", &bad_args1);
-// m.def("bad_args2", &bad_args2);
-// m.def("bad_args3", &bad_args3);
-// m.def("bad_args4", &bad_args4);
-// m.def("bad_args5", &bad_args5);
-// m.def("bad_args6", &bad_args6);
-// m.def("bad_args7", &bad_args7);
-});
+}
diff --git a/ext/pybind11/tests/test_kwargs_and_defaults.py b/ext/pybind11/tests/test_kwargs_and_defaults.py
index 90f8489ed..733fe8593 100644
--- a/ext/pybind11/tests/test_kwargs_and_defaults.py
+++ b/ext/pybind11/tests/test_kwargs_and_defaults.py
@@ -1,65 +1,64 @@
import pytest
-from pybind11_tests import (kw_func0, kw_func1, kw_func2, kw_func3, kw_func4, args_function,
- args_kwargs_function, kw_func_udl, kw_func_udl_z, KWClass)
+from pybind11_tests import kwargs_and_defaults as m
def test_function_signatures(doc):
- assert doc(kw_func0) == "kw_func0(arg0: int, arg1: int) -> str"
- assert doc(kw_func1) == "kw_func1(x: int, y: int) -> str"
- assert doc(kw_func2) == "kw_func2(x: int=100, y: int=200) -> str"
- assert doc(kw_func3) == "kw_func3(data: str='Hello world!') -> None"
- assert doc(kw_func4) == "kw_func4(myList: List[int]=[13, 17]) -> str"
- assert doc(kw_func_udl) == "kw_func_udl(x: int, y: int=300) -> str"
- assert doc(kw_func_udl_z) == "kw_func_udl_z(x: int, y: int=0) -> str"
- assert doc(args_function) == "args_function(*args) -> tuple"
- assert doc(args_kwargs_function) == "args_kwargs_function(*args, **kwargs) -> tuple"
- assert doc(KWClass.foo0) == "foo0(self: m.KWClass, arg0: int, arg1: float) -> None"
- assert doc(KWClass.foo1) == "foo1(self: m.KWClass, x: int, y: float) -> None"
+ assert doc(m.kw_func0) == "kw_func0(arg0: int, arg1: int) -> str"
+ assert doc(m.kw_func1) == "kw_func1(x: int, y: int) -> str"
+ assert doc(m.kw_func2) == "kw_func2(x: int=100, y: int=200) -> str"
+ assert doc(m.kw_func3) == "kw_func3(data: str='Hello world!') -> None"
+ assert doc(m.kw_func4) == "kw_func4(myList: List[int]=[13, 17]) -> str"
+ assert doc(m.kw_func_udl) == "kw_func_udl(x: int, y: int=300) -> str"
+ assert doc(m.kw_func_udl_z) == "kw_func_udl_z(x: int, y: int=0) -> str"
+ assert doc(m.args_function) == "args_function(*args) -> tuple"
+ assert doc(m.args_kwargs_function) == "args_kwargs_function(*args, **kwargs) -> tuple"
+ assert doc(m.KWClass.foo0) == \
+ "foo0(self: m.kwargs_and_defaults.KWClass, arg0: int, arg1: float) -> None"
+ assert doc(m.KWClass.foo1) == \
+ "foo1(self: m.kwargs_and_defaults.KWClass, x: int, y: float) -> None"
def test_named_arguments(msg):
- assert kw_func0(5, 10) == "x=5, y=10"
+ assert m.kw_func0(5, 10) == "x=5, y=10"
- assert kw_func1(5, 10) == "x=5, y=10"
- assert kw_func1(5, y=10) == "x=5, y=10"
- assert kw_func1(y=10, x=5) == "x=5, y=10"
+ assert m.kw_func1(5, 10) == "x=5, y=10"
+ assert m.kw_func1(5, y=10) == "x=5, y=10"
+ assert m.kw_func1(y=10, x=5) == "x=5, y=10"
- assert kw_func2() == "x=100, y=200"
- assert kw_func2(5) == "x=5, y=200"
- assert kw_func2(x=5) == "x=5, y=200"
- assert kw_func2(y=10) == "x=100, y=10"
- assert kw_func2(5, 10) == "x=5, y=10"
- assert kw_func2(x=5, y=10) == "x=5, y=10"
+ assert m.kw_func2() == "x=100, y=200"
+ assert m.kw_func2(5) == "x=5, y=200"
+ assert m.kw_func2(x=5) == "x=5, y=200"
+ assert m.kw_func2(y=10) == "x=100, y=10"
+ assert m.kw_func2(5, 10) == "x=5, y=10"
+ assert m.kw_func2(x=5, y=10) == "x=5, y=10"
with pytest.raises(TypeError) as excinfo:
# noinspection PyArgumentList
- kw_func2(x=5, y=10, z=12)
+ m.kw_func2(x=5, y=10, z=12)
assert excinfo.match(
r'(?s)^kw_func2\(\): incompatible.*Invoked with: kwargs: ((x=5|y=10|z=12)(, |$))' + '{3}$')
- assert kw_func4() == "{13 17}"
- assert kw_func4(myList=[1, 2, 3]) == "{1 2 3}"
+ assert m.kw_func4() == "{13 17}"
+ assert m.kw_func4(myList=[1, 2, 3]) == "{1 2 3}"
- assert kw_func_udl(x=5, y=10) == "x=5, y=10"
- assert kw_func_udl_z(x=5) == "x=5, y=0"
+ assert m.kw_func_udl(x=5, y=10) == "x=5, y=10"
+ assert m.kw_func_udl_z(x=5) == "x=5, y=0"
def test_arg_and_kwargs():
args = 'arg1_value', 'arg2_value', 3
- assert args_function(*args) == args
+ assert m.args_function(*args) == args
args = 'a1', 'a2'
kwargs = dict(arg3='a3', arg4=4)
- assert args_kwargs_function(*args, **kwargs) == (args, kwargs)
+ assert m.args_kwargs_function(*args, **kwargs) == (args, kwargs)
def test_mixed_args_and_kwargs(msg):
- from pybind11_tests import (mixed_plus_args, mixed_plus_kwargs, mixed_plus_args_kwargs,
- mixed_plus_args_kwargs_defaults)
- mpa = mixed_plus_args
- mpk = mixed_plus_kwargs
- mpak = mixed_plus_args_kwargs
- mpakd = mixed_plus_args_kwargs_defaults
+ mpa = m.mixed_plus_args
+ mpk = m.mixed_plus_kwargs
+ mpak = m.mixed_plus_args_kwargs
+ mpakd = m.mixed_plus_args_kwargs_defaults
assert mpa(1, 2.5, 4, 99.5, None) == (1, 2.5, (4, 99.5, None))
assert mpa(1, 2.5) == (1, 2.5, ())
diff --git a/ext/pybind11/tests/test_local_bindings.cpp b/ext/pybind11/tests/test_local_bindings.cpp
new file mode 100644
index 000000000..97c02dbeb
--- /dev/null
+++ b/ext/pybind11/tests/test_local_bindings.cpp
@@ -0,0 +1,101 @@
+/*
+ tests/test_local_bindings.cpp -- tests the py::module_local class feature which makes a class
+ binding local to the module in which it is defined.
+
+ Copyright (c) 2017 Jason Rhinelander <jason@imaginary.ca>
+
+ All rights reserved. Use of this source code is governed by a
+ BSD-style license that can be found in the LICENSE file.
+*/
+
+#include "pybind11_tests.h"
+#include "local_bindings.h"
+#include <pybind11/stl.h>
+#include <pybind11/stl_bind.h>
+#include <numeric>
+
+TEST_SUBMODULE(local_bindings, m) {
+ // test_load_external
+ m.def("load_external1", [](ExternalType1 &e) { return e.i; });
+ m.def("load_external2", [](ExternalType2 &e) { return e.i; });
+
+ // test_local_bindings
+ // Register a class with py::module_local:
+ bind_local<LocalType, -1>(m, "LocalType", py::module_local())
+ .def("get3", [](LocalType &t) { return t.i + 3; })
+ ;
+
+ m.def("local_value", [](LocalType &l) { return l.i; });
+
+ // test_nonlocal_failure
+ // The main pybind11 test module is loaded first, so this registration will succeed (the second
+ // one, in pybind11_cross_module_tests.cpp, is designed to fail):
+ bind_local<NonLocalType, 0>(m, "NonLocalType")
+ .def(py::init<int>())
+ .def("get", [](LocalType &i) { return i.i; })
+ ;
+
+ // test_duplicate_local
+ // py::module_local declarations should be visible across compilation units that get linked together;
+ // this tries to register a duplicate local. It depends on a definition in test_class.cpp and
+ // should raise a runtime error from the duplicate definition attempt. If test_class isn't
+ // available it *also* throws a runtime error (with "test_class not enabled" as value).
+ m.def("register_local_external", [m]() {
+ auto main = py::module::import("pybind11_tests");
+ if (py::hasattr(main, "class_")) {
+ bind_local<LocalExternal, 7>(m, "LocalExternal", py::module_local());
+ }
+ else throw std::runtime_error("test_class not enabled");
+ });
+
+ // test_stl_bind_local
+ // stl_bind.h binders defaults to py::module_local if the types are local or converting:
+ py::bind_vector<LocalVec>(m, "LocalVec");
+ py::bind_map<LocalMap>(m, "LocalMap");
+ // and global if the type (or one of the types, for the map) is global:
+ py::bind_vector<NonLocalVec>(m, "NonLocalVec");
+ py::bind_map<NonLocalMap>(m, "NonLocalMap");
+
+ // test_stl_bind_global
+ // They can, however, be overridden to global using `py::module_local(false)`:
+ bind_local<NonLocal2, 10>(m, "NonLocal2");
+ py::bind_vector<LocalVec2>(m, "LocalVec2", py::module_local());
+ py::bind_map<NonLocalMap2>(m, "NonLocalMap2", py::module_local(false));
+
+ // test_mixed_local_global
+ // We try this both with the global type registered first and vice versa (the order shouldn't
+ // matter).
+ m.def("register_mixed_global", [m]() {
+ bind_local<MixedGlobalLocal, 100>(m, "MixedGlobalLocal", py::module_local(false));
+ });
+ m.def("register_mixed_local", [m]() {
+ bind_local<MixedLocalGlobal, 1000>(m, "MixedLocalGlobal", py::module_local());
+ });
+ m.def("get_mixed_gl", [](int i) { return MixedGlobalLocal(i); });
+ m.def("get_mixed_lg", [](int i) { return MixedLocalGlobal(i); });
+
+ // test_internal_locals_differ
+ m.def("local_cpp_types_addr", []() { return (uintptr_t) &py::detail::registered_local_types_cpp(); });
+
+ // test_stl_caster_vs_stl_bind
+ m.def("load_vector_via_caster", [](std::vector<int> v) {
+ return std::accumulate(v.begin(), v.end(), 0);
+ });
+
+ // test_cross_module_calls
+ m.def("return_self", [](LocalVec *v) { return v; });
+ m.def("return_copy", [](const LocalVec &v) { return LocalVec(v); });
+
+ class Cat : public pets::Pet { public: Cat(std::string name) : Pet(name) {}; };
+ py::class_<pets::Pet>(m, "Pet", py::module_local())
+ .def("get_name", &pets::Pet::name);
+ // Binding for local extending class:
+ py::class_<Cat, pets::Pet>(m, "Cat")
+ .def(py::init<std::string>());
+ m.def("pet_name", [](pets::Pet &p) { return p.name(); });
+
+ py::class_<MixGL>(m, "MixGL").def(py::init<int>());
+ m.def("get_gl_value", [](MixGL &o) { return o.i + 10; });
+
+ py::class_<MixGL2>(m, "MixGL2").def(py::init<int>());
+}
diff --git a/ext/pybind11/tests/test_local_bindings.py b/ext/pybind11/tests/test_local_bindings.py
new file mode 100644
index 000000000..b3dc3619c
--- /dev/null
+++ b/ext/pybind11/tests/test_local_bindings.py
@@ -0,0 +1,226 @@
+import pytest
+
+from pybind11_tests import local_bindings as m
+
+
+def test_load_external():
+ """Load a `py::module_local` type that's only registered in an external module"""
+ import pybind11_cross_module_tests as cm
+
+ assert m.load_external1(cm.ExternalType1(11)) == 11
+ assert m.load_external2(cm.ExternalType2(22)) == 22
+
+ with pytest.raises(TypeError) as excinfo:
+ assert m.load_external2(cm.ExternalType1(21)) == 21
+ assert "incompatible function arguments" in str(excinfo.value)
+
+ with pytest.raises(TypeError) as excinfo:
+ assert m.load_external1(cm.ExternalType2(12)) == 12
+ assert "incompatible function arguments" in str(excinfo.value)
+
+
+def test_local_bindings():
+ """Tests that duplicate `py::module_local` class bindings work across modules"""
+
+ # Make sure we can load the second module with the conflicting (but local) definition:
+ import pybind11_cross_module_tests as cm
+
+ i1 = m.LocalType(5)
+ assert i1.get() == 4
+ assert i1.get3() == 8
+
+ i2 = cm.LocalType(10)
+ assert i2.get() == 11
+ assert i2.get2() == 12
+
+ assert not hasattr(i1, 'get2')
+ assert not hasattr(i2, 'get3')
+
+ # Loading within the local module
+ assert m.local_value(i1) == 5
+ assert cm.local_value(i2) == 10
+
+ # Cross-module loading works as well (on failure, the type loader looks for
+ # external module-local converters):
+ assert m.local_value(i2) == 10
+ assert cm.local_value(i1) == 5
+
+
+def test_nonlocal_failure():
+ """Tests that attempting to register a non-local type in multiple modules fails"""
+ import pybind11_cross_module_tests as cm
+
+ with pytest.raises(RuntimeError) as excinfo:
+ cm.register_nonlocal()
+ assert str(excinfo.value) == 'generic_type: type "NonLocalType" is already registered!'
+
+
+def test_duplicate_local():
+ """Tests expected failure when registering a class twice with py::local in the same module"""
+ with pytest.raises(RuntimeError) as excinfo:
+ m.register_local_external()
+ import pybind11_tests
+ assert str(excinfo.value) == (
+ 'generic_type: type "LocalExternal" is already registered!'
+ if hasattr(pybind11_tests, 'class_') else 'test_class not enabled')
+
+
+def test_stl_bind_local():
+ import pybind11_cross_module_tests as cm
+
+ v1, v2 = m.LocalVec(), cm.LocalVec()
+ v1.append(m.LocalType(1))
+ v1.append(m.LocalType(2))
+ v2.append(cm.LocalType(1))
+ v2.append(cm.LocalType(2))
+
+ # Cross module value loading:
+ v1.append(cm.LocalType(3))
+ v2.append(m.LocalType(3))
+
+ assert [i.get() for i in v1] == [0, 1, 2]
+ assert [i.get() for i in v2] == [2, 3, 4]
+
+ v3, v4 = m.NonLocalVec(), cm.NonLocalVec2()
+ v3.append(m.NonLocalType(1))
+ v3.append(m.NonLocalType(2))
+ v4.append(m.NonLocal2(3))
+ v4.append(m.NonLocal2(4))
+
+ assert [i.get() for i in v3] == [1, 2]
+ assert [i.get() for i in v4] == [13, 14]
+
+ d1, d2 = m.LocalMap(), cm.LocalMap()
+ d1["a"] = v1[0]
+ d1["b"] = v1[1]
+ d2["c"] = v2[0]
+ d2["d"] = v2[1]
+ assert {i: d1[i].get() for i in d1} == {'a': 0, 'b': 1}
+ assert {i: d2[i].get() for i in d2} == {'c': 2, 'd': 3}
+
+
+def test_stl_bind_global():
+ import pybind11_cross_module_tests as cm
+
+ with pytest.raises(RuntimeError) as excinfo:
+ cm.register_nonlocal_map()
+ assert str(excinfo.value) == 'generic_type: type "NonLocalMap" is already registered!'
+
+ with pytest.raises(RuntimeError) as excinfo:
+ cm.register_nonlocal_vec()
+ assert str(excinfo.value) == 'generic_type: type "NonLocalVec" is already registered!'
+
+ with pytest.raises(RuntimeError) as excinfo:
+ cm.register_nonlocal_map2()
+ assert str(excinfo.value) == 'generic_type: type "NonLocalMap2" is already registered!'
+
+
+def test_mixed_local_global():
+ """Local types take precedence over globally registered types: a module with a `module_local`
+ type can be registered even if the type is already registered globally. With the module,
+ casting will go to the local type; outside the module casting goes to the global type."""
+ import pybind11_cross_module_tests as cm
+ m.register_mixed_global()
+ m.register_mixed_local()
+
+ a = []
+ a.append(m.MixedGlobalLocal(1))
+ a.append(m.MixedLocalGlobal(2))
+ a.append(m.get_mixed_gl(3))
+ a.append(m.get_mixed_lg(4))
+
+ assert [x.get() for x in a] == [101, 1002, 103, 1004]
+
+ cm.register_mixed_global_local()
+ cm.register_mixed_local_global()
+ a.append(m.MixedGlobalLocal(5))
+ a.append(m.MixedLocalGlobal(6))
+ a.append(cm.MixedGlobalLocal(7))
+ a.append(cm.MixedLocalGlobal(8))
+ a.append(m.get_mixed_gl(9))
+ a.append(m.get_mixed_lg(10))
+ a.append(cm.get_mixed_gl(11))
+ a.append(cm.get_mixed_lg(12))
+
+ assert [x.get() for x in a] == \
+ [101, 1002, 103, 1004, 105, 1006, 207, 2008, 109, 1010, 211, 2012]
+
+
+def test_internal_locals_differ():
+ """Makes sure the internal local type map differs across the two modules"""
+ import pybind11_cross_module_tests as cm
+ assert m.local_cpp_types_addr() != cm.local_cpp_types_addr()
+
+
+def test_stl_caster_vs_stl_bind(msg):
+ """One module uses a generic vector caster from `<pybind11/stl.h>` while the other
+ exports `std::vector<int>` via `py:bind_vector` and `py::module_local`"""
+ import pybind11_cross_module_tests as cm
+
+ v1 = cm.VectorInt([1, 2, 3])
+ assert m.load_vector_via_caster(v1) == 6
+ assert cm.load_vector_via_binding(v1) == 6
+
+ v2 = [1, 2, 3]
+ assert m.load_vector_via_caster(v2) == 6
+ with pytest.raises(TypeError) as excinfo:
+ cm.load_vector_via_binding(v2) == 6
+ assert msg(excinfo.value) == """
+ load_vector_via_binding(): incompatible function arguments. The following argument types are supported:
+ 1. (arg0: pybind11_cross_module_tests.VectorInt) -> int
+
+ Invoked with: [1, 2, 3]
+ """ # noqa: E501 line too long
+
+
+def test_cross_module_calls():
+ import pybind11_cross_module_tests as cm
+
+ v1 = m.LocalVec()
+ v1.append(m.LocalType(1))
+ v2 = cm.LocalVec()
+ v2.append(cm.LocalType(2))
+
+ # Returning the self pointer should get picked up as returning an existing
+ # instance (even when that instance is of a foreign, non-local type).
+ assert m.return_self(v1) is v1
+ assert cm.return_self(v2) is v2
+ assert m.return_self(v2) is v2
+ assert cm.return_self(v1) is v1
+
+ assert m.LocalVec is not cm.LocalVec
+ # Returning a copy, on the other hand, always goes to the local type,
+ # regardless of where the source type came from.
+ assert type(m.return_copy(v1)) is m.LocalVec
+ assert type(m.return_copy(v2)) is m.LocalVec
+ assert type(cm.return_copy(v1)) is cm.LocalVec
+ assert type(cm.return_copy(v2)) is cm.LocalVec
+
+ # Test the example given in the documentation (which also tests inheritance casting):
+ mycat = m.Cat("Fluffy")
+ mydog = cm.Dog("Rover")
+ assert mycat.get_name() == "Fluffy"
+ assert mydog.name() == "Rover"
+ assert m.Cat.__base__.__name__ == "Pet"
+ assert cm.Dog.__base__.__name__ == "Pet"
+ assert m.Cat.__base__ is not cm.Dog.__base__
+ assert m.pet_name(mycat) == "Fluffy"
+ assert m.pet_name(mydog) == "Rover"
+ assert cm.pet_name(mycat) == "Fluffy"
+ assert cm.pet_name(mydog) == "Rover"
+
+ assert m.MixGL is not cm.MixGL
+ a = m.MixGL(1)
+ b = cm.MixGL(2)
+ assert m.get_gl_value(a) == 11
+ assert m.get_gl_value(b) == 12
+ assert cm.get_gl_value(a) == 101
+ assert cm.get_gl_value(b) == 102
+
+ c, d = m.MixGL2(3), cm.MixGL2(4)
+ with pytest.raises(TypeError) as excinfo:
+ m.get_gl_value(c)
+ assert "incompatible function arguments" in str(excinfo)
+ with pytest.raises(TypeError) as excinfo:
+ m.get_gl_value(d)
+ assert "incompatible function arguments" in str(excinfo)
diff --git a/ext/pybind11/tests/test_methods_and_attributes.cpp b/ext/pybind11/tests/test_methods_and_attributes.cpp
index b7b2edfd8..cd15869f4 100644
--- a/ext/pybind11/tests/test_methods_and_attributes.cpp
+++ b/ext/pybind11/tests/test_methods_and_attributes.cpp
@@ -26,39 +26,44 @@ public:
void operator=(const ExampleMandA &e) { print_copy_assigned(this); value = e.value; }
void operator=(ExampleMandA &&e) { print_move_assigned(this); value = e.value; }
- void add1(ExampleMandA other) { value += other.value; } // passing by value
- void add2(ExampleMandA &other) { value += other.value; } // passing by reference
- void add3(const ExampleMandA &other) { value += other.value; } // passing by const reference
- void add4(ExampleMandA *other) { value += other->value; } // passing by pointer
- void add5(const ExampleMandA *other) { value += other->value; } // passing by const pointer
-
- void add6(int other) { value += other; } // passing by value
- void add7(int &other) { value += other; } // passing by reference
- void add8(const int &other) { value += other; } // passing by const reference
- void add9(int *other) { value += *other; } // passing by pointer
- void add10(const int *other) { value += *other; } // passing by const pointer
-
- ExampleMandA self1() { return *this; } // return by value
- ExampleMandA &self2() { return *this; } // return by reference
- const ExampleMandA &self3() { return *this; } // return by const reference
- ExampleMandA *self4() { return this; } // return by pointer
- const ExampleMandA *self5() { return this; } // return by const pointer
-
- int internal1() { return value; } // return by value
- int &internal2() { return value; } // return by reference
- const int &internal3() { return value; } // return by const reference
- int *internal4() { return &value; } // return by pointer
- const int *internal5() { return &value; } // return by const pointer
-
+ void add1(ExampleMandA other) { value += other.value; } // passing by value
+ void add2(ExampleMandA &other) { value += other.value; } // passing by reference
+ void add3(const ExampleMandA &other) { value += other.value; } // passing by const reference
+ void add4(ExampleMandA *other) { value += other->value; } // passing by pointer
+ void add5(const ExampleMandA *other) { value += other->value; } // passing by const pointer
+
+ void add6(int other) { value += other; } // passing by value
+ void add7(int &other) { value += other; } // passing by reference
+ void add8(const int &other) { value += other; } // passing by const reference
+ void add9(int *other) { value += *other; } // passing by pointer
+ void add10(const int *other) { value += *other; } // passing by const pointer
+
+ ExampleMandA self1() { return *this; } // return by value
+ ExampleMandA &self2() { return *this; } // return by reference
+ const ExampleMandA &self3() { return *this; } // return by const reference
+ ExampleMandA *self4() { return this; } // return by pointer
+ const ExampleMandA *self5() { return this; } // return by const pointer
+
+ int internal1() { return value; } // return by value
+ int &internal2() { return value; } // return by reference
+ const int &internal3() { return value; } // return by const reference
+ int *internal4() { return &value; } // return by pointer
+ const int *internal5() { return &value; } // return by const pointer
+
+ py::str overloaded() { return "()"; }
+ py::str overloaded(int) { return "(int)"; }
py::str overloaded(int, float) { return "(int, float)"; }
py::str overloaded(float, int) { return "(float, int)"; }
py::str overloaded(int, int) { return "(int, int)"; }
py::str overloaded(float, float) { return "(float, float)"; }
+ py::str overloaded(int) const { return "(int) const"; }
py::str overloaded(int, float) const { return "(int, float) const"; }
py::str overloaded(float, int) const { return "(float, int) const"; }
py::str overloaded(int, int) const { return "(int, int) const"; }
py::str overloaded(float, float) const { return "(float, float) const"; }
+ static py::str overloaded(float) { return "static float"; }
+
int value = 0;
};
@@ -72,34 +77,28 @@ struct TestProperties {
static int static_get() { return static_value; }
static void static_set(int v) { static_value = v; }
};
-
int TestProperties::static_value = 1;
-struct SimpleValue { int value = 1; };
-
-struct TestPropRVP {
- SimpleValue v1;
- SimpleValue v2;
- static SimpleValue sv1;
- static SimpleValue sv2;
-
- const SimpleValue &get1() const { return v1; }
- const SimpleValue &get2() const { return v2; }
- SimpleValue get_rvalue() const { return v2; }
- void set1(int v) { v1.value = v; }
- void set2(int v) { v2.value = v; }
+struct TestPropertiesOverride : TestProperties {
+ int value = 99;
+ static int static_value;
};
+int TestPropertiesOverride::static_value = 99;
-SimpleValue TestPropRVP::sv1{};
-SimpleValue TestPropRVP::sv2{};
-
-class DynamicClass {
-public:
- DynamicClass() { print_default_created(this); }
- ~DynamicClass() { print_destroyed(this); }
+struct TestPropRVP {
+ UserType v1{1};
+ UserType v2{1};
+ static UserType sv1;
+ static UserType sv2;
+
+ const UserType &get1() const { return v1; }
+ const UserType &get2() const { return v2; }
+ UserType get_rvalue() const { return v2; }
+ void set1(int v) { v1.set(v); }
+ void set2(int v) { v2.set(v); }
};
-
-class CppDerivedDynamicClass : public DynamicClass { };
+UserType TestPropRVP::sv1(1);
+UserType TestPropRVP::sv2(1);
// py::arg/py::arg_v testing: these arguments just record their argument when invoked
class ArgInspector1 { public: std::string arg = "(default arg inspector 1)"; };
@@ -145,17 +144,68 @@ public:
}
static handle cast(const ArgAlwaysConverts &, return_value_policy, handle) {
- return py::none();
+ return py::none().release();
}
};
}}
-/// Issue/PR #648: bad arg default debugging output
-class NotRegistered {};
+// test_custom_caster_destruction
+class DestructionTester {
+public:
+ DestructionTester() { print_default_created(this); }
+ ~DestructionTester() { print_destroyed(this); }
+ DestructionTester(const DestructionTester &) { print_copy_created(this); }
+ DestructionTester(DestructionTester &&) { print_move_created(this); }
+ DestructionTester &operator=(const DestructionTester &) { print_copy_assigned(this); return *this; }
+ DestructionTester &operator=(DestructionTester &&) { print_move_assigned(this); return *this; }
+};
+namespace pybind11 { namespace detail {
+template <> struct type_caster<DestructionTester> {
+ PYBIND11_TYPE_CASTER(DestructionTester, _("DestructionTester"));
+ bool load(handle, bool) { return true; }
-test_initializer methods_and_attributes([](py::module &m) {
- py::class_<ExampleMandA>(m, "ExampleMandA")
- .def(py::init<>())
+ static handle cast(const DestructionTester &, return_value_policy, handle) {
+ return py::bool_(true).release();
+ }
+};
+}}
+
+// Test None-allowed py::arg argument policy
+class NoneTester { public: int answer = 42; };
+int none1(const NoneTester &obj) { return obj.answer; }
+int none2(NoneTester *obj) { return obj ? obj->answer : -1; }
+int none3(std::shared_ptr<NoneTester> &obj) { return obj ? obj->answer : -1; }
+int none4(std::shared_ptr<NoneTester> *obj) { return obj && *obj ? (*obj)->answer : -1; }
+int none5(std::shared_ptr<NoneTester> obj) { return obj ? obj->answer : -1; }
+
+struct StrIssue {
+ int val = -1;
+
+ StrIssue() = default;
+ StrIssue(int i) : val{i} {}
+};
+
+// Issues #854, #910: incompatible function args when member function/pointer is in unregistered base class
+class UnregisteredBase {
+public:
+ void do_nothing() const {}
+ void increase_value() { rw_value++; ro_value += 0.25; }
+ void set_int(int v) { rw_value = v; }
+ int get_int() const { return rw_value; }
+ double get_double() const { return ro_value; }
+ int rw_value = 42;
+ double ro_value = 1.25;
+};
+class RegisteredDerived : public UnregisteredBase {
+public:
+ using UnregisteredBase::UnregisteredBase;
+ double sum() const { return rw_value + ro_value; }
+};
+
+TEST_SUBMODULE(methods_and_attributes, m) {
+ // test_methods_and_attributes
+ py::class_<ExampleMandA> emna(m, "ExampleMandA");
+ emna.def(py::init<>())
.def(py::init<int>())
.def(py::init<const ExampleMandA&>())
.def("add1", &ExampleMandA::add1)
@@ -179,29 +229,52 @@ test_initializer methods_and_attributes([](py::module &m) {
.def("internal4", &ExampleMandA::internal4)
.def("internal5", &ExampleMandA::internal5)
#if defined(PYBIND11_OVERLOAD_CAST)
+ .def("overloaded", py::overload_cast<>(&ExampleMandA::overloaded))
+ .def("overloaded", py::overload_cast<int>(&ExampleMandA::overloaded))
.def("overloaded", py::overload_cast<int, float>(&ExampleMandA::overloaded))
.def("overloaded", py::overload_cast<float, int>(&ExampleMandA::overloaded))
.def("overloaded", py::overload_cast<int, int>(&ExampleMandA::overloaded))
.def("overloaded", py::overload_cast<float, float>(&ExampleMandA::overloaded))
.def("overloaded_float", py::overload_cast<float, float>(&ExampleMandA::overloaded))
+ .def("overloaded_const", py::overload_cast<int >(&ExampleMandA::overloaded, py::const_))
.def("overloaded_const", py::overload_cast<int, float>(&ExampleMandA::overloaded, py::const_))
.def("overloaded_const", py::overload_cast<float, int>(&ExampleMandA::overloaded, py::const_))
.def("overloaded_const", py::overload_cast<int, int>(&ExampleMandA::overloaded, py::const_))
.def("overloaded_const", py::overload_cast<float, float>(&ExampleMandA::overloaded, py::const_))
#else
+ .def("overloaded", static_cast<py::str (ExampleMandA::*)()>(&ExampleMandA::overloaded))
+ .def("overloaded", static_cast<py::str (ExampleMandA::*)(int)>(&ExampleMandA::overloaded))
.def("overloaded", static_cast<py::str (ExampleMandA::*)(int, float)>(&ExampleMandA::overloaded))
.def("overloaded", static_cast<py::str (ExampleMandA::*)(float, int)>(&ExampleMandA::overloaded))
.def("overloaded", static_cast<py::str (ExampleMandA::*)(int, int)>(&ExampleMandA::overloaded))
.def("overloaded", static_cast<py::str (ExampleMandA::*)(float, float)>(&ExampleMandA::overloaded))
.def("overloaded_float", static_cast<py::str (ExampleMandA::*)(float, float)>(&ExampleMandA::overloaded))
+ .def("overloaded_const", static_cast<py::str (ExampleMandA::*)(int ) const>(&ExampleMandA::overloaded))
.def("overloaded_const", static_cast<py::str (ExampleMandA::*)(int, float) const>(&ExampleMandA::overloaded))
.def("overloaded_const", static_cast<py::str (ExampleMandA::*)(float, int) const>(&ExampleMandA::overloaded))
.def("overloaded_const", static_cast<py::str (ExampleMandA::*)(int, int) const>(&ExampleMandA::overloaded))
.def("overloaded_const", static_cast<py::str (ExampleMandA::*)(float, float) const>(&ExampleMandA::overloaded))
#endif
+ // test_no_mixed_overloads
+ // Raise error if trying to mix static/non-static overloads on the same name:
+ .def_static("add_mixed_overloads1", []() {
+ auto emna = py::reinterpret_borrow<py::class_<ExampleMandA>>(py::module::import("pybind11_tests.methods_and_attributes").attr("ExampleMandA"));
+ emna.def ("overload_mixed1", static_cast<py::str (ExampleMandA::*)(int, int)>(&ExampleMandA::overloaded))
+ .def_static("overload_mixed1", static_cast<py::str ( *)(float )>(&ExampleMandA::overloaded));
+ })
+ .def_static("add_mixed_overloads2", []() {
+ auto emna = py::reinterpret_borrow<py::class_<ExampleMandA>>(py::module::import("pybind11_tests.methods_and_attributes").attr("ExampleMandA"));
+ emna.def_static("overload_mixed2", static_cast<py::str ( *)(float )>(&ExampleMandA::overloaded))
+ .def ("overload_mixed2", static_cast<py::str (ExampleMandA::*)(int, int)>(&ExampleMandA::overloaded));
+ })
.def("__str__", &ExampleMandA::toString)
.def_readwrite("value", &ExampleMandA::value);
+ // test_copy_method
+ // Issue #443: can't call copied methods in Python 3
+ emna.attr("add2b") = emna.attr("add2");
+
+ // test_properties, test_static_properties, test_static_cls
py::class_<TestProperties>(m, "TestProperties")
.def(py::init<>())
.def_readonly("def_readonly", &TestProperties::value)
@@ -219,15 +292,18 @@ test_initializer methods_and_attributes([](py::module &m) {
[](py::object cls) { return cls; },
[](py::object cls, py::function f) { f(cls); });
- py::class_<SimpleValue>(m, "SimpleValue")
- .def_readwrite("value", &SimpleValue::value);
+ py::class_<TestPropertiesOverride, TestProperties>(m, "TestPropertiesOverride")
+ .def(py::init<>())
+ .def_readonly("def_readonly", &TestPropertiesOverride::value)
+ .def_readonly_static("def_readonly_static", &TestPropertiesOverride::static_value);
- auto static_get1 = [](py::object) -> const SimpleValue & { return TestPropRVP::sv1; };
- auto static_get2 = [](py::object) -> const SimpleValue & { return TestPropRVP::sv2; };
- auto static_set1 = [](py::object, int v) { TestPropRVP::sv1.value = v; };
- auto static_set2 = [](py::object, int v) { TestPropRVP::sv2.value = v; };
+ auto static_get1 = [](py::object) -> const UserType & { return TestPropRVP::sv1; };
+ auto static_get2 = [](py::object) -> const UserType & { return TestPropRVP::sv2; };
+ auto static_set1 = [](py::object, int v) { TestPropRVP::sv1.set(v); };
+ auto static_set2 = [](py::object, int v) { TestPropRVP::sv2.set(v); };
auto rvp_copy = py::return_value_policy::copy;
+ // test_property_return_value_policies
py::class_<TestPropRVP>(m, "TestPropRVP")
.def(py::init<>())
.def_property_readonly("ro_ref", &TestPropRVP::get1)
@@ -242,21 +318,32 @@ test_initializer methods_and_attributes([](py::module &m) {
.def_property_static("static_rw_ref", static_get1, static_set1)
.def_property_static("static_rw_copy", static_get2, static_set2, rvp_copy)
.def_property_static("static_rw_func", py::cpp_function(static_get2, rvp_copy), static_set2)
+ // test_property_rvalue_policy
.def_property_readonly("rvalue", &TestPropRVP::get_rvalue)
- .def_property_readonly_static("static_rvalue", [](py::object) { return SimpleValue(); });
+ .def_property_readonly_static("static_rvalue", [](py::object) { return UserType(1); });
+ // test_metaclass_override
struct MetaclassOverride { };
py::class_<MetaclassOverride>(m, "MetaclassOverride", py::metaclass((PyObject *) &PyType_Type))
.def_property_readonly_static("readonly", [](py::object) { return 1; });
#if !defined(PYPY_VERSION)
+ // test_dynamic_attributes
+ class DynamicClass {
+ public:
+ DynamicClass() { print_default_created(this); }
+ ~DynamicClass() { print_destroyed(this); }
+ };
py::class_<DynamicClass>(m, "DynamicClass", py::dynamic_attr())
.def(py::init());
+ class CppDerivedDynamicClass : public DynamicClass { };
py::class_<CppDerivedDynamicClass, DynamicClass>(m, "CppDerivedDynamicClass")
.def(py::init());
#endif
+ // test_noconvert_args
+ //
// Test converting. The ArgAlwaysConverts is just there to make the first no-conversion pass
// fail so that our call always ends up happening via the second dispatch (the one that allows
// some conversion).
@@ -279,19 +366,81 @@ test_initializer methods_and_attributes([](py::module &m) {
m.def("floats_preferred", [](double f) { return 0.5 * f; }, py::arg("f"));
m.def("floats_only", [](double f) { return 0.5 * f; }, py::arg("f").noconvert());
+ m.def("ints_preferred", [](int i) { return i / 2; }, py::arg("i"));
+ m.def("ints_only", [](int i) { return i / 2; }, py::arg("i").noconvert());
- /// Issue/PR #648: bad arg default debugging output
+ // test_bad_arg_default
+ // Issue/PR #648: bad arg default debugging output
#if !defined(NDEBUG)
m.attr("debug_enabled") = true;
#else
m.attr("debug_enabled") = false;
#endif
m.def("bad_arg_def_named", []{
- auto m = py::module::import("pybind11_tests.issues");
- m.def("should_fail", [](int, NotRegistered) {}, py::arg(), py::arg("a") = NotRegistered());
+ auto m = py::module::import("pybind11_tests");
+ m.def("should_fail", [](int, UnregisteredType) {}, py::arg(), py::arg("a") = UnregisteredType());
});
m.def("bad_arg_def_unnamed", []{
- auto m = py::module::import("pybind11_tests.issues");
- m.def("should_fail", [](int, NotRegistered) {}, py::arg(), py::arg() = NotRegistered());
+ auto m = py::module::import("pybind11_tests");
+ m.def("should_fail", [](int, UnregisteredType) {}, py::arg(), py::arg() = UnregisteredType());
});
-});
+
+ // test_accepts_none
+ py::class_<NoneTester, std::shared_ptr<NoneTester>>(m, "NoneTester")
+ .def(py::init<>());
+ m.def("no_none1", &none1, py::arg().none(false));
+ m.def("no_none2", &none2, py::arg().none(false));
+ m.def("no_none3", &none3, py::arg().none(false));
+ m.def("no_none4", &none4, py::arg().none(false));
+ m.def("no_none5", &none5, py::arg().none(false));
+ m.def("ok_none1", &none1);
+ m.def("ok_none2", &none2, py::arg().none(true));
+ m.def("ok_none3", &none3);
+ m.def("ok_none4", &none4, py::arg().none(true));
+ m.def("ok_none5", &none5);
+
+ // test_str_issue
+ // Issue #283: __str__ called on uninitialized instance when constructor arguments invalid
+ py::class_<StrIssue>(m, "StrIssue")
+ .def(py::init<int>())
+ .def(py::init<>())
+ .def("__str__", [](const StrIssue &si) {
+ return "StrIssue[" + std::to_string(si.val) + "]"; }
+ );
+
+ // test_unregistered_base_implementations
+ //
+ // Issues #854/910: incompatible function args when member function/pointer is in unregistered
+ // base class The methods and member pointers below actually resolve to members/pointers in
+ // UnregisteredBase; before this test/fix they would be registered via lambda with a first
+ // argument of an unregistered type, and thus uncallable.
+ py::class_<RegisteredDerived>(m, "RegisteredDerived")
+ .def(py::init<>())
+ .def("do_nothing", &RegisteredDerived::do_nothing)
+ .def("increase_value", &RegisteredDerived::increase_value)
+ .def_readwrite("rw_value", &RegisteredDerived::rw_value)
+ .def_readonly("ro_value", &RegisteredDerived::ro_value)
+ // These should trigger a static_assert if uncommented
+ //.def_readwrite("fails", &UserType::value) // should trigger a static_assert if uncommented
+ //.def_readonly("fails", &UserType::value) // should trigger a static_assert if uncommented
+ .def_property("rw_value_prop", &RegisteredDerived::get_int, &RegisteredDerived::set_int)
+ .def_property_readonly("ro_value_prop", &RegisteredDerived::get_double)
+ // This one is in the registered class:
+ .def("sum", &RegisteredDerived::sum)
+ ;
+
+ using Adapted = decltype(py::method_adaptor<RegisteredDerived>(&RegisteredDerived::do_nothing));
+ static_assert(std::is_same<Adapted, void (RegisteredDerived::*)() const>::value, "");
+
+ // test_custom_caster_destruction
+ // Test that `take_ownership` works on types with a custom type caster when given a pointer
+
+ // default policy: don't take ownership:
+ m.def("custom_caster_no_destroy", []() { static auto *dt = new DestructionTester(); return dt; });
+
+ m.def("custom_caster_destroy", []() { return new DestructionTester(); },
+ py::return_value_policy::take_ownership); // Takes ownership: destroy when finished
+ m.def("custom_caster_destroy_const", []() -> const DestructionTester * { return new DestructionTester(); },
+ py::return_value_policy::take_ownership); // Likewise (const doesn't inhibit destruction)
+ m.def("destruction_tester_cstats", &ConstructorStats::get<DestructionTester>, py::return_value_policy::reference);
+}
diff --git a/ext/pybind11/tests/test_methods_and_attributes.py b/ext/pybind11/tests/test_methods_and_attributes.py
index f185ac26d..9fd9cb75c 100644
--- a/ext/pybind11/tests/test_methods_and_attributes.py
+++ b/ext/pybind11/tests/test_methods_and_attributes.py
@@ -1,10 +1,11 @@
import pytest
-from pybind11_tests import ExampleMandA, ConstructorStats
+from pybind11_tests import methods_and_attributes as m
+from pybind11_tests import ConstructorStats
def test_methods_and_attributes():
- instance1 = ExampleMandA()
- instance2 = ExampleMandA(32)
+ instance1 = m.ExampleMandA()
+ instance2 = m.ExampleMandA(32)
instance1.add1(instance2)
instance1.add2(instance2)
@@ -31,10 +32,13 @@ def test_methods_and_attributes():
assert instance1.internal4() == 320
assert instance1.internal5() == 320
+ assert instance1.overloaded() == "()"
+ assert instance1.overloaded(0) == "(int)"
assert instance1.overloaded(1, 1.0) == "(int, float)"
assert instance1.overloaded(2.0, 2) == "(float, int)"
assert instance1.overloaded(3, 3) == "(int, int)"
assert instance1.overloaded(4., 4.) == "(float, float)"
+ assert instance1.overloaded_const(-3) == "(int) const"
assert instance1.overloaded_const(5, 5.0) == "(int, float) const"
assert instance1.overloaded_const(6.0, 6) == "(float, int) const"
assert instance1.overloaded_const(7, 7) == "(int, int) const"
@@ -48,7 +52,7 @@ def test_methods_and_attributes():
instance1.value = 100
assert str(instance1) == "ExampleMandA[value=100]"
- cstats = ConstructorStats.get(ExampleMandA)
+ cstats = ConstructorStats.get(m.ExampleMandA)
assert cstats.alive() == 2
del instance1, instance2
assert cstats.alive() == 0
@@ -60,10 +64,25 @@ def test_methods_and_attributes():
assert cstats.move_assignments == 0
-def test_properties():
- from pybind11_tests import TestProperties
+def test_copy_method():
+ """Issue #443: calling copied methods fails in Python 3"""
+
+ m.ExampleMandA.add2c = m.ExampleMandA.add2
+ m.ExampleMandA.add2d = m.ExampleMandA.add2b
+ a = m.ExampleMandA(123)
+ assert a.value == 123
+ a.add2(m.ExampleMandA(-100))
+ assert a.value == 23
+ a.add2b(m.ExampleMandA(20))
+ assert a.value == 43
+ a.add2c(m.ExampleMandA(6))
+ assert a.value == 49
+ a.add2d(m.ExampleMandA(-7))
+ assert a.value == 42
- instance = TestProperties()
+
+def test_properties():
+ instance = m.TestProperties()
assert instance.def_readonly == 1
with pytest.raises(AttributeError):
@@ -81,75 +100,96 @@ def test_properties():
def test_static_properties():
- from pybind11_tests import TestProperties as Type
-
- assert Type.def_readonly_static == 1
+ assert m.TestProperties.def_readonly_static == 1
with pytest.raises(AttributeError) as excinfo:
- Type.def_readonly_static = 2
+ m.TestProperties.def_readonly_static = 2
assert "can't set attribute" in str(excinfo)
- Type.def_readwrite_static = 2
- assert Type.def_readwrite_static == 2
+ m.TestProperties.def_readwrite_static = 2
+ assert m.TestProperties.def_readwrite_static == 2
- assert Type.def_property_readonly_static == 2
+ assert m.TestProperties.def_property_readonly_static == 2
with pytest.raises(AttributeError) as excinfo:
- Type.def_property_readonly_static = 3
+ m.TestProperties.def_property_readonly_static = 3
assert "can't set attribute" in str(excinfo)
- Type.def_property_static = 3
- assert Type.def_property_static == 3
+ m.TestProperties.def_property_static = 3
+ assert m.TestProperties.def_property_static == 3
# Static property read and write via instance
- instance = Type()
+ instance = m.TestProperties()
- Type.def_readwrite_static = 0
- assert Type.def_readwrite_static == 0
+ m.TestProperties.def_readwrite_static = 0
+ assert m.TestProperties.def_readwrite_static == 0
assert instance.def_readwrite_static == 0
instance.def_readwrite_static = 2
- assert Type.def_readwrite_static == 2
+ assert m.TestProperties.def_readwrite_static == 2
assert instance.def_readwrite_static == 2
+ # It should be possible to override properties in derived classes
+ assert m.TestPropertiesOverride().def_readonly == 99
+ assert m.TestPropertiesOverride.def_readonly_static == 99
+
def test_static_cls():
"""Static property getter and setters expect the type object as the their only argument"""
- from pybind11_tests import TestProperties as Type
- instance = Type()
- assert Type.static_cls is Type
- assert instance.static_cls is Type
+ instance = m.TestProperties()
+ assert m.TestProperties.static_cls is m.TestProperties
+ assert instance.static_cls is m.TestProperties
def check_self(self):
- assert self is Type
+ assert self is m.TestProperties
- Type.static_cls = check_self
+ m.TestProperties.static_cls = check_self
instance.static_cls = check_self
def test_metaclass_override():
"""Overriding pybind11's default metaclass changes the behavior of `static_property`"""
- from pybind11_tests import MetaclassOverride
- assert type(ExampleMandA).__name__ == "pybind11_type"
- assert type(MetaclassOverride).__name__ == "type"
+ assert type(m.ExampleMandA).__name__ == "pybind11_type"
+ assert type(m.MetaclassOverride).__name__ == "type"
- assert MetaclassOverride.readonly == 1
- assert type(MetaclassOverride.__dict__["readonly"]).__name__ == "pybind11_static_property"
+ assert m.MetaclassOverride.readonly == 1
+ assert type(m.MetaclassOverride.__dict__["readonly"]).__name__ == "pybind11_static_property"
# Regular `type` replaces the property instead of calling `__set__()`
- MetaclassOverride.readonly = 2
- assert MetaclassOverride.readonly == 2
- assert isinstance(MetaclassOverride.__dict__["readonly"], int)
+ m.MetaclassOverride.readonly = 2
+ assert m.MetaclassOverride.readonly == 2
+ assert isinstance(m.MetaclassOverride.__dict__["readonly"], int)
+
+
+def test_no_mixed_overloads():
+ from pybind11_tests import debug_enabled
+
+ with pytest.raises(RuntimeError) as excinfo:
+ m.ExampleMandA.add_mixed_overloads1()
+ assert (str(excinfo.value) ==
+ "overloading a method with both static and instance methods is not supported; " +
+ ("compile in debug mode for more details" if not debug_enabled else
+ "error while attempting to bind static method ExampleMandA.overload_mixed1"
+ "(arg0: float) -> str")
+ )
+
+ with pytest.raises(RuntimeError) as excinfo:
+ m.ExampleMandA.add_mixed_overloads2()
+ assert (str(excinfo.value) ==
+ "overloading a method with both static and instance methods is not supported; " +
+ ("compile in debug mode for more details" if not debug_enabled else
+ "error while attempting to bind instance method ExampleMandA.overload_mixed2"
+ "(self: pybind11_tests.methods_and_attributes.ExampleMandA, arg0: int, arg1: int)"
+ " -> str")
+ )
@pytest.mark.parametrize("access", ["ro", "rw", "static_ro", "static_rw"])
def test_property_return_value_policies(access):
- from pybind11_tests import TestPropRVP
-
if not access.startswith("static"):
- obj = TestPropRVP()
+ obj = m.TestPropRVP()
else:
- obj = TestPropRVP
+ obj = m.TestPropRVP
ref = getattr(obj, access + "_ref")
assert ref.value == 1
@@ -170,30 +210,20 @@ def test_property_return_value_policies(access):
def test_property_rvalue_policy():
"""When returning an rvalue, the return value policy is automatically changed from
- `reference(_internal)` to `move`. The following would not work otherwise.
- """
- from pybind11_tests import TestPropRVP
+ `reference(_internal)` to `move`. The following would not work otherwise."""
- instance = TestPropRVP()
+ instance = m.TestPropRVP()
o = instance.rvalue
assert o.value == 1
-
-def test_property_rvalue_policy_static():
- """When returning an rvalue, the return value policy is automatically changed from
- `reference(_internal)` to `move`. The following would not work otherwise.
- """
- from pybind11_tests import TestPropRVP
- o = TestPropRVP.static_rvalue
- assert o.value == 1
+ os = m.TestPropRVP.static_rvalue
+ assert os.value == 1
# https://bitbucket.org/pypy/pypy/issues/2447
@pytest.unsupported_on_pypy
def test_dynamic_attributes():
- from pybind11_tests import DynamicClass, CppDerivedDynamicClass
-
- instance = DynamicClass()
+ instance = m.DynamicClass()
assert not hasattr(instance, "foo")
assert "foo" not in dir(instance)
@@ -213,16 +243,16 @@ def test_dynamic_attributes():
instance.__dict__ = []
assert str(excinfo.value) == "__dict__ must be set to a dictionary, not a 'list'"
- cstats = ConstructorStats.get(DynamicClass)
+ cstats = ConstructorStats.get(m.DynamicClass)
assert cstats.alive() == 1
del instance
assert cstats.alive() == 0
# Derived classes should work as well
- class PythonDerivedDynamicClass(DynamicClass):
+ class PythonDerivedDynamicClass(m.DynamicClass):
pass
- for cls in CppDerivedDynamicClass, PythonDerivedDynamicClass:
+ for cls in m.CppDerivedDynamicClass, PythonDerivedDynamicClass:
derived = cls()
derived.foobar = 100
assert derived.foobar == 100
@@ -235,20 +265,18 @@ def test_dynamic_attributes():
# https://bitbucket.org/pypy/pypy/issues/2447
@pytest.unsupported_on_pypy
def test_cyclic_gc():
- from pybind11_tests import DynamicClass
-
# One object references itself
- instance = DynamicClass()
+ instance = m.DynamicClass()
instance.circular_reference = instance
- cstats = ConstructorStats.get(DynamicClass)
+ cstats = ConstructorStats.get(m.DynamicClass)
assert cstats.alive() == 1
del instance
assert cstats.alive() == 0
# Two object reference each other
- i1 = DynamicClass()
- i2 = DynamicClass()
+ i1 = m.DynamicClass()
+ i2 = m.DynamicClass()
i1.cycle = i2
i2.cycle = i1
@@ -258,9 +286,7 @@ def test_cyclic_gc():
def test_noconvert_args(msg):
- from pybind11_tests import ArgInspector, arg_inspect_func, floats_only, floats_preferred
-
- a = ArgInspector()
+ a = m.ArgInspector()
assert msg(a.f("hi")) == """
loading ArgInspector1 argument WITH conversion allowed. Argument value = hi
"""
@@ -284,15 +310,15 @@ def test_noconvert_args(msg):
"""
assert (a.h("arg 1") ==
"loading ArgInspector2 argument WITHOUT conversion allowed. Argument value = arg 1")
- assert msg(arg_inspect_func("A1", "A2")) == """
+ assert msg(m.arg_inspect_func("A1", "A2")) == """
loading ArgInspector2 argument WITH conversion allowed. Argument value = A1
loading ArgInspector1 argument WITHOUT conversion allowed. Argument value = A2
"""
- assert floats_preferred(4) == 2.0
- assert floats_only(4.0) == 2.0
+ assert m.floats_preferred(4) == 2.0
+ assert m.floats_only(4.0) == 2.0
with pytest.raises(TypeError) as excinfo:
- floats_only(4)
+ m.floats_only(4)
assert msg(excinfo.value) == """
floats_only(): incompatible function arguments. The following argument types are supported:
1. (f: float) -> float
@@ -300,26 +326,151 @@ def test_noconvert_args(msg):
Invoked with: 4
"""
+ assert m.ints_preferred(4) == 2
+ assert m.ints_preferred(True) == 0
+ with pytest.raises(TypeError) as excinfo:
+ m.ints_preferred(4.0)
+ assert msg(excinfo.value) == """
+ ints_preferred(): incompatible function arguments. The following argument types are supported:
+ 1. (i: int) -> int
+
+ Invoked with: 4.0
+ """ # noqa: E501 line too long
+
+ assert m.ints_only(4) == 2
+ with pytest.raises(TypeError) as excinfo:
+ m.ints_only(4.0)
+ assert msg(excinfo.value) == """
+ ints_only(): incompatible function arguments. The following argument types are supported:
+ 1. (i: int) -> int
+
+ Invoked with: 4.0
+ """
+
def test_bad_arg_default(msg):
- from pybind11_tests import debug_enabled, bad_arg_def_named, bad_arg_def_unnamed
+ from pybind11_tests import debug_enabled
with pytest.raises(RuntimeError) as excinfo:
- bad_arg_def_named()
+ m.bad_arg_def_named()
assert msg(excinfo.value) == (
- "arg(): could not convert default argument 'a: NotRegistered' in function 'should_fail' "
- "into a Python object (type not registered yet?)"
+ "arg(): could not convert default argument 'a: UnregisteredType' in function "
+ "'should_fail' into a Python object (type not registered yet?)"
if debug_enabled else
"arg(): could not convert default argument into a Python object (type not registered "
"yet?). Compile in debug mode for more information."
)
with pytest.raises(RuntimeError) as excinfo:
- bad_arg_def_unnamed()
+ m.bad_arg_def_unnamed()
assert msg(excinfo.value) == (
- "arg(): could not convert default argument 'NotRegistered' in function 'should_fail' "
- "into a Python object (type not registered yet?)"
+ "arg(): could not convert default argument 'UnregisteredType' in function "
+ "'should_fail' into a Python object (type not registered yet?)"
if debug_enabled else
"arg(): could not convert default argument into a Python object (type not registered "
"yet?). Compile in debug mode for more information."
)
+
+
+def test_accepts_none(msg):
+ a = m.NoneTester()
+ assert m.no_none1(a) == 42
+ assert m.no_none2(a) == 42
+ assert m.no_none3(a) == 42
+ assert m.no_none4(a) == 42
+ assert m.no_none5(a) == 42
+ assert m.ok_none1(a) == 42
+ assert m.ok_none2(a) == 42
+ assert m.ok_none3(a) == 42
+ assert m.ok_none4(a) == 42
+ assert m.ok_none5(a) == 42
+
+ with pytest.raises(TypeError) as excinfo:
+ m.no_none1(None)
+ assert "incompatible function arguments" in str(excinfo.value)
+ with pytest.raises(TypeError) as excinfo:
+ m.no_none2(None)
+ assert "incompatible function arguments" in str(excinfo.value)
+ with pytest.raises(TypeError) as excinfo:
+ m.no_none3(None)
+ assert "incompatible function arguments" in str(excinfo.value)
+ with pytest.raises(TypeError) as excinfo:
+ m.no_none4(None)
+ assert "incompatible function arguments" in str(excinfo.value)
+ with pytest.raises(TypeError) as excinfo:
+ m.no_none5(None)
+ assert "incompatible function arguments" in str(excinfo.value)
+
+ # The first one still raises because you can't pass None as a lvalue reference arg:
+ with pytest.raises(TypeError) as excinfo:
+ assert m.ok_none1(None) == -1
+ assert msg(excinfo.value) == """
+ ok_none1(): incompatible function arguments. The following argument types are supported:
+ 1. (arg0: m.methods_and_attributes.NoneTester) -> int
+
+ Invoked with: None
+ """
+
+ # The rest take the argument as pointer or holder, and accept None:
+ assert m.ok_none2(None) == -1
+ assert m.ok_none3(None) == -1
+ assert m.ok_none4(None) == -1
+ assert m.ok_none5(None) == -1
+
+
+def test_str_issue(msg):
+ """#283: __str__ called on uninitialized instance when constructor arguments invalid"""
+
+ assert str(m.StrIssue(3)) == "StrIssue[3]"
+
+ with pytest.raises(TypeError) as excinfo:
+ str(m.StrIssue("no", "such", "constructor"))
+ assert msg(excinfo.value) == """
+ __init__(): incompatible constructor arguments. The following argument types are supported:
+ 1. m.methods_and_attributes.StrIssue(arg0: int)
+ 2. m.methods_and_attributes.StrIssue()
+
+ Invoked with: 'no', 'such', 'constructor'
+ """
+
+
+def test_unregistered_base_implementations():
+ a = m.RegisteredDerived()
+ a.do_nothing()
+ assert a.rw_value == 42
+ assert a.ro_value == 1.25
+ a.rw_value += 5
+ assert a.sum() == 48.25
+ a.increase_value()
+ assert a.rw_value == 48
+ assert a.ro_value == 1.5
+ assert a.sum() == 49.5
+ assert a.rw_value_prop == 48
+ a.rw_value_prop += 1
+ assert a.rw_value_prop == 49
+ a.increase_value()
+ assert a.ro_value_prop == 1.75
+
+
+def test_custom_caster_destruction():
+ """Tests that returning a pointer to a type that gets converted with a custom type caster gets
+ destroyed when the function has py::return_value_policy::take_ownership policy applied."""
+
+ cstats = m.destruction_tester_cstats()
+ # This one *doesn't* have take_ownership: the pointer should be used but not destroyed:
+ z = m.custom_caster_no_destroy()
+ assert cstats.alive() == 1 and cstats.default_constructions == 1
+ assert z
+
+ # take_ownership applied: this constructs a new object, casts it, then destroys it:
+ z = m.custom_caster_destroy()
+ assert z
+ assert cstats.default_constructions == 2
+
+ # Same, but with a const pointer return (which should *not* inhibit destruction):
+ z = m.custom_caster_destroy_const()
+ assert z
+ assert cstats.default_constructions == 3
+
+ # Make sure we still only have the original object (from ..._no_destroy()) alive:
+ assert cstats.alive() == 1
diff --git a/ext/pybind11/tests/test_modules.cpp b/ext/pybind11/tests/test_modules.cpp
index 50c7d8412..c1475fa62 100644
--- a/ext/pybind11/tests/test_modules.cpp
+++ b/ext/pybind11/tests/test_modules.cpp
@@ -11,42 +11,38 @@
#include "pybind11_tests.h"
#include "constructor_stats.h"
-std::string submodule_func() {
- return "submodule_func()";
-}
-
-class A {
-public:
- A(int v) : v(v) { print_created(this, v); }
- ~A() { print_destroyed(this); }
- A(const A&) { print_copy_created(this); }
- A& operator=(const A &copy) { print_copy_assigned(this); v = copy.v; return *this; }
- std::string toString() { return "A[" + std::to_string(v) + "]"; }
-private:
- int v;
-};
-
-class B {
-public:
- B() { print_default_created(this); }
- ~B() { print_destroyed(this); }
- B(const B&) { print_copy_created(this); }
- B& operator=(const B &copy) { print_copy_assigned(this); a1 = copy.a1; a2 = copy.a2; return *this; }
- A &get_a1() { return a1; }
- A &get_a2() { return a2; }
-
- A a1{1};
- A a2{2};
-};
-
-test_initializer modules([](py::module &m) {
- py::module m_sub = m.def_submodule("submodule");
- m_sub.def("submodule_func", &submodule_func);
+TEST_SUBMODULE(modules, m) {
+ // test_nested_modules
+ py::module m_sub = m.def_submodule("subsubmodule");
+ m_sub.def("submodule_func", []() { return "submodule_func()"; });
+ // test_reference_internal
+ class A {
+ public:
+ A(int v) : v(v) { print_created(this, v); }
+ ~A() { print_destroyed(this); }
+ A(const A&) { print_copy_created(this); }
+ A& operator=(const A &copy) { print_copy_assigned(this); v = copy.v; return *this; }
+ std::string toString() { return "A[" + std::to_string(v) + "]"; }
+ private:
+ int v;
+ };
py::class_<A>(m_sub, "A")
.def(py::init<int>())
.def("__repr__", &A::toString);
+ class B {
+ public:
+ B() { print_default_created(this); }
+ ~B() { print_destroyed(this); }
+ B(const B&) { print_copy_created(this); }
+ B& operator=(const B &copy) { print_copy_assigned(this); a1 = copy.a1; a2 = copy.a2; return *this; }
+ A &get_a1() { return a1; }
+ A &get_a2() { return a2; }
+
+ A a1{1};
+ A a2{2};
+ };
py::class_<B>(m_sub, "B")
.def(py::init<>())
.def("get_a1", &B::get_a1, "Return the internal A 1", py::return_value_policy::reference_internal)
@@ -55,4 +51,48 @@ test_initializer modules([](py::module &m) {
.def_readwrite("a2", &B::a2);
m.attr("OD") = py::module::import("collections").attr("OrderedDict");
-});
+
+ // test_duplicate_registration
+ // Registering two things with the same name
+ m.def("duplicate_registration", []() {
+ class Dupe1 { };
+ class Dupe2 { };
+ class Dupe3 { };
+ class DupeException { };
+
+ auto dm = py::module("dummy");
+ auto failures = py::list();
+
+ py::class_<Dupe1>(dm, "Dupe1");
+ py::class_<Dupe2>(dm, "Dupe2");
+ dm.def("dupe1_factory", []() { return Dupe1(); });
+ py::exception<DupeException>(dm, "DupeException");
+
+ try {
+ py::class_<Dupe1>(dm, "Dupe1");
+ failures.append("Dupe1 class");
+ } catch (std::runtime_error &) {}
+ try {
+ dm.def("Dupe1", []() { return Dupe1(); });
+ failures.append("Dupe1 function");
+ } catch (std::runtime_error &) {}
+ try {
+ py::class_<Dupe3>(dm, "dupe1_factory");
+ failures.append("dupe1_factory");
+ } catch (std::runtime_error &) {}
+ try {
+ py::exception<Dupe3>(dm, "Dupe2");
+ failures.append("Dupe2");
+ } catch (std::runtime_error &) {}
+ try {
+ dm.def("DupeException", []() { return 30; });
+ failures.append("DupeException1");
+ } catch (std::runtime_error &) {}
+ try {
+ py::class_<DupeException>(dm, "DupeException");
+ failures.append("DupeException2");
+ } catch (std::runtime_error &) {}
+
+ return failures;
+ });
+}
diff --git a/ext/pybind11/tests/test_modules.py b/ext/pybind11/tests/test_modules.py
index 69620949b..2552838c2 100644
--- a/ext/pybind11/tests/test_modules.py
+++ b/ext/pybind11/tests/test_modules.py
@@ -1,32 +1,34 @@
+from pybind11_tests import modules as m
+from pybind11_tests.modules import subsubmodule as ms
+from pybind11_tests import ConstructorStats
+
def test_nested_modules():
import pybind11_tests
- from pybind11_tests.submodule import submodule_func
-
assert pybind11_tests.__name__ == "pybind11_tests"
- assert pybind11_tests.submodule.__name__ == "pybind11_tests.submodule"
+ assert pybind11_tests.modules.__name__ == "pybind11_tests.modules"
+ assert pybind11_tests.modules.subsubmodule.__name__ == "pybind11_tests.modules.subsubmodule"
+ assert m.__name__ == "pybind11_tests.modules"
+ assert ms.__name__ == "pybind11_tests.modules.subsubmodule"
- assert submodule_func() == "submodule_func()"
+ assert ms.submodule_func() == "submodule_func()"
def test_reference_internal():
- from pybind11_tests import ConstructorStats
- from pybind11_tests.submodule import A, B
-
- b = B()
+ b = ms.B()
assert str(b.get_a1()) == "A[1]"
assert str(b.a1) == "A[1]"
assert str(b.get_a2()) == "A[2]"
assert str(b.a2) == "A[2]"
- b.a1 = A(42)
- b.a2 = A(43)
+ b.a1 = ms.A(42)
+ b.a2 = ms.A(43)
assert str(b.get_a1()) == "A[42]"
assert str(b.a1) == "A[42]"
assert str(b.get_a2()) == "A[43]"
assert str(b.a2) == "A[43]"
- astats, bstats = ConstructorStats.get(A), ConstructorStats.get(B)
+ astats, bstats = ConstructorStats.get(ms.A), ConstructorStats.get(ms.B)
assert astats.alive() == 2
assert bstats.alive() == 1
del b
@@ -47,7 +49,7 @@ def test_reference_internal():
def test_importing():
- from pybind11_tests import OD
+ from pybind11_tests.modules import OD
from collections import OrderedDict
assert OD is OrderedDict
@@ -59,4 +61,12 @@ def test_pydoc():
import pybind11_tests
import pydoc
+ assert pybind11_tests.__name__ == "pybind11_tests"
+ assert pybind11_tests.__doc__ == "pybind11 test module"
assert pydoc.text.docmodule(pybind11_tests)
+
+
+def test_duplicate_registration():
+ """Registering two things with the same name"""
+
+ assert m.duplicate_registration() == []
diff --git a/ext/pybind11/tests/test_multiple_inheritance.cpp b/ext/pybind11/tests/test_multiple_inheritance.cpp
index 3ebeb202b..35f9d9c4e 100644
--- a/ext/pybind11/tests/test_multiple_inheritance.cpp
+++ b/ext/pybind11/tests/test_multiple_inheritance.cpp
@@ -9,41 +9,83 @@
*/
#include "pybind11_tests.h"
+#include "constructor_stats.h"
-struct Base1 {
- Base1(int i) : i(i) { }
- int foo() { return i; }
+// Many bases for testing that multiple inheritance from many classes (i.e. requiring extra
+// space for holder constructed flags) works.
+template <int N> struct BaseN {
+ BaseN(int i) : i(i) { }
int i;
};
-struct Base2 {
- Base2(int i) : i(i) { }
- int bar() { return i; }
- int i;
+// test_mi_static_properties
+struct Vanilla {
+ std::string vanilla() { return "Vanilla"; };
};
-
-struct Base12 : Base1, Base2 {
- Base12(int i, int j) : Base1(i), Base2(j) { }
+struct WithStatic1 {
+ static std::string static_func1() { return "WithStatic1"; };
+ static int static_value1;
};
-
-struct MIType : Base12 {
- MIType(int i, int j) : Base12(i, j) { }
+struct WithStatic2 {
+ static std::string static_func2() { return "WithStatic2"; };
+ static int static_value2;
};
+struct VanillaStaticMix1 : Vanilla, WithStatic1, WithStatic2 {
+ static std::string static_func() { return "VanillaStaticMix1"; }
+ static int static_value;
+};
+struct VanillaStaticMix2 : WithStatic1, Vanilla, WithStatic2 {
+ static std::string static_func() { return "VanillaStaticMix2"; }
+ static int static_value;
+};
+int WithStatic1::static_value1 = 1;
+int WithStatic2::static_value2 = 2;
+int VanillaStaticMix1::static_value = 12;
+int VanillaStaticMix2::static_value = 12;
-test_initializer multiple_inheritance([](py::module &m) {
+TEST_SUBMODULE(multiple_inheritance, m) {
+
+ // test_multiple_inheritance_mix1
+ // test_multiple_inheritance_mix2
+ struct Base1 {
+ Base1(int i) : i(i) { }
+ int foo() { return i; }
+ int i;
+ };
py::class_<Base1> b1(m, "Base1");
b1.def(py::init<int>())
.def("foo", &Base1::foo);
+ struct Base2 {
+ Base2(int i) : i(i) { }
+ int bar() { return i; }
+ int i;
+ };
py::class_<Base2> b2(m, "Base2");
b2.def(py::init<int>())
.def("bar", &Base2::bar);
- py::class_<Base12, Base1, Base2>(m, "Base12");
+ // test_multiple_inheritance_cpp
+ struct Base12 : Base1, Base2 {
+ Base12(int i, int j) : Base1(i), Base2(j) { }
+ };
+ struct MIType : Base12 {
+ MIType(int i, int j) : Base12(i, j) { }
+ };
+ py::class_<Base12, Base1, Base2>(m, "Base12");
py::class_<MIType, Base12>(m, "MIType")
.def(py::init<int, int>());
+
+ // test_multiple_inheritance_python_many_bases
+ #define PYBIND11_BASEN(N) py::class_<BaseN<N>>(m, "BaseN" #N).def(py::init<int>()).def("f" #N, [](BaseN<N> &b) { return b.i + N; })
+ PYBIND11_BASEN( 1); PYBIND11_BASEN( 2); PYBIND11_BASEN( 3); PYBIND11_BASEN( 4);
+ PYBIND11_BASEN( 5); PYBIND11_BASEN( 6); PYBIND11_BASEN( 7); PYBIND11_BASEN( 8);
+ PYBIND11_BASEN( 9); PYBIND11_BASEN(10); PYBIND11_BASEN(11); PYBIND11_BASEN(12);
+ PYBIND11_BASEN(13); PYBIND11_BASEN(14); PYBIND11_BASEN(15); PYBIND11_BASEN(16);
+ PYBIND11_BASEN(17);
+
// Uncommenting this should result in a compile time failure (MI can only be specified via
// template parameters because pybind has to know the types involved; see discussion in #742 for
// details).
@@ -52,82 +94,78 @@ test_initializer multiple_inheritance([](py::module &m) {
// };
// py::class_<Base12v2>(m, "Base12v2", b1, b2)
// .def(py::init<int, int>());
-});
-
-/* Test the case where not all base classes are specified,
- and where pybind11 requires the py::multiple_inheritance
- flag to perform proper casting between types */
-
-struct Base1a {
- Base1a(int i) : i(i) { }
- int foo() { return i; }
- int i;
-};
-
-struct Base2a {
- Base2a(int i) : i(i) { }
- int bar() { return i; }
- int i;
-};
-struct Base12a : Base1a, Base2a {
- Base12a(int i, int j) : Base1a(i), Base2a(j) { }
-};
-test_initializer multiple_inheritance_nonexplicit([](py::module &m) {
+ // test_multiple_inheritance_virtbase
+ // Test the case where not all base classes are specified, and where pybind11 requires the
+ // py::multiple_inheritance flag to perform proper casting between types.
+ struct Base1a {
+ Base1a(int i) : i(i) { }
+ int foo() { return i; }
+ int i;
+ };
py::class_<Base1a, std::shared_ptr<Base1a>>(m, "Base1a")
.def(py::init<int>())
.def("foo", &Base1a::foo);
+ struct Base2a {
+ Base2a(int i) : i(i) { }
+ int bar() { return i; }
+ int i;
+ };
py::class_<Base2a, std::shared_ptr<Base2a>>(m, "Base2a")
.def(py::init<int>())
.def("bar", &Base2a::bar);
+ struct Base12a : Base1a, Base2a {
+ Base12a(int i, int j) : Base1a(i), Base2a(j) { }
+ };
py::class_<Base12a, /* Base1 missing */ Base2a,
std::shared_ptr<Base12a>>(m, "Base12a", py::multiple_inheritance())
.def(py::init<int, int>());
m.def("bar_base2a", [](Base2a *b) { return b->bar(); });
m.def("bar_base2a_sharedptr", [](std::shared_ptr<Base2a> b) { return b->bar(); });
-});
-
-struct Vanilla {
- std::string vanilla() { return "Vanilla"; };
-};
-
-struct WithStatic1 {
- static std::string static_func1() { return "WithStatic1"; };
- static int static_value1;
-};
-
-struct WithStatic2 {
- static std::string static_func2() { return "WithStatic2"; };
- static int static_value2;
-};
-
-struct WithDict { };
-
-struct VanillaStaticMix1 : Vanilla, WithStatic1, WithStatic2 {
- static std::string static_func() { return "VanillaStaticMix1"; }
- static int static_value;
-};
-
-struct VanillaStaticMix2 : WithStatic1, Vanilla, WithStatic2 {
- static std::string static_func() { return "VanillaStaticMix2"; }
- static int static_value;
-};
-
-struct VanillaDictMix1 : Vanilla, WithDict { };
-struct VanillaDictMix2 : WithDict, Vanilla { };
-
-int WithStatic1::static_value1 = 1;
-int WithStatic2::static_value2 = 2;
-int VanillaStaticMix1::static_value = 12;
-int VanillaStaticMix2::static_value = 12;
-
-test_initializer mi_static_properties([](py::module &pm) {
- auto m = pm.def_submodule("mi");
+ // test_mi_unaligned_base
+ // test_mi_base_return
+ // Issue #801: invalid casting to derived type with MI bases
+ struct I801B1 { int a = 1; virtual ~I801B1() = default; };
+ struct I801B2 { int b = 2; virtual ~I801B2() = default; };
+ struct I801C : I801B1, I801B2 {};
+ struct I801D : I801C {}; // Indirect MI
+ // Unregistered classes:
+ struct I801B3 { int c = 3; virtual ~I801B3() = default; };
+ struct I801E : I801B3, I801D {};
+
+ py::class_<I801B1, std::shared_ptr<I801B1>>(m, "I801B1").def(py::init<>()).def_readonly("a", &I801B1::a);
+ py::class_<I801B2, std::shared_ptr<I801B2>>(m, "I801B2").def(py::init<>()).def_readonly("b", &I801B2::b);
+ py::class_<I801C, I801B1, I801B2, std::shared_ptr<I801C>>(m, "I801C").def(py::init<>());
+ py::class_<I801D, I801C, std::shared_ptr<I801D>>(m, "I801D").def(py::init<>());
+
+ // Two separate issues here: first, we want to recognize a pointer to a base type as being a
+ // known instance even when the pointer value is unequal (i.e. due to a non-first
+ // multiple-inheritance base class):
+ m.def("i801b1_c", [](I801C *c) { return static_cast<I801B1 *>(c); });
+ m.def("i801b2_c", [](I801C *c) { return static_cast<I801B2 *>(c); });
+ m.def("i801b1_d", [](I801D *d) { return static_cast<I801B1 *>(d); });
+ m.def("i801b2_d", [](I801D *d) { return static_cast<I801B2 *>(d); });
+
+ // Second, when returned a base class pointer to a derived instance, we cannot assume that the
+ // pointer is `reinterpret_cast`able to the derived pointer because, like above, the base class
+ // pointer could be offset.
+ m.def("i801c_b1", []() -> I801B1 * { return new I801C(); });
+ m.def("i801c_b2", []() -> I801B2 * { return new I801C(); });
+ m.def("i801d_b1", []() -> I801B1 * { return new I801D(); });
+ m.def("i801d_b2", []() -> I801B2 * { return new I801D(); });
+
+ // Return a base class pointer to a pybind-registered type when the actual derived type
+ // isn't pybind-registered (and uses multiple-inheritance to offset the pybind base)
+ m.def("i801e_c", []() -> I801C * { return new I801E(); });
+ m.def("i801e_b2", []() -> I801B2 * { return new I801E(); });
+
+
+ // test_mi_static_properties
py::class_<Vanilla>(m, "Vanilla")
.def(py::init<>())
.def("vanilla", &Vanilla::vanilla);
@@ -154,9 +192,29 @@ test_initializer mi_static_properties([](py::module &pm) {
.def_static("static_func", &VanillaStaticMix2::static_func)
.def_readwrite_static("static_value", &VanillaStaticMix2::static_value);
+
#if !defined(PYPY_VERSION)
+ struct WithDict { };
+ struct VanillaDictMix1 : Vanilla, WithDict { };
+ struct VanillaDictMix2 : WithDict, Vanilla { };
py::class_<WithDict>(m, "WithDict", py::dynamic_attr()).def(py::init<>());
py::class_<VanillaDictMix1, Vanilla, WithDict>(m, "VanillaDictMix1").def(py::init<>());
py::class_<VanillaDictMix2, WithDict, Vanilla>(m, "VanillaDictMix2").def(py::init<>());
#endif
-});
+
+ // test_diamond_inheritance
+ // Issue #959: segfault when constructing diamond inheritance instance
+ // All of these have int members so that there will be various unequal pointers involved.
+ struct B { int b; virtual ~B() = default; };
+ struct C0 : public virtual B { int c0; };
+ struct C1 : public virtual B { int c1; };
+ struct D : public C0, public C1 { int d; };
+ py::class_<B>(m, "B")
+ .def("b", [](B *self) { return self; });
+ py::class_<C0, B>(m, "C0")
+ .def("c0", [](C0 *self) { return self; });
+ py::class_<C1, B>(m, "C1")
+ .def("c1", [](C1 *self) { return self; });
+ py::class_<D, C0, C1>(m, "D")
+ .def(py::init<>());
+}
diff --git a/ext/pybind11/tests/test_multiple_inheritance.py b/ext/pybind11/tests/test_multiple_inheritance.py
index 7aaab7cd0..475dd3b3d 100644
--- a/ext/pybind11/tests/test_multiple_inheritance.py
+++ b/ext/pybind11/tests/test_multiple_inheritance.py
@@ -1,18 +1,16 @@
import pytest
+from pybind11_tests import ConstructorStats
+from pybind11_tests import multiple_inheritance as m
def test_multiple_inheritance_cpp():
- from pybind11_tests import MIType
-
- mt = MIType(3, 4)
+ mt = m.MIType(3, 4)
assert mt.foo() == 3
assert mt.bar() == 4
def test_multiple_inheritance_mix1():
- from pybind11_tests import Base2
-
class Base1:
def __init__(self, i):
self.i = i
@@ -20,10 +18,10 @@ def test_multiple_inheritance_mix1():
def foo(self):
return self.i
- class MITypePy(Base1, Base2):
+ class MITypePy(Base1, m.Base2):
def __init__(self, i, j):
Base1.__init__(self, i)
- Base2.__init__(self, j)
+ m.Base2.__init__(self, j)
mt = MITypePy(3, 4)
@@ -32,7 +30,6 @@ def test_multiple_inheritance_mix1():
def test_multiple_inheritance_mix2():
- from pybind11_tests import Base1
class Base2:
def __init__(self, i):
@@ -41,9 +38,9 @@ def test_multiple_inheritance_mix2():
def bar(self):
return self.i
- class MITypePy(Base1, Base2):
+ class MITypePy(m.Base1, Base2):
def __init__(self, i, j):
- Base1.__init__(self, i)
+ m.Base1.__init__(self, i)
Base2.__init__(self, j)
mt = MITypePy(3, 4)
@@ -52,43 +49,198 @@ def test_multiple_inheritance_mix2():
assert mt.bar() == 4
-def test_multiple_inheritance_error():
- """Inheriting from multiple C++ bases in Python is not supported"""
- from pybind11_tests import Base1, Base2
+def test_multiple_inheritance_python():
+
+ class MI1(m.Base1, m.Base2):
+ def __init__(self, i, j):
+ m.Base1.__init__(self, i)
+ m.Base2.__init__(self, j)
+
+ class B1(object):
+ def v(self):
+ return 1
+
+ class MI2(B1, m.Base1, m.Base2):
+ def __init__(self, i, j):
+ B1.__init__(self)
+ m.Base1.__init__(self, i)
+ m.Base2.__init__(self, j)
+
+ class MI3(MI2):
+ def __init__(self, i, j):
+ MI2.__init__(self, i, j)
+
+ class MI4(MI3, m.Base2):
+ def __init__(self, i, j):
+ MI3.__init__(self, i, j)
+ # This should be ignored (Base2 is already initialized via MI2):
+ m.Base2.__init__(self, i + 100)
+
+ class MI5(m.Base2, B1, m.Base1):
+ def __init__(self, i, j):
+ B1.__init__(self)
+ m.Base1.__init__(self, i)
+ m.Base2.__init__(self, j)
+
+ class MI6(m.Base2, B1):
+ def __init__(self, i):
+ m.Base2.__init__(self, i)
+ B1.__init__(self)
+
+ class B2(B1):
+ def v(self):
+ return 2
+
+ class B3(object):
+ def v(self):
+ return 3
+
+ class B4(B3, B2):
+ def v(self):
+ return 4
+
+ class MI7(B4, MI6):
+ def __init__(self, i):
+ B4.__init__(self)
+ MI6.__init__(self, i)
+
+ class MI8(MI6, B3):
+ def __init__(self, i):
+ MI6.__init__(self, i)
+ B3.__init__(self)
- with pytest.raises(TypeError) as excinfo:
- # noinspection PyUnusedLocal
- class MI(Base1, Base2):
- pass
- assert "Can't inherit from multiple C++ classes in Python" in str(excinfo.value)
+ class MI8b(B3, MI6):
+ def __init__(self, i):
+ B3.__init__(self)
+ MI6.__init__(self, i)
+
+ mi1 = MI1(1, 2)
+ assert mi1.foo() == 1
+ assert mi1.bar() == 2
+
+ mi2 = MI2(3, 4)
+ assert mi2.v() == 1
+ assert mi2.foo() == 3
+ assert mi2.bar() == 4
+
+ mi3 = MI3(5, 6)
+ assert mi3.v() == 1
+ assert mi3.foo() == 5
+ assert mi3.bar() == 6
+
+ mi4 = MI4(7, 8)
+ assert mi4.v() == 1
+ assert mi4.foo() == 7
+ assert mi4.bar() == 8
+
+ mi5 = MI5(10, 11)
+ assert mi5.v() == 1
+ assert mi5.foo() == 10
+ assert mi5.bar() == 11
+
+ mi6 = MI6(12)
+ assert mi6.v() == 1
+ assert mi6.bar() == 12
+
+ mi7 = MI7(13)
+ assert mi7.v() == 4
+ assert mi7.bar() == 13
+
+ mi8 = MI8(14)
+ assert mi8.v() == 1
+ assert mi8.bar() == 14
+
+ mi8b = MI8b(15)
+ assert mi8b.v() == 3
+ assert mi8b.bar() == 15
+
+
+def test_multiple_inheritance_python_many_bases():
+
+ class MIMany14(m.BaseN1, m.BaseN2, m.BaseN3, m.BaseN4):
+ def __init__(self):
+ m.BaseN1.__init__(self, 1)
+ m.BaseN2.__init__(self, 2)
+ m.BaseN3.__init__(self, 3)
+ m.BaseN4.__init__(self, 4)
+
+ class MIMany58(m.BaseN5, m.BaseN6, m.BaseN7, m.BaseN8):
+ def __init__(self):
+ m.BaseN5.__init__(self, 5)
+ m.BaseN6.__init__(self, 6)
+ m.BaseN7.__init__(self, 7)
+ m.BaseN8.__init__(self, 8)
+
+ class MIMany916(m.BaseN9, m.BaseN10, m.BaseN11, m.BaseN12, m.BaseN13, m.BaseN14, m.BaseN15,
+ m.BaseN16):
+ def __init__(self):
+ m.BaseN9.__init__(self, 9)
+ m.BaseN10.__init__(self, 10)
+ m.BaseN11.__init__(self, 11)
+ m.BaseN12.__init__(self, 12)
+ m.BaseN13.__init__(self, 13)
+ m.BaseN14.__init__(self, 14)
+ m.BaseN15.__init__(self, 15)
+ m.BaseN16.__init__(self, 16)
+
+ class MIMany19(MIMany14, MIMany58, m.BaseN9):
+ def __init__(self):
+ MIMany14.__init__(self)
+ MIMany58.__init__(self)
+ m.BaseN9.__init__(self, 9)
+
+ class MIMany117(MIMany14, MIMany58, MIMany916, m.BaseN17):
+ def __init__(self):
+ MIMany14.__init__(self)
+ MIMany58.__init__(self)
+ MIMany916.__init__(self)
+ m.BaseN17.__init__(self, 17)
+
+ # Inherits from 4 registered C++ classes: can fit in one pointer on any modern arch:
+ a = MIMany14()
+ for i in range(1, 4):
+ assert getattr(a, "f" + str(i))() == 2 * i
+
+ # Inherits from 8: requires 1/2 pointers worth of holder flags on 32/64-bit arch:
+ b = MIMany916()
+ for i in range(9, 16):
+ assert getattr(b, "f" + str(i))() == 2 * i
+
+ # Inherits from 9: requires >= 2 pointers worth of holder flags
+ c = MIMany19()
+ for i in range(1, 9):
+ assert getattr(c, "f" + str(i))() == 2 * i
+
+ # Inherits from 17: requires >= 3 pointers worth of holder flags
+ d = MIMany117()
+ for i in range(1, 17):
+ assert getattr(d, "f" + str(i))() == 2 * i
def test_multiple_inheritance_virtbase():
- from pybind11_tests import Base12a, bar_base2a, bar_base2a_sharedptr
- class MITypePy(Base12a):
+ class MITypePy(m.Base12a):
def __init__(self, i, j):
- Base12a.__init__(self, i, j)
+ m.Base12a.__init__(self, i, j)
mt = MITypePy(3, 4)
assert mt.bar() == 4
- assert bar_base2a(mt) == 4
- assert bar_base2a_sharedptr(mt) == 4
+ assert m.bar_base2a(mt) == 4
+ assert m.bar_base2a_sharedptr(mt) == 4
def test_mi_static_properties():
"""Mixing bases with and without static properties should be possible
and the result should be independent of base definition order"""
- from pybind11_tests import mi
- for d in (mi.VanillaStaticMix1(), mi.VanillaStaticMix2()):
+ for d in (m.VanillaStaticMix1(), m.VanillaStaticMix2()):
assert d.vanilla() == "Vanilla"
assert d.static_func1() == "WithStatic1"
assert d.static_func2() == "WithStatic2"
assert d.static_func() == d.__class__.__name__
- mi.WithStatic1.static_value1 = 1
- mi.WithStatic2.static_value2 = 2
+ m.WithStatic1.static_value1 = 1
+ m.WithStatic2.static_value2 = 2
assert d.static_value1 == 1
assert d.static_value2 == 2
assert d.static_value == 12
@@ -104,8 +256,94 @@ def test_mi_static_properties():
@pytest.unsupported_on_pypy
def test_mi_dynamic_attributes():
"""Mixing bases with and without dynamic attribute support"""
- from pybind11_tests import mi
- for d in (mi.VanillaDictMix1(), mi.VanillaDictMix2()):
+ for d in (m.VanillaDictMix1(), m.VanillaDictMix2()):
d.dynamic = 1
assert d.dynamic == 1
+
+
+def test_mi_unaligned_base():
+ """Returning an offset (non-first MI) base class pointer should recognize the instance"""
+
+ n_inst = ConstructorStats.detail_reg_inst()
+
+ c = m.I801C()
+ d = m.I801D()
+ # + 4 below because we have the two instances, and each instance has offset base I801B2
+ assert ConstructorStats.detail_reg_inst() == n_inst + 4
+ b1c = m.i801b1_c(c)
+ assert b1c is c
+ b2c = m.i801b2_c(c)
+ assert b2c is c
+ b1d = m.i801b1_d(d)
+ assert b1d is d
+ b2d = m.i801b2_d(d)
+ assert b2d is d
+
+ assert ConstructorStats.detail_reg_inst() == n_inst + 4 # no extra instances
+ del c, b1c, b2c
+ assert ConstructorStats.detail_reg_inst() == n_inst + 2
+ del d, b1d, b2d
+ assert ConstructorStats.detail_reg_inst() == n_inst
+
+
+def test_mi_base_return():
+ """Tests returning an offset (non-first MI) base class pointer to a derived instance"""
+
+ n_inst = ConstructorStats.detail_reg_inst()
+
+ c1 = m.i801c_b1()
+ assert type(c1) is m.I801C
+ assert c1.a == 1
+ assert c1.b == 2
+
+ d1 = m.i801d_b1()
+ assert type(d1) is m.I801D
+ assert d1.a == 1
+ assert d1.b == 2
+
+ assert ConstructorStats.detail_reg_inst() == n_inst + 4
+
+ c2 = m.i801c_b2()
+ assert type(c2) is m.I801C
+ assert c2.a == 1
+ assert c2.b == 2
+
+ d2 = m.i801d_b2()
+ assert type(d2) is m.I801D
+ assert d2.a == 1
+ assert d2.b == 2
+
+ assert ConstructorStats.detail_reg_inst() == n_inst + 8
+
+ del c2
+ assert ConstructorStats.detail_reg_inst() == n_inst + 6
+ del c1, d1, d2
+ assert ConstructorStats.detail_reg_inst() == n_inst
+
+ # Returning an unregistered derived type with a registered base; we won't
+ # pick up the derived type, obviously, but should still work (as an object
+ # of whatever type was returned).
+ e1 = m.i801e_c()
+ assert type(e1) is m.I801C
+ assert e1.a == 1
+ assert e1.b == 2
+
+ e2 = m.i801e_b2()
+ assert type(e2) is m.I801B2
+ assert e2.b == 2
+
+
+def test_diamond_inheritance():
+ """Tests that diamond inheritance works as expected (issue #959)"""
+
+ # Issue #959: this shouldn't segfault:
+ d = m.D()
+
+ # Make sure all the various distinct pointers are all recognized as registered instances:
+ assert d is d.c0()
+ assert d is d.c1()
+ assert d is d.b()
+ assert d is d.c0().b()
+ assert d is d.c1().b()
+ assert d is d.c0().c1().b().c0().b()
diff --git a/ext/pybind11/tests/test_numpy_array.cpp b/ext/pybind11/tests/test_numpy_array.cpp
index cd6487249..2046c0e03 100644
--- a/ext/pybind11/tests/test_numpy_array.cpp
+++ b/ext/pybind11/tests/test_numpy_array.cpp
@@ -13,7 +13,6 @@
#include <pybind11/stl.h>
#include <cstdint>
-#include <vector>
using arr = py::array;
using arr_t = py::array_t<uint16_t, 0>;
@@ -27,39 +26,25 @@ template<typename... Ix> arr data_t(const arr_t& a, Ix... index) {
return arr(a.size() - a.index_at(index...), a.data(index...));
}
-arr& mutate_data(arr& a) {
- auto ptr = (uint8_t *) a.mutable_data();
- for (size_t i = 0; i < a.nbytes(); i++)
- ptr[i] = (uint8_t) (ptr[i] * 2);
- return a;
-}
-
-arr_t& mutate_data_t(arr_t& a) {
- auto ptr = a.mutable_data();
- for (size_t i = 0; i < a.size(); i++)
- ptr[i]++;
- return a;
-}
-
template<typename... Ix> arr& mutate_data(arr& a, Ix... index) {
auto ptr = (uint8_t *) a.mutable_data(index...);
- for (size_t i = 0; i < a.nbytes() - a.offset_at(index...); i++)
+ for (ssize_t i = 0; i < a.nbytes() - a.offset_at(index...); i++)
ptr[i] = (uint8_t) (ptr[i] * 2);
return a;
}
template<typename... Ix> arr_t& mutate_data_t(arr_t& a, Ix... index) {
auto ptr = a.mutable_data(index...);
- for (size_t i = 0; i < a.size() - a.index_at(index...); i++)
+ for (ssize_t i = 0; i < a.size() - a.index_at(index...); i++)
ptr[i]++;
return a;
}
-template<typename... Ix> size_t index_at(const arr& a, Ix... idx) { return a.index_at(idx...); }
-template<typename... Ix> size_t index_at_t(const arr_t& a, Ix... idx) { return a.index_at(idx...); }
-template<typename... Ix> size_t offset_at(const arr& a, Ix... idx) { return a.offset_at(idx...); }
-template<typename... Ix> size_t offset_at_t(const arr_t& a, Ix... idx) { return a.offset_at(idx...); }
-template<typename... Ix> size_t at_t(const arr_t& a, Ix... idx) { return a.at(idx...); }
+template<typename... Ix> ssize_t index_at(const arr& a, Ix... idx) { return a.index_at(idx...); }
+template<typename... Ix> ssize_t index_at_t(const arr_t& a, Ix... idx) { return a.index_at(idx...); }
+template<typename... Ix> ssize_t offset_at(const arr& a, Ix... idx) { return a.offset_at(idx...); }
+template<typename... Ix> ssize_t offset_at_t(const arr_t& a, Ix... idx) { return a.offset_at(idx...); }
+template<typename... Ix> ssize_t at_t(const arr_t& a, Ix... idx) { return a.at(idx...); }
template<typename... Ix> arr_t& mutate_at_t(arr_t& a, Ix... idx) { a.mutable_at(idx...)++; return a; }
#define def_index_fn(name, type) \
@@ -83,55 +68,57 @@ template <typename T, typename T2> py::handle auxiliaries(T &&r, T2 &&r2) {
return l.release();
}
-test_initializer numpy_array([](py::module &m) {
- auto sm = m.def_submodule("array");
+TEST_SUBMODULE(numpy_array, sm) {
+ try { py::module::import("numpy"); }
+ catch (...) { return; }
+ // test_array_attributes
sm.def("ndim", [](const arr& a) { return a.ndim(); });
sm.def("shape", [](const arr& a) { return arr(a.ndim(), a.shape()); });
- sm.def("shape", [](const arr& a, size_t dim) { return a.shape(dim); });
+ sm.def("shape", [](const arr& a, ssize_t dim) { return a.shape(dim); });
sm.def("strides", [](const arr& a) { return arr(a.ndim(), a.strides()); });
- sm.def("strides", [](const arr& a, size_t dim) { return a.strides(dim); });
+ sm.def("strides", [](const arr& a, ssize_t dim) { return a.strides(dim); });
sm.def("writeable", [](const arr& a) { return a.writeable(); });
sm.def("size", [](const arr& a) { return a.size(); });
sm.def("itemsize", [](const arr& a) { return a.itemsize(); });
sm.def("nbytes", [](const arr& a) { return a.nbytes(); });
sm.def("owndata", [](const arr& a) { return a.owndata(); });
- def_index_fn(data, const arr&);
- def_index_fn(data_t, const arr_t&);
+ // test_index_offset
def_index_fn(index_at, const arr&);
def_index_fn(index_at_t, const arr_t&);
def_index_fn(offset_at, const arr&);
def_index_fn(offset_at_t, const arr_t&);
+ // test_data
+ def_index_fn(data, const arr&);
+ def_index_fn(data_t, const arr_t&);
+ // test_mutate_data, test_mutate_readonly
def_index_fn(mutate_data, arr&);
def_index_fn(mutate_data_t, arr_t&);
def_index_fn(at_t, const arr_t&);
def_index_fn(mutate_at_t, arr_t&);
- sm.def("make_f_array", [] {
- return py::array_t<float>({ 2, 2 }, { 4, 8 });
- });
-
- sm.def("make_c_array", [] {
- return py::array_t<float>({ 2, 2 }, { 8, 4 });
- });
+ // test_make_c_f_array
+ sm.def("make_f_array", [] { return py::array_t<float>({ 2, 2 }, { 4, 8 }); });
+ sm.def("make_c_array", [] { return py::array_t<float>({ 2, 2 }, { 8, 4 }); });
+ // test_wrap
sm.def("wrap", [](py::array a) {
return py::array(
a.dtype(),
- std::vector<size_t>(a.shape(), a.shape() + a.ndim()),
- std::vector<size_t>(a.strides(), a.strides() + a.ndim()),
+ {a.shape(), a.shape() + a.ndim()},
+ {a.strides(), a.strides() + a.ndim()},
a.data(),
a
);
});
+ // test_numpy_view
struct ArrayClass {
int data[2] = { 1, 2 };
ArrayClass() { py::print("ArrayClass()"); }
~ArrayClass() { py::print("~ArrayClass()"); }
};
-
py::class_<ArrayClass>(sm, "ArrayClass")
.def(py::init<>())
.def("numpy_view", [](py::object &obj) {
@@ -141,16 +128,18 @@ test_initializer numpy_array([](py::module &m) {
}
);
+ // test_cast_numpy_int64_to_uint64
sm.def("function_taking_uint64", [](uint64_t) { });
+ // test_isinstance
sm.def("isinstance_untyped", [](py::object yes, py::object no) {
return py::isinstance<py::array>(yes) && !py::isinstance<py::array>(no);
});
-
sm.def("isinstance_typed", [](py::object o) {
return py::isinstance<py::array_t<double>>(o) && !py::isinstance<py::array_t<int>>(o);
});
+ // test_constructors
sm.def("default_constructors", []() {
return py::dict(
"array"_a=py::array(),
@@ -158,7 +147,6 @@ test_initializer numpy_array([](py::module &m) {
"array_t<double>"_a=py::array_t<double>()
);
});
-
sm.def("converting_constructors", [](py::object o) {
return py::dict(
"array"_a=py::array(o),
@@ -167,7 +155,7 @@ test_initializer numpy_array([](py::module &m) {
);
});
- // Overload resolution tests:
+ // test_overload_resolution
sm.def("overloaded", [](py::array_t<double>) { return "double"; });
sm.def("overloaded", [](py::array_t<float>) { return "float"; });
sm.def("overloaded", [](py::array_t<int>) { return "int"; });
@@ -195,40 +183,42 @@ test_initializer numpy_array([](py::module &m) {
sm.def("overloaded5", [](py::array_t<unsigned int>) { return "unsigned int"; });
sm.def("overloaded5", [](py::array_t<double>) { return "double"; });
+ // test_greedy_string_overload
// Issue 685: ndarray shouldn't go to std::string overload
sm.def("issue685", [](std::string) { return "string"; });
sm.def("issue685", [](py::array) { return "array"; });
sm.def("issue685", [](py::object) { return "other"; });
+ // test_array_unchecked_fixed_dims
sm.def("proxy_add2", [](py::array_t<double> a, double v) {
auto r = a.mutable_unchecked<2>();
- for (size_t i = 0; i < r.shape(0); i++)
- for (size_t j = 0; j < r.shape(1); j++)
+ for (ssize_t i = 0; i < r.shape(0); i++)
+ for (ssize_t j = 0; j < r.shape(1); j++)
r(i, j) += v;
}, py::arg().noconvert(), py::arg());
sm.def("proxy_init3", [](double start) {
py::array_t<double, py::array::c_style> a({ 3, 3, 3 });
auto r = a.mutable_unchecked<3>();
- for (size_t i = 0; i < r.shape(0); i++)
- for (size_t j = 0; j < r.shape(1); j++)
- for (size_t k = 0; k < r.shape(2); k++)
+ for (ssize_t i = 0; i < r.shape(0); i++)
+ for (ssize_t j = 0; j < r.shape(1); j++)
+ for (ssize_t k = 0; k < r.shape(2); k++)
r(i, j, k) = start++;
return a;
});
sm.def("proxy_init3F", [](double start) {
py::array_t<double, py::array::f_style> a({ 3, 3, 3 });
auto r = a.mutable_unchecked<3>();
- for (size_t k = 0; k < r.shape(2); k++)
- for (size_t j = 0; j < r.shape(1); j++)
- for (size_t i = 0; i < r.shape(0); i++)
+ for (ssize_t k = 0; k < r.shape(2); k++)
+ for (ssize_t j = 0; j < r.shape(1); j++)
+ for (ssize_t i = 0; i < r.shape(0); i++)
r(i, j, k) = start++;
return a;
});
sm.def("proxy_squared_L2_norm", [](py::array_t<double> a) {
auto r = a.unchecked<1>();
double sumsq = 0;
- for (size_t i = 0; i < r.shape(0); i++)
+ for (ssize_t i = 0; i < r.shape(0); i++)
sumsq += r[i] * r(i); // Either notation works for a 1D array
return sumsq;
});
@@ -239,21 +229,22 @@ test_initializer numpy_array([](py::module &m) {
return auxiliaries(r, r2);
});
+ // test_array_unchecked_dyn_dims
// Same as the above, but without a compile-time dimensions specification:
sm.def("proxy_add2_dyn", [](py::array_t<double> a, double v) {
auto r = a.mutable_unchecked();
if (r.ndim() != 2) throw std::domain_error("error: ndim != 2");
- for (size_t i = 0; i < r.shape(0); i++)
- for (size_t j = 0; j < r.shape(1); j++)
+ for (ssize_t i = 0; i < r.shape(0); i++)
+ for (ssize_t j = 0; j < r.shape(1); j++)
r(i, j) += v;
}, py::arg().noconvert(), py::arg());
sm.def("proxy_init3_dyn", [](double start) {
py::array_t<double, py::array::c_style> a({ 3, 3, 3 });
auto r = a.mutable_unchecked();
if (r.ndim() != 3) throw std::domain_error("error: ndim != 3");
- for (size_t i = 0; i < r.shape(0); i++)
- for (size_t j = 0; j < r.shape(1); j++)
- for (size_t k = 0; k < r.shape(2); k++)
+ for (ssize_t i = 0; i < r.shape(0); i++)
+ for (ssize_t j = 0; j < r.shape(1); j++)
+ for (ssize_t k = 0; k < r.shape(2); k++)
r(i, j, k) = start++;
return a;
});
@@ -264,4 +255,41 @@ test_initializer numpy_array([](py::module &m) {
sm.def("array_auxiliaries2", [](py::array_t<double> a) {
return auxiliaries(a, a);
});
-});
+
+ // test_array_failures
+ // Issue #785: Uninformative "Unknown internal error" exception when constructing array from empty object:
+ sm.def("array_fail_test", []() { return py::array(py::object()); });
+ sm.def("array_t_fail_test", []() { return py::array_t<double>(py::object()); });
+ // Make sure the error from numpy is being passed through:
+ sm.def("array_fail_test_negative_size", []() { int c = 0; return py::array(-1, &c); });
+
+ // test_initializer_list
+ // Issue (unnumbered; reported in #788): regression: initializer lists can be ambiguous
+ sm.def("array_initializer_list1", []() { return py::array_t<float>(1); }); // { 1 } also works, but clang warns about it
+ sm.def("array_initializer_list2", []() { return py::array_t<float>({ 1, 2 }); });
+ sm.def("array_initializer_list3", []() { return py::array_t<float>({ 1, 2, 3 }); });
+ sm.def("array_initializer_list4", []() { return py::array_t<float>({ 1, 2, 3, 4 }); });
+
+ // test_array_resize
+ // reshape array to 2D without changing size
+ sm.def("array_reshape2", [](py::array_t<double> a) {
+ const ssize_t dim_sz = (ssize_t)std::sqrt(a.size());
+ if (dim_sz * dim_sz != a.size())
+ throw std::domain_error("array_reshape2: input array total size is not a squared integer");
+ a.resize({dim_sz, dim_sz});
+ });
+
+ // resize to 3D array with each dimension = N
+ sm.def("array_resize3", [](py::array_t<double> a, size_t N, bool refcheck) {
+ a.resize({N, N, N}, refcheck);
+ });
+
+ // test_array_create_and_resize
+ // return 2D array with Nrows = Ncols = N
+ sm.def("create_and_resize", [](size_t N) {
+ py::array_t<double> a;
+ a.resize({N, N});
+ std::fill(a.mutable_data(), a.mutable_data() + a.size(), 42.);
+ return a;
+ });
+}
diff --git a/ext/pybind11/tests/test_numpy_array.py b/ext/pybind11/tests/test_numpy_array.py
index 14c25b371..27433934f 100644
--- a/ext/pybind11/tests/test_numpy_array.py
+++ b/ext/pybind11/tests/test_numpy_array.py
@@ -1,4 +1,5 @@
import pytest
+from pybind11_tests import numpy_array as m
pytestmark = pytest.requires_numpy
@@ -12,62 +13,55 @@ def arr():
def test_array_attributes():
- from pybind11_tests.array import (
- ndim, shape, strides, writeable, size, itemsize, nbytes, owndata
- )
-
a = np.array(0, 'f8')
- assert ndim(a) == 0
- assert all(shape(a) == [])
- assert all(strides(a) == [])
+ assert m.ndim(a) == 0
+ assert all(m.shape(a) == [])
+ assert all(m.strides(a) == [])
with pytest.raises(IndexError) as excinfo:
- shape(a, 0)
+ m.shape(a, 0)
assert str(excinfo.value) == 'invalid axis: 0 (ndim = 0)'
with pytest.raises(IndexError) as excinfo:
- strides(a, 0)
+ m.strides(a, 0)
assert str(excinfo.value) == 'invalid axis: 0 (ndim = 0)'
- assert writeable(a)
- assert size(a) == 1
- assert itemsize(a) == 8
- assert nbytes(a) == 8
- assert owndata(a)
+ assert m.writeable(a)
+ assert m.size(a) == 1
+ assert m.itemsize(a) == 8
+ assert m.nbytes(a) == 8
+ assert m.owndata(a)
a = np.array([[1, 2, 3], [4, 5, 6]], 'u2').view()
a.flags.writeable = False
- assert ndim(a) == 2
- assert all(shape(a) == [2, 3])
- assert shape(a, 0) == 2
- assert shape(a, 1) == 3
- assert all(strides(a) == [6, 2])
- assert strides(a, 0) == 6
- assert strides(a, 1) == 2
+ assert m.ndim(a) == 2
+ assert all(m.shape(a) == [2, 3])
+ assert m.shape(a, 0) == 2
+ assert m.shape(a, 1) == 3
+ assert all(m.strides(a) == [6, 2])
+ assert m.strides(a, 0) == 6
+ assert m.strides(a, 1) == 2
with pytest.raises(IndexError) as excinfo:
- shape(a, 2)
+ m.shape(a, 2)
assert str(excinfo.value) == 'invalid axis: 2 (ndim = 2)'
with pytest.raises(IndexError) as excinfo:
- strides(a, 2)
+ m.strides(a, 2)
assert str(excinfo.value) == 'invalid axis: 2 (ndim = 2)'
- assert not writeable(a)
- assert size(a) == 6
- assert itemsize(a) == 2
- assert nbytes(a) == 12
- assert not owndata(a)
+ assert not m.writeable(a)
+ assert m.size(a) == 6
+ assert m.itemsize(a) == 2
+ assert m.nbytes(a) == 12
+ assert not m.owndata(a)
@pytest.mark.parametrize('args, ret', [([], 0), ([0], 0), ([1], 3), ([0, 1], 1), ([1, 2], 5)])
def test_index_offset(arr, args, ret):
- from pybind11_tests.array import index_at, index_at_t, offset_at, offset_at_t
- assert index_at(arr, *args) == ret
- assert index_at_t(arr, *args) == ret
- assert offset_at(arr, *args) == ret * arr.dtype.itemsize
- assert offset_at_t(arr, *args) == ret * arr.dtype.itemsize
+ assert m.index_at(arr, *args) == ret
+ assert m.index_at_t(arr, *args) == ret
+ assert m.offset_at(arr, *args) == ret * arr.dtype.itemsize
+ assert m.offset_at_t(arr, *args) == ret * arr.dtype.itemsize
def test_dim_check_fail(arr):
- from pybind11_tests.array import (index_at, index_at_t, offset_at, offset_at_t, data, data_t,
- mutate_data, mutate_data_t)
- for func in (index_at, index_at_t, offset_at, offset_at_t, data, data_t,
- mutate_data, mutate_data_t):
+ for func in (m.index_at, m.index_at_t, m.offset_at, m.offset_at_t, m.data, m.data_t,
+ m.mutate_data, m.mutate_data_t):
with pytest.raises(IndexError) as excinfo:
func(arr, 1, 2, 3)
assert str(excinfo.value) == 'too many indices for an array: 3 (ndim = 2)'
@@ -79,63 +73,53 @@ def test_dim_check_fail(arr):
([0, 1], [2, 3, 4, 5, 6]),
([1, 2], [6])])
def test_data(arr, args, ret):
- from pybind11_tests.array import data, data_t
from sys import byteorder
- assert all(data_t(arr, *args) == ret)
- assert all(data(arr, *args)[(0 if byteorder == 'little' else 1)::2] == ret)
- assert all(data(arr, *args)[(1 if byteorder == 'little' else 0)::2] == 0)
-
-
-def test_mutate_readonly(arr):
- from pybind11_tests.array import mutate_data, mutate_data_t, mutate_at_t
- arr.flags.writeable = False
- for func, args in (mutate_data, ()), (mutate_data_t, ()), (mutate_at_t, (0, 0)):
- with pytest.raises(ValueError) as excinfo:
- func(arr, *args)
- assert str(excinfo.value) == 'array is not writeable'
+ assert all(m.data_t(arr, *args) == ret)
+ assert all(m.data(arr, *args)[(0 if byteorder == 'little' else 1)::2] == ret)
+ assert all(m.data(arr, *args)[(1 if byteorder == 'little' else 0)::2] == 0)
@pytest.mark.parametrize('dim', [0, 1, 3])
def test_at_fail(arr, dim):
- from pybind11_tests.array import at_t, mutate_at_t
- for func in at_t, mutate_at_t:
+ for func in m.at_t, m.mutate_at_t:
with pytest.raises(IndexError) as excinfo:
func(arr, *([0] * dim))
assert str(excinfo.value) == 'index dimension mismatch: {} (ndim = 2)'.format(dim)
def test_at(arr):
- from pybind11_tests.array import at_t, mutate_at_t
+ assert m.at_t(arr, 0, 2) == 3
+ assert m.at_t(arr, 1, 0) == 4
- assert at_t(arr, 0, 2) == 3
- assert at_t(arr, 1, 0) == 4
+ assert all(m.mutate_at_t(arr, 0, 2).ravel() == [1, 2, 4, 4, 5, 6])
+ assert all(m.mutate_at_t(arr, 1, 0).ravel() == [1, 2, 4, 5, 5, 6])
- assert all(mutate_at_t(arr, 0, 2).ravel() == [1, 2, 4, 4, 5, 6])
- assert all(mutate_at_t(arr, 1, 0).ravel() == [1, 2, 4, 5, 5, 6])
+def test_mutate_readonly(arr):
+ arr.flags.writeable = False
+ for func, args in (m.mutate_data, ()), (m.mutate_data_t, ()), (m.mutate_at_t, (0, 0)):
+ with pytest.raises(ValueError) as excinfo:
+ func(arr, *args)
+ assert str(excinfo.value) == 'array is not writeable'
-def test_mutate_data(arr):
- from pybind11_tests.array import mutate_data, mutate_data_t
- assert all(mutate_data(arr).ravel() == [2, 4, 6, 8, 10, 12])
- assert all(mutate_data(arr).ravel() == [4, 8, 12, 16, 20, 24])
- assert all(mutate_data(arr, 1).ravel() == [4, 8, 12, 32, 40, 48])
- assert all(mutate_data(arr, 0, 1).ravel() == [4, 16, 24, 64, 80, 96])
- assert all(mutate_data(arr, 1, 2).ravel() == [4, 16, 24, 64, 80, 192])
+def test_mutate_data(arr):
+ assert all(m.mutate_data(arr).ravel() == [2, 4, 6, 8, 10, 12])
+ assert all(m.mutate_data(arr).ravel() == [4, 8, 12, 16, 20, 24])
+ assert all(m.mutate_data(arr, 1).ravel() == [4, 8, 12, 32, 40, 48])
+ assert all(m.mutate_data(arr, 0, 1).ravel() == [4, 16, 24, 64, 80, 96])
+ assert all(m.mutate_data(arr, 1, 2).ravel() == [4, 16, 24, 64, 80, 192])
- assert all(mutate_data_t(arr).ravel() == [5, 17, 25, 65, 81, 193])
- assert all(mutate_data_t(arr).ravel() == [6, 18, 26, 66, 82, 194])
- assert all(mutate_data_t(arr, 1).ravel() == [6, 18, 26, 67, 83, 195])
- assert all(mutate_data_t(arr, 0, 1).ravel() == [6, 19, 27, 68, 84, 196])
- assert all(mutate_data_t(arr, 1, 2).ravel() == [6, 19, 27, 68, 84, 197])
+ assert all(m.mutate_data_t(arr).ravel() == [5, 17, 25, 65, 81, 193])
+ assert all(m.mutate_data_t(arr).ravel() == [6, 18, 26, 66, 82, 194])
+ assert all(m.mutate_data_t(arr, 1).ravel() == [6, 18, 26, 67, 83, 195])
+ assert all(m.mutate_data_t(arr, 0, 1).ravel() == [6, 19, 27, 68, 84, 196])
+ assert all(m.mutate_data_t(arr, 1, 2).ravel() == [6, 19, 27, 68, 84, 197])
def test_bounds_check(arr):
- from pybind11_tests.array import (index_at, index_at_t, data, data_t,
- mutate_data, mutate_data_t, at_t, mutate_at_t)
- funcs = (index_at, index_at_t, data, data_t,
- mutate_data, mutate_data_t, at_t, mutate_at_t)
- for func in funcs:
+ for func in (m.index_at, m.index_at_t, m.data, m.data_t,
+ m.mutate_data, m.mutate_data_t, m.at_t, m.mutate_at_t):
with pytest.raises(IndexError) as excinfo:
func(arr, 2, 0)
assert str(excinfo.value) == 'index 2 is out of bounds for axis 0 with size 2'
@@ -145,18 +129,13 @@ def test_bounds_check(arr):
def test_make_c_f_array():
- from pybind11_tests.array import (
- make_c_array, make_f_array
- )
- assert make_c_array().flags.c_contiguous
- assert not make_c_array().flags.f_contiguous
- assert make_f_array().flags.f_contiguous
- assert not make_f_array().flags.c_contiguous
+ assert m.make_c_array().flags.c_contiguous
+ assert not m.make_c_array().flags.f_contiguous
+ assert m.make_f_array().flags.f_contiguous
+ assert not m.make_f_array().flags.c_contiguous
def test_wrap():
- from pybind11_tests.array import wrap
-
def assert_references(a, b, base=None):
if base is None:
base = a
@@ -178,36 +157,39 @@ def test_wrap():
a1 = np.array([1, 2], dtype=np.int16)
assert a1.flags.owndata and a1.base is None
- a2 = wrap(a1)
+ a2 = m.wrap(a1)
assert_references(a1, a2)
a1 = np.array([[1, 2], [3, 4]], dtype=np.float32, order='F')
assert a1.flags.owndata and a1.base is None
- a2 = wrap(a1)
+ a2 = m.wrap(a1)
assert_references(a1, a2)
a1 = np.array([[1, 2], [3, 4]], dtype=np.float32, order='C')
a1.flags.writeable = False
- a2 = wrap(a1)
+ a2 = m.wrap(a1)
assert_references(a1, a2)
a1 = np.random.random((4, 4, 4))
- a2 = wrap(a1)
+ a2 = m.wrap(a1)
assert_references(a1, a2)
a1t = a1.transpose()
- a2 = wrap(a1t)
+ a2 = m.wrap(a1t)
assert_references(a1t, a2, a1)
a1d = a1.diagonal()
- a2 = wrap(a1d)
+ a2 = m.wrap(a1d)
assert_references(a1d, a2, a1)
+ a1m = a1[::-1, ::-1, ::-1]
+ a2 = m.wrap(a1m)
+ assert_references(a1m, a2, a1)
+
def test_numpy_view(capture):
- from pybind11_tests.array import ArrayClass
with capture:
- ac = ArrayClass()
+ ac = m.ArrayClass()
ac_view_1 = ac.numpy_view()
ac_view_2 = ac.numpy_view()
assert np.all(ac_view_1 == np.array([1, 2], dtype=np.int32))
@@ -234,29 +216,24 @@ def test_numpy_view(capture):
@pytest.unsupported_on_pypy
def test_cast_numpy_int64_to_uint64():
- from pybind11_tests.array import function_taking_uint64
- function_taking_uint64(123)
- function_taking_uint64(np.uint64(123))
+ m.function_taking_uint64(123)
+ m.function_taking_uint64(np.uint64(123))
def test_isinstance():
- from pybind11_tests.array import isinstance_untyped, isinstance_typed
-
- assert isinstance_untyped(np.array([1, 2, 3]), "not an array")
- assert isinstance_typed(np.array([1.0, 2.0, 3.0]))
+ assert m.isinstance_untyped(np.array([1, 2, 3]), "not an array")
+ assert m.isinstance_typed(np.array([1.0, 2.0, 3.0]))
def test_constructors():
- from pybind11_tests.array import default_constructors, converting_constructors
-
- defaults = default_constructors()
+ defaults = m.default_constructors()
for a in defaults.values():
assert a.size == 0
assert defaults["array"].dtype == np.array([]).dtype
assert defaults["array_t<int32>"].dtype == np.int32
assert defaults["array_t<double>"].dtype == np.float64
- results = converting_constructors([1, 2, 3])
+ results = m.converting_constructors([1, 2, 3])
for a in results.values():
np.testing.assert_array_equal(a, [1, 2, 3])
assert results["array"].dtype == np.int_
@@ -265,22 +242,20 @@ def test_constructors():
def test_overload_resolution(msg):
- from pybind11_tests.array import overloaded, overloaded2, overloaded3, overloaded4, overloaded5
-
# Exact overload matches:
- assert overloaded(np.array([1], dtype='float64')) == 'double'
- assert overloaded(np.array([1], dtype='float32')) == 'float'
- assert overloaded(np.array([1], dtype='ushort')) == 'unsigned short'
- assert overloaded(np.array([1], dtype='intc')) == 'int'
- assert overloaded(np.array([1], dtype='longlong')) == 'long long'
- assert overloaded(np.array([1], dtype='complex')) == 'double complex'
- assert overloaded(np.array([1], dtype='csingle')) == 'float complex'
+ assert m.overloaded(np.array([1], dtype='float64')) == 'double'
+ assert m.overloaded(np.array([1], dtype='float32')) == 'float'
+ assert m.overloaded(np.array([1], dtype='ushort')) == 'unsigned short'
+ assert m.overloaded(np.array([1], dtype='intc')) == 'int'
+ assert m.overloaded(np.array([1], dtype='longlong')) == 'long long'
+ assert m.overloaded(np.array([1], dtype='complex')) == 'double complex'
+ assert m.overloaded(np.array([1], dtype='csingle')) == 'float complex'
# No exact match, should call first convertible version:
- assert overloaded(np.array([1], dtype='uint8')) == 'double'
+ assert m.overloaded(np.array([1], dtype='uint8')) == 'double'
with pytest.raises(TypeError) as excinfo:
- overloaded("not an array")
+ m.overloaded("not an array")
assert msg(excinfo.value) == """
overloaded(): incompatible function arguments. The following argument types are supported:
1. (arg0: numpy.ndarray[float64]) -> str
@@ -294,14 +269,14 @@ def test_overload_resolution(msg):
Invoked with: 'not an array'
"""
- assert overloaded2(np.array([1], dtype='float64')) == 'double'
- assert overloaded2(np.array([1], dtype='float32')) == 'float'
- assert overloaded2(np.array([1], dtype='complex64')) == 'float complex'
- assert overloaded2(np.array([1], dtype='complex128')) == 'double complex'
- assert overloaded2(np.array([1], dtype='float32')) == 'float'
+ assert m.overloaded2(np.array([1], dtype='float64')) == 'double'
+ assert m.overloaded2(np.array([1], dtype='float32')) == 'float'
+ assert m.overloaded2(np.array([1], dtype='complex64')) == 'float complex'
+ assert m.overloaded2(np.array([1], dtype='complex128')) == 'double complex'
+ assert m.overloaded2(np.array([1], dtype='float32')) == 'float'
- assert overloaded3(np.array([1], dtype='float64')) == 'double'
- assert overloaded3(np.array([1], dtype='intc')) == 'int'
+ assert m.overloaded3(np.array([1], dtype='float64')) == 'double'
+ assert m.overloaded3(np.array([1], dtype='intc')) == 'int'
expected_exc = """
overloaded3(): incompatible function arguments. The following argument types are supported:
1. (arg0: numpy.ndarray[int32]) -> str
@@ -310,70 +285,118 @@ def test_overload_resolution(msg):
Invoked with:"""
with pytest.raises(TypeError) as excinfo:
- overloaded3(np.array([1], dtype='uintc'))
+ m.overloaded3(np.array([1], dtype='uintc'))
assert msg(excinfo.value) == expected_exc + " array([1], dtype=uint32)"
with pytest.raises(TypeError) as excinfo:
- overloaded3(np.array([1], dtype='float32'))
+ m.overloaded3(np.array([1], dtype='float32'))
assert msg(excinfo.value) == expected_exc + " array([ 1.], dtype=float32)"
with pytest.raises(TypeError) as excinfo:
- overloaded3(np.array([1], dtype='complex'))
+ m.overloaded3(np.array([1], dtype='complex'))
assert msg(excinfo.value) == expected_exc + " array([ 1.+0.j])"
# Exact matches:
- assert overloaded4(np.array([1], dtype='double')) == 'double'
- assert overloaded4(np.array([1], dtype='longlong')) == 'long long'
+ assert m.overloaded4(np.array([1], dtype='double')) == 'double'
+ assert m.overloaded4(np.array([1], dtype='longlong')) == 'long long'
# Non-exact matches requiring conversion. Since float to integer isn't a
# save conversion, it should go to the double overload, but short can go to
# either (and so should end up on the first-registered, the long long).
- assert overloaded4(np.array([1], dtype='float32')) == 'double'
- assert overloaded4(np.array([1], dtype='short')) == 'long long'
+ assert m.overloaded4(np.array([1], dtype='float32')) == 'double'
+ assert m.overloaded4(np.array([1], dtype='short')) == 'long long'
- assert overloaded5(np.array([1], dtype='double')) == 'double'
- assert overloaded5(np.array([1], dtype='uintc')) == 'unsigned int'
- assert overloaded5(np.array([1], dtype='float32')) == 'unsigned int'
+ assert m.overloaded5(np.array([1], dtype='double')) == 'double'
+ assert m.overloaded5(np.array([1], dtype='uintc')) == 'unsigned int'
+ assert m.overloaded5(np.array([1], dtype='float32')) == 'unsigned int'
-def test_greedy_string_overload(): # issue 685
- from pybind11_tests.array import issue685
+def test_greedy_string_overload():
+ """Tests fix for #685 - ndarray shouldn't go to std::string overload"""
- assert issue685("abc") == "string"
- assert issue685(np.array([97, 98, 99], dtype='b')) == "array"
- assert issue685(123) == "other"
+ assert m.issue685("abc") == "string"
+ assert m.issue685(np.array([97, 98, 99], dtype='b')) == "array"
+ assert m.issue685(123) == "other"
def test_array_unchecked_fixed_dims(msg):
- from pybind11_tests.array import (proxy_add2, proxy_init3F, proxy_init3, proxy_squared_L2_norm,
- proxy_auxiliaries2, array_auxiliaries2)
-
z1 = np.array([[1, 2], [3, 4]], dtype='float64')
- proxy_add2(z1, 10)
+ m.proxy_add2(z1, 10)
assert np.all(z1 == [[11, 12], [13, 14]])
with pytest.raises(ValueError) as excinfo:
- proxy_add2(np.array([1., 2, 3]), 5.0)
+ m.proxy_add2(np.array([1., 2, 3]), 5.0)
assert msg(excinfo.value) == "array has incorrect number of dimensions: 1; expected 2"
expect_c = np.ndarray(shape=(3, 3, 3), buffer=np.array(range(3, 30)), dtype='int')
- assert np.all(proxy_init3(3.0) == expect_c)
+ assert np.all(m.proxy_init3(3.0) == expect_c)
expect_f = np.transpose(expect_c)
- assert np.all(proxy_init3F(3.0) == expect_f)
+ assert np.all(m.proxy_init3F(3.0) == expect_f)
- assert proxy_squared_L2_norm(np.array(range(6))) == 55
- assert proxy_squared_L2_norm(np.array(range(6), dtype="float64")) == 55
+ assert m.proxy_squared_L2_norm(np.array(range(6))) == 55
+ assert m.proxy_squared_L2_norm(np.array(range(6), dtype="float64")) == 55
- assert proxy_auxiliaries2(z1) == [11, 11, True, 2, 8, 2, 2, 4, 32]
- assert proxy_auxiliaries2(z1) == array_auxiliaries2(z1)
+ assert m.proxy_auxiliaries2(z1) == [11, 11, True, 2, 8, 2, 2, 4, 32]
+ assert m.proxy_auxiliaries2(z1) == m.array_auxiliaries2(z1)
def test_array_unchecked_dyn_dims(msg):
- from pybind11_tests.array import (proxy_add2_dyn, proxy_init3_dyn, proxy_auxiliaries2_dyn,
- array_auxiliaries2)
z1 = np.array([[1, 2], [3, 4]], dtype='float64')
- proxy_add2_dyn(z1, 10)
+ m.proxy_add2_dyn(z1, 10)
assert np.all(z1 == [[11, 12], [13, 14]])
expect_c = np.ndarray(shape=(3, 3, 3), buffer=np.array(range(3, 30)), dtype='int')
- assert np.all(proxy_init3_dyn(3.0) == expect_c)
+ assert np.all(m.proxy_init3_dyn(3.0) == expect_c)
+
+ assert m.proxy_auxiliaries2_dyn(z1) == [11, 11, True, 2, 8, 2, 2, 4, 32]
+ assert m.proxy_auxiliaries2_dyn(z1) == m.array_auxiliaries2(z1)
- assert proxy_auxiliaries2_dyn(z1) == [11, 11, True, 2, 8, 2, 2, 4, 32]
- assert proxy_auxiliaries2_dyn(z1) == array_auxiliaries2(z1)
+
+def test_array_failure():
+ with pytest.raises(ValueError) as excinfo:
+ m.array_fail_test()
+ assert str(excinfo.value) == 'cannot create a pybind11::array from a nullptr'
+
+ with pytest.raises(ValueError) as excinfo:
+ m.array_t_fail_test()
+ assert str(excinfo.value) == 'cannot create a pybind11::array_t from a nullptr'
+
+ with pytest.raises(ValueError) as excinfo:
+ m.array_fail_test_negative_size()
+ assert str(excinfo.value) == 'negative dimensions are not allowed'
+
+
+def test_initializer_list():
+ assert m.array_initializer_list1().shape == (1,)
+ assert m.array_initializer_list2().shape == (1, 2)
+ assert m.array_initializer_list3().shape == (1, 2, 3)
+ assert m.array_initializer_list4().shape == (1, 2, 3, 4)
+
+
+def test_array_resize(msg):
+ a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype='float64')
+ m.array_reshape2(a)
+ assert(a.size == 9)
+ assert(np.all(a == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]))
+
+ # total size change should succced with refcheck off
+ m.array_resize3(a, 4, False)
+ assert(a.size == 64)
+ # ... and fail with refcheck on
+ try:
+ m.array_resize3(a, 3, True)
+ except ValueError as e:
+ assert(str(e).startswith("cannot resize an array"))
+ # transposed array doesn't own data
+ b = a.transpose()
+ try:
+ m.array_resize3(b, 3, False)
+ except ValueError as e:
+ assert(str(e).startswith("cannot resize this array: it does not own its data"))
+ # ... but reshape should be fine
+ m.array_reshape2(b)
+ assert(b.shape == (8, 8))
+
+
+@pytest.unsupported_on_pypy
+def test_array_create_and_resize(msg):
+ a = m.create_and_resize(2)
+ assert(a.size == 4)
+ assert(np.all(a == 42.))
diff --git a/ext/pybind11/tests/test_numpy_dtypes.cpp b/ext/pybind11/tests/test_numpy_dtypes.cpp
index 1f6c85704..ddec851f6 100644
--- a/ext/pybind11/tests/test_numpy_dtypes.cpp
+++ b/ext/pybind11/tests/test_numpy_dtypes.cpp
@@ -70,6 +70,22 @@ struct StringStruct {
std::array<char, 3> b;
};
+struct ComplexStruct {
+ std::complex<float> cflt;
+ std::complex<double> cdbl;
+};
+
+std::ostream& operator<<(std::ostream& os, const ComplexStruct& v) {
+ return os << "c:" << v.cflt << "," << v.cdbl;
+}
+
+struct ArrayStruct {
+ char a[3][4];
+ int32_t b[2];
+ std::array<uint8_t, 3> c;
+ std::array<float, 2> d[4];
+};
+
PYBIND11_PACKED(struct StructWithUglyNames {
int8_t __x__;
uint64_t __y__;
@@ -91,6 +107,27 @@ std::ostream& operator<<(std::ostream& os, const StringStruct& v) {
return os << "'";
}
+std::ostream& operator<<(std::ostream& os, const ArrayStruct& v) {
+ os << "a={";
+ for (int i = 0; i < 3; i++) {
+ if (i > 0)
+ os << ',';
+ os << '{';
+ for (int j = 0; j < 3; j++)
+ os << v.a[i][j] << ',';
+ os << v.a[i][3] << '}';
+ }
+ os << "},b={" << v.b[0] << ',' << v.b[1];
+ os << "},c={" << int(v.c[0]) << ',' << int(v.c[1]) << ',' << int(v.c[2]);
+ os << "},d={";
+ for (int i = 0; i < 4; i++) {
+ if (i > 0)
+ os << ',';
+ os << '{' << v.d[i][0] << ',' << v.d[i][1] << '}';
+ }
+ return os << '}';
+}
+
std::ostream& operator<<(std::ostream& os, const EnumStruct& v) {
return os << "e1=" << (v.e1 == E1::A ? "A" : "B") << ",e2=" << (v.e2 == E2::X ? "X" : "Y");
}
@@ -119,66 +156,12 @@ py::array_t<S, 0> create_recarray(size_t n) {
return arr;
}
-std::string get_format_unbound() {
- return py::format_descriptor<UnboundStruct>::format();
-}
-
-py::array_t<NestedStruct, 0> create_nested(size_t n) {
- auto arr = mkarray_via_buffer<NestedStruct>(n);
- auto req = arr.request();
- auto ptr = static_cast<NestedStruct*>(req.ptr);
- for (size_t i = 0; i < n; i++) {
- SET_TEST_VALS(ptr[i].a, i);
- SET_TEST_VALS(ptr[i].b, i + 1);
- }
- return arr;
-}
-
-py::array_t<PartialNestedStruct, 0> create_partial_nested(size_t n) {
- auto arr = mkarray_via_buffer<PartialNestedStruct>(n);
- auto req = arr.request();
- auto ptr = static_cast<PartialNestedStruct*>(req.ptr);
- for (size_t i = 0; i < n; i++) {
- SET_TEST_VALS(ptr[i].a, i);
- }
- return arr;
-}
-
-py::array_t<StringStruct, 0> create_string_array(bool non_empty) {
- auto arr = mkarray_via_buffer<StringStruct>(non_empty ? 4 : 0);
- if (non_empty) {
- auto req = arr.request();
- auto ptr = static_cast<StringStruct*>(req.ptr);
- for (size_t i = 0; i < req.size * req.itemsize; i++)
- static_cast<char*>(req.ptr)[i] = 0;
- ptr[1].a[0] = 'a'; ptr[1].b[0] = 'a';
- ptr[2].a[0] = 'a'; ptr[2].b[0] = 'a';
- ptr[3].a[0] = 'a'; ptr[3].b[0] = 'a';
-
- ptr[2].a[1] = 'b'; ptr[2].b[1] = 'b';
- ptr[3].a[1] = 'b'; ptr[3].b[1] = 'b';
-
- ptr[3].a[2] = 'c'; ptr[3].b[2] = 'c';
- }
- return arr;
-}
-
-py::array_t<EnumStruct, 0> create_enum_array(size_t n) {
- auto arr = mkarray_via_buffer<EnumStruct>(n);
- auto ptr = (EnumStruct *) arr.mutable_data();
- for (size_t i = 0; i < n; i++) {
- ptr[i].e1 = static_cast<E1>(-1 + ((int) i % 2) * 2);
- ptr[i].e2 = static_cast<E2>(1 + (i % 2));
- }
- return arr;
-}
-
template <typename S>
py::list print_recarray(py::array_t<S, 0> arr) {
const auto req = arr.request();
const auto ptr = static_cast<S*>(req.ptr);
auto l = py::list();
- for (size_t i = 0; i < req.size; i++) {
+ for (ssize_t i = 0; i < req.size; i++) {
std::stringstream ss;
ss << ptr[i];
l.append(py::str(ss.str()));
@@ -186,47 +169,12 @@ py::list print_recarray(py::array_t<S, 0> arr) {
return l;
}
-py::list print_format_descriptors() {
- const auto fmts = {
- py::format_descriptor<SimpleStruct>::format(),
- py::format_descriptor<PackedStruct>::format(),
- py::format_descriptor<NestedStruct>::format(),
- py::format_descriptor<PartialStruct>::format(),
- py::format_descriptor<PartialNestedStruct>::format(),
- py::format_descriptor<StringStruct>::format(),
- py::format_descriptor<EnumStruct>::format()
- };
- auto l = py::list();
- for (const auto &fmt : fmts) {
- l.append(py::cast(fmt));
- }
- return l;
-}
-
-py::list print_dtypes() {
- const auto dtypes = {
- py::str(py::dtype::of<SimpleStruct>()),
- py::str(py::dtype::of<PackedStruct>()),
- py::str(py::dtype::of<NestedStruct>()),
- py::str(py::dtype::of<PartialStruct>()),
- py::str(py::dtype::of<PartialNestedStruct>()),
- py::str(py::dtype::of<StringStruct>()),
- py::str(py::dtype::of<EnumStruct>()),
- py::str(py::dtype::of<StructWithUglyNames>())
- };
- auto l = py::list();
- for (const auto &s : dtypes) {
- l.append(s);
- }
- return l;
-}
-
py::array_t<int32_t, 0> test_array_ctors(int i) {
using arr_t = py::array_t<int32_t, 0>;
std::vector<int32_t> data { 1, 2, 3, 4, 5, 6 };
- std::vector<size_t> shape { 3, 2 };
- std::vector<size_t> strides { 8, 4 };
+ std::vector<ssize_t> shape { 3, 2 };
+ std::vector<ssize_t> strides { 8, 4 };
auto ptr = data.data();
auto vptr = (void *) ptr;
@@ -296,51 +244,9 @@ py::list test_dtype_ctors() {
return list;
}
-struct TrailingPaddingStruct {
- int32_t a;
- char b;
-};
-
-py::dtype trailing_padding_dtype() {
- return py::dtype::of<TrailingPaddingStruct>();
-}
-
-py::dtype buffer_to_dtype(py::buffer& buf) {
- return py::dtype(buf.request());
-}
-
-py::list test_dtype_methods() {
- py::list list;
- auto dt1 = py::dtype::of<int32_t>();
- auto dt2 = py::dtype::of<SimpleStruct>();
- list.append(dt1); list.append(dt2);
- list.append(py::bool_(dt1.has_fields())); list.append(py::bool_(dt2.has_fields()));
- list.append(py::int_(dt1.itemsize())); list.append(py::int_(dt2.itemsize()));
- return list;
-}
-
-struct CompareStruct {
- bool x;
- uint32_t y;
- float z;
-};
-
-py::list test_compare_buffer_info() {
- py::list list;
- list.append(py::bool_(py::detail::compare_buffer_info<float>::compare(py::buffer_info(nullptr, sizeof(float), "f", 1))));
- list.append(py::bool_(py::detail::compare_buffer_info<unsigned>::compare(py::buffer_info(nullptr, sizeof(int), "I", 1))));
- list.append(py::bool_(py::detail::compare_buffer_info<long>::compare(py::buffer_info(nullptr, sizeof(long), "l", 1))));
- list.append(py::bool_(py::detail::compare_buffer_info<long>::compare(py::buffer_info(nullptr, sizeof(long), sizeof(long) == sizeof(int) ? "i" : "q", 1))));
- list.append(py::bool_(py::detail::compare_buffer_info<CompareStruct>::compare(py::buffer_info(nullptr, sizeof(CompareStruct), "T{?:x:3xI:y:f:z:}", 1))));
- return list;
-}
-
-test_initializer numpy_dtypes([](py::module &m) {
- try {
- py::module::import("numpy");
- } catch (...) {
- return;
- }
+TEST_SUBMODULE(numpy_dtypes, m) {
+ try { py::module::import("numpy"); }
+ catch (...) { return; }
// typeinfo may be registered before the dtype descriptor for scalar casts to work...
py::class_<SimpleStruct>(m, "SimpleStruct");
@@ -351,9 +257,9 @@ test_initializer numpy_dtypes([](py::module &m) {
PYBIND11_NUMPY_DTYPE(PartialStruct, bool_, uint_, float_, ldbl_);
PYBIND11_NUMPY_DTYPE(PartialNestedStruct, a);
PYBIND11_NUMPY_DTYPE(StringStruct, a, b);
+ PYBIND11_NUMPY_DTYPE(ArrayStruct, a, b, c, d);
PYBIND11_NUMPY_DTYPE(EnumStruct, e1, e2);
- PYBIND11_NUMPY_DTYPE(TrailingPaddingStruct, a, b);
- PYBIND11_NUMPY_DTYPE(CompareStruct, x, y, z);
+ PYBIND11_NUMPY_DTYPE(ComplexStruct, cflt, cdbl);
// ... or after
py::class_<PackedStruct>(m, "PackedStruct");
@@ -365,31 +271,181 @@ test_initializer numpy_dtypes([](py::module &m) {
// struct NotPOD { std::string v; NotPOD() : v("hi") {}; };
// PYBIND11_NUMPY_DTYPE(NotPOD, v);
+ // test_recarray, test_scalar_conversion
m.def("create_rec_simple", &create_recarray<SimpleStruct>);
m.def("create_rec_packed", &create_recarray<PackedStruct>);
- m.def("create_rec_nested", &create_nested);
+ m.def("create_rec_nested", [](size_t n) { // test_signature
+ py::array_t<NestedStruct, 0> arr = mkarray_via_buffer<NestedStruct>(n);
+ auto req = arr.request();
+ auto ptr = static_cast<NestedStruct*>(req.ptr);
+ for (size_t i = 0; i < n; i++) {
+ SET_TEST_VALS(ptr[i].a, i);
+ SET_TEST_VALS(ptr[i].b, i + 1);
+ }
+ return arr;
+ });
m.def("create_rec_partial", &create_recarray<PartialStruct>);
- m.def("create_rec_partial_nested", &create_partial_nested);
- m.def("print_format_descriptors", &print_format_descriptors);
+ m.def("create_rec_partial_nested", [](size_t n) {
+ py::array_t<PartialNestedStruct, 0> arr = mkarray_via_buffer<PartialNestedStruct>(n);
+ auto req = arr.request();
+ auto ptr = static_cast<PartialNestedStruct*>(req.ptr);
+ for (size_t i = 0; i < n; i++) {
+ SET_TEST_VALS(ptr[i].a, i);
+ }
+ return arr;
+ });
m.def("print_rec_simple", &print_recarray<SimpleStruct>);
m.def("print_rec_packed", &print_recarray<PackedStruct>);
m.def("print_rec_nested", &print_recarray<NestedStruct>);
- m.def("print_dtypes", &print_dtypes);
- m.def("get_format_unbound", &get_format_unbound);
- m.def("create_string_array", &create_string_array);
+
+ // test_format_descriptors
+ m.def("get_format_unbound", []() { return py::format_descriptor<UnboundStruct>::format(); });
+ m.def("print_format_descriptors", []() {
+ py::list l;
+ for (const auto &fmt : {
+ py::format_descriptor<SimpleStruct>::format(),
+ py::format_descriptor<PackedStruct>::format(),
+ py::format_descriptor<NestedStruct>::format(),
+ py::format_descriptor<PartialStruct>::format(),
+ py::format_descriptor<PartialNestedStruct>::format(),
+ py::format_descriptor<StringStruct>::format(),
+ py::format_descriptor<ArrayStruct>::format(),
+ py::format_descriptor<EnumStruct>::format(),
+ py::format_descriptor<ComplexStruct>::format()
+ }) {
+ l.append(py::cast(fmt));
+ }
+ return l;
+ });
+
+ // test_dtype
+ m.def("print_dtypes", []() {
+ py::list l;
+ for (const py::handle &d : {
+ py::dtype::of<SimpleStruct>(),
+ py::dtype::of<PackedStruct>(),
+ py::dtype::of<NestedStruct>(),
+ py::dtype::of<PartialStruct>(),
+ py::dtype::of<PartialNestedStruct>(),
+ py::dtype::of<StringStruct>(),
+ py::dtype::of<ArrayStruct>(),
+ py::dtype::of<EnumStruct>(),
+ py::dtype::of<StructWithUglyNames>(),
+ py::dtype::of<ComplexStruct>()
+ })
+ l.append(py::str(d));
+ return l;
+ });
+ m.def("test_dtype_ctors", &test_dtype_ctors);
+ m.def("test_dtype_methods", []() {
+ py::list list;
+ auto dt1 = py::dtype::of<int32_t>();
+ auto dt2 = py::dtype::of<SimpleStruct>();
+ list.append(dt1); list.append(dt2);
+ list.append(py::bool_(dt1.has_fields())); list.append(py::bool_(dt2.has_fields()));
+ list.append(py::int_(dt1.itemsize())); list.append(py::int_(dt2.itemsize()));
+ return list;
+ });
+ struct TrailingPaddingStruct {
+ int32_t a;
+ char b;
+ };
+ PYBIND11_NUMPY_DTYPE(TrailingPaddingStruct, a, b);
+ m.def("trailing_padding_dtype", []() { return py::dtype::of<TrailingPaddingStruct>(); });
+
+ // test_string_array
+ m.def("create_string_array", [](bool non_empty) {
+ py::array_t<StringStruct, 0> arr = mkarray_via_buffer<StringStruct>(non_empty ? 4 : 0);
+ if (non_empty) {
+ auto req = arr.request();
+ auto ptr = static_cast<StringStruct*>(req.ptr);
+ for (ssize_t i = 0; i < req.size * req.itemsize; i++)
+ static_cast<char*>(req.ptr)[i] = 0;
+ ptr[1].a[0] = 'a'; ptr[1].b[0] = 'a';
+ ptr[2].a[0] = 'a'; ptr[2].b[0] = 'a';
+ ptr[3].a[0] = 'a'; ptr[3].b[0] = 'a';
+
+ ptr[2].a[1] = 'b'; ptr[2].b[1] = 'b';
+ ptr[3].a[1] = 'b'; ptr[3].b[1] = 'b';
+
+ ptr[3].a[2] = 'c'; ptr[3].b[2] = 'c';
+ }
+ return arr;
+ });
m.def("print_string_array", &print_recarray<StringStruct>);
- m.def("create_enum_array", &create_enum_array);
+
+ // test_array_array
+ m.def("create_array_array", [](size_t n) {
+ py::array_t<ArrayStruct, 0> arr = mkarray_via_buffer<ArrayStruct>(n);
+ auto ptr = (ArrayStruct *) arr.mutable_data();
+ for (size_t i = 0; i < n; i++) {
+ for (size_t j = 0; j < 3; j++)
+ for (size_t k = 0; k < 4; k++)
+ ptr[i].a[j][k] = char('A' + (i * 100 + j * 10 + k) % 26);
+ for (size_t j = 0; j < 2; j++)
+ ptr[i].b[j] = int32_t(i * 1000 + j);
+ for (size_t j = 0; j < 3; j++)
+ ptr[i].c[j] = uint8_t(i * 10 + j);
+ for (size_t j = 0; j < 4; j++)
+ for (size_t k = 0; k < 2; k++)
+ ptr[i].d[j][k] = float(i) * 100.0f + float(j) * 10.0f + float(k);
+ }
+ return arr;
+ });
+ m.def("print_array_array", &print_recarray<ArrayStruct>);
+
+ // test_enum_array
+ m.def("create_enum_array", [](size_t n) {
+ py::array_t<EnumStruct, 0> arr = mkarray_via_buffer<EnumStruct>(n);
+ auto ptr = (EnumStruct *) arr.mutable_data();
+ for (size_t i = 0; i < n; i++) {
+ ptr[i].e1 = static_cast<E1>(-1 + ((int) i % 2) * 2);
+ ptr[i].e2 = static_cast<E2>(1 + (i % 2));
+ }
+ return arr;
+ });
m.def("print_enum_array", &print_recarray<EnumStruct>);
+
+ // test_complex_array
+ m.def("create_complex_array", [](size_t n) {
+ py::array_t<ComplexStruct, 0> arr = mkarray_via_buffer<ComplexStruct>(n);
+ auto ptr = (ComplexStruct *) arr.mutable_data();
+ for (size_t i = 0; i < n; i++) {
+ ptr[i].cflt.real(float(i));
+ ptr[i].cflt.imag(float(i) + 0.25f);
+ ptr[i].cdbl.real(double(i) + 0.5);
+ ptr[i].cdbl.imag(double(i) + 0.75);
+ }
+ return arr;
+ });
+ m.def("print_complex_array", &print_recarray<ComplexStruct>);
+
+ // test_array_constructors
m.def("test_array_ctors", &test_array_ctors);
- m.def("test_dtype_ctors", &test_dtype_ctors);
- m.def("test_dtype_methods", &test_dtype_methods);
- m.def("compare_buffer_info", &test_compare_buffer_info);
- m.def("trailing_padding_dtype", &trailing_padding_dtype);
- m.def("buffer_to_dtype", &buffer_to_dtype);
+
+ // test_compare_buffer_info
+ struct CompareStruct {
+ bool x;
+ uint32_t y;
+ float z;
+ };
+ PYBIND11_NUMPY_DTYPE(CompareStruct, x, y, z);
+ m.def("compare_buffer_info", []() {
+ py::list list;
+ list.append(py::bool_(py::detail::compare_buffer_info<float>::compare(py::buffer_info(nullptr, sizeof(float), "f", 1))));
+ list.append(py::bool_(py::detail::compare_buffer_info<unsigned>::compare(py::buffer_info(nullptr, sizeof(int), "I", 1))));
+ list.append(py::bool_(py::detail::compare_buffer_info<long>::compare(py::buffer_info(nullptr, sizeof(long), "l", 1))));
+ list.append(py::bool_(py::detail::compare_buffer_info<long>::compare(py::buffer_info(nullptr, sizeof(long), sizeof(long) == sizeof(int) ? "i" : "q", 1))));
+ list.append(py::bool_(py::detail::compare_buffer_info<CompareStruct>::compare(py::buffer_info(nullptr, sizeof(CompareStruct), "T{?:x:3xI:y:f:z:}", 1))));
+ return list;
+ });
+ m.def("buffer_to_dtype", [](py::buffer& buf) { return py::dtype(buf.request()); });
+
+ // test_scalar_conversion
m.def("f_simple", [](SimpleStruct s) { return s.uint_ * 10; });
m.def("f_packed", [](PackedStruct s) { return s.uint_ * 10; });
m.def("f_nested", [](NestedStruct s) { return s.a.uint_ * 10; });
- m.def("register_dtype", []() { PYBIND11_NUMPY_DTYPE(SimpleStruct, bool_, uint_, float_, ldbl_); });
-});
-#undef PYBIND11_PACKED
+ // test_register_dtype
+ m.def("register_dtype", []() { PYBIND11_NUMPY_DTYPE(SimpleStruct, bool_, uint_, float_, ldbl_); });
+}
diff --git a/ext/pybind11/tests/test_numpy_dtypes.py b/ext/pybind11/tests/test_numpy_dtypes.py
index f63814f9d..5f9a95404 100644
--- a/ext/pybind11/tests/test_numpy_dtypes.py
+++ b/ext/pybind11/tests/test_numpy_dtypes.py
@@ -1,5 +1,6 @@
import re
import pytest
+from pybind11_tests import numpy_dtypes as m
pytestmark = pytest.requires_numpy
@@ -65,68 +66,66 @@ def assert_equal(actual, expected_data, expected_dtype):
def test_format_descriptors():
- from pybind11_tests import get_format_unbound, print_format_descriptors
-
with pytest.raises(RuntimeError) as excinfo:
- get_format_unbound()
+ m.get_format_unbound()
assert re.match('^NumPy type info missing for .*UnboundStruct.*$', str(excinfo.value))
ld = np.dtype('longdouble')
ldbl_fmt = ('4x' if ld.alignment > 4 else '') + ld.char
- ss_fmt = "T{?:bool_:3xI:uint_:f:float_:" + ldbl_fmt + ":ldbl_:}"
+ ss_fmt = "^T{?:bool_:3xI:uint_:f:float_:" + ldbl_fmt + ":ldbl_:}"
dbl = np.dtype('double')
- partial_fmt = ("T{?:bool_:3xI:uint_:f:float_:" +
+ partial_fmt = ("^T{?:bool_:3xI:uint_:f:float_:" +
str(4 * (dbl.alignment > 4) + dbl.itemsize + 8 * (ld.alignment > 8)) +
"xg:ldbl_:}")
nested_extra = str(max(8, ld.alignment))
- assert print_format_descriptors() == [
+ assert m.print_format_descriptors() == [
ss_fmt,
- "T{?:bool_:^I:uint_:^f:float_:^g:ldbl_:}",
- "T{" + ss_fmt + ":a:T{?:bool_:^I:uint_:^f:float_:^g:ldbl_:}:b:}",
+ "^T{?:bool_:I:uint_:f:float_:g:ldbl_:}",
+ "^T{" + ss_fmt + ":a:^T{?:bool_:I:uint_:f:float_:g:ldbl_:}:b:}",
partial_fmt,
- "T{" + nested_extra + "x" + partial_fmt + ":a:" + nested_extra + "x}",
- "T{3s:a:3s:b:}",
- 'T{q:e1:B:e2:}'
+ "^T{" + nested_extra + "x" + partial_fmt + ":a:" + nested_extra + "x}",
+ "^T{3s:a:3s:b:}",
+ "^T{(3)4s:a:(2)i:b:(3)B:c:1x(4, 2)f:d:}",
+ '^T{q:e1:B:e2:}',
+ '^T{Zf:cflt:Zd:cdbl:}'
]
def test_dtype(simple_dtype):
- from pybind11_tests import (print_dtypes, test_dtype_ctors, test_dtype_methods,
- trailing_padding_dtype, buffer_to_dtype)
from sys import byteorder
e = '<' if byteorder == 'little' else '>'
- assert print_dtypes() == [
+ assert m.print_dtypes() == [
simple_dtype_fmt(),
packed_dtype_fmt(),
"[('a', {}), ('b', {})]".format(simple_dtype_fmt(), packed_dtype_fmt()),
partial_dtype_fmt(),
partial_nested_fmt(),
"[('a', 'S3'), ('b', 'S3')]",
+ ("{{'names':['a','b','c','d'], " +
+ "'formats':[('S4', (3,)),('<i4', (2,)),('u1', (3,)),('<f4', (4, 2))], " +
+ "'offsets':[0,12,20,24], 'itemsize':56}}").format(e=e),
"[('e1', '" + e + "i8'), ('e2', 'u1')]",
- "[('x', 'i1'), ('y', '" + e + "u8')]"
+ "[('x', 'i1'), ('y', '" + e + "u8')]",
+ "[('cflt', '" + e + "c8'), ('cdbl', '" + e + "c16')]"
]
d1 = np.dtype({'names': ['a', 'b'], 'formats': ['int32', 'float64'],
'offsets': [1, 10], 'itemsize': 20})
d2 = np.dtype([('a', 'i4'), ('b', 'f4')])
- assert test_dtype_ctors() == [np.dtype('int32'), np.dtype('float64'),
- np.dtype('bool'), d1, d1, np.dtype('uint32'), d2]
+ assert m.test_dtype_ctors() == [np.dtype('int32'), np.dtype('float64'),
+ np.dtype('bool'), d1, d1, np.dtype('uint32'), d2]
- assert test_dtype_methods() == [np.dtype('int32'), simple_dtype, False, True,
- np.dtype('int32').itemsize, simple_dtype.itemsize]
+ assert m.test_dtype_methods() == [np.dtype('int32'), simple_dtype, False, True,
+ np.dtype('int32').itemsize, simple_dtype.itemsize]
- assert trailing_padding_dtype() == buffer_to_dtype(np.zeros(1, trailing_padding_dtype()))
+ assert m.trailing_padding_dtype() == m.buffer_to_dtype(np.zeros(1, m.trailing_padding_dtype()))
def test_recarray(simple_dtype, packed_dtype):
- from pybind11_tests import (create_rec_simple, create_rec_packed, create_rec_nested,
- print_rec_simple, print_rec_packed, print_rec_nested,
- create_rec_partial, create_rec_partial_nested)
-
elements = [(False, 0, 0.0, -0.0), (True, 1, 1.5, -2.5), (False, 2, 3.0, -5.0)]
- for func, dtype in [(create_rec_simple, simple_dtype), (create_rec_packed, packed_dtype)]:
+ for func, dtype in [(m.create_rec_simple, simple_dtype), (m.create_rec_packed, packed_dtype)]:
arr = func(0)
assert arr.dtype == dtype
assert_equal(arr, [], simple_dtype)
@@ -138,13 +137,13 @@ def test_recarray(simple_dtype, packed_dtype):
assert_equal(arr, elements, packed_dtype)
if dtype == simple_dtype:
- assert print_rec_simple(arr) == [
+ assert m.print_rec_simple(arr) == [
"s:0,0,0,-0",
"s:1,1,1.5,-2.5",
"s:0,2,3,-5"
]
else:
- assert print_rec_packed(arr) == [
+ assert m.print_rec_packed(arr) == [
"p:0,0,0,-0",
"p:1,1,1.5,-2.5",
"p:0,2,3,-5"
@@ -152,22 +151,22 @@ def test_recarray(simple_dtype, packed_dtype):
nested_dtype = np.dtype([('a', simple_dtype), ('b', packed_dtype)])
- arr = create_rec_nested(0)
+ arr = m.create_rec_nested(0)
assert arr.dtype == nested_dtype
assert_equal(arr, [], nested_dtype)
- arr = create_rec_nested(3)
+ arr = m.create_rec_nested(3)
assert arr.dtype == nested_dtype
assert_equal(arr, [((False, 0, 0.0, -0.0), (True, 1, 1.5, -2.5)),
((True, 1, 1.5, -2.5), (False, 2, 3.0, -5.0)),
((False, 2, 3.0, -5.0), (True, 3, 4.5, -7.5))], nested_dtype)
- assert print_rec_nested(arr) == [
+ assert m.print_rec_nested(arr) == [
"n:a=s:0,0,0,-0;b=p:1,1,1.5,-2.5",
"n:a=s:1,1,1.5,-2.5;b=p:0,2,3,-5",
"n:a=s:0,2,3,-5;b=p:1,3,4.5,-7.5"
]
- arr = create_rec_partial(3)
+ arr = m.create_rec_partial(3)
assert str(arr.dtype) == partial_dtype_fmt()
partial_dtype = arr.dtype
assert '' not in arr.dtype.fields
@@ -175,32 +174,28 @@ def test_recarray(simple_dtype, packed_dtype):
assert_equal(arr, elements, simple_dtype)
assert_equal(arr, elements, packed_dtype)
- arr = create_rec_partial_nested(3)
+ arr = m.create_rec_partial_nested(3)
assert str(arr.dtype) == partial_nested_fmt()
assert '' not in arr.dtype.fields
assert '' not in arr.dtype.fields['a'][0].fields
assert arr.dtype.itemsize > partial_dtype.itemsize
- np.testing.assert_equal(arr['a'], create_rec_partial(3))
+ np.testing.assert_equal(arr['a'], m.create_rec_partial(3))
def test_array_constructors():
- from pybind11_tests import test_array_ctors
-
data = np.arange(1, 7, dtype='int32')
for i in range(8):
- np.testing.assert_array_equal(test_array_ctors(10 + i), data.reshape((3, 2)))
- np.testing.assert_array_equal(test_array_ctors(20 + i), data.reshape((3, 2)))
+ np.testing.assert_array_equal(m.test_array_ctors(10 + i), data.reshape((3, 2)))
+ np.testing.assert_array_equal(m.test_array_ctors(20 + i), data.reshape((3, 2)))
for i in range(5):
- np.testing.assert_array_equal(test_array_ctors(30 + i), data)
- np.testing.assert_array_equal(test_array_ctors(40 + i), data)
+ np.testing.assert_array_equal(m.test_array_ctors(30 + i), data)
+ np.testing.assert_array_equal(m.test_array_ctors(40 + i), data)
def test_string_array():
- from pybind11_tests import create_string_array, print_string_array
-
- arr = create_string_array(True)
+ arr = m.create_string_array(True)
assert str(arr.dtype) == "[('a', 'S3'), ('b', 'S3')]"
- assert print_string_array(arr) == [
+ assert m.print_string_array(arr) == [
"a='',b=''",
"a='a',b='a'",
"a='ab',b='ab'",
@@ -209,44 +204,78 @@ def test_string_array():
dtype = arr.dtype
assert arr['a'].tolist() == [b'', b'a', b'ab', b'abc']
assert arr['b'].tolist() == [b'', b'a', b'ab', b'abc']
- arr = create_string_array(False)
+ arr = m.create_string_array(False)
assert dtype == arr.dtype
+def test_array_array():
+ from sys import byteorder
+ e = '<' if byteorder == 'little' else '>'
+
+ arr = m.create_array_array(3)
+ assert str(arr.dtype) == (
+ "{{'names':['a','b','c','d'], " +
+ "'formats':[('S4', (3,)),('<i4', (2,)),('u1', (3,)),('{e}f4', (4, 2))], " +
+ "'offsets':[0,12,20,24], 'itemsize':56}}").format(e=e)
+ assert m.print_array_array(arr) == [
+ "a={{A,B,C,D},{K,L,M,N},{U,V,W,X}},b={0,1}," +
+ "c={0,1,2},d={{0,1},{10,11},{20,21},{30,31}}",
+ "a={{W,X,Y,Z},{G,H,I,J},{Q,R,S,T}},b={1000,1001}," +
+ "c={10,11,12},d={{100,101},{110,111},{120,121},{130,131}}",
+ "a={{S,T,U,V},{C,D,E,F},{M,N,O,P}},b={2000,2001}," +
+ "c={20,21,22},d={{200,201},{210,211},{220,221},{230,231}}",
+ ]
+ assert arr['a'].tolist() == [[b'ABCD', b'KLMN', b'UVWX'],
+ [b'WXYZ', b'GHIJ', b'QRST'],
+ [b'STUV', b'CDEF', b'MNOP']]
+ assert arr['b'].tolist() == [[0, 1], [1000, 1001], [2000, 2001]]
+ assert m.create_array_array(0).dtype == arr.dtype
+
+
def test_enum_array():
- from pybind11_tests import create_enum_array, print_enum_array
from sys import byteorder
e = '<' if byteorder == 'little' else '>'
- arr = create_enum_array(3)
+ arr = m.create_enum_array(3)
dtype = arr.dtype
assert dtype == np.dtype([('e1', e + 'i8'), ('e2', 'u1')])
- assert print_enum_array(arr) == [
+ assert m.print_enum_array(arr) == [
"e1=A,e2=X",
"e1=B,e2=Y",
"e1=A,e2=X"
]
assert arr['e1'].tolist() == [-1, 1, -1]
assert arr['e2'].tolist() == [1, 2, 1]
- assert create_enum_array(0).dtype == dtype
+ assert m.create_enum_array(0).dtype == dtype
-def test_signature(doc):
- from pybind11_tests import create_rec_nested
+def test_complex_array():
+ from sys import byteorder
+ e = '<' if byteorder == 'little' else '>'
+
+ arr = m.create_complex_array(3)
+ dtype = arr.dtype
+ assert dtype == np.dtype([('cflt', e + 'c8'), ('cdbl', e + 'c16')])
+ assert m.print_complex_array(arr) == [
+ "c:(0,0.25),(0.5,0.75)",
+ "c:(1,1.25),(1.5,1.75)",
+ "c:(2,2.25),(2.5,2.75)"
+ ]
+ assert arr['cflt'].tolist() == [0.0 + 0.25j, 1.0 + 1.25j, 2.0 + 2.25j]
+ assert arr['cdbl'].tolist() == [0.5 + 0.75j, 1.5 + 1.75j, 2.5 + 2.75j]
+ assert m.create_complex_array(0).dtype == dtype
- assert doc(create_rec_nested) == "create_rec_nested(arg0: int) -> numpy.ndarray[NestedStruct]"
+def test_signature(doc):
+ assert doc(m.create_rec_nested) == \
+ "create_rec_nested(arg0: int) -> numpy.ndarray[NestedStruct]"
-def test_scalar_conversion():
- from pybind11_tests import (create_rec_simple, f_simple,
- create_rec_packed, f_packed,
- create_rec_nested, f_nested,
- create_enum_array)
+def test_scalar_conversion():
n = 3
- arrays = [create_rec_simple(n), create_rec_packed(n),
- create_rec_nested(n), create_enum_array(n)]
- funcs = [f_simple, f_packed, f_nested]
+ arrays = [m.create_rec_simple(n), m.create_rec_packed(n),
+ m.create_rec_nested(n), m.create_enum_array(n)]
+ funcs = [m.f_simple, m.f_packed, m.f_nested]
for i, func in enumerate(funcs):
for j, arr in enumerate(arrays):
@@ -259,14 +288,11 @@ def test_scalar_conversion():
def test_register_dtype():
- from pybind11_tests import register_dtype
-
with pytest.raises(RuntimeError) as excinfo:
- register_dtype()
+ m.register_dtype()
assert 'dtype is already registered' in str(excinfo.value)
@pytest.requires_numpy
def test_compare_buffer_info():
- from pybind11_tests import compare_buffer_info
- assert all(compare_buffer_info())
+ assert all(m.compare_buffer_info())
diff --git a/ext/pybind11/tests/test_numpy_vectorize.cpp b/ext/pybind11/tests/test_numpy_vectorize.cpp
index 8e951c6e1..a875a74b9 100644
--- a/ext/pybind11/tests/test_numpy_vectorize.cpp
+++ b/ext/pybind11/tests/test_numpy_vectorize.cpp
@@ -16,11 +16,11 @@ double my_func(int x, float y, double z) {
return (float) x*y*z;
}
-std::complex<double> my_func3(std::complex<double> c) {
- return c * std::complex<double>(2.f);
-}
+TEST_SUBMODULE(numpy_vectorize, m) {
+ try { py::module::import("numpy"); }
+ catch (...) { return; }
-test_initializer numpy_vectorize([](py::module &m) {
+ // test_vectorize, test_docs, test_array_collapse
// Vectorize all arguments of a function (though non-vector arguments are also allowed)
m.def("vectorized_func", py::vectorize(my_func));
@@ -32,14 +32,45 @@ test_initializer numpy_vectorize([](py::module &m) {
);
// Vectorize a complex-valued function
- m.def("vectorized_func3", py::vectorize(my_func3));
+ m.def("vectorized_func3", py::vectorize(
+ [](std::complex<double> c) { return c * std::complex<double>(2.f); }
+ ));
- /// Numpy function which only accepts specific data types
+ // test_type_selection
+ // Numpy function which only accepts specific data types
m.def("selective_func", [](py::array_t<int, py::array::c_style>) { return "Int branch taken."; });
m.def("selective_func", [](py::array_t<float, py::array::c_style>) { return "Float branch taken."; });
m.def("selective_func", [](py::array_t<std::complex<float>, py::array::c_style>) { return "Complex float branch taken."; });
+ // test_passthrough_arguments
+ // Passthrough test: references and non-pod types should be automatically passed through (in the
+ // function definition below, only `b`, `d`, and `g` are vectorized):
+ struct NonPODClass {
+ NonPODClass(int v) : value{v} {}
+ int value;
+ };
+ py::class_<NonPODClass>(m, "NonPODClass").def(py::init<int>());
+ m.def("vec_passthrough", py::vectorize(
+ [](double *a, double b, py::array_t<double> c, const int &d, int &e, NonPODClass f, const double g) {
+ return *a + b + c.at(0) + d + e + f.value + g;
+ }
+ ));
+
+ // test_method_vectorization
+ struct VectorizeTestClass {
+ VectorizeTestClass(int v) : value{v} {};
+ float method(int x, float y) { return y + (float) (x + value); }
+ int value = 0;
+ };
+ py::class_<VectorizeTestClass> vtc(m, "VectorizeTestClass");
+ vtc .def(py::init<int>())
+ .def_readwrite("value", &VectorizeTestClass::value);
+
+ // Automatic vectorizing of methods
+ vtc.def("method", py::vectorize(&VectorizeTestClass::method));
+
+ // test_trivial_broadcasting
// Internal optimization test for whether the input is trivially broadcastable:
py::enum_<py::detail::broadcast_trivial>(m, "trivial")
.value("f_trivial", py::detail::broadcast_trivial::f_trivial)
@@ -50,9 +81,9 @@ test_initializer numpy_vectorize([](py::module &m) {
py::array_t<float, py::array::forcecast> arg2,
py::array_t<double, py::array::forcecast> arg3
) {
- size_t ndim;
- std::vector<size_t> shape;
+ ssize_t ndim;
+ std::vector<ssize_t> shape;
std::array<py::buffer_info, 3> buffers {{ arg1.request(), arg2.request(), arg3.request() }};
return py::detail::broadcast(buffers, ndim, shape);
});
-});
+}
diff --git a/ext/pybind11/tests/test_numpy_vectorize.py b/ext/pybind11/tests/test_numpy_vectorize.py
index 7ae777227..0e9c88397 100644
--- a/ext/pybind11/tests/test_numpy_vectorize.py
+++ b/ext/pybind11/tests/test_numpy_vectorize.py
@@ -1,4 +1,5 @@
import pytest
+from pybind11_tests import numpy_vectorize as m
pytestmark = pytest.requires_numpy
@@ -7,11 +8,9 @@ with pytest.suppress(ImportError):
def test_vectorize(capture):
- from pybind11_tests import vectorized_func, vectorized_func2, vectorized_func3
+ assert np.isclose(m.vectorized_func3(np.array(3 + 7j)), [6 + 14j])
- assert np.isclose(vectorized_func3(np.array(3 + 7j)), [6 + 14j])
-
- for f in [vectorized_func, vectorized_func2]:
+ for f in [m.vectorized_func, m.vectorized_func2]:
with capture:
assert np.isclose(f(1, 2, 3), 6)
assert capture == "my_func(x:int=1, y:float=2, z:float=3)"
@@ -103,23 +102,19 @@ def test_vectorize(capture):
def test_type_selection():
- from pybind11_tests import selective_func
-
- assert selective_func(np.array([1], dtype=np.int32)) == "Int branch taken."
- assert selective_func(np.array([1.0], dtype=np.float32)) == "Float branch taken."
- assert selective_func(np.array([1.0j], dtype=np.complex64)) == "Complex float branch taken."
+ assert m.selective_func(np.array([1], dtype=np.int32)) == "Int branch taken."
+ assert m.selective_func(np.array([1.0], dtype=np.float32)) == "Float branch taken."
+ assert m.selective_func(np.array([1.0j], dtype=np.complex64)) == "Complex float branch taken."
def test_docs(doc):
- from pybind11_tests import vectorized_func
-
- assert doc(vectorized_func) == """
+ assert doc(m.vectorized_func) == """
vectorized_func(arg0: numpy.ndarray[int32], arg1: numpy.ndarray[float32], arg2: numpy.ndarray[float64]) -> object
""" # noqa: E501 line too long
def test_trivial_broadcasting():
- from pybind11_tests import vectorized_is_trivial, trivial, vectorized_func
+ trivial, vectorized_is_trivial = m.trivial, m.vectorized_is_trivial
assert vectorized_is_trivial(1, 2, 3) == trivial.c_trivial
assert vectorized_is_trivial(np.array(1), np.array(2), 3) == trivial.c_trivial
@@ -153,9 +148,49 @@ def test_trivial_broadcasting():
assert vectorized_is_trivial(z1[1::4, 1::4], y2, 1) == trivial.f_trivial
assert vectorized_is_trivial(y1[1::4, 1::4], z2, 1) == trivial.c_trivial
- assert vectorized_func(z1, z2, z3).flags.c_contiguous
- assert vectorized_func(y1, y2, y3).flags.f_contiguous
- assert vectorized_func(z1, 1, 1).flags.c_contiguous
- assert vectorized_func(1, y2, 1).flags.f_contiguous
- assert vectorized_func(z1[1::4, 1::4], y2, 1).flags.f_contiguous
- assert vectorized_func(y1[1::4, 1::4], z2, 1).flags.c_contiguous
+ assert m.vectorized_func(z1, z2, z3).flags.c_contiguous
+ assert m.vectorized_func(y1, y2, y3).flags.f_contiguous
+ assert m.vectorized_func(z1, 1, 1).flags.c_contiguous
+ assert m.vectorized_func(1, y2, 1).flags.f_contiguous
+ assert m.vectorized_func(z1[1::4, 1::4], y2, 1).flags.f_contiguous
+ assert m.vectorized_func(y1[1::4, 1::4], z2, 1).flags.c_contiguous
+
+
+def test_passthrough_arguments(doc):
+ assert doc(m.vec_passthrough) == (
+ "vec_passthrough(" + ", ".join([
+ "arg0: float",
+ "arg1: numpy.ndarray[float64]",
+ "arg2: numpy.ndarray[float64]",
+ "arg3: numpy.ndarray[int32]",
+ "arg4: int",
+ "arg5: m.numpy_vectorize.NonPODClass",
+ "arg6: numpy.ndarray[float64]"]) + ") -> object")
+
+ b = np.array([[10, 20, 30]], dtype='float64')
+ c = np.array([100, 200]) # NOT a vectorized argument
+ d = np.array([[1000], [2000], [3000]], dtype='int')
+ g = np.array([[1000000, 2000000, 3000000]], dtype='int') # requires casting
+ assert np.all(
+ m.vec_passthrough(1, b, c, d, 10000, m.NonPODClass(100000), g) ==
+ np.array([[1111111, 2111121, 3111131],
+ [1112111, 2112121, 3112131],
+ [1113111, 2113121, 3113131]]))
+
+
+def test_method_vectorization():
+ o = m.VectorizeTestClass(3)
+ x = np.array([1, 2], dtype='int')
+ y = np.array([[10], [20]], dtype='float32')
+ assert np.all(o.method(x, y) == [[14, 15], [24, 25]])
+
+
+def test_array_collapse():
+ assert not isinstance(m.vectorized_func(1, 2, 3), np.ndarray)
+ assert not isinstance(m.vectorized_func(np.array(1), 2, 3), np.ndarray)
+ z = m.vectorized_func([1], 2, 3)
+ assert isinstance(z, np.ndarray)
+ assert z.shape == (1, )
+ z = m.vectorized_func(1, [[[2]]], 3)
+ assert isinstance(z, np.ndarray)
+ assert z.shape == (1, 1, 1)
diff --git a/ext/pybind11/tests/test_opaque_types.cpp b/ext/pybind11/tests/test_opaque_types.cpp
index 54f4dc7a5..5e83df0f6 100644
--- a/ext/pybind11/tests/test_opaque_types.cpp
+++ b/ext/pybind11/tests/test_opaque_types.cpp
@@ -11,17 +11,13 @@
#include <pybind11/stl.h>
#include <vector>
-typedef std::vector<std::string> StringList;
-
-class ClassWithSTLVecProperty {
-public:
- StringList stringList;
-};
+using StringList = std::vector<std::string>;
/* IMPORTANT: Disable internal pybind11 translation mechanisms for STL data structures */
PYBIND11_MAKE_OPAQUE(StringList);
-test_initializer opaque_types([](py::module &m) {
+TEST_SUBMODULE(opaque_types, m) {
+ // test_string_list
py::class_<StringList>(m, "StringList")
.def(py::init<>())
.def("pop_back", &StringList::pop_back)
@@ -33,6 +29,10 @@ test_initializer opaque_types([](py::module &m) {
return py::make_iterator(v.begin(), v.end());
}, py::keep_alive<0, 1>());
+ class ClassWithSTLVecProperty {
+ public:
+ StringList stringList;
+ };
py::class_<ClassWithSTLVecProperty>(m, "ClassWithSTLVecProperty")
.def(py::init<>())
.def_readwrite("stringList", &ClassWithSTLVecProperty::stringList);
@@ -49,6 +49,7 @@ test_initializer opaque_types([](py::module &m) {
return ret + "]";
});
+ // test_pointers
m.def("return_void_ptr", []() { return (void *) 0x1234; });
m.def("get_void_ptr_value", [](void *ptr) { return reinterpret_cast<std::intptr_t>(ptr); });
m.def("return_null_str", []() { return (char *) nullptr; });
@@ -59,4 +60,4 @@ test_initializer opaque_types([](py::module &m) {
result->push_back("some value");
return std::unique_ptr<StringList>(result);
});
-});
+}
diff --git a/ext/pybind11/tests/test_opaque_types.py b/ext/pybind11/tests/test_opaque_types.py
index 1cd410208..2d3aef5d1 100644
--- a/ext/pybind11/tests/test_opaque_types.py
+++ b/ext/pybind11/tests/test_opaque_types.py
@@ -1,40 +1,36 @@
import pytest
+from pybind11_tests import opaque_types as m
+from pybind11_tests import ConstructorStats, UserType
def test_string_list():
- from pybind11_tests import StringList, ClassWithSTLVecProperty, print_opaque_list
-
- l = StringList()
+ l = m.StringList()
l.push_back("Element 1")
l.push_back("Element 2")
- assert print_opaque_list(l) == "Opaque list: [Element 1, Element 2]"
+ assert m.print_opaque_list(l) == "Opaque list: [Element 1, Element 2]"
assert l.back() == "Element 2"
for i, k in enumerate(l, start=1):
assert k == "Element {}".format(i)
l.pop_back()
- assert print_opaque_list(l) == "Opaque list: [Element 1]"
+ assert m.print_opaque_list(l) == "Opaque list: [Element 1]"
- cvp = ClassWithSTLVecProperty()
- assert print_opaque_list(cvp.stringList) == "Opaque list: []"
+ cvp = m.ClassWithSTLVecProperty()
+ assert m.print_opaque_list(cvp.stringList) == "Opaque list: []"
cvp.stringList = l
cvp.stringList.push_back("Element 3")
- assert print_opaque_list(cvp.stringList) == "Opaque list: [Element 1, Element 3]"
+ assert m.print_opaque_list(cvp.stringList) == "Opaque list: [Element 1, Element 3]"
def test_pointers(msg):
- from pybind11_tests import (return_void_ptr, get_void_ptr_value, ExampleMandA,
- print_opaque_list, return_null_str, get_null_str_value,
- return_unique_ptr, ConstructorStats)
-
- living_before = ConstructorStats.get(ExampleMandA).alive()
- assert get_void_ptr_value(return_void_ptr()) == 0x1234
- assert get_void_ptr_value(ExampleMandA()) # Should also work for other C++ types
- assert ConstructorStats.get(ExampleMandA).alive() == living_before
+ living_before = ConstructorStats.get(UserType).alive()
+ assert m.get_void_ptr_value(m.return_void_ptr()) == 0x1234
+ assert m.get_void_ptr_value(UserType()) # Should also work for other C++ types
+ assert ConstructorStats.get(UserType).alive() == living_before
with pytest.raises(TypeError) as excinfo:
- get_void_ptr_value([1, 2, 3]) # This should not work
+ m.get_void_ptr_value([1, 2, 3]) # This should not work
assert msg(excinfo.value) == """
get_void_ptr_value(): incompatible function arguments. The following argument types are supported:
1. (arg0: capsule) -> int
@@ -42,9 +38,9 @@ def test_pointers(msg):
Invoked with: [1, 2, 3]
""" # noqa: E501 line too long
- assert return_null_str() is None
- assert get_null_str_value(return_null_str()) is not None
+ assert m.return_null_str() is None
+ assert m.get_null_str_value(m.return_null_str()) is not None
- ptr = return_unique_ptr()
+ ptr = m.return_unique_ptr()
assert "StringList" in repr(ptr)
- assert print_opaque_list(ptr) == "Opaque list: [some value]"
+ assert m.print_opaque_list(ptr) == "Opaque list: [some value]"
diff --git a/ext/pybind11/tests/test_operator_overloading.cpp b/ext/pybind11/tests/test_operator_overloading.cpp
index 93aea8010..4ad34d104 100644
--- a/ext/pybind11/tests/test_operator_overloading.cpp
+++ b/ext/pybind11/tests/test_operator_overloading.cpp
@@ -10,28 +10,18 @@
#include "pybind11_tests.h"
#include "constructor_stats.h"
#include <pybind11/operators.h>
+#include <functional>
class Vector2 {
public:
Vector2(float x, float y) : x(x), y(y) { print_created(this, toString()); }
Vector2(const Vector2 &v) : x(v.x), y(v.y) { print_copy_created(this); }
Vector2(Vector2 &&v) : x(v.x), y(v.y) { print_move_created(this); v.x = v.y = 0; }
+ Vector2 &operator=(const Vector2 &v) { x = v.x; y = v.y; print_copy_assigned(this); return *this; }
+ Vector2 &operator=(Vector2 &&v) { x = v.x; y = v.y; v.x = v.y = 0; print_move_assigned(this); return *this; }
~Vector2() { print_destroyed(this); }
- std::string toString() const {
- return "[" + std::to_string(x) + ", " + std::to_string(y) + "]";
- }
-
- void operator=(const Vector2 &v) {
- print_copy_assigned(this);
- x = v.x;
- y = v.y;
- }
-
- void operator=(Vector2 &&v) {
- print_move_assigned(this);
- x = v.x; y = v.y; v.x = v.y = 0;
- }
+ std::string toString() const { return "[" + std::to_string(x) + ", " + std::to_string(y) + "]"; }
Vector2 operator+(const Vector2 &v) const { return Vector2(x + v.x, y + v.y); }
Vector2 operator-(const Vector2 &v) const { return Vector2(x - v.x, y - v.y); }
@@ -39,10 +29,14 @@ public:
Vector2 operator+(float value) const { return Vector2(x + value, y + value); }
Vector2 operator*(float value) const { return Vector2(x * value, y * value); }
Vector2 operator/(float value) const { return Vector2(x / value, y / value); }
+ Vector2 operator*(const Vector2 &v) const { return Vector2(x * v.x, y * v.y); }
+ Vector2 operator/(const Vector2 &v) const { return Vector2(x / v.x, y / v.y); }
Vector2& operator+=(const Vector2 &v) { x += v.x; y += v.y; return *this; }
Vector2& operator-=(const Vector2 &v) { x -= v.x; y -= v.y; return *this; }
Vector2& operator*=(float v) { x *= v; y *= v; return *this; }
Vector2& operator/=(float v) { x /= v; y /= v; return *this; }
+ Vector2& operator*=(const Vector2 &v) { x *= v.x; y *= v.y; return *this; }
+ Vector2& operator/=(const Vector2 &v) { x /= v.x; y /= v.y; return *this; }
friend Vector2 operator+(float f, const Vector2 &v) { return Vector2(f + v.x, f + v.y); }
friend Vector2 operator-(float f, const Vector2 &v) { return Vector2(f - v.x, f - v.y); }
@@ -52,7 +46,25 @@ private:
float x, y;
};
-test_initializer operator_overloading([](py::module &m) {
+class C1 { };
+class C2 { };
+
+int operator+(const C1 &, const C1 &) { return 11; }
+int operator+(const C2 &, const C2 &) { return 22; }
+int operator+(const C2 &, const C1 &) { return 21; }
+int operator+(const C1 &, const C2 &) { return 12; }
+
+namespace std {
+ template<>
+ struct hash<Vector2> {
+ // Not a good hash function, but easy to test
+ size_t operator()(const Vector2 &) { return 4; }
+ };
+}
+
+TEST_SUBMODULE(operators, m) {
+
+ // test_operator_overloading
py::class_<Vector2>(m, "Vector2")
.def(py::init<float, float>())
.def(py::self + py::self)
@@ -61,16 +73,74 @@ test_initializer operator_overloading([](py::module &m) {
.def(py::self - float())
.def(py::self * float())
.def(py::self / float())
+ .def(py::self * py::self)
+ .def(py::self / py::self)
.def(py::self += py::self)
.def(py::self -= py::self)
.def(py::self *= float())
.def(py::self /= float())
+ .def(py::self *= py::self)
+ .def(py::self /= py::self)
.def(float() + py::self)
.def(float() - py::self)
.def(float() * py::self)
.def(float() / py::self)
.def("__str__", &Vector2::toString)
+ .def(hash(py::self))
;
m.attr("Vector") = m.attr("Vector2");
-});
+
+ // test_operators_notimplemented
+ // #393: need to return NotSupported to ensure correct arithmetic operator behavior
+ py::class_<C1>(m, "C1")
+ .def(py::init<>())
+ .def(py::self + py::self);
+
+ py::class_<C2>(m, "C2")
+ .def(py::init<>())
+ .def(py::self + py::self)
+ .def("__add__", [](const C2& c2, const C1& c1) { return c2 + c1; })
+ .def("__radd__", [](const C2& c2, const C1& c1) { return c1 + c2; });
+
+ // test_nested
+ // #328: first member in a class can't be used in operators
+ struct NestABase { int value = -2; };
+ py::class_<NestABase>(m, "NestABase")
+ .def(py::init<>())
+ .def_readwrite("value", &NestABase::value);
+
+ struct NestA : NestABase {
+ int value = 3;
+ NestA& operator+=(int i) { value += i; return *this; }
+ };
+ py::class_<NestA>(m, "NestA")
+ .def(py::init<>())
+ .def(py::self += int())
+ .def("as_base", [](NestA &a) -> NestABase& {
+ return (NestABase&) a;
+ }, py::return_value_policy::reference_internal);
+ m.def("get_NestA", [](const NestA &a) { return a.value; });
+
+ struct NestB {
+ NestA a;
+ int value = 4;
+ NestB& operator-=(int i) { value -= i; return *this; }
+ };
+ py::class_<NestB>(m, "NestB")
+ .def(py::init<>())
+ .def(py::self -= int())
+ .def_readwrite("a", &NestB::a);
+ m.def("get_NestB", [](const NestB &b) { return b.value; });
+
+ struct NestC {
+ NestB b;
+ int value = 5;
+ NestC& operator*=(int i) { value *= i; return *this; }
+ };
+ py::class_<NestC>(m, "NestC")
+ .def(py::init<>())
+ .def(py::self *= int())
+ .def_readwrite("b", &NestC::b);
+ m.def("get_NestC", [](const NestC &c) { return c.value; });
+}
diff --git a/ext/pybind11/tests/test_operator_overloading.py b/ext/pybind11/tests/test_operator_overloading.py
index 02ccb9633..0d80e5ed3 100644
--- a/ext/pybind11/tests/test_operator_overloading.py
+++ b/ext/pybind11/tests/test_operator_overloading.py
@@ -1,8 +1,11 @@
-def test_operator_overloading():
- from pybind11_tests import Vector2, Vector, ConstructorStats
+import pytest
+from pybind11_tests import operators as m
+from pybind11_tests import ConstructorStats
+
- v1 = Vector2(1, 2)
- v2 = Vector(3, -1)
+def test_operator_overloading():
+ v1 = m.Vector2(1, 2)
+ v2 = m.Vector(3, -1)
assert str(v1) == "[1.000000, 2.000000]"
assert str(v2) == "[3.000000, -1.000000]"
@@ -16,12 +19,25 @@ def test_operator_overloading():
assert str(8 + v1) == "[9.000000, 10.000000]"
assert str(8 * v1) == "[8.000000, 16.000000]"
assert str(8 / v1) == "[8.000000, 4.000000]"
+ assert str(v1 * v2) == "[3.000000, -2.000000]"
+ assert str(v2 / v1) == "[3.000000, -0.500000]"
- v1 += v2
+ v1 += 2 * v2
+ assert str(v1) == "[7.000000, 0.000000]"
+ v1 -= v2
+ assert str(v1) == "[4.000000, 1.000000]"
v1 *= 2
assert str(v1) == "[8.000000, 2.000000]"
+ v1 /= 16
+ assert str(v1) == "[0.500000, 0.125000]"
+ v1 *= v2
+ assert str(v1) == "[1.500000, -0.125000]"
+ v2 /= v1
+ assert str(v2) == "[2.000000, 8.000000]"
+
+ assert hash(v1) == 4
- cstats = ConstructorStats.get(Vector2)
+ cstats = ConstructorStats.get(m.Vector2)
assert cstats.alive() == 2
del v1
assert cstats.alive() == 1
@@ -32,9 +48,59 @@ def test_operator_overloading():
'[-7.000000, -6.000000]', '[9.000000, 10.000000]',
'[8.000000, 16.000000]', '[0.125000, 0.250000]',
'[7.000000, 6.000000]', '[9.000000, 10.000000]',
- '[8.000000, 16.000000]', '[8.000000, 4.000000]']
+ '[8.000000, 16.000000]', '[8.000000, 4.000000]',
+ '[3.000000, -2.000000]', '[3.000000, -0.500000]',
+ '[6.000000, -2.000000]']
assert cstats.default_constructions == 0
assert cstats.copy_constructions == 0
assert cstats.move_constructions >= 10
assert cstats.copy_assignments == 0
assert cstats.move_assignments == 0
+
+
+def test_operators_notimplemented():
+ """#393: need to return NotSupported to ensure correct arithmetic operator behavior"""
+
+ c1, c2 = m.C1(), m.C2()
+ assert c1 + c1 == 11
+ assert c2 + c2 == 22
+ assert c2 + c1 == 21
+ assert c1 + c2 == 12
+
+
+def test_nested():
+ """#328: first member in a class can't be used in operators"""
+
+ a = m.NestA()
+ b = m.NestB()
+ c = m.NestC()
+
+ a += 10
+ assert m.get_NestA(a) == 13
+ b.a += 100
+ assert m.get_NestA(b.a) == 103
+ c.b.a += 1000
+ assert m.get_NestA(c.b.a) == 1003
+ b -= 1
+ assert m.get_NestB(b) == 3
+ c.b -= 3
+ assert m.get_NestB(c.b) == 1
+ c *= 7
+ assert m.get_NestC(c) == 35
+
+ abase = a.as_base()
+ assert abase.value == -2
+ a.as_base().value += 44
+ assert abase.value == 42
+ assert c.b.a.as_base().value == -2
+ c.b.a.as_base().value += 44
+ assert c.b.a.as_base().value == 42
+
+ del c
+ pytest.gc_collect()
+ del a # Should't delete while abase is still alive
+ pytest.gc_collect()
+
+ assert abase.value == 42
+ del abase, b
+ pytest.gc_collect()
diff --git a/ext/pybind11/tests/test_pickling.cpp b/ext/pybind11/tests/test_pickling.cpp
index 52b1dbc30..9dc63bda3 100644
--- a/ext/pybind11/tests/test_pickling.cpp
+++ b/ext/pybind11/tests/test_pickling.cpp
@@ -9,30 +9,28 @@
#include "pybind11_tests.h"
-class Pickleable {
-public:
- Pickleable(const std::string &value) : m_value(value) { }
- const std::string &value() const { return m_value; }
-
- void setExtra1(int extra1) { m_extra1 = extra1; }
- void setExtra2(int extra2) { m_extra2 = extra2; }
- int extra1() const { return m_extra1; }
- int extra2() const { return m_extra2; }
-private:
- std::string m_value;
- int m_extra1 = 0;
- int m_extra2 = 0;
-};
-
-class PickleableWithDict {
-public:
- PickleableWithDict(const std::string &value) : value(value) { }
-
- std::string value;
- int extra;
-};
-
-test_initializer pickling([](py::module &m) {
+TEST_SUBMODULE(pickling, m) {
+ // test_roundtrip
+ class Pickleable {
+ public:
+ Pickleable(const std::string &value) : m_value(value) { }
+ const std::string &value() const { return m_value; }
+
+ void setExtra1(int extra1) { m_extra1 = extra1; }
+ void setExtra2(int extra2) { m_extra2 = extra2; }
+ int extra1() const { return m_extra1; }
+ int extra2() const { return m_extra2; }
+ private:
+ std::string m_value;
+ int m_extra1 = 0;
+ int m_extra2 = 0;
+ };
+
+ class PickleableNew : public Pickleable {
+ public:
+ using Pickleable::Pickleable;
+ };
+
py::class_<Pickleable>(m, "Pickleable")
.def(py::init<std::string>())
.def("value", &Pickleable::value)
@@ -57,7 +55,38 @@ test_initializer pickling([](py::module &m) {
p.setExtra2(t[2].cast<int>());
});
+ py::class_<PickleableNew, Pickleable>(m, "PickleableNew")
+ .def(py::init<std::string>())
+ .def(py::pickle(
+ [](const PickleableNew &p) {
+ return py::make_tuple(p.value(), p.extra1(), p.extra2());
+ },
+ [](py::tuple t) {
+ if (t.size() != 3)
+ throw std::runtime_error("Invalid state!");
+ auto p = PickleableNew(t[0].cast<std::string>());
+
+ p.setExtra1(t[1].cast<int>());
+ p.setExtra2(t[2].cast<int>());
+ return p;
+ }
+ ));
+
#if !defined(PYPY_VERSION)
+ // test_roundtrip_with_dict
+ class PickleableWithDict {
+ public:
+ PickleableWithDict(const std::string &value) : value(value) { }
+
+ std::string value;
+ int extra;
+ };
+
+ class PickleableWithDictNew : public PickleableWithDict {
+ public:
+ using PickleableWithDict::PickleableWithDict;
+ };
+
py::class_<PickleableWithDict>(m, "PickleableWithDict", py::dynamic_attr())
.def(py::init<std::string>())
.def_readwrite("value", &PickleableWithDict::value)
@@ -79,5 +108,23 @@ test_initializer pickling([](py::module &m) {
/* Assign Python state */
self.attr("__dict__") = t[2];
});
+
+ py::class_<PickleableWithDictNew, PickleableWithDict>(m, "PickleableWithDictNew")
+ .def(py::init<std::string>())
+ .def(py::pickle(
+ [](py::object self) {
+ return py::make_tuple(self.attr("value"), self.attr("extra"), self.attr("__dict__"));
+ },
+ [](const py::tuple &t) {
+ if (t.size() != 3)
+ throw std::runtime_error("Invalid state!");
+
+ auto cpp_state = PickleableWithDictNew(t[0].cast<std::string>());
+ cpp_state.extra = t[1].cast<int>();
+
+ auto py_state = t[2].cast<py::dict>();
+ return std::make_pair(cpp_state, py_state);
+ }
+ ));
#endif
-});
+}
diff --git a/ext/pybind11/tests/test_pickling.py b/ext/pybind11/tests/test_pickling.py
index 548c618af..707d34786 100644
--- a/ext/pybind11/tests/test_pickling.py
+++ b/ext/pybind11/tests/test_pickling.py
@@ -1,4 +1,5 @@
import pytest
+from pybind11_tests import pickling as m
try:
import cPickle as pickle # Use cPickle on Python 2.7
@@ -6,10 +7,10 @@ except ImportError:
import pickle
-def test_roundtrip():
- from pybind11_tests import Pickleable
-
- p = Pickleable("test_value")
+@pytest.mark.parametrize("cls_name", ["Pickleable", "PickleableNew"])
+def test_roundtrip(cls_name):
+ cls = getattr(m, cls_name)
+ p = cls("test_value")
p.setExtra1(15)
p.setExtra2(48)
@@ -21,10 +22,10 @@ def test_roundtrip():
@pytest.unsupported_on_pypy
-def test_roundtrip_with_dict():
- from pybind11_tests import PickleableWithDict
-
- p = PickleableWithDict("test_value")
+@pytest.mark.parametrize("cls_name", ["PickleableWithDict", "PickleableWithDictNew"])
+def test_roundtrip_with_dict(cls_name):
+ cls = getattr(m, cls_name)
+ p = cls("test_value")
p.extra = 15
p.dynamic = "Attribute"
diff --git a/ext/pybind11/tests/test_python_types.cpp b/ext/pybind11/tests/test_python_types.cpp
deleted file mode 100644
index 5696239b4..000000000
--- a/ext/pybind11/tests/test_python_types.cpp
+++ /dev/null
@@ -1,495 +0,0 @@
-/*
- tests/test_python_types.cpp -- singleton design pattern, static functions and
- variables, passing and interacting with Python types
-
- Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
-
- All rights reserved. Use of this source code is governed by a
- BSD-style license that can be found in the LICENSE file.
-*/
-
-#include "pybind11_tests.h"
-#include "constructor_stats.h"
-#include <pybind11/stl.h>
-
-#ifdef _WIN32
-# include <io.h>
-# include <fcntl.h>
-#endif
-
-#if defined(_MSC_VER)
-# pragma warning(push)
-# pragma warning(disable: 4127) // warning C4127: Conditional expression is constant
-#endif
-
-class ExamplePythonTypes {
-public:
- static ExamplePythonTypes *new_instance() {
- auto *ptr = new ExamplePythonTypes();
- print_created(ptr, "via new_instance");
- return ptr;
- }
- ~ExamplePythonTypes() { print_destroyed(this); }
-
- /* Create and return a Python dictionary */
- py::dict get_dict() {
- py::dict dict;
- dict[py::str("key")] = py::str("value");
- return dict;
- }
-
- /* Create and return a Python set */
- py::set get_set() {
- py::set set;
- set.add(py::str("key1"));
- set.add("key2");
- set.add(std::string("key3"));
- return set;
- }
-
- /* Create and return a C++ dictionary */
- std::map<std::string, std::string> get_dict_2() {
- std::map<std::string, std::string> result;
- result["key"] = "value";
- return result;
- }
-
- /* Create and return a C++ set */
- std::set<std::string> get_set_2() {
- std::set<std::string> result;
- result.insert("key1");
- result.insert("key2");
- return result;
- }
-
- /* Create, manipulate, and return a Python list */
- py::list get_list() {
- py::list list;
- list.append("value");
- py::print("Entry at position 0:", list[0]);
- list[0] = py::str("overwritten");
- return list;
- }
-
- /* C++ STL data types are automatically casted */
- std::vector<std::wstring> get_list_2() {
- std::vector<std::wstring> list;
- list.push_back(L"value");
- return list;
- }
-
- /* C++ STL data types are automatically casted */
- std::array<std::string, 2> get_array() {
- return std::array<std::string, 2> {{ "array entry 1" , "array entry 2"}};
- }
-
- std::valarray<int> get_valarray() {
- return std::valarray<int>({ 1, 4, 9 });
- }
-
- /* Easily iterate over a dictionary using a C++11 range-based for loop */
- void print_dict(py::dict dict) {
- for (auto item : dict)
- py::print("key: {}, value={}"_s.format(item.first, item.second));
- }
-
- /* Easily iterate over a set using a C++11 range-based for loop */
- void print_set(py::set set) {
- for (auto item : set)
- py::print("key:", item);
- }
-
- /* Easily iterate over a list using a C++11 range-based for loop */
- void print_list(py::list list) {
- int index = 0;
- for (auto item : list)
- py::print("list item {}: {}"_s.format(index++, item));
- }
-
- /* STL data types (such as maps) are automatically casted from Python */
- void print_dict_2(const std::map<std::string, std::string> &dict) {
- for (auto item : dict)
- py::print("key: {}, value={}"_s.format(item.first, item.second));
- }
-
- /* STL data types (such as sets) are automatically casted from Python */
- void print_set_2(const std::set<std::string> &set) {
- for (auto item : set)
- py::print("key:", item);
- }
-
- /* STL data types (such as vectors) are automatically casted from Python */
- void print_list_2(std::vector<std::wstring> &list) {
- int index = 0;
- for (auto item : list)
- py::print("list item {}: {}"_s.format(index++, item));
- }
-
- /* pybind automatically translates between C++11 and Python tuples */
- std::pair<std::string, bool> pair_passthrough(std::pair<bool, std::string> input) {
- return std::make_pair(input.second, input.first);
- }
-
- /* pybind automatically translates between C++11 and Python tuples */
- std::tuple<int, std::string, bool> tuple_passthrough(std::tuple<bool, std::string, int> input) {
- return std::make_tuple(std::get<2>(input), std::get<1>(input), std::get<0>(input));
- }
-
- /* STL data types (such as arrays) are automatically casted from Python */
- void print_array(std::array<std::string, 2> &array) {
- int index = 0;
- for (auto item : array)
- py::print("array item {}: {}"_s.format(index++, item));
- }
-
- void print_valarray(std::valarray<int> &varray) {
- int index = 0;
- for (auto item : varray)
- py::print("valarray item {}: {}"_s.format(index++, item));
- }
-
- void throw_exception() {
- throw std::runtime_error("This exception was intentionally thrown.");
- }
-
- py::bytes get_bytes_from_string() {
- return (py::bytes) std::string("foo");
- }
-
- py::bytes get_bytes_from_str() {
- return (py::bytes) py::str("bar", 3);
- }
-
- py::str get_str_from_string() {
- return (py::str) std::string("baz");
- }
-
- py::str get_str_from_bytes() {
- return (py::str) py::bytes("boo", 3);
- }
-
- void test_print(const py::object& obj) {
- py::print(py::str(obj));
- py::print(py::repr(obj));
- }
-
- static int value;
- static const int value2;
-};
-
-int ExamplePythonTypes::value = 0;
-const int ExamplePythonTypes::value2 = 5;
-
-struct MoveOutContainer {
- struct Value { int value; };
-
- std::list<Value> move_list() const { return {{0}, {1}, {2}}; }
-};
-
-
-test_initializer python_types([](py::module &m) {
- /* No constructor is explicitly defined below. An exception is raised when
- trying to construct it directly from Python */
- py::class_<ExamplePythonTypes>(m, "ExamplePythonTypes", "Example 2 documentation")
- .def("get_dict", &ExamplePythonTypes::get_dict, "Return a Python dictionary")
- .def("get_dict_2", &ExamplePythonTypes::get_dict_2, "Return a C++ dictionary")
- .def("get_list", &ExamplePythonTypes::get_list, "Return a Python list")
- .def("get_list_2", &ExamplePythonTypes::get_list_2, "Return a C++ list")
- .def("get_set", &ExamplePythonTypes::get_set, "Return a Python set")
- .def("get_set2", &ExamplePythonTypes::get_set_2, "Return a C++ set")
- .def("get_array", &ExamplePythonTypes::get_array, "Return a C++ array")
- .def("get_valarray", &ExamplePythonTypes::get_valarray, "Return a C++ valarray")
- .def("print_dict", &ExamplePythonTypes::print_dict, "Print entries of a Python dictionary")
- .def("print_dict_2", &ExamplePythonTypes::print_dict_2, "Print entries of a C++ dictionary")
- .def("print_set", &ExamplePythonTypes::print_set, "Print entries of a Python set")
- .def("print_set_2", &ExamplePythonTypes::print_set_2, "Print entries of a C++ set")
- .def("print_list", &ExamplePythonTypes::print_list, "Print entries of a Python list")
- .def("print_list_2", &ExamplePythonTypes::print_list_2, "Print entries of a C++ list")
- .def("print_array", &ExamplePythonTypes::print_array, "Print entries of a C++ array")
- .def("print_valarray", &ExamplePythonTypes::print_valarray, "Print entries of a C++ valarray")
- .def("pair_passthrough", &ExamplePythonTypes::pair_passthrough, "Return a pair in reversed order")
- .def("tuple_passthrough", &ExamplePythonTypes::tuple_passthrough, "Return a triple in reversed order")
- .def("throw_exception", &ExamplePythonTypes::throw_exception, "Throw an exception")
- .def("get_bytes_from_string", &ExamplePythonTypes::get_bytes_from_string, "py::bytes from std::string")
- .def("get_bytes_from_str", &ExamplePythonTypes::get_bytes_from_str, "py::bytes from py::str")
- .def("get_str_from_string", &ExamplePythonTypes::get_str_from_string, "py::str from std::string")
- .def("get_str_from_bytes", &ExamplePythonTypes::get_str_from_bytes, "py::str from py::bytes")
- .def("test_print", &ExamplePythonTypes::test_print, "test the print function")
- .def_static("new_instance", &ExamplePythonTypes::new_instance, "Return an instance")
- .def_readwrite_static("value", &ExamplePythonTypes::value, "Static value member")
- .def_readonly_static("value2", &ExamplePythonTypes::value2, "Static value member (readonly)");
-
- m.def("test_print_function", []() {
- py::print("Hello, World!");
- py::print(1, 2.0, "three", true, std::string("-- multiple args"));
- auto args = py::make_tuple("and", "a", "custom", "separator");
- py::print("*args", *args, "sep"_a="-");
- py::print("no new line here", "end"_a=" -- ");
- py::print("next print");
-
- auto py_stderr = py::module::import("sys").attr("stderr");
- py::print("this goes to stderr", "file"_a=py_stderr);
-
- py::print("flush", "flush"_a=true);
-
- py::print("{a} + {b} = {c}"_s.format("a"_a="py::print", "b"_a="str.format", "c"_a="this"));
- });
-
- m.def("test_str_format", []() {
- auto s1 = "{} + {} = {}"_s.format(1, 2, 3);
- auto s2 = "{a} + {b} = {c}"_s.format("a"_a=1, "b"_a=2, "c"_a=3);
- return py::make_tuple(s1, s2);
- });
-
- m.def("test_dict_keyword_constructor", []() {
- auto d1 = py::dict("x"_a=1, "y"_a=2);
- auto d2 = py::dict("z"_a=3, **d1);
- return d2;
- });
-
- m.def("test_accessor_api", [](py::object o) {
- auto d = py::dict();
-
- d["basic_attr"] = o.attr("basic_attr");
-
- auto l = py::list();
- for (const auto &item : o.attr("begin_end")) {
- l.append(item);
- }
- d["begin_end"] = l;
-
- d["operator[object]"] = o.attr("d")["operator[object]"_s];
- d["operator[char *]"] = o.attr("d")["operator[char *]"];
-
- d["attr(object)"] = o.attr("sub").attr("attr_obj");
- d["attr(char *)"] = o.attr("sub").attr("attr_char");
- try {
- o.attr("sub").attr("missing").ptr();
- } catch (const py::error_already_set &) {
- d["missing_attr_ptr"] = "raised"_s;
- }
- try {
- o.attr("missing").attr("doesn't matter");
- } catch (const py::error_already_set &) {
- d["missing_attr_chain"] = "raised"_s;
- }
-
- d["is_none"] = o.attr("basic_attr").is_none();
-
- d["operator()"] = o.attr("func")(1);
- d["operator*"] = o.attr("func")(*o.attr("begin_end"));
-
- return d;
- });
-
- m.def("test_tuple_accessor", [](py::tuple existing_t) {
- try {
- existing_t[0] = 1;
- } catch (const py::error_already_set &) {
- // --> Python system error
- // Only new tuples (refcount == 1) are mutable
- auto new_t = py::tuple(3);
- for (size_t i = 0; i < new_t.size(); ++i) {
- new_t[i] = i;
- }
- return new_t;
- }
- return py::tuple();
- });
-
- m.def("test_accessor_assignment", []() {
- auto l = py::list(1);
- l[0] = 0;
-
- auto d = py::dict();
- d["get"] = l[0];
- auto var = l[0];
- d["deferred_get"] = var;
- l[0] = 1;
- d["set"] = l[0];
- var = 99; // this assignment should not overwrite l[0]
- d["deferred_set"] = l[0];
- d["var"] = var;
-
- return d;
- });
-
- bool has_optional = false, has_exp_optional = false;
-#ifdef PYBIND11_HAS_OPTIONAL
- has_optional = true;
- using opt_int = std::optional<int>;
- m.def("double_or_zero", [](const opt_int& x) -> int {
- return x.value_or(0) * 2;
- });
- m.def("half_or_none", [](int x) -> opt_int {
- return x ? opt_int(x / 2) : opt_int();
- });
- m.def("test_nullopt", [](opt_int x) {
- return x.value_or(42);
- }, py::arg_v("x", std::nullopt, "None"));
-#endif
-
-#ifdef PYBIND11_HAS_EXP_OPTIONAL
- has_exp_optional = true;
- using exp_opt_int = std::experimental::optional<int>;
- m.def("double_or_zero_exp", [](const exp_opt_int& x) -> int {
- return x.value_or(0) * 2;
- });
- m.def("half_or_none_exp", [](int x) -> exp_opt_int {
- return x ? exp_opt_int(x / 2) : exp_opt_int();
- });
- m.def("test_nullopt_exp", [](exp_opt_int x) {
- return x.value_or(42);
- }, py::arg_v("x", std::experimental::nullopt, "None"));
-#endif
-
- m.attr("has_optional") = has_optional;
- m.attr("has_exp_optional") = has_exp_optional;
-
- m.def("test_default_constructors", []() {
- return py::dict(
- "str"_a=py::str(),
- "bool"_a=py::bool_(),
- "int"_a=py::int_(),
- "float"_a=py::float_(),
- "tuple"_a=py::tuple(),
- "list"_a=py::list(),
- "dict"_a=py::dict(),
- "set"_a=py::set()
- );
- });
-
- m.def("test_converting_constructors", [](py::dict d) {
- return py::dict(
- "str"_a=py::str(d["str"]),
- "bool"_a=py::bool_(d["bool"]),
- "int"_a=py::int_(d["int"]),
- "float"_a=py::float_(d["float"]),
- "tuple"_a=py::tuple(d["tuple"]),
- "list"_a=py::list(d["list"]),
- "dict"_a=py::dict(d["dict"]),
- "set"_a=py::set(d["set"]),
- "memoryview"_a=py::memoryview(d["memoryview"])
- );
- });
-
- m.def("test_cast_functions", [](py::dict d) {
- // When converting between Python types, obj.cast<T>() should be the same as T(obj)
- return py::dict(
- "str"_a=d["str"].cast<py::str>(),
- "bool"_a=d["bool"].cast<py::bool_>(),
- "int"_a=d["int"].cast<py::int_>(),
- "float"_a=d["float"].cast<py::float_>(),
- "tuple"_a=d["tuple"].cast<py::tuple>(),
- "list"_a=d["list"].cast<py::list>(),
- "dict"_a=d["dict"].cast<py::dict>(),
- "set"_a=d["set"].cast<py::set>(),
- "memoryview"_a=d["memoryview"].cast<py::memoryview>()
- );
- });
-
- py::class_<MoveOutContainer::Value>(m, "MoveOutContainerValue")
- .def_readonly("value", &MoveOutContainer::Value::value);
-
- py::class_<MoveOutContainer>(m, "MoveOutContainer")
- .def(py::init<>())
- .def_property_readonly("move_list", &MoveOutContainer::move_list);
-
- m.def("get_implicit_casting", []() {
- py::dict d;
- d["char*_i1"] = "abc";
- const char *c2 = "abc";
- d["char*_i2"] = c2;
- d["char*_e"] = py::cast(c2);
- d["char*_p"] = py::str(c2);
-
- d["int_i1"] = 42;
- int i = 42;
- d["int_i2"] = i;
- i++;
- d["int_e"] = py::cast(i);
- i++;
- d["int_p"] = py::int_(i);
-
- d["str_i1"] = std::string("str");
- std::string s2("str1");
- d["str_i2"] = s2;
- s2[3] = '2';
- d["str_e"] = py::cast(s2);
- s2[3] = '3';
- d["str_p"] = py::str(s2);
-
- py::list l(2);
- l[0] = 3;
- l[1] = py::cast(6);
- l.append(9);
- l.append(py::cast(12));
- l.append(py::int_(15));
-
- return py::dict(
- "d"_a=d,
- "l"_a=l
- );
- });
-
- // Some test characters in utf16 and utf32 encodings. The last one (the 𝐀) contains a null byte
- char32_t a32 = 0x61 /*a*/, z32 = 0x7a /*z*/, ib32 = 0x203d /*‽*/, cake32 = 0x1f382 /*🎂*/, mathbfA32 = 0x1d400 /*𝐀*/;
- char16_t b16 = 0x62 /*b*/, z16 = 0x7a, ib16 = 0x203d, cake16_1 = 0xd83c, cake16_2 = 0xdf82, mathbfA16_1 = 0xd835, mathbfA16_2 = 0xdc00;
- std::wstring wstr;
- wstr.push_back(0x61); // a
- wstr.push_back(0x2e18); // ⸘
- if (sizeof(wchar_t) == 2) { wstr.push_back(mathbfA16_1); wstr.push_back(mathbfA16_2); } // 𝐀, utf16
- else { wstr.push_back((wchar_t) mathbfA32); } // 𝐀, utf32
- wstr.push_back(0x7a); // z
-
- m.def("good_utf8_string", []() { return std::string(u8"Say utf8\u203d \U0001f382 \U0001d400"); }); // Say utf8‽ 🎂 𝐀
- m.def("good_utf16_string", [=]() { return std::u16string({ b16, ib16, cake16_1, cake16_2, mathbfA16_1, mathbfA16_2, z16 }); }); // b‽🎂𝐀z
- m.def("good_utf32_string", [=]() { return std::u32string({ a32, mathbfA32, cake32, ib32, z32 }); }); // a𝐀🎂‽z
- m.def("good_wchar_string", [=]() { return wstr; }); // a‽𝐀z
- m.def("bad_utf8_string", []() { return std::string("abc\xd0" "def"); });
- m.def("bad_utf16_string", [=]() { return std::u16string({ b16, char16_t(0xd800), z16 }); });
- // Under Python 2.7, invalid unicode UTF-32 characters don't appear to trigger UnicodeDecodeError
- if (PY_MAJOR_VERSION >= 3)
- m.def("bad_utf32_string", [=]() { return std::u32string({ a32, char32_t(0xd800), z32 }); });
- if (PY_MAJOR_VERSION >= 3 || sizeof(wchar_t) == 2)
- m.def("bad_wchar_string", [=]() { return std::wstring({ wchar_t(0x61), wchar_t(0xd800) }); });
- m.def("u8_Z", []() -> char { return 'Z'; });
- m.def("u8_eacute", []() -> char { return '\xe9'; });
- m.def("u16_ibang", [=]() -> char16_t { return ib16; });
- m.def("u32_mathbfA", [=]() -> char32_t { return mathbfA32; });
- m.def("wchar_heart", []() -> wchar_t { return 0x2665; });
-
- m.attr("wchar_size") = py::cast(sizeof(wchar_t));
- m.def("ord_char", [](char c) -> int { return static_cast<unsigned char>(c); });
- m.def("ord_char16", [](char16_t c) -> uint16_t { return c; });
- m.def("ord_char32", [](char32_t c) -> uint32_t { return c; });
- m.def("ord_wchar", [](wchar_t c) -> int { return c; });
-
- m.def("return_none_string", []() -> std::string * { return nullptr; });
- m.def("return_none_char", []() -> const char * { return nullptr; });
- m.def("return_none_bool", []() -> bool * { return nullptr; });
- m.def("return_none_int", []() -> int * { return nullptr; });
- m.def("return_none_float", []() -> float * { return nullptr; });
-
- m.def("return_capsule_with_destructor",
- []() {
- py::print("creating capsule");
- return py::capsule([]() {
- py::print("destructing capsule");
- });
- }
- );
-
- m.def("return_capsule_with_destructor_2",
- []() {
- py::print("creating capsule");
- return py::capsule((void *) 1234, [](void *ptr) {
- py::print("destructing capsule: {}"_s.format((size_t) ptr));
- });
- }
- );
-});
-
-#if defined(_MSC_VER)
-# pragma warning(pop)
-#endif
diff --git a/ext/pybind11/tests/test_python_types.py b/ext/pybind11/tests/test_python_types.py
deleted file mode 100644
index 7956c7cc6..000000000
--- a/ext/pybind11/tests/test_python_types.py
+++ /dev/null
@@ -1,535 +0,0 @@
-# Python < 3 needs this: coding=utf-8
-import pytest
-
-from pybind11_tests import ExamplePythonTypes, ConstructorStats, has_optional, has_exp_optional
-
-
-def test_repr():
- # In Python 3.3+, repr() accesses __qualname__
- assert "pybind11_type" in repr(type(ExamplePythonTypes))
- assert "ExamplePythonTypes" in repr(ExamplePythonTypes)
-
-
-def test_static():
- ExamplePythonTypes.value = 15
- assert ExamplePythonTypes.value == 15
- assert ExamplePythonTypes.value2 == 5
-
- with pytest.raises(AttributeError) as excinfo:
- ExamplePythonTypes.value2 = 15
- assert str(excinfo.value) == "can't set attribute"
-
-
-def test_instance(capture):
- with pytest.raises(TypeError) as excinfo:
- ExamplePythonTypes()
- assert str(excinfo.value) == "pybind11_tests.ExamplePythonTypes: No constructor defined!"
-
- instance = ExamplePythonTypes.new_instance()
-
- with capture:
- dict_result = instance.get_dict()
- dict_result['key2'] = 'value2'
- instance.print_dict(dict_result)
- assert capture.unordered == """
- key: key, value=value
- key: key2, value=value2
- """
- with capture:
- dict_result = instance.get_dict_2()
- dict_result['key2'] = 'value2'
- instance.print_dict_2(dict_result)
- assert capture.unordered == """
- key: key, value=value
- key: key2, value=value2
- """
- with capture:
- set_result = instance.get_set()
- set_result.add('key4')
- instance.print_set(set_result)
- assert capture.unordered == """
- key: key1
- key: key2
- key: key3
- key: key4
- """
- with capture:
- set_result = instance.get_set2()
- set_result.add('key3')
- instance.print_set_2(set_result)
- assert capture.unordered == """
- key: key1
- key: key2
- key: key3
- """
- with capture:
- list_result = instance.get_list()
- list_result.append('value2')
- instance.print_list(list_result)
- assert capture.unordered == """
- Entry at position 0: value
- list item 0: overwritten
- list item 1: value2
- """
- with capture:
- list_result = instance.get_list_2()
- list_result.append('value2')
- instance.print_list_2(list_result)
- assert capture.unordered == """
- list item 0: value
- list item 1: value2
- """
- with capture:
- list_result = instance.get_list_2()
- list_result.append('value2')
- instance.print_list_2(tuple(list_result))
- assert capture.unordered == """
- list item 0: value
- list item 1: value2
- """
- array_result = instance.get_array()
- assert array_result == ['array entry 1', 'array entry 2']
- with capture:
- instance.print_array(array_result)
- assert capture.unordered == """
- array item 0: array entry 1
- array item 1: array entry 2
- """
- varray_result = instance.get_valarray()
- assert varray_result == [1, 4, 9]
- with capture:
- instance.print_valarray(varray_result)
- assert capture.unordered == """
- valarray item 0: 1
- valarray item 1: 4
- valarray item 2: 9
- """
- with pytest.raises(RuntimeError) as excinfo:
- instance.throw_exception()
- assert str(excinfo.value) == "This exception was intentionally thrown."
-
- assert instance.pair_passthrough((True, "test")) == ("test", True)
- assert instance.tuple_passthrough((True, "test", 5)) == (5, "test", True)
- # Any sequence can be cast to a std::pair or std::tuple
- assert instance.pair_passthrough([True, "test"]) == ("test", True)
- assert instance.tuple_passthrough([True, "test", 5]) == (5, "test", True)
-
- assert instance.get_bytes_from_string().decode() == "foo"
- assert instance.get_bytes_from_str().decode() == "bar"
- assert instance.get_str_from_string().encode().decode() == "baz"
- assert instance.get_str_from_bytes().encode().decode() == "boo"
-
- class A(object):
- def __str__(self):
- return "this is a str"
-
- def __repr__(self):
- return "this is a repr"
-
- with capture:
- instance.test_print(A())
- assert capture == """
- this is a str
- this is a repr
- """
-
- cstats = ConstructorStats.get(ExamplePythonTypes)
- assert cstats.alive() == 1
- del instance
- assert cstats.alive() == 0
-
-
-# PyPy does not seem to propagate the tp_docs field at the moment
-def test_class_docs(doc):
- assert doc(ExamplePythonTypes) == "Example 2 documentation"
-
-
-def test_method_docs(doc):
- assert doc(ExamplePythonTypes.get_dict) == """
- get_dict(self: m.ExamplePythonTypes) -> dict
-
- Return a Python dictionary
- """
- assert doc(ExamplePythonTypes.get_dict_2) == """
- get_dict_2(self: m.ExamplePythonTypes) -> Dict[str, str]
-
- Return a C++ dictionary
- """
- assert doc(ExamplePythonTypes.get_list) == """
- get_list(self: m.ExamplePythonTypes) -> list
-
- Return a Python list
- """
- assert doc(ExamplePythonTypes.get_list_2) == """
- get_list_2(self: m.ExamplePythonTypes) -> List[str]
-
- Return a C++ list
- """
- assert doc(ExamplePythonTypes.get_dict) == """
- get_dict(self: m.ExamplePythonTypes) -> dict
-
- Return a Python dictionary
- """
- assert doc(ExamplePythonTypes.get_set) == """
- get_set(self: m.ExamplePythonTypes) -> set
-
- Return a Python set
- """
- assert doc(ExamplePythonTypes.get_set2) == """
- get_set2(self: m.ExamplePythonTypes) -> Set[str]
-
- Return a C++ set
- """
- assert doc(ExamplePythonTypes.get_array) == """
- get_array(self: m.ExamplePythonTypes) -> List[str[2]]
-
- Return a C++ array
- """
- assert doc(ExamplePythonTypes.get_valarray) == """
- get_valarray(self: m.ExamplePythonTypes) -> List[int]
-
- Return a C++ valarray
- """
- assert doc(ExamplePythonTypes.print_dict) == """
- print_dict(self: m.ExamplePythonTypes, arg0: dict) -> None
-
- Print entries of a Python dictionary
- """
- assert doc(ExamplePythonTypes.print_dict_2) == """
- print_dict_2(self: m.ExamplePythonTypes, arg0: Dict[str, str]) -> None
-
- Print entries of a C++ dictionary
- """
- assert doc(ExamplePythonTypes.print_set) == """
- print_set(self: m.ExamplePythonTypes, arg0: set) -> None
-
- Print entries of a Python set
- """
- assert doc(ExamplePythonTypes.print_set_2) == """
- print_set_2(self: m.ExamplePythonTypes, arg0: Set[str]) -> None
-
- Print entries of a C++ set
- """
- assert doc(ExamplePythonTypes.print_list) == """
- print_list(self: m.ExamplePythonTypes, arg0: list) -> None
-
- Print entries of a Python list
- """
- assert doc(ExamplePythonTypes.print_list_2) == """
- print_list_2(self: m.ExamplePythonTypes, arg0: List[str]) -> None
-
- Print entries of a C++ list
- """
- assert doc(ExamplePythonTypes.print_array) == """
- print_array(self: m.ExamplePythonTypes, arg0: List[str[2]]) -> None
-
- Print entries of a C++ array
- """
- assert doc(ExamplePythonTypes.pair_passthrough) == """
- pair_passthrough(self: m.ExamplePythonTypes, arg0: Tuple[bool, str]) -> Tuple[str, bool]
-
- Return a pair in reversed order
- """
- assert doc(ExamplePythonTypes.tuple_passthrough) == """
- tuple_passthrough(self: m.ExamplePythonTypes, arg0: Tuple[bool, str, int]) -> Tuple[int, str, bool]
-
- Return a triple in reversed order
- """ # noqa: E501 line too long
- assert doc(ExamplePythonTypes.throw_exception) == """
- throw_exception(self: m.ExamplePythonTypes) -> None
-
- Throw an exception
- """
- assert doc(ExamplePythonTypes.new_instance) == """
- new_instance() -> m.ExamplePythonTypes
-
- Return an instance
- """
-
-
-def test_module():
- import pybind11_tests
-
- assert pybind11_tests.__name__ == "pybind11_tests"
- assert ExamplePythonTypes.__name__ == "ExamplePythonTypes"
- assert ExamplePythonTypes.__module__ == "pybind11_tests"
- assert ExamplePythonTypes.get_set.__name__ == "get_set"
- assert ExamplePythonTypes.get_set.__module__ == "pybind11_tests"
-
-
-def test_print(capture):
- from pybind11_tests import test_print_function
-
- with capture:
- test_print_function()
- assert capture == """
- Hello, World!
- 1 2.0 three True -- multiple args
- *args-and-a-custom-separator
- no new line here -- next print
- flush
- py::print + str.format = this
- """
- assert capture.stderr == "this goes to stderr"
-
-
-def test_str_api():
- from pybind11_tests import test_str_format
-
- s1, s2 = test_str_format()
- assert s1 == "1 + 2 = 3"
- assert s1 == s2
-
-
-def test_dict_api():
- from pybind11_tests import test_dict_keyword_constructor
-
- assert test_dict_keyword_constructor() == {"x": 1, "y": 2, "z": 3}
-
-
-def test_accessors():
- from pybind11_tests import test_accessor_api, test_tuple_accessor, test_accessor_assignment
-
- class SubTestObject:
- attr_obj = 1
- attr_char = 2
-
- class TestObject:
- basic_attr = 1
- begin_end = [1, 2, 3]
- d = {"operator[object]": 1, "operator[char *]": 2}
- sub = SubTestObject()
-
- def func(self, x, *args):
- return self.basic_attr + x + sum(args)
-
- d = test_accessor_api(TestObject())
- assert d["basic_attr"] == 1
- assert d["begin_end"] == [1, 2, 3]
- assert d["operator[object]"] == 1
- assert d["operator[char *]"] == 2
- assert d["attr(object)"] == 1
- assert d["attr(char *)"] == 2
- assert d["missing_attr_ptr"] == "raised"
- assert d["missing_attr_chain"] == "raised"
- assert d["is_none"] is False
- assert d["operator()"] == 2
- assert d["operator*"] == 7
-
- assert test_tuple_accessor(tuple()) == (0, 1, 2)
-
- d = test_accessor_assignment()
- assert d["get"] == 0
- assert d["deferred_get"] == 0
- assert d["set"] == 1
- assert d["deferred_set"] == 1
- assert d["var"] == 99
-
-
-@pytest.mark.skipif(not has_optional, reason='no <optional>')
-def test_optional():
- from pybind11_tests import double_or_zero, half_or_none, test_nullopt
-
- assert double_or_zero(None) == 0
- assert double_or_zero(42) == 84
- pytest.raises(TypeError, double_or_zero, 'foo')
-
- assert half_or_none(0) is None
- assert half_or_none(42) == 21
- pytest.raises(TypeError, half_or_none, 'foo')
-
- assert test_nullopt() == 42
- assert test_nullopt(None) == 42
- assert test_nullopt(42) == 42
- assert test_nullopt(43) == 43
-
-
-@pytest.mark.skipif(not has_exp_optional, reason='no <experimental/optional>')
-def test_exp_optional():
- from pybind11_tests import double_or_zero_exp, half_or_none_exp, test_nullopt_exp
-
- assert double_or_zero_exp(None) == 0
- assert double_or_zero_exp(42) == 84
- pytest.raises(TypeError, double_or_zero_exp, 'foo')
-
- assert half_or_none_exp(0) is None
- assert half_or_none_exp(42) == 21
- pytest.raises(TypeError, half_or_none_exp, 'foo')
-
- assert test_nullopt_exp() == 42
- assert test_nullopt_exp(None) == 42
- assert test_nullopt_exp(42) == 42
- assert test_nullopt_exp(43) == 43
-
-
-def test_constructors():
- """C++ default and converting constructors are equivalent to type calls in Python"""
- from pybind11_tests import (test_default_constructors, test_converting_constructors,
- test_cast_functions)
-
- types = [str, bool, int, float, tuple, list, dict, set]
- expected = {t.__name__: t() for t in types}
- assert test_default_constructors() == expected
-
- data = {
- str: 42,
- bool: "Not empty",
- int: "42",
- float: "+1e3",
- tuple: range(3),
- list: range(3),
- dict: [("two", 2), ("one", 1), ("three", 3)],
- set: [4, 4, 5, 6, 6, 6],
- memoryview: b'abc'
- }
- inputs = {k.__name__: v for k, v in data.items()}
- expected = {k.__name__: k(v) for k, v in data.items()}
- assert test_converting_constructors(inputs) == expected
- assert test_cast_functions(inputs) == expected
-
-
-def test_move_out_container():
- """Properties use the `reference_internal` policy by default. If the underlying function
- returns an rvalue, the policy is automatically changed to `move` to avoid referencing
- a temporary. In case the return value is a container of user-defined types, the policy
- also needs to be applied to the elements, not just the container."""
- from pybind11_tests import MoveOutContainer
-
- c = MoveOutContainer()
- moved_out_list = c.move_list
- assert [x.value for x in moved_out_list] == [0, 1, 2]
-
-
-def test_implicit_casting():
- """Tests implicit casting when assigning or appending to dicts and lists."""
- from pybind11_tests import get_implicit_casting
-
- z = get_implicit_casting()
- assert z['d'] == {
- 'char*_i1': 'abc', 'char*_i2': 'abc', 'char*_e': 'abc', 'char*_p': 'abc',
- 'str_i1': 'str', 'str_i2': 'str1', 'str_e': 'str2', 'str_p': 'str3',
- 'int_i1': 42, 'int_i2': 42, 'int_e': 43, 'int_p': 44
- }
- assert z['l'] == [3, 6, 9, 12, 15]
-
-
-def test_unicode_conversion():
- """Tests unicode conversion and error reporting."""
- import pybind11_tests
- from pybind11_tests import (good_utf8_string, bad_utf8_string,
- good_utf16_string, bad_utf16_string,
- good_utf32_string, # bad_utf32_string,
- good_wchar_string, # bad_wchar_string,
- u8_Z, u8_eacute, u16_ibang, u32_mathbfA, wchar_heart)
-
- assert good_utf8_string() == u"Say utf8‽ 🎂 𝐀"
- assert good_utf16_string() == u"b‽🎂𝐀z"
- assert good_utf32_string() == u"a𝐀🎂‽z"
- assert good_wchar_string() == u"a⸘𝐀z"
-
- with pytest.raises(UnicodeDecodeError):
- bad_utf8_string()
-
- with pytest.raises(UnicodeDecodeError):
- bad_utf16_string()
-
- # These are provided only if they actually fail (they don't when 32-bit and under Python 2.7)
- if hasattr(pybind11_tests, "bad_utf32_string"):
- with pytest.raises(UnicodeDecodeError):
- pybind11_tests.bad_utf32_string()
- if hasattr(pybind11_tests, "bad_wchar_string"):
- with pytest.raises(UnicodeDecodeError):
- pybind11_tests.bad_wchar_string()
-
- assert u8_Z() == 'Z'
- assert u8_eacute() == u'é'
- assert u16_ibang() == u'‽'
- assert u32_mathbfA() == u'𝐀'
- assert wchar_heart() == u'♥'
-
-
-def test_single_char_arguments():
- """Tests failures for passing invalid inputs to char-accepting functions"""
- from pybind11_tests import ord_char, ord_char16, ord_char32, ord_wchar, wchar_size
-
- def toobig_message(r):
- return "Character code point not in range({0:#x})".format(r)
- toolong_message = "Expected a character, but multi-character string found"
-
- assert ord_char(u'a') == 0x61 # simple ASCII
- assert ord_char(u'é') == 0xE9 # requires 2 bytes in utf-8, but can be stuffed in a char
- with pytest.raises(ValueError) as excinfo:
- assert ord_char(u'Ā') == 0x100 # requires 2 bytes, doesn't fit in a char
- assert str(excinfo.value) == toobig_message(0x100)
- with pytest.raises(ValueError) as excinfo:
- assert ord_char(u'ab')
- assert str(excinfo.value) == toolong_message
-
- assert ord_char16(u'a') == 0x61
- assert ord_char16(u'é') == 0xE9
- assert ord_char16(u'Ā') == 0x100
- assert ord_char16(u'‽') == 0x203d
- assert ord_char16(u'♥') == 0x2665
- with pytest.raises(ValueError) as excinfo:
- assert ord_char16(u'🎂') == 0x1F382 # requires surrogate pair
- assert str(excinfo.value) == toobig_message(0x10000)
- with pytest.raises(ValueError) as excinfo:
- assert ord_char16(u'aa')
- assert str(excinfo.value) == toolong_message
-
- assert ord_char32(u'a') == 0x61
- assert ord_char32(u'é') == 0xE9
- assert ord_char32(u'Ā') == 0x100
- assert ord_char32(u'‽') == 0x203d
- assert ord_char32(u'♥') == 0x2665
- assert ord_char32(u'🎂') == 0x1F382
- with pytest.raises(ValueError) as excinfo:
- assert ord_char32(u'aa')
- assert str(excinfo.value) == toolong_message
-
- assert ord_wchar(u'a') == 0x61
- assert ord_wchar(u'é') == 0xE9
- assert ord_wchar(u'Ā') == 0x100
- assert ord_wchar(u'‽') == 0x203d
- assert ord_wchar(u'♥') == 0x2665
- if wchar_size == 2:
- with pytest.raises(ValueError) as excinfo:
- assert ord_wchar(u'🎂') == 0x1F382 # requires surrogate pair
- assert str(excinfo.value) == toobig_message(0x10000)
- else:
- assert ord_wchar(u'🎂') == 0x1F382
- with pytest.raises(ValueError) as excinfo:
- assert ord_wchar(u'aa')
- assert str(excinfo.value) == toolong_message
-
-
-def test_builtins_cast_return_none():
- """Casters produced with PYBIND11_TYPE_CASTER() should convert nullptr to None"""
- import pybind11_tests as m
-
- assert m.return_none_string() is None
- assert m.return_none_char() is None
- assert m.return_none_bool() is None
- assert m.return_none_int() is None
- assert m.return_none_float() is None
-
-
-def test_capsule_with_destructor(capture):
- import pybind11_tests as m
- with capture:
- a = m.return_capsule_with_destructor()
- del a
- pytest.gc_collect()
- assert capture.unordered == """
- creating capsule
- destructing capsule
- """
-
- with capture:
- a = m.return_capsule_with_destructor_2()
- del a
- pytest.gc_collect()
- assert capture.unordered == """
- creating capsule
- destructing capsule: 1234
- """
diff --git a/ext/pybind11/tests/test_pytypes.cpp b/ext/pybind11/tests/test_pytypes.cpp
new file mode 100644
index 000000000..a962f0ccc
--- /dev/null
+++ b/ext/pybind11/tests/test_pytypes.cpp
@@ -0,0 +1,272 @@
+/*
+ tests/test_pytypes.cpp -- Python type casters
+
+ Copyright (c) 2017 Wenzel Jakob <wenzel.jakob@epfl.ch>
+
+ All rights reserved. Use of this source code is governed by a
+ BSD-style license that can be found in the LICENSE file.
+*/
+
+#include "pybind11_tests.h"
+
+
+TEST_SUBMODULE(pytypes, m) {
+ // test_list
+ m.def("get_list", []() {
+ py::list list;
+ list.append("value");
+ py::print("Entry at position 0:", list[0]);
+ list[0] = py::str("overwritten");
+ return list;
+ });
+ m.def("print_list", [](py::list list) {
+ int index = 0;
+ for (auto item : list)
+ py::print("list item {}: {}"_s.format(index++, item));
+ });
+
+ // test_set
+ m.def("get_set", []() {
+ py::set set;
+ set.add(py::str("key1"));
+ set.add("key2");
+ set.add(std::string("key3"));
+ return set;
+ });
+ m.def("print_set", [](py::set set) {
+ for (auto item : set)
+ py::print("key:", item);
+ });
+
+ // test_dict
+ m.def("get_dict", []() { return py::dict("key"_a="value"); });
+ m.def("print_dict", [](py::dict dict) {
+ for (auto item : dict)
+ py::print("key: {}, value={}"_s.format(item.first, item.second));
+ });
+ m.def("dict_keyword_constructor", []() {
+ auto d1 = py::dict("x"_a=1, "y"_a=2);
+ auto d2 = py::dict("z"_a=3, **d1);
+ return d2;
+ });
+
+ // test_str
+ m.def("str_from_string", []() { return py::str(std::string("baz")); });
+ m.def("str_from_bytes", []() { return py::str(py::bytes("boo", 3)); });
+ m.def("str_from_object", [](const py::object& obj) { return py::str(obj); });
+ m.def("repr_from_object", [](const py::object& obj) { return py::repr(obj); });
+
+ m.def("str_format", []() {
+ auto s1 = "{} + {} = {}"_s.format(1, 2, 3);
+ auto s2 = "{a} + {b} = {c}"_s.format("a"_a=1, "b"_a=2, "c"_a=3);
+ return py::make_tuple(s1, s2);
+ });
+
+ // test_bytes
+ m.def("bytes_from_string", []() { return py::bytes(std::string("foo")); });
+ m.def("bytes_from_str", []() { return py::bytes(py::str("bar", 3)); });
+
+ // test_capsule
+ m.def("return_capsule_with_destructor", []() {
+ py::print("creating capsule");
+ return py::capsule([]() {
+ py::print("destructing capsule");
+ });
+ });
+
+ m.def("return_capsule_with_destructor_2", []() {
+ py::print("creating capsule");
+ return py::capsule((void *) 1234, [](void *ptr) {
+ py::print("destructing capsule: {}"_s.format((size_t) ptr));
+ });
+ });
+
+ m.def("return_capsule_with_name_and_destructor", []() {
+ auto capsule = py::capsule((void *) 1234, "pointer type description", [](PyObject *ptr) {
+ if (ptr) {
+ auto name = PyCapsule_GetName(ptr);
+ py::print("destructing capsule ({}, '{}')"_s.format(
+ (size_t) PyCapsule_GetPointer(ptr, name), name
+ ));
+ }
+ });
+ void *contents = capsule;
+ py::print("created capsule ({}, '{}')"_s.format((size_t) contents, capsule.name()));
+ return capsule;
+ });
+
+ // test_accessors
+ m.def("accessor_api", [](py::object o) {
+ auto d = py::dict();
+
+ d["basic_attr"] = o.attr("basic_attr");
+
+ auto l = py::list();
+ for (const auto &item : o.attr("begin_end")) {
+ l.append(item);
+ }
+ d["begin_end"] = l;
+
+ d["operator[object]"] = o.attr("d")["operator[object]"_s];
+ d["operator[char *]"] = o.attr("d")["operator[char *]"];
+
+ d["attr(object)"] = o.attr("sub").attr("attr_obj");
+ d["attr(char *)"] = o.attr("sub").attr("attr_char");
+ try {
+ o.attr("sub").attr("missing").ptr();
+ } catch (const py::error_already_set &) {
+ d["missing_attr_ptr"] = "raised"_s;
+ }
+ try {
+ o.attr("missing").attr("doesn't matter");
+ } catch (const py::error_already_set &) {
+ d["missing_attr_chain"] = "raised"_s;
+ }
+
+ d["is_none"] = o.attr("basic_attr").is_none();
+
+ d["operator()"] = o.attr("func")(1);
+ d["operator*"] = o.attr("func")(*o.attr("begin_end"));
+
+ // Test implicit conversion
+ py::list implicit_list = o.attr("begin_end");
+ d["implicit_list"] = implicit_list;
+ py::dict implicit_dict = o.attr("__dict__");
+ d["implicit_dict"] = implicit_dict;
+
+ return d;
+ });
+
+ m.def("tuple_accessor", [](py::tuple existing_t) {
+ try {
+ existing_t[0] = 1;
+ } catch (const py::error_already_set &) {
+ // --> Python system error
+ // Only new tuples (refcount == 1) are mutable
+ auto new_t = py::tuple(3);
+ for (size_t i = 0; i < new_t.size(); ++i) {
+ new_t[i] = i;
+ }
+ return new_t;
+ }
+ return py::tuple();
+ });
+
+ m.def("accessor_assignment", []() {
+ auto l = py::list(1);
+ l[0] = 0;
+
+ auto d = py::dict();
+ d["get"] = l[0];
+ auto var = l[0];
+ d["deferred_get"] = var;
+ l[0] = 1;
+ d["set"] = l[0];
+ var = 99; // this assignment should not overwrite l[0]
+ d["deferred_set"] = l[0];
+ d["var"] = var;
+
+ return d;
+ });
+
+ // test_constructors
+ m.def("default_constructors", []() {
+ return py::dict(
+ "str"_a=py::str(),
+ "bool"_a=py::bool_(),
+ "int"_a=py::int_(),
+ "float"_a=py::float_(),
+ "tuple"_a=py::tuple(),
+ "list"_a=py::list(),
+ "dict"_a=py::dict(),
+ "set"_a=py::set()
+ );
+ });
+
+ m.def("converting_constructors", [](py::dict d) {
+ return py::dict(
+ "str"_a=py::str(d["str"]),
+ "bool"_a=py::bool_(d["bool"]),
+ "int"_a=py::int_(d["int"]),
+ "float"_a=py::float_(d["float"]),
+ "tuple"_a=py::tuple(d["tuple"]),
+ "list"_a=py::list(d["list"]),
+ "dict"_a=py::dict(d["dict"]),
+ "set"_a=py::set(d["set"]),
+ "memoryview"_a=py::memoryview(d["memoryview"])
+ );
+ });
+
+ m.def("cast_functions", [](py::dict d) {
+ // When converting between Python types, obj.cast<T>() should be the same as T(obj)
+ return py::dict(
+ "str"_a=d["str"].cast<py::str>(),
+ "bool"_a=d["bool"].cast<py::bool_>(),
+ "int"_a=d["int"].cast<py::int_>(),
+ "float"_a=d["float"].cast<py::float_>(),
+ "tuple"_a=d["tuple"].cast<py::tuple>(),
+ "list"_a=d["list"].cast<py::list>(),
+ "dict"_a=d["dict"].cast<py::dict>(),
+ "set"_a=d["set"].cast<py::set>(),
+ "memoryview"_a=d["memoryview"].cast<py::memoryview>()
+ );
+ });
+
+ m.def("get_implicit_casting", []() {
+ py::dict d;
+ d["char*_i1"] = "abc";
+ const char *c2 = "abc";
+ d["char*_i2"] = c2;
+ d["char*_e"] = py::cast(c2);
+ d["char*_p"] = py::str(c2);
+
+ d["int_i1"] = 42;
+ int i = 42;
+ d["int_i2"] = i;
+ i++;
+ d["int_e"] = py::cast(i);
+ i++;
+ d["int_p"] = py::int_(i);
+
+ d["str_i1"] = std::string("str");
+ std::string s2("str1");
+ d["str_i2"] = s2;
+ s2[3] = '2';
+ d["str_e"] = py::cast(s2);
+ s2[3] = '3';
+ d["str_p"] = py::str(s2);
+
+ py::list l(2);
+ l[0] = 3;
+ l[1] = py::cast(6);
+ l.append(9);
+ l.append(py::cast(12));
+ l.append(py::int_(15));
+
+ return py::dict(
+ "d"_a=d,
+ "l"_a=l
+ );
+ });
+
+ // test_print
+ m.def("print_function", []() {
+ py::print("Hello, World!");
+ py::print(1, 2.0, "three", true, std::string("-- multiple args"));
+ auto args = py::make_tuple("and", "a", "custom", "separator");
+ py::print("*args", *args, "sep"_a="-");
+ py::print("no new line here", "end"_a=" -- ");
+ py::print("next print");
+
+ auto py_stderr = py::module::import("sys").attr("stderr");
+ py::print("this goes to stderr", "file"_a=py_stderr);
+
+ py::print("flush", "flush"_a=true);
+
+ py::print("{a} + {b} = {c}"_s.format("a"_a="py::print", "b"_a="str.format", "c"_a="this"));
+ });
+
+ m.def("print_failure", []() { py::print(42, UnregisteredType()); });
+
+ m.def("hash_function", [](py::object obj) { return py::hash(obj); });
+}
diff --git a/ext/pybind11/tests/test_pytypes.py b/ext/pybind11/tests/test_pytypes.py
new file mode 100644
index 000000000..94c90a909
--- /dev/null
+++ b/ext/pybind11/tests/test_pytypes.py
@@ -0,0 +1,240 @@
+import pytest
+import sys
+
+from pybind11_tests import pytypes as m
+from pybind11_tests import debug_enabled
+
+
+def test_list(capture, doc):
+ with capture:
+ l = m.get_list()
+ assert l == ["overwritten"]
+
+ l.append("value2")
+ m.print_list(l)
+ assert capture.unordered == """
+ Entry at position 0: value
+ list item 0: overwritten
+ list item 1: value2
+ """
+
+ assert doc(m.get_list) == "get_list() -> list"
+ assert doc(m.print_list) == "print_list(arg0: list) -> None"
+
+
+def test_set(capture, doc):
+ s = m.get_set()
+ assert s == {"key1", "key2", "key3"}
+
+ with capture:
+ s.add("key4")
+ m.print_set(s)
+ assert capture.unordered == """
+ key: key1
+ key: key2
+ key: key3
+ key: key4
+ """
+
+ assert doc(m.get_list) == "get_list() -> list"
+ assert doc(m.print_list) == "print_list(arg0: list) -> None"
+
+
+def test_dict(capture, doc):
+ d = m.get_dict()
+ assert d == {"key": "value"}
+
+ with capture:
+ d["key2"] = "value2"
+ m.print_dict(d)
+ assert capture.unordered == """
+ key: key, value=value
+ key: key2, value=value2
+ """
+
+ assert doc(m.get_dict) == "get_dict() -> dict"
+ assert doc(m.print_dict) == "print_dict(arg0: dict) -> None"
+
+ assert m.dict_keyword_constructor() == {"x": 1, "y": 2, "z": 3}
+
+
+def test_str(doc):
+ assert m.str_from_string().encode().decode() == "baz"
+ assert m.str_from_bytes().encode().decode() == "boo"
+
+ assert doc(m.str_from_bytes) == "str_from_bytes() -> str"
+
+ class A(object):
+ def __str__(self):
+ return "this is a str"
+
+ def __repr__(self):
+ return "this is a repr"
+
+ assert m.str_from_object(A()) == "this is a str"
+ assert m.repr_from_object(A()) == "this is a repr"
+
+ s1, s2 = m.str_format()
+ assert s1 == "1 + 2 = 3"
+ assert s1 == s2
+
+
+def test_bytes(doc):
+ assert m.bytes_from_string().decode() == "foo"
+ assert m.bytes_from_str().decode() == "bar"
+
+ assert doc(m.bytes_from_str) == "bytes_from_str() -> {}".format(
+ "bytes" if sys.version_info[0] == 3 else "str"
+ )
+
+
+def test_capsule(capture):
+ pytest.gc_collect()
+ with capture:
+ a = m.return_capsule_with_destructor()
+ del a
+ pytest.gc_collect()
+ assert capture.unordered == """
+ creating capsule
+ destructing capsule
+ """
+
+ with capture:
+ a = m.return_capsule_with_destructor_2()
+ del a
+ pytest.gc_collect()
+ assert capture.unordered == """
+ creating capsule
+ destructing capsule: 1234
+ """
+
+ with capture:
+ a = m.return_capsule_with_name_and_destructor()
+ del a
+ pytest.gc_collect()
+ assert capture.unordered == """
+ created capsule (1234, 'pointer type description')
+ destructing capsule (1234, 'pointer type description')
+ """
+
+
+def test_accessors():
+ class SubTestObject:
+ attr_obj = 1
+ attr_char = 2
+
+ class TestObject:
+ basic_attr = 1
+ begin_end = [1, 2, 3]
+ d = {"operator[object]": 1, "operator[char *]": 2}
+ sub = SubTestObject()
+
+ def func(self, x, *args):
+ return self.basic_attr + x + sum(args)
+
+ d = m.accessor_api(TestObject())
+ assert d["basic_attr"] == 1
+ assert d["begin_end"] == [1, 2, 3]
+ assert d["operator[object]"] == 1
+ assert d["operator[char *]"] == 2
+ assert d["attr(object)"] == 1
+ assert d["attr(char *)"] == 2
+ assert d["missing_attr_ptr"] == "raised"
+ assert d["missing_attr_chain"] == "raised"
+ assert d["is_none"] is False
+ assert d["operator()"] == 2
+ assert d["operator*"] == 7
+ assert d["implicit_list"] == [1, 2, 3]
+ assert all(x in TestObject.__dict__ for x in d["implicit_dict"])
+
+ assert m.tuple_accessor(tuple()) == (0, 1, 2)
+
+ d = m.accessor_assignment()
+ assert d["get"] == 0
+ assert d["deferred_get"] == 0
+ assert d["set"] == 1
+ assert d["deferred_set"] == 1
+ assert d["var"] == 99
+
+
+def test_constructors():
+ """C++ default and converting constructors are equivalent to type calls in Python"""
+ types = [str, bool, int, float, tuple, list, dict, set]
+ expected = {t.__name__: t() for t in types}
+ assert m.default_constructors() == expected
+
+ data = {
+ str: 42,
+ bool: "Not empty",
+ int: "42",
+ float: "+1e3",
+ tuple: range(3),
+ list: range(3),
+ dict: [("two", 2), ("one", 1), ("three", 3)],
+ set: [4, 4, 5, 6, 6, 6],
+ memoryview: b'abc'
+ }
+ inputs = {k.__name__: v for k, v in data.items()}
+ expected = {k.__name__: k(v) for k, v in data.items()}
+
+ assert m.converting_constructors(inputs) == expected
+ assert m.cast_functions(inputs) == expected
+
+ # Converting constructors and cast functions should just reference rather
+ # than copy when no conversion is needed:
+ noconv1 = m.converting_constructors(expected)
+ for k in noconv1:
+ assert noconv1[k] is expected[k]
+
+ noconv2 = m.cast_functions(expected)
+ for k in noconv2:
+ assert noconv2[k] is expected[k]
+
+
+def test_implicit_casting():
+ """Tests implicit casting when assigning or appending to dicts and lists."""
+ z = m.get_implicit_casting()
+ assert z['d'] == {
+ 'char*_i1': 'abc', 'char*_i2': 'abc', 'char*_e': 'abc', 'char*_p': 'abc',
+ 'str_i1': 'str', 'str_i2': 'str1', 'str_e': 'str2', 'str_p': 'str3',
+ 'int_i1': 42, 'int_i2': 42, 'int_e': 43, 'int_p': 44
+ }
+ assert z['l'] == [3, 6, 9, 12, 15]
+
+
+def test_print(capture):
+ with capture:
+ m.print_function()
+ assert capture == """
+ Hello, World!
+ 1 2.0 three True -- multiple args
+ *args-and-a-custom-separator
+ no new line here -- next print
+ flush
+ py::print + str.format = this
+ """
+ assert capture.stderr == "this goes to stderr"
+
+ with pytest.raises(RuntimeError) as excinfo:
+ m.print_failure()
+ assert str(excinfo.value) == "make_tuple(): unable to convert " + (
+ "argument of type 'UnregisteredType' to Python object"
+ if debug_enabled else
+ "arguments to Python object (compile in debug mode for details)"
+ )
+
+
+def test_hash():
+ class Hashable(object):
+ def __init__(self, value):
+ self.value = value
+
+ def __hash__(self):
+ return self.value
+
+ class Unhashable(object):
+ __hash__ = None
+
+ assert m.hash_function(Hashable(42)) == 42
+ with pytest.raises(TypeError):
+ m.hash_function(Unhashable())
diff --git a/ext/pybind11/tests/test_sequences_and_iterators.cpp b/ext/pybind11/tests/test_sequences_and_iterators.cpp
index c2051fadb..a45521256 100644
--- a/ext/pybind11/tests/test_sequences_and_iterators.cpp
+++ b/ext/pybind11/tests/test_sequences_and_iterators.cpp
@@ -13,146 +13,6 @@
#include <pybind11/operators.h>
#include <pybind11/stl.h>
-class Sequence {
-public:
- Sequence(size_t size) : m_size(size) {
- print_created(this, "of size", m_size);
- m_data = new float[size];
- memset(m_data, 0, sizeof(float) * size);
- }
-
- Sequence(const std::vector<float> &value) : m_size(value.size()) {
- print_created(this, "of size", m_size, "from std::vector");
- m_data = new float[m_size];
- memcpy(m_data, &value[0], sizeof(float) * m_size);
- }
-
- Sequence(const Sequence &s) : m_size(s.m_size) {
- print_copy_created(this);
- m_data = new float[m_size];
- memcpy(m_data, s.m_data, sizeof(float)*m_size);
- }
-
- Sequence(Sequence &&s) : m_size(s.m_size), m_data(s.m_data) {
- print_move_created(this);
- s.m_size = 0;
- s.m_data = nullptr;
- }
-
- ~Sequence() {
- print_destroyed(this);
- delete[] m_data;
- }
-
- Sequence &operator=(const Sequence &s) {
- if (&s != this) {
- delete[] m_data;
- m_size = s.m_size;
- m_data = new float[m_size];
- memcpy(m_data, s.m_data, sizeof(float)*m_size);
- }
-
- print_copy_assigned(this);
-
- return *this;
- }
-
- Sequence &operator=(Sequence &&s) {
- if (&s != this) {
- delete[] m_data;
- m_size = s.m_size;
- m_data = s.m_data;
- s.m_size = 0;
- s.m_data = nullptr;
- }
-
- print_move_assigned(this);
-
- return *this;
- }
-
- bool operator==(const Sequence &s) const {
- if (m_size != s.size())
- return false;
- for (size_t i=0; i<m_size; ++i)
- if (m_data[i] != s[i])
- return false;
- return true;
- }
-
- bool operator!=(const Sequence &s) const {
- return !operator==(s);
- }
-
- float operator[](size_t index) const {
- return m_data[index];
- }
-
- float &operator[](size_t index) {
- return m_data[index];
- }
-
- bool contains(float v) const {
- for (size_t i=0; i<m_size; ++i)
- if (v == m_data[i])
- return true;
- return false;
- }
-
- Sequence reversed() const {
- Sequence result(m_size);
- for (size_t i=0; i<m_size; ++i)
- result[m_size-i-1] = m_data[i];
- return result;
- }
-
- size_t size() const { return m_size; }
-
- const float *begin() const { return m_data; }
- const float *end() const { return m_data+m_size; }
-
-private:
- size_t m_size;
- float *m_data;
-};
-
-class IntPairs {
-public:
- IntPairs(std::vector<std::pair<int, int>> data) : data_(std::move(data)) {}
- const std::pair<int, int>* begin() const { return data_.data(); }
-
-private:
- std::vector<std::pair<int, int>> data_;
-};
-
-// Interface of a map-like object that isn't (directly) an unordered_map, but provides some basic
-// map-like functionality.
-class StringMap {
-public:
- StringMap() = default;
- StringMap(std::unordered_map<std::string, std::string> init)
- : map(std::move(init)) {}
-
- void set(std::string key, std::string val) {
- map[key] = val;
- }
-
- std::string get(std::string key) const {
- return map.at(key);
- }
-
- size_t size() const {
- return map.size();
- }
-
-private:
- std::unordered_map<std::string, std::string> map;
-
-public:
- decltype(map.cbegin()) begin() const { return map.cbegin(); }
- decltype(map.cend()) end() const { return map.cend(); }
-};
-
template<typename T>
class NonZeroIterator {
const T* ptr_;
@@ -210,66 +70,164 @@ py::list test_random_access_iterator(PythonType x) {
return checks;
}
-test_initializer sequences_and_iterators([](py::module &pm) {
- auto m = pm.def_submodule("sequences_and_iterators");
+TEST_SUBMODULE(sequences_and_iterators, m) {
+
+ // test_sequence
+ class Sequence {
+ public:
+ Sequence(size_t size) : m_size(size) {
+ print_created(this, "of size", m_size);
+ m_data = new float[size];
+ memset(m_data, 0, sizeof(float) * size);
+ }
+ Sequence(const std::vector<float> &value) : m_size(value.size()) {
+ print_created(this, "of size", m_size, "from std::vector");
+ m_data = new float[m_size];
+ memcpy(m_data, &value[0], sizeof(float) * m_size);
+ }
+ Sequence(const Sequence &s) : m_size(s.m_size) {
+ print_copy_created(this);
+ m_data = new float[m_size];
+ memcpy(m_data, s.m_data, sizeof(float)*m_size);
+ }
+ Sequence(Sequence &&s) : m_size(s.m_size), m_data(s.m_data) {
+ print_move_created(this);
+ s.m_size = 0;
+ s.m_data = nullptr;
+ }
+
+ ~Sequence() { print_destroyed(this); delete[] m_data; }
- py::class_<Sequence> seq(m, "Sequence");
+ Sequence &operator=(const Sequence &s) {
+ if (&s != this) {
+ delete[] m_data;
+ m_size = s.m_size;
+ m_data = new float[m_size];
+ memcpy(m_data, s.m_data, sizeof(float)*m_size);
+ }
+ print_copy_assigned(this);
+ return *this;
+ }
- seq.def(py::init<size_t>())
- .def(py::init<const std::vector<float>&>())
- /// Bare bones interface
- .def("__getitem__", [](const Sequence &s, size_t i) {
- if (i >= s.size())
- throw py::index_error();
+ Sequence &operator=(Sequence &&s) {
+ if (&s != this) {
+ delete[] m_data;
+ m_size = s.m_size;
+ m_data = s.m_data;
+ s.m_size = 0;
+ s.m_data = nullptr;
+ }
+ print_move_assigned(this);
+ return *this;
+ }
+
+ bool operator==(const Sequence &s) const {
+ if (m_size != s.size()) return false;
+ for (size_t i = 0; i < m_size; ++i)
+ if (m_data[i] != s[i])
+ return false;
+ return true;
+ }
+ bool operator!=(const Sequence &s) const { return !operator==(s); }
+
+ float operator[](size_t index) const { return m_data[index]; }
+ float &operator[](size_t index) { return m_data[index]; }
+
+ bool contains(float v) const {
+ for (size_t i = 0; i < m_size; ++i)
+ if (v == m_data[i])
+ return true;
+ return false;
+ }
+
+ Sequence reversed() const {
+ Sequence result(m_size);
+ for (size_t i = 0; i < m_size; ++i)
+ result[m_size - i - 1] = m_data[i];
+ return result;
+ }
+
+ size_t size() const { return m_size; }
+
+ const float *begin() const { return m_data; }
+ const float *end() const { return m_data+m_size; }
+
+ private:
+ size_t m_size;
+ float *m_data;
+ };
+ py::class_<Sequence>(m, "Sequence")
+ .def(py::init<size_t>())
+ .def(py::init<const std::vector<float>&>())
+ /// Bare bones interface
+ .def("__getitem__", [](const Sequence &s, size_t i) {
+ if (i >= s.size()) throw py::index_error();
return s[i];
})
- .def("__setitem__", [](Sequence &s, size_t i, float v) {
- if (i >= s.size())
- throw py::index_error();
+ .def("__setitem__", [](Sequence &s, size_t i, float v) {
+ if (i >= s.size()) throw py::index_error();
s[i] = v;
})
- .def("__len__", &Sequence::size)
- /// Optional sequence protocol operations
- .def("__iter__", [](const Sequence &s) { return py::make_iterator(s.begin(), s.end()); },
- py::keep_alive<0, 1>() /* Essential: keep object alive while iterator exists */)
- .def("__contains__", [](const Sequence &s, float v) { return s.contains(v); })
- .def("__reversed__", [](const Sequence &s) -> Sequence { return s.reversed(); })
- /// Slicing protocol (optional)
- .def("__getitem__", [](const Sequence &s, py::slice slice) -> Sequence* {
+ .def("__len__", &Sequence::size)
+ /// Optional sequence protocol operations
+ .def("__iter__", [](const Sequence &s) { return py::make_iterator(s.begin(), s.end()); },
+ py::keep_alive<0, 1>() /* Essential: keep object alive while iterator exists */)
+ .def("__contains__", [](const Sequence &s, float v) { return s.contains(v); })
+ .def("__reversed__", [](const Sequence &s) -> Sequence { return s.reversed(); })
+ /// Slicing protocol (optional)
+ .def("__getitem__", [](const Sequence &s, py::slice slice) -> Sequence* {
size_t start, stop, step, slicelength;
if (!slice.compute(s.size(), &start, &stop, &step, &slicelength))
throw py::error_already_set();
Sequence *seq = new Sequence(slicelength);
- for (size_t i=0; i<slicelength; ++i) {
+ for (size_t i = 0; i < slicelength; ++i) {
(*seq)[i] = s[start]; start += step;
}
return seq;
})
- .def("__setitem__", [](Sequence &s, py::slice slice, const Sequence &value) {
+ .def("__setitem__", [](Sequence &s, py::slice slice, const Sequence &value) {
size_t start, stop, step, slicelength;
if (!slice.compute(s.size(), &start, &stop, &step, &slicelength))
throw py::error_already_set();
if (slicelength != value.size())
throw std::runtime_error("Left and right hand size of slice assignment have different sizes!");
- for (size_t i=0; i<slicelength; ++i) {
+ for (size_t i = 0; i < slicelength; ++i) {
s[start] = value[i]; start += step;
}
})
- /// Comparisons
- .def(py::self == py::self)
- .def(py::self != py::self);
- // Could also define py::self + py::self for concatenation, etc.
-
- py::class_<StringMap> map(m, "StringMap");
+ /// Comparisons
+ .def(py::self == py::self)
+ .def(py::self != py::self)
+ // Could also define py::self + py::self for concatenation, etc.
+ ;
- map .def(py::init<>())
+ // test_map_iterator
+ // Interface of a map-like object that isn't (directly) an unordered_map, but provides some basic
+ // map-like functionality.
+ class StringMap {
+ public:
+ StringMap() = default;
+ StringMap(std::unordered_map<std::string, std::string> init)
+ : map(std::move(init)) {}
+
+ void set(std::string key, std::string val) { map[key] = val; }
+ std::string get(std::string key) const { return map.at(key); }
+ size_t size() const { return map.size(); }
+ private:
+ std::unordered_map<std::string, std::string> map;
+ public:
+ decltype(map.cbegin()) begin() const { return map.cbegin(); }
+ decltype(map.cend()) end() const { return map.cend(); }
+ };
+ py::class_<StringMap>(m, "StringMap")
+ .def(py::init<>())
.def(py::init<std::unordered_map<std::string, std::string>>())
.def("__getitem__", [](const StringMap &map, std::string key) {
try { return map.get(key); }
catch (const std::out_of_range&) {
throw py::key_error("key '" + key + "' does not exist");
}
- })
+ })
.def("__setitem__", &StringMap::set)
.def("__len__", &StringMap::size)
.def("__iter__", [](const StringMap &map) { return py::make_key_iterator(map.begin(), map.end()); },
@@ -278,14 +236,23 @@ test_initializer sequences_and_iterators([](py::module &pm) {
py::keep_alive<0, 1>())
;
+ // test_generalized_iterators
+ class IntPairs {
+ public:
+ IntPairs(std::vector<std::pair<int, int>> data) : data_(std::move(data)) {}
+ const std::pair<int, int>* begin() const { return data_.data(); }
+ private:
+ std::vector<std::pair<int, int>> data_;
+ };
py::class_<IntPairs>(m, "IntPairs")
.def(py::init<std::vector<std::pair<int, int>>>())
.def("nonzero", [](const IntPairs& s) {
return py::make_iterator(NonZeroIterator<std::pair<int, int>>(s.begin()), NonZeroSentinel());
- }, py::keep_alive<0, 1>())
+ }, py::keep_alive<0, 1>())
.def("nonzero_keys", [](const IntPairs& s) {
return py::make_key_iterator(NonZeroIterator<std::pair<int, int>>(s.begin()), NonZeroSentinel());
- }, py::keep_alive<0, 1>());
+ }, py::keep_alive<0, 1>())
+ ;
#if 0
@@ -315,6 +282,7 @@ test_initializer sequences_and_iterators([](py::module &pm) {
.def("__iter__", [](py::object s) { return PySequenceIterator(s.cast<const Sequence &>(), s); })
#endif
+ // test_python_iterator_in_cpp
m.def("object_to_list", [](py::object o) {
auto l = py::list();
for (auto item : o) {
@@ -348,7 +316,19 @@ test_initializer sequences_and_iterators([](py::module &pm) {
});
});
- m.def("tuple_iterator", [](py::tuple x) { return test_random_access_iterator(x); });
- m.def("list_iterator", [](py::list x) { return test_random_access_iterator(x); });
- m.def("sequence_iterator", [](py::sequence x) { return test_random_access_iterator(x); });
-});
+ m.def("tuple_iterator", &test_random_access_iterator<py::tuple>);
+ m.def("list_iterator", &test_random_access_iterator<py::list>);
+ m.def("sequence_iterator", &test_random_access_iterator<py::sequence>);
+
+ // test_iterator_passthrough
+ // #181: iterator passthrough did not compile
+ m.def("iterator_passthrough", [](py::iterator s) -> py::iterator {
+ return py::make_iterator(std::begin(s), std::end(s));
+ });
+
+ // test_iterator_rvp
+ // #388: Can't make iterators via make_iterator() with different r/v policies
+ static std::vector<int> list = { 1, 2, 3 };
+ m.def("make_iterator_1", []() { return py::make_iterator<py::return_value_policy::copy>(list); });
+ m.def("make_iterator_2", []() { return py::make_iterator<py::return_value_policy::automatic>(list); });
+}
diff --git a/ext/pybind11/tests/test_sequences_and_iterators.py b/ext/pybind11/tests/test_sequences_and_iterators.py
index 30b6aaf4b..640ca07bd 100644
--- a/ext/pybind11/tests/test_sequences_and_iterators.py
+++ b/ext/pybind11/tests/test_sequences_and_iterators.py
@@ -1,4 +1,6 @@
import pytest
+from pybind11_tests import sequences_and_iterators as m
+from pybind11_tests import ConstructorStats
def isclose(a, b, rel_tol=1e-05, abs_tol=0.0):
@@ -11,24 +13,30 @@ def allclose(a_list, b_list, rel_tol=1e-05, abs_tol=0.0):
def test_generalized_iterators():
- from pybind11_tests.sequences_and_iterators import IntPairs
+ assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero()) == [(1, 2), (3, 4)]
+ assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero()) == [(1, 2)]
+ assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero()) == []
- assert list(IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero()) == [(1, 2), (3, 4)]
- assert list(IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero()) == [(1, 2)]
- assert list(IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero()) == []
+ assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero_keys()) == [1, 3]
+ assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero_keys()) == [1]
+ assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero_keys()) == []
- assert list(IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero_keys()) == [1, 3]
- assert list(IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero_keys()) == [1]
- assert list(IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero_keys()) == []
+ # __next__ must continue to raise StopIteration
+ it = m.IntPairs([(0, 0)]).nonzero()
+ for _ in range(3):
+ with pytest.raises(StopIteration):
+ next(it)
+ it = m.IntPairs([(0, 0)]).nonzero_keys()
+ for _ in range(3):
+ with pytest.raises(StopIteration):
+ next(it)
-def test_sequence():
- from pybind11_tests import ConstructorStats
- from pybind11_tests.sequences_and_iterators import Sequence
- cstats = ConstructorStats.get(Sequence)
+def test_sequence():
+ cstats = ConstructorStats.get(m.Sequence)
- s = Sequence(5)
+ s = m.Sequence(5)
assert cstats.values() == ['of size', '5']
assert "Sequence" in repr(s)
@@ -45,16 +53,24 @@ def test_sequence():
rev2 = s[::-1]
assert cstats.values() == ['of size', '5']
+ it = iter(m.Sequence(0))
+ for _ in range(3): # __next__ must continue to raise StopIteration
+ with pytest.raises(StopIteration):
+ next(it)
+ assert cstats.values() == ['of size', '0']
+
expected = [0, 56.78, 0, 0, 12.34]
assert allclose(rev, expected)
assert allclose(rev2, expected)
assert rev == rev2
- rev[0::2] = Sequence([2.0, 2.0, 2.0])
+ rev[0::2] = m.Sequence([2.0, 2.0, 2.0])
assert cstats.values() == ['of size', '3', 'from std::vector']
assert allclose(rev, [2, 56.78, 2, 0, 2])
+ assert cstats.alive() == 4
+ del it
assert cstats.alive() == 3
del s
assert cstats.alive() == 2
@@ -72,28 +88,29 @@ def test_sequence():
def test_map_iterator():
- from pybind11_tests.sequences_and_iterators import StringMap
-
- m = StringMap({'hi': 'bye', 'black': 'white'})
- assert m['hi'] == 'bye'
- assert len(m) == 2
- assert m['black'] == 'white'
+ sm = m.StringMap({'hi': 'bye', 'black': 'white'})
+ assert sm['hi'] == 'bye'
+ assert len(sm) == 2
+ assert sm['black'] == 'white'
with pytest.raises(KeyError):
- assert m['orange']
- m['orange'] = 'banana'
- assert m['orange'] == 'banana'
+ assert sm['orange']
+ sm['orange'] = 'banana'
+ assert sm['orange'] == 'banana'
expected = {'hi': 'bye', 'black': 'white', 'orange': 'banana'}
- for k in m:
- assert m[k] == expected[k]
- for k, v in m.items():
+ for k in sm:
+ assert sm[k] == expected[k]
+ for k, v in sm.items():
assert v == expected[k]
+ it = iter(m.StringMap({}))
+ for _ in range(3): # __next__ must continue to raise StopIteration
+ with pytest.raises(StopIteration):
+ next(it)
-def test_python_iterator_in_cpp():
- import pybind11_tests.sequences_and_iterators as m
+def test_python_iterator_in_cpp():
t = (1, 2, 3)
assert m.object_to_list(t) == [1, 2, 3]
assert m.object_to_list(iter(t)) == [1, 2, 3]
@@ -123,3 +140,19 @@ def test_python_iterator_in_cpp():
assert all(m.tuple_iterator(tuple(r)))
assert all(m.list_iterator(list(r)))
assert all(m.sequence_iterator(r))
+
+
+def test_iterator_passthrough():
+ """#181: iterator passthrough did not compile"""
+ from pybind11_tests.sequences_and_iterators import iterator_passthrough
+
+ assert list(iterator_passthrough(iter([3, 5, 7, 9, 11, 13, 15]))) == [3, 5, 7, 9, 11, 13, 15]
+
+
+def test_iterator_rvp():
+ """#388: Can't make iterators via make_iterator() with different r/v policies """
+ import pybind11_tests.sequences_and_iterators as m
+
+ assert list(m.make_iterator_1()) == [1, 2, 3]
+ assert list(m.make_iterator_2()) == [1, 2, 3]
+ assert not isinstance(m.make_iterator_1(), type(m.make_iterator_2()))
diff --git a/ext/pybind11/tests/test_smart_ptr.cpp b/ext/pybind11/tests/test_smart_ptr.cpp
index 83c1c018a..dccb1e9be 100644
--- a/ext/pybind11/tests/test_smart_ptr.cpp
+++ b/ext/pybind11/tests/test_smart_ptr.cpp
@@ -8,88 +8,19 @@
BSD-style license that can be found in the LICENSE file.
*/
+#if defined(_MSC_VER) && _MSC_VER < 1910
+# pragma warning(disable: 4702) // unreachable code in system header
+#endif
+
#include "pybind11_tests.h"
#include "object.h"
-/// Custom object with builtin reference counting (see 'object.h' for the implementation)
-class MyObject1 : public Object {
-public:
- MyObject1(int value) : value(value) {
- print_created(this, toString());
- }
-
- std::string toString() const {
- return "MyObject1[" + std::to_string(value) + "]";
- }
-
-protected:
- virtual ~MyObject1() {
- print_destroyed(this);
- }
-
-private:
- int value;
-};
-
-/// Object managed by a std::shared_ptr<>
-class MyObject2 {
-public:
- MyObject2(int value) : value(value) {
- print_created(this, toString());
- }
-
- std::string toString() const {
- return "MyObject2[" + std::to_string(value) + "]";
- }
-
- virtual ~MyObject2() {
- print_destroyed(this);
- }
-
-private:
- int value;
-};
-
-/// Object managed by a std::shared_ptr<>, additionally derives from std::enable_shared_from_this<>
-class MyObject3 : public std::enable_shared_from_this<MyObject3> {
-public:
- MyObject3(int value) : value(value) {
- print_created(this, toString());
- }
-
- std::string toString() const {
- return "MyObject3[" + std::to_string(value) + "]";
- }
-
- virtual ~MyObject3() {
- print_destroyed(this);
- }
-
-private:
- int value;
-};
-
-class MyObject4 {
-public:
- MyObject4(int value) : value{value} {
- print_created(this);
- }
- int value;
-private:
- ~MyObject4() {
- print_destroyed(this);
- }
-};
-
-/// Make pybind aware of the ref-counted wrapper type (s)
+// Make pybind aware of the ref-counted wrapper type (s):
// ref<T> is a wrapper for 'Object' which uses intrusive reference counting
// It is always possible to construct a ref<T> from an Object* pointer without
// possible incosistencies, hence the 'true' argument at the end.
PYBIND11_DECLARE_HOLDER_TYPE(T, ref<T>, true);
-PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>); // Not required any more for std::shared_ptr,
- // but it should compile without error
-
// Make pybind11 aware of the non-standard getter member function
namespace pybind11 { namespace detail {
template <typename T>
@@ -98,140 +29,157 @@ namespace pybind11 { namespace detail {
};
}}
-Object *make_object_1() { return new MyObject1(1); }
-ref<Object> make_object_2() { return new MyObject1(2); }
-
-MyObject1 *make_myobject1_1() { return new MyObject1(4); }
-ref<MyObject1> make_myobject1_2() { return new MyObject1(5); }
-
-MyObject2 *make_myobject2_1() { return new MyObject2(6); }
-std::shared_ptr<MyObject2> make_myobject2_2() { return std::make_shared<MyObject2>(7); }
+// The following is not required anymore for std::shared_ptr, but it should compile without error:
+PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>);
-MyObject3 *make_myobject3_1() { return new MyObject3(8); }
-std::shared_ptr<MyObject3> make_myobject3_2() { return std::make_shared<MyObject3>(9); }
+// This is just a wrapper around unique_ptr, but with extra fields to deliberately bloat up the
+// holder size to trigger the non-simple-layout internal instance layout for single inheritance with
+// large holder type:
+template <typename T> class huge_unique_ptr {
+ std::unique_ptr<T> ptr;
+ uint64_t padding[10];
+public:
+ huge_unique_ptr(T *p) : ptr(p) {};
+ T *get() { return ptr.get(); }
+};
+PYBIND11_DECLARE_HOLDER_TYPE(T, huge_unique_ptr<T>);
-void print_object_1(const Object *obj) { py::print(obj->toString()); }
-void print_object_2(ref<Object> obj) { py::print(obj->toString()); }
-void print_object_3(const ref<Object> &obj) { py::print(obj->toString()); }
-void print_object_4(const ref<Object> *obj) { py::print((*obj)->toString()); }
+// Simple custom holder that works like unique_ptr
+template <typename T>
+class custom_unique_ptr {
+ std::unique_ptr<T> impl;
+public:
+ custom_unique_ptr(T* p) : impl(p) { }
+ T* get() const { return impl.get(); }
+ T* release_ptr() { return impl.release(); }
+};
+PYBIND11_DECLARE_HOLDER_TYPE(T, custom_unique_ptr<T>);
-void print_myobject1_1(const MyObject1 *obj) { py::print(obj->toString()); }
-void print_myobject1_2(ref<MyObject1> obj) { py::print(obj->toString()); }
-void print_myobject1_3(const ref<MyObject1> &obj) { py::print(obj->toString()); }
-void print_myobject1_4(const ref<MyObject1> *obj) { py::print((*obj)->toString()); }
-void print_myobject2_1(const MyObject2 *obj) { py::print(obj->toString()); }
-void print_myobject2_2(std::shared_ptr<MyObject2> obj) { py::print(obj->toString()); }
-void print_myobject2_3(const std::shared_ptr<MyObject2> &obj) { py::print(obj->toString()); }
-void print_myobject2_4(const std::shared_ptr<MyObject2> *obj) { py::print((*obj)->toString()); }
+TEST_SUBMODULE(smart_ptr, m) {
-void print_myobject3_1(const MyObject3 *obj) { py::print(obj->toString()); }
-void print_myobject3_2(std::shared_ptr<MyObject3> obj) { py::print(obj->toString()); }
-void print_myobject3_3(const std::shared_ptr<MyObject3> &obj) { py::print(obj->toString()); }
-void print_myobject3_4(const std::shared_ptr<MyObject3> *obj) { py::print((*obj)->toString()); }
+ // test_smart_ptr
-test_initializer smart_ptr([](py::module &m) {
+ // Object implementation in `object.h`
py::class_<Object, ref<Object>> obj(m, "Object");
obj.def("getRefCount", &Object::getRefCount);
+ // Custom object with builtin reference counting (see 'object.h' for the implementation)
+ class MyObject1 : public Object {
+ public:
+ MyObject1(int value) : value(value) { print_created(this, toString()); }
+ std::string toString() const { return "MyObject1[" + std::to_string(value) + "]"; }
+ protected:
+ virtual ~MyObject1() { print_destroyed(this); }
+ private:
+ int value;
+ };
py::class_<MyObject1, ref<MyObject1>>(m, "MyObject1", obj)
.def(py::init<int>());
+ py::implicitly_convertible<py::int_, MyObject1>();
- m.def("test_object1_refcounting",
- []() -> bool {
- ref<MyObject1> o = new MyObject1(0);
- bool good = o->getRefCount() == 1;
- py::object o2 = py::cast(o, py::return_value_policy::reference);
- // always request (partial) ownership for objects with intrusive
- // reference counting even when using the 'reference' RVP
- good &= o->getRefCount() == 2;
- return good;
- }
- );
-
- m.def("make_object_1", &make_object_1);
- m.def("make_object_2", &make_object_2);
- m.def("make_myobject1_1", &make_myobject1_1);
- m.def("make_myobject1_2", &make_myobject1_2);
- m.def("print_object_1", &print_object_1);
- m.def("print_object_2", &print_object_2);
- m.def("print_object_3", &print_object_3);
- m.def("print_object_4", &print_object_4);
- m.def("print_myobject1_1", &print_myobject1_1);
- m.def("print_myobject1_2", &print_myobject1_2);
- m.def("print_myobject1_3", &print_myobject1_3);
- m.def("print_myobject1_4", &print_myobject1_4);
+ m.def("make_object_1", []() -> Object * { return new MyObject1(1); });
+ m.def("make_object_2", []() -> ref<Object> { return new MyObject1(2); });
+ m.def("make_myobject1_1", []() -> MyObject1 * { return new MyObject1(4); });
+ m.def("make_myobject1_2", []() -> ref<MyObject1> { return new MyObject1(5); });
+ m.def("print_object_1", [](const Object *obj) { py::print(obj->toString()); });
+ m.def("print_object_2", [](ref<Object> obj) { py::print(obj->toString()); });
+ m.def("print_object_3", [](const ref<Object> &obj) { py::print(obj->toString()); });
+ m.def("print_object_4", [](const ref<Object> *obj) { py::print((*obj)->toString()); });
+ m.def("print_myobject1_1", [](const MyObject1 *obj) { py::print(obj->toString()); });
+ m.def("print_myobject1_2", [](ref<MyObject1> obj) { py::print(obj->toString()); });
+ m.def("print_myobject1_3", [](const ref<MyObject1> &obj) { py::print(obj->toString()); });
+ m.def("print_myobject1_4", [](const ref<MyObject1> *obj) { py::print((*obj)->toString()); });
+ // Expose constructor stats for the ref type
+ m.def("cstats_ref", &ConstructorStats::get<ref_tag>);
+
+
+ // Object managed by a std::shared_ptr<>
+ class MyObject2 {
+ public:
+ MyObject2(int value) : value(value) { print_created(this, toString()); }
+ std::string toString() const { return "MyObject2[" + std::to_string(value) + "]"; }
+ virtual ~MyObject2() { print_destroyed(this); }
+ private:
+ int value;
+ };
py::class_<MyObject2, std::shared_ptr<MyObject2>>(m, "MyObject2")
.def(py::init<int>());
- m.def("make_myobject2_1", &make_myobject2_1);
- m.def("make_myobject2_2", &make_myobject2_2);
- m.def("print_myobject2_1", &print_myobject2_1);
- m.def("print_myobject2_2", &print_myobject2_2);
- m.def("print_myobject2_3", &print_myobject2_3);
- m.def("print_myobject2_4", &print_myobject2_4);
-
+ m.def("make_myobject2_1", []() { return new MyObject2(6); });
+ m.def("make_myobject2_2", []() { return std::make_shared<MyObject2>(7); });
+ m.def("print_myobject2_1", [](const MyObject2 *obj) { py::print(obj->toString()); });
+ m.def("print_myobject2_2", [](std::shared_ptr<MyObject2> obj) { py::print(obj->toString()); });
+ m.def("print_myobject2_3", [](const std::shared_ptr<MyObject2> &obj) { py::print(obj->toString()); });
+ m.def("print_myobject2_4", [](const std::shared_ptr<MyObject2> *obj) { py::print((*obj)->toString()); });
+
+ // Object managed by a std::shared_ptr<>, additionally derives from std::enable_shared_from_this<>
+ class MyObject3 : public std::enable_shared_from_this<MyObject3> {
+ public:
+ MyObject3(int value) : value(value) { print_created(this, toString()); }
+ std::string toString() const { return "MyObject3[" + std::to_string(value) + "]"; }
+ virtual ~MyObject3() { print_destroyed(this); }
+ private:
+ int value;
+ };
py::class_<MyObject3, std::shared_ptr<MyObject3>>(m, "MyObject3")
.def(py::init<int>());
- m.def("make_myobject3_1", &make_myobject3_1);
- m.def("make_myobject3_2", &make_myobject3_2);
- m.def("print_myobject3_1", &print_myobject3_1);
- m.def("print_myobject3_2", &print_myobject3_2);
- m.def("print_myobject3_3", &print_myobject3_3);
- m.def("print_myobject3_4", &print_myobject3_4);
-
+ m.def("make_myobject3_1", []() { return new MyObject3(8); });
+ m.def("make_myobject3_2", []() { return std::make_shared<MyObject3>(9); });
+ m.def("print_myobject3_1", [](const MyObject3 *obj) { py::print(obj->toString()); });
+ m.def("print_myobject3_2", [](std::shared_ptr<MyObject3> obj) { py::print(obj->toString()); });
+ m.def("print_myobject3_3", [](const std::shared_ptr<MyObject3> &obj) { py::print(obj->toString()); });
+ m.def("print_myobject3_4", [](const std::shared_ptr<MyObject3> *obj) { py::print((*obj)->toString()); });
+
+ // test_smart_ptr_refcounting
+ m.def("test_object1_refcounting", []() {
+ ref<MyObject1> o = new MyObject1(0);
+ bool good = o->getRefCount() == 1;
+ py::object o2 = py::cast(o, py::return_value_policy::reference);
+ // always request (partial) ownership for objects with intrusive
+ // reference counting even when using the 'reference' RVP
+ good &= o->getRefCount() == 2;
+ return good;
+ });
+
+ // test_unique_nodelete
+ // Object with a private destructor
+ class MyObject4 {
+ public:
+ MyObject4(int value) : value{value} { print_created(this); }
+ int value;
+ private:
+ ~MyObject4() { print_destroyed(this); }
+ };
py::class_<MyObject4, std::unique_ptr<MyObject4, py::nodelete>>(m, "MyObject4")
.def(py::init<int>())
.def_readwrite("value", &MyObject4::value);
- py::implicitly_convertible<py::int_, MyObject1>();
-
- // Expose constructor stats for the ref type
- m.def("cstats_ref", &ConstructorStats::get<ref_tag>);
-});
-
-struct SharedPtrRef {
- struct A {
- A() { print_created(this); }
- A(const A &) { print_copy_created(this); }
- A(A &&) { print_move_created(this); }
- ~A() { print_destroyed(this); }
+ // test_large_holder
+ class MyObject5 { // managed by huge_unique_ptr
+ public:
+ MyObject5(int value) : value{value} { print_created(this); }
+ ~MyObject5() { print_destroyed(this); }
+ int value;
};
-
- A value = {};
- std::shared_ptr<A> shared = std::make_shared<A>();
-};
-
-struct SharedFromThisRef {
- struct B : std::enable_shared_from_this<B> {
- B() { print_created(this); }
- B(const B &) : std::enable_shared_from_this<B>() { print_copy_created(this); }
- B(B &&) : std::enable_shared_from_this<B>() { print_move_created(this); }
- ~B() { print_destroyed(this); }
+ py::class_<MyObject5, huge_unique_ptr<MyObject5>>(m, "MyObject5")
+ .def(py::init<int>())
+ .def_readwrite("value", &MyObject5::value);
+
+ // test_shared_ptr_and_references
+ struct SharedPtrRef {
+ struct A {
+ A() { print_created(this); }
+ A(const A &) { print_copy_created(this); }
+ A(A &&) { print_move_created(this); }
+ ~A() { print_destroyed(this); }
+ };
+
+ A value = {};
+ std::shared_ptr<A> shared = std::make_shared<A>();
};
-
- B value = {};
- std::shared_ptr<B> shared = std::make_shared<B>();
-};
-
-template <typename T>
-class CustomUniquePtr {
- std::unique_ptr<T> impl;
-
-public:
- CustomUniquePtr(T* p) : impl(p) { }
- T* get() const { return impl.get(); }
- T* release_ptr() { return impl.release(); }
-};
-
-PYBIND11_DECLARE_HOLDER_TYPE(T, CustomUniquePtr<T>);
-
-test_initializer smart_ptr_and_references([](py::module &pm) {
- auto m = pm.def_submodule("smart_ptr");
-
using A = SharedPtrRef::A;
py::class_<A, std::shared_ptr<A>>(m, "A");
-
py::class_<SharedPtrRef>(m, "SharedPtrRef")
.def(py::init<>())
.def_readonly("ref", &SharedPtrRef::value)
@@ -243,9 +191,20 @@ test_initializer smart_ptr_and_references([](py::module &pm) {
.def("set_ref", [](SharedPtrRef &, const A &) { return true; })
.def("set_holder", [](SharedPtrRef &, std::shared_ptr<A>) { return true; });
+ // test_shared_ptr_from_this_and_references
+ struct SharedFromThisRef {
+ struct B : std::enable_shared_from_this<B> {
+ B() { print_created(this); }
+ B(const B &) : std::enable_shared_from_this<B>() { print_copy_created(this); }
+ B(B &&) : std::enable_shared_from_this<B>() { print_move_created(this); }
+ ~B() { print_destroyed(this); }
+ };
+
+ B value = {};
+ std::shared_ptr<B> shared = std::make_shared<B>();
+ };
using B = SharedFromThisRef::B;
py::class_<B, std::shared_ptr<B>>(m, "B");
-
py::class_<SharedFromThisRef>(m, "SharedFromThisRef")
.def(py::init<>())
.def_readonly("bad_wp", &SharedFromThisRef::value)
@@ -258,17 +217,54 @@ test_initializer smart_ptr_and_references([](py::module &pm) {
.def("set_ref", [](SharedFromThisRef &, const B &) { return true; })
.def("set_holder", [](SharedFromThisRef &, std::shared_ptr<B>) { return true; });
+ // Issue #865: shared_from_this doesn't work with virtual inheritance
+ struct SharedFromThisVBase : std::enable_shared_from_this<SharedFromThisVBase> {
+ virtual ~SharedFromThisVBase() = default;
+ };
+ struct SharedFromThisVirt : virtual SharedFromThisVBase {};
+ static std::shared_ptr<SharedFromThisVirt> sft(new SharedFromThisVirt());
+ py::class_<SharedFromThisVirt, std::shared_ptr<SharedFromThisVirt>>(m, "SharedFromThisVirt")
+ .def_static("get", []() { return sft.get(); });
+
+ // test_move_only_holder
struct C {
C() { print_created(this); }
~C() { print_destroyed(this); }
};
+ py::class_<C, custom_unique_ptr<C>>(m, "TypeWithMoveOnlyHolder")
+ .def_static("make", []() { return custom_unique_ptr<C>(new C); });
- py::class_<C, CustomUniquePtr<C>>(m, "TypeWithMoveOnlyHolder")
- .def_static("make", []() { return CustomUniquePtr<C>(new C); });
-
+ // test_smart_ptr_from_default
struct HeldByDefaultHolder { };
-
py::class_<HeldByDefaultHolder>(m, "HeldByDefaultHolder")
.def(py::init<>())
.def_static("load_shared_ptr", [](std::shared_ptr<HeldByDefaultHolder>) {});
-});
+
+ // test_shared_ptr_gc
+ // #187: issue involving std::shared_ptr<> return value policy & garbage collection
+ struct ElementBase { virtual void foo() { } /* Force creation of virtual table */ };
+ py::class_<ElementBase, std::shared_ptr<ElementBase>>(m, "ElementBase");
+
+ struct ElementA : ElementBase {
+ ElementA(int v) : v(v) { }
+ int value() { return v; }
+ int v;
+ };
+ py::class_<ElementA, ElementBase, std::shared_ptr<ElementA>>(m, "ElementA")
+ .def(py::init<int>())
+ .def("value", &ElementA::value);
+
+ struct ElementList {
+ void add(std::shared_ptr<ElementBase> e) { l.push_back(e); }
+ std::vector<std::shared_ptr<ElementBase>> l;
+ };
+ py::class_<ElementList, std::shared_ptr<ElementList>>(m, "ElementList")
+ .def(py::init<>())
+ .def("add", &ElementList::add)
+ .def("get", [](ElementList &el) {
+ py::list list;
+ for (auto &e : el.l)
+ list.append(py::cast(e));
+ return list;
+ });
+}
diff --git a/ext/pybind11/tests/test_smart_ptr.py b/ext/pybind11/tests/test_smart_ptr.py
index b5af3bd38..4dfe0036f 100644
--- a/ext/pybind11/tests/test_smart_ptr.py
+++ b/ext/pybind11/tests/test_smart_ptr.py
@@ -1,40 +1,35 @@
import pytest
+from pybind11_tests import smart_ptr as m
from pybind11_tests import ConstructorStats
def test_smart_ptr(capture):
# Object1
- from pybind11_tests import (MyObject1, make_object_1, make_object_2,
- print_object_1, print_object_2, print_object_3, print_object_4)
-
- for i, o in enumerate([make_object_1(), make_object_2(), MyObject1(3)], start=1):
+ for i, o in enumerate([m.make_object_1(), m.make_object_2(), m.MyObject1(3)], start=1):
assert o.getRefCount() == 1
with capture:
- print_object_1(o)
- print_object_2(o)
- print_object_3(o)
- print_object_4(o)
+ m.print_object_1(o)
+ m.print_object_2(o)
+ m.print_object_3(o)
+ m.print_object_4(o)
assert capture == "MyObject1[{i}]\n".format(i=i) * 4
- from pybind11_tests import (make_myobject1_1, make_myobject1_2,
- print_myobject1_1, print_myobject1_2,
- print_myobject1_3, print_myobject1_4)
-
- for i, o in enumerate([make_myobject1_1(), make_myobject1_2(), MyObject1(6), 7], start=4):
+ for i, o in enumerate([m.make_myobject1_1(), m.make_myobject1_2(), m.MyObject1(6), 7],
+ start=4):
print(o)
with capture:
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)
+ m.print_object_1(o)
+ m.print_object_2(o)
+ m.print_object_3(o)
+ m.print_object_4(o)
+ m.print_myobject1_1(o)
+ m.print_myobject1_2(o)
+ m.print_myobject1_3(o)
+ m.print_myobject1_4(o)
assert capture == "MyObject1[{i}]\n".format(i=i) * (4 if isinstance(o, int) else 8)
- cstats = ConstructorStats.get(MyObject1)
+ cstats = ConstructorStats.get(m.MyObject1)
assert cstats.alive() == 0
expected_values = ['MyObject1[{}]'.format(i) for i in range(1, 7)] + ['MyObject1[7]'] * 4
assert cstats.values() == expected_values
@@ -45,21 +40,16 @@ def test_smart_ptr(capture):
assert cstats.move_assignments == 0
# Object2
- from pybind11_tests import (MyObject2, make_myobject2_1, make_myobject2_2,
- make_myobject3_1, make_myobject3_2,
- print_myobject2_1, print_myobject2_2,
- print_myobject2_3, print_myobject2_4)
-
- for i, o in zip([8, 6, 7], [MyObject2(8), make_myobject2_1(), make_myobject2_2()]):
+ for i, o in zip([8, 6, 7], [m.MyObject2(8), m.make_myobject2_1(), m.make_myobject2_2()]):
print(o)
with capture:
- print_myobject2_1(o)
- print_myobject2_2(o)
- print_myobject2_3(o)
- print_myobject2_4(o)
+ m.print_myobject2_1(o)
+ m.print_myobject2_2(o)
+ m.print_myobject2_3(o)
+ m.print_myobject2_4(o)
assert capture == "MyObject2[{i}]\n".format(i=i) * 4
- cstats = ConstructorStats.get(MyObject2)
+ cstats = ConstructorStats.get(m.MyObject2)
assert cstats.alive() == 1
o = None
assert cstats.alive() == 0
@@ -71,19 +61,16 @@ def test_smart_ptr(capture):
assert cstats.move_assignments == 0
# Object3
- from pybind11_tests import (MyObject3, print_myobject3_1, print_myobject3_2,
- print_myobject3_3, print_myobject3_4)
-
- for i, o in zip([9, 8, 9], [MyObject3(9), make_myobject3_1(), make_myobject3_2()]):
+ for i, o in zip([9, 8, 9], [m.MyObject3(9), m.make_myobject3_1(), m.make_myobject3_2()]):
print(o)
with capture:
- print_myobject3_1(o)
- print_myobject3_2(o)
- print_myobject3_3(o)
- print_myobject3_4(o)
+ m.print_myobject3_1(o)
+ m.print_myobject3_2(o)
+ m.print_myobject3_3(o)
+ m.print_myobject3_4(o)
assert capture == "MyObject3[{i}]\n".format(i=i) * 4
- cstats = ConstructorStats.get(MyObject3)
+ cstats = ConstructorStats.get(m.MyObject3)
assert cstats.alive() == 1
o = None
assert cstats.alive() == 0
@@ -94,10 +81,8 @@ def test_smart_ptr(capture):
assert cstats.copy_assignments == 0
assert cstats.move_assignments == 0
- # Object and ref
- from pybind11_tests import Object, cstats_ref
-
- cstats = ConstructorStats.get(Object)
+ # Object
+ cstats = ConstructorStats.get(m.Object)
assert cstats.alive() == 0
assert cstats.values() == []
assert cstats.default_constructions == 10
@@ -106,7 +91,8 @@ def test_smart_ptr(capture):
assert cstats.copy_assignments == 0
assert cstats.move_assignments == 0
- cstats = cstats_ref()
+ # ref<>
+ cstats = m.cstats_ref()
assert cstats.alive() == 0
assert cstats.values() == ['from pointer'] * 10
assert cstats.default_constructions == 30
@@ -117,26 +103,30 @@ def test_smart_ptr(capture):
def test_smart_ptr_refcounting():
- from pybind11_tests import test_object1_refcounting
- assert test_object1_refcounting()
+ assert m.test_object1_refcounting()
def test_unique_nodelete():
- from pybind11_tests import MyObject4
- o = MyObject4(23)
+ o = m.MyObject4(23)
assert o.value == 23
- cstats = ConstructorStats.get(MyObject4)
+ cstats = ConstructorStats.get(m.MyObject4)
assert cstats.alive() == 1
del o
- cstats = ConstructorStats.get(MyObject4)
assert cstats.alive() == 1 # Leak, but that's intentional
-def test_shared_ptr_and_references():
- from pybind11_tests.smart_ptr import SharedPtrRef, A
+def test_large_holder():
+ o = m.MyObject5(5)
+ assert o.value == 5
+ cstats = ConstructorStats.get(m.MyObject5)
+ assert cstats.alive() == 1
+ del o
+ assert cstats.alive() == 0
- s = SharedPtrRef()
- stats = ConstructorStats.get(A)
+
+def test_shared_ptr_and_references():
+ s = m.SharedPtrRef()
+ stats = ConstructorStats.get(m.A)
assert stats.alive() == 2
ref = s.ref # init_holder_helper(holder_ptr=false, owned=false)
@@ -166,10 +156,8 @@ def test_shared_ptr_and_references():
def test_shared_ptr_from_this_and_references():
- from pybind11_tests.smart_ptr import SharedFromThisRef, B
-
- s = SharedFromThisRef()
- stats = ConstructorStats.get(B)
+ s = m.SharedFromThisRef()
+ stats = ConstructorStats.get(m.B)
assert stats.alive() == 2
ref = s.ref # init_holder_helper(holder_ptr=false, owned=false, bad_wp=false)
@@ -202,21 +190,31 @@ def test_shared_ptr_from_this_and_references():
del ref, bad_wp, copy, holder_ref, holder_copy, s
assert stats.alive() == 0
+ z = m.SharedFromThisVirt.get()
+ y = m.SharedFromThisVirt.get()
+ assert y is z
-def test_move_only_holder():
- from pybind11_tests.smart_ptr import TypeWithMoveOnlyHolder
- a = TypeWithMoveOnlyHolder.make()
- stats = ConstructorStats.get(TypeWithMoveOnlyHolder)
+def test_move_only_holder():
+ a = m.TypeWithMoveOnlyHolder.make()
+ stats = ConstructorStats.get(m.TypeWithMoveOnlyHolder)
assert stats.alive() == 1
del a
assert stats.alive() == 0
def test_smart_ptr_from_default():
- from pybind11_tests.smart_ptr import HeldByDefaultHolder
-
- instance = HeldByDefaultHolder()
+ instance = m.HeldByDefaultHolder()
with pytest.raises(RuntimeError) as excinfo:
- HeldByDefaultHolder.load_shared_ptr(instance)
+ m.HeldByDefaultHolder.load_shared_ptr(instance)
assert "Unable to load a custom holder type from a default-holder instance" in str(excinfo)
+
+
+def test_shared_ptr_gc():
+ """#187: issue involving std::shared_ptr<> return value policy & garbage collection"""
+ el = m.ElementList()
+ for i in range(10):
+ el.add(m.ElementA(i))
+ pytest.gc_collect()
+ for i, v in enumerate(el.get()):
+ assert i == v.value()
diff --git a/ext/pybind11/tests/test_stl.cpp b/ext/pybind11/tests/test_stl.cpp
new file mode 100644
index 000000000..7d53e9c18
--- /dev/null
+++ b/ext/pybind11/tests/test_stl.cpp
@@ -0,0 +1,238 @@
+/*
+ tests/test_stl.cpp -- STL type casters
+
+ Copyright (c) 2017 Wenzel Jakob <wenzel.jakob@epfl.ch>
+
+ All rights reserved. Use of this source code is governed by a
+ BSD-style license that can be found in the LICENSE file.
+*/
+
+#include "pybind11_tests.h"
+#include <pybind11/stl.h>
+
+// Test with `std::variant` in C++17 mode, or with `boost::variant` in C++11/14
+#if PYBIND11_HAS_VARIANT
+using std::variant;
+#elif defined(PYBIND11_TEST_BOOST) && (!defined(_MSC_VER) || _MSC_VER >= 1910)
+# include <boost/variant.hpp>
+# define PYBIND11_HAS_VARIANT 1
+using boost::variant;
+
+namespace pybind11 { namespace detail {
+template <typename... Ts>
+struct type_caster<boost::variant<Ts...>> : variant_caster<boost::variant<Ts...>> {};
+
+template <>
+struct visit_helper<boost::variant> {
+ template <typename... Args>
+ static auto call(Args &&...args) -> decltype(boost::apply_visitor(args...)) {
+ return boost::apply_visitor(args...);
+ }
+};
+}} // namespace pybind11::detail
+#endif
+
+/// Issue #528: templated constructor
+struct TplCtorClass {
+ template <typename T> TplCtorClass(const T &) { }
+ bool operator==(const TplCtorClass &) const { return true; }
+};
+
+namespace std {
+ template <>
+ struct hash<TplCtorClass> { size_t operator()(const TplCtorClass &) const { return 0; } };
+}
+
+
+TEST_SUBMODULE(stl, m) {
+ // test_vector
+ m.def("cast_vector", []() { return std::vector<int>{1}; });
+ m.def("load_vector", [](const std::vector<int> &v) { return v.at(0) == 1 && v.at(1) == 2; });
+ // `std::vector<bool>` is special because it returns proxy objects instead of references
+ m.def("cast_bool_vector", []() { return std::vector<bool>{true, false}; });
+ m.def("load_bool_vector", [](const std::vector<bool> &v) {
+ return v.at(0) == true && v.at(1) == false;
+ });
+ // Unnumbered regression (caused by #936): pointers to stl containers aren't castable
+ static std::vector<RValueCaster> lvv{2};
+ m.def("cast_ptr_vector", []() { return &lvv; });
+
+ // test_array
+ m.def("cast_array", []() { return std::array<int, 2> {{1 , 2}}; });
+ m.def("load_array", [](const std::array<int, 2> &a) { return a[0] == 1 && a[1] == 2; });
+
+ // test_valarray
+ m.def("cast_valarray", []() { return std::valarray<int>{1, 4, 9}; });
+ m.def("load_valarray", [](const std::valarray<int>& v) {
+ return v.size() == 3 && v[0] == 1 && v[1] == 4 && v[2] == 9;
+ });
+
+ // test_map
+ m.def("cast_map", []() { return std::map<std::string, std::string>{{"key", "value"}}; });
+ m.def("load_map", [](const std::map<std::string, std::string> &map) {
+ return map.at("key") == "value" && map.at("key2") == "value2";
+ });
+
+ // test_set
+ m.def("cast_set", []() { return std::set<std::string>{"key1", "key2"}; });
+ m.def("load_set", [](const std::set<std::string> &set) {
+ return set.count("key1") && set.count("key2") && set.count("key3");
+ });
+
+ // test_recursive_casting
+ m.def("cast_rv_vector", []() { return std::vector<RValueCaster>{2}; });
+ m.def("cast_rv_array", []() { return std::array<RValueCaster, 3>(); });
+ // NB: map and set keys are `const`, so while we technically do move them (as `const Type &&`),
+ // casters don't typically do anything with that, which means they fall to the `const Type &`
+ // caster.
+ m.def("cast_rv_map", []() { return std::unordered_map<std::string, RValueCaster>{{"a", RValueCaster{}}}; });
+ m.def("cast_rv_nested", []() {
+ std::vector<std::array<std::list<std::unordered_map<std::string, RValueCaster>>, 2>> v;
+ v.emplace_back(); // add an array
+ v.back()[0].emplace_back(); // add a map to the array
+ v.back()[0].back().emplace("b", RValueCaster{});
+ v.back()[0].back().emplace("c", RValueCaster{});
+ v.back()[1].emplace_back(); // add a map to the array
+ v.back()[1].back().emplace("a", RValueCaster{});
+ return v;
+ });
+ static std::array<RValueCaster, 2> lva;
+ static std::unordered_map<std::string, RValueCaster> lvm{{"a", RValueCaster{}}, {"b", RValueCaster{}}};
+ static std::unordered_map<std::string, std::vector<std::list<std::array<RValueCaster, 2>>>> lvn;
+ lvn["a"].emplace_back(); // add a list
+ lvn["a"].back().emplace_back(); // add an array
+ lvn["a"].emplace_back(); // another list
+ lvn["a"].back().emplace_back(); // add an array
+ lvn["b"].emplace_back(); // add a list
+ lvn["b"].back().emplace_back(); // add an array
+ lvn["b"].back().emplace_back(); // add another array
+ m.def("cast_lv_vector", []() -> const decltype(lvv) & { return lvv; });
+ m.def("cast_lv_array", []() -> const decltype(lva) & { return lva; });
+ m.def("cast_lv_map", []() -> const decltype(lvm) & { return lvm; });
+ m.def("cast_lv_nested", []() -> const decltype(lvn) & { return lvn; });
+ // #853:
+ m.def("cast_unique_ptr_vector", []() {
+ std::vector<std::unique_ptr<UserType>> v;
+ v.emplace_back(new UserType{7});
+ v.emplace_back(new UserType{42});
+ return v;
+ });
+
+ // test_move_out_container
+ struct MoveOutContainer {
+ struct Value { int value; };
+ std::list<Value> move_list() const { return {{0}, {1}, {2}}; }
+ };
+ py::class_<MoveOutContainer::Value>(m, "MoveOutContainerValue")
+ .def_readonly("value", &MoveOutContainer::Value::value);
+ py::class_<MoveOutContainer>(m, "MoveOutContainer")
+ .def(py::init<>())
+ .def_property_readonly("move_list", &MoveOutContainer::move_list);
+
+ // Class that can be move- and copy-constructed, but not assigned
+ struct NoAssign {
+ int value;
+
+ explicit NoAssign(int value = 0) : value(value) { }
+ NoAssign(const NoAssign &) = default;
+ NoAssign(NoAssign &&) = default;
+
+ NoAssign &operator=(const NoAssign &) = delete;
+ NoAssign &operator=(NoAssign &&) = delete;
+ };
+ py::class_<NoAssign>(m, "NoAssign", "Class with no C++ assignment operators")
+ .def(py::init<>())
+ .def(py::init<int>());
+
+#ifdef PYBIND11_HAS_OPTIONAL
+ // test_optional
+ m.attr("has_optional") = true;
+
+ using opt_int = std::optional<int>;
+ using opt_no_assign = std::optional<NoAssign>;
+ m.def("double_or_zero", [](const opt_int& x) -> int {
+ return x.value_or(0) * 2;
+ });
+ m.def("half_or_none", [](int x) -> opt_int {
+ return x ? opt_int(x / 2) : opt_int();
+ });
+ m.def("test_nullopt", [](opt_int x) {
+ return x.value_or(42);
+ }, py::arg_v("x", std::nullopt, "None"));
+ m.def("test_no_assign", [](const opt_no_assign &x) {
+ return x ? x->value : 42;
+ }, py::arg_v("x", std::nullopt, "None"));
+
+ m.def("nodefer_none_optional", [](std::optional<int>) { return true; });
+ m.def("nodefer_none_optional", [](py::none) { return false; });
+#endif
+
+#ifdef PYBIND11_HAS_EXP_OPTIONAL
+ // test_exp_optional
+ m.attr("has_exp_optional") = true;
+
+ using exp_opt_int = std::experimental::optional<int>;
+ using exp_opt_no_assign = std::experimental::optional<NoAssign>;
+ m.def("double_or_zero_exp", [](const exp_opt_int& x) -> int {
+ return x.value_or(0) * 2;
+ });
+ m.def("half_or_none_exp", [](int x) -> exp_opt_int {
+ return x ? exp_opt_int(x / 2) : exp_opt_int();
+ });
+ m.def("test_nullopt_exp", [](exp_opt_int x) {
+ return x.value_or(42);
+ }, py::arg_v("x", std::experimental::nullopt, "None"));
+ m.def("test_no_assign_exp", [](const exp_opt_no_assign &x) {
+ return x ? x->value : 42;
+ }, py::arg_v("x", std::experimental::nullopt, "None"));
+#endif
+
+#ifdef PYBIND11_HAS_VARIANT
+ static_assert(std::is_same<py::detail::variant_caster_visitor::result_type, py::handle>::value,
+ "visitor::result_type is required by boost::variant in C++11 mode");
+
+ struct visitor {
+ using result_type = const char *;
+
+ result_type operator()(int) { return "int"; }
+ result_type operator()(std::string) { return "std::string"; }
+ result_type operator()(double) { return "double"; }
+ result_type operator()(std::nullptr_t) { return "std::nullptr_t"; }
+ };
+
+ // test_variant
+ m.def("load_variant", [](variant<int, std::string, double, std::nullptr_t> v) {
+ return py::detail::visit_helper<variant>::call(visitor(), v);
+ });
+ m.def("load_variant_2pass", [](variant<double, int> v) {
+ return py::detail::visit_helper<variant>::call(visitor(), v);
+ });
+ m.def("cast_variant", []() {
+ using V = variant<int, std::string>;
+ return py::make_tuple(V(5), V("Hello"));
+ });
+#endif
+
+ // #528: templated constructor
+ // (no python tests: the test here is that this compiles)
+ m.def("tpl_ctor_vector", [](std::vector<TplCtorClass> &) {});
+ m.def("tpl_ctor_map", [](std::unordered_map<TplCtorClass, TplCtorClass> &) {});
+ m.def("tpl_ctor_set", [](std::unordered_set<TplCtorClass> &) {});
+#if defined(PYBIND11_HAS_OPTIONAL)
+ m.def("tpl_constr_optional", [](std::optional<TplCtorClass> &) {});
+#elif defined(PYBIND11_HAS_EXP_OPTIONAL)
+ m.def("tpl_constr_optional", [](std::experimental::optional<TplCtorClass> &) {});
+#endif
+
+ // test_vec_of_reference_wrapper
+ // #171: Can't return STL structures containing reference wrapper
+ m.def("return_vec_of_reference_wrapper", [](std::reference_wrapper<UserType> p4) {
+ static UserType p1{1}, p2{2}, p3{3};
+ return std::vector<std::reference_wrapper<UserType>> {
+ std::ref(p1), std::ref(p2), std::ref(p3), p4
+ };
+ });
+
+ // test_stl_pass_by_pointer
+ m.def("stl_pass_by_pointer", [](std::vector<int>* v) { return *v; }, "v"_a=nullptr);
+}
diff --git a/ext/pybind11/tests/test_stl.py b/ext/pybind11/tests/test_stl.py
new file mode 100644
index 000000000..db8515e7a
--- /dev/null
+++ b/ext/pybind11/tests/test_stl.py
@@ -0,0 +1,200 @@
+import pytest
+
+from pybind11_tests import stl as m
+from pybind11_tests import UserType
+
+
+def test_vector(doc):
+ """std::vector <-> list"""
+ l = m.cast_vector()
+ assert l == [1]
+ l.append(2)
+ assert m.load_vector(l)
+ assert m.load_vector(tuple(l))
+
+ assert m.cast_bool_vector() == [True, False]
+ assert m.load_bool_vector([True, False])
+
+ assert doc(m.cast_vector) == "cast_vector() -> List[int]"
+ assert doc(m.load_vector) == "load_vector(arg0: List[int]) -> bool"
+
+ # Test regression caused by 936: pointers to stl containers weren't castable
+ assert m.cast_ptr_vector() == ["lvalue", "lvalue"]
+
+
+def test_array(doc):
+ """std::array <-> list"""
+ l = m.cast_array()
+ assert l == [1, 2]
+ assert m.load_array(l)
+
+ assert doc(m.cast_array) == "cast_array() -> List[int[2]]"
+ assert doc(m.load_array) == "load_array(arg0: List[int[2]]) -> bool"
+
+
+def test_valarray(doc):
+ """std::valarray <-> list"""
+ l = m.cast_valarray()
+ assert l == [1, 4, 9]
+ assert m.load_valarray(l)
+
+ assert doc(m.cast_valarray) == "cast_valarray() -> List[int]"
+ assert doc(m.load_valarray) == "load_valarray(arg0: List[int]) -> bool"
+
+
+def test_map(doc):
+ """std::map <-> dict"""
+ d = m.cast_map()
+ assert d == {"key": "value"}
+ d["key2"] = "value2"
+ assert m.load_map(d)
+
+ assert doc(m.cast_map) == "cast_map() -> Dict[str, str]"
+ assert doc(m.load_map) == "load_map(arg0: Dict[str, str]) -> bool"
+
+
+def test_set(doc):
+ """std::set <-> set"""
+ s = m.cast_set()
+ assert s == {"key1", "key2"}
+ s.add("key3")
+ assert m.load_set(s)
+
+ assert doc(m.cast_set) == "cast_set() -> Set[str]"
+ assert doc(m.load_set) == "load_set(arg0: Set[str]) -> bool"
+
+
+def test_recursive_casting():
+ """Tests that stl casters preserve lvalue/rvalue context for container values"""
+ assert m.cast_rv_vector() == ["rvalue", "rvalue"]
+ assert m.cast_lv_vector() == ["lvalue", "lvalue"]
+ assert m.cast_rv_array() == ["rvalue", "rvalue", "rvalue"]
+ assert m.cast_lv_array() == ["lvalue", "lvalue"]
+ assert m.cast_rv_map() == {"a": "rvalue"}
+ assert m.cast_lv_map() == {"a": "lvalue", "b": "lvalue"}
+ assert m.cast_rv_nested() == [[[{"b": "rvalue", "c": "rvalue"}], [{"a": "rvalue"}]]]
+ assert m.cast_lv_nested() == {
+ "a": [[["lvalue", "lvalue"]], [["lvalue", "lvalue"]]],
+ "b": [[["lvalue", "lvalue"], ["lvalue", "lvalue"]]]
+ }
+
+ # Issue #853 test case:
+ z = m.cast_unique_ptr_vector()
+ assert z[0].value == 7 and z[1].value == 42
+
+
+def test_move_out_container():
+ """Properties use the `reference_internal` policy by default. If the underlying function
+ returns an rvalue, the policy is automatically changed to `move` to avoid referencing
+ a temporary. In case the return value is a container of user-defined types, the policy
+ also needs to be applied to the elements, not just the container."""
+ c = m.MoveOutContainer()
+ moved_out_list = c.move_list
+ assert [x.value for x in moved_out_list] == [0, 1, 2]
+
+
+@pytest.mark.skipif(not hasattr(m, "has_optional"), reason='no <optional>')
+def test_optional():
+ assert m.double_or_zero(None) == 0
+ assert m.double_or_zero(42) == 84
+ pytest.raises(TypeError, m.double_or_zero, 'foo')
+
+ assert m.half_or_none(0) is None
+ assert m.half_or_none(42) == 21
+ pytest.raises(TypeError, m.half_or_none, 'foo')
+
+ assert m.test_nullopt() == 42
+ assert m.test_nullopt(None) == 42
+ assert m.test_nullopt(42) == 42
+ assert m.test_nullopt(43) == 43
+
+ assert m.test_no_assign() == 42
+ assert m.test_no_assign(None) == 42
+ assert m.test_no_assign(m.NoAssign(43)) == 43
+ pytest.raises(TypeError, m.test_no_assign, 43)
+
+ assert m.nodefer_none_optional(None)
+
+
+@pytest.mark.skipif(not hasattr(m, "has_exp_optional"), reason='no <experimental/optional>')
+def test_exp_optional():
+ assert m.double_or_zero_exp(None) == 0
+ assert m.double_or_zero_exp(42) == 84
+ pytest.raises(TypeError, m.double_or_zero_exp, 'foo')
+
+ assert m.half_or_none_exp(0) is None
+ assert m.half_or_none_exp(42) == 21
+ pytest.raises(TypeError, m.half_or_none_exp, 'foo')
+
+ assert m.test_nullopt_exp() == 42
+ assert m.test_nullopt_exp(None) == 42
+ assert m.test_nullopt_exp(42) == 42
+ assert m.test_nullopt_exp(43) == 43
+
+ assert m.test_no_assign_exp() == 42
+ assert m.test_no_assign_exp(None) == 42
+ assert m.test_no_assign_exp(m.NoAssign(43)) == 43
+ pytest.raises(TypeError, m.test_no_assign_exp, 43)
+
+
+@pytest.mark.skipif(not hasattr(m, "load_variant"), reason='no <variant>')
+def test_variant(doc):
+ assert m.load_variant(1) == "int"
+ assert m.load_variant("1") == "std::string"
+ assert m.load_variant(1.0) == "double"
+ assert m.load_variant(None) == "std::nullptr_t"
+
+ assert m.load_variant_2pass(1) == "int"
+ assert m.load_variant_2pass(1.0) == "double"
+
+ assert m.cast_variant() == (5, "Hello")
+
+ assert doc(m.load_variant) == "load_variant(arg0: Union[int, str, float, None]) -> str"
+
+
+def test_vec_of_reference_wrapper():
+ """#171: Can't return reference wrappers (or STL structures containing them)"""
+ assert str(m.return_vec_of_reference_wrapper(UserType(4))) == \
+ "[UserType(1), UserType(2), UserType(3), UserType(4)]"
+
+
+def test_stl_pass_by_pointer(msg):
+ """Passing nullptr or None to an STL container pointer is not expected to work"""
+ with pytest.raises(TypeError) as excinfo:
+ m.stl_pass_by_pointer() # default value is `nullptr`
+ assert msg(excinfo.value) == """
+ stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported:
+ 1. (v: List[int]=None) -> List[int]
+
+ Invoked with:
+ """ # noqa: E501 line too long
+
+ with pytest.raises(TypeError) as excinfo:
+ m.stl_pass_by_pointer(None)
+ assert msg(excinfo.value) == """
+ stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported:
+ 1. (v: List[int]=None) -> List[int]
+
+ Invoked with: None
+ """ # noqa: E501 line too long
+
+ assert m.stl_pass_by_pointer([1, 2, 3]) == [1, 2, 3]
+
+
+def test_missing_header_message():
+ """Trying convert `list` to a `std::vector`, or vice versa, without including
+ <pybind11/stl.h> should result in a helpful suggestion in the error message"""
+ import pybind11_cross_module_tests as cm
+
+ expected_message = ("Did you forget to `#include <pybind11/stl.h>`? Or <pybind11/complex.h>,\n"
+ "<pybind11/functional.h>, <pybind11/chrono.h>, etc. Some automatic\n"
+ "conversions are optional and require extra headers to be included\n"
+ "when compiling your pybind11 module.")
+
+ with pytest.raises(TypeError) as excinfo:
+ cm.missing_header_arg([1.0, 2.0, 3.0])
+ assert expected_message in str(excinfo.value)
+
+ with pytest.raises(TypeError) as excinfo:
+ cm.missing_header_return()
+ assert expected_message in str(excinfo.value)
diff --git a/ext/pybind11/tests/test_stl_binders.cpp b/ext/pybind11/tests/test_stl_binders.cpp
index f636c0b55..a88b589e1 100644
--- a/ext/pybind11/tests/test_stl_binders.cpp
+++ b/ext/pybind11/tests/test_stl_binders.cpp
@@ -15,11 +15,6 @@
#include <deque>
#include <unordered_map>
-#ifdef _MSC_VER
-// We get some really long type names here which causes MSVC to emit warnings
-# pragma warning(disable: 4503) // warning C4503: decorated name length exceeded, name was truncated
-#endif
-
class El {
public:
El() = delete;
@@ -59,70 +54,54 @@ template <class Map> Map *times_ten(int n) {
return m;
}
-struct VStruct {
- bool w;
- uint32_t x;
- double y;
- bool z;
-};
-
-struct VUndeclStruct { //dtype not declared for this version
- bool w;
- uint32_t x;
- double y;
- bool z;
-};
+TEST_SUBMODULE(stl_binders, m) {
+ // test_vector_int
+ py::bind_vector<std::vector<unsigned int>>(m, "VectorInt", py::buffer_protocol());
-test_initializer stl_binder_vector([](py::module &m) {
+ // test_vector_custom
py::class_<El>(m, "El")
.def(py::init<int>());
-
- py::bind_vector<std::vector<unsigned char>>(m, "VectorUChar", py::buffer_protocol());
- py::bind_vector<std::vector<unsigned int>>(m, "VectorInt", py::buffer_protocol());
- py::bind_vector<std::vector<bool>>(m, "VectorBool");
-
py::bind_vector<std::vector<El>>(m, "VectorEl");
-
py::bind_vector<std::vector<std::vector<El>>>(m, "VectorVectorEl");
- m.def("create_undeclstruct", [m] () mutable {
- py::bind_vector<std::vector<VUndeclStruct>>(m, "VectorUndeclStruct", py::buffer_protocol());
- });
-
- try {
- py::module::import("numpy");
- } catch (...) {
- return;
- }
- PYBIND11_NUMPY_DTYPE(VStruct, w, x, y, z);
- py::class_<VStruct>(m, "VStruct").def_readwrite("x", &VStruct::x);
- py::bind_vector<std::vector<VStruct>>(m, "VectorStruct", py::buffer_protocol());
- m.def("get_vectorstruct", [] {return std::vector<VStruct> {{0, 5, 3.0, 1}, {1, 30, -1e4, 0}};});
-});
-
-test_initializer stl_binder_map([](py::module &m) {
+ // test_map_string_double
py::bind_map<std::map<std::string, double>>(m, "MapStringDouble");
py::bind_map<std::unordered_map<std::string, double>>(m, "UnorderedMapStringDouble");
+ // test_map_string_double_const
py::bind_map<std::map<std::string, double const>>(m, "MapStringDoubleConst");
py::bind_map<std::unordered_map<std::string, double const>>(m, "UnorderedMapStringDoubleConst");
-});
-
-test_initializer stl_binder_noncopyable([](py::module &m) {
py::class_<E_nc>(m, "ENC")
.def(py::init<int>())
.def_readwrite("value", &E_nc::value);
+ // test_noncopyable_containers
py::bind_vector<std::vector<E_nc>>(m, "VectorENC");
m.def("get_vnc", &one_to_n<std::vector<E_nc>>, py::return_value_policy::reference);
-
py::bind_vector<std::deque<E_nc>>(m, "DequeENC");
m.def("get_dnc", &one_to_n<std::deque<E_nc>>, py::return_value_policy::reference);
-
py::bind_map<std::map<int, E_nc>>(m, "MapENC");
m.def("get_mnc", &times_ten<std::map<int, E_nc>>, py::return_value_policy::reference);
-
py::bind_map<std::unordered_map<int, E_nc>>(m, "UmapENC");
m.def("get_umnc", &times_ten<std::unordered_map<int, E_nc>>, py::return_value_policy::reference);
-});
+
+ // test_vector_buffer
+ py::bind_vector<std::vector<unsigned char>>(m, "VectorUChar", py::buffer_protocol());
+ // no dtype declared for this version:
+ struct VUndeclStruct { bool w; uint32_t x; double y; bool z; };
+ m.def("create_undeclstruct", [m] () mutable {
+ py::bind_vector<std::vector<VUndeclStruct>>(m, "VectorUndeclStruct", py::buffer_protocol());
+ });
+
+ // The rest depends on numpy:
+ try { py::module::import("numpy"); }
+ catch (...) { return; }
+
+ // test_vector_buffer_numpy
+ struct VStruct { bool w; uint32_t x; double y; bool z; };
+ PYBIND11_NUMPY_DTYPE(VStruct, w, x, y, z);
+ py::class_<VStruct>(m, "VStruct").def_readwrite("x", &VStruct::x);
+ py::bind_vector<std::vector<VStruct>>(m, "VectorStruct", py::buffer_protocol());
+ m.def("get_vectorstruct", [] {return std::vector<VStruct> {{0, 5, 3.0, 1}, {1, 30, -1e4, 0}};});
+}
diff --git a/ext/pybind11/tests/test_stl_binders.py b/ext/pybind11/tests/test_stl_binders.py
index 0edf9e26e..bf1aa674c 100644
--- a/ext/pybind11/tests/test_stl_binders.py
+++ b/ext/pybind11/tests/test_stl_binders.py
@@ -1,107 +1,94 @@
import pytest
import sys
+from pybind11_tests import stl_binders as m
with pytest.suppress(ImportError):
import numpy as np
def test_vector_int():
- from pybind11_tests import VectorInt
-
- v_int = VectorInt([0, 0])
+ v_int = m.VectorInt([0, 0])
assert len(v_int) == 2
assert bool(v_int) is True
- v_int2 = VectorInt([0, 0])
+ v_int2 = m.VectorInt([0, 0])
assert v_int == v_int2
v_int2[1] = 1
assert 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)
+ v_int2.insert(6, 3)
assert str(v_int2) == "VectorInt[3, 2, 1, 0, 1, 2, 3]"
+ with pytest.raises(IndexError):
+ v_int2.insert(8, 4)
v_int.append(99)
v_int2[2:-2] = v_int
- assert v_int2 == VectorInt([3, 2, 0, 0, 99, 2, 3])
+ assert v_int2 == m.VectorInt([3, 2, 0, 0, 99, 2, 3])
del v_int2[1:3]
- assert v_int2 == VectorInt([3, 0, 99, 2, 3])
+ assert v_int2 == m.VectorInt([3, 0, 99, 2, 3])
del v_int2[0]
- assert v_int2 == VectorInt([0, 99, 2, 3])
+ assert v_int2 == m.VectorInt([0, 99, 2, 3])
+# related to the PyPy's buffer protocol.
@pytest.unsupported_on_pypy
def test_vector_buffer():
- from pybind11_tests import VectorUChar, create_undeclstruct
b = bytearray([1, 2, 3, 4])
- v = VectorUChar(b)
+ v = m.VectorUChar(b)
assert v[1] == 2
v[2] = 5
- m = memoryview(v) # We expose the buffer interface
+ mv = memoryview(v) # We expose the buffer interface
if sys.version_info.major > 2:
- assert m[2] == 5
- m[2] = 6
+ assert mv[2] == 5
+ mv[2] = 6
else:
- assert m[2] == '\x05'
- m[2] = '\x06'
+ assert mv[2] == '\x05'
+ mv[2] = '\x06'
assert v[2] == 6
- with pytest.raises(RuntimeError):
- create_undeclstruct() # Undeclared struct contents, no buffer interface
+ with pytest.raises(RuntimeError) as excinfo:
+ m.create_undeclstruct() # Undeclared struct contents, no buffer interface
+ assert "NumPy type info missing for " in str(excinfo.value)
+@pytest.unsupported_on_pypy
@pytest.requires_numpy
def test_vector_buffer_numpy():
- from pybind11_tests import VectorInt, VectorStruct, get_vectorstruct
-
a = np.array([1, 2, 3, 4], dtype=np.int32)
with pytest.raises(TypeError):
- VectorInt(a)
+ m.VectorInt(a)
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], dtype=np.uintc)
- v = VectorInt(a[0, :])
+ v = m.VectorInt(a[0, :])
assert len(v) == 4
assert v[2] == 3
- m = np.asarray(v)
- m[2] = 5
+ ma = np.asarray(v)
+ ma[2] = 5
assert v[2] == 5
- v = VectorInt(a[:, 1])
+ v = m.VectorInt(a[:, 1])
assert len(v) == 3
assert v[2] == 10
- v = get_vectorstruct()
+ v = m.get_vectorstruct()
assert v[0].x == 5
- m = np.asarray(v)
- m[1]['x'] = 99
+ ma = np.asarray(v)
+ ma[1]['x'] = 99
assert v[1].x == 99
- v = VectorStruct(np.zeros(3, dtype=np.dtype([('w', 'bool'), ('x', 'I'),
- ('y', 'float64'), ('z', 'bool')], align=True)))
+ v = m.VectorStruct(np.zeros(3, dtype=np.dtype([('w', 'bool'), ('x', 'I'),
+ ('y', 'float64'), ('z', 'bool')], align=True)))
assert len(v) == 3
-def test_vector_custom():
- from pybind11_tests import El, VectorEl, VectorVectorEl
-
- v_a = VectorEl()
- v_a.append(El(1))
- v_a.append(El(2))
- assert str(v_a) == "VectorEl[El{1}, El{2}]"
-
- vv_a = VectorVectorEl()
- vv_a.append(v_a)
- vv_b = vv_a[0]
- assert str(vv_b) == "VectorEl[El{1}, El{2}]"
-
-
def test_vector_bool():
- from pybind11_tests import VectorBool
+ import pybind11_cross_module_tests as cm
- vv_c = VectorBool()
+ vv_c = cm.VectorBool()
for i in range(10):
vv_c.append(i % 2 == 0)
for i in range(10):
@@ -109,18 +96,28 @@ def test_vector_bool():
assert str(vv_c) == "VectorBool[1, 0, 1, 0, 1, 0, 1, 0, 1, 0]"
-def test_map_string_double():
- from pybind11_tests import MapStringDouble, UnorderedMapStringDouble
+def test_vector_custom():
+ v_a = m.VectorEl()
+ v_a.append(m.El(1))
+ v_a.append(m.El(2))
+ assert str(v_a) == "VectorEl[El{1}, El{2}]"
- m = MapStringDouble()
- m['a'] = 1
- m['b'] = 2.5
+ vv_a = m.VectorVectorEl()
+ vv_a.append(v_a)
+ vv_b = vv_a[0]
+ assert str(vv_b) == "VectorEl[El{1}, El{2}]"
- assert list(m) == ['a', 'b']
- assert list(m.items()) == [('a', 1), ('b', 2.5)]
- assert str(m) == "MapStringDouble{a: 1, b: 2.5}"
- um = UnorderedMapStringDouble()
+def test_map_string_double():
+ mm = m.MapStringDouble()
+ mm['a'] = 1
+ mm['b'] = 2.5
+
+ assert list(mm) == ['a', 'b']
+ assert list(mm.items()) == [('a', 1), ('b', 2.5)]
+ assert str(mm) == "MapStringDouble{a: 1, b: 2.5}"
+
+ um = m.UnorderedMapStringDouble()
um['ua'] = 1.1
um['ub'] = 2.6
@@ -130,35 +127,29 @@ def test_map_string_double():
def test_map_string_double_const():
- from pybind11_tests import MapStringDoubleConst, UnorderedMapStringDoubleConst
-
- mc = MapStringDoubleConst()
+ mc = m.MapStringDoubleConst()
mc['a'] = 10
mc['b'] = 20.5
assert str(mc) == "MapStringDoubleConst{a: 10, b: 20.5}"
- umc = UnorderedMapStringDoubleConst()
+ umc = m.UnorderedMapStringDoubleConst()
umc['a'] = 11
umc['b'] = 21.5
str(umc)
-def test_noncopyable_vector():
- from pybind11_tests import get_vnc
-
- vnc = get_vnc(5)
+def test_noncopyable_containers():
+ # std::vector
+ vnc = m.get_vnc(5)
for i in range(0, 5):
assert vnc[i].value == i + 1
for i, j in enumerate(vnc, start=1):
assert j.value == i
-
-def test_noncopyable_deque():
- from pybind11_tests import get_dnc
-
- dnc = get_dnc(5)
+ # std::deque
+ dnc = m.get_dnc(5)
for i in range(0, 5):
assert dnc[i].value == i + 1
@@ -167,11 +158,8 @@ def test_noncopyable_deque():
assert(j.value == i)
i += 1
-
-def test_noncopyable_map():
- from pybind11_tests import get_mnc
-
- mnc = get_mnc(5)
+ # std::map
+ mnc = m.get_mnc(5)
for i in range(1, 6):
assert mnc[i].value == 10 * i
@@ -182,11 +170,8 @@ def test_noncopyable_map():
assert vsum == 150
-
-def test_noncopyable_unordered_map():
- from pybind11_tests import get_umnc
-
- mnc = get_umnc(5)
+ # std::unordered_map
+ mnc = m.get_umnc(5)
for i in range(1, 6):
assert mnc[i].value == 10 * i
diff --git a/ext/pybind11/tests/test_virtual_functions.cpp b/ext/pybind11/tests/test_virtual_functions.cpp
index 0f8ed2afb..953b390b8 100644
--- a/ext/pybind11/tests/test_virtual_functions.cpp
+++ b/ext/pybind11/tests/test_virtual_functions.cpp
@@ -145,16 +145,150 @@ class NCVirtTrampoline : public NCVirt {
}
};
-int runExampleVirt(ExampleVirt *ex, int value) {
- return ex->run(value);
-}
+struct Base {
+ /* for some reason MSVC2015 can't compile this if the function is pure virtual */
+ virtual std::string dispatch() const { return {}; };
+ virtual ~Base() = default;
+};
-bool runExampleVirtBool(ExampleVirt* ex) {
- return ex->run_bool();
-}
+struct DispatchIssue : Base {
+ virtual std::string dispatch() const {
+ PYBIND11_OVERLOAD_PURE(std::string, Base, dispatch, /* no arguments */);
+ }
+};
+
+// Forward declaration (so that we can put the main tests here; the inherited virtual approaches are
+// rather long).
+void initialize_inherited_virtuals(py::module &m);
+
+TEST_SUBMODULE(virtual_functions, m) {
+ // test_override
+ py::class_<ExampleVirt, PyExampleVirt>(m, "ExampleVirt")
+ .def(py::init<int>())
+ /* Reference original class in function definitions */
+ .def("run", &ExampleVirt::run)
+ .def("run_bool", &ExampleVirt::run_bool)
+ .def("pure_virtual", &ExampleVirt::pure_virtual);
+
+ py::class_<NonCopyable>(m, "NonCopyable")
+ .def(py::init<int, int>());
+
+ py::class_<Movable>(m, "Movable")
+ .def(py::init<int, int>());
+
+ // test_move_support
+#if !defined(__INTEL_COMPILER)
+ py::class_<NCVirt, NCVirtTrampoline>(m, "NCVirt")
+ .def(py::init<>())
+ .def("get_noncopyable", &NCVirt::get_noncopyable)
+ .def("get_movable", &NCVirt::get_movable)
+ .def("print_nc", &NCVirt::print_nc)
+ .def("print_movable", &NCVirt::print_movable);
+#endif
+
+ m.def("runExampleVirt", [](ExampleVirt *ex, int value) { return ex->run(value); });
+ m.def("runExampleVirtBool", [](ExampleVirt* ex) { return ex->run_bool(); });
+ m.def("runExampleVirtVirtual", [](ExampleVirt *ex) { ex->pure_virtual(); });
+
+ m.def("cstats_debug", &ConstructorStats::get<ExampleVirt>);
+ initialize_inherited_virtuals(m);
-void runExampleVirtVirtual(ExampleVirt *ex) {
- ex->pure_virtual();
+ // test_alias_delay_initialization1
+ // don't invoke Python dispatch classes by default when instantiating C++ classes
+ // that were not extended on the Python side
+ struct A {
+ virtual ~A() {}
+ virtual void f() { py::print("A.f()"); }
+ };
+
+ struct PyA : A {
+ PyA() { py::print("PyA.PyA()"); }
+ ~PyA() { py::print("PyA.~PyA()"); }
+
+ void f() override {
+ py::print("PyA.f()");
+ PYBIND11_OVERLOAD(void, A, f);
+ }
+ };
+
+ py::class_<A, PyA>(m, "A")
+ .def(py::init<>())
+ .def("f", &A::f);
+
+ m.def("call_f", [](A *a) { a->f(); });
+
+ // test_alias_delay_initialization2
+ // ... unless we explicitly request it, as in this example:
+ struct A2 {
+ virtual ~A2() {}
+ virtual void f() { py::print("A2.f()"); }
+ };
+
+ struct PyA2 : A2 {
+ PyA2() { py::print("PyA2.PyA2()"); }
+ ~PyA2() { py::print("PyA2.~PyA2()"); }
+ void f() override {
+ py::print("PyA2.f()");
+ PYBIND11_OVERLOAD(void, A2, f);
+ }
+ };
+
+ py::class_<A2, PyA2>(m, "A2")
+ .def(py::init_alias<>())
+ .def(py::init([](int) { return new PyA2(); }))
+ .def("f", &A2::f);
+
+ m.def("call_f", [](A2 *a2) { a2->f(); });
+
+ // test_dispatch_issue
+ // #159: virtual function dispatch has problems with similar-named functions
+ py::class_<Base, DispatchIssue>(m, "DispatchIssue")
+ .def(py::init<>())
+ .def("dispatch", &Base::dispatch);
+
+ m.def("dispatch_issue_go", [](const Base * b) { return b->dispatch(); });
+
+ // test_override_ref
+ // #392/397: overridding reference-returning functions
+ class OverrideTest {
+ public:
+ struct A { std::string value = "hi"; };
+ std::string v;
+ A a;
+ explicit OverrideTest(const std::string &v) : v{v} {}
+ virtual std::string str_value() { return v; }
+ virtual std::string &str_ref() { return v; }
+ virtual A A_value() { return a; }
+ virtual A &A_ref() { return a; }
+ virtual ~OverrideTest() = default;
+ };
+
+ class PyOverrideTest : public OverrideTest {
+ public:
+ using OverrideTest::OverrideTest;
+ std::string str_value() override { PYBIND11_OVERLOAD(std::string, OverrideTest, str_value); }
+ // Not allowed (uncommenting should hit a static_assert failure): we can't get a reference
+ // to a python numeric value, since we only copy values in the numeric type caster:
+// std::string &str_ref() override { PYBIND11_OVERLOAD(std::string &, OverrideTest, str_ref); }
+ // But we can work around it like this:
+ private:
+ std::string _tmp;
+ std::string str_ref_helper() { PYBIND11_OVERLOAD(std::string, OverrideTest, str_ref); }
+ public:
+ std::string &str_ref() override { return _tmp = str_ref_helper(); }
+
+ A A_value() override { PYBIND11_OVERLOAD(A, OverrideTest, A_value); }
+ A &A_ref() override { PYBIND11_OVERLOAD(A &, OverrideTest, A_ref); }
+ };
+
+ py::class_<OverrideTest::A>(m, "OverrideTest_A")
+ .def_readwrite("value", &OverrideTest::A::value);
+ py::class_<OverrideTest, PyOverrideTest>(m, "OverrideTest")
+ .def(py::init<const std::string &>())
+ .def("str_value", &OverrideTest::str_value)
+// .def("str_ref", &OverrideTest::str_ref)
+ .def("A_value", &OverrideTest::A_value)
+ .def("A_ref", &OverrideTest::A_ref);
}
@@ -179,6 +313,7 @@ public: \
return say_something(1) + " " + std::to_string(unlucky_number()); \
}
A_METHODS
+ virtual ~A_Repeat() = default;
};
class B_Repeat : public A_Repeat {
#define B_METHODS \
@@ -203,7 +338,7 @@ D_METHODS
};
// Base classes for templated inheritance trampolines. Identical to the repeat-everything version:
-class A_Tpl { A_METHODS };
+class A_Tpl { A_METHODS; virtual ~A_Tpl() = default; };
class B_Tpl : public A_Tpl { B_METHODS };
class C_Tpl : public B_Tpl { C_METHODS };
class D_Tpl : public C_Tpl { D_METHODS };
@@ -281,6 +416,8 @@ public:
void initialize_inherited_virtuals(py::module &m) {
+ // test_inherited_virtuals
+
// Method 1: repeat
py::class_<A_Repeat, PyA_Repeat>(m, "A_Repeat")
.def(py::init<>())
@@ -295,6 +432,7 @@ void initialize_inherited_virtuals(py::module &m) {
py::class_<D_Repeat, C_Repeat, PyD_Repeat>(m, "D_Repeat")
.def(py::init<>());
+ // test_
// Method 2: Templated trampolines
py::class_<A_Tpl, PyA_Tpl<>>(m, "A_Tpl")
.def(py::init<>())
@@ -310,38 +448,3 @@ void initialize_inherited_virtuals(py::module &m) {
.def(py::init<>());
};
-
-
-test_initializer virtual_functions([](py::module &m) {
- /* Important: indicate the trampoline class PyExampleVirt using the third
- argument to py::class_. The second argument with the unique pointer
- is simply the default holder type used by pybind11. */
- py::class_<ExampleVirt, PyExampleVirt>(m, "ExampleVirt")
- .def(py::init<int>())
- /* Reference original class in function definitions */
- .def("run", &ExampleVirt::run)
- .def("run_bool", &ExampleVirt::run_bool)
- .def("pure_virtual", &ExampleVirt::pure_virtual);
-
- py::class_<NonCopyable>(m, "NonCopyable")
- .def(py::init<int, int>());
-
- py::class_<Movable>(m, "Movable")
- .def(py::init<int, int>());
-
-#if !defined(__INTEL_COMPILER)
- py::class_<NCVirt, NCVirtTrampoline>(m, "NCVirt")
- .def(py::init<>())
- .def("get_noncopyable", &NCVirt::get_noncopyable)
- .def("get_movable", &NCVirt::get_movable)
- .def("print_nc", &NCVirt::print_nc)
- .def("print_movable", &NCVirt::print_movable);
-#endif
-
- m.def("runExampleVirt", &runExampleVirt);
- m.def("runExampleVirtBool", &runExampleVirtBool);
- m.def("runExampleVirtVirtual", &runExampleVirtVirtual);
-
- m.def("cstats_debug", &ConstructorStats::get<ExampleVirt>);
- initialize_inherited_virtuals(m);
-});
diff --git a/ext/pybind11/tests/test_virtual_functions.py b/ext/pybind11/tests/test_virtual_functions.py
index b11c699df..b91ebfa3e 100644
--- a/ext/pybind11/tests/test_virtual_functions.py
+++ b/ext/pybind11/tests/test_virtual_functions.py
@@ -1,13 +1,11 @@
import pytest
-import pybind11_tests
+
+from pybind11_tests import virtual_functions as m
from pybind11_tests import ConstructorStats
def test_override(capture, msg):
- from pybind11_tests import (ExampleVirt, runExampleVirt, runExampleVirtVirtual,
- runExampleVirtBool)
-
- class ExtendedExampleVirt(ExampleVirt):
+ class ExtendedExampleVirt(m.ExampleVirt):
def __init__(self, state):
super(ExtendedExampleVirt, self).__init__(state + 1)
self.data = "Hello world"
@@ -33,40 +31,40 @@ def test_override(capture, msg):
def get_string2(self):
return "override2"
- ex12 = ExampleVirt(10)
+ ex12 = m.ExampleVirt(10)
with capture:
- assert runExampleVirt(ex12, 20) == 30
+ assert m.runExampleVirt(ex12, 20) == 30
assert capture == """
Original implementation of ExampleVirt::run(state=10, value=20, str1=default1, str2=default2)
""" # noqa: E501 line too long
with pytest.raises(RuntimeError) as excinfo:
- runExampleVirtVirtual(ex12)
+ m.runExampleVirtVirtual(ex12)
assert msg(excinfo.value) == 'Tried to call pure virtual function "ExampleVirt::pure_virtual"'
ex12p = ExtendedExampleVirt(10)
with capture:
- assert runExampleVirt(ex12p, 20) == 32
+ assert m.runExampleVirt(ex12p, 20) == 32
assert capture == """
ExtendedExampleVirt::run(20), calling parent..
Original implementation of ExampleVirt::run(state=11, value=21, str1=override1, str2=default2)
""" # noqa: E501 line too long
with capture:
- assert runExampleVirtBool(ex12p) is False
+ assert m.runExampleVirtBool(ex12p) is False
assert capture == "ExtendedExampleVirt::run_bool()"
with capture:
- runExampleVirtVirtual(ex12p)
+ m.runExampleVirtVirtual(ex12p)
assert capture == "ExtendedExampleVirt::pure_virtual(): Hello world"
ex12p2 = ExtendedExampleVirt2(15)
with capture:
- assert runExampleVirt(ex12p2, 50) == 68
+ assert m.runExampleVirt(ex12p2, 50) == 68
assert capture == """
ExtendedExampleVirt::run(50), calling parent..
Original implementation of ExampleVirt::run(state=17, value=51, str1=override1, str2=override2)
""" # noqa: E501 line too long
- cstats = ConstructorStats.get(ExampleVirt)
+ cstats = ConstructorStats.get(m.ExampleVirt)
assert cstats.alive() == 3
del ex12, ex12p, ex12p2
assert cstats.alive() == 0
@@ -75,14 +73,181 @@ def test_override(capture, msg):
assert cstats.move_constructions >= 0
-def test_inheriting_repeat():
- from pybind11_tests import A_Repeat, B_Repeat, C_Repeat, D_Repeat, A_Tpl, B_Tpl, C_Tpl, D_Tpl
+def test_alias_delay_initialization1(capture):
+ """`A` only initializes its trampoline class when we inherit from it
+
+ If we just create and use an A instance directly, the trampoline initialization is
+ bypassed and we only initialize an A() instead (for performance reasons).
+ """
+ class B(m.A):
+ def __init__(self):
+ super(B, self).__init__()
+
+ def f(self):
+ print("In python f()")
+
+ # C++ version
+ with capture:
+ a = m.A()
+ m.call_f(a)
+ del a
+ pytest.gc_collect()
+ assert capture == "A.f()"
+
+ # Python version
+ with capture:
+ b = B()
+ m.call_f(b)
+ del b
+ pytest.gc_collect()
+ assert capture == """
+ PyA.PyA()
+ PyA.f()
+ In python f()
+ PyA.~PyA()
+ """
+
+
+def test_alias_delay_initialization2(capture):
+ """`A2`, unlike the above, is configured to always initialize the alias
+
+ While the extra initialization and extra class layer has small virtual dispatch
+ performance penalty, it also allows us to do more things with the trampoline
+ class such as defining local variables and performing construction/destruction.
+ """
+ class B2(m.A2):
+ def __init__(self):
+ super(B2, self).__init__()
+
+ def f(self):
+ print("In python B2.f()")
+
+ # No python subclass version
+ with capture:
+ a2 = m.A2()
+ m.call_f(a2)
+ del a2
+ pytest.gc_collect()
+ a3 = m.A2(1)
+ m.call_f(a3)
+ del a3
+ pytest.gc_collect()
+ assert capture == """
+ PyA2.PyA2()
+ PyA2.f()
+ A2.f()
+ PyA2.~PyA2()
+ PyA2.PyA2()
+ PyA2.f()
+ A2.f()
+ PyA2.~PyA2()
+ """
+
+ # Python subclass version
+ with capture:
+ b2 = B2()
+ m.call_f(b2)
+ del b2
+ pytest.gc_collect()
+ assert capture == """
+ PyA2.PyA2()
+ PyA2.f()
+ In python B2.f()
+ PyA2.~PyA2()
+ """
+
+
+# PyPy: Reference count > 1 causes call with noncopyable instance
+# to fail in ncv1.print_nc()
+@pytest.unsupported_on_pypy
+@pytest.mark.skipif(not hasattr(m, "NCVirt"), reason="NCVirt test broken on ICPC")
+def test_move_support():
+ class NCVirtExt(m.NCVirt):
+ def get_noncopyable(self, a, b):
+ # Constructs and returns a new instance:
+ nc = m.NonCopyable(a * a, b * b)
+ return nc
+
+ def get_movable(self, a, b):
+ # Return a referenced copy
+ self.movable = m.Movable(a, b)
+ return self.movable
+
+ class NCVirtExt2(m.NCVirt):
+ def get_noncopyable(self, a, b):
+ # Keep a reference: this is going to throw an exception
+ self.nc = m.NonCopyable(a, b)
+ return self.nc
+
+ def get_movable(self, a, b):
+ # Return a new instance without storing it
+ return m.Movable(a, b)
+
+ ncv1 = NCVirtExt()
+ assert ncv1.print_nc(2, 3) == "36"
+ assert ncv1.print_movable(4, 5) == "9"
+ ncv2 = NCVirtExt2()
+ assert ncv2.print_movable(7, 7) == "14"
+ # Don't check the exception message here because it differs under debug/non-debug mode
+ with pytest.raises(RuntimeError):
+ ncv2.print_nc(9, 9)
- class AR(A_Repeat):
+ nc_stats = ConstructorStats.get(m.NonCopyable)
+ mv_stats = ConstructorStats.get(m.Movable)
+ assert nc_stats.alive() == 1
+ assert mv_stats.alive() == 1
+ del ncv1, ncv2
+ assert nc_stats.alive() == 0
+ assert mv_stats.alive() == 0
+ assert nc_stats.values() == ['4', '9', '9', '9']
+ assert mv_stats.values() == ['4', '5', '7', '7']
+ assert nc_stats.copy_constructions == 0
+ assert mv_stats.copy_constructions == 1
+ assert nc_stats.move_constructions >= 0
+ assert mv_stats.move_constructions >= 0
+
+
+def test_dispatch_issue(msg):
+ """#159: virtual function dispatch has problems with similar-named functions"""
+ class PyClass1(m.DispatchIssue):
+ def dispatch(self):
+ return "Yay.."
+
+ class PyClass2(m.DispatchIssue):
+ def dispatch(self):
+ with pytest.raises(RuntimeError) as excinfo:
+ super(PyClass2, self).dispatch()
+ assert msg(excinfo.value) == 'Tried to call pure virtual function "Base::dispatch"'
+
+ p = PyClass1()
+ return m.dispatch_issue_go(p)
+
+ b = PyClass2()
+ assert m.dispatch_issue_go(b) == "Yay.."
+
+
+def test_override_ref():
+ """#392/397: overridding reference-returning functions"""
+ o = m.OverrideTest("asdf")
+
+ # Not allowed (see associated .cpp comment)
+ # i = o.str_ref()
+ # assert o.str_ref() == "asdf"
+ assert o.str_value() == "asdf"
+
+ assert o.A_value().value == "hi"
+ a = o.A_ref()
+ assert a.value == "hi"
+ a.value = "bye"
+ assert a.value == "bye"
+
+
+def test_inherited_virtuals():
+ class AR(m.A_Repeat):
def unlucky_number(self):
return 99
- class AT(A_Tpl):
+ class AT(m.A_Tpl):
def unlucky_number(self):
return 999
@@ -96,21 +261,21 @@ def test_inheriting_repeat():
assert obj.unlucky_number() == 999
assert obj.say_everything() == "hi 999"
- for obj in [B_Repeat(), B_Tpl()]:
+ for obj in [m.B_Repeat(), m.B_Tpl()]:
assert obj.say_something(3) == "B says hi 3 times"
assert obj.unlucky_number() == 13
assert obj.lucky_number() == 7.0
assert obj.say_everything() == "B says hi 1 times 13"
- for obj in [C_Repeat(), C_Tpl()]:
+ for obj in [m.C_Repeat(), m.C_Tpl()]:
assert obj.say_something(3) == "B says hi 3 times"
assert obj.unlucky_number() == 4444
assert obj.lucky_number() == 888.0
assert obj.say_everything() == "B says hi 1 times 4444"
- class CR(C_Repeat):
+ class CR(m.C_Repeat):
def lucky_number(self):
- return C_Repeat.lucky_number(self) + 1.25
+ return m.C_Repeat.lucky_number(self) + 1.25
obj = CR()
assert obj.say_something(3) == "B says hi 3 times"
@@ -118,7 +283,7 @@ def test_inheriting_repeat():
assert obj.lucky_number() == 889.25
assert obj.say_everything() == "B says hi 1 times 4444"
- class CT(C_Tpl):
+ class CT(m.C_Tpl):
pass
obj = CT()
@@ -147,14 +312,14 @@ def test_inheriting_repeat():
assert obj.lucky_number() == 888000.0
assert obj.say_everything() == "B says hi 1 times 4444"
- class DR(D_Repeat):
+ class DR(m.D_Repeat):
def unlucky_number(self):
return 123
def lucky_number(self):
return 42.0
- for obj in [D_Repeat(), D_Tpl()]:
+ for obj in [m.D_Repeat(), m.D_Tpl()]:
assert obj.say_something(3) == "B says hi 3 times"
assert obj.unlucky_number() == 4444
assert obj.lucky_number() == 888.0
@@ -166,7 +331,7 @@ def test_inheriting_repeat():
assert obj.lucky_number() == 42.0
assert obj.say_everything() == "B says hi 1 times 123"
- class DT(D_Tpl):
+ class DT(m.D_Tpl):
def say_something(self, times):
return "DT says:" + (' quack' * times)
@@ -189,7 +354,7 @@ def test_inheriting_repeat():
def unlucky_number(self):
return -3
- class BT(B_Tpl):
+ class BT(m.B_Tpl):
def say_something(self, times):
return "BT" * times
@@ -204,56 +369,3 @@ def test_inheriting_repeat():
assert obj.unlucky_number() == -7
assert obj.lucky_number() == -1.375
assert obj.say_everything() == "BT -7"
-
-
-# PyPy: Reference count > 1 causes call with noncopyable instance
-# to fail in ncv1.print_nc()
-@pytest.unsupported_on_pypy
-@pytest.mark.skipif(not hasattr(pybind11_tests, 'NCVirt'),
- reason="NCVirt test broken on ICPC")
-def test_move_support():
- from pybind11_tests import NCVirt, NonCopyable, Movable
-
- 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()
- assert ncv1.print_nc(2, 3) == "36"
- assert ncv1.print_movable(4, 5) == "9"
- ncv2 = NCVirtExt2()
- assert ncv2.print_movable(7, 7) == "14"
- # Don't check the exception message here because it differs under debug/non-debug mode
- with pytest.raises(RuntimeError):
- ncv2.print_nc(9, 9)
-
- nc_stats = ConstructorStats.get(NonCopyable)
- mv_stats = ConstructorStats.get(Movable)
- assert nc_stats.alive() == 1
- assert mv_stats.alive() == 1
- del ncv1, ncv2
- assert nc_stats.alive() == 0
- assert mv_stats.alive() == 0
- assert nc_stats.values() == ['4', '9', '9', '9']
- assert mv_stats.values() == ['4', '5', '7', '7']
- assert nc_stats.copy_constructions == 0
- assert mv_stats.copy_constructions == 1
- assert nc_stats.move_constructions >= 0
- assert mv_stats.move_constructions >= 0