From 974d1b1b0d8cb9a14cf7025ac13da0c202847bb3 Mon Sep 17 00:00:00 2001 From: Julianus Pfeuffer Date: Wed, 21 Dec 2022 14:47:28 +0100 Subject: [PATCH 01/10] Add BoolConverter Check what happens. I think importing it with the same name as python's bool was a bad idea. Makes writing pxds easier though. --- autowrap/ConversionProvider.py | 35 +++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/autowrap/ConversionProvider.py b/autowrap/ConversionProvider.py index f853a1c..e9496ad 100644 --- a/autowrap/ConversionProvider.py +++ b/autowrap/ConversionProvider.py @@ -220,7 +220,6 @@ class IntegerConverter(TypeConverterBase): def get_base_types(self) -> List[str]: return [ "int", - "bool", "long", "int32_t", "ptrdiff_t", @@ -255,6 +254,40 @@ def output_conversion( return "%s = <%s>%s" % (output_py_var, cpp_type, input_cpp_var) +class BooleanConverter(TypeConverterBase): + """ + Wraps a C++ bool. Bools are automatically imported in the beginning of a file with + 'from libcpp import bool'. + """ + + def get_base_types(self) -> List[str]: + return [ + "bool", + ] + + def matches(self, cpp_type: CppType) -> bool: + return not cpp_type.is_ptr + + def matching_python_type(self, cpp_type: CppType) -> str: + return "bool" + + def matching_python_type_full(self, cpp_type: CppType) -> str: + return "bool" + + def type_check_expression(self, cpp_type: CppType, argument_var: str) -> str: + return "isinstance(%s, bool)" % (argument_var,) + + def input_conversion(self, cpp_type, argument_var, arg_num) -> Tuple[str, str, str]: + code = "" + call_as = "(<%s>%s)" % (cpp_type, argument_var) + cleanup = "" + return code, call_as, cleanup + + def output_conversion( + self, cpp_type: CppType, input_cpp_var: str, output_py_var: str + ) -> Optional[str]: + return "%s = <%s>%s" % (output_py_var, cpp_type, input_cpp_var) + class UnsignedIntegerConverter(TypeConverterBase): """ wraps unsigned long and int. "long" base_type is converted to "int" by the From c29cb88255873fe871db80ef86e685453cf0c666 Mon Sep 17 00:00:00 2001 From: Julianus Pfeuffer Date: Wed, 21 Dec 2022 14:48:56 +0100 Subject: [PATCH 02/10] add to list of CPs --- autowrap/ConversionProvider.py | 1 + 1 file changed, 1 insertion(+) diff --git a/autowrap/ConversionProvider.py b/autowrap/ConversionProvider.py index e9496ad..ef11618 100644 --- a/autowrap/ConversionProvider.py +++ b/autowrap/ConversionProvider.py @@ -2267,6 +2267,7 @@ def setup_converter_registry(classes_to_wrap, enums_to_wrap, instance_map): ) converters.register(IntegerConverter()) + converters.register(BooleanConverter()) converters.register(UnsignedIntegerConverter()) converters.register(FloatConverter()) converters.register(DoubleConverter()) From 87aa161353bdfc514837deb34966cdd900090070 Mon Sep 17 00:00:00 2001 From: Julianus Pfeuffer Date: Wed, 21 Dec 2022 14:59:13 +0100 Subject: [PATCH 03/10] lint --- autowrap/ConversionProvider.py | 1 + 1 file changed, 1 insertion(+) diff --git a/autowrap/ConversionProvider.py b/autowrap/ConversionProvider.py index ef11618..5a97473 100644 --- a/autowrap/ConversionProvider.py +++ b/autowrap/ConversionProvider.py @@ -220,6 +220,7 @@ class IntegerConverter(TypeConverterBase): def get_base_types(self) -> List[str]: return [ "int", + "bint", # C boolean type "long", "int32_t", "ptrdiff_t", From 80a49dd1e90800d5b6c3d428340888604e1a183f Mon Sep 17 00:00:00 2001 From: Julianus Pfeuffer Date: Wed, 21 Dec 2022 14:59:58 +0100 Subject: [PATCH 04/10] lint --- autowrap/ConversionProvider.py | 1 + 1 file changed, 1 insertion(+) diff --git a/autowrap/ConversionProvider.py b/autowrap/ConversionProvider.py index 5a97473..8e4823a 100644 --- a/autowrap/ConversionProvider.py +++ b/autowrap/ConversionProvider.py @@ -289,6 +289,7 @@ def output_conversion( ) -> Optional[str]: return "%s = <%s>%s" % (output_py_var, cpp_type, input_cpp_var) + class UnsignedIntegerConverter(TypeConverterBase): """ wraps unsigned long and int. "long" base_type is converted to "int" by the From a1aa89fe79f506329df163c0c5b94643a680ca54 Mon Sep 17 00:00:00 2001 From: Julianus Pfeuffer Date: Wed, 21 Dec 2022 15:07:51 +0100 Subject: [PATCH 05/10] Try importing the python bool type with different name --- autowrap/CodeGenerator.py | 5 +++-- autowrap/ConversionProvider.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/autowrap/CodeGenerator.py b/autowrap/CodeGenerator.py index 921d6e7..4bac9aa 100644 --- a/autowrap/CodeGenerator.py +++ b/autowrap/CodeGenerator.py @@ -2100,8 +2100,9 @@ def create_default_cimports(self): |#Generated with autowrap %s and Cython (Parser) %s |#cython: c_string_encoding=ascii |#cython: embedsignature=False - |from enum import Enum as _PyEnum - |from cpython cimport Py_buffer + |from enum import Enum as _PyEnum + |from cpython cimport Py_buffer + |from cpython import bool as pybool_t |from libcpp.string cimport string as libcpp_string |from libcpp.string cimport string as libcpp_utf8_string |from libcpp.string cimport string as libcpp_utf8_output_string diff --git a/autowrap/ConversionProvider.py b/autowrap/ConversionProvider.py index 8e4823a..df79807 100644 --- a/autowrap/ConversionProvider.py +++ b/autowrap/ConversionProvider.py @@ -276,7 +276,7 @@ def matching_python_type_full(self, cpp_type: CppType) -> str: return "bool" def type_check_expression(self, cpp_type: CppType, argument_var: str) -> str: - return "isinstance(%s, bool)" % (argument_var,) + return "isinstance(%s, pybool_t)" % (argument_var,) def input_conversion(self, cpp_type, argument_var, arg_num) -> Tuple[str, str, str]: code = "" From 9254f4bec36dbbbaba81b24c2a2475a0c0454303 Mon Sep 17 00:00:00 2001 From: Julianus Pfeuffer Date: Wed, 21 Dec 2022 15:12:35 +0100 Subject: [PATCH 06/10] cimport? --- autowrap/CodeGenerator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autowrap/CodeGenerator.py b/autowrap/CodeGenerator.py index 4bac9aa..0259ebf 100644 --- a/autowrap/CodeGenerator.py +++ b/autowrap/CodeGenerator.py @@ -2102,7 +2102,7 @@ def create_default_cimports(self): |#cython: embedsignature=False |from enum import Enum as _PyEnum |from cpython cimport Py_buffer - |from cpython import bool as pybool_t + |from cpython cimport bool as pybool_t |from libcpp.string cimport string as libcpp_string |from libcpp.string cimport string as libcpp_utf8_string |from libcpp.string cimport string as libcpp_utf8_output_string From 9734e854f9ab96b128c4a985e827b1db31abf0b5 Mon Sep 17 00:00:00 2001 From: Julianus Pfeuffer Date: Wed, 21 Dec 2022 15:29:32 +0100 Subject: [PATCH 07/10] add test --- tests/test_code_generator_minimal.py | 4 ++++ tests/test_files/minimal.hpp | 1 + tests/test_files/minimal.pxd | 1 + 3 files changed, 6 insertions(+) diff --git a/tests/test_code_generator_minimal.py b/tests/test_code_generator_minimal.py index 77ee1c8..af33100 100644 --- a/tests/test_code_generator_minimal.py +++ b/tests/test_code_generator_minimal.py @@ -105,6 +105,10 @@ def output_conversion(self, cpp_type, input_cpp_var, output_py_var): minimal.m_accessible = 10 assert minimal.m_accessible == 10 + minimal.m_bool = True + assert isinstance(minimal.m_bool, bool) + assert minimal.m_bool == True + try: minimal.m_const = 10 assert False diff --git a/tests/test_files/minimal.hpp b/tests/test_files/minimal.hpp index 4b3e20c..54688ef 100644 --- a/tests/test_files/minimal.hpp +++ b/tests/test_files/minimal.hpp @@ -19,6 +19,7 @@ class Minimal { static const int m_const = -1; static const int m_constdef = -1; int m_accessible; + bool m_bool; Minimal(); Minimal(Int); diff --git a/tests/test_files/minimal.pxd b/tests/test_files/minimal.pxd index 7254f94..f494d0c 100644 --- a/tests/test_files/minimal.pxd +++ b/tests/test_files/minimal.pxd @@ -22,6 +22,7 @@ cdef extern from "minimal.hpp": Minimal(Minimal &) int m_accessible + bool m_bool int m_const # wrap-constant const int m_constdef From 7546c503b422f9d90b158be197b76e1b7347f542 Mon Sep 17 00:00:00 2001 From: Julianus Pfeuffer Date: Wed, 21 Dec 2022 15:31:29 +0100 Subject: [PATCH 08/10] forgot changes for test --- tests/test_files/minimal.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/test_files/minimal.cpp b/tests/test_files/minimal.cpp index 9d406c5..f6776c1 100644 --- a/tests/test_files/minimal.cpp +++ b/tests/test_files/minimal.cpp @@ -1,16 +1,16 @@ #include #include "minimal.hpp" -Minimal::Minimal() : _i(0), _mi(), m_accessible(0) +Minimal::Minimal() : _i(0), _mi(), m_accessible(0), m_bool(false) { } -Minimal::Minimal(std::vector const & ii): _mi(), m_accessible(0) +Minimal::Minimal(std::vector const & ii): _mi(), m_accessible(0), m_bool(false) { _i = ii.size(); } -Minimal::Minimal(int i) : _i(i), _mi(), m_accessible(0) +Minimal::Minimal(int i) : _i(i), _mi(), m_accessible(0), m_bool(false) { } @@ -19,6 +19,7 @@ Minimal::Minimal(const Minimal &m) _i = m._i; _mi = m._mi; m_accessible = m.m_accessible; + m_bool = m.m_bool; } From 4cfad5f0c3667e58fd63511bcef098513df5fb1e Mon Sep 17 00:00:00 2001 From: Julianus Pfeuffer Date: Wed, 21 Dec 2022 15:36:04 +0100 Subject: [PATCH 09/10] import bool in pxd --- tests/test_files/minimal.pxd | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_files/minimal.pxd b/tests/test_files/minimal.pxd index f494d0c..3786d80 100644 --- a/tests/test_files/minimal.pxd +++ b/tests/test_files/minimal.pxd @@ -1,6 +1,7 @@ # cython: language_level=2 from libcpp.string cimport string as libcpp_string from libcpp.vector cimport vector as libcpp_vector +from libcpp cimport bool from libc.string cimport const_char from minimal_td cimport * From 7a62e8f0abfdd94aadf274a96f4efc36c4fe590d Mon Sep 17 00:00:00 2001 From: Julianus Pfeuffer Date: Wed, 21 Dec 2022 15:45:39 +0100 Subject: [PATCH 10/10] linting --- autowrap/ConversionProvider.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autowrap/ConversionProvider.py b/autowrap/ConversionProvider.py index df79807..54cf806 100644 --- a/autowrap/ConversionProvider.py +++ b/autowrap/ConversionProvider.py @@ -220,7 +220,7 @@ class IntegerConverter(TypeConverterBase): def get_base_types(self) -> List[str]: return [ "int", - "bint", # C boolean type + "bint", # C boolean type "long", "int32_t", "ptrdiff_t",