Skip to content

Commit

Permalink
Improve error messages for all is_convertible functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Wentzell committed Aug 5, 2020
1 parent da1762e commit ca17d77
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 28 deletions.
8 changes: 4 additions & 4 deletions c++/cpp2py/converters/basic_types.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include "./../pyref.hpp"
#include "../pyref.hpp"
#include "./complex.hpp"

#include <numpy/arrayobject.h>
Expand All @@ -24,7 +24,7 @@ namespace cpp2py {
static bool py2c(PyObject *ob) { return ob == Py_True; }
static bool is_convertible(PyObject *ob, bool raise_exception) {
if (PyBool_Check(ob)) return true;
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to bool"); }
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to bool"s).c_str()); }
return false;
}
};
Expand All @@ -46,7 +46,7 @@ namespace cpp2py {
pyref py_arr = PyArray_FromScalar(ob, NULL);
if (PyArray_ISINTEGER((PyObject *)py_arr)) return true;
}
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to integer type"); }
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to integer type"s).c_str()); }
return false;
}
};
Expand Down Expand Up @@ -74,7 +74,7 @@ namespace cpp2py {
pyref py_arr = PyArray_FromScalar(ob, NULL);
if (PyArray_ISINTEGER((PyObject*)py_arr) or PyArray_ISFLOAT((PyObject*)py_arr)) return true;
}
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to double"); }
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to double"s).c_str()); }
return false;
}
};
Expand Down
4 changes: 2 additions & 2 deletions c++/cpp2py/converters/complex.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include "./../pyref.hpp"
#include "../pyref.hpp"

#include <numpy/arrayobject.h>

Expand Down Expand Up @@ -34,7 +34,7 @@ namespace cpp2py {
pyref py_arr = PyArray_FromScalar(ob, NULL);
if (PyArray_ISINTEGER((PyObject*)py_arr) or PyArray_ISFLOAT((PyObject*)py_arr) or PyArray_ISCOMPLEX((PyObject*)py_arr)) return true;
}
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to complex"); }
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to complex"s).c_str()); }
return false;
}
};
Expand Down
4 changes: 2 additions & 2 deletions c++/cpp2py/converters/function.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include <functional>
#include "./../misc.hpp"
#include "../pyref.hpp"

namespace cpp2py {

Expand Down Expand Up @@ -172,7 +172,7 @@ namespace cpp2py {

static bool is_convertible(PyObject *ob, bool raise_exception) {
if (PyCallable_Check(ob)) return true;
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to std::function a non callable object"); }
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " std::function as it is not callable"s).c_str()); }
return false;
}

Expand Down
3 changes: 2 additions & 1 deletion c++/cpp2py/converters/map.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include <map>
#include "../traits.hpp"
#include "../pyref.hpp"

namespace cpp2py {

Expand Down Expand Up @@ -46,7 +47,7 @@ namespace cpp2py {
return true;
}
_false:
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to std::map"); }
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to std::map"s).c_str()); }
return false;
}

Expand Down
3 changes: 2 additions & 1 deletion c++/cpp2py/converters/pair.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "../traits.hpp"
#include "../pyref.hpp"

namespace cpp2py {

Expand All @@ -23,7 +24,7 @@ namespace cpp2py {
return true;
}
_false:
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to std::pair"); }
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to std::pair"s).c_str()); }
return false;
}

Expand Down
3 changes: 2 additions & 1 deletion c++/cpp2py/converters/set.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include <set>
#include "../traits.hpp"
#include "../pyref.hpp"

namespace cpp2py {

Expand Down Expand Up @@ -35,7 +36,7 @@ namespace cpp2py {
return true;
}
_false:
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to std::set"); }
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to std::set"s).c_str()); }
return false;
}

Expand Down
3 changes: 2 additions & 1 deletion c++/cpp2py/converters/std_array.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <array>
#include "../pyref.hpp"

namespace cpp2py {

Expand Down Expand Up @@ -39,7 +40,7 @@ namespace cpp2py {
return true;
}
_false:
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to std::array"); }
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to std::array"s).c_str()); }
return false;
}

Expand Down
10 changes: 5 additions & 5 deletions c++/cpp2py/converters/string.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
//#include <string>
#include "../pyref.hpp"

namespace cpp2py {

Expand All @@ -11,7 +11,7 @@ namespace cpp2py {

static bool is_convertible(PyObject *ob, bool raise_exception) {
if (PyUnicode_Check(ob) or PyUnicode_Check(ob)) return true;
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to string"); }
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to string"s).c_str()); }
return false;
}
};
Expand All @@ -24,7 +24,7 @@ namespace cpp2py {

static bool is_convertible(PyObject *ob, bool raise_exception) {
if (PyUnicode_Check(ob) and PyUnicode_GET_LENGTH(ob) == 1) return true;
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to char"); }
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to char"s).c_str()); }
return false;
}
};
Expand All @@ -37,7 +37,7 @@ namespace cpp2py {

static bool is_convertible(PyObject *ob, bool raise_exception) {
if (PyBytes_Check(ob) and PyBytes_Size(ob) == 1) return true;
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to unsigned char"); }
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to unsigned char"s).c_str()); }
return false;
}
};
Expand All @@ -50,7 +50,7 @@ namespace cpp2py {

static bool is_convertible(PyObject *ob, bool raise_exception) {
if (PyUnicode_Check(ob)) return true;
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to string"); }
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to string"s).c_str()); }
return false;
}
};
Expand Down
5 changes: 3 additions & 2 deletions c++/cpp2py/converters/tuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <numeric>

#include "../traits.hpp"
#include "../pyref.hpp"

namespace cpp2py {

Expand Down Expand Up @@ -37,13 +38,13 @@ namespace cpp2py {
public:
static bool is_convertible(PyObject *ob, bool raise_exception) {
if (not PySequence_Check(ob)) {
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert non-sequence to std::tuple"); }
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to std::tuple as it is not a sequence"s).c_str()); }
return false;
}
pyref seq = PySequence_Fast(ob, "expected a sequence");
// Sizes must match! Could we relax this condition to '<'?
if (PySequence_Fast_GET_SIZE((PyObject *)seq) != std::tuple_size<tuple_t>::value) {
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to std::tuple due to improper length"); }
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to std::tuple due to improper length"s).c_str()); }
return false;
}
return is_convertible_impl((PyObject *)seq, raise_exception, std::make_index_sequence<sizeof...(Types)>());
Expand Down
3 changes: 2 additions & 1 deletion c++/cpp2py/converters/variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <variant>

#include "../traits.hpp"
#include "../pyref.hpp"

namespace cpp2py {

Expand Down Expand Up @@ -58,7 +59,7 @@ namespace cpp2py {

static bool is_convertible(PyObject *ob, bool raise_exception) {
if ((... or py_converter<std::decay_t<T>>::is_convertible(ob, false))) return true;
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to std::variant"); }
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to std::variant"s).c_str()); }
return false;
}

Expand Down
3 changes: 2 additions & 1 deletion c++/cpp2py/converters/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <numpy/arrayobject.h>

#include "../traits.hpp"
#include "../pyref.hpp"
#include "../macros.hpp"
#include "../numpy_proxy.hpp"

Expand Down Expand Up @@ -92,7 +93,7 @@ namespace cpp2py {
}

if (!PySequence_Check(ob)) {
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert a non-sequence to std::vector"); }
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to std::vector as it is not a sequence"s).c_str()); }
return false;
}

Expand Down
5 changes: 0 additions & 5 deletions c++/cpp2py/misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@

namespace cpp2py {

inline std::string to_string(PyObject * ob){
pyref py_str = PyObject_Str(ob);
return PyUnicode_AsUTF8(py_str);
}

inline char *get_current_time() { // helper function to print the time in the CATCH_AND_RETURN macro
time_t rawtime;
time(&rawtime);
Expand Down
5 changes: 3 additions & 2 deletions c++/cpp2py/py_converter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "./get_module.hpp"
#include "./macros.hpp"
#include "./pyref.hpp"

#include <string>
#include <complex>
Expand Down Expand Up @@ -246,7 +247,7 @@ namespace cpp2py {

static bool is_convertible(PyObject *ob, bool raise_exception) {
if (conv_t::is_convertible(ob, false)) return true;
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to ... failed"); }
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to ... failed"s).c_str()); }
return false;
}

Expand All @@ -267,7 +268,7 @@ namespace cpp2py {

static bool is_convertible(PyObject *ob, bool raise_exception) {
if (conv_t::is_convertible(ob, false)) return true;
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to ... failed"); }
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to ... failed"s).c_str()); }
return false;
}

Expand Down
7 changes: 7 additions & 0 deletions c++/cpp2py/pyref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "./get_module.hpp"

#include <string>
using namespace std::string_literals;

namespace cpp2py {

Expand Down Expand Up @@ -140,4 +141,10 @@ namespace cpp2py {
Py_XINCREF(ob);
return {ob};
}

inline std::string to_string(PyObject * ob){
pyref py_str = PyObject_Str(ob);
return PyUnicode_AsUTF8(py_str);
}

} // namespace cpp2py

0 comments on commit ca17d77

Please sign in to comment.