Commit c029e695 authored by Janek Kozicki's avatar Janek Kozicki

Use PyErr_Print(); in case when importing python module 'raised unreported...

Use PyErr_Print(); in case when importing python module 'raised unreported exception', to make python/C++ boundary easier to debug.
parent 3b9a75c2
#include <lib/base/Logging.hpp>
#include <core/Omega.hpp>
#include <signal.h>
CREATE_CPP_LOCAL_LOGGER("pyboot.cpp");
#ifdef YADE_DEBUG
void crashHandler(int sig)
{
......@@ -57,8 +58,15 @@ void yadeInitialize(boost::python::list& pp, const std::string& confDir)
void yadeFinalize() { yade::Omega::instance().cleanupTemps(); }
BOOST_PYTHON_MODULE(boot)
{
try {
boost::python::scope().attr("initialize") = &yadeInitialize;
boost::python::scope().attr("finalize") = &yadeFinalize; //,"Finalize yade (only to be used internally).")
} catch (...) {
LOG_FATAL("Importing this module caused an exception and this module is in an inconsistent state now.");
PyErr_Print();
PyErr_SetString(PyExc_SystemError, __FILE__);
boost::python::handle_exception();
throw;
}
#include "GLViewer.hpp"
#include "OpenGLManager.hpp"
#include <lib/base/AliasNamespaces.hpp>
#include <lib/base/Logging.hpp>
#include <lib/pyutil/doc_opts.hpp>
#include <pkg/common/OpenGLRenderer.hpp>
#include <QApplication>
#include <QCloseEvent>
CREATE_CPP_LOCAL_LOGGER("qt4/_GLViewer.cpp");
namespace yade { // Cannot have #include directive inside.
qglviewer::Vec tuple2vec(py::tuple t)
......@@ -262,7 +264,7 @@ shared_ptr<OpenGLRenderer> getRenderer() { return OpenGLManager::self->renderer;
// BOOST_PYTHON_MODULE cannot be inside yade namespace, it has 'extern "C"' keyword, which strips it out of any namespaces.
BOOST_PYTHON_MODULE(_GLViewer)
{
try {
namespace y = ::yade;
namespace py = ::boost::python;
......@@ -329,5 +331,12 @@ BOOST_PYTHON_MODULE(_GLViewer)
.def("close", &pyGLViewer::close)
.def("saveSnapshot", &pyGLViewer::saveSnapshot, (py::arg("filename")), "Save the current view to image file")
.add_property("selection", &pyGLViewer::get_selection, &pyGLViewer::set_selection);
} catch (...) {
LOG_FATAL("Importing this module caused an exception and this module is in an inconsistent state now.");
PyErr_Print();
PyErr_SetString(PyExc_SystemError, __FILE__);
boost::python::handle_exception();
throw;
}
#include "GLViewer.hpp"
#include "OpenGLManager.hpp"
#include <lib/base/AliasNamespaces.hpp>
#include <lib/base/Logging.hpp>
#include <lib/pyutil/doc_opts.hpp>
#include <pkg/common/OpenGLRenderer.hpp>
#include <QApplication>
#include <QCloseEvent>
CREATE_CPP_LOCAL_LOGGER("qt5/_GLViewer.cpp");
namespace yade { // Cannot have #include directive inside.
qglviewer::Vec tuple2vec(py::tuple t)
......@@ -262,7 +264,7 @@ shared_ptr<OpenGLRenderer> getRenderer() { return OpenGLManager::self->renderer;
// BOOST_PYTHON_MODULE cannot be inside yade namespace, it has 'extern "C"' keyword, which strips it out of any namespaces.
BOOST_PYTHON_MODULE(_GLViewer)
{
try {
namespace y = ::yade;
namespace py = ::boost::python;
......@@ -329,5 +331,12 @@ BOOST_PYTHON_MODULE(_GLViewer)
.def("close", &pyGLViewer::close)
.def("saveSnapshot", &pyGLViewer::saveSnapshot, (py::arg("filename")), "Save the current view to image file")
.add_property("selection", &pyGLViewer::get_selection, &pyGLViewer::set_selection);
} catch (...) {
LOG_FATAL("Importing this module caused an exception and this module is in an inconsistent state now.");
PyErr_Print();
PyErr_SetString(PyExc_SystemError, __FILE__);
boost::python::handle_exception();
throw;
}
#include <lib/base/Logging.hpp>
#include <lib/smoothing/WeightedAverage2d.hpp>
CREATE_CPP_LOCAL_LOGGER("WeightedAverage2d.cpp");
namespace yade { // Cannot have #include directive inside.
/* Tell whether point is inside polygon
......@@ -24,7 +27,7 @@ bool pyGaussAverage::pointInsidePolygon(const Vector2r& pt, const vector<Vector2
// BOOST_PYTHON_MODULE cannot be inside yade namespace, it has 'extern "C"' keyword, which strips it out of any namespaces.
BOOST_PYTHON_MODULE(WeightedAverage2d)
{
try {
using pyGaussAverage = ::yade::pyGaussAverage;
boost::python::scope().attr("__doc__") = "Smoothing (2d gauss-weighted average) for postprocessing scalars in 2d.";
boost::python::class_<pyGaussAverage>(
......@@ -46,5 +49,12 @@ BOOST_PYTHON_MODULE(WeightedAverage2d)
.add_property("nCells", &pyGaussAverage::nCells_get)
.add_property("cellArea", &pyGaussAverage::cellArea)
.add_property("cellDim", &pyGaussAverage::cellDim);
};
} catch (...) {
LOG_FATAL("Importing this module caused an exception and this module is in an inconsistent state now.");
PyErr_Print();
PyErr_SetString(PyExc_SystemError, __FILE__);
boost::python::handle_exception();
throw;
}
#include <lib/base/Logging.hpp>
#include <lib/pyutil/doc_opts.hpp>
#include <core/Omega.hpp>
#include <boost/lexical_cast.hpp>
......@@ -345,7 +346,7 @@ py::dict getAllVersionsCpp()
}
BOOST_PYTHON_MODULE(_libVersions)
{
try {
YADE_SET_DOCSTRING_OPTS;
// We can use C++ string literal just like """ """ in python to write docstrings (see. https://en.cppreference.com/w/cpp/language/string_literal )
// The """ is a custom delimeter, we could use R"RAW( instead, or any other delimeter. This decides what will be the termination delimeter.
......@@ -367,4 +368,19 @@ As an example the dict below reflects what libraries this documentation was comp
.. note:: Please add here C++ detection of other libraries when yade starts using them.
)""");
} catch (...) {
// How to work with python exceptions:
// https://www.boost.org/doc/libs/1_71_0/libs/python/doc/html/reference/high_level_components/boost_python_errors_hpp.html#high_level_components.boost_python_errors_hpp.example
// https://www.boost.org/doc/libs/1_71_0/libs/python/doc/html/tutorial/tutorial/embedding.html
// https://stackoverflow.com/questions/1418015/how-to-get-python-exception-text
// If we wanted custom yade exceptions thrown to python:
// https://www.boost.org/doc/libs/1_71_0/libs/python/doc/html/tutorial/tutorial/exception.html
// https://www.boost.org/doc/libs/1_71_0/libs/python/doc/html/reference/high_level_components/boost_python_exception_translato.html
LOG_FATAL("Importing this module caused an exception and this module is in an inconsistent state now.");
PyErr_Print();
PyErr_SetString(PyExc_SystemError, __FILE__);
boost::python::handle_exception();
throw;
}
......@@ -15,10 +15,10 @@
#include <core/Omega.hpp>
#include <string>
namespace yade { // Cannot have #include directive inside.
CREATE_CPP_LOCAL_LOGGER("_log.cpp");
namespace yade { // Cannot have #include directive inside.
#ifdef YADE_BOOST_LOG
int getDefaultLogLevel() { return Logging::instance().getDefaultLogLevel(); }
......@@ -153,7 +153,7 @@ void testAllLevels()
// BOOST_PYTHON_MODULE cannot be inside yade namespace, it has 'extern "C"' keyword, which strips it out of any namespaces.
BOOST_PYTHON_MODULE(_log)
{
try {
using namespace yade; // 'using namespace' inside function keeps namespace pollution under control. Alernatively I could add y:: in front of function names below and put 'namespace y = ::yade;' here.
namespace py = ::boost::python;
YADE_SET_DOCSTRING_OPTS;
......@@ -236,13 +236,19 @@ Saves log config to specified file.
py::scope().attr("ERROR") = int(2);
py::scope().attr("FATAL") = int(1);
py::scope().attr("NOFILTER") = int(0);
}
/* this was in git revision 014b11496
BOOST_PYTHON_MODULE(log){
python::def("loadConfig",logLoadConfig,(python::arg("fileName")),"Load configuration from file (log4cxx::PropertyConfigurator::configure)");
} catch (...) {
// How to work with python exceptions:
// https://www.boost.org/doc/libs/1_71_0/libs/python/doc/html/reference/high_level_components/boost_python_errors_hpp.html#high_level_components.boost_python_errors_hpp.example
// https://www.boost.org/doc/libs/1_71_0/libs/python/doc/html/tutorial/tutorial/embedding.html
// https://stackoverflow.com/questions/1418015/how-to-get-python-exception-text
// If we wanted custom yade exceptions thrown to python:
// https://www.boost.org/doc/libs/1_71_0/libs/python/doc/html/tutorial/tutorial/exception.html
// https://www.boost.org/doc/libs/1_71_0/libs/python/doc/html/reference/high_level_components/boost_python_exception_translato.html
LOG_FATAL("Importing this module caused an exception and this module is in an inconsistent state now.");
PyErr_Print();
PyErr_SetString(PyExc_SystemError, __FILE__);
boost::python::handle_exception();
throw;
}
*/
......@@ -3,22 +3,21 @@
#ifdef YADE_CGAL
#include "pkg/dem/Polyhedra.hpp"
#include <lib/base/AliasNamespaces.hpp>
#include <lib/base/Logging.hpp>
#include <lib/pyutil/doc_opts.hpp>
#include <core/Omega.hpp>
#include <core/Scene.hpp>
#include <pkg/common/ElastMat.hpp>
#include <pkg/common/Sphere.hpp>
#include <pkg/dem/Polyhedra.hpp>
#include <cmath>
#include <numpy/ndarraytypes.h>
namespace yade { // Cannot have #include directive inside.
CREATE_CPP_LOCAL_LOGGER("_polyhedra_utils.cpp");
namespace yade { // Cannot have #include directive inside.
//**********************************************************************************
//print polyhedron in basic position
void PrintPolyhedra(const shared_ptr<Shape>& shape)
......@@ -664,7 +663,7 @@ bool convexHull(vector<Vector3r> points)
// BOOST_PYTHON_MODULE cannot be inside yade namespace, it has 'extern "C"' keyword, which strips it out of any namespaces.
BOOST_PYTHON_MODULE(_polyhedra_utils)
{
try {
using namespace yade; // 'using namespace' inside function keeps namespace pollution under control. Alernatively I could add y:: in front of function names below and put 'namespace y = ::yade;' here.
namespace py = ::boost::python;
YADE_SET_DOCSTRING_OPTS;
......@@ -684,6 +683,13 @@ BOOST_PYTHON_MODULE(_polyhedra_utils)
py::def("SizeRatio", SizeRatio, "save sizes of polyhedra into file");
py::def("convexHull", convexHull, "TODO");
py::def("Split", Split, "split polyhedron perpendicularly to given direction through given point");
} catch (...) {
LOG_FATAL("Importing this module caused an exception and this module is in an inconsistent state now.");
PyErr_Print();
PyErr_SetString(PyExc_SystemError, __FILE__);
boost::python::handle_exception();
throw;
}
#endif // YADE_CGAL
......
......@@ -8,10 +8,10 @@
#include <numpy/arrayobject.h>
#pragma GCC diagnostic pop
namespace yade { // Cannot have #include directive inside.
CREATE_CPP_LOCAL_LOGGER("_utils.cpp");
namespace yade { // Cannot have #include directive inside.
py::tuple negPosExtremeIds(int axis, Real distFactor)
{
py::tuple extrema = Shop::aabbExtrema();
......@@ -618,7 +618,7 @@ void setBodyColor(int id, Vector3r newColor)
// BOOST_PYTHON_MODULE cannot be inside yade namespace, it has 'extern "C"' keyword, which strips it out of any namespaces.
BOOST_PYTHON_MODULE(_utils)
{
try {
using namespace yade; // 'using namespace' inside function keeps namespace pollution under control. Alernatively I could add y:: in front of function names below and put 'namespace y = ::yade;' here.
namespace py = ::boost::python;
YADE_SET_DOCSTRING_OPTS;
......@@ -962,5 +962,12 @@ BOOST_PYTHON_MODULE(_utils)
setBodyColor,
(py::args("id"), py::args("color")),
"Set a body color from its id and a new Vector3r.\n\n:param int id: the body id.\n:param Vector3 color: the desired updated color.");
} catch (...) {
LOG_FATAL("Importing this module caused an exception and this module is in an inconsistent state now.");
PyErr_Print();
PyErr_SetString(PyExc_SystemError, __FILE__);
boost::python::handle_exception();
throw;
}
......@@ -6,6 +6,8 @@
#include <lib/base/Math.hpp>
#include <lib/pyutil/doc_opts.hpp>
CREATE_CPP_LOCAL_LOGGER("_packObb.cpp");
template <typename Scalar> Eigen::Matrix<Scalar, 3, 3> matrixFromEulerAnglesXYZ(Scalar x, Scalar y, Scalar z)
{
Eigen::Matrix<Scalar, 3, 3> m;
......@@ -105,7 +107,7 @@ boost::python::tuple bestFitOBB_py(const boost::python::tuple& _pts)
// BOOST_PYTHON_MODULE cannot be inside yade namespace, it has 'extern "C"' keyword, which strips it out of any namespaces.
BOOST_PYTHON_MODULE(_packObb)
{
try {
YADE_SET_DOCSTRING_OPTS;
boost::python::scope().attr("__doc__") = "Computation of oriented bounding box for cloud of points.";
boost::python::def(
......@@ -113,5 +115,12 @@ BOOST_PYTHON_MODULE(_packObb)
yade::bestFitOBB_py,
"Return (Vector3 center, Vector3 halfSize, Quaternion orientation) of\nbest-fit oriented bounding-box for given tuple of points\n(uses "
"brute-force velome minimization, do not use for very large clouds).");
};
} catch (...) {
LOG_FATAL("Importing this module caused an exception and this module is in an inconsistent state now.");
PyErr_Print();
PyErr_SetString(PyExc_SystemError, __FILE__);
boost::python::handle_exception();
throw;
}
......@@ -4,10 +4,10 @@
#include <lib/base/Math.hpp>
#include <lib/pyutil/doc_opts.hpp>
namespace yade { // Cannot have #include directive inside.
CREATE_CPP_LOCAL_LOGGER("_packPredicates.cpp");
namespace yade { // Cannot have #include directive inside.
/*
This file contains various predicates that say whether a given point is within the solid,
or, not closer than "pad" to its boundary, if pad is nonzero
......@@ -494,7 +494,7 @@ public:
// BOOST_PYTHON_MODULE cannot be inside yade namespace, it has 'extern "C"' keyword, which strips it out of any namespaces.
BOOST_PYTHON_MODULE(_packPredicates)
{
try {
using namespace yade; // 'using namespace' inside function keeps namespace pollution under control. Alernatively I could add y:: in front of function names below and put 'namespace y = ::yade;' here.
namespace py = ::boost::python;
py::scope().attr("__doc__") = "Spatial predicates for volumes (defined analytically or by triangulation).";
......@@ -581,5 +581,12 @@ BOOST_PYTHON_MODULE(_packPredicates)
"the pad distance from given point, which is not exact."))
.add_property("surf", &inGtsSurface::surface, "The associated gts.Surface object.");
#endif
} catch (...) {
LOG_FATAL("Importing this module caused an exception and this module is in an inconsistent state now.");
PyErr_Print();
PyErr_SetString(PyExc_SystemError, __FILE__);
boost::python::handle_exception();
throw;
}
// 2009 © Václav Šmilauer <eudoxos@arcig.cz>
#include <lib/base/Logging.hpp>
#include <lib/base/Math.hpp>
#include <lib/pyutil/doc_opts.hpp>
#include <pkg/dem/SpherePack.hpp>
CREATE_CPP_LOCAL_LOGGER("_packSpheres.cpp");
// BOOST_PYTHON_MODULE cannot be inside yade namespace, it has 'extern "C"' keyword, which strips it out of any namespaces.
BOOST_PYTHON_MODULE(_packSpheres)
{
try {
using SpherePack = ::yade::SpherePack;
using Vector3r = ::yade::Vector3r;
using Matrix3r = ::yade::Matrix3r;
......@@ -136,5 +139,12 @@ BOOST_PYTHON_MODULE(_packSpheres)
boost::python::class_<SpherePack::_iterator>("SpherePackIterator", boost::python::init<SpherePack::_iterator&>())
.def("__iter__", &SpherePack::_iterator::iter)
.def("__next__", &SpherePack::_iterator::next);
} catch (...) {
LOG_FATAL("Importing this module caused an exception and this module is in an inconsistent state now.");
PyErr_Print();
PyErr_SetString(PyExc_SystemError, __FILE__);
boost::python::handle_exception();
throw;
}
......@@ -2,20 +2,21 @@
// for some reading about these conversions, see e.g. https://misspent.wordpress.com/2009/09/27/how-to-write-boost-python-converters/ (except the latter advocates using some "borrowed()" in the from-Python construc(), which is not done here)
#include <lib/base/Logging.hpp>
#include <lib/base/Math.hpp>
#include <lib/base/openmp-accu.hpp>
#include <core/Engine.hpp>
#include <pkg/common/Callbacks.hpp>
#include <pkg/common/Dispatching.hpp>
#include <pkg/common/KinematicEngines.hpp>
#include <pkg/common/MatchMaker.hpp>
#include <pkg/dem/SpherePack.hpp>
#ifdef YADE_OPENGL
#include <pkg/common/GLDrawFunctors.hpp>
#include <pkg/common/OpenGLRenderer.hpp>
#endif
#include <pkg/common/MatchMaker.hpp>
CREATE_CPP_LOCAL_LOGGER("customConverters.cpp");
namespace yade { // Cannot have #include directive inside.
......@@ -244,7 +245,7 @@ struct custom_mask_from_long {
// BOOST_PYTHON_MODULE cannot be inside yade namespace, it has 'extern "C"' keyword, which strips it out of any namespaces.
BOOST_PYTHON_MODULE(_customConverters)
{
try {
namespace y = ::yade;
y::custom_Se3r_from_seq();
......@@ -318,5 +319,12 @@ BOOST_PYTHON_MODULE(_customConverters)
#endif
}
#undef VECTOR_SEQ_CONV
} catch (...) {
LOG_FATAL("Importing this module caused an exception and this module is in an inconsistent state now.");
PyErr_Print();
PyErr_SetString(PyExc_SystemError, __FILE__);
boost::python::handle_exception();
throw;
}
......@@ -39,10 +39,10 @@
#include <core/Subdomain.hpp>
#endif
namespace yade { // Cannot have #include directive inside.
CREATE_CPP_LOCAL_LOGGER("yadeWrapper.cpp");
namespace yade { // Cannot have #include directive inside.
/*
Python normally iterates over object it is has __getitem__ and __len__, which BodyContainer does.
However, it will not skip removed bodies automatically, hence this iterator which does just that.
......@@ -1430,11 +1430,11 @@ public:
};
} // namespace yade
// BOOST_PYTHON_MODULE cannot be inside yade namespace, it has 'extern "C"' keyword, which strips it out of any namespaces.
// BOOST_PYTHON_MODULE cannot be inside yade namespace, it has 'extern "C"' keyword, which strips it out of any namespaces.
BOOST_PYTHON_MODULE(wrapper)
{
try {
using namespace yade; // 'using namespace' inside function keeps namespace pollution under control. Alernatively I could add y:: in front of function names below and put 'namespace y = ::yade;' here.
namespace py = ::boost::python;
py::scope().attr("__doc__") = "Wrapper for c++ internals of yade.";
......@@ -1807,5 +1807,12 @@ BOOST_PYTHON_MODULE(wrapper)
.def("reset", &TimingDeltas::reset, "Reset timing information");
py::scope().attr("O") = pyOmega();
} catch (...) {
LOG_FATAL("Importing this module caused an exception and this module is in an inconsistent state now.");
PyErr_Print();
PyErr_SetString(PyExc_SystemError, __FILE__);
boost::python::handle_exception();
throw;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment