Skip to content

Commit 07aa347

Browse files
committed
Fix tuple bug
- forgot to include a header in c2py.hpp - started a new test to check std container (to be continued)
1 parent 4eebcad commit 07aa347

File tree

6 files changed

+108
-0
lines changed

6 files changed

+108
-0
lines changed

src/c2py/c2py.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "converters/stl/map.hpp"
5454
#include "converters/stl/optional.hpp"
5555
#include "converters/stl/pair.hpp"
56+
#include "converters/stl/tuple.hpp"
5657
#include "converters/stl/set.hpp"
5758
#include "converters/stl/array.hpp"
5859
#include "converters/stl/string.hpp"

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ set(c2py_all_full_tests
5555
tpl_cls
5656
tpl_derived
5757
callback
58+
std_container
5859
two_module_1
5960
two_module_2
6061
CACHE INTERNAL "")

test/std_container.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <c2py/c2py.hpp>
2+
#include <tuple>
3+
4+
std::tuple<int, int> f() { return {1, 2}; }
5+
6+
#include "std_container.wrap.cxx"

test/std_container.wrap.cxx

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
// C.f. https://numpy.org/doc/1.21/reference/c-api/array.html#importing-the-api
3+
#define PY_ARRAY_UNIQUE_SYMBOL _cpp2py_ARRAY_API
4+
#ifndef CLAIR_C2PY_WRAP_GEN
5+
#ifdef __clang__
6+
// #pragma clang diagnostic ignored "-W#warnings"
7+
#endif
8+
#ifdef __GNUC__
9+
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
10+
#pragma GCC diagnostic ignored "-Wcast-function-type"
11+
#pragma GCC diagnostic ignored "-Wcpp"
12+
#endif
13+
14+
#define C2PY_VERSION_MAJOR 0
15+
#define C2PY_VERSION_MINOR 1
16+
17+
#include <c2py/c2py.hpp>
18+
19+
using c2py::operator""_a;
20+
21+
// ==================== Wrapped classes =====================
22+
23+
// ==================== enums =====================
24+
25+
// ==================== module classes =====================
26+
27+
// ==================== module functions ====================
28+
29+
// f
30+
static auto const fun_0 = c2py::dispatcher_f_kw_t{c2py::cfun([]() { return f(); })};
31+
32+
static const auto doc_d_0 = fun_0.doc({R"DOC( )DOC"});
33+
//--------------------- module function table -----------------------------
34+
35+
static PyMethodDef module_methods[] = {
36+
{"f", (PyCFunction)c2py::pyfkw<fun_0>, METH_VARARGS | METH_KEYWORDS, doc_d_0.c_str()},
37+
{nullptr, nullptr, 0, nullptr} // Sentinel
38+
};
39+
40+
//--------------------- module struct & init error definition ------------
41+
42+
//// module doc directly in the code or "" if not present...
43+
/// Or mandatory ?
44+
static struct PyModuleDef module_def = {PyModuleDef_HEAD_INIT,
45+
"std_container", /* name of module */
46+
R"RAWDOC()RAWDOC", /* module documentation, may be NULL */
47+
-1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
48+
module_methods,
49+
NULL,
50+
NULL,
51+
NULL,
52+
NULL};
53+
54+
//--------------------- module init function -----------------------------
55+
56+
extern "C" __attribute__((visibility("default"))) PyObject *PyInit_std_container() {
57+
58+
if (not c2py::check_python_version("std_container")) return NULL;
59+
60+
// import numpy iff 'numpy/arrayobject.h' included
61+
#ifdef Py_ARRAYOBJECT_H
62+
import_array();
63+
#endif
64+
65+
PyObject *m;
66+
67+
if (PyType_Ready(&c2py::wrap_pytype<c2py::py_range>) < 0) return NULL;
68+
69+
m = PyModule_Create(&module_def);
70+
if (m == NULL) return NULL;
71+
72+
auto &conv_table = *c2py::conv_table_sptr.get();
73+
74+
conv_table[std::type_index(typeid(c2py::py_range)).name()] = &c2py::wrap_pytype<c2py::py_range>;
75+
76+
return m;
77+
}
78+
#endif
79+
// CLAIR_WRAP_GEN

test/std_container.wrap.hxx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <c2py/c2py.hpp>
2+
3+
#ifndef C2PY_HXX_DECLARATION_std_container_GUARDS
4+
#define C2PY_HXX_DECLARATION_std_container_GUARDS
5+
6+
#endif

test/std_container_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import unittest
2+
import numpy as np
3+
4+
import std_container as M
5+
6+
class TestIterable(unittest.TestCase):
7+
8+
def test_f(self):
9+
self.assertEqual(M.f(), (1,2))
10+
11+
if __name__ == '__main__':
12+
unittest.main()
13+
14+
15+

0 commit comments

Comments
 (0)