From c886b074554e7f86f9c00764b2491e33c9f8e743 Mon Sep 17 00:00:00 2001 From: Alexey Shlyonskikh Date: Thu, 7 Sep 2023 22:04:44 +0300 Subject: [PATCH] Use core's exception in Python bindings --- src/python_bindings/bindings.cpp | 4 ++++ src/python_bindings/create_dataframe_reader.cpp | 4 +++- src/python_bindings/py_algorithm.cpp | 5 +++-- src/python_bindings/py_to_any.cpp | 9 +++++---- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/python_bindings/bindings.cpp b/src/python_bindings/bindings.cpp index 081242aa6c..141d935447 100644 --- a/src/python_bindings/bindings.cpp +++ b/src/python_bindings/bindings.cpp @@ -36,6 +36,7 @@ #include "algorithms/algorithms.h" #include "algorithms/association_rules/ar.h" +#include "config/exceptions.h" #include "config/tabular_data/input_table_type.h" #include "py_ac_algorithm.h" #include "py_ar_algorithm.h" @@ -91,6 +92,9 @@ PYBIND11_MODULE(desbordante, module) { module.doc() = "A data profiling library"; + py::register_exception(module, "ConfigurationError", + PyExc_ValueError); + py::class_(module, "Table"); py::class_(module, "AssociativeRule") diff --git a/src/python_bindings/create_dataframe_reader.cpp b/src/python_bindings/create_dataframe_reader.cpp index dc4f628bcf..77d1184ed2 100644 --- a/src/python_bindings/create_dataframe_reader.cpp +++ b/src/python_bindings/create_dataframe_reader.cpp @@ -1,5 +1,6 @@ #include "create_dataframe_reader.h" +#include "config/exceptions.h" #include "dataframe_reader.h" namespace python_bindings { @@ -24,7 +25,8 @@ static bool AllColumnsAreStrings(py::handle dataframe) { } config::InputTable CreateDataFrameReader(py::handle dataframe, std::string name) { - if (!IsDataFrame(dataframe)) throw std::invalid_argument("Passed object is not a dataframe"); + if (!IsDataFrame(dataframe)) + throw config::ConfigurationError("Passed object is not a dataframe"); if (AllColumnsAreStrings(dataframe)) { return std::make_shared(dataframe, std::move(name)); } else { diff --git a/src/python_bindings/py_algorithm.cpp b/src/python_bindings/py_algorithm.cpp index 6b46c4d04f..5be0c9b0df 100644 --- a/src/python_bindings/py_algorithm.cpp +++ b/src/python_bindings/py_algorithm.cpp @@ -6,6 +6,7 @@ #include #include "algorithms/algo_factory.h" +#include "config/exceptions.h" #include "config/names.h" #include "config/tabular_data/input_table_type.h" #include "create_dataframe_reader.h" @@ -62,8 +63,8 @@ std::unordered_set PyAlgorithmBase::GetPossibleOptions() const py::tuple PyAlgorithmBase::GetOptionType(std::string_view option_name) const { auto type_index = algorithm_->GetTypeIndex(option_name); if (type_index == void_index) - throw std::invalid_argument{std::string{"Option named \""} + option_name.data() + - "\" doesn't exist!"}; + throw config::ConfigurationError{std::string{"Option named \""} + option_name.data() + + "\" doesn't exist!"}; return GetPyType(type_index); } diff --git a/src/python_bindings/py_to_any.cpp b/src/python_bindings/py_to_any.cpp index 17b8ab1934..f2adcd6dd2 100644 --- a/src/python_bindings/py_to_any.cpp +++ b/src/python_bindings/py_to_any.cpp @@ -9,6 +9,7 @@ #include "algorithms/algebraic_constraints/bin_operation_enum.h" #include "algorithms/metric/enums.h" #include "association_rules/ar_algorithm_enums.h" +#include "config/exceptions.h" #include "config/tabular_data/input_table_type.h" #include "create_dataframe_reader.h" #include "parser/csv_parser/csv_parser.h" @@ -24,7 +25,7 @@ T CastAndReplaceCastError(std::string_view option_name, py::handle value) { try { return py::cast(value); } catch (py::cast_error& e) { - throw std::invalid_argument( + throw config::ConfigurationError( std::string("Unable to cast Python object to C++ type for option \"") + option_name.data() + '"'); } @@ -32,7 +33,7 @@ T CastAndReplaceCastError(std::string_view option_name, py::handle value) { config::InputTable CreateCsvParser(std::string_view option_name, py::tuple const& arguments) { if (py::len(arguments) != 3) { - throw std::invalid_argument("Cannot create a csv parser from passed tuple."); + throw config::ConfigurationError("Cannot create a CSV parser from passed tuple."); } return std::make_shared( @@ -58,7 +59,7 @@ std::pair const EnumConvPair{ std::stringstream error_message; error_message << "Incorrect value for option \"" << option_name << "\". Possible values: " << util::EnumToAvailableValues(); - throw std::invalid_argument(error_message.str()); + throw config::ConfigurationError(error_message.str()); }}; template @@ -83,7 +84,7 @@ std::pair const CharEnumConvPair{ error_message.seekp(-1, std::stringstream::cur); error_message << ']'; - throw std::invalid_argument(error_message.str()); + throw config::ConfigurationError(error_message.str()); }}; boost::any InputTableToAny(std::string_view option_name, py::handle obj) {