Skip to content

Commit

Permalink
Merge pull request #150 from OpenMS/feat/typingSpecialMethods
Browse files Browse the repository at this point in the history
[FEATURE] add docs to stubs and add stubs for most special methods plus refactor
  • Loading branch information
jpfeuffer authored Apr 22, 2022
2 parents 9415d71 + a03e0a3 commit 5982e82
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 239 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
autowrap 0.22.8
- Added support for missing operators '-', '/', '-=', '*=', '/='
- Refactored special method generation a little, with minor fixes.
- Finished complete support for type stubs for all? generated Cython code
7 changes: 3 additions & 4 deletions autowrap/Code.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,16 @@ def __init__(self):
self.content = []

def extend(self, other):
# keeps identation
# keeps indentation
self.content.extend(other.content)

def add(self, what, *a, **kw):
# may increase indent !
# may increase indent!
if a: # if dict given
kw.update(a[0])
if "self" in kw:
del kw["self"] # self causes problems in substitute call below
if isinstance(what, basestring):
# print repr(what)
try:
res = string.Template(what).substitute(**kw)
except:
Expand All @@ -78,7 +77,7 @@ def add(self, what, *a, **kw):
res = re.sub(r"\n+ *\+", "", res)
for line in re.split(r"\n *\|", res):
self.content.append(line.rstrip())
else:
else: # TODO do we really want to allow adding "ANYTHING" e.g. even None?
self.content.append(what)
return self

Expand Down
407 changes: 180 additions & 227 deletions autowrap/CodeGenerator.py

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions autowrap/ConversionProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def matching_python_type_full(self, cpp_type):
return "int"

def type_check_expression(self, cpp_type, argument_var):
return "isinstance(%s, (int, long))" % (argument_var,)
return "isinstance(%s, int)" % (argument_var,)

def input_conversion(self, cpp_type, argument_var, arg_num):
code = ""
Expand Down Expand Up @@ -322,7 +322,7 @@ def matching_python_type_full(self, cpp_type):
return "int"

def type_check_expression(self, cpp_type, argument_var):
return "isinstance(%s, (int, long)) and %s >= 0" % (argument_var, argument_var,)
return "isinstance(%s, int) and %s >= 0" % (argument_var, argument_var,)

def input_conversion(self, cpp_type, argument_var, arg_num):
code = ""
Expand Down
2 changes: 1 addition & 1 deletion autowrap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def generate_code(decls, instance_map, target, debug=False, manual_code=None,
allDecl=allDecl,
add_relative=add_relative,
shared_ptr=shared_ptr)
gen.include_numpy=include_numpy
gen.include_numpy = include_numpy
gen.create_pyx_file(debug)
includes = gen.get_include_dirs(include_boost)
print("Autowrap has wrapped %s classes, %s methods and %s enums" % (
Expand Down
10 changes: 7 additions & 3 deletions tests/test_code_generator_minimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ def output_conversion(self, cpp_type, input_cpp_var, output_py_var):
# That is what we expected, cant modify a const member
pass


assert minimal.compute(3) == 4
# overloaded for float:
assert minimal.compute(0.0) == 42.0
Expand Down Expand Up @@ -224,6 +223,11 @@ def output_conversion(self, cpp_type, input_cpp_var, output_py_var):
assert b == m1
assert c == m3

c, b, a = list(reversed(m2)) # call __reversed__
assert a == m2
assert b == m1
assert c == m3

assert m2.test2Lists([m1], [1, 2]) == 3
assert m1 == m1

Expand All @@ -247,10 +251,10 @@ def output_conversion(self, cpp_type, input_cpp_var, output_py_var):

# Was OverflowError, but now we check positivity for unsigned ints
with pytest.raises(AssertionError):
m2[-1]
print(m2[-1])

with pytest.raises(IndexError):
m2[3]
print(m2[3])

# operator add
assert wrapped.Minimal(1) + wrapped.Minimal(2) == wrapped.Minimal(3)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_code_generator_stllibcpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def test_stl_libcpp():

# Test shared_ptr < const Widget >
const_res = t.process_11_const()
const_res.i_ == 42
assert const_res.i_ == 42

# Part 6
# Test std::map< string, IntWrapper >
Expand Down
9 changes: 9 additions & 0 deletions tests/test_files/minimal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,15 @@ std::vector<Minimal>::iterator Minimal::end()
return this->_mi.end();
}

std::vector<Minimal>::reverse_iterator Minimal::rbegin()
{
return this->_mi.rbegin();
}
std::vector<Minimal>::reverse_iterator Minimal::rend()
{
return this->_mi.rend();
}

long int Minimal::run_static(long int i, bool)
{
return i*4;
Expand Down
3 changes: 3 additions & 0 deletions tests/test_files/minimal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ class Minimal {
std::vector<Minimal>::iterator begin();
std::vector<Minimal>::iterator end();

std::vector<Minimal>::reverse_iterator rbegin();
std::vector<Minimal>::reverse_iterator rend();

Minimal create() const;

Minimal & getRef();
Expand Down
5 changes: 4 additions & 1 deletion tests/test_files/minimal.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,16 @@ cdef extern from "minimal.hpp":
unsigned int test_special_converter(unsigned int)

void setVector(libcpp_vector[Minimal]) # wrap-doc:Sets vector
libcpp_vector[Minimal] getVector()# wrap-doc:Gets vector
libcpp_vector[Minimal] getVector() # wrap-doc:Gets vector

int test2Lists(const libcpp_vector[Minimal]&, libcpp_vector[int])

libcpp_vector[Minimal].iterator begin() # wrap-iter-begin:__iter__(Minimal)
libcpp_vector[Minimal].iterator end() # wrap-iter-end:__iter__(Minimal)

libcpp_vector[Minimal].reverse_iterator rbegin() # wrap-iter-begin:__reversed__(Minimal)
libcpp_vector[Minimal].reverse_iterator rend() # wrap-iter-end:__reversed__(Minimal)

int operator()(Minimal) # wrap-cast:toInt

int size()
Expand Down

0 comments on commit 5982e82

Please sign in to comment.