-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix: properly translate C++ exception to Python exception when creating Python buffer from wrapped object #5324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8712543
Add test for throwing def_buffer case
vnlitvinov 1c7f3c9
Translate C++ -> Python exceptions in buffer creation
vnlitvinov 753c58c
Fix code formatting
vnlitvinov 8eb84ab
Fix "unused parameter" warning
vnlitvinov 7f6684a
Refactor per review
vnlitvinov 4c66dac
style: pre-commit fixes
pre-commit-ci[bot] 9a4a00c
Address review comments
vnlitvinov 8047e6b
Merge remote-tracking branch 'upstream/master' into fix-2764
vnlitvinov df44bd9
Address review comments
vnlitvinov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
pybind11/detail/exception_translation.h: means to translate C++ exceptions to Python exceptions | ||
|
||
Copyright (c) 2024 The Pybind Development Team. | ||
|
||
All rights reserved. Use of this source code is governed by a | ||
BSD-style license that can be found in the LICENSE file. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "common.h" | ||
#include "internals.h" | ||
|
||
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) | ||
PYBIND11_NAMESPACE_BEGIN(detail) | ||
|
||
// Apply all the extensions translators from a list | ||
// Return true if one of the translators completed without raising an exception | ||
// itself. Return of false indicates that if there are other translators | ||
// available, they should be tried. | ||
inline bool apply_exception_translators(std::forward_list<ExceptionTranslator> &translators) { | ||
auto last_exception = std::current_exception(); | ||
|
||
for (auto &translator : translators) { | ||
try { | ||
translator(last_exception); | ||
return true; | ||
} catch (...) { | ||
last_exception = std::current_exception(); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
inline void try_translate_exceptions() { | ||
/* When an exception is caught, give each registered exception | ||
translator a chance to translate it to a Python exception. First | ||
all module-local translators will be tried in reverse order of | ||
registration. If none of the module-locale translators handle | ||
the exception (or there are no module-locale translators) then | ||
the global translators will be tried, also in reverse order of | ||
registration. | ||
|
||
A translator may choose to do one of the following: | ||
|
||
- catch the exception and call py::set_error() | ||
to set a standard (or custom) Python exception, or | ||
- do nothing and let the exception fall through to the next translator, or | ||
- delegate translation to the next translator by throwing a new type of exception. | ||
*/ | ||
|
||
bool handled = with_internals([&](internals &internals) { | ||
auto &local_exception_translators = get_local_internals().registered_exception_translators; | ||
if (detail::apply_exception_translators(local_exception_translators)) { | ||
return true; | ||
} | ||
auto &exception_translators = internals.registered_exception_translators; | ||
if (detail::apply_exception_translators(exception_translators)) { | ||
return true; | ||
} | ||
return false; | ||
}); | ||
|
||
if (!handled) { | ||
set_error(PyExc_SystemError, "Exception escaped from default exception translator!"); | ||
} | ||
} | ||
|
||
PYBIND11_NAMESPACE_END(detail) | ||
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.