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
Exceptions: Enable translations of dynamic strings #9709
Draft
abdullahtahiriyo
wants to merge
5
commits into
FreeCAD:main
Choose a base branch
from
abdullahtahiriyo:formater_argument_exceptions
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+639
−240
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9528b4b
Exception: Enhance exceptions for dynamic string translations
abdullahtahiriyo 073d194
PythonConsole: Enhance error reporting of Exceptions with dictionary …
abdullahtahiriyo 17a64e2
Gui: Extend Notifications Framework to take exceptions as message
abdullahtahiriyo 8e285f7
Base: Enhanced Python Exceptions
abdullahtahiriyo 9e16b48
Sketcher: Demonstrator for the new enhanced exceptions framework
abdullahtahiriyo File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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,96 @@ | ||
/*************************************************************************** | ||
* Copyright (c) 2023 Abdullah Tahiri <abdullah.tahiri.yo@gmail.com> * | ||
* * | ||
* This file is part of the FreeCAD CAx development system. * | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU Library General Public License (LGPL) * | ||
* as published by the Free Software Foundation; either version 2 of * | ||
* the License, or (at your option) any later version. * | ||
* for detail see the LICENCE text file. * | ||
* * | ||
* FreeCAD is distributed in the hope that it will be useful, * | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
* GNU Library General Public License for more details. * | ||
* * | ||
* You should have received a copy of the GNU Library General Public * | ||
* License along with FreeCAD; if not, write to the Free Software * | ||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * | ||
* USA * | ||
* * | ||
***************************************************************************/ | ||
|
||
|
||
#ifndef BASE_ENHANCED_PYTHON_EXCEPTIONS_H | ||
#define BASE_ENHANCED_PYTHON_EXCEPTIONS_H | ||
|
||
#include <fmt/core.h> | ||
#include <CXX/Objects.hxx> | ||
|
||
namespace Base | ||
{ | ||
|
||
/** Triggers an enhanced Python Exception | ||
* | ||
* An enhanced Python Exception stores separately a formatter string and arguments. This | ||
* allows it to produce untranslated and translated dynamic strings (provided that the | ||
* strings provided are marked for translation using the QT_TRANSLATE_NOOP with the "Notifications" | ||
* context). | ||
* | ||
* @param exceptiontype, one of the types of Python exception (e.g. PyExc_ValueError) | ||
* @param format string following the syntax of fmt::format | ||
* @param args one or more args to be substituted in the format string (as in fmt::format) | ||
* | ||
* This method expects that any translatable string in format and args is marked with | ||
* QT_TRANSLATE_NOOP with the context "Notifications". It marks that the string CAN be | ||
* translated. Failure to provide it with marked strings is a bug by client code and will | ||
* result in untranslated strings appearing to the user. | ||
* | ||
* When a Base::Exception is constructed from an enhanced Python Exception, the what() method | ||
* provides the formatted untranslated string. The formatted translated string can be obtained | ||
* using the translateEnhancedMessage method, requiring a translating function. See Base::Exception | ||
* for more details. | ||
* | ||
* Examples: | ||
* PyError_SetEnhancedString(PyExc_ValueError, | ||
* QT_TRANSLATE_NOOP("Notifications", "Invalid constraint index: {}"), Index); | ||
* | ||
* | ||
*/ | ||
template<typename... Args> | ||
inline void PyError_SetEnhancedString(PyObject * exceptiontype, const char* format, Args&&... args) | ||
{ | ||
std::string swhat = fmt::format(format, args...); | ||
|
||
Py::Dict d; | ||
|
||
d.setItem("swhat", Py::String(swhat)); | ||
|
||
// if it has arguments, then we need to store formater and arguments separately | ||
if constexpr(sizeof...(args) > 0) { | ||
d.setItem("sformatter", Py::String(format)); | ||
|
||
Py::Tuple t(static_cast<size_t>(sizeof...(args))); | ||
|
||
int i = 0; | ||
|
||
( | ||
( | ||
t.setItem(i, Py::String(fmt::format("{}", (std::forward<decltype(args)>(args))))), | ||
i++ | ||
), | ||
... | ||
); | ||
|
||
d.setItem("sformatterArguments",t); | ||
} | ||
|
||
d.setItem("btranslatable", Py::Boolean(true)); | ||
|
||
PyErr_SetObject(exceptiontype, Py::new_reference_to(d)); | ||
} | ||
|
||
} //namespace Base | ||
|
||
#endif // BASE_ENHANCED_PYTHON_EXCEPTIONS_H |
This file contains 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 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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wwmayer
I would welcome a trained second set of eyes on this function and in particular on whether I may be leaking (ref counter issues).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks correct to me.