cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
Error.cpp
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2022-2024 Laurent Forthomme
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19// clang-format off
22#include <frameobject.h>
23// clang-format on
24
25#include "CepGen/Utils/String.h"
26
27namespace cepgen {
28 namespace python {
29 Error::Error(const char *origin, const char *file, short lineno) noexcept
30 : Exception("", origin, Exception::Type::error, file, lineno) {
31 // retrieve error indicator and clear it to handle ourself the error
32 PyErr_Fetch(&ptype_, &pvalue_, &ptraceback_obj_);
33 PyErr_Clear();
34 // ensure the objects retrieved are properly normalised and point to compatible objects
35 PyErr_NormalizeException(&ptype_, &pvalue_, &ptraceback_obj_);
36 if (ptype_) {
37 // we can start the traceback
38 (*this) << "Error: " << ObjectPtr::wrap(PyObject_Str(pvalue_)).value<std::string>();
39 if (auto mod_traceback = ObjectPtr::importModule("traceback"))
40 if (auto fmt = mod_traceback.attribute("format_exception"); PyCallable_Check(fmt.get()))
41 if (ObjectPtr pyth_val(PyObject_CallFunctionObjArgs(fmt.get(), ptype_, pvalue_, ptraceback_obj_, nullptr));
42 pyth_val) {
43 (*this) << "\n" << std::string(80, '.') << "\n";
44 for (const auto &tb : pyth_val.vector<std::string>()) {
45 size_t i = 0;
46 std::string sep;
47 for (const auto &err_line : utils::split(tb, '\n'))
48 (*this) << sep << (i == 0 ? utils::boldify(err_line) : err_line), ++i, sep = "\n";
49 (*this) << "\n";
50 }
51 }
52 (*this) << std::string(80, '.') << "\n";
53 }
54 }
55 } // namespace python
56} // namespace cepgen
@ error
General non-stopping error.
Definition Exception.h:30
Error(const char *, const char *, short) noexcept
Definition Error.cpp:29
Smart pointer to a Python object and its dereferencing operator.
Definition ObjectPtr.h:36
static ObjectPtr importModule(const std::string &)
Import a Python module in a new reference-counted Python object.
T value() const
Cast a Python object into a C++ type.
static ObjectPtr wrap(PyObject *)
Wrap a PyObject without cleaning at the destructor.
Definition ObjectPtr.cpp:45
std::string boldify(std::string str)
String implementation of the boldification procedure.
Definition String.cpp:49
std::vector< std::string > split(const std::string &str, char delim, bool trim)
Split a string according to a separation character.
Definition String.cpp:233
Common namespace for this Monte Carlo generator.