Skip to content

Commit 620af4f

Browse files
committed
Add test for namespaces toml key
1 parent a58388f commit 620af4f

File tree

6 files changed

+129
-0
lines changed

6 files changed

+129
-0
lines changed

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ set(c2py_all_full_tests
4141
basicfun
4242
bug_tpl_type
4343
cls_basic
44+
namespace
4445
cls_der
4546
pointer
4647
comparison

test/namespace.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <c2py/c2py.hpp>
2+
#include <iostream>
3+
4+
namespace A {
5+
void funcA() { std::cout << "Function in namespace A" << std::endl; }
6+
7+
namespace B {
8+
void funcB() { std::cout << "Function in namespace A::B" << std::endl; }
9+
} // namespace B
10+
11+
namespace detail {
12+
void funcDetail() { std::cout << "Function in namespace A::detail" << std::endl; }
13+
} // namespace detail
14+
} // namespace A
15+
16+
#include "namespace.wrap.cxx"

test/namespace.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
documentation = "Test for namespace filtering"
2+
namespaces = "A A::B" # ignore A::detail

test/namespace.wrap.cxx

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
// funcA
30+
static auto const fun_0 = c2py::dispatcher_f_kw_t{c2py::cfun([]() { return A::funcA(); })};
31+
32+
// funcB
33+
static auto const fun_1 = c2py::dispatcher_f_kw_t{c2py::cfun([]() { return A::B::funcB(); })};
34+
35+
static const auto doc_d_0 = fun_0.doc({R"DOC( )DOC"});
36+
static const auto doc_d_1 = fun_1.doc({R"DOC( )DOC"});
37+
//--------------------- module function table -----------------------------
38+
39+
static PyMethodDef module_methods[] = {
40+
{"funcA", (PyCFunction)c2py::pyfkw<fun_0>, METH_VARARGS | METH_KEYWORDS, doc_d_0.c_str()},
41+
{"funcB", (PyCFunction)c2py::pyfkw<fun_1>, METH_VARARGS | METH_KEYWORDS, doc_d_1.c_str()},
42+
{nullptr, nullptr, 0, nullptr} // Sentinel
43+
};
44+
45+
//--------------------- module struct & init error definition ------------
46+
47+
//// module doc directly in the code or "" if not present...
48+
/// Or mandatory ?
49+
static struct PyModuleDef module_def = {PyModuleDef_HEAD_INIT,
50+
"namespace", /* name of module */
51+
R"RAWDOC(Test for namespace filtering)RAWDOC", /* module documentation, may be NULL */
52+
-1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
53+
module_methods,
54+
NULL,
55+
NULL,
56+
NULL,
57+
NULL};
58+
59+
//--------------------- module init function -----------------------------
60+
61+
extern "C" __attribute__((visibility("default"))) PyObject *PyInit_namespace() {
62+
63+
if (not c2py::check_python_version("namespace")) return NULL;
64+
65+
// import numpy iff 'numpy/arrayobject.h' included
66+
#ifdef Py_ARRAYOBJECT_H
67+
import_array();
68+
#endif
69+
70+
PyObject *m;
71+
72+
if (PyType_Ready(&c2py::wrap_pytype<c2py::py_range>) < 0) return NULL;
73+
74+
m = PyModule_Create(&module_def);
75+
if (m == NULL) return NULL;
76+
77+
auto &conv_table = *c2py::conv_table_sptr.get();
78+
79+
conv_table[std::type_index(typeid(c2py::py_range)).name()] = &c2py::wrap_pytype<c2py::py_range>;
80+
81+
return m;
82+
}
83+
#endif
84+
// CLAIR_WRAP_GEN

test/namespace.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_namespace_GUARDS
4+
#define C2PY_HXX_DECLARATION_namespace_GUARDS
5+
6+
#endif

test/namespace_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import namespace as M
2+
import unittest
3+
4+
class TestNamespaceFiltering(unittest.TestCase):
5+
def test_funcA(self):
6+
"""Test that funcA is wrapped and callable."""
7+
self.assertTrue(hasattr(M, "funcA"), "funcA should be wrapped")
8+
M.funcA()
9+
10+
def test_funcB(self):
11+
"""Test that funcB is wrapped and callable."""
12+
self.assertTrue(hasattr(M, "funcB"), "funcB should be wrapped")
13+
M.funcB()
14+
15+
def test_funcDetail(self):
16+
"""Test that funcDetail is NOT wrapped."""
17+
self.assertFalse(hasattr(M, "funcDetail"), "funcDetail should NOT be wrapped")
18+
19+
if __name__ == "__main__":
20+
unittest.main()

0 commit comments

Comments
 (0)