exception_translation.h
Go to the documentation of this file.
1 /*
2  pybind11/detail/exception_translation.h: means to translate C++ exceptions to Python exceptions
3 
4  Copyright (c) 2024 The Pybind Development Team.
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 "common.h"
13 #include "internals.h"
14 
17 
18 // Apply all the extensions translators from a list
19 // Return true if one of the translators completed without raising an exception
20 // itself. Return of false indicates that if there are other translators
21 // available, they should be tried.
22 inline bool apply_exception_translators(std::forward_list<ExceptionTranslator> &translators) {
23  auto last_exception = std::current_exception();
24 
25  for (auto &translator : translators) {
26  try {
27  translator(last_exception);
28  return true;
29  } catch (...) {
30  last_exception = std::current_exception();
31  }
32  }
33  return false;
34 }
35 
36 inline void try_translate_exceptions() {
37  /* When an exception is caught, give each registered exception
38  translator a chance to translate it to a Python exception. First
39  all module-local translators will be tried in reverse order of
40  registration. If none of the module-locale translators handle
41  the exception (or there are no module-locale translators) then
42  the global translators will be tried, also in reverse order of
43  registration.
44 
45  A translator may choose to do one of the following:
46 
47  - catch the exception and call py::set_error()
48  to set a standard (or custom) Python exception, or
49  - do nothing and let the exception fall through to the next translator, or
50  - delegate translation to the next translator by throwing a new type of exception.
51  */
52 
53  bool handled = with_internals([&](internals &internals) {
54  auto &local_exception_translators = get_local_internals().registered_exception_translators;
55  if (detail::apply_exception_translators(local_exception_translators)) {
56  return true;
57  }
58  auto &exception_translators = internals.registered_exception_translators;
59  if (detail::apply_exception_translators(exception_translators)) {
60  return true;
61  }
62  return false;
63  });
64 
65  if (!handled) {
66  set_error(PyExc_SystemError, "Exception escaped from default exception translator!");
67  }
68 }
69 
ExceptionTranslator
void(*)(std::exception_ptr) ExceptionTranslator
Definition: internals.h:54
internals
Definition: internals.h:177
local_internals::registered_exception_translators
std::forward_list< ExceptionTranslator > registered_exception_translators
Definition: internals.h:585
PYBIND11_NAMESPACE_END
#define PYBIND11_NAMESPACE_END(name)
Definition: wrap/pybind11/include/pybind11/detail/common.h:80
internals.h
detail
Definition: testSerializationNonlinear.cpp:69
PYBIND11_NAMESPACE_BEGIN
#define PYBIND11_NAMESPACE_BEGIN(name)
Definition: wrap/pybind11/include/pybind11/detail/common.h:76
internals::registered_exception_translators
std::forward_list< ExceptionTranslator > registered_exception_translators
Definition: internals.h:195
try_translate_exceptions
void try_translate_exceptions()
Definition: exception_translation.h:36
PYBIND11_NAMESPACE
Definition: test_custom_type_casters.cpp:24
common.h
set_error
void set_error(const handle &type, const char *message)
Definition: pytypes.h:346
with_internals
auto with_internals(const F &cb) -> decltype(cb(get_internals()))
Definition: internals.h:640
std
Definition: BFloat16.h:88
get_local_internals
local_internals & get_local_internals()
Works like get_internals, but for things which are locally registered.
Definition: internals.h:623
apply_exception_translators
bool apply_exception_translators(std::forward_list< ExceptionTranslator > &translators)
Definition: exception_translation.h:22


gtsam
Author(s):
autogenerated on Wed Mar 19 2025 03:01:39