mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-24 22:25:10 +00:00
a97e9d8cac
* Changing `_MSC_VER` guard to `< 1910` (dropping MSVC 2015). * Removing MSVC 2015 from ci.yml, and .appveyor.yml entirely. * Bringing back .appveyor.yml from master. * appveyor Visual Studio 2017 * 1st manual pass, builds & tests with unix_clang, before pre-commit. * After clang-format (via pre-commit). * Manual pass looking for "2015", builds & tests with unix_clang, before pre-commit. * Backtracking for include/pybind11 changes in previous commit. git checkoutd07865846c
include/pybind11/attr.h include/pybind11/detail/common.h include/pybind11/functional.h -------------------- CI #4160 errors observed:2a26873727
https://github.com/pybind/pybind11/runs/5168332130?check_suite_focus=true $ grep ' error C' *.txt | sed 's/2022-02-12[^ ]*//' | sed 's/^[0-9][0-9]*//' | sed 's/^.*\.txt: //' | sort | uniqD:\a\pybind11\pybind11\include\pybind11\cast.h(1364,1): error C2752: 'pybind11::detail::type_caster<Eigen::Ref<Eigen::Vector3f,0,pybind11::EigenDStride>,void>': more than one partial specialization matches the template argument list [D:\a\pybind11\pybind11\build\tests\pybind11_tests.vcxproj] d:\a\pybind11\pybind11\include\pybind11\detail/common.h(1023): error C2737: 'pybind11::overload_cast': 'constexpr' object must be initialized [D:\a\pybind11\pybind11\build\tests\cross_module_gil_utils.vcxproj] d:\a\pybind11\pybind11\include\pybind11\detail/common.h(1023): error C2737: 'pybind11::overload_cast': 'constexpr' object must be initialized [D:\a\pybind11\pybind11\build\tests\pybind11_cross_module_tests.vcxproj] d:\a\pybind11\pybind11\include\pybind11\detail/common.h(1023): error C2737: 'pybind11::overload_cast': 'constexpr' object must be initialized [D:\a\pybind11\pybind11\build\tests\pybind11_tests.vcxproj] d:\a\pybind11\pybind11\include\pybind11\detail/common.h(1023): error C2737: 'pybind11::overload_cast': 'constexpr' object must be initialized [D:\a\pybind11\pybind11\build\tests\test_embed\external_module.vcxproj] D:\a\pybind11\pybind11\include\pybind11\detail/common.h(624): fatal error C1001: Internal compiler error. [D:\a\pybind11\pybind11\build\tests\pybind11_tests.vcxproj] D:\a\pybind11\pybind11\include\pybind11\detail/common.h(624): fatal error C1001: Internal compiler error. [D:\a\pybind11\pybind11\tests\pybind11_tests.vcxproj] $ grep ': error C2737' *.txt | sed 's/^.*MSVC//' | sed 's/___.*//' | sort | uniq _2017 $ grep ': error C2752' *.txt 3______3.8_____MSVC_2019_____x86_-DCMAKE_CXX_STANDARD=17.txt:2022-02-12T16:12:45.9921122Z D:\a\pybind11\pybind11\include\pybind11\cast.h(1364,1): error C2752: 'pybind11::detail::type_caster<Eigen::Ref<Eigen::Vector3f,0,pybind11::EigenDStride>,void>': more than one partial specialization matches the template argument list [D:\a\pybind11\pybind11\build\tests\pybind11_tests.vcxproj] $ grep ': fatal error C1001:' *.txt 10______pypy-3.8-v7.3.7_____windows-2022_____x64.txt:2022-02-12T16:12:56.3163683Z D:\a\pybind11\pybind11\include\pybind11\detail/common.h(624): fatal error C1001: Internal compiler error. [D:\a\pybind11\pybind11\tests\pybind11_tests.vcxproj] 1______3.6_____MSVC_2019_____x86.txt:2022-02-12T16:12:47.6774625Z D:\a\pybind11\pybind11\include\pybind11\detail/common.h(624): fatal error C1001: Internal compiler error. [D:\a\pybind11\pybind11\build\tests\pybind11_tests.vcxproj] 16______3.6_____windows-latest_____x64_-DPYBIND11_FINDPYTHON=ON.txt:2022-02-12T16:12:27.0556151Z D:\a\pybind11\pybind11\include\pybind11\detail/common.h(624): fatal error C1001: Internal compiler error. [D:\a\pybind11\pybind11\tests\pybind11_tests.vcxproj] 17______3.9_____windows-2019_____x64.txt:2022-02-12T16:12:30.3822566Z D:\a\pybind11\pybind11\include\pybind11\detail/common.h(624): fatal error C1001: Internal compiler error. [D:\a\pybind11\pybind11\tests\pybind11_tests.vcxproj] 2______3.7_____MSVC_2019_____x86.txt:2022-02-12T16:12:38.7018911Z D:\a\pybind11\pybind11\include\pybind11\detail/common.h(624): fatal error C1001: Internal compiler error. [D:\a\pybind11\pybind11\build\tests\pybind11_tests.vcxproj] 6______3.6_____windows-2022_____x64.txt:2022-02-12T16:12:00.4513642Z D:\a\pybind11\pybind11\include\pybind11\detail/common.h(624): fatal error C1001: Internal compiler error. [D:\a\pybind11\pybind11\tests\pybind11_tests.vcxproj] 7______3.9_____windows-2022_____x64.txt:2022-02-12T16:11:43.6306160Z D:\a\pybind11\pybind11\include\pybind11\detail/common.h(624): fatal error C1001: Internal compiler error. [D:\a\pybind11\pybind11\tests\pybind11_tests.vcxproj] 8______3.10_____windows-2022_____x64.txt:2022-02-12T16:11:49.9589644Z D:\a\pybind11\pybind11\include\pybind11\detail/common.h(624): fatal error C1001: Internal compiler error. [D:\a\pybind11\pybind11\tests\pybind11_tests.vcxproj] 9______pypy-3.7-v7.3.7_____windows-2022_____x64.txt:2022-02-12T16:11:53.7912112Z D:\a\pybind11\pybind11\include\pybind11\detail/common.h(624): fatal error C1001: Internal compiler error. [D:\a\pybind11\pybind11\tests\pybind11_tests.vcxproj] * common.h: is_template_base_of * Re-applying 4 changes from2a26873727
that work universally. * `overload_cast = {};` only for MSVC 2017 and Clang 5 * Refining condition for using is_template_base_of workaround. * Undoing MSVC 2015 workaround in test_constants_and_functions.cpp * CentOS7: silence_unused_warnings * Tweaks in response to reviews. * Adding windows-2022 C++20 * Trying another way of adding windows-2022 C++20
131 lines
4.5 KiB
C++
131 lines
4.5 KiB
C++
/*
|
|
pybind11/functional.h: std::function<> support
|
|
|
|
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.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include "pybind11.h"
|
|
|
|
#include <functional>
|
|
|
|
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
|
|
PYBIND11_NAMESPACE_BEGIN(detail)
|
|
|
|
template <typename Return, typename... Args>
|
|
struct type_caster<std::function<Return(Args...)>> {
|
|
using type = std::function<Return(Args...)>;
|
|
using retval_type = conditional_t<std::is_same<Return, void>::value, void_type, Return>;
|
|
using function_type = Return (*)(Args...);
|
|
|
|
public:
|
|
bool load(handle src, bool convert) {
|
|
if (src.is_none()) {
|
|
// Defer accepting None to other overloads (if we aren't in convert mode):
|
|
if (!convert) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
if (!isinstance<function>(src)) {
|
|
return false;
|
|
}
|
|
|
|
auto func = reinterpret_borrow<function>(src);
|
|
|
|
/*
|
|
When passing a C++ function as an argument to another C++
|
|
function via Python, every function call would normally involve
|
|
a full C++ -> Python -> C++ roundtrip, which can be prohibitive.
|
|
Here, we try to at least detect the case where the function is
|
|
stateless (i.e. function pointer or lambda function without
|
|
captured variables), in which case the roundtrip can be avoided.
|
|
*/
|
|
if (auto cfunc = func.cpp_function()) {
|
|
auto *cfunc_self = PyCFunction_GET_SELF(cfunc.ptr());
|
|
if (isinstance<capsule>(cfunc_self)) {
|
|
auto c = reinterpret_borrow<capsule>(cfunc_self);
|
|
auto *rec = (function_record *) c;
|
|
|
|
while (rec != nullptr) {
|
|
if (rec->is_stateless
|
|
&& same_type(typeid(function_type),
|
|
*reinterpret_cast<const std::type_info *>(rec->data[1]))) {
|
|
struct capture {
|
|
function_type f;
|
|
};
|
|
value = ((capture *) &rec->data)->f;
|
|
return true;
|
|
}
|
|
rec = rec->next;
|
|
}
|
|
}
|
|
// PYPY segfaults here when passing builtin function like sum.
|
|
// Raising an fail exception here works to prevent the segfault, but only on gcc.
|
|
// See PR #1413 for full details
|
|
}
|
|
|
|
// ensure GIL is held during functor destruction
|
|
struct func_handle {
|
|
function f;
|
|
#if !(defined(_MSC_VER) && _MSC_VER == 1916 && defined(PYBIND11_CPP17))
|
|
// This triggers a syntax error under very special conditions (very weird indeed).
|
|
explicit
|
|
#endif
|
|
func_handle(function &&f_) noexcept
|
|
: f(std::move(f_)) {
|
|
}
|
|
func_handle(const func_handle &f_) { operator=(f_); }
|
|
func_handle &operator=(const func_handle &f_) {
|
|
gil_scoped_acquire acq;
|
|
f = f_.f;
|
|
return *this;
|
|
}
|
|
~func_handle() {
|
|
gil_scoped_acquire acq;
|
|
function kill_f(std::move(f));
|
|
}
|
|
};
|
|
|
|
// to emulate 'move initialization capture' in C++11
|
|
struct func_wrapper {
|
|
func_handle hfunc;
|
|
explicit func_wrapper(func_handle &&hf) noexcept : hfunc(std::move(hf)) {}
|
|
Return operator()(Args... args) const {
|
|
gil_scoped_acquire acq;
|
|
object retval(hfunc.f(std::forward<Args>(args)...));
|
|
return retval.template cast<Return>();
|
|
}
|
|
};
|
|
|
|
value = func_wrapper(func_handle(std::move(func)));
|
|
return true;
|
|
}
|
|
|
|
template <typename Func>
|
|
static handle cast(Func &&f_, return_value_policy policy, handle /* parent */) {
|
|
if (!f_) {
|
|
return none().inc_ref();
|
|
}
|
|
|
|
auto result = f_.template target<function_type>();
|
|
if (result) {
|
|
return cpp_function(*result, policy).release();
|
|
}
|
|
return cpp_function(std::forward<Func>(f_), policy).release();
|
|
}
|
|
|
|
PYBIND11_TYPE_CASTER(type,
|
|
const_name("Callable[[") + concat(make_caster<Args>::name...)
|
|
+ const_name("], ") + make_caster<retval_type>::name
|
|
+ const_name("]"));
|
|
};
|
|
|
|
PYBIND11_NAMESPACE_END(detail)
|
|
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
|