Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1 | /* |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 2 | pybind11/pybind11.h: Main header file of the C++11 python |
| 3 | binding generator library |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 4 | |
| 5 | Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch> |
| 6 | |
| 7 | All rights reserved. Use of this source code is governed by a |
| 8 | BSD-style license that can be found in the LICENSE file. |
| 9 | */ |
| 10 | |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 11 | #pragma once |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 12 | |
| 13 | #if defined(_MSC_VER) |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 14 | # pragma warning(push) |
| 15 | # pragma warning(disable: 4127) // warning C4127: Conditional expression is constant |
| 16 | # pragma warning(disable: 4800) // warning C4800: 'int': forcing value to bool 'true' or 'false' (performance warning) |
| 17 | # pragma warning(disable: 4996) // warning C4996: The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name |
| 18 | # pragma warning(disable: 4100) // warning C4100: Unreferenced formal parameter |
| 19 | # pragma warning(disable: 4512) // warning C4512: Assignment operator was implicitly defined as deleted |
Ben Pritchard | 70ee47d | 2016-02-18 13:06:43 -0500 | [diff] [blame] | 20 | #elif defined(__ICC) || defined(__INTEL_COMPILER) |
Ben Pritchard | 33f3430 | 2016-02-18 15:25:51 -0500 | [diff] [blame] | 21 | # pragma warning(push) |
Ben Pritchard | 70ee47d | 2016-02-18 13:06:43 -0500 | [diff] [blame] | 22 | # pragma warning(disable:2196) // warning #2196: routine is both "inline" and "noinline" |
Wenzel Jakob | cd5cda7 | 2015-08-03 12:17:54 +0200 | [diff] [blame] | 23 | #elif defined(__GNUG__) and !defined(__clang__) |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 24 | # pragma GCC diagnostic push |
| 25 | # pragma GCC diagnostic ignored "-Wunused-but-set-parameter" |
| 26 | # pragma GCC diagnostic ignored "-Wunused-but-set-variable" |
| 27 | # pragma GCC diagnostic ignored "-Wmissing-field-initializers" |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 28 | #endif |
| 29 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 30 | #include "attr.h" |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 31 | |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 32 | NAMESPACE_BEGIN(pybind11) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 33 | |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 34 | /// Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 35 | class cpp_function : public function { |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 36 | protected: |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 37 | /// Picks a suitable return value converter from cast.h |
| 38 | template <typename T> using return_value_caster = |
| 39 | detail::type_caster<typename std::conditional< |
Wenzel Jakob | 4177ed4 | 2016-01-17 22:36:38 +0100 | [diff] [blame] | 40 | std::is_void<T>::value, detail::void_type, typename detail::intrinsic_type<T>::type>::type>; |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 41 | |
| 42 | /// Picks a suitable argument value converter from cast.h |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 43 | template <typename... T> using arg_value_caster = |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 44 | detail::type_caster<typename std::tuple<T...>>; |
| 45 | public: |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 46 | cpp_function() { } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 47 | |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 48 | /// Vanilla function pointers |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 49 | template <typename Return, typename... Args, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 50 | cpp_function(Return (*f)(Args...), const Extra&... extra) { |
| 51 | auto rec = new detail::function_record(); |
| 52 | rec->data = (void *) f; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 53 | |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 54 | typedef arg_value_caster<Args...> cast_in; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 55 | typedef return_value_caster<Return> cast_out; |
| 56 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 57 | /* Dispatch code which converts function arguments and performs the actual function call */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 58 | rec->impl = [](detail::function_record *rec, handle pyArgs, handle parent) -> handle { |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 59 | cast_in args; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 60 | |
| 61 | /* Try to cast the function arguments into the C++ domain */ |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 62 | if (!args.load(pyArgs, true)) |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 63 | return PYBIND11_TRY_NEXT_OVERLOAD; |
| 64 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 65 | /* Invoke call policy pre-call hook */ |
| 66 | detail::process_attributes<Extra...>::precall(pyArgs); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 67 | |
| 68 | /* Do the call and convert the return value back into the Python domain */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 69 | handle result = cast_out::cast( |
| 70 | args.template call<Return>((Return (*) (Args...)) rec->data), |
| 71 | rec->policy, parent); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 72 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 73 | /* Invoke call policy post-call hook */ |
| 74 | detail::process_attributes<Extra...>::postcall(pyArgs, result); |
| 75 | |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 76 | return result; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 77 | }; |
| 78 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 79 | /* Process any user-provided function attributes */ |
| 80 | detail::process_attributes<Extra...>::init(extra..., rec); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 81 | |
| 82 | /* Generate a readable signature describing the function's arguments and return value types */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 83 | using detail::descr; |
Sylvain Corlay | 4c7bf9b | 2016-03-08 18:44:04 -0500 | [diff] [blame] | 84 | PYBIND11_DESCR signature = cast_in::name() + detail::_(" -> ") + cast_out::name(); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 85 | |
| 86 | /* Register the function with Python from generic (non-templated) code */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 87 | initialize(rec, signature.text(), signature.types(), sizeof...(Args)); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 88 | } |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 89 | |
| 90 | /// Delegating helper constructor to deal with lambda functions |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 91 | template <typename Func, typename... Extra> cpp_function(Func &&f, const Extra&... extra) { |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 92 | initialize(std::forward<Func>(f), |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 93 | (typename detail::remove_class<decltype( |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 94 | &std::remove_reference<Func>::type::operator())>::type *) nullptr, extra...); |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 97 | /// Delegating helper constructor to deal with class methods (non-const) |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 98 | template <typename Return, typename Class, typename... Arg, typename... Extra> cpp_function( |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 99 | Return (Class::*f)(Arg...), const Extra&... extra) { |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 100 | initialize([f](Class *c, Arg... args) -> Return { return (c->*f)(args...); }, |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 101 | (Return (*) (Class *, Arg...)) nullptr, extra...); |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 104 | /// Delegating helper constructor to deal with class methods (const) |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 105 | template <typename Return, typename Class, typename... Arg, typename... Extra> cpp_function( |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 106 | Return (Class::*f)(Arg...) const, const Extra&... extra) { |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 107 | initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(args...); }, |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 108 | (Return (*)(const Class *, Arg ...)) nullptr, extra...); |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 111 | /// Return the function name |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 112 | object name() const { return attr("__name__"); } |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 113 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 114 | protected: |
| 115 | /// Special internal constructor for functors, lambda functions, etc. |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 116 | template <typename Func, typename Return, typename... Args, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 117 | void initialize(Func &&f, Return (*)(Args...), const Extra&... extra) { |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 118 | struct capture { typename std::remove_reference<Func>::type f; }; |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 119 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 120 | /* Store the function including any extra state it might have (e.g. a lambda capture object) */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 121 | auto rec = new detail::function_record(); |
| 122 | rec->data = new capture { std::forward<Func>(f) }; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 123 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 124 | /* Create a cleanup handler, but only if we have to (less generated code) */ |
Wenzel Jakob | 19208fe | 2015-10-13 17:37:25 +0200 | [diff] [blame] | 125 | if (!std::is_trivially_destructible<Func>::value) |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 126 | rec->free_data = [](void *ptr) { delete (capture *) ptr; }; |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 127 | else |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 128 | rec->free_data = operator delete; |
Wenzel Jakob | 19208fe | 2015-10-13 17:37:25 +0200 | [diff] [blame] | 129 | |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 130 | typedef arg_value_caster<Args...> cast_in; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 131 | typedef return_value_caster<Return> cast_out; |
| 132 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 133 | /* Dispatch code which converts function arguments and performs the actual function call */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 134 | rec->impl = [](detail::function_record *rec, handle pyArgs, handle parent) -> handle { |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 135 | cast_in args; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 136 | |
| 137 | /* Try to cast the function arguments into the C++ domain */ |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 138 | if (!args.load(pyArgs, true)) |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 139 | return PYBIND11_TRY_NEXT_OVERLOAD; |
| 140 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 141 | /* Invoke call policy pre-call hook */ |
| 142 | detail::process_attributes<Extra...>::precall(pyArgs); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 143 | |
| 144 | /* Do the call and convert the return value back into the Python domain */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 145 | handle result = cast_out::cast( |
| 146 | args.template call<Return>(((capture *) rec->data)->f), |
| 147 | rec->policy, parent); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 148 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 149 | /* Invoke call policy post-call hook */ |
| 150 | detail::process_attributes<Extra...>::postcall(pyArgs, result); |
| 151 | |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 152 | return result; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 153 | }; |
| 154 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 155 | /* Process any user-provided function attributes */ |
| 156 | detail::process_attributes<Extra...>::init(extra..., rec); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 157 | |
| 158 | /* Generate a readable signature describing the function's arguments and return value types */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 159 | using detail::descr; |
Sylvain Corlay | 4c7bf9b | 2016-03-08 18:44:04 -0500 | [diff] [blame] | 160 | PYBIND11_DESCR signature = cast_in::name() + detail::_(" -> ") + cast_out::name(); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 161 | |
| 162 | /* Register the function with Python from generic (non-templated) code */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 163 | initialize(rec, signature.text(), signature.types(), sizeof...(Args)); |
Wenzel Jakob | 281aa0e | 2015-07-30 15:29:00 +0200 | [diff] [blame] | 164 | } |
| 165 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 166 | /// Register a function call with Python (generic non-templated code goes here) |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 167 | void initialize(detail::function_record *rec, const char *text, |
| 168 | const std::type_info *const *types, int args) { |
| 169 | |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 170 | /* Create copies of all referenced C-style strings */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 171 | rec->name = strdup(rec->name ? rec->name : ""); |
| 172 | if (rec->doc) rec->doc = strdup(rec->doc); |
| 173 | for (auto &a: rec->args) { |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 174 | if (a.name) |
| 175 | a.name = strdup(a.name); |
| 176 | if (a.descr) |
| 177 | a.descr = strdup(a.descr); |
| 178 | else if (a.value) |
Wenzel Jakob | 27e8e10 | 2016-01-17 22:36:37 +0100 | [diff] [blame] | 179 | a.descr = strdup(((std::string) ((object) handle(a.value).attr("__repr__")).call().str()).c_str()); |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 180 | } |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 181 | auto const ®istered_types = detail::get_internals().registered_types_cpp; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 182 | |
| 183 | /* Generate a proper function signature */ |
| 184 | std::string signature; |
| 185 | size_t type_depth = 0, char_index = 0, type_index = 0, arg_index = 0; |
| 186 | while (true) { |
| 187 | char c = text[char_index++]; |
| 188 | if (c == '\0') |
| 189 | break; |
| 190 | |
| 191 | if (c == '{') { |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 192 | if (type_depth == 1 && arg_index < rec->args.size()) { |
| 193 | signature += rec->args[arg_index].name; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 194 | signature += " : "; |
| 195 | } |
| 196 | ++type_depth; |
| 197 | } else if (c == '}') { |
| 198 | --type_depth; |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 199 | if (type_depth == 1 && arg_index < rec->args.size()) { |
| 200 | if (rec->args[arg_index].descr) { |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 201 | signature += " = "; |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 202 | signature += rec->args[arg_index].descr; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 203 | } |
| 204 | arg_index++; |
| 205 | } |
| 206 | } else if (c == '%') { |
| 207 | const std::type_info *t = types[type_index++]; |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 208 | if (!t) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 209 | pybind11_fail("Internal error while parsing type signature (1)"); |
Wenzel Jakob | b6cf75d | 2016-01-29 11:39:32 +0100 | [diff] [blame] | 210 | auto it = registered_types.find(std::type_index(*t)); |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 211 | if (it != registered_types.end()) { |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 212 | signature += ((const detail::type_info *) it->second)->type->tp_name; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 213 | } else { |
| 214 | std::string tname(t->name()); |
| 215 | detail::clean_type_id(tname); |
| 216 | signature += tname; |
| 217 | } |
| 218 | } else { |
| 219 | signature += c; |
| 220 | } |
| 221 | } |
Wenzel Jakob | 95d1869 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 222 | if (type_depth != 0 || types[type_index] != nullptr) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 223 | pybind11_fail("Internal error while parsing type signature (2)"); |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 224 | |
| 225 | #if !defined(PYBIND11_CPP14) |
| 226 | delete[] types; |
| 227 | delete[] text; |
| 228 | #endif |
Wenzel Jakob | f5fae92 | 2015-08-24 15:31:24 +0200 | [diff] [blame] | 229 | |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 230 | #if PY_MAJOR_VERSION < 3 |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 231 | if (strcmp(rec->name, "__next__") == 0) { |
| 232 | std::free(rec->name); |
| 233 | rec->name = strdup("next"); |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 234 | } |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 235 | #endif |
| 236 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 237 | if (!rec->args.empty() && (int) rec->args.size() != args) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 238 | pybind11_fail( |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 239 | "cpp_function(): function \"" + std::string(rec->name) + "\" takes " + |
| 240 | std::to_string(args) + " arguments, but " + std::to_string(rec->args.size()) + |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 241 | " pybind11::arg entries were specified!"); |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 242 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 243 | rec->is_constructor = !strcmp(rec->name, "__init__"); |
| 244 | rec->signature = strdup(signature.c_str()); |
| 245 | rec->args.shrink_to_fit(); |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 246 | |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 247 | #if PY_MAJOR_VERSION < 3 |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 248 | if (rec->sibling && PyMethod_Check(rec->sibling.ptr())) |
| 249 | rec->sibling = PyMethod_GET_FUNCTION(rec->sibling.ptr()); |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 250 | #endif |
| 251 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 252 | detail::function_record *chain = nullptr, *chain_start = rec; |
| 253 | if (rec->sibling && PyCFunction_Check(rec->sibling.ptr())) { |
| 254 | capsule rec_capsule(PyCFunction_GetSelf(rec->sibling.ptr()), true); |
| 255 | chain = (detail::function_record *) rec_capsule; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 256 | /* Never append a method to an overload chain of a parent class; |
| 257 | instead, hide the parent's overloads in this case */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 258 | if (chain->class_ != rec->class_) |
| 259 | chain = nullptr; |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 260 | } |
| 261 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 262 | if (!chain) { |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 263 | /* No existing overload was found, create a new function object */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 264 | rec->def = new PyMethodDef(); |
| 265 | memset(rec->def, 0, sizeof(PyMethodDef)); |
| 266 | rec->def->ml_name = rec->name; |
| 267 | rec->def->ml_meth = reinterpret_cast<PyCFunction>(*dispatcher); |
| 268 | rec->def->ml_flags = METH_VARARGS | METH_KEYWORDS; |
Wenzel Jakob | a650179 | 2016-02-04 23:02:07 +0100 | [diff] [blame] | 269 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 270 | capsule rec_capsule(rec, [](PyObject *o) { |
| 271 | destruct((detail::function_record *) PyCapsule_GetPointer(o, nullptr)); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 272 | }); |
Wenzel Jakob | a650179 | 2016-02-04 23:02:07 +0100 | [diff] [blame] | 273 | |
| 274 | object scope_module; |
| 275 | if (rec->scope) { |
| 276 | scope_module = (object) rec->scope.attr("__module__"); |
| 277 | if (!scope_module) |
| 278 | scope_module = (object) rec->scope.attr("__name__"); |
| 279 | } |
| 280 | |
| 281 | m_ptr = PyCFunction_NewEx(rec->def, rec_capsule.ptr(), scope_module.ptr()); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 282 | if (!m_ptr) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 283 | pybind11_fail("cpp_function::cpp_function(): Could not allocate function object"); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 284 | } else { |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 285 | /* Append at the end of the overload chain */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 286 | m_ptr = rec->sibling.ptr(); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 287 | inc_ref(); |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 288 | chain_start = chain; |
| 289 | while (chain->next) |
| 290 | chain = chain->next; |
| 291 | chain->next = rec; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 292 | } |
Wenzel Jakob | 281aa0e | 2015-07-30 15:29:00 +0200 | [diff] [blame] | 293 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 294 | std::string signatures; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 295 | int index = 0; |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 296 | /* Create a nice pydoc rec including all signatures and |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 297 | docstrings of the functions in the overload chain */ |
Sylvain Corlay | 0e04fdf | 2016-03-08 16:42:12 -0500 | [diff] [blame] | 298 | if (chain) { |
| 299 | // First a generic signature |
| 300 | signatures += rec->name; |
| 301 | signatures += "(*args, **kwargs)\n"; |
| 302 | signatures += "Overloaded function.\n\n"; |
| 303 | } |
| 304 | // Then specific overload signatures |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 305 | for (auto it = chain_start; it != nullptr; it = it->next) { |
| 306 | if (chain) |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 307 | signatures += std::to_string(++index) + ". "; |
Sylvain Corlay | 13b22bf | 2016-02-28 21:23:39 -0500 | [diff] [blame] | 308 | signatures += rec->name; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 309 | signatures += it->signature; |
| 310 | signatures += "\n"; |
| 311 | if (it->doc && strlen(it->doc) > 0) { |
| 312 | signatures += "\n"; |
Wenzel Jakob | 218b6ce | 2016-02-28 23:52:37 +0100 | [diff] [blame] | 313 | signatures += it->doc; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 314 | signatures += "\n"; |
| 315 | } |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 316 | if (it->next) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 317 | signatures += "\n"; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 318 | } |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 319 | |
| 320 | /* Install docstring */ |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 321 | PyCFunctionObject *func = (PyCFunctionObject *) m_ptr; |
| 322 | if (func->m_ml->ml_doc) |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 323 | std::free((char *) func->m_ml->ml_doc); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 324 | func->m_ml->ml_doc = strdup(signatures.c_str()); |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 325 | |
| 326 | if (rec->class_) { |
| 327 | m_ptr = PYBIND11_INSTANCE_METHOD_NEW(m_ptr, rec->class_.ptr()); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 328 | if (!m_ptr) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 329 | pybind11_fail("cpp_function::cpp_function(): Could not allocate instance method object"); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 330 | Py_DECREF(func); |
| 331 | } |
| 332 | } |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 333 | |
| 334 | /// When a cpp_function is GCed, release any memory allocated by pybind11 |
| 335 | static void destruct(detail::function_record *rec) { |
| 336 | while (rec) { |
| 337 | detail::function_record *next = rec->next; |
| 338 | if (rec->free_data) |
| 339 | rec->free_data(rec->data); |
| 340 | std::free((char *) rec->name); |
| 341 | std::free((char *) rec->doc); |
| 342 | std::free((char *) rec->signature); |
| 343 | for (auto &arg: rec->args) { |
| 344 | std::free((char *) arg.name); |
| 345 | std::free((char *) arg.descr); |
| 346 | arg.value.dec_ref(); |
| 347 | } |
| 348 | if (rec->def) { |
| 349 | std::free((char *) rec->def->ml_doc); |
| 350 | delete rec->def; |
| 351 | } |
| 352 | delete rec; |
| 353 | rec = next; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | /// Main dispatch logic for calls to functions bound using pybind11 |
| 358 | static PyObject *dispatcher(PyObject *self, PyObject *args, PyObject *kwargs) { |
| 359 | /* Iterator over the list of potentially admissible overloads */ |
| 360 | detail::function_record *overloads = (detail::function_record *) PyCapsule_GetPointer(self, nullptr), |
| 361 | *it = overloads; |
| 362 | |
| 363 | /* Need to know how many arguments + keyword arguments there are to pick the right overload */ |
| 364 | int nargs = (int) PyTuple_Size(args), |
| 365 | nkwargs = kwargs ? (int) PyDict_Size(kwargs) : 0; |
| 366 | |
| 367 | handle parent = nargs > 0 ? PyTuple_GetItem(args, 0) : nullptr, |
| 368 | result = PYBIND11_TRY_NEXT_OVERLOAD; |
| 369 | try { |
| 370 | for (; it != nullptr; it = it->next) { |
| 371 | tuple args_(args, true); |
| 372 | int kwargs_consumed = 0; |
| 373 | |
| 374 | /* For each overload: |
| 375 | 1. If the required list of arguments is longer than the |
| 376 | actually provided amount, create a copy of the argument |
| 377 | list and fill in any available keyword/default arguments. |
| 378 | 2. Ensure that all keyword arguments were "consumed" |
| 379 | 3. Call the function call dispatcher (function_record::impl) |
| 380 | */ |
| 381 | |
| 382 | if (nargs < (int) it->args.size()) { |
| 383 | args_ = tuple(it->args.size()); |
| 384 | for (int i = 0; i < nargs; ++i) { |
| 385 | handle item = PyTuple_GET_ITEM(args, i); |
| 386 | PyTuple_SET_ITEM(args_.ptr(), i, item.inc_ref().ptr()); |
| 387 | } |
| 388 | |
| 389 | int arg_ctr = 0; |
| 390 | for (auto const &it2 : it->args) { |
| 391 | int index = arg_ctr++; |
| 392 | if (PyTuple_GET_ITEM(args_.ptr(), index)) |
| 393 | continue; |
| 394 | |
| 395 | handle value; |
| 396 | if (kwargs) |
| 397 | value = PyDict_GetItemString(kwargs, it2.name); |
| 398 | |
| 399 | if (value) |
| 400 | kwargs_consumed++; |
| 401 | else if (it2.value) |
| 402 | value = it2.value; |
| 403 | |
| 404 | if (value) { |
| 405 | PyTuple_SET_ITEM(args_.ptr(), index, value.inc_ref().ptr()); |
| 406 | } else { |
| 407 | kwargs_consumed = -1; /* definite failure */ |
| 408 | break; |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | if (kwargs_consumed == nkwargs) |
| 414 | result = it->impl(it, args_, parent); |
| 415 | |
| 416 | if (result.ptr() != PYBIND11_TRY_NEXT_OVERLOAD) |
| 417 | break; |
| 418 | } |
| 419 | } catch (const error_already_set &) { return nullptr; |
| 420 | } catch (const index_error &e) { PyErr_SetString(PyExc_IndexError, e.what()); return nullptr; |
| 421 | } catch (const stop_iteration &e) { PyErr_SetString(PyExc_StopIteration, e.what()); return nullptr; |
| 422 | } catch (const std::bad_alloc &e) { PyErr_SetString(PyExc_MemoryError, e.what()); return nullptr; |
| 423 | } catch (const std::domain_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return nullptr; |
| 424 | } catch (const std::invalid_argument &e) { PyErr_SetString(PyExc_ValueError, e.what()); return nullptr; |
| 425 | } catch (const std::length_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return nullptr; |
| 426 | } catch (const std::out_of_range &e) { PyErr_SetString(PyExc_IndexError, e.what()); return nullptr; |
| 427 | } catch (const std::range_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return nullptr; |
| 428 | } catch (const std::exception &e) { PyErr_SetString(PyExc_RuntimeError, e.what()); return nullptr; |
| 429 | } catch (...) { |
| 430 | PyErr_SetString(PyExc_RuntimeError, "Caught an unknown exception!"); |
| 431 | return nullptr; |
| 432 | } |
| 433 | |
| 434 | if (result.ptr() == PYBIND11_TRY_NEXT_OVERLOAD) { |
| 435 | std::string msg = "Incompatible function arguments. The " |
| 436 | "following argument types are supported:\n"; |
| 437 | int ctr = 0; |
| 438 | for (detail::function_record *it2 = overloads; it2 != nullptr; it2 = it2->next) { |
| 439 | msg += " "+ std::to_string(++ctr) + ". "; |
| 440 | msg += it2->signature; |
| 441 | msg += "\n"; |
| 442 | } |
| 443 | PyErr_SetString(PyExc_TypeError, msg.c_str()); |
| 444 | return nullptr; |
| 445 | } else if (!result) { |
| 446 | std::string msg = "Unable to convert function return value to a " |
| 447 | "Python type! The signature was\n\t"; |
| 448 | msg += it->signature; |
| 449 | PyErr_SetString(PyExc_TypeError, msg.c_str()); |
| 450 | return nullptr; |
| 451 | } else { |
| 452 | if (overloads->is_constructor) { |
| 453 | /* When a construtor ran successfully, the corresponding |
| 454 | holder type (e.g. std::unique_ptr) must still be initialized. */ |
| 455 | PyObject *inst = PyTuple_GetItem(args, 0); |
| 456 | auto tinfo = detail::get_type_info(Py_TYPE(inst)); |
| 457 | tinfo->init_holder(inst, nullptr); |
| 458 | } |
| 459 | return result.ptr(); |
| 460 | } |
| 461 | } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 462 | }; |
| 463 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 464 | /// Wrapper for Python extension modules |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 465 | class module : public object { |
| 466 | public: |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 467 | PYBIND11_OBJECT_DEFAULT(module, object, PyModule_Check) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 468 | |
| 469 | module(const char *name, const char *doc = nullptr) { |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 470 | #if PY_MAJOR_VERSION >= 3 |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 471 | PyModuleDef *def = new PyModuleDef(); |
| 472 | memset(def, 0, sizeof(PyModuleDef)); |
| 473 | def->m_name = name; |
| 474 | def->m_doc = doc; |
| 475 | def->m_size = -1; |
| 476 | Py_INCREF(def); |
| 477 | m_ptr = PyModule_Create(def); |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 478 | #else |
| 479 | m_ptr = Py_InitModule3(name, nullptr, doc); |
| 480 | #endif |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 481 | if (m_ptr == nullptr) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 482 | pybind11_fail("Internal error in module::module()"); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 483 | inc_ref(); |
| 484 | } |
| 485 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 486 | template <typename Func, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 487 | module &def(const char *name_, Func &&f, const Extra& ... extra) { |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 488 | cpp_function func(std::forward<Func>(f), name(name_), |
Wenzel Jakob | a650179 | 2016-02-04 23:02:07 +0100 | [diff] [blame] | 489 | sibling((handle) attr(name_)), scope(*this), extra...); |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 490 | /* PyModule_AddObject steals a reference to 'func' */ |
| 491 | PyModule_AddObject(ptr(), name_, func.inc_ref().ptr()); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 492 | return *this; |
| 493 | } |
| 494 | |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 495 | module def_submodule(const char *name, const char *doc = nullptr) { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 496 | std::string full_name = std::string(PyModule_GetName(m_ptr)) |
| 497 | + std::string(".") + std::string(name); |
| 498 | module result(PyImport_AddModule(full_name.c_str()), true); |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 499 | if (doc) |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 500 | result.attr("__doc__") = pybind11::str(doc); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 501 | attr(name) = result; |
| 502 | return result; |
| 503 | } |
Wenzel Jakob | db028d6 | 2015-10-13 23:44:25 +0200 | [diff] [blame] | 504 | |
| 505 | static module import(const char *name) { |
Wenzel Jakob | dd57a34 | 2015-12-26 14:04:52 +0100 | [diff] [blame] | 506 | PyObject *obj = PyImport_ImportModule(name); |
| 507 | if (!obj) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 508 | pybind11_fail("Module \"" + std::string(name) + "\" not found!"); |
Wenzel Jakob | dd57a34 | 2015-12-26 14:04:52 +0100 | [diff] [blame] | 509 | return module(obj, false); |
Wenzel Jakob | db028d6 | 2015-10-13 23:44:25 +0200 | [diff] [blame] | 510 | } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 511 | }; |
| 512 | |
| 513 | NAMESPACE_BEGIN(detail) |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 514 | /// Generic support for creating new Python heap types |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 515 | class generic_type : public object { |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 516 | template <typename type, typename holder_type> friend class class_; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 517 | public: |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 518 | PYBIND11_OBJECT_DEFAULT(generic_type, object, PyType_Check) |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 519 | protected: |
| 520 | void initialize(type_record *rec) { |
| 521 | if (rec->base_type) { |
| 522 | if (rec->base_handle) |
| 523 | pybind11_fail("generic_type: specified base type multiple times!"); |
| 524 | rec->base_handle = detail::get_type_handle(*(rec->base_type)); |
| 525 | if (!rec->base_handle) { |
| 526 | std::string tname(rec->base_type->name()); |
| 527 | detail::clean_type_id(tname); |
| 528 | pybind11_fail("generic_type: type \"" + std::string(rec->name) + |
| 529 | "\" referenced unknown base type \"" + tname + "\""); |
| 530 | } |
| 531 | } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 532 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 533 | object type_holder(PyType_Type.tp_alloc(&PyType_Type, 0), false); |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 534 | object name(PYBIND11_FROM_STRING(rec->name), false); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 535 | auto type = (PyHeapTypeObject*) type_holder.ptr(); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 536 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 537 | if (!type_holder || !name) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 538 | pybind11_fail("generic_type: unable to create type object!"); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 539 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 540 | /* Register supplemental type information in C++ dict */ |
| 541 | auto &internals = get_internals(); |
| 542 | detail::type_info *tinfo = new detail::type_info(); |
| 543 | tinfo->type = (PyTypeObject *) type; |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 544 | tinfo->type_size = rec->type_size; |
| 545 | tinfo->init_holder = rec->init_holder; |
Wenzel Jakob | b6cf75d | 2016-01-29 11:39:32 +0100 | [diff] [blame] | 546 | internals.registered_types_cpp[std::type_index(*(rec->type))] = tinfo; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 547 | internals.registered_types_py[type] = tinfo; |
| 548 | |
Wenzel Jakob | b282595 | 2016-04-13 23:33:00 +0200 | [diff] [blame] | 549 | object scope_module; |
| 550 | if (rec->scope) { |
| 551 | scope_module = (object) rec->scope.attr("__module__"); |
| 552 | if (!scope_module) |
| 553 | scope_module = (object) rec->scope.attr("__name__"); |
| 554 | } |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 555 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 556 | std::string full_name = (scope_module ? ((std::string) scope_module.str() + "." + rec->name) |
| 557 | : std::string(rec->name)); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 558 | /* Basic type attributes */ |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 559 | type->ht_type.tp_name = strdup(full_name.c_str()); |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 560 | type->ht_type.tp_basicsize = rec->instance_size; |
| 561 | type->ht_type.tp_base = (PyTypeObject *) rec->base_handle.ptr(); |
| 562 | rec->base_handle.inc_ref(); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 563 | |
| 564 | #if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3 |
| 565 | /* Qualified names for Python >= 3.3 */ |
Wenzel Jakob | b282595 | 2016-04-13 23:33:00 +0200 | [diff] [blame] | 566 | object scope_qualname; |
| 567 | if (rec->scope) |
| 568 | scope_qualname = (object) rec->scope.attr("__qualname__"); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 569 | if (scope_qualname) { |
| 570 | type->ht_qualname = PyUnicode_FromFormat( |
| 571 | "%U.%U", scope_qualname.ptr(), name.ptr()); |
| 572 | } else { |
| 573 | type->ht_qualname = name.ptr(); |
| 574 | name.inc_ref(); |
| 575 | } |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 576 | #endif |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 577 | type->ht_name = name.release().ptr(); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 578 | |
| 579 | /* Supported protocols */ |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 580 | type->ht_type.tp_as_number = &type->as_number; |
| 581 | type->ht_type.tp_as_sequence = &type->as_sequence; |
| 582 | type->ht_type.tp_as_mapping = &type->as_mapping; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 583 | |
| 584 | /* Supported elementary operations */ |
| 585 | type->ht_type.tp_init = (initproc) init; |
| 586 | type->ht_type.tp_new = (newfunc) new_instance; |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 587 | type->ht_type.tp_dealloc = rec->dealloc; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 588 | |
| 589 | /* Support weak references (needed for the keep_alive feature) */ |
Wenzel Jakob | 88d1d04 | 2016-01-20 01:26:42 +0100 | [diff] [blame] | 590 | type->ht_type.tp_weaklistoffset = offsetof(instance_essentials<void>, weakrefs); |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 591 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 592 | /* Flags */ |
| 593 | type->ht_type.tp_flags |= Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE; |
| 594 | #if PY_MAJOR_VERSION < 3 |
| 595 | type->ht_type.tp_flags |= Py_TPFLAGS_CHECKTYPES; |
| 596 | #endif |
| 597 | type->ht_type.tp_flags &= ~Py_TPFLAGS_HAVE_GC; |
| 598 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 599 | if (rec->doc) { |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 600 | /* Allocate memory for docstring (using PyObject_MALLOC, since |
| 601 | Python will free this later on) */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 602 | size_t size = strlen(rec->doc) + 1; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 603 | type->ht_type.tp_doc = (char *) PyObject_MALLOC(size); |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 604 | memcpy((void *) type->ht_type.tp_doc, rec->doc, size); |
Wenzel Jakob | 5116b02 | 2015-09-05 02:09:17 +0200 | [diff] [blame] | 605 | } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 606 | |
| 607 | if (PyType_Ready(&type->ht_type) < 0) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 608 | pybind11_fail("generic_type: PyType_Ready failed!"); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 609 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 610 | m_ptr = type_holder.ptr(); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 611 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 612 | if (scope_module) // Needed by pydoc |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 613 | attr("__module__") = scope_module; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 614 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 615 | /* Register type with the parent scope */ |
Wenzel Jakob | b282595 | 2016-04-13 23:33:00 +0200 | [diff] [blame] | 616 | if (rec->scope) |
| 617 | rec->scope.attr(handle(type->ht_name)) = *this; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 618 | |
| 619 | type_holder.release(); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 620 | } |
| 621 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 622 | /// Allocate a metaclass on demand (for static properties) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 623 | handle metaclass() { |
| 624 | auto &ht_type = ((PyHeapTypeObject *) m_ptr)->ht_type; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 625 | auto &ob_type = PYBIND11_OB_TYPE(ht_type); |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 626 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 627 | if (ob_type == &PyType_Type) { |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 628 | std::string name_ = std::string(ht_type.tp_name) + "__Meta"; |
| 629 | object type_holder(PyType_Type.tp_alloc(&PyType_Type, 0), false); |
| 630 | object name(PYBIND11_FROM_STRING(name_.c_str()), false); |
| 631 | if (!type_holder || !name) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 632 | pybind11_fail("generic_type::metaclass(): unable to create type object!"); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 633 | |
| 634 | auto type = (PyHeapTypeObject*) type_holder.ptr(); |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 635 | type->ht_name = name.release().ptr(); |
| 636 | #if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3 |
| 637 | /* Qualified names for Python >= 3.3 */ |
| 638 | type->ht_qualname = PyUnicode_FromFormat( |
| 639 | "%U__Meta", ((object) attr("__qualname__")).ptr()); |
| 640 | #endif |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 641 | type->ht_type.tp_name = strdup(name_.c_str()); |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 642 | type->ht_type.tp_base = ob_type; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 643 | type->ht_type.tp_flags |= (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE) & |
| 644 | ~Py_TPFLAGS_HAVE_GC; |
| 645 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 646 | if (PyType_Ready(&type->ht_type) < 0) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 647 | pybind11_fail("generic_type::metaclass(): PyType_Ready failed!"); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 648 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 649 | ob_type = (PyTypeObject *) type_holder.release().ptr(); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 650 | } |
| 651 | return handle((PyObject *) ob_type); |
| 652 | } |
| 653 | |
| 654 | static int init(void *self, PyObject *, PyObject *) { |
| 655 | std::string msg = std::string(Py_TYPE(self)->tp_name) + ": No constructor defined!"; |
| 656 | PyErr_SetString(PyExc_TypeError, msg.c_str()); |
| 657 | return -1; |
| 658 | } |
| 659 | |
| 660 | static PyObject *new_instance(PyTypeObject *type, PyObject *, PyObject *) { |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 661 | instance<void> *self = (instance<void> *) PyType_GenericAlloc((PyTypeObject *) type, 0); |
| 662 | auto tinfo = detail::get_type_info(type); |
| 663 | self->value = ::operator new(tinfo->type_size); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 664 | self->owned = true; |
| 665 | self->parent = nullptr; |
| 666 | self->constructed = false; |
| 667 | detail::get_internals().registered_instances[self->value] = (PyObject *) self; |
| 668 | return (PyObject *) self; |
| 669 | } |
| 670 | |
| 671 | static void dealloc(instance<void> *self) { |
| 672 | if (self->value) { |
| 673 | bool dont_cache = self->parent && ((instance<void> *) self->parent)->value == self->value; |
| 674 | if (!dont_cache) { // avoid an issue with internal references matching their parent's address |
| 675 | auto ®istered_instances = detail::get_internals().registered_instances; |
| 676 | auto it = registered_instances.find(self->value); |
| 677 | if (it == registered_instances.end()) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 678 | pybind11_fail("generic_type::dealloc(): Tried to deallocate unregistered instance!"); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 679 | registered_instances.erase(it); |
| 680 | } |
| 681 | Py_XDECREF(self->parent); |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 682 | if (self->weakrefs) |
| 683 | PyObject_ClearWeakRefs((PyObject *) self); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 684 | } |
| 685 | Py_TYPE(self)->tp_free((PyObject*) self); |
| 686 | } |
| 687 | |
Wenzel Jakob | 43398a8 | 2015-07-28 16:12:20 +0200 | [diff] [blame] | 688 | void install_buffer_funcs( |
| 689 | buffer_info *(*get_buffer)(PyObject *, void *), |
| 690 | void *get_buffer_data) { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 691 | PyHeapTypeObject *type = (PyHeapTypeObject*) m_ptr; |
| 692 | type->ht_type.tp_as_buffer = &type->as_buffer; |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 693 | #if PY_MAJOR_VERSION < 3 |
| 694 | type->ht_type.tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER; |
| 695 | #endif |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 696 | type->as_buffer.bf_getbuffer = getbuffer; |
| 697 | type->as_buffer.bf_releasebuffer = releasebuffer; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 698 | auto tinfo = detail::get_type_info(&type->ht_type); |
| 699 | tinfo->get_buffer = get_buffer; |
| 700 | tinfo->get_buffer_data = get_buffer_data; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | static int getbuffer(PyObject *obj, Py_buffer *view, int flags) { |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 704 | auto tinfo = detail::get_type_info(Py_TYPE(obj)); |
| 705 | if (view == nullptr || obj == nullptr || !tinfo || !tinfo->get_buffer) { |
| 706 | PyErr_SetString(PyExc_BufferError, "generic_type::getbuffer(): Internal error"); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 707 | return -1; |
| 708 | } |
| 709 | memset(view, 0, sizeof(Py_buffer)); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 710 | buffer_info *info = tinfo->get_buffer(obj, tinfo->get_buffer_data); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 711 | view->obj = obj; |
| 712 | view->ndim = 1; |
| 713 | view->internal = info; |
| 714 | view->buf = info->ptr; |
| 715 | view->itemsize = info->itemsize; |
| 716 | view->len = view->itemsize; |
| 717 | for (auto s : info->shape) |
| 718 | view->len *= s; |
| 719 | if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) |
| 720 | view->format = const_cast<char *>(info->format.c_str()); |
| 721 | if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) { |
| 722 | view->ndim = info->ndim; |
Wenzel Jakob | 27e8e10 | 2016-01-17 22:36:37 +0100 | [diff] [blame] | 723 | view->strides = (ssize_t *) &info->strides[0]; |
| 724 | view->shape = (ssize_t *) &info->shape[0]; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 725 | } |
| 726 | Py_INCREF(view->obj); |
| 727 | return 0; |
| 728 | } |
| 729 | |
| 730 | static void releasebuffer(PyObject *, Py_buffer *view) { delete (buffer_info *) view->internal; } |
| 731 | }; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 732 | NAMESPACE_END(detail) |
| 733 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 734 | template <typename type, typename holder_type = std::unique_ptr<type>> |
| 735 | class class_ : public detail::generic_type { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 736 | public: |
| 737 | typedef detail::instance<type, holder_type> instance_type; |
| 738 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 739 | PYBIND11_OBJECT(class_, detail::generic_type, PyType_Check) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 740 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 741 | template <typename... Extra> |
| 742 | class_(handle scope, const char *name, const Extra &... extra) { |
| 743 | detail::type_record record; |
| 744 | record.scope = scope; |
| 745 | record.name = name; |
| 746 | record.type = &typeid(type); |
| 747 | record.type_size = sizeof(type); |
| 748 | record.instance_size = sizeof(instance_type); |
| 749 | record.init_holder = init_holder; |
| 750 | record.dealloc = dealloc; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 751 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 752 | /* Process optional arguments, if any */ |
| 753 | detail::process_attributes<Extra...>::init(extra..., &record); |
| 754 | |
| 755 | detail::generic_type::initialize(&record); |
| 756 | } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 757 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 758 | template <typename Func, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 759 | class_ &def(const char *name_, Func&& f, const Extra&... extra) { |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 760 | cpp_function cf(std::forward<Func>(f), name(name_), |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 761 | sibling(attr(name_)), is_method(*this), |
| 762 | extra...); |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 763 | attr(cf.name()) = cf; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 764 | return *this; |
| 765 | } |
| 766 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 767 | template <typename Func, typename... Extra> class_ & |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 768 | def_static(const char *name_, Func f, const Extra&... extra) { |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 769 | cpp_function cf(std::forward<Func>(f), name(name_), |
Wenzel Jakob | a650179 | 2016-02-04 23:02:07 +0100 | [diff] [blame] | 770 | sibling(attr(name_)), scope(*this), extra...); |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 771 | attr(cf.name()) = cf; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 772 | return *this; |
| 773 | } |
| 774 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 775 | template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 776 | class_ &def(const detail::op_<id, ot, L, R> &op, const Extra&... extra) { |
Wenzel Jakob | e206564 | 2016-02-04 23:29:29 +0100 | [diff] [blame] | 777 | op.template execute<type>(*this, extra...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 778 | return *this; |
| 779 | } |
| 780 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 781 | template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 782 | class_ & def_cast(const detail::op_<id, ot, L, R> &op, const Extra&... extra) { |
Wenzel Jakob | e206564 | 2016-02-04 23:29:29 +0100 | [diff] [blame] | 783 | op.template execute_cast<type>(*this, extra...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 784 | return *this; |
| 785 | } |
| 786 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 787 | template <typename... Args, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 788 | class_ &def(const detail::init<Args...> &init, const Extra&... extra) { |
Wenzel Jakob | e206564 | 2016-02-04 23:29:29 +0100 | [diff] [blame] | 789 | init.template execute<type>(*this, extra...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 790 | return *this; |
| 791 | } |
| 792 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 793 | template <typename Func> class_& def_buffer(Func &&func) { |
Wenzel Jakob | 43398a8 | 2015-07-28 16:12:20 +0200 | [diff] [blame] | 794 | struct capture { Func func; }; |
| 795 | capture *ptr = new capture { std::forward<Func>(func) }; |
| 796 | install_buffer_funcs([](PyObject *obj, void *ptr) -> buffer_info* { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 797 | detail::type_caster<type> caster; |
| 798 | if (!caster.load(obj, false)) |
| 799 | return nullptr; |
Wenzel Jakob | 43398a8 | 2015-07-28 16:12:20 +0200 | [diff] [blame] | 800 | return new buffer_info(((capture *) ptr)->func(caster)); |
| 801 | }, ptr); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 802 | return *this; |
| 803 | } |
| 804 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 805 | template <typename C, typename D, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 806 | class_ &def_readwrite(const char *name, D C::*pm, const Extra&... extra) { |
Wenzel Jakob | 0b48958 | 2016-03-25 16:13:10 +0100 | [diff] [blame] | 807 | cpp_function fget([pm](const C &c) -> const D &{ return c.*pm; }, is_method(*this)), |
| 808 | fset([pm](C &c, const D &value) { c.*pm = value; }, is_method(*this)); |
| 809 | def_property(name, fget, fset, return_value_policy::reference_internal, extra...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 810 | return *this; |
| 811 | } |
| 812 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 813 | template <typename C, typename D, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 814 | class_ &def_readonly(const char *name, const D C::*pm, const Extra& ...extra) { |
Wenzel Jakob | 0b48958 | 2016-03-25 16:13:10 +0100 | [diff] [blame] | 815 | cpp_function fget([pm](const C &c) -> const D &{ return c.*pm; }, is_method(*this)); |
| 816 | def_property_readonly(name, fget, return_value_policy::reference_internal, extra...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 817 | return *this; |
| 818 | } |
| 819 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 820 | template <typename D, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 821 | class_ &def_readwrite_static(const char *name, D *pm, const Extra& ...extra) { |
Wenzel Jakob | 0b48958 | 2016-03-25 16:13:10 +0100 | [diff] [blame] | 822 | cpp_function fget([pm](object) -> const D &{ return *pm; }, scope(*this)), |
| 823 | fset([pm](object, const D &value) { *pm = value; }, scope(*this)); |
| 824 | def_property_static(name, fget, fset, return_value_policy::reference, extra...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 825 | return *this; |
| 826 | } |
| 827 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 828 | template <typename D, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 829 | class_ &def_readonly_static(const char *name, const D *pm, const Extra& ...extra) { |
Wenzel Jakob | 0b48958 | 2016-03-25 16:13:10 +0100 | [diff] [blame] | 830 | cpp_function fget([pm](object) -> const D &{ return *pm; }, scope(*this)); |
| 831 | def_property_readonly_static(name, fget, return_value_policy::reference, extra...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 832 | return *this; |
| 833 | } |
| 834 | |
Wenzel Jakob | 84ec78f | 2016-03-21 17:54:24 +0100 | [diff] [blame] | 835 | template <typename... Extra> |
| 836 | class_ &def_property_readonly(const char *name, const cpp_function &fget, const Extra& ...extra) { |
| 837 | def_property(name, fget, cpp_function(), extra...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 838 | return *this; |
| 839 | } |
| 840 | |
Wenzel Jakob | 84ec78f | 2016-03-21 17:54:24 +0100 | [diff] [blame] | 841 | template <typename... Extra> |
| 842 | class_ &def_property_readonly_static(const char *name, const cpp_function &fget, const Extra& ...extra) { |
| 843 | def_property_static(name, fget, cpp_function(), extra...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 844 | return *this; |
| 845 | } |
| 846 | |
Wenzel Jakob | 84ec78f | 2016-03-21 17:54:24 +0100 | [diff] [blame] | 847 | template <typename... Extra> |
| 848 | class_ &def_property(const char *name, const cpp_function &fget, const cpp_function &fset, const Extra& ...extra) { |
Wenzel Jakob | 0b48958 | 2016-03-25 16:13:10 +0100 | [diff] [blame] | 849 | return def_property_static(name, fget, fset, is_method(*this), extra...); |
Wenzel Jakob | 84ec78f | 2016-03-21 17:54:24 +0100 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | template <typename... Extra> |
| 853 | class_ &def_property_static(const char *name, const cpp_function &fget, const cpp_function &fset, const Extra& ...extra) { |
| 854 | auto rec_fget = get_function_record(fget), rec_fset = get_function_record(fset); |
| 855 | detail::process_attributes<Extra...>::init(extra..., rec_fget); |
| 856 | if (rec_fset) |
| 857 | detail::process_attributes<Extra...>::init(extra..., rec_fset); |
| 858 | pybind11::str doc_obj = pybind11::str(rec_fget->doc ? rec_fget->doc : ""); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 859 | object property( |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 860 | PyObject_CallFunctionObjArgs((PyObject *) &PyProperty_Type, fget.ptr() ? fget.ptr() : Py_None, |
| 861 | fset.ptr() ? fset.ptr() : Py_None, Py_None, doc_obj.ptr(), nullptr), false); |
Wenzel Jakob | 84ec78f | 2016-03-21 17:54:24 +0100 | [diff] [blame] | 862 | if (rec_fget->class_) |
| 863 | attr(name) = property; |
| 864 | else |
| 865 | metaclass().attr(name) = property; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 866 | return *this; |
| 867 | } |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 868 | |
| 869 | template <typename target> class_ alias() { |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 870 | auto &instances = pybind11::detail::get_internals().registered_types_cpp; |
Wenzel Jakob | b6cf75d | 2016-01-29 11:39:32 +0100 | [diff] [blame] | 871 | instances[std::type_index(typeid(target))] = instances[std::type_index(typeid(type))]; |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 872 | return *this; |
| 873 | } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 874 | private: |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 875 | /// Initialize holder object, variant 1: object derives from enable_shared_from_this |
| 876 | template <typename T> |
| 877 | static void init_holder_helper(instance_type *inst, const holder_type * /* unused */, const std::enable_shared_from_this<T> * /* dummy */) { |
Wenzel Jakob | 6e213c9 | 2015-11-24 23:05:58 +0100 | [diff] [blame] | 878 | try { |
Wenzel Jakob | 309a85b | 2016-03-08 17:59:10 +0100 | [diff] [blame] | 879 | new (&inst->holder) holder_type(std::static_pointer_cast<type>(inst->value->shared_from_this())); |
Wenzel Jakob | 6e213c9 | 2015-11-24 23:05:58 +0100 | [diff] [blame] | 880 | } catch (const std::bad_weak_ptr &) { |
| 881 | new (&inst->holder) holder_type(inst->value); |
| 882 | } |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | /// Initialize holder object, variant 2: try to construct from existing holder object, if possible |
| 886 | template <typename T = holder_type, |
| 887 | typename std::enable_if<std::is_copy_constructible<T>::value, int>::type = 0> |
| 888 | static void init_holder_helper(instance_type *inst, const holder_type *holder_ptr, const void * /* dummy */) { |
| 889 | if (holder_ptr) |
| 890 | new (&inst->holder) holder_type(*holder_ptr); |
| 891 | else |
| 892 | new (&inst->holder) holder_type(inst->value); |
| 893 | } |
| 894 | |
| 895 | /// Initialize holder object, variant 3: holder is not copy constructible (e.g. unique_ptr), always initialize from raw pointer |
| 896 | template <typename T = holder_type, |
| 897 | typename std::enable_if<!std::is_copy_constructible<T>::value, int>::type = 0> |
| 898 | static void init_holder_helper(instance_type *inst, const holder_type * /* unused */, const void * /* dummy */) { |
| 899 | new (&inst->holder) holder_type(inst->value); |
| 900 | } |
| 901 | |
| 902 | /// Initialize holder object of an instance, possibly given a pointer to an existing holder |
| 903 | static void init_holder(PyObject *inst_, const void *holder_ptr) { |
| 904 | auto inst = (instance_type *) inst_; |
| 905 | init_holder_helper(inst, (const holder_type *) holder_ptr, inst->value); |
Wenzel Jakob | 6e213c9 | 2015-11-24 23:05:58 +0100 | [diff] [blame] | 906 | inst->constructed = true; |
| 907 | } |
| 908 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 909 | static void dealloc(PyObject *inst_) { |
| 910 | instance_type *inst = (instance_type *) inst_; |
| 911 | if (inst->owned) { |
| 912 | if (inst->constructed) |
| 913 | inst->holder.~holder_type(); |
| 914 | else |
| 915 | ::operator delete(inst->value); |
| 916 | } |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 917 | generic_type::dealloc((detail::instance<void> *) inst); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 918 | } |
Wenzel Jakob | 84ec78f | 2016-03-21 17:54:24 +0100 | [diff] [blame] | 919 | |
| 920 | static detail::function_record *get_function_record(handle h) { |
| 921 | h = detail::get_function(h); |
| 922 | return h ? (detail::function_record *) capsule( |
| 923 | PyCFunction_GetSelf(h.ptr()), true) : nullptr; |
| 924 | } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 925 | }; |
| 926 | |
| 927 | /// Binds C++ enumerations and enumeration classes to Python |
| 928 | template <typename Type> class enum_ : public class_<Type> { |
| 929 | public: |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 930 | template <typename... Extra> |
| 931 | enum_(const handle &scope, const char *name, const Extra&... extra) |
| 932 | : class_<Type>(scope, name, extra...), m_parent(scope) { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 933 | auto entries = new std::unordered_map<int, const char *>(); |
Wenzel Jakob | 8456a4d | 2015-10-13 02:42:20 +0200 | [diff] [blame] | 934 | this->def("__repr__", [name, entries](Type value) -> std::string { |
Wenzel Jakob | a750031 | 2015-08-29 02:08:32 +0200 | [diff] [blame] | 935 | auto it = entries->find((int) value); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 936 | return std::string(name) + "." + |
| 937 | ((it == entries->end()) ? std::string("???") |
| 938 | : std::string(it->second)); |
| 939 | }); |
Wenzel Jakob | 15f6a00 | 2016-01-24 14:05:12 +0100 | [diff] [blame] | 940 | this->def("__init__", [](Type& value, int i) { value = (Type) i; }); |
Wenzel Jakob | 6918922 | 2015-10-01 21:32:23 +0200 | [diff] [blame] | 941 | this->def("__int__", [](Type value) { return (int) value; }); |
Wenzel Jakob | 15f6a00 | 2016-01-24 14:05:12 +0100 | [diff] [blame] | 942 | this->def("__eq__", [](const Type &value, Type value2) { return value == value2; }); |
| 943 | this->def("__ne__", [](const Type &value, Type value2) { return value != value2; }); |
| 944 | this->def("__hash__", [](const Type &value) { return (int) value; }); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 945 | m_entries = entries; |
| 946 | } |
| 947 | |
| 948 | /// Export enumeration entries into the parent scope |
| 949 | void export_values() { |
| 950 | PyObject *dict = ((PyTypeObject *) this->m_ptr)->tp_dict; |
| 951 | PyObject *key, *value; |
Wenzel Jakob | 27e8e10 | 2016-01-17 22:36:37 +0100 | [diff] [blame] | 952 | ssize_t pos = 0; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 953 | while (PyDict_Next(dict, &pos, &key, &value)) |
| 954 | if (PyObject_IsInstance(value, this->m_ptr)) |
| 955 | m_parent.attr(key) = value; |
| 956 | } |
| 957 | |
| 958 | /// Add an enumeration entry |
| 959 | enum_& value(char const* name, Type value) { |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 960 | this->attr(name) = pybind11::cast(value, return_value_policy::copy); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 961 | (*m_entries)[(int) value] = name; |
| 962 | return *this; |
| 963 | } |
| 964 | private: |
| 965 | std::unordered_map<int, const char *> *m_entries; |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 966 | handle m_parent; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 967 | }; |
| 968 | |
| 969 | NAMESPACE_BEGIN(detail) |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 970 | template <typename... Args> struct init { |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 971 | template <typename Base, typename Holder, typename... Extra> void execute(pybind11::class_<Base, Holder> &class_, const Extra&... extra) const { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 972 | /// Function which calls a specific C++ in-place constructor |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 973 | class_.def("__init__", [](Base *instance, Args... args) { new (instance) Base(args...); }, extra...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 974 | } |
| 975 | }; |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 976 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 977 | PYBIND11_NOINLINE inline void keep_alive_impl(int Nurse, int Patient, handle args, handle ret) { |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 978 | /* Clever approach based on weak references taken from Boost.Python */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 979 | handle nurse (Nurse > 0 ? PyTuple_GetItem(args.ptr(), Nurse - 1) : ret.ptr()); |
| 980 | handle patient(Patient > 0 ? PyTuple_GetItem(args.ptr(), Patient - 1) : ret.ptr()); |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 981 | |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 982 | if (!nurse || !patient) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 983 | pybind11_fail("Could not activate keep_alive!"); |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 984 | |
| 985 | cpp_function disable_lifesupport( |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 986 | [patient](handle weakref) { patient.dec_ref(); weakref.dec_ref(); }); |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 987 | |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 988 | weakref wr(nurse, disable_lifesupport); |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 989 | |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 990 | patient.inc_ref(); /* reference patient and leak the weak reference */ |
| 991 | (void) wr.release(); |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 992 | } |
| 993 | |
Wenzel Jakob | b282595 | 2016-04-13 23:33:00 +0200 | [diff] [blame] | 994 | template <typename Iterator> struct iterator_state { Iterator it, end; }; |
| 995 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 996 | NAMESPACE_END(detail) |
| 997 | |
Ben Pritchard | 2de6e1d | 2016-02-18 13:20:15 -0500 | [diff] [blame] | 998 | template <typename... Args> detail::init<Args...> init() { return detail::init<Args...>(); } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 999 | |
Wenzel Jakob | b282595 | 2016-04-13 23:33:00 +0200 | [diff] [blame] | 1000 | template <typename Iterator, typename... Extra> iterator make_iterator(Iterator first, Iterator last, Extra&&... extra) { |
| 1001 | typedef detail::iterator_state<Iterator> state; |
| 1002 | |
| 1003 | if (!detail::get_type_info(typeid(state))) { |
| 1004 | class_<state>(handle(), "") |
| 1005 | .def("__iter__", [](state &s) -> state& { return s; }) |
| 1006 | .def("__next__", [](state &s) -> decltype(*std::declval<Iterator>()) & { |
| 1007 | if (s.it == s.end) |
| 1008 | throw stop_iteration(); |
| 1009 | return *s.it++; |
| 1010 | }, return_value_policy::reference_internal, std::forward<Extra>(extra)...); |
| 1011 | } |
| 1012 | |
| 1013 | return (iterator) cast(state { first, last }); |
| 1014 | } |
| 1015 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1016 | template <typename InputType, typename OutputType> void implicitly_convertible() { |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 1017 | auto implicit_caster = [](PyObject *obj, PyTypeObject *type) -> PyObject * { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1018 | if (!detail::type_caster<InputType>().load(obj, false)) |
| 1019 | return nullptr; |
| 1020 | tuple args(1); |
| 1021 | args[0] = obj; |
| 1022 | PyObject *result = PyObject_Call((PyObject *) type, args.ptr(), nullptr); |
| 1023 | if (result == nullptr) |
| 1024 | PyErr_Clear(); |
| 1025 | return result; |
| 1026 | }; |
Wenzel Jakob | b6cf75d | 2016-01-29 11:39:32 +0100 | [diff] [blame] | 1027 | auto ®istered_types = detail::get_internals().registered_types_cpp; |
| 1028 | auto it = registered_types.find(std::type_index(typeid(OutputType))); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1029 | if (it == registered_types.end()) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 1030 | pybind11_fail("implicitly_convertible: Unable to find type " + type_id<OutputType>()); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 1031 | ((detail::type_info *) it->second)->implicit_conversions.push_back(implicit_caster); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1032 | } |
| 1033 | |
Felipe Lema | 2547ca4 | 2016-01-25 16:22:44 -0300 | [diff] [blame] | 1034 | #if defined(WITH_THREAD) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1035 | inline void init_threading() { PyEval_InitThreads(); } |
| 1036 | |
| 1037 | class gil_scoped_acquire { |
| 1038 | PyGILState_STATE state; |
| 1039 | public: |
| 1040 | inline gil_scoped_acquire() { state = PyGILState_Ensure(); } |
| 1041 | inline ~gil_scoped_acquire() { PyGILState_Release(state); } |
| 1042 | }; |
| 1043 | |
| 1044 | class gil_scoped_release { |
| 1045 | PyThreadState *state; |
| 1046 | public: |
| 1047 | inline gil_scoped_release() { state = PyEval_SaveThread(); } |
| 1048 | inline ~gil_scoped_release() { PyEval_RestoreThread(state); } |
| 1049 | }; |
Felipe Lema | 2547ca4 | 2016-01-25 16:22:44 -0300 | [diff] [blame] | 1050 | #endif |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1051 | |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1052 | inline function get_overload(const void *this_ptr, const char *name) { |
| 1053 | handle py_object = detail::get_object_handle(this_ptr); |
Wenzel Jakob | ac0fde9 | 2015-10-01 18:37:26 +0200 | [diff] [blame] | 1054 | if (!py_object) |
| 1055 | return function(); |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1056 | handle type = py_object.get_type(); |
| 1057 | auto key = std::make_pair(type.ptr(), name); |
| 1058 | |
Wenzel Jakob | f5c154a | 2016-04-11 18:13:08 +0200 | [diff] [blame] | 1059 | /* Cache functions that aren't overloaded in Python to avoid |
| 1060 | many costly Python dictionary lookups below */ |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1061 | auto &cache = detail::get_internals().inactive_overload_cache; |
| 1062 | if (cache.find(key) != cache.end()) |
| 1063 | return function(); |
| 1064 | |
| 1065 | function overload = (function) py_object.attr(name); |
| 1066 | if (overload.is_cpp_function()) { |
| 1067 | cache.insert(key); |
| 1068 | return function(); |
| 1069 | } |
Wenzel Jakob | 27e8e10 | 2016-01-17 22:36:37 +0100 | [diff] [blame] | 1070 | |
Wenzel Jakob | f5c154a | 2016-04-11 18:13:08 +0200 | [diff] [blame] | 1071 | /* Don't call dispatch code if invoked from overridden function */ |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1072 | PyFrameObject *frame = PyThreadState_Get()->frame; |
Wenzel Jakob | b2b44a9 | 2016-04-15 17:50:40 +0200 | [diff] [blame^] | 1073 | if (frame && (std::string) pybind11::handle(frame->f_code->co_name).str() == name && |
Wenzel Jakob | f5c154a | 2016-04-11 18:13:08 +0200 | [diff] [blame] | 1074 | frame->f_code->co_argcount > 0) { |
| 1075 | PyFrame_FastToLocals(frame); |
| 1076 | PyObject *self_caller = PyDict_GetItem( |
| 1077 | frame->f_locals, PyTuple_GET_ITEM(frame->f_code->co_varnames, 0)); |
| 1078 | if (self_caller == py_object.ptr()) |
| 1079 | return function(); |
| 1080 | } |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1081 | return overload; |
| 1082 | } |
| 1083 | |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 1084 | #define PYBIND11_OVERLOAD_INT(ret_type, class_name, name, ...) { \ |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 1085 | pybind11::gil_scoped_acquire gil; \ |
| 1086 | pybind11::function overload = pybind11::get_overload(this, #name); \ |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1087 | if (overload) \ |
Johan Mabille | 25dd478 | 2016-02-26 13:09:22 +0100 | [diff] [blame] | 1088 | return overload.call(__VA_ARGS__).template cast<ret_type>(); } |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1089 | |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 1090 | #define PYBIND11_OVERLOAD(ret_type, class_name, name, ...) \ |
| 1091 | PYBIND11_OVERLOAD_INT(ret_type, class_name, name, __VA_ARGS__) \ |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1092 | return class_name::name(__VA_ARGS__) |
| 1093 | |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 1094 | #define PYBIND11_OVERLOAD_PURE(ret_type, class_name, name, ...) \ |
| 1095 | PYBIND11_OVERLOAD_INT(ret_type, class_name, name, __VA_ARGS__) \ |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 1096 | pybind11::pybind11_fail("Tried to call pure virtual function \"" #name "\""); |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1097 | |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 1098 | NAMESPACE_END(pybind11) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1099 | |
| 1100 | #if defined(_MSC_VER) |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 1101 | # pragma warning(pop) |
Ben Pritchard | 33f3430 | 2016-02-18 15:25:51 -0500 | [diff] [blame] | 1102 | #elif defined(__ICC) || defined(__INTEL_COMPILER) |
| 1103 | # pragma warning(pop) |
Wenzel Jakob | cd5cda7 | 2015-08-03 12:17:54 +0200 | [diff] [blame] | 1104 | #elif defined(__GNUG__) and !defined(__clang__) |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 1105 | # pragma GCC diagnostic pop |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1106 | #endif |