mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-23 13:45:10 +00:00
705efccecd
* API: Make `numpy.h` compatible with both NumPy 1.x and 2.x * TST: Update numpy dtype flags test to not covert flags to char * API: Add `numpy2.h` instead and make `numpy.h` safe This means that users of `numpy.h` cannot be broken, but need to update to `numpy2.h` if they want to compile for NumPy 2. Using Macros simply and didn't bother to try to remove unnecessary code paths. * API: Rather than `numpy2.h` use a define for the user. * Thread `PYBIND11_NUMPY2_SUPPORT` through things and try to adept test matrix * Small fixups (shouldn't matter)? * Fixup. Does upgrading scipy help? (it shouldn't?) (Some other small fixup) * Use NumPy 2 nightlies for ubuntu-latest job also * BUG: Fix numpy.bool check * TST: Fix complexwarning * BUG: Fix the fact that only the 50 slot is filled with the copy alias (There were 3 functions all doing the same, only this slot survived 2.x) * TST: One more test tweak * TST: Use "long" name for long, since it changed on windows * TST: Apparently we didn't always have ulong, so just use `L` * TST: Enforce dtype='l' for test as default isn't long anymore on windows * Rename macro and invert logic to PYBIND11_NUMPY_1_ONLY * PYBIND11_INTERNAL_NUMPY_1_ONLY_DETECTED * Test and code comment expansion * CI: Use pre-releases of numpy/scipy from pip via explicit version * CI: NumPy 2 only available on almalinux (as it is Python >=3.9) * MAINT: Match name more exactly and adopt error phrasing * MAINT: Pushed early, move helper to be private member * fix error message compilation when using NumPy 1.x-only backcompat * silence name shadowing warning * chore: minor optimization Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com> --------- Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com> Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com> Co-authored-by: Henry Schreiner <henryschreineriii@gmail.com>
130 lines
4.5 KiB
C++
130 lines
4.5 KiB
C++
/*
|
|
tests/pybind11_tests.cpp -- pybind example plugin
|
|
|
|
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 <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
|
|
compile some test files to disable/ignore some of the test code.
|
|
|
|
It is NOT recommended as a way to use pybind11 in practice, however: the initialization order will
|
|
be essentially random, which is okay for our test scripts (there are no dependencies between the
|
|
individual pybind11 test .cpp files), but most likely not what you want when using pybind11
|
|
productively.
|
|
|
|
Instead, see the "How can I reduce the build time?" question in the "Frequently asked questions"
|
|
section of the documentation for good practice on splitting binding code over multiple files.
|
|
*/
|
|
std::list<std::function<void(py::module_ &)>> &initializers() {
|
|
static std::list<std::function<void(py::module_ &)>> inits;
|
|
return inits;
|
|
}
|
|
|
|
test_initializer::test_initializer(Initializer init) { initializers().emplace_back(init); }
|
|
|
|
test_initializer::test_initializer(const char *submodule_name, Initializer init) {
|
|
initializers().emplace_back([=](py::module_ &parent) {
|
|
auto m = parent.def_submodule(submodule_name);
|
|
init(m);
|
|
});
|
|
}
|
|
|
|
void bind_ConstructorStats(py::module_ &m) {
|
|
py::class_<ConstructorStats>(m, "ConstructorStats")
|
|
.def("alive", &ConstructorStats::alive)
|
|
.def("values", &ConstructorStats::values)
|
|
.def_readwrite("default_constructions", &ConstructorStats::default_constructions)
|
|
.def_readwrite("copy_assignments", &ConstructorStats::copy_assignments)
|
|
.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)
|
|
|
|
// 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();
|
|
});
|
|
}
|
|
|
|
const char *cpp_std() {
|
|
return
|
|
#if defined(PYBIND11_CPP20)
|
|
"C++20";
|
|
#elif defined(PYBIND11_CPP17)
|
|
"C++17";
|
|
#elif defined(PYBIND11_CPP14)
|
|
"C++14";
|
|
#else
|
|
"C++11";
|
|
#endif
|
|
}
|
|
|
|
PYBIND11_MODULE(pybind11_tests, m) {
|
|
m.doc() = "pybind11 test module";
|
|
|
|
// Intentionally kept minimal to not create a maintenance chore
|
|
// ("just enough" to be conclusive).
|
|
#if defined(__VERSION__)
|
|
m.attr("compiler_info") = __VERSION__;
|
|
#elif defined(_MSC_FULL_VER)
|
|
m.attr("compiler_info") = "MSVC " PYBIND11_TOSTRING(_MSC_FULL_VER);
|
|
#else
|
|
m.attr("compiler_info") = py::none();
|
|
#endif
|
|
m.attr("cpp_std") = cpp_std();
|
|
m.attr("PYBIND11_INTERNALS_ID") = PYBIND11_INTERNALS_ID;
|
|
m.attr("PYBIND11_SIMPLE_GIL_MANAGEMENT") =
|
|
#if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
|
|
true;
|
|
#else
|
|
false;
|
|
#endif
|
|
m.attr("PYBIND11_NUMPY_1_ONLY") =
|
|
#if defined(PYBIND11_NUMPY_1_ONLY)
|
|
true;
|
|
#else
|
|
false;
|
|
#endif
|
|
|
|
bind_ConstructorStats(m);
|
|
|
|
#if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
|
|
m.attr("detailed_error_messages_enabled") = true;
|
|
#else
|
|
m.attr("detailed_error_messages_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);
|
|
}
|
|
}
|