cppyabm  1.0.17
An agent-based library to integrate C++ and Python
internals.h
Go to the documentation of this file.
1 /*
2  pybind11/detail/internals.h: Internal data structure and related functions
3 
4  Copyright (c) 2017 Wenzel Jakob <wenzel.jakob@epfl.ch>
5 
6  All rights reserved. Use of this source code is governed by a
7  BSD-style license that can be found in the LICENSE file.
8 */
9 
10 #pragma once
11 
12 #include "../pytypes.h"
13 
16 // Forward declarations
17 inline PyTypeObject *make_static_property_type();
18 inline PyTypeObject *make_default_metaclass();
19 inline PyObject *make_object_base_type(PyTypeObject *metaclass);
20 
21 // The old Python Thread Local Storage (TLS) API is deprecated in Python 3.7 in favor of the new
22 // Thread Specific Storage (TSS) API.
23 #if PY_VERSION_HEX >= 0x03070000
24 # define PYBIND11_TLS_KEY_INIT(var) Py_tss_t *var = nullptr
25 # define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get((key))
26 # define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set((key), (value))
27 # define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set((key), nullptr)
28 # define PYBIND11_TLS_FREE(key) PyThread_tss_free(key)
29 #else
30  // Usually an int but a long on Cygwin64 with Python 3.x
31 # define PYBIND11_TLS_KEY_INIT(var) decltype(PyThread_create_key()) var = 0
32 # define PYBIND11_TLS_GET_VALUE(key) PyThread_get_key_value((key))
33 # if PY_MAJOR_VERSION < 3
34 # define PYBIND11_TLS_DELETE_VALUE(key) \
35  PyThread_delete_key_value(key)
36 # define PYBIND11_TLS_REPLACE_VALUE(key, value) \
37  do { \
38  PyThread_delete_key_value((key)); \
39  PyThread_set_key_value((key), (value)); \
40  } while (false)
41 # else
42 # define PYBIND11_TLS_DELETE_VALUE(key) \
43  PyThread_set_key_value((key), nullptr)
44 # define PYBIND11_TLS_REPLACE_VALUE(key, value) \
45  PyThread_set_key_value((key), (value))
46 # endif
47 # define PYBIND11_TLS_FREE(key) (void)key
48 #endif
49 
50 // Python loads modules by default with dlopen with the RTLD_LOCAL flag; under libc++ and possibly
51 // other STLs, this means `typeid(A)` from one module won't equal `typeid(A)` from another module
52 // even when `A` is the same, non-hidden-visibility type (e.g. from a common include). Under
53 // libstdc++, this doesn't happen: equality and the type_index hash are based on the type name,
54 // which works. If not under a known-good stl, provide our own name-based hash and equality
55 // functions that use the type name.
56 #if defined(__GLIBCXX__)
57 inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) { return lhs == rhs; }
58 using type_hash = std::hash<std::type_index>;
59 using type_equal_to = std::equal_to<std::type_index>;
60 #else
61 inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) {
62  return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
63 }
64 
65 struct type_hash {
66  size_t operator()(const std::type_index &t) const {
67  size_t hash = 5381;
68  const char *ptr = t.name();
69  while (auto c = static_cast<unsigned char>(*ptr++))
70  hash = (hash * 33) ^ c;
71  return hash;
72  }
73 };
74 
75 struct type_equal_to {
76  bool operator()(const std::type_index &lhs, const std::type_index &rhs) const {
77  return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
78  }
79 };
80 #endif
81 
82 template <typename value_type>
83 using type_map = std::unordered_map<std::type_index, value_type, type_hash, type_equal_to>;
84 
85 struct override_hash {
86  inline size_t operator()(const std::pair<const PyObject *, const char *>& v) const {
87  size_t value = std::hash<const void *>()(v.first);
88  value ^= std::hash<const void *>()(v.second) + 0x9e3779b9 + (value<<6) + (value>>2);
89  return value;
90  }
91 };
92 
93 /// Internal data structure used to track registered instances and types.
94 /// Whenever binary incompatible changes are made to this structure,
95 /// `PYBIND11_INTERNALS_VERSION` must be incremented.
96 struct internals {
97  type_map<type_info *> registered_types_cpp; // std::type_index -> pybind11's type information
98  std::unordered_map<PyTypeObject *, std::vector<type_info *>> registered_types_py; // PyTypeObject* -> base type_info(s)
99  std::unordered_multimap<const void *, instance*> registered_instances; // void * -> instance*
100  std::unordered_set<std::pair<const PyObject *, const char *>, override_hash> inactive_override_cache;
101  type_map<std::vector<bool (*)(PyObject *, void *&)>> direct_conversions;
102  std::unordered_map<const PyObject *, std::vector<PyObject *>> patients;
103  std::forward_list<void (*) (std::exception_ptr)> registered_exception_translators;
104  std::unordered_map<std::string, void *> shared_data; // Custom data to be shared across extensions
105  std::vector<PyObject *> loader_patient_stack; // Used by `loader_life_support`
106  std::forward_list<std::string> static_strings; // Stores the std::strings backing detail::c_str()
107  PyTypeObject *static_property_type;
108  PyTypeObject *default_metaclass;
109  PyObject *instance_base;
110 #if defined(WITH_THREAD)
111  PYBIND11_TLS_KEY_INIT(tstate);
112  PyInterpreterState *istate = nullptr;
113  ~internals() {
114  // This destructor is called *after* Py_Finalize() in finalize_interpreter().
115  // That *SHOULD BE* fine. The following details what happens when PyThread_tss_free is called.
116  // PYBIND11_TLS_FREE is PyThread_tss_free on python 3.7+. On older python, it does nothing.
117  // PyThread_tss_free calls PyThread_tss_delete and PyMem_RawFree.
118  // PyThread_tss_delete just calls TlsFree (on Windows) or pthread_key_delete (on *NIX). Neither
119  // of those have anything to do with CPython internals.
120  // PyMem_RawFree *requires* that the `tstate` be allocated with the CPython allocator.
121  PYBIND11_TLS_FREE(tstate);
122  }
123 #endif
124 };
125 
126 /// Additional type information which does not fit into the PyTypeObject.
127 /// Changes to this struct also require bumping `PYBIND11_INTERNALS_VERSION`.
128 struct type_info {
129  PyTypeObject *type;
130  const std::type_info *cpptype;
132  void *(*operator_new)(size_t);
133  void (*init_instance)(instance *, const void *);
134  void (*dealloc)(value_and_holder &v_h);
135  std::vector<PyObject *(*)(PyObject *, PyTypeObject *)> implicit_conversions;
136  std::vector<std::pair<const std::type_info *, void *(*)(void *)>> implicit_casts;
137  std::vector<bool (*)(PyObject *, void *&)> *direct_conversions;
138  buffer_info *(*get_buffer)(PyObject *, void *) = nullptr;
139  void *get_buffer_data = nullptr;
140  void *(*module_local_load)(PyObject *, const type_info *) = nullptr;
141  /* A simple type never occurs as a (direct or indirect) parent
142  * of a class that makes use of multiple inheritance */
143  bool simple_type : 1;
144  /* True if there is no multiple inheritance in this type's inheritance tree */
146  /* for base vs derived holder_type checks */
147  bool default_holder : 1;
148  /* true if this is a type registered with py::module_local */
149  bool module_local : 1;
150 };
151 
152 /// Tracks the `internals` and `type_info` ABI version independent of the main library version
153 #define PYBIND11_INTERNALS_VERSION 4
154 
155 /// On MSVC, debug and release builds are not ABI-compatible!
156 #if defined(_MSC_VER) && defined(_DEBUG)
157 # define PYBIND11_BUILD_TYPE "_debug"
158 #else
159 # define PYBIND11_BUILD_TYPE ""
160 #endif
161 
162 /// Let's assume that different compilers are ABI-incompatible.
163 /// A user can manually set this string if they know their
164 /// compiler is compatible.
165 #ifndef PYBIND11_COMPILER_TYPE
166 # if defined(_MSC_VER)
167 # define PYBIND11_COMPILER_TYPE "_msvc"
168 # elif defined(__INTEL_COMPILER)
169 # define PYBIND11_COMPILER_TYPE "_icc"
170 # elif defined(__clang__)
171 # define PYBIND11_COMPILER_TYPE "_clang"
172 # elif defined(__PGI)
173 # define PYBIND11_COMPILER_TYPE "_pgi"
174 # elif defined(__MINGW32__)
175 # define PYBIND11_COMPILER_TYPE "_mingw"
176 # elif defined(__CYGWIN__)
177 # define PYBIND11_COMPILER_TYPE "_gcc_cygwin"
178 # elif defined(__GNUC__)
179 # define PYBIND11_COMPILER_TYPE "_gcc"
180 # else
181 # define PYBIND11_COMPILER_TYPE "_unknown"
182 # endif
183 #endif
184 
185 /// Also standard libs
186 #ifndef PYBIND11_STDLIB
187 # if defined(_LIBCPP_VERSION)
188 # define PYBIND11_STDLIB "_libcpp"
189 # elif defined(__GLIBCXX__) || defined(__GLIBCPP__)
190 # define PYBIND11_STDLIB "_libstdcpp"
191 # else
192 # define PYBIND11_STDLIB ""
193 # endif
194 #endif
195 
196 /// On Linux/OSX, changes in __GXX_ABI_VERSION__ indicate ABI incompatibility.
197 #ifndef PYBIND11_BUILD_ABI
198 # if defined(__GXX_ABI_VERSION)
199 # define PYBIND11_BUILD_ABI "_cxxabi" PYBIND11_TOSTRING(__GXX_ABI_VERSION)
200 # else
201 # define PYBIND11_BUILD_ABI ""
202 # endif
203 #endif
204 
205 #ifndef PYBIND11_INTERNALS_KIND
206 # if defined(WITH_THREAD)
207 # define PYBIND11_INTERNALS_KIND ""
208 # else
209 # define PYBIND11_INTERNALS_KIND "_without_thread"
210 # endif
211 #endif
212 
213 #define PYBIND11_INTERNALS_ID "__pybind11_internals_v" \
214  PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI PYBIND11_BUILD_TYPE "__"
215 
216 #define PYBIND11_MODULE_LOCAL_ID "__pybind11_module_local_v" \
217  PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI PYBIND11_BUILD_TYPE "__"
218 
219 /// Each module locally stores a pointer to the `internals` data. The data
220 /// itself is shared among modules with the same `PYBIND11_INTERNALS_ID`.
222  static internals **internals_pp = nullptr;
223  return internals_pp;
224 }
225 
226 inline void translate_exception(std::exception_ptr p) {
227  try {
228  if (p) std::rethrow_exception(p);
229  } catch (error_already_set &e) { e.restore(); return;
230  } catch (const builtin_exception &e) { e.set_error(); return;
231  } catch (const std::bad_alloc &e) { PyErr_SetString(PyExc_MemoryError, e.what()); return;
232  } catch (const std::domain_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return;
233  } catch (const std::invalid_argument &e) { PyErr_SetString(PyExc_ValueError, e.what()); return;
234  } catch (const std::length_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return;
235  } catch (const std::out_of_range &e) { PyErr_SetString(PyExc_IndexError, e.what()); return;
236  } catch (const std::range_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return;
237  } catch (const std::overflow_error &e) { PyErr_SetString(PyExc_OverflowError, e.what()); return;
238  } catch (const std::exception &e) { PyErr_SetString(PyExc_RuntimeError, e.what()); return;
239  } catch (...) {
240  PyErr_SetString(PyExc_RuntimeError, "Caught an unknown exception!");
241  return;
242  }
243 }
244 
245 #if !defined(__GLIBCXX__)
246 inline void translate_local_exception(std::exception_ptr p) {
247  try {
248  if (p) std::rethrow_exception(p);
249  } catch (error_already_set &e) { e.restore(); return;
250  } catch (const builtin_exception &e) { e.set_error(); return;
251  }
252 }
253 #endif
254 
255 /// Return a reference to the current `internals` data
257  auto **&internals_pp = get_internals_pp();
258  if (internals_pp && *internals_pp)
259  return **internals_pp;
260 
261  // Ensure that the GIL is held since we will need to make Python calls.
262  // Cannot use py::gil_scoped_acquire here since that constructor calls get_internals.
263  struct gil_scoped_acquire_local {
264  gil_scoped_acquire_local() : state (PyGILState_Ensure()) {}
265  ~gil_scoped_acquire_local() { PyGILState_Release(state); }
266  const PyGILState_STATE state;
267  } gil;
268 
270  auto builtins = handle(PyEval_GetBuiltins());
271  if (builtins.contains(id) && isinstance<capsule>(builtins[id])) {
272  internals_pp = static_cast<internals **>(capsule(builtins[id]));
273 
274  // We loaded builtins through python's builtins, which means that our `error_already_set`
275  // and `builtin_exception` may be different local classes than the ones set up in the
276  // initial exception translator, below, so add another for our local exception classes.
277  //
278  // libstdc++ doesn't require this (types there are identified only by name)
279 #if !defined(__GLIBCXX__)
280  (*internals_pp)->registered_exception_translators.push_front(&translate_local_exception);
281 #endif
282  } else {
283  if (!internals_pp) internals_pp = new internals*();
284  auto *&internals_ptr = *internals_pp;
285  internals_ptr = new internals();
286 #if defined(WITH_THREAD)
287 
288  #if PY_VERSION_HEX < 0x03090000
289  PyEval_InitThreads();
290  #endif
291  PyThreadState *tstate = PyThreadState_Get();
292  #if PY_VERSION_HEX >= 0x03070000
293  internals_ptr->tstate = PyThread_tss_alloc();
294  if (!internals_ptr->tstate || PyThread_tss_create(internals_ptr->tstate))
295  pybind11_fail("get_internals: could not successfully initialize the TSS key!");
296  PyThread_tss_set(internals_ptr->tstate, tstate);
297  #else
298  internals_ptr->tstate = PyThread_create_key();
299  if (internals_ptr->tstate == -1)
300  pybind11_fail("get_internals: could not successfully initialize the TLS key!");
301  PyThread_set_key_value(internals_ptr->tstate, tstate);
302  #endif
303  internals_ptr->istate = tstate->interp;
304 #endif
305  builtins[id] = capsule(internals_pp);
306  internals_ptr->registered_exception_translators.push_front(&translate_exception);
308  internals_ptr->default_metaclass = make_default_metaclass();
309  internals_ptr->instance_base = make_object_base_type(internals_ptr->default_metaclass);
310  }
311  return **internals_pp;
312 }
313 
314 /// Works like `internals.registered_types_cpp`, but for module-local registered types:
316  static type_map<type_info *> locals{};
317  return locals;
318 }
319 
320 /// Constructs a std::string with the given arguments, stores it in `internals`, and returns its
321 /// `c_str()`. Such strings objects have a long storage duration -- the internal strings are only
322 /// cleared when the program exits or after interpreter shutdown (when embedding), and so are
323 /// suitable for c-style strings needed by Python internals (such as PyTypeObject's tp_name).
324 template <typename... Args>
325 const char *c_str(Args &&...args) {
326  auto &strings = get_internals().static_strings;
327  strings.emplace_front(std::forward<Args>(args)...);
328  return strings.front().c_str();
329 }
330 
332 
333 /// Returns a named pointer that is shared among all extension modules (using the same
334 /// pybind11 version) running in the current interpreter. Names starting with underscores
335 /// are reserved for internal usage. Returns `nullptr` if no matching entry was found.
336 inline PYBIND11_NOINLINE void *get_shared_data(const std::string &name) {
338  auto it = internals.shared_data.find(name);
339  return it != internals.shared_data.end() ? it->second : nullptr;
340 }
341 
342 /// Set the shared data that can be later recovered by `get_shared_data()`.
343 inline PYBIND11_NOINLINE void *set_shared_data(const std::string &name, void *data) {
345  return data;
346 }
347 
348 /// Returns a typed reference to a shared data entry (by using `get_shared_data()`) if
349 /// such entry exists. Otherwise, a new object of default-constructible type `T` is
350 /// added to the shared data under the given name and a reference to it is returned.
351 template<typename T>
352 T &get_or_create_shared_data(const std::string &name) {
354  auto it = internals.shared_data.find(name);
355  T *ptr = (T *) (it != internals.shared_data.end() ? it->second : nullptr);
356  if (!ptr) {
357  ptr = new T();
358  internals.shared_data[name] = ptr;
359  }
360  return *ptr;
361 }
362 
internals::shared_data
std::unordered_map< std::string, void * > shared_data
Definition: internals.h:104
name
Annotation for function names.
Definition: attr.h:36
PYBIND11_NAMESPACE_BEGIN
#define PYBIND11_NAMESPACE_BEGIN(name)
Definition: common.h:16
internals
Definition: internals.h:96
PYBIND11_NAMESPACE_END
#define PYBIND11_NAMESPACE_END(name)
Definition: common.h:17
type_hash::operator()
size_t operator()(const std::type_index &t) const
Definition: internals.h:66
error_already_set
Definition: pytypes.h:326
type_info
Definition: internals.h:128
PYBIND11_NAMESPACE
#define PYBIND11_NAMESPACE
Definition: common.h:26
data
arr data(const arr &a, Ix... index)
Definition: test_numpy_array.cpp:82
type_info::simple_type
bool simple_type
Definition: internals.h:143
PYBIND11_STR_TYPE
#define PYBIND11_STR_TYPE
Definition: common.h:230
type_info::type
PyTypeObject * type
Definition: internals.h:129
internals::registered_types_cpp
type_map< type_info * > registered_types_cpp
Definition: internals.h:97
internals::loader_patient_stack
std::vector< PyObject * > loader_patient_stack
Definition: internals.h:105
make_static_property_type
PyTypeObject * make_static_property_type()
Definition: class.h:60
type_info::get_buffer_data
void * get_buffer_data
Definition: internals.h:139
type_info::default_holder
bool default_holder
Definition: internals.h:147
PYBIND11_NOINLINE
#define PYBIND11_NOINLINE
Definition: common.h:88
type_info::type_size
size_t type_size
Definition: internals.h:131
internals::registered_instances
std::unordered_multimap< const void *, instance * > registered_instances
Definition: internals.h:99
capsule
Definition: pytypes.h:1211
type_info::implicit_conversions
std::vector< PyObject *(*)(PyObject *, PyTypeObject *)> implicit_conversions
Definition: internals.h:135
value_and_holder
Definition: cast.h:223
internals::static_property_type
PyTypeObject * static_property_type
Definition: internals.h:107
make_object_base_type
PyObject * make_object_base_type(PyTypeObject *metaclass)
Definition: class.h:443
get_internals
PYBIND11_NOINLINE internals & get_internals()
Return a reference to the current internals data.
Definition: internals.h:256
type_info::dealloc
void(* dealloc)(value_and_holder &v_h)
Definition: internals.h:134
type_hash
Definition: internals.h:65
error_already_set::restore
void restore()
Definition: pytypes.h:342
type_info::cpptype
const std::type_info * cpptype
Definition: internals.h:130
type_info::type_align
size_t type_align
Definition: internals.h:131
hash
ssize_t hash(handle obj)
Definition: pytypes.h:459
internals::instance_base
PyObject * instance_base
Definition: internals.h:109
get_or_create_shared_data
T & get_or_create_shared_data(const std::string &name)
Definition: internals.h:352
builtin_exception::set_error
virtual void set_error() const =0
Set the error using the Python C API.
instance
The 'instance' type which needs to be standard layout (need to be able to use 'offsetof')
Definition: common.h:437
same_type
bool same_type(const std::type_info &lhs, const std::type_info &rhs)
Definition: internals.h:61
registered_local_types_cpp
type_map< type_info * > & registered_local_types_cpp()
Works like internals.registered_types_cpp, but for module-local registered types:
Definition: internals.h:315
handle
Definition: pytypes.h:176
internals::inactive_override_cache
std::unordered_set< std::pair< const PyObject *, const char * >, override_hash > inactive_override_cache
Definition: internals.h:100
PYBIND11_INTERNALS_ID
#define PYBIND11_INTERNALS_ID
Definition: internals.h:213
set_shared_data
PYBIND11_NOINLINE void * set_shared_data(const std::string &name, void *data)
Set the shared data that can be later recovered by get_shared_data().
Definition: internals.h:343
metaclass
Annotation which requests that a special metaclass is created for a type.
Definition: attr.h:61
override_hash
Definition: internals.h:85
size_t
std::size_t size_t
Definition: common.h:354
translate_local_exception
void translate_local_exception(std::exception_ptr p)
Definition: internals.h:246
type_info::direct_conversions
std::vector< bool(*)(PyObject *, void *&)> * direct_conversions
Definition: internals.h:137
pybind11_fail
PyExc_RuntimeError PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Used internally.
Definition: common.h:751
internals::static_strings
std::forward_list< std::string > static_strings
Definition: internals.h:106
type_equal_to
Definition: internals.h:75
type_info::implicit_casts
std::vector< std::pair< const std::type_info *, void *(*)(void *)> > implicit_casts
Definition: internals.h:136
translate_exception
void translate_exception(std::exception_ptr p)
Definition: internals.h:226
internals::direct_conversions
type_map< std::vector< bool(*)(PyObject *, void *&)> > direct_conversions
Definition: internals.h:101
internals::patients
std::unordered_map< const PyObject *, std::vector< PyObject * > > patients
Definition: internals.h:102
args
Definition: pytypes.h:1366
buffer_info
Information record describing a Python buffer object.
Definition: buffer_info.h:40
c_str
const char * c_str(Args &&...args)
Definition: internals.h:325
type_info::module_local
bool module_local
Definition: internals.h:149
type_info::init_instance
void(* init_instance)(instance *, const void *)
Definition: internals.h:133
PYBIND11_TLS_KEY_INIT
#define PYBIND11_TLS_KEY_INIT(var)
Definition: internals.h:31
override_hash::operator()
size_t operator()(const std::pair< const PyObject *, const char * > &v) const
Definition: internals.h:86
internals::default_metaclass
PyTypeObject * default_metaclass
Definition: internals.h:108
type_equal_to::operator()
bool operator()(const std::type_index &lhs, const std::type_index &rhs) const
Definition: internals.h:76
internals::registered_types_py
std::unordered_map< PyTypeObject *, std::vector< type_info * > > registered_types_py
Definition: internals.h:98
get_internals_pp
internals **& get_internals_pp()
Definition: internals.h:221
PYBIND11_TLS_FREE
#define PYBIND11_TLS_FREE(key)
Definition: internals.h:47
get_shared_data
PYBIND11_NOINLINE void * get_shared_data(const std::string &name)
Definition: internals.h:336
builtin_exception
C++ bindings of builtin Python exceptions.
Definition: common.h:727
internals::registered_exception_translators
std::forward_list< void(*)(std::exception_ptr)> registered_exception_translators
Definition: internals.h:103
type_info::simple_ancestors
bool simple_ancestors
Definition: internals.h:145
make_default_metaclass
PyTypeObject * make_default_metaclass()
Definition: class.h:237
test_callbacks.value
value
Definition: test_callbacks.py:126
type_map
std::unordered_map< std::type_index, value_type, type_hash, type_equal_to > type_map
Definition: internals.h:83
make_changelog.state
state
Definition: make_changelog.py:30
type_info::holder_size_in_ptrs
size_t holder_size_in_ptrs
Definition: internals.h:131