Skip to content

Commit 439576a

Browse files
committed
tp_names for STL converters
1 parent 91e85f7 commit 439576a

File tree

9 files changed

+52
-1
lines changed

9 files changed

+52
-1
lines changed

src/c2py/converters/stl/any.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ namespace c2py {
77

88
template <> struct py_converter<std::any> {
99

10+
static constexpr const char *tp_name = "capsule";
11+
1012
static void capsule_destructor(PyObject *capsule) {
1113
void *p = PyCapsule_GetPointer(capsule, "std::any");
1214
assert(p);

src/c2py/converters/stl/array.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ namespace c2py {
2424

2525
template <typename T, size_t R> struct py_converter<std::array<T, R>> {
2626

27+
static std::string tp_name() {
28+
std::ostringstream out;
29+
out << "[" << python_typename<T>() << ", len = " << R << "]";
30+
return out.str();
31+
}
32+
2733
static PyObject *c2py(std::array<T, R> const &v) {
2834
PyObject *list = PyList_New(0);
2935
for (auto const &x : v) {

src/c2py/converters/stl/function.hpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ namespace c2py {
1717

1818
// a dispatcher for this std::function
1919
template <typename R, typename... T>
20-
static const auto std_function_call = dispatcher_f_kw_t{cfun(&std::function<R(T...)>::operator(), empty_string<T>...)};
20+
static const auto std_function_call = dispatcher_f_kw_t {
21+
cfun(&std::function<R(T...)>::operator(), empty_string<T>...)
22+
};
2123

2224
} // namespace detail
2325

@@ -35,6 +37,15 @@ namespace c2py {
3537
static_assert(concepts::IsConvertibleC2Py<R>, "Return type not convertible");
3638
static_assert((concepts::IsConvertiblePy2C<T> and ... and true), "Parameter not convertible");
3739

40+
static std::string tp_name() {
41+
std::ostringstream out;
42+
std::string sep;
43+
out << "(";
44+
((out << sep << python_typename<T>(), sep = ", "), ...);
45+
out << ") -> " << python_typename<R>();
46+
return out.str();
47+
}
48+
3849
using c_t = std::function<R(T...)>;
3950
using py_t = wrap<c_t>;
4051

src/c2py/converters/stl/map.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ namespace c2py {
2727

2828
template <typename K, typename V> struct py_converter<std::map<K, V>> {
2929

30+
static std::string tp_name() {
31+
std::ostringstream out;
32+
std::string sep;
33+
out << "dict[" << python_typename<K>() << " -> " << python_typename<V>() << "]";
34+
return out.str();
35+
}
36+
3037
template <typename M>
3138
static PyObject *c2py(M &&m)
3239
requires(concepts::IsConvertibleC2Py<V>)

src/c2py/converters/stl/optional.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ namespace c2py {
2424

2525
template <typename T> struct py_converter<std::optional<T>> {
2626

27+
static std::string tp_name() {
28+
std::ostringstream out;
29+
out << python_typename<T>() << " | None";
30+
return out.str();
31+
}
32+
2733
using conv = py_converter<std::decay_t<T>>;
2834

2935
template <typename O> static PyObject *c2py(O &&op) {

src/c2py/converters/stl/set.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ namespace c2py {
2424

2525
template <typename K> struct py_converter<std::set<K>> {
2626

27+
static std::string tp_name() {
28+
std::ostringstream out;
29+
std::string sep;
30+
out << "set[" << python_typename<K>() << "]";
31+
return out.str();
32+
}
33+
2734
template <typename S> static PyObject *c2py(S &&s) {
2835
static_assert(is_instantiation_of_v<std::set, std::decay_t<S>>, "Logic error");
2936
PyObject *set = PySet_New(nullptr);

src/c2py/converters/stl/span.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
namespace c2py {
2424

2525
template <> struct py_converter<std::span<std::byte>> {
26+
27+
static constexpr const char *tp_name = "[bytes]";
28+
2629
// --------------------------------------
2730

2831
static bool is_convertible(PyObject *ob, bool raise_exception) {

src/c2py/converters/stl/variant.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ namespace c2py {
2727
// converts in the first possible type
2828
template <typename... T> struct py_converter<std::variant<T...>> {
2929

30+
static std::string tp_name() {
31+
std::ostringstream out;
32+
std::string sep;
33+
((out << sep << python_typename<T>(), sep = " | "), ...);
34+
return out.str();
35+
}
36+
3037
private:
3138
template <int N> using types_t = std::tuple_element_t<N, std::tuple<T...>>;
3239

src/c2py/converters/stl/vector.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ namespace c2py {
6868

6969
template <> struct py_converter<std::vector<std::byte>> {
7070

71+
static constexpr const char *tp_name = "[bytes]";
72+
7173
static PyObject *c2py(std::vector<std::byte> const &v) {
7274
auto *char_ptr = reinterpret_cast<const char *>(v.data()); // NOLINT
7375
return PyBytes_FromStringAndSize(char_ptr, Py_ssize_t(v.size()));

0 commit comments

Comments
 (0)