/usr/local/lib64/python3.6/site-packages/torch/include/pybind11
NameSizeModeActions
detail/-0755rm
attr.h214120644editdlrm
buffer_info.h61180644editdlrm
cast.h955570644editdlrm
chrono.h81850644editdlrm
common.h1200644editdlrm
complex.h20370644editdlrm
eigen.h290860644editdlrm
embed.h78430644editdlrm
eval.h50790644editdlrm
functional.h37090644editdlrm
iostream.h60840644editdlrm
numpy.h693100644editdlrm
operators.h90850644editdlrm
options.h20490644editdlrm
pybind11.h1115580644editdlrm
pytypes.h661180644editdlrm
stl.h141360644editdlrm
stl_bind.h239120644editdlrm
Edit: /usr/local/lib64/python3.6/site-packages/torch/include/pybind11/functional.h (3709B)
/* pybind11/functional.h: std::function<> support Copyright (c) 2016 Wenzel Jakob 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 PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(detail) template struct type_caster> { using type = std::function; using retval_type = conditional_t::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(src)) return false; auto func = reinterpret_borrow(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 c = reinterpret_borrow(PyCFunction_GET_SELF(cfunc.ptr())); auto rec = (function_record *) c; if (rec && rec->is_stateless && same_type(typeid(function_type), *reinterpret_cast(rec->data[1]))) { struct capture { function_type f; }; value = ((capture *) &rec->data)->f; return true; } } // ensure GIL is held during functor destruction struct func_handle { function f; func_handle(function&& f_) : f(std::move(f_)) {} func_handle(const func_handle& f_) { gil_scoped_acquire acq; f = f_.f; } ~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; func_wrapper(func_handle&& hf): hfunc(std::move(hf)) {} Return operator()(Args... args) const { gil_scoped_acquire acq; object retval(hfunc.f(std::forward(args)...)); /* Visual studio 2015 parser issue: need parentheses around this expression */ return (retval.template cast()); } }; value = func_wrapper(func_handle(std::move(func))); return true; } template static handle cast(Func &&f_, return_value_policy policy, handle /* parent */) { if (!f_) return none().inc_ref(); auto result = f_.template target(); if (result) return cpp_function(*result, policy).release(); else return cpp_function(std::forward(f_), policy).release(); } PYBIND11_TYPE_CASTER(type, _("Callable[[") + concat(make_caster::name...) + _("], ") + make_caster::name + _("]")); }; PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)