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 | |
Wenzel Jakob | 8cb6cb3 | 2016-04-17 20:21:41 +0200 | [diff] [blame] | 5 | Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch> |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 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 | 6d25296 | 2016-05-01 20:47:49 +0200 | [diff] [blame] | 23 | #elif defined(__GNUG__) && !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 | deeab55 | 2016-05-15 23:54:34 +0200 | [diff] [blame] | 28 | # pragma GCC diagnostic ignored "-Wstrict-aliasing" |
| 29 | # pragma GCC diagnostic ignored "-Wattributes" |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 30 | #endif |
| 31 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 32 | #include "attr.h" |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 33 | |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 34 | NAMESPACE_BEGIN(pybind11) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 35 | |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 36 | /// 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] | 37 | class cpp_function : public function { |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 38 | public: |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 39 | cpp_function() { } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 40 | |
Wenzel Jakob | 5984baa | 2016-05-10 15:05:03 +0100 | [diff] [blame] | 41 | /// Construct a cpp_function from a vanilla function pointer |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 42 | template <typename Return, typename... Args, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 43 | cpp_function(Return (*f)(Args...), const Extra&... extra) { |
Wenzel Jakob | 5984baa | 2016-05-10 15:05:03 +0100 | [diff] [blame] | 44 | initialize(f, f, extra...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 45 | } |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 46 | |
Wenzel Jakob | 5984baa | 2016-05-10 15:05:03 +0100 | [diff] [blame] | 47 | /// Construct a cpp_function from a lambda function (possibly with internal state) |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 48 | template <typename Func, typename... Extra> cpp_function(Func &&f, const Extra&... extra) { |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 49 | initialize(std::forward<Func>(f), |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 50 | (typename detail::remove_class<decltype( |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 51 | &std::remove_reference<Func>::type::operator())>::type *) nullptr, extra...); |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 52 | } |
| 53 | |
Wenzel Jakob | 5984baa | 2016-05-10 15:05:03 +0100 | [diff] [blame] | 54 | /// Construct a cpp_function from a class method (non-const) |
| 55 | template <typename Return, typename Class, typename... Arg, typename... Extra> |
| 56 | cpp_function(Return (Class::*f)(Arg...), const Extra&... extra) { |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 57 | initialize([f](Class *c, Arg... args) -> Return { return (c->*f)(args...); }, |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 58 | (Return (*) (Class *, Arg...)) nullptr, extra...); |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Wenzel Jakob | 5984baa | 2016-05-10 15:05:03 +0100 | [diff] [blame] | 61 | /// Construct a cpp_function from a class method (const) |
| 62 | template <typename Return, typename Class, typename... Arg, typename... Extra> |
| 63 | cpp_function(Return (Class::*f)(Arg...) const, const Extra&... extra) { |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 64 | initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(args...); }, |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 65 | (Return (*)(const Class *, Arg ...)) nullptr, extra...); |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 68 | /// Return the function name |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 69 | object name() const { return attr("__name__"); } |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 70 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 71 | protected: |
| 72 | /// Special internal constructor for functors, lambda functions, etc. |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 73 | template <typename Func, typename Return, typename... Args, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 74 | void initialize(Func &&f, Return (*)(Args...), const Extra&... extra) { |
Dean Moldovan | 9e62558 | 2016-06-04 00:27:32 +0200 | [diff] [blame] | 75 | static_assert(detail::expected_num_args<Extra...>(sizeof...(Args)), |
| 76 | "The number of named arguments does not match the function signature"); |
| 77 | |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 78 | struct capture { typename std::remove_reference<Func>::type f; }; |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 79 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 80 | /* 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] | 81 | auto rec = new detail::function_record(); |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 82 | |
Wenzel Jakob | 5984baa | 2016-05-10 15:05:03 +0100 | [diff] [blame] | 83 | /* Store the capture object directly in the function record if there is enough space */ |
| 84 | if (sizeof(capture) <= sizeof(rec->data)) { |
Wenzel Jakob | 954b793 | 2016-07-10 10:13:18 +0200 | [diff] [blame] | 85 | /* Without these pragmas, GCC warns that there might not be |
| 86 | enough space to use the placement new operator. However, the |
| 87 | 'if' statement above ensures that this is the case. */ |
Jason Rhinelander | 0b12f91 | 2016-07-07 16:26:04 -0400 | [diff] [blame] | 88 | #if defined(__GNUG__) && !defined(__clang__) && __GNUC__ >= 6 |
Jason Rhinelander | cae0e00 | 2016-07-07 16:11:42 -0400 | [diff] [blame] | 89 | # pragma GCC diagnostic push |
| 90 | # pragma GCC diagnostic ignored "-Wplacement-new" |
| 91 | #endif |
Wenzel Jakob | 5984baa | 2016-05-10 15:05:03 +0100 | [diff] [blame] | 92 | new ((capture *) &rec->data) capture { std::forward<Func>(f) }; |
Jason Rhinelander | 0b12f91 | 2016-07-07 16:26:04 -0400 | [diff] [blame] | 93 | #if defined(__GNUG__) && !defined(__clang__) && __GNUC__ >= 6 |
Jason Rhinelander | cae0e00 | 2016-07-07 16:11:42 -0400 | [diff] [blame] | 94 | # pragma GCC diagnostic pop |
| 95 | #endif |
Wenzel Jakob | 5984baa | 2016-05-10 15:05:03 +0100 | [diff] [blame] | 96 | if (!std::is_trivially_destructible<Func>::value) |
| 97 | rec->free_data = [](detail::function_record *r) { ((capture *) &r->data)->~capture(); }; |
| 98 | } else { |
| 99 | rec->data[0] = new capture { std::forward<Func>(f) }; |
| 100 | rec->free_data = [](detail::function_record *r) { delete ((capture *) r->data[0]); }; |
| 101 | } |
Wenzel Jakob | 19208fe | 2015-10-13 17:37:25 +0200 | [diff] [blame] | 102 | |
Wenzel Jakob | 5984baa | 2016-05-10 15:05:03 +0100 | [diff] [blame] | 103 | /* Type casters for the function arguments and return value */ |
| 104 | typedef detail::type_caster<typename std::tuple<Args...>> cast_in; |
| 105 | typedef detail::type_caster<typename std::conditional< |
| 106 | std::is_void<Return>::value, detail::void_type, |
| 107 | typename detail::intrinsic_type<Return>::type>::type> cast_out; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 108 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 109 | /* Dispatch code which converts function arguments and performs the actual function call */ |
Wenzel Jakob | 178c8a8 | 2016-05-10 15:59:01 +0100 | [diff] [blame] | 110 | rec->impl = [](detail::function_record *rec, handle args, handle kwargs, handle parent) -> handle { |
Wenzel Jakob | 5984baa | 2016-05-10 15:05:03 +0100 | [diff] [blame] | 111 | cast_in args_converter; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 112 | |
| 113 | /* Try to cast the function arguments into the C++ domain */ |
Wenzel Jakob | 178c8a8 | 2016-05-10 15:59:01 +0100 | [diff] [blame] | 114 | if (!args_converter.load_args(args, kwargs, true)) |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 115 | return PYBIND11_TRY_NEXT_OVERLOAD; |
| 116 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 117 | /* Invoke call policy pre-call hook */ |
Wenzel Jakob | 5984baa | 2016-05-10 15:05:03 +0100 | [diff] [blame] | 118 | detail::process_attributes<Extra...>::precall(args); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 119 | |
Wenzel Jakob | 5984baa | 2016-05-10 15:05:03 +0100 | [diff] [blame] | 120 | /* Get a pointer to the capture object */ |
| 121 | capture *cap = (capture *) (sizeof(capture) <= sizeof(rec->data) |
| 122 | ? &rec->data : rec->data[0]); |
| 123 | |
Wenzel Jakob | 954b793 | 2016-07-10 10:13:18 +0200 | [diff] [blame] | 124 | /* Perform the function call */ |
Wenzel Jakob | 5984baa | 2016-05-10 15:05:03 +0100 | [diff] [blame] | 125 | handle result = cast_out::cast(args_converter.template call<Return>(cap->f), |
| 126 | rec->policy, parent); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 127 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 128 | /* Invoke call policy post-call hook */ |
Wenzel Jakob | 5984baa | 2016-05-10 15:05:03 +0100 | [diff] [blame] | 129 | detail::process_attributes<Extra...>::postcall(args, result); |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 130 | |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 131 | return result; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 132 | }; |
| 133 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 134 | /* Process any user-provided function attributes */ |
| 135 | detail::process_attributes<Extra...>::init(extra..., rec); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 136 | |
| 137 | /* 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] | 138 | using detail::descr; |
Sylvain Corlay | 4c7bf9b | 2016-03-08 18:44:04 -0500 | [diff] [blame] | 139 | PYBIND11_DESCR signature = cast_in::name() + detail::_(" -> ") + cast_out::name(); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 140 | |
| 141 | /* Register the function with Python from generic (non-templated) code */ |
Wenzel Jakob | 5984baa | 2016-05-10 15:05:03 +0100 | [diff] [blame] | 142 | initialize_generic(rec, signature.text(), signature.types(), sizeof...(Args)); |
Wenzel Jakob | 178c8a8 | 2016-05-10 15:59:01 +0100 | [diff] [blame] | 143 | |
| 144 | if (cast_in::has_args) rec->has_args = true; |
| 145 | if (cast_in::has_kwargs) rec->has_kwargs = true; |
Wenzel Jakob | 954b793 | 2016-07-10 10:13:18 +0200 | [diff] [blame] | 146 | |
| 147 | /* Stash some additional information used by an important optimization in 'functional.h' */ |
| 148 | using FunctionType = Return (*)(Args...); |
| 149 | constexpr bool is_function_ptr = |
| 150 | std::is_convertible<Func, FunctionType>::value && |
| 151 | sizeof(capture) == sizeof(void *); |
| 152 | if (is_function_ptr) { |
| 153 | rec->is_stateless = true; |
| 154 | rec->data[1] = (void *) &typeid(FunctionType); |
| 155 | } |
Wenzel Jakob | 281aa0e | 2015-07-30 15:29:00 +0200 | [diff] [blame] | 156 | } |
| 157 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 158 | /// Register a function call with Python (generic non-templated code goes here) |
Wenzel Jakob | 5984baa | 2016-05-10 15:05:03 +0100 | [diff] [blame] | 159 | void initialize_generic(detail::function_record *rec, const char *text, |
| 160 | const std::type_info *const *types, int args) { |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 161 | |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 162 | /* Create copies of all referenced C-style strings */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 163 | rec->name = strdup(rec->name ? rec->name : ""); |
| 164 | if (rec->doc) rec->doc = strdup(rec->doc); |
| 165 | for (auto &a: rec->args) { |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 166 | if (a.name) |
| 167 | a.name = strdup(a.name); |
| 168 | if (a.descr) |
| 169 | a.descr = strdup(a.descr); |
| 170 | else if (a.value) |
Wenzel Jakob | 6c03beb | 2016-05-08 14:34:09 +0200 | [diff] [blame] | 171 | a.descr = strdup(((std::string) ((object) handle(a.value).attr("__repr__"))().str()).c_str()); |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 172 | } |
Wenzel Jakob | 954b793 | 2016-07-10 10:13:18 +0200 | [diff] [blame] | 173 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 174 | auto const ®istered_types = detail::get_internals().registered_types_cpp; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 175 | |
| 176 | /* Generate a proper function signature */ |
| 177 | std::string signature; |
| 178 | size_t type_depth = 0, char_index = 0, type_index = 0, arg_index = 0; |
| 179 | while (true) { |
| 180 | char c = text[char_index++]; |
| 181 | if (c == '\0') |
| 182 | break; |
| 183 | |
| 184 | if (c == '{') { |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 185 | if (type_depth == 1 && arg_index < rec->args.size()) { |
| 186 | signature += rec->args[arg_index].name; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 187 | signature += " : "; |
| 188 | } |
| 189 | ++type_depth; |
| 190 | } else if (c == '}') { |
| 191 | --type_depth; |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 192 | if (type_depth == 1 && arg_index < rec->args.size()) { |
| 193 | if (rec->args[arg_index].descr) { |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 194 | signature += " = "; |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 195 | signature += rec->args[arg_index].descr; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 196 | } |
| 197 | arg_index++; |
| 198 | } |
| 199 | } else if (c == '%') { |
| 200 | const std::type_info *t = types[type_index++]; |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 201 | if (!t) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 202 | pybind11_fail("Internal error while parsing type signature (1)"); |
Wenzel Jakob | b6cf75d | 2016-01-29 11:39:32 +0100 | [diff] [blame] | 203 | auto it = registered_types.find(std::type_index(*t)); |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 204 | if (it != registered_types.end()) { |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 205 | signature += ((const detail::type_info *) it->second)->type->tp_name; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 206 | } else { |
| 207 | std::string tname(t->name()); |
| 208 | detail::clean_type_id(tname); |
| 209 | signature += tname; |
| 210 | } |
| 211 | } else { |
| 212 | signature += c; |
| 213 | } |
| 214 | } |
Wenzel Jakob | 95d1869 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 215 | if (type_depth != 0 || types[type_index] != nullptr) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 216 | pybind11_fail("Internal error while parsing type signature (2)"); |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 217 | |
| 218 | #if !defined(PYBIND11_CPP14) |
| 219 | delete[] types; |
| 220 | delete[] text; |
| 221 | #endif |
Wenzel Jakob | f5fae92 | 2015-08-24 15:31:24 +0200 | [diff] [blame] | 222 | |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 223 | #if PY_MAJOR_VERSION < 3 |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 224 | if (strcmp(rec->name, "__next__") == 0) { |
| 225 | std::free(rec->name); |
| 226 | rec->name = strdup("next"); |
Wenzel Jakob | d1bfc4e | 2016-05-16 18:52:46 +0200 | [diff] [blame] | 227 | } else if (strcmp(rec->name, "__bool__") == 0) { |
| 228 | std::free(rec->name); |
| 229 | rec->name = strdup("__nonzero__"); |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 230 | } |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 231 | #endif |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 232 | rec->signature = strdup(signature.c_str()); |
| 233 | rec->args.shrink_to_fit(); |
Wenzel Jakob | a380ed9 | 2016-05-15 23:54:13 +0200 | [diff] [blame] | 234 | rec->is_constructor = !strcmp(rec->name, "__init__") || !strcmp(rec->name, "__setstate__"); |
Wenzel Jakob | 954b793 | 2016-07-10 10:13:18 +0200 | [diff] [blame] | 235 | rec->is_stateless = false; |
Wenzel Jakob | 178c8a8 | 2016-05-10 15:59:01 +0100 | [diff] [blame] | 236 | rec->has_args = false; |
| 237 | rec->has_kwargs = false; |
Wenzel Jakob | affb9f4 | 2016-05-15 23:55:06 +0200 | [diff] [blame] | 238 | rec->nargs = (uint16_t) args; |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 239 | |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 240 | #if PY_MAJOR_VERSION < 3 |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 241 | if (rec->sibling && PyMethod_Check(rec->sibling.ptr())) |
| 242 | rec->sibling = PyMethod_GET_FUNCTION(rec->sibling.ptr()); |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 243 | #endif |
| 244 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 245 | detail::function_record *chain = nullptr, *chain_start = rec; |
| 246 | if (rec->sibling && PyCFunction_Check(rec->sibling.ptr())) { |
| 247 | capsule rec_capsule(PyCFunction_GetSelf(rec->sibling.ptr()), true); |
| 248 | chain = (detail::function_record *) rec_capsule; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 249 | /* Never append a method to an overload chain of a parent class; |
| 250 | instead, hide the parent's overloads in this case */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 251 | if (chain->class_ != rec->class_) |
| 252 | chain = nullptr; |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 253 | } |
| 254 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 255 | if (!chain) { |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 256 | /* No existing overload was found, create a new function object */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 257 | rec->def = new PyMethodDef(); |
| 258 | memset(rec->def, 0, sizeof(PyMethodDef)); |
| 259 | rec->def->ml_name = rec->name; |
| 260 | rec->def->ml_meth = reinterpret_cast<PyCFunction>(*dispatcher); |
| 261 | rec->def->ml_flags = METH_VARARGS | METH_KEYWORDS; |
Wenzel Jakob | a650179 | 2016-02-04 23:02:07 +0100 | [diff] [blame] | 262 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 263 | capsule rec_capsule(rec, [](PyObject *o) { |
| 264 | destruct((detail::function_record *) PyCapsule_GetPointer(o, nullptr)); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 265 | }); |
Wenzel Jakob | a650179 | 2016-02-04 23:02:07 +0100 | [diff] [blame] | 266 | |
| 267 | object scope_module; |
| 268 | if (rec->scope) { |
| 269 | scope_module = (object) rec->scope.attr("__module__"); |
| 270 | if (!scope_module) |
| 271 | scope_module = (object) rec->scope.attr("__name__"); |
| 272 | } |
| 273 | |
| 274 | m_ptr = PyCFunction_NewEx(rec->def, rec_capsule.ptr(), scope_module.ptr()); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 275 | if (!m_ptr) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 276 | pybind11_fail("cpp_function::cpp_function(): Could not allocate function object"); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 277 | } else { |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 278 | /* Append at the end of the overload chain */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 279 | m_ptr = rec->sibling.ptr(); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 280 | inc_ref(); |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 281 | chain_start = chain; |
| 282 | while (chain->next) |
| 283 | chain = chain->next; |
| 284 | chain->next = rec; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 285 | } |
Wenzel Jakob | 281aa0e | 2015-07-30 15:29:00 +0200 | [diff] [blame] | 286 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 287 | std::string signatures; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 288 | int index = 0; |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 289 | /* Create a nice pydoc rec including all signatures and |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 290 | docstrings of the functions in the overload chain */ |
Sylvain Corlay | 0e04fdf | 2016-03-08 16:42:12 -0500 | [diff] [blame] | 291 | if (chain) { |
| 292 | // First a generic signature |
| 293 | signatures += rec->name; |
| 294 | signatures += "(*args, **kwargs)\n"; |
| 295 | signatures += "Overloaded function.\n\n"; |
| 296 | } |
| 297 | // Then specific overload signatures |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 298 | for (auto it = chain_start; it != nullptr; it = it->next) { |
| 299 | if (chain) |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 300 | signatures += std::to_string(++index) + ". "; |
Sylvain Corlay | 13b22bf | 2016-02-28 21:23:39 -0500 | [diff] [blame] | 301 | signatures += rec->name; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 302 | signatures += it->signature; |
| 303 | signatures += "\n"; |
| 304 | if (it->doc && strlen(it->doc) > 0) { |
| 305 | signatures += "\n"; |
Wenzel Jakob | 218b6ce | 2016-02-28 23:52:37 +0100 | [diff] [blame] | 306 | signatures += it->doc; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 307 | signatures += "\n"; |
| 308 | } |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 309 | if (it->next) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 310 | signatures += "\n"; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 311 | } |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 312 | |
| 313 | /* Install docstring */ |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 314 | PyCFunctionObject *func = (PyCFunctionObject *) m_ptr; |
| 315 | if (func->m_ml->ml_doc) |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 316 | std::free((char *) func->m_ml->ml_doc); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 317 | func->m_ml->ml_doc = strdup(signatures.c_str()); |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 318 | |
| 319 | if (rec->class_) { |
| 320 | m_ptr = PYBIND11_INSTANCE_METHOD_NEW(m_ptr, rec->class_.ptr()); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 321 | if (!m_ptr) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 322 | pybind11_fail("cpp_function::cpp_function(): Could not allocate instance method object"); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 323 | Py_DECREF(func); |
| 324 | } |
| 325 | } |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 326 | |
| 327 | /// When a cpp_function is GCed, release any memory allocated by pybind11 |
| 328 | static void destruct(detail::function_record *rec) { |
| 329 | while (rec) { |
| 330 | detail::function_record *next = rec->next; |
| 331 | if (rec->free_data) |
Wenzel Jakob | 5984baa | 2016-05-10 15:05:03 +0100 | [diff] [blame] | 332 | rec->free_data(rec); |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 333 | std::free((char *) rec->name); |
| 334 | std::free((char *) rec->doc); |
| 335 | std::free((char *) rec->signature); |
| 336 | for (auto &arg: rec->args) { |
| 337 | std::free((char *) arg.name); |
| 338 | std::free((char *) arg.descr); |
| 339 | arg.value.dec_ref(); |
| 340 | } |
| 341 | if (rec->def) { |
| 342 | std::free((char *) rec->def->ml_doc); |
| 343 | delete rec->def; |
| 344 | } |
| 345 | delete rec; |
| 346 | rec = next; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /// Main dispatch logic for calls to functions bound using pybind11 |
| 351 | static PyObject *dispatcher(PyObject *self, PyObject *args, PyObject *kwargs) { |
| 352 | /* Iterator over the list of potentially admissible overloads */ |
| 353 | detail::function_record *overloads = (detail::function_record *) PyCapsule_GetPointer(self, nullptr), |
| 354 | *it = overloads; |
| 355 | |
| 356 | /* Need to know how many arguments + keyword arguments there are to pick the right overload */ |
Wenzel Jakob | 0a07805 | 2016-05-29 13:40:40 +0200 | [diff] [blame] | 357 | size_t nargs = (size_t) PyTuple_GET_SIZE(args), |
| 358 | nkwargs = kwargs ? (size_t) PyDict_Size(kwargs) : 0; |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 359 | |
Wenzel Jakob | 178c8a8 | 2016-05-10 15:59:01 +0100 | [diff] [blame] | 360 | handle parent = nargs > 0 ? PyTuple_GET_ITEM(args, 0) : nullptr, |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 361 | result = PYBIND11_TRY_NEXT_OVERLOAD; |
| 362 | try { |
| 363 | for (; it != nullptr; it = it->next) { |
| 364 | tuple args_(args, true); |
Wenzel Jakob | 178c8a8 | 2016-05-10 15:59:01 +0100 | [diff] [blame] | 365 | size_t kwargs_consumed = 0; |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 366 | |
| 367 | /* For each overload: |
| 368 | 1. If the required list of arguments is longer than the |
| 369 | actually provided amount, create a copy of the argument |
| 370 | list and fill in any available keyword/default arguments. |
| 371 | 2. Ensure that all keyword arguments were "consumed" |
| 372 | 3. Call the function call dispatcher (function_record::impl) |
| 373 | */ |
Wenzel Jakob | 178c8a8 | 2016-05-10 15:59:01 +0100 | [diff] [blame] | 374 | size_t nargs_ = nargs; |
| 375 | if (nargs < it->args.size()) { |
| 376 | nargs_ = it->args.size(); |
| 377 | args_ = tuple(nargs_); |
| 378 | for (size_t i = 0; i < nargs; ++i) { |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 379 | handle item = PyTuple_GET_ITEM(args, i); |
| 380 | PyTuple_SET_ITEM(args_.ptr(), i, item.inc_ref().ptr()); |
| 381 | } |
| 382 | |
| 383 | int arg_ctr = 0; |
| 384 | for (auto const &it2 : it->args) { |
| 385 | int index = arg_ctr++; |
| 386 | if (PyTuple_GET_ITEM(args_.ptr(), index)) |
| 387 | continue; |
| 388 | |
| 389 | handle value; |
| 390 | if (kwargs) |
| 391 | value = PyDict_GetItemString(kwargs, it2.name); |
| 392 | |
| 393 | if (value) |
| 394 | kwargs_consumed++; |
| 395 | else if (it2.value) |
| 396 | value = it2.value; |
| 397 | |
| 398 | if (value) { |
| 399 | PyTuple_SET_ITEM(args_.ptr(), index, value.inc_ref().ptr()); |
| 400 | } else { |
Wenzel Jakob | 178c8a8 | 2016-05-10 15:59:01 +0100 | [diff] [blame] | 401 | kwargs_consumed = (size_t) -1; /* definite failure */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 402 | break; |
| 403 | } |
| 404 | } |
| 405 | } |
Wenzel Jakob | bd57eb4 | 2016-05-01 14:42:20 +0200 | [diff] [blame] | 406 | |
| 407 | try { |
Wenzel Jakob | 178c8a8 | 2016-05-10 15:59:01 +0100 | [diff] [blame] | 408 | if ((kwargs_consumed == nkwargs || it->has_kwargs) && |
| 409 | (nargs_ == it->nargs || it->has_args)) |
| 410 | result = it->impl(it, args_, kwargs, parent); |
Wenzel Jakob | 0006259 | 2016-07-01 16:07:35 +0200 | [diff] [blame] | 411 | } catch (reference_cast_error &) { |
Wenzel Jakob | bd57eb4 | 2016-05-01 14:42:20 +0200 | [diff] [blame] | 412 | result = PYBIND11_TRY_NEXT_OVERLOAD; |
| 413 | } |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 414 | |
| 415 | if (result.ptr() != PYBIND11_TRY_NEXT_OVERLOAD) |
| 416 | break; |
| 417 | } |
Pim Schellart | 5a7d17f | 2016-06-17 17:35:59 -0400 | [diff] [blame] | 418 | } catch (const error_already_set &) { |
| 419 | return nullptr; |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 420 | } catch (...) { |
Pim Schellart | 5a7d17f | 2016-06-17 17:35:59 -0400 | [diff] [blame] | 421 | /* When an exception is caught, give each registered exception |
| 422 | translator a chance to translate it to a Python exception |
| 423 | in reverse order of registration. |
Wenzel Jakob | 1f66a58 | 2016-07-18 10:47:10 +0200 | [diff] [blame] | 424 | |
Pim Schellart | 5a7d17f | 2016-06-17 17:35:59 -0400 | [diff] [blame] | 425 | A translator may choose to do one of the following: |
Wenzel Jakob | 1f66a58 | 2016-07-18 10:47:10 +0200 | [diff] [blame] | 426 | |
Pim Schellart | 5a7d17f | 2016-06-17 17:35:59 -0400 | [diff] [blame] | 427 | - catch the exception and call PyErr_SetString or PyErr_SetObject |
| 428 | to set a standard (or custom) Python exception, or |
| 429 | - do nothing and let the exception fall through to the next translator, or |
| 430 | - delegate translation to the next translator by throwing a new type of exception. */ |
| 431 | |
Wenzel Jakob | 1f66a58 | 2016-07-18 10:47:10 +0200 | [diff] [blame] | 432 | auto last_exception = std::current_exception(); |
Pim Schellart | 5a7d17f | 2016-06-17 17:35:59 -0400 | [diff] [blame] | 433 | auto ®istered_exception_translators = pybind11::detail::get_internals().registered_exception_translators; |
| 434 | for (auto& translator : registered_exception_translators) { |
| 435 | try { |
| 436 | translator(last_exception); |
| 437 | } catch (...) { |
| 438 | last_exception = std::current_exception(); |
| 439 | continue; |
| 440 | } |
| 441 | return nullptr; |
| 442 | } |
| 443 | PyErr_SetString(PyExc_SystemError, "Exception escaped from default exception translator!"); |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 444 | return nullptr; |
| 445 | } |
| 446 | |
| 447 | if (result.ptr() == PYBIND11_TRY_NEXT_OVERLOAD) { |
Jason Rhinelander | 4e45e18 | 2016-07-17 17:43:00 -0400 | [diff] [blame] | 448 | std::string msg = "Incompatible " + std::string(overloads->is_constructor ? "constructor" : "function") + |
| 449 | " arguments. The following argument types are supported:\n"; |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 450 | int ctr = 0; |
| 451 | for (detail::function_record *it2 = overloads; it2 != nullptr; it2 = it2->next) { |
| 452 | msg += " "+ std::to_string(++ctr) + ". "; |
Jason Rhinelander | 4e45e18 | 2016-07-17 17:43:00 -0400 | [diff] [blame] | 453 | |
| 454 | bool wrote_sig = false; |
| 455 | if (overloads->is_constructor) { |
| 456 | // For a constructor, rewrite `(Object, arg0, ...) -> NoneType` as `Object(arg0, ...)` |
| 457 | std::string sig = it2->signature; |
| 458 | size_t start = sig.find('(') + 1; |
| 459 | if (start < sig.size()) { |
| 460 | // End at the , for the next argument |
| 461 | size_t end = sig.find(", "), next = end + 2; |
| 462 | size_t ret = sig.rfind(" -> "); |
| 463 | // Or the ), if there is no comma: |
| 464 | if (end >= sig.size()) next = end = sig.find(')'); |
| 465 | if (start < end && next < sig.size()) { |
| 466 | msg.append(sig, start, end - start); |
| 467 | msg += '('; |
| 468 | msg.append(sig, next, ret - next); |
| 469 | wrote_sig = true; |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | if (!wrote_sig) msg += it2->signature; |
| 474 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 475 | msg += "\n"; |
| 476 | } |
Andreas Bergmeier | 16d4394 | 2016-05-24 09:19:35 +0200 | [diff] [blame] | 477 | msg += " Invoked with: "; |
| 478 | tuple args_(args, true); |
Wenzel Jakob | 1f66a58 | 2016-07-18 10:47:10 +0200 | [diff] [blame] | 479 | for (size_t ti = overloads->is_constructor ? 1 : 0; ti < args_.size(); ++ti) { |
Andreas Bergmeier | 16d4394 | 2016-05-24 09:19:35 +0200 | [diff] [blame] | 480 | msg += static_cast<std::string>(static_cast<object>(args_[ti]).str()); |
| 481 | if ((ti + 1) != args_.size() ) |
| 482 | msg += ", "; |
| 483 | } |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 484 | PyErr_SetString(PyExc_TypeError, msg.c_str()); |
| 485 | return nullptr; |
| 486 | } else if (!result) { |
| 487 | std::string msg = "Unable to convert function return value to a " |
| 488 | "Python type! The signature was\n\t"; |
| 489 | msg += it->signature; |
| 490 | PyErr_SetString(PyExc_TypeError, msg.c_str()); |
| 491 | return nullptr; |
| 492 | } else { |
| 493 | if (overloads->is_constructor) { |
Wenzel Jakob | 772c6d5 | 2016-04-30 19:56:10 +0200 | [diff] [blame] | 494 | /* When a constructor ran successfully, the corresponding |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 495 | holder type (e.g. std::unique_ptr) must still be initialized. */ |
Wenzel Jakob | 178c8a8 | 2016-05-10 15:59:01 +0100 | [diff] [blame] | 496 | PyObject *inst = PyTuple_GET_ITEM(args, 0); |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 497 | auto tinfo = detail::get_type_info(Py_TYPE(inst)); |
| 498 | tinfo->init_holder(inst, nullptr); |
| 499 | } |
| 500 | return result.ptr(); |
| 501 | } |
| 502 | } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 503 | }; |
| 504 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 505 | /// Wrapper for Python extension modules |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 506 | class module : public object { |
| 507 | public: |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 508 | PYBIND11_OBJECT_DEFAULT(module, object, PyModule_Check) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 509 | |
| 510 | module(const char *name, const char *doc = nullptr) { |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 511 | #if PY_MAJOR_VERSION >= 3 |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 512 | PyModuleDef *def = new PyModuleDef(); |
| 513 | memset(def, 0, sizeof(PyModuleDef)); |
| 514 | def->m_name = name; |
| 515 | def->m_doc = doc; |
| 516 | def->m_size = -1; |
| 517 | Py_INCREF(def); |
| 518 | m_ptr = PyModule_Create(def); |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 519 | #else |
| 520 | m_ptr = Py_InitModule3(name, nullptr, doc); |
| 521 | #endif |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 522 | if (m_ptr == nullptr) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 523 | pybind11_fail("Internal error in module::module()"); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 524 | inc_ref(); |
| 525 | } |
| 526 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 527 | template <typename Func, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 528 | module &def(const char *name_, Func &&f, const Extra& ... extra) { |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 529 | cpp_function func(std::forward<Func>(f), name(name_), |
Wenzel Jakob | a650179 | 2016-02-04 23:02:07 +0100 | [diff] [blame] | 530 | sibling((handle) attr(name_)), scope(*this), extra...); |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 531 | /* PyModule_AddObject steals a reference to 'func' */ |
| 532 | PyModule_AddObject(ptr(), name_, func.inc_ref().ptr()); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 533 | return *this; |
| 534 | } |
| 535 | |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 536 | module def_submodule(const char *name, const char *doc = nullptr) { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 537 | std::string full_name = std::string(PyModule_GetName(m_ptr)) |
| 538 | + std::string(".") + std::string(name); |
| 539 | module result(PyImport_AddModule(full_name.c_str()), true); |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 540 | if (doc) |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 541 | result.attr("__doc__") = pybind11::str(doc); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 542 | attr(name) = result; |
| 543 | return result; |
| 544 | } |
Wenzel Jakob | db028d6 | 2015-10-13 23:44:25 +0200 | [diff] [blame] | 545 | |
| 546 | static module import(const char *name) { |
Wenzel Jakob | dd57a34 | 2015-12-26 14:04:52 +0100 | [diff] [blame] | 547 | PyObject *obj = PyImport_ImportModule(name); |
| 548 | if (!obj) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 549 | pybind11_fail("Module \"" + std::string(name) + "\" not found!"); |
Wenzel Jakob | dd57a34 | 2015-12-26 14:04:52 +0100 | [diff] [blame] | 550 | return module(obj, false); |
Wenzel Jakob | db028d6 | 2015-10-13 23:44:25 +0200 | [diff] [blame] | 551 | } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 552 | }; |
| 553 | |
| 554 | NAMESPACE_BEGIN(detail) |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 555 | /// Generic support for creating new Python heap types |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 556 | class generic_type : public object { |
Wenzel Jakob | 86d825f | 2016-05-26 13:19:27 +0200 | [diff] [blame] | 557 | template <typename type, typename holder_type, typename type_alias> friend class class_; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 558 | public: |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 559 | PYBIND11_OBJECT_DEFAULT(generic_type, object, PyType_Check) |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 560 | protected: |
| 561 | void initialize(type_record *rec) { |
| 562 | if (rec->base_type) { |
| 563 | if (rec->base_handle) |
| 564 | pybind11_fail("generic_type: specified base type multiple times!"); |
| 565 | rec->base_handle = detail::get_type_handle(*(rec->base_type)); |
| 566 | if (!rec->base_handle) { |
| 567 | std::string tname(rec->base_type->name()); |
| 568 | detail::clean_type_id(tname); |
| 569 | pybind11_fail("generic_type: type \"" + std::string(rec->name) + |
| 570 | "\" referenced unknown base type \"" + tname + "\""); |
| 571 | } |
| 572 | } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 573 | |
Wenzel Jakob | 38d8b8c | 2016-05-31 09:53:28 +0200 | [diff] [blame] | 574 | auto &internals = get_internals(); |
| 575 | auto tindex = std::type_index(*(rec->type)); |
| 576 | |
| 577 | if (internals.registered_types_cpp.find(tindex) != |
| 578 | internals.registered_types_cpp.end()) |
| 579 | pybind11_fail("generic_type: type \"" + std::string(rec->name) + |
| 580 | "\" is already registered!"); |
| 581 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 582 | object name(PYBIND11_FROM_STRING(rec->name), false); |
Wenzel Jakob | c47d498 | 2016-07-11 23:40:28 +0200 | [diff] [blame] | 583 | object scope_module; |
| 584 | if (rec->scope) { |
| 585 | scope_module = (object) rec->scope.attr("__module__"); |
| 586 | if (!scope_module) |
| 587 | scope_module = (object) rec->scope.attr("__name__"); |
| 588 | } |
| 589 | |
| 590 | #if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3 |
| 591 | /* Qualified names for Python >= 3.3 */ |
| 592 | object scope_qualname; |
| 593 | if (rec->scope) |
| 594 | scope_qualname = (object) rec->scope.attr("__qualname__"); |
| 595 | object ht_qualname; |
| 596 | if (scope_qualname) { |
| 597 | ht_qualname = object(PyUnicode_FromFormat( |
| 598 | "%U.%U", scope_qualname.ptr(), name.ptr()), false); |
| 599 | } else { |
| 600 | ht_qualname = name; |
| 601 | } |
| 602 | #endif |
| 603 | std::string full_name = (scope_module ? ((std::string) scope_module.str() + "." + rec->name) |
| 604 | : std::string(rec->name)); |
| 605 | |
| 606 | char *tp_doc = nullptr; |
| 607 | if (rec->doc) { |
| 608 | /* Allocate memory for docstring (using PyObject_MALLOC, since |
| 609 | Python will free this later on) */ |
| 610 | size_t size = strlen(rec->doc) + 1; |
| 611 | tp_doc = (char *) PyObject_MALLOC(size); |
| 612 | memcpy((void *) tp_doc, rec->doc, size); |
| 613 | } |
| 614 | |
| 615 | object type_holder(PyType_Type.tp_alloc(&PyType_Type, 0), false); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 616 | auto type = (PyHeapTypeObject*) type_holder.ptr(); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 617 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 618 | if (!type_holder || !name) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 619 | pybind11_fail("generic_type: unable to create type object!"); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 620 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 621 | /* Register supplemental type information in C++ dict */ |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 622 | detail::type_info *tinfo = new detail::type_info(); |
| 623 | tinfo->type = (PyTypeObject *) type; |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 624 | tinfo->type_size = rec->type_size; |
| 625 | tinfo->init_holder = rec->init_holder; |
Wenzel Jakob | 38d8b8c | 2016-05-31 09:53:28 +0200 | [diff] [blame] | 626 | internals.registered_types_cpp[tindex] = tinfo; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 627 | internals.registered_types_py[type] = tinfo; |
| 628 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 629 | /* Basic type attributes */ |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 630 | type->ht_type.tp_name = strdup(full_name.c_str()); |
Wenzel Jakob | 0a07805 | 2016-05-29 13:40:40 +0200 | [diff] [blame] | 631 | type->ht_type.tp_basicsize = (ssize_t) rec->instance_size; |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 632 | type->ht_type.tp_base = (PyTypeObject *) rec->base_handle.ptr(); |
| 633 | rec->base_handle.inc_ref(); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 634 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 635 | type->ht_name = name.release().ptr(); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 636 | |
Wenzel Jakob | c47d498 | 2016-07-11 23:40:28 +0200 | [diff] [blame] | 637 | #if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3 |
| 638 | type->ht_qualname = ht_qualname.release().ptr(); |
| 639 | #endif |
| 640 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 641 | /* Supported protocols */ |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 642 | type->ht_type.tp_as_number = &type->as_number; |
| 643 | type->ht_type.tp_as_sequence = &type->as_sequence; |
| 644 | type->ht_type.tp_as_mapping = &type->as_mapping; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 645 | |
| 646 | /* Supported elementary operations */ |
| 647 | type->ht_type.tp_init = (initproc) init; |
| 648 | type->ht_type.tp_new = (newfunc) new_instance; |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 649 | type->ht_type.tp_dealloc = rec->dealloc; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 650 | |
| 651 | /* Support weak references (needed for the keep_alive feature) */ |
Wenzel Jakob | 88d1d04 | 2016-01-20 01:26:42 +0100 | [diff] [blame] | 652 | type->ht_type.tp_weaklistoffset = offsetof(instance_essentials<void>, weakrefs); |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 653 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 654 | /* Flags */ |
| 655 | type->ht_type.tp_flags |= Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE; |
| 656 | #if PY_MAJOR_VERSION < 3 |
| 657 | type->ht_type.tp_flags |= Py_TPFLAGS_CHECKTYPES; |
| 658 | #endif |
| 659 | type->ht_type.tp_flags &= ~Py_TPFLAGS_HAVE_GC; |
| 660 | |
Wenzel Jakob | c47d498 | 2016-07-11 23:40:28 +0200 | [diff] [blame] | 661 | type->ht_type.tp_doc = tp_doc; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 662 | |
| 663 | if (PyType_Ready(&type->ht_type) < 0) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 664 | pybind11_fail("generic_type: PyType_Ready failed!"); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 665 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 666 | m_ptr = type_holder.ptr(); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 667 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 668 | if (scope_module) // Needed by pydoc |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 669 | attr("__module__") = scope_module; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 670 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 671 | /* Register type with the parent scope */ |
Wenzel Jakob | b282595 | 2016-04-13 23:33:00 +0200 | [diff] [blame] | 672 | if (rec->scope) |
| 673 | rec->scope.attr(handle(type->ht_name)) = *this; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 674 | |
| 675 | type_holder.release(); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 676 | } |
| 677 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 678 | /// Allocate a metaclass on demand (for static properties) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 679 | handle metaclass() { |
| 680 | auto &ht_type = ((PyHeapTypeObject *) m_ptr)->ht_type; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 681 | auto &ob_type = PYBIND11_OB_TYPE(ht_type); |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 682 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 683 | if (ob_type == &PyType_Type) { |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 684 | std::string name_ = std::string(ht_type.tp_name) + "__Meta"; |
Wenzel Jakob | c47d498 | 2016-07-11 23:40:28 +0200 | [diff] [blame] | 685 | #if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3 |
| 686 | object ht_qualname(PyUnicode_FromFormat( |
| 687 | "%U__Meta", ((object) attr("__qualname__")).ptr()), false); |
| 688 | #endif |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 689 | object name(PYBIND11_FROM_STRING(name_.c_str()), false); |
Wenzel Jakob | c47d498 | 2016-07-11 23:40:28 +0200 | [diff] [blame] | 690 | object type_holder(PyType_Type.tp_alloc(&PyType_Type, 0), false); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 691 | if (!type_holder || !name) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 692 | pybind11_fail("generic_type::metaclass(): unable to create type object!"); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 693 | |
| 694 | auto type = (PyHeapTypeObject*) type_holder.ptr(); |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 695 | type->ht_name = name.release().ptr(); |
Wenzel Jakob | c47d498 | 2016-07-11 23:40:28 +0200 | [diff] [blame] | 696 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 697 | #if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3 |
| 698 | /* Qualified names for Python >= 3.3 */ |
Wenzel Jakob | c47d498 | 2016-07-11 23:40:28 +0200 | [diff] [blame] | 699 | type->ht_qualname = ht_qualname.release().ptr(); |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 700 | #endif |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 701 | type->ht_type.tp_name = strdup(name_.c_str()); |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 702 | type->ht_type.tp_base = ob_type; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 703 | type->ht_type.tp_flags |= (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE) & |
| 704 | ~Py_TPFLAGS_HAVE_GC; |
| 705 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 706 | if (PyType_Ready(&type->ht_type) < 0) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 707 | pybind11_fail("generic_type::metaclass(): PyType_Ready failed!"); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 708 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 709 | ob_type = (PyTypeObject *) type_holder.release().ptr(); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 710 | } |
| 711 | return handle((PyObject *) ob_type); |
| 712 | } |
| 713 | |
| 714 | static int init(void *self, PyObject *, PyObject *) { |
| 715 | std::string msg = std::string(Py_TYPE(self)->tp_name) + ": No constructor defined!"; |
| 716 | PyErr_SetString(PyExc_TypeError, msg.c_str()); |
| 717 | return -1; |
| 718 | } |
| 719 | |
| 720 | static PyObject *new_instance(PyTypeObject *type, PyObject *, PyObject *) { |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 721 | instance<void> *self = (instance<void> *) PyType_GenericAlloc((PyTypeObject *) type, 0); |
| 722 | auto tinfo = detail::get_type_info(type); |
| 723 | self->value = ::operator new(tinfo->type_size); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 724 | self->owned = true; |
| 725 | self->parent = nullptr; |
| 726 | self->constructed = false; |
| 727 | detail::get_internals().registered_instances[self->value] = (PyObject *) self; |
| 728 | return (PyObject *) self; |
| 729 | } |
| 730 | |
| 731 | static void dealloc(instance<void> *self) { |
| 732 | if (self->value) { |
| 733 | bool dont_cache = self->parent && ((instance<void> *) self->parent)->value == self->value; |
| 734 | if (!dont_cache) { // avoid an issue with internal references matching their parent's address |
| 735 | auto ®istered_instances = detail::get_internals().registered_instances; |
| 736 | auto it = registered_instances.find(self->value); |
| 737 | if (it == registered_instances.end()) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 738 | pybind11_fail("generic_type::dealloc(): Tried to deallocate unregistered instance!"); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 739 | registered_instances.erase(it); |
| 740 | } |
| 741 | Py_XDECREF(self->parent); |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 742 | if (self->weakrefs) |
| 743 | PyObject_ClearWeakRefs((PyObject *) self); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 744 | } |
| 745 | Py_TYPE(self)->tp_free((PyObject*) self); |
| 746 | } |
| 747 | |
Wenzel Jakob | 43398a8 | 2015-07-28 16:12:20 +0200 | [diff] [blame] | 748 | void install_buffer_funcs( |
| 749 | buffer_info *(*get_buffer)(PyObject *, void *), |
| 750 | void *get_buffer_data) { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 751 | PyHeapTypeObject *type = (PyHeapTypeObject*) m_ptr; |
| 752 | type->ht_type.tp_as_buffer = &type->as_buffer; |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 753 | #if PY_MAJOR_VERSION < 3 |
| 754 | type->ht_type.tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER; |
| 755 | #endif |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 756 | type->as_buffer.bf_getbuffer = getbuffer; |
| 757 | type->as_buffer.bf_releasebuffer = releasebuffer; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 758 | auto tinfo = detail::get_type_info(&type->ht_type); |
| 759 | tinfo->get_buffer = get_buffer; |
| 760 | tinfo->get_buffer_data = get_buffer_data; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | static int getbuffer(PyObject *obj, Py_buffer *view, int flags) { |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 764 | auto tinfo = detail::get_type_info(Py_TYPE(obj)); |
| 765 | if (view == nullptr || obj == nullptr || !tinfo || !tinfo->get_buffer) { |
| 766 | PyErr_SetString(PyExc_BufferError, "generic_type::getbuffer(): Internal error"); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 767 | return -1; |
| 768 | } |
| 769 | memset(view, 0, sizeof(Py_buffer)); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 770 | buffer_info *info = tinfo->get_buffer(obj, tinfo->get_buffer_data); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 771 | view->obj = obj; |
| 772 | view->ndim = 1; |
| 773 | view->internal = info; |
| 774 | view->buf = info->ptr; |
Wenzel Jakob | 0a07805 | 2016-05-29 13:40:40 +0200 | [diff] [blame] | 775 | view->itemsize = (ssize_t) info->itemsize; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 776 | view->len = view->itemsize; |
| 777 | for (auto s : info->shape) |
| 778 | view->len *= s; |
| 779 | if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) |
| 780 | view->format = const_cast<char *>(info->format.c_str()); |
| 781 | if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) { |
Wenzel Jakob | 0a07805 | 2016-05-29 13:40:40 +0200 | [diff] [blame] | 782 | view->ndim = (int) info->ndim; |
Wenzel Jakob | 27e8e10 | 2016-01-17 22:36:37 +0100 | [diff] [blame] | 783 | view->strides = (ssize_t *) &info->strides[0]; |
| 784 | view->shape = (ssize_t *) &info->shape[0]; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 785 | } |
| 786 | Py_INCREF(view->obj); |
| 787 | return 0; |
| 788 | } |
| 789 | |
| 790 | static void releasebuffer(PyObject *, Py_buffer *view) { delete (buffer_info *) view->internal; } |
| 791 | }; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 792 | NAMESPACE_END(detail) |
| 793 | |
Wenzel Jakob | 86d825f | 2016-05-26 13:19:27 +0200 | [diff] [blame] | 794 | template <typename type, typename holder_type = std::unique_ptr<type>, typename type_alias = type> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 795 | class class_ : public detail::generic_type { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 796 | public: |
| 797 | typedef detail::instance<type, holder_type> instance_type; |
| 798 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 799 | PYBIND11_OBJECT(class_, detail::generic_type, PyType_Check) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 800 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 801 | template <typename... Extra> |
| 802 | class_(handle scope, const char *name, const Extra &... extra) { |
| 803 | detail::type_record record; |
| 804 | record.scope = scope; |
| 805 | record.name = name; |
| 806 | record.type = &typeid(type); |
| 807 | record.type_size = sizeof(type); |
| 808 | record.instance_size = sizeof(instance_type); |
| 809 | record.init_holder = init_holder; |
| 810 | record.dealloc = dealloc; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 811 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 812 | /* Process optional arguments, if any */ |
| 813 | detail::process_attributes<Extra...>::init(extra..., &record); |
| 814 | |
| 815 | detail::generic_type::initialize(&record); |
Wenzel Jakob | 86d825f | 2016-05-26 13:19:27 +0200 | [diff] [blame] | 816 | |
| 817 | if (!std::is_same<type, type_alias>::value) { |
| 818 | auto &instances = pybind11::detail::get_internals().registered_types_cpp; |
| 819 | instances[std::type_index(typeid(type_alias))] = instances[std::type_index(typeid(type))]; |
| 820 | } |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 821 | } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 822 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 823 | template <typename Func, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 824 | class_ &def(const char *name_, Func&& f, const Extra&... extra) { |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 825 | cpp_function cf(std::forward<Func>(f), name(name_), |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 826 | sibling(attr(name_)), is_method(*this), |
| 827 | extra...); |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 828 | attr(cf.name()) = cf; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 829 | return *this; |
| 830 | } |
| 831 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 832 | template <typename Func, typename... Extra> class_ & |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 833 | def_static(const char *name_, Func f, const Extra&... extra) { |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 834 | cpp_function cf(std::forward<Func>(f), name(name_), |
Wenzel Jakob | a650179 | 2016-02-04 23:02:07 +0100 | [diff] [blame] | 835 | sibling(attr(name_)), scope(*this), extra...); |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 836 | attr(cf.name()) = cf; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 837 | return *this; |
| 838 | } |
| 839 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 840 | 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] | 841 | 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] | 842 | op.template execute<type>(*this, extra...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 843 | return *this; |
| 844 | } |
| 845 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 846 | 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] | 847 | 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] | 848 | op.template execute_cast<type>(*this, extra...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 849 | return *this; |
| 850 | } |
| 851 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 852 | template <typename... Args, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 853 | class_ &def(const detail::init<Args...> &init, const Extra&... extra) { |
Wenzel Jakob | e206564 | 2016-02-04 23:29:29 +0100 | [diff] [blame] | 854 | init.template execute<type>(*this, extra...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 855 | return *this; |
| 856 | } |
| 857 | |
Wenzel Jakob | 86d825f | 2016-05-26 13:19:27 +0200 | [diff] [blame] | 858 | template <typename... Args, typename... Extra> |
| 859 | class_ &def(const detail::init_alias<Args...> &init, const Extra&... extra) { |
| 860 | init.template execute<type>(*this, extra...); |
| 861 | return *this; |
| 862 | } |
| 863 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 864 | template <typename Func> class_& def_buffer(Func &&func) { |
Wenzel Jakob | 43398a8 | 2015-07-28 16:12:20 +0200 | [diff] [blame] | 865 | struct capture { Func func; }; |
| 866 | capture *ptr = new capture { std::forward<Func>(func) }; |
| 867 | install_buffer_funcs([](PyObject *obj, void *ptr) -> buffer_info* { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 868 | detail::type_caster<type> caster; |
| 869 | if (!caster.load(obj, false)) |
| 870 | return nullptr; |
Wenzel Jakob | 43398a8 | 2015-07-28 16:12:20 +0200 | [diff] [blame] | 871 | return new buffer_info(((capture *) ptr)->func(caster)); |
| 872 | }, ptr); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 873 | return *this; |
| 874 | } |
| 875 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 876 | template <typename C, typename D, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 877 | class_ &def_readwrite(const char *name, D C::*pm, const Extra&... extra) { |
Wenzel Jakob | 0b48958 | 2016-03-25 16:13:10 +0100 | [diff] [blame] | 878 | cpp_function fget([pm](const C &c) -> const D &{ return c.*pm; }, is_method(*this)), |
| 879 | fset([pm](C &c, const D &value) { c.*pm = value; }, is_method(*this)); |
| 880 | def_property(name, fget, fset, return_value_policy::reference_internal, extra...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 881 | return *this; |
| 882 | } |
| 883 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 884 | template <typename C, typename D, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 885 | 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] | 886 | cpp_function fget([pm](const C &c) -> const D &{ return c.*pm; }, is_method(*this)); |
| 887 | def_property_readonly(name, fget, return_value_policy::reference_internal, extra...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 888 | return *this; |
| 889 | } |
| 890 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 891 | template <typename D, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 892 | class_ &def_readwrite_static(const char *name, D *pm, const Extra& ...extra) { |
Wenzel Jakob | 0b48958 | 2016-03-25 16:13:10 +0100 | [diff] [blame] | 893 | cpp_function fget([pm](object) -> const D &{ return *pm; }, scope(*this)), |
| 894 | fset([pm](object, const D &value) { *pm = value; }, scope(*this)); |
| 895 | def_property_static(name, fget, fset, return_value_policy::reference, extra...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 896 | return *this; |
| 897 | } |
| 898 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 899 | template <typename D, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 900 | 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] | 901 | cpp_function fget([pm](object) -> const D &{ return *pm; }, scope(*this)); |
| 902 | def_property_readonly_static(name, fget, return_value_policy::reference, extra...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 903 | return *this; |
| 904 | } |
| 905 | |
Wenzel Jakob | 84ec78f | 2016-03-21 17:54:24 +0100 | [diff] [blame] | 906 | template <typename... Extra> |
| 907 | class_ &def_property_readonly(const char *name, const cpp_function &fget, const Extra& ...extra) { |
| 908 | def_property(name, fget, cpp_function(), extra...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 909 | return *this; |
| 910 | } |
| 911 | |
Wenzel Jakob | 84ec78f | 2016-03-21 17:54:24 +0100 | [diff] [blame] | 912 | template <typename... Extra> |
| 913 | class_ &def_property_readonly_static(const char *name, const cpp_function &fget, const Extra& ...extra) { |
| 914 | def_property_static(name, fget, cpp_function(), extra...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 915 | return *this; |
| 916 | } |
| 917 | |
Wenzel Jakob | 84ec78f | 2016-03-21 17:54:24 +0100 | [diff] [blame] | 918 | template <typename... Extra> |
| 919 | 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] | 920 | return def_property_static(name, fget, fset, is_method(*this), extra...); |
Wenzel Jakob | 84ec78f | 2016-03-21 17:54:24 +0100 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | template <typename... Extra> |
| 924 | class_ &def_property_static(const char *name, const cpp_function &fget, const cpp_function &fset, const Extra& ...extra) { |
| 925 | auto rec_fget = get_function_record(fget), rec_fset = get_function_record(fset); |
Wenzel Jakob | 7c99ff2 | 2016-06-02 20:33:01 +0200 | [diff] [blame] | 926 | char *doc_prev = rec_fget->doc; /* 'extra' field may include a property-specific documentation string */ |
Wenzel Jakob | 84ec78f | 2016-03-21 17:54:24 +0100 | [diff] [blame] | 927 | detail::process_attributes<Extra...>::init(extra..., rec_fget); |
Wenzel Jakob | 7c99ff2 | 2016-06-02 20:33:01 +0200 | [diff] [blame] | 928 | if (rec_fget->doc && rec_fget->doc != doc_prev) { |
| 929 | free(doc_prev); |
| 930 | rec_fget->doc = strdup(rec_fget->doc); |
| 931 | } |
| 932 | if (rec_fset) { |
| 933 | doc_prev = rec_fset->doc; |
Wenzel Jakob | 84ec78f | 2016-03-21 17:54:24 +0100 | [diff] [blame] | 934 | detail::process_attributes<Extra...>::init(extra..., rec_fset); |
Wenzel Jakob | 7c99ff2 | 2016-06-02 20:33:01 +0200 | [diff] [blame] | 935 | if (rec_fset->doc && rec_fset->doc != doc_prev) { |
| 936 | free(doc_prev); |
| 937 | rec_fset->doc = strdup(rec_fset->doc); |
| 938 | } |
| 939 | } |
Wenzel Jakob | 84ec78f | 2016-03-21 17:54:24 +0100 | [diff] [blame] | 940 | pybind11::str doc_obj = pybind11::str(rec_fget->doc ? rec_fget->doc : ""); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 941 | object property( |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 942 | PyObject_CallFunctionObjArgs((PyObject *) &PyProperty_Type, fget.ptr() ? fget.ptr() : Py_None, |
| 943 | 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] | 944 | if (rec_fget->class_) |
| 945 | attr(name) = property; |
| 946 | else |
| 947 | metaclass().attr(name) = property; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 948 | return *this; |
| 949 | } |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 950 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 951 | private: |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 952 | /// Initialize holder object, variant 1: object derives from enable_shared_from_this |
| 953 | template <typename T> |
| 954 | 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] | 955 | try { |
hbruintjes | 70d2e57 | 2016-07-01 12:39:55 +0200 | [diff] [blame] | 956 | new (&inst->holder) holder_type(std::static_pointer_cast<typename holder_type::element_type>(inst->value->shared_from_this())); |
Wenzel Jakob | 6e213c9 | 2015-11-24 23:05:58 +0100 | [diff] [blame] | 957 | } catch (const std::bad_weak_ptr &) { |
| 958 | new (&inst->holder) holder_type(inst->value); |
| 959 | } |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | /// Initialize holder object, variant 2: try to construct from existing holder object, if possible |
| 963 | template <typename T = holder_type, |
| 964 | typename std::enable_if<std::is_copy_constructible<T>::value, int>::type = 0> |
| 965 | static void init_holder_helper(instance_type *inst, const holder_type *holder_ptr, const void * /* dummy */) { |
| 966 | if (holder_ptr) |
| 967 | new (&inst->holder) holder_type(*holder_ptr); |
| 968 | else |
| 969 | new (&inst->holder) holder_type(inst->value); |
| 970 | } |
| 971 | |
| 972 | /// Initialize holder object, variant 3: holder is not copy constructible (e.g. unique_ptr), always initialize from raw pointer |
| 973 | template <typename T = holder_type, |
| 974 | typename std::enable_if<!std::is_copy_constructible<T>::value, int>::type = 0> |
| 975 | static void init_holder_helper(instance_type *inst, const holder_type * /* unused */, const void * /* dummy */) { |
| 976 | new (&inst->holder) holder_type(inst->value); |
| 977 | } |
| 978 | |
| 979 | /// Initialize holder object of an instance, possibly given a pointer to an existing holder |
| 980 | static void init_holder(PyObject *inst_, const void *holder_ptr) { |
| 981 | auto inst = (instance_type *) inst_; |
| 982 | init_holder_helper(inst, (const holder_type *) holder_ptr, inst->value); |
Wenzel Jakob | 6e213c9 | 2015-11-24 23:05:58 +0100 | [diff] [blame] | 983 | inst->constructed = true; |
| 984 | } |
| 985 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 986 | static void dealloc(PyObject *inst_) { |
| 987 | instance_type *inst = (instance_type *) inst_; |
| 988 | if (inst->owned) { |
| 989 | if (inst->constructed) |
| 990 | inst->holder.~holder_type(); |
| 991 | else |
| 992 | ::operator delete(inst->value); |
| 993 | } |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 994 | generic_type::dealloc((detail::instance<void> *) inst); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 995 | } |
Wenzel Jakob | 84ec78f | 2016-03-21 17:54:24 +0100 | [diff] [blame] | 996 | |
| 997 | static detail::function_record *get_function_record(handle h) { |
| 998 | h = detail::get_function(h); |
| 999 | return h ? (detail::function_record *) capsule( |
| 1000 | PyCFunction_GetSelf(h.ptr()), true) : nullptr; |
| 1001 | } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1002 | }; |
| 1003 | |
| 1004 | /// Binds C++ enumerations and enumeration classes to Python |
| 1005 | template <typename Type> class enum_ : public class_<Type> { |
| 1006 | public: |
Pim Schellart | e5b42ef | 2016-08-02 10:58:32 -0400 | [diff] [blame^] | 1007 | using UnderlyingType = typename std::underlying_type<Type>::type; |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 1008 | template <typename... Extra> |
| 1009 | enum_(const handle &scope, const char *name, const Extra&... extra) |
| 1010 | : class_<Type>(scope, name, extra...), m_parent(scope) { |
Pim Schellart | e5b42ef | 2016-08-02 10:58:32 -0400 | [diff] [blame^] | 1011 | auto entries = new std::unordered_map<UnderlyingType, const char *>(); |
Wenzel Jakob | 8456a4d | 2015-10-13 02:42:20 +0200 | [diff] [blame] | 1012 | this->def("__repr__", [name, entries](Type value) -> std::string { |
Pim Schellart | e5b42ef | 2016-08-02 10:58:32 -0400 | [diff] [blame^] | 1013 | auto it = entries->find((UnderlyingType) value); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1014 | return std::string(name) + "." + |
| 1015 | ((it == entries->end()) ? std::string("???") |
| 1016 | : std::string(it->second)); |
| 1017 | }); |
Pim Schellart | e5b42ef | 2016-08-02 10:58:32 -0400 | [diff] [blame^] | 1018 | this->def("__init__", [](Type& value, UnderlyingType i) { value = (Type)i; }); |
| 1019 | this->def("__init__", [](Type& value, UnderlyingType i) { new (&value) Type((Type) i); }); |
| 1020 | this->def("__int__", [](Type value) { return (UnderlyingType) value; }); |
Wenzel Jakob | 6fb4849 | 2016-05-01 12:45:38 +0200 | [diff] [blame] | 1021 | this->def("__eq__", [](const Type &value, Type *value2) { return value2 && value == *value2; }); |
Pim Schellart | e5b42ef | 2016-08-02 10:58:32 -0400 | [diff] [blame^] | 1022 | this->def("__eq__", [](const Type &value, UnderlyingType value2) { return value2 && value == value2; }); |
Wenzel Jakob | 6fb4849 | 2016-05-01 12:45:38 +0200 | [diff] [blame] | 1023 | this->def("__ne__", [](const Type &value, Type *value2) { return !value2 || value != *value2; }); |
Pim Schellart | e5b42ef | 2016-08-02 10:58:32 -0400 | [diff] [blame^] | 1024 | this->def("__ne__", [](const Type &value, UnderlyingType value2) { return value != value2; }); |
| 1025 | this->def("__hash__", [](const Type &value) { return (UnderlyingType) value; }); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1026 | m_entries = entries; |
| 1027 | } |
| 1028 | |
| 1029 | /// Export enumeration entries into the parent scope |
| 1030 | void export_values() { |
| 1031 | PyObject *dict = ((PyTypeObject *) this->m_ptr)->tp_dict; |
| 1032 | PyObject *key, *value; |
Wenzel Jakob | 27e8e10 | 2016-01-17 22:36:37 +0100 | [diff] [blame] | 1033 | ssize_t pos = 0; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1034 | while (PyDict_Next(dict, &pos, &key, &value)) |
| 1035 | if (PyObject_IsInstance(value, this->m_ptr)) |
| 1036 | m_parent.attr(key) = value; |
| 1037 | } |
| 1038 | |
| 1039 | /// Add an enumeration entry |
| 1040 | enum_& value(char const* name, Type value) { |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 1041 | this->attr(name) = pybind11::cast(value, return_value_policy::copy); |
Pim Schellart | e5b42ef | 2016-08-02 10:58:32 -0400 | [diff] [blame^] | 1042 | (*m_entries)[(UnderlyingType) value] = name; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1043 | return *this; |
| 1044 | } |
| 1045 | private: |
Pim Schellart | e5b42ef | 2016-08-02 10:58:32 -0400 | [diff] [blame^] | 1046 | std::unordered_map<UnderlyingType, const char *> *m_entries; |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 1047 | handle m_parent; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1048 | }; |
| 1049 | |
| 1050 | NAMESPACE_BEGIN(detail) |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 1051 | template <typename... Args> struct init { |
Wenzel Jakob | 86d825f | 2016-05-26 13:19:27 +0200 | [diff] [blame] | 1052 | template <typename Base, typename Holder, typename Alias, typename... Extra, |
| 1053 | typename std::enable_if<std::is_same<Base, Alias>::value, int>::type = 0> |
| 1054 | void execute(pybind11::class_<Base, Holder, Alias> &class_, const Extra&... extra) const { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1055 | /// Function which calls a specific C++ in-place constructor |
Wenzel Jakob | 69e1a5c | 2016-05-26 14:29:31 +0200 | [diff] [blame] | 1056 | class_.def("__init__", [](Base *self_, Args... args) { new (self_) Base(args...); }, extra...); |
Wenzel Jakob | 86d825f | 2016-05-26 13:19:27 +0200 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | template <typename Base, typename Holder, typename Alias, typename... Extra, |
| 1060 | typename std::enable_if<!std::is_same<Base, Alias>::value && |
| 1061 | std::is_constructible<Base, Args...>::value, int>::type = 0> |
| 1062 | void execute(pybind11::class_<Base, Holder, Alias> &class_, const Extra&... extra) const { |
| 1063 | handle cl_type = class_; |
Wenzel Jakob | 69e1a5c | 2016-05-26 14:29:31 +0200 | [diff] [blame] | 1064 | class_.def("__init__", [cl_type](handle self_, Args... args) { |
| 1065 | if (self_.get_type() == cl_type) |
| 1066 | new (self_.cast<Base *>()) Base(args...); |
Wenzel Jakob | 86d825f | 2016-05-26 13:19:27 +0200 | [diff] [blame] | 1067 | else |
Wenzel Jakob | 69e1a5c | 2016-05-26 14:29:31 +0200 | [diff] [blame] | 1068 | new (self_.cast<Alias *>()) Alias(args...); |
Wenzel Jakob | 86d825f | 2016-05-26 13:19:27 +0200 | [diff] [blame] | 1069 | }, extra...); |
| 1070 | } |
| 1071 | |
| 1072 | template <typename Base, typename Holder, typename Alias, typename... Extra, |
| 1073 | typename std::enable_if<!std::is_same<Base, Alias>::value && |
| 1074 | !std::is_constructible<Base, Args...>::value, int>::type = 0> |
| 1075 | void execute(pybind11::class_<Base, Holder, Alias> &class_, const Extra&... extra) const { |
Wenzel Jakob | 2353b9b | 2016-06-27 16:05:46 +0200 | [diff] [blame] | 1076 | class_.def("__init__", [](Alias *self_, Args... args) { new (self_) Alias(args...); }, extra...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1077 | } |
| 1078 | }; |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 1079 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 1080 | 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] | 1081 | /* Clever approach based on weak references taken from Boost.Python */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 1082 | handle nurse (Nurse > 0 ? PyTuple_GetItem(args.ptr(), Nurse - 1) : ret.ptr()); |
| 1083 | handle patient(Patient > 0 ? PyTuple_GetItem(args.ptr(), Patient - 1) : ret.ptr()); |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 1084 | |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 1085 | if (!nurse || !patient) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 1086 | pybind11_fail("Could not activate keep_alive!"); |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 1087 | |
Wenzel Jakob | 9b880ba | 2016-04-25 03:25:13 +0200 | [diff] [blame] | 1088 | if (patient.ptr() == Py_None) |
| 1089 | return; /* Nothing to keep alive */ |
| 1090 | |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 1091 | cpp_function disable_lifesupport( |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 1092 | [patient](handle weakref) { patient.dec_ref(); weakref.dec_ref(); }); |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 1093 | |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 1094 | weakref wr(nurse, disable_lifesupport); |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 1095 | |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 1096 | patient.inc_ref(); /* reference patient and leak the weak reference */ |
| 1097 | (void) wr.release(); |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 1098 | } |
| 1099 | |
Ivan Smirnov | daed1ab | 2016-06-17 22:29:39 +0100 | [diff] [blame] | 1100 | template <typename Iterator> struct iterator_state { |
| 1101 | Iterator it, end; |
| 1102 | bool first; |
| 1103 | }; |
Wenzel Jakob | b282595 | 2016-04-13 23:33:00 +0200 | [diff] [blame] | 1104 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1105 | NAMESPACE_END(detail) |
| 1106 | |
Ben Pritchard | 2de6e1d | 2016-02-18 13:20:15 -0500 | [diff] [blame] | 1107 | template <typename... Args> detail::init<Args...> init() { return detail::init<Args...>(); } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1108 | |
Wenzel Jakob | 5dd33d8 | 2016-05-30 11:28:21 +0200 | [diff] [blame] | 1109 | template <typename Iterator, |
| 1110 | typename ValueType = decltype(*std::declval<Iterator>()), |
| 1111 | typename... Extra> |
| 1112 | iterator make_iterator(Iterator first, Iterator last, Extra &&... extra) { |
Wenzel Jakob | b282595 | 2016-04-13 23:33:00 +0200 | [diff] [blame] | 1113 | typedef detail::iterator_state<Iterator> state; |
| 1114 | |
| 1115 | if (!detail::get_type_info(typeid(state))) { |
| 1116 | class_<state>(handle(), "") |
| 1117 | .def("__iter__", [](state &s) -> state& { return s; }) |
Wenzel Jakob | 5dd33d8 | 2016-05-30 11:28:21 +0200 | [diff] [blame] | 1118 | .def("__next__", [](state &s) -> ValueType { |
Ivan Smirnov | daed1ab | 2016-06-17 22:29:39 +0100 | [diff] [blame] | 1119 | if (!s.first) |
| 1120 | ++s.it; |
| 1121 | else |
| 1122 | s.first = false; |
Wenzel Jakob | b282595 | 2016-04-13 23:33:00 +0200 | [diff] [blame] | 1123 | if (s.it == s.end) |
| 1124 | throw stop_iteration(); |
Ivan Smirnov | daed1ab | 2016-06-17 22:29:39 +0100 | [diff] [blame] | 1125 | return *s.it; |
Wenzel Jakob | b282595 | 2016-04-13 23:33:00 +0200 | [diff] [blame] | 1126 | }, return_value_policy::reference_internal, std::forward<Extra>(extra)...); |
| 1127 | } |
| 1128 | |
Ivan Smirnov | daed1ab | 2016-06-17 22:29:39 +0100 | [diff] [blame] | 1129 | return (iterator) cast(state { first, last, true }); |
Wenzel Jakob | b282595 | 2016-04-13 23:33:00 +0200 | [diff] [blame] | 1130 | } |
| 1131 | |
Wenzel Jakob | bf0c7dc | 2016-04-18 10:52:12 +0200 | [diff] [blame] | 1132 | template <typename Type, typename... Extra> iterator make_iterator(Type &value, Extra&&... extra) { |
| 1133 | return make_iterator(std::begin(value), std::end(value), extra...); |
| 1134 | } |
| 1135 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1136 | template <typename InputType, typename OutputType> void implicitly_convertible() { |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 1137 | auto implicit_caster = [](PyObject *obj, PyTypeObject *type) -> PyObject * { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1138 | if (!detail::type_caster<InputType>().load(obj, false)) |
| 1139 | return nullptr; |
| 1140 | tuple args(1); |
| 1141 | args[0] = obj; |
| 1142 | PyObject *result = PyObject_Call((PyObject *) type, args.ptr(), nullptr); |
| 1143 | if (result == nullptr) |
| 1144 | PyErr_Clear(); |
| 1145 | return result; |
| 1146 | }; |
Wenzel Jakob | b6cf75d | 2016-01-29 11:39:32 +0100 | [diff] [blame] | 1147 | auto ®istered_types = detail::get_internals().registered_types_cpp; |
| 1148 | auto it = registered_types.find(std::type_index(typeid(OutputType))); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1149 | if (it == registered_types.end()) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 1150 | pybind11_fail("implicitly_convertible: Unable to find type " + type_id<OutputType>()); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 1151 | ((detail::type_info *) it->second)->implicit_conversions.push_back(implicit_caster); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1152 | } |
| 1153 | |
Pim Schellart | 5a7d17f | 2016-06-17 17:35:59 -0400 | [diff] [blame] | 1154 | template <typename ExceptionTranslator> |
| 1155 | void register_exception_translator(ExceptionTranslator&& translator) { |
| 1156 | detail::get_internals().registered_exception_translators.push_front( |
| 1157 | std::forward<ExceptionTranslator>(translator)); |
| 1158 | } |
| 1159 | |
| 1160 | /* Wrapper to generate a new Python exception type. |
| 1161 | * |
| 1162 | * This should only be used with PyErr_SetString for now. |
| 1163 | * It is not (yet) possible to use as a py::base. |
| 1164 | * Template type argument is reserved for future use. |
| 1165 | */ |
| 1166 | template <typename type> |
| 1167 | class exception : public object { |
| 1168 | public: |
| 1169 | exception(module &m, const std::string name, PyObject* base=PyExc_Exception) { |
| 1170 | std::string full_name = std::string(PyModule_GetName(m.ptr())) |
| 1171 | + std::string(".") + name; |
| 1172 | char* exception_name = const_cast<char*>(full_name.c_str()); |
| 1173 | m_ptr = PyErr_NewException(exception_name, base, NULL); |
| 1174 | inc_ref(); // PyModule_AddObject() steals a reference |
| 1175 | PyModule_AddObject(m.ptr(), name.c_str(), m_ptr); |
| 1176 | } |
| 1177 | }; |
| 1178 | |
Felipe Lema | 2547ca4 | 2016-01-25 16:22:44 -0300 | [diff] [blame] | 1179 | #if defined(WITH_THREAD) |
Wenzel Jakob | 39e97e6 | 2016-04-25 03:26:15 +0200 | [diff] [blame] | 1180 | |
| 1181 | /* The functions below essentially reproduce the PyGILState_* API using a RAII |
| 1182 | * pattern, but there are a few important differences: |
| 1183 | * |
| 1184 | * 1. When acquiring the GIL from an non-main thread during the finalization |
| 1185 | * phase, the GILState API blindly terminates the calling thread, which |
| 1186 | * is often not what is wanted. This API does not do this. |
| 1187 | * |
| 1188 | * 2. The gil_scoped_release function can optionally cut the relationship |
| 1189 | * of a PyThreadState and its associated thread, which allows moving it to |
| 1190 | * another thread (this is a fairly rare/advanced use case). |
| 1191 | * |
| 1192 | * 3. The reference count of an acquired thread state can be controlled. This |
| 1193 | * can be handy to prevent cases where callbacks issued from an external |
Wenzel Jakob | fbafdea | 2016-04-25 15:02:43 +0200 | [diff] [blame] | 1194 | * thread would otherwise constantly construct and destroy thread state data |
| 1195 | * structures. |
Wenzel Jakob | 69e1a5c | 2016-05-26 14:29:31 +0200 | [diff] [blame] | 1196 | * |
| 1197 | * See the Python bindings of NanoGUI (http://github.com/wjakob/nanogui) for an |
| 1198 | * example which uses features 2 and 3 to migrate the Python thread of |
| 1199 | * execution to another thread (to run the event loop on the original thread, |
| 1200 | * in this case). |
Wenzel Jakob | fbafdea | 2016-04-25 15:02:43 +0200 | [diff] [blame] | 1201 | */ |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1202 | |
| 1203 | class gil_scoped_acquire { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1204 | public: |
Wenzel Jakob | fbafdea | 2016-04-25 15:02:43 +0200 | [diff] [blame] | 1205 | PYBIND11_NOINLINE gil_scoped_acquire() { |
Wenzel Jakob | 39e97e6 | 2016-04-25 03:26:15 +0200 | [diff] [blame] | 1206 | auto const &internals = detail::get_internals(); |
| 1207 | tstate = (PyThreadState *) PyThread_get_key_value(internals.tstate); |
| 1208 | |
| 1209 | if (!tstate) { |
| 1210 | tstate = PyThreadState_New(internals.istate); |
| 1211 | #if !defined(NDEBUG) |
| 1212 | if (!tstate) |
| 1213 | pybind11_fail("scoped_acquire: could not create thread state!"); |
| 1214 | #endif |
| 1215 | tstate->gilstate_counter = 0; |
Wenzel Jakob | fbafdea | 2016-04-25 15:02:43 +0200 | [diff] [blame] | 1216 | #if PY_MAJOR_VERSION < 3 |
| 1217 | PyThread_delete_key_value(internals.tstate); |
| 1218 | #endif |
Wenzel Jakob | 39e97e6 | 2016-04-25 03:26:15 +0200 | [diff] [blame] | 1219 | PyThread_set_key_value(internals.tstate, tstate); |
| 1220 | } else { |
Wenzel Jakob | fbafdea | 2016-04-25 15:02:43 +0200 | [diff] [blame] | 1221 | release = detail::get_thread_state_unchecked() != tstate; |
Wenzel Jakob | 39e97e6 | 2016-04-25 03:26:15 +0200 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | if (release) { |
Wenzel Jakob | 39e97e6 | 2016-04-25 03:26:15 +0200 | [diff] [blame] | 1225 | /* Work around an annoying assertion in PyThreadState_Swap */ |
Wenzel Jakob | fbafdea | 2016-04-25 15:02:43 +0200 | [diff] [blame] | 1226 | #if defined(Py_DEBUG) |
| 1227 | PyInterpreterState *interp = tstate->interp; |
| 1228 | tstate->interp = nullptr; |
| 1229 | #endif |
Wenzel Jakob | 39e97e6 | 2016-04-25 03:26:15 +0200 | [diff] [blame] | 1230 | PyEval_AcquireThread(tstate); |
Wenzel Jakob | fbafdea | 2016-04-25 15:02:43 +0200 | [diff] [blame] | 1231 | #if defined(Py_DEBUG) |
| 1232 | tstate->interp = interp; |
| 1233 | #endif |
Wenzel Jakob | 39e97e6 | 2016-04-25 03:26:15 +0200 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | inc_ref(); |
| 1237 | } |
| 1238 | |
| 1239 | void inc_ref() { |
| 1240 | ++tstate->gilstate_counter; |
| 1241 | } |
| 1242 | |
Wenzel Jakob | fbafdea | 2016-04-25 15:02:43 +0200 | [diff] [blame] | 1243 | PYBIND11_NOINLINE void dec_ref() { |
Wenzel Jakob | 39e97e6 | 2016-04-25 03:26:15 +0200 | [diff] [blame] | 1244 | --tstate->gilstate_counter; |
| 1245 | #if !defined(NDEBUG) |
Wenzel Jakob | fbafdea | 2016-04-25 15:02:43 +0200 | [diff] [blame] | 1246 | if (detail::get_thread_state_unchecked() != tstate) |
Wenzel Jakob | 39e97e6 | 2016-04-25 03:26:15 +0200 | [diff] [blame] | 1247 | pybind11_fail("scoped_acquire::dec_ref(): thread state must be current!"); |
| 1248 | if (tstate->gilstate_counter < 0) |
| 1249 | pybind11_fail("scoped_acquire::dec_ref(): reference count underflow!"); |
| 1250 | #endif |
| 1251 | if (tstate->gilstate_counter == 0) { |
| 1252 | #if !defined(NDEBUG) |
| 1253 | if (!release) |
| 1254 | pybind11_fail("scoped_acquire::dec_ref(): internal error!"); |
| 1255 | #endif |
| 1256 | PyThreadState_Clear(tstate); |
| 1257 | PyThreadState_DeleteCurrent(); |
Wenzel Jakob | fbafdea | 2016-04-25 15:02:43 +0200 | [diff] [blame] | 1258 | PyThread_delete_key_value(detail::get_internals().tstate); |
Wenzel Jakob | 39e97e6 | 2016-04-25 03:26:15 +0200 | [diff] [blame] | 1259 | release = false; |
| 1260 | } |
| 1261 | } |
| 1262 | |
Wenzel Jakob | fbafdea | 2016-04-25 15:02:43 +0200 | [diff] [blame] | 1263 | PYBIND11_NOINLINE ~gil_scoped_acquire() { |
Wenzel Jakob | 39e97e6 | 2016-04-25 03:26:15 +0200 | [diff] [blame] | 1264 | dec_ref(); |
| 1265 | if (release) |
| 1266 | PyEval_SaveThread(); |
| 1267 | } |
| 1268 | private: |
| 1269 | PyThreadState *tstate = nullptr; |
| 1270 | bool release = true; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1271 | }; |
| 1272 | |
| 1273 | class gil_scoped_release { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1274 | public: |
Wenzel Jakob | 39e97e6 | 2016-04-25 03:26:15 +0200 | [diff] [blame] | 1275 | gil_scoped_release(bool disassoc = false) : disassoc(disassoc) { |
| 1276 | tstate = PyEval_SaveThread(); |
Wenzel Jakob | 2f6662e | 2016-04-25 09:16:41 +0200 | [diff] [blame] | 1277 | if (disassoc) { |
Boris Schäling | 20ee935 | 2016-05-28 12:26:18 +0200 | [diff] [blame] | 1278 | auto key = detail::get_internals().tstate; |
Wenzel Jakob | 2f6662e | 2016-04-25 09:16:41 +0200 | [diff] [blame] | 1279 | #if PY_MAJOR_VERSION < 3 |
| 1280 | PyThread_delete_key_value(key); |
| 1281 | #else |
| 1282 | PyThread_set_key_value(key, nullptr); |
| 1283 | #endif |
| 1284 | } |
Wenzel Jakob | 39e97e6 | 2016-04-25 03:26:15 +0200 | [diff] [blame] | 1285 | } |
| 1286 | ~gil_scoped_release() { |
| 1287 | if (!tstate) |
| 1288 | return; |
| 1289 | PyEval_RestoreThread(tstate); |
Wenzel Jakob | 2f6662e | 2016-04-25 09:16:41 +0200 | [diff] [blame] | 1290 | if (disassoc) { |
Boris Schäling | 20ee935 | 2016-05-28 12:26:18 +0200 | [diff] [blame] | 1291 | auto key = detail::get_internals().tstate; |
Wenzel Jakob | 2f6662e | 2016-04-25 09:16:41 +0200 | [diff] [blame] | 1292 | #if PY_MAJOR_VERSION < 3 |
| 1293 | PyThread_delete_key_value(key); |
| 1294 | #endif |
| 1295 | PyThread_set_key_value(key, tstate); |
| 1296 | } |
Wenzel Jakob | 39e97e6 | 2016-04-25 03:26:15 +0200 | [diff] [blame] | 1297 | } |
| 1298 | private: |
| 1299 | PyThreadState *tstate; |
| 1300 | bool disassoc; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1301 | }; |
Wenzel Jakob | 39e97e6 | 2016-04-25 03:26:15 +0200 | [diff] [blame] | 1302 | #else |
| 1303 | class gil_scoped_acquire { }; |
| 1304 | class gil_scoped_release { }; |
Felipe Lema | 2547ca4 | 2016-01-25 16:22:44 -0300 | [diff] [blame] | 1305 | #endif |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1306 | |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1307 | inline function get_overload(const void *this_ptr, const char *name) { |
| 1308 | handle py_object = detail::get_object_handle(this_ptr); |
Wenzel Jakob | ac0fde9 | 2015-10-01 18:37:26 +0200 | [diff] [blame] | 1309 | if (!py_object) |
| 1310 | return function(); |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1311 | handle type = py_object.get_type(); |
| 1312 | auto key = std::make_pair(type.ptr(), name); |
| 1313 | |
Wenzel Jakob | f5c154a | 2016-04-11 18:13:08 +0200 | [diff] [blame] | 1314 | /* Cache functions that aren't overloaded in Python to avoid |
| 1315 | many costly Python dictionary lookups below */ |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1316 | auto &cache = detail::get_internals().inactive_overload_cache; |
| 1317 | if (cache.find(key) != cache.end()) |
| 1318 | return function(); |
| 1319 | |
| 1320 | function overload = (function) py_object.attr(name); |
| 1321 | if (overload.is_cpp_function()) { |
| 1322 | cache.insert(key); |
| 1323 | return function(); |
| 1324 | } |
Wenzel Jakob | 27e8e10 | 2016-01-17 22:36:37 +0100 | [diff] [blame] | 1325 | |
Wenzel Jakob | f5c154a | 2016-04-11 18:13:08 +0200 | [diff] [blame] | 1326 | /* Don't call dispatch code if invoked from overridden function */ |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1327 | PyFrameObject *frame = PyThreadState_Get()->frame; |
Wenzel Jakob | b2b44a9 | 2016-04-15 17:50:40 +0200 | [diff] [blame] | 1328 | 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] | 1329 | frame->f_code->co_argcount > 0) { |
| 1330 | PyFrame_FastToLocals(frame); |
| 1331 | PyObject *self_caller = PyDict_GetItem( |
| 1332 | frame->f_locals, PyTuple_GET_ITEM(frame->f_code->co_varnames, 0)); |
| 1333 | if (self_caller == py_object.ptr()) |
| 1334 | return function(); |
| 1335 | } |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1336 | return overload; |
| 1337 | } |
| 1338 | |
Wenzel Jakob | 1e3be73 | 2016-05-24 23:42:05 +0200 | [diff] [blame] | 1339 | #define PYBIND11_OVERLOAD_INT(ret_type, name, ...) { \ |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 1340 | pybind11::gil_scoped_acquire gil; \ |
Wenzel Jakob | 1e3be73 | 2016-05-24 23:42:05 +0200 | [diff] [blame] | 1341 | pybind11::function overload = pybind11::get_overload(this, name); \ |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1342 | if (overload) \ |
Wenzel Jakob | 6c03beb | 2016-05-08 14:34:09 +0200 | [diff] [blame] | 1343 | return overload(__VA_ARGS__).template cast<ret_type>(); } |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1344 | |
Wenzel Jakob | 1e3be73 | 2016-05-24 23:42:05 +0200 | [diff] [blame] | 1345 | #define PYBIND11_OVERLOAD_NAME(ret_type, cname, name, fn, ...) \ |
| 1346 | PYBIND11_OVERLOAD_INT(ret_type, name, __VA_ARGS__) \ |
| 1347 | return cname::fn(__VA_ARGS__) |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1348 | |
Wenzel Jakob | 1e3be73 | 2016-05-24 23:42:05 +0200 | [diff] [blame] | 1349 | #define PYBIND11_OVERLOAD_PURE_NAME(ret_type, cname, name, fn, ...) \ |
| 1350 | PYBIND11_OVERLOAD_INT(ret_type, name, __VA_ARGS__) \ |
| 1351 | pybind11::pybind11_fail("Tried to call pure virtual function \"" #cname "::" name "\""); |
| 1352 | |
| 1353 | #define PYBIND11_OVERLOAD(ret_type, cname, fn, ...) \ |
| 1354 | PYBIND11_OVERLOAD_NAME(ret_type, cname, #fn, fn, __VA_ARGS__) |
| 1355 | |
| 1356 | #define PYBIND11_OVERLOAD_PURE(ret_type, cname, fn, ...) \ |
| 1357 | PYBIND11_OVERLOAD_PURE_NAME(ret_type, cname, #fn, fn, __VA_ARGS__) |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1358 | |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 1359 | NAMESPACE_END(pybind11) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1360 | |
| 1361 | #if defined(_MSC_VER) |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 1362 | # pragma warning(pop) |
Ben Pritchard | 33f3430 | 2016-02-18 15:25:51 -0500 | [diff] [blame] | 1363 | #elif defined(__ICC) || defined(__INTEL_COMPILER) |
| 1364 | # pragma warning(pop) |
Wenzel Jakob | 6d25296 | 2016-05-01 20:47:49 +0200 | [diff] [blame] | 1365 | #elif defined(__GNUG__) && !defined(__clang__) |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 1366 | # pragma GCC diagnostic pop |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1367 | #endif |