Skip to content

Commit d4fd150

Browse files
authored
Merge pull request #174 from OpenMS/jpfeuffer-patch-1
Try to add real BooleanConverter
2 parents 9bfd55e + 7a62e8f commit d4fd150

File tree

6 files changed

+51
-6
lines changed

6 files changed

+51
-6
lines changed

autowrap/CodeGenerator.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2100,8 +2100,9 @@ def create_default_cimports(self):
21002100
|#Generated with autowrap %s and Cython (Parser) %s
21012101
|#cython: c_string_encoding=ascii
21022102
|#cython: embedsignature=False
2103-
|from enum import Enum as _PyEnum
2104-
|from cpython cimport Py_buffer
2103+
|from enum import Enum as _PyEnum
2104+
|from cpython cimport Py_buffer
2105+
|from cpython cimport bool as pybool_t
21052106
|from libcpp.string cimport string as libcpp_string
21062107
|from libcpp.string cimport string as libcpp_utf8_string
21072108
|from libcpp.string cimport string as libcpp_utf8_output_string

autowrap/ConversionProvider.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ class IntegerConverter(TypeConverterBase):
220220
def get_base_types(self) -> List[str]:
221221
return [
222222
"int",
223-
"bool",
223+
"bint", # C boolean type
224224
"long",
225225
"int32_t",
226226
"ptrdiff_t",
@@ -255,6 +255,41 @@ def output_conversion(
255255
return "%s = <%s>%s" % (output_py_var, cpp_type, input_cpp_var)
256256

257257

258+
class BooleanConverter(TypeConverterBase):
259+
"""
260+
Wraps a C++ bool. Bools are automatically imported in the beginning of a file with
261+
'from libcpp import bool'.
262+
"""
263+
264+
def get_base_types(self) -> List[str]:
265+
return [
266+
"bool",
267+
]
268+
269+
def matches(self, cpp_type: CppType) -> bool:
270+
return not cpp_type.is_ptr
271+
272+
def matching_python_type(self, cpp_type: CppType) -> str:
273+
return "bool"
274+
275+
def matching_python_type_full(self, cpp_type: CppType) -> str:
276+
return "bool"
277+
278+
def type_check_expression(self, cpp_type: CppType, argument_var: str) -> str:
279+
return "isinstance(%s, pybool_t)" % (argument_var,)
280+
281+
def input_conversion(self, cpp_type, argument_var, arg_num) -> Tuple[str, str, str]:
282+
code = ""
283+
call_as = "(<%s>%s)" % (cpp_type, argument_var)
284+
cleanup = ""
285+
return code, call_as, cleanup
286+
287+
def output_conversion(
288+
self, cpp_type: CppType, input_cpp_var: str, output_py_var: str
289+
) -> Optional[str]:
290+
return "%s = <%s>%s" % (output_py_var, cpp_type, input_cpp_var)
291+
292+
258293
class UnsignedIntegerConverter(TypeConverterBase):
259294
"""
260295
wraps unsigned long and int. "long" base_type is converted to "int" by the
@@ -2234,6 +2269,7 @@ def setup_converter_registry(classes_to_wrap, enums_to_wrap, instance_map):
22342269
)
22352270

22362271
converters.register(IntegerConverter())
2272+
converters.register(BooleanConverter())
22372273
converters.register(UnsignedIntegerConverter())
22382274
converters.register(FloatConverter())
22392275
converters.register(DoubleConverter())

tests/test_code_generator_minimal.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ def output_conversion(self, cpp_type, input_cpp_var, output_py_var):
105105
minimal.m_accessible = 10
106106
assert minimal.m_accessible == 10
107107

108+
minimal.m_bool = True
109+
assert isinstance(minimal.m_bool, bool)
110+
assert minimal.m_bool == True
111+
108112
try:
109113
minimal.m_const = 10
110114
assert False

tests/test_files/minimal.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#include <iostream>
22
#include "minimal.hpp"
33

4-
Minimal::Minimal() : _i(0), _mi(), m_accessible(0)
4+
Minimal::Minimal() : _i(0), _mi(), m_accessible(0), m_bool(false)
55
{
66
}
77

8-
Minimal::Minimal(std::vector<int> const & ii): _mi(), m_accessible(0)
8+
Minimal::Minimal(std::vector<int> const & ii): _mi(), m_accessible(0), m_bool(false)
99
{
1010
_i = ii.size();
1111
}
1212

13-
Minimal::Minimal(int i) : _i(i), _mi(), m_accessible(0)
13+
Minimal::Minimal(int i) : _i(i), _mi(), m_accessible(0), m_bool(false)
1414
{
1515
}
1616

@@ -19,6 +19,7 @@ Minimal::Minimal(const Minimal &m)
1919
_i = m._i;
2020
_mi = m._mi;
2121
m_accessible = m.m_accessible;
22+
m_bool = m.m_bool;
2223
}
2324

2425

tests/test_files/minimal.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Minimal {
1919
static const int m_const = -1;
2020
static const int m_constdef = -1;
2121
int m_accessible;
22+
bool m_bool;
2223

2324
Minimal();
2425
Minimal(Int);

tests/test_files/minimal.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# cython: language_level=2
22
from libcpp.string cimport string as libcpp_string
33
from libcpp.vector cimport vector as libcpp_vector
4+
from libcpp cimport bool
45
from libc.string cimport const_char
56

67
from minimal_td cimport *
@@ -22,6 +23,7 @@ cdef extern from "minimal.hpp":
2223
Minimal(Minimal &)
2324

2425
int m_accessible
26+
bool m_bool
2527
int m_const # wrap-constant
2628
const int m_constdef
2729

0 commit comments

Comments
 (0)