Skip to content

Commit ca17d77

Browse files
committed
Improve error messages for all is_convertible functions
1 parent da1762e commit ca17d77

14 files changed

+38
-28
lines changed

c++/cpp2py/converters/basic_types.hpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#pragma once
2-
#include "./../pyref.hpp"
2+
#include "../pyref.hpp"
33
#include "./complex.hpp"
44

55
#include <numpy/arrayobject.h>
@@ -24,7 +24,7 @@ namespace cpp2py {
2424
static bool py2c(PyObject *ob) { return ob == Py_True; }
2525
static bool is_convertible(PyObject *ob, bool raise_exception) {
2626
if (PyBool_Check(ob)) return true;
27-
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to bool"); }
27+
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to bool"s).c_str()); }
2828
return false;
2929
}
3030
};
@@ -46,7 +46,7 @@ namespace cpp2py {
4646
pyref py_arr = PyArray_FromScalar(ob, NULL);
4747
if (PyArray_ISINTEGER((PyObject *)py_arr)) return true;
4848
}
49-
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to integer type"); }
49+
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to integer type"s).c_str()); }
5050
return false;
5151
}
5252
};
@@ -74,7 +74,7 @@ namespace cpp2py {
7474
pyref py_arr = PyArray_FromScalar(ob, NULL);
7575
if (PyArray_ISINTEGER((PyObject*)py_arr) or PyArray_ISFLOAT((PyObject*)py_arr)) return true;
7676
}
77-
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to double"); }
77+
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to double"s).c_str()); }
7878
return false;
7979
}
8080
};

c++/cpp2py/converters/complex.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#pragma once
2-
#include "./../pyref.hpp"
2+
#include "../pyref.hpp"
33

44
#include <numpy/arrayobject.h>
55

@@ -34,7 +34,7 @@ namespace cpp2py {
3434
pyref py_arr = PyArray_FromScalar(ob, NULL);
3535
if (PyArray_ISINTEGER((PyObject*)py_arr) or PyArray_ISFLOAT((PyObject*)py_arr) or PyArray_ISCOMPLEX((PyObject*)py_arr)) return true;
3636
}
37-
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to complex"); }
37+
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to complex"s).c_str()); }
3838
return false;
3939
}
4040
};

c++/cpp2py/converters/function.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22
#include <functional>
3-
#include "./../misc.hpp"
3+
#include "../pyref.hpp"
44

55
namespace cpp2py {
66

@@ -172,7 +172,7 @@ namespace cpp2py {
172172

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

c++/cpp2py/converters/map.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22
#include <map>
33
#include "../traits.hpp"
4+
#include "../pyref.hpp"
45

56
namespace cpp2py {
67

@@ -46,7 +47,7 @@ namespace cpp2py {
4647
return true;
4748
}
4849
_false:
49-
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to std::map"); }
50+
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to std::map"s).c_str()); }
5051
return false;
5152
}
5253

c++/cpp2py/converters/pair.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include "../traits.hpp"
3+
#include "../pyref.hpp"
34

45
namespace cpp2py {
56

@@ -23,7 +24,7 @@ namespace cpp2py {
2324
return true;
2425
}
2526
_false:
26-
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to std::pair"); }
27+
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to std::pair"s).c_str()); }
2728
return false;
2829
}
2930

c++/cpp2py/converters/set.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22
#include <set>
33
#include "../traits.hpp"
4+
#include "../pyref.hpp"
45

56
namespace cpp2py {
67

@@ -35,7 +36,7 @@ namespace cpp2py {
3536
return true;
3637
}
3738
_false:
38-
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to std::set"); }
39+
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to std::set"s).c_str()); }
3940
return false;
4041
}
4142

c++/cpp2py/converters/std_array.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include <array>
3+
#include "../pyref.hpp"
34

45
namespace cpp2py {
56

@@ -39,7 +40,7 @@ namespace cpp2py {
3940
return true;
4041
}
4142
_false:
42-
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to std::array"); }
43+
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to std::array"s).c_str()); }
4344
return false;
4445
}
4546

c++/cpp2py/converters/string.hpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#pragma once
2-
//#include <string>
2+
#include "../pyref.hpp"
33

44
namespace cpp2py {
55

@@ -11,7 +11,7 @@ namespace cpp2py {
1111

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

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

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

5151
static bool is_convertible(PyObject *ob, bool raise_exception) {
5252
if (PyUnicode_Check(ob)) return true;
53-
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to string"); }
53+
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to string"s).c_str()); }
5454
return false;
5555
}
5656
};

c++/cpp2py/converters/tuple.hpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <numeric>
55

66
#include "../traits.hpp"
7+
#include "../pyref.hpp"
78

89
namespace cpp2py {
910

@@ -37,13 +38,13 @@ namespace cpp2py {
3738
public:
3839
static bool is_convertible(PyObject *ob, bool raise_exception) {
3940
if (not PySequence_Check(ob)) {
40-
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert non-sequence to std::tuple"); }
41+
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()); }
4142
return false;
4243
}
4344
pyref seq = PySequence_Fast(ob, "expected a sequence");
4445
// Sizes must match! Could we relax this condition to '<'?
4546
if (PySequence_Fast_GET_SIZE((PyObject *)seq) != std::tuple_size<tuple_t>::value) {
46-
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to std::tuple due to improper length"); }
47+
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to std::tuple due to improper length"s).c_str()); }
4748
return false;
4849
}
4950
return is_convertible_impl((PyObject *)seq, raise_exception, std::make_index_sequence<sizeof...(Types)>());

c++/cpp2py/converters/variant.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <variant>
44

55
#include "../traits.hpp"
6+
#include "../pyref.hpp"
67

78
namespace cpp2py {
89

@@ -58,7 +59,7 @@ namespace cpp2py {
5859

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

c++/cpp2py/converters/vector.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <numpy/arrayobject.h>
55

66
#include "../traits.hpp"
7+
#include "../pyref.hpp"
78
#include "../macros.hpp"
89
#include "../numpy_proxy.hpp"
910

@@ -92,7 +93,7 @@ namespace cpp2py {
9293
}
9394

9495
if (!PySequence_Check(ob)) {
95-
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert a non-sequence to std::vector"); }
96+
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()); }
9697
return false;
9798
}
9899

c++/cpp2py/misc.hpp

-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@
1313

1414
namespace cpp2py {
1515

16-
inline std::string to_string(PyObject * ob){
17-
pyref py_str = PyObject_Str(ob);
18-
return PyUnicode_AsUTF8(py_str);
19-
}
20-
2116
inline char *get_current_time() { // helper function to print the time in the CATCH_AND_RETURN macro
2217
time_t rawtime;
2318
time(&rawtime);

c++/cpp2py/py_converter.hpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "./get_module.hpp"
66
#include "./macros.hpp"
7+
#include "./pyref.hpp"
78

89
#include <string>
910
#include <complex>
@@ -246,7 +247,7 @@ namespace cpp2py {
246247

247248
static bool is_convertible(PyObject *ob, bool raise_exception) {
248249
if (conv_t::is_convertible(ob, false)) return true;
249-
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to ... failed"); }
250+
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to ... failed"s).c_str()); }
250251
return false;
251252
}
252253

@@ -267,7 +268,7 @@ namespace cpp2py {
267268

268269
static bool is_convertible(PyObject *ob, bool raise_exception) {
269270
if (conv_t::is_convertible(ob, false)) return true;
270-
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to ... failed"); }
271+
if (raise_exception) { PyErr_SetString(PyExc_TypeError, ("Cannot convert "s + to_string(ob) + " to ... failed"s).c_str()); }
271272
return false;
272273
}
273274

c++/cpp2py/pyref.hpp

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "./get_module.hpp"
55

66
#include <string>
7+
using namespace std::string_literals;
78

89
namespace cpp2py {
910

@@ -140,4 +141,10 @@ namespace cpp2py {
140141
Py_XINCREF(ob);
141142
return {ob};
142143
}
144+
145+
inline std::string to_string(PyObject * ob){
146+
pyref py_str = PyObject_Str(ob);
147+
return PyUnicode_AsUTF8(py_str);
148+
}
149+
143150
} // namespace cpp2py

0 commit comments

Comments
 (0)