From c00f408eb413cb2b2483e36779cefc89980122e5 Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Tue, 12 Aug 2008 15:52:49 +0100 Subject: [PATCH] Add tp_call support to the python API; corresponding gccxml support is TODO. --- pybindgen/cppclass.py | 14 +++++++++++++- tests/foo.h | 6 ++++++ tests/foomodulegen.py | 6 ++++++ tests/footest.py | 6 ++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/pybindgen/cppclass.py b/pybindgen/cppclass.py index 1d39350..2cb9660 100644 --- a/pybindgen/cppclass.py +++ b/pybindgen/cppclass.py @@ -985,6 +985,9 @@ public: method.class_ = self if method.visibility == 'public': + if name == '__call__': # needs special handling + method.force_parse = method.PARSE_TUPLE_AND_KEYWORDS + try: overload = self.methods[name] except KeyError: @@ -1428,6 +1431,14 @@ typedef struct { else: dict_.setdefault("tp_name", '%s.%s' % (self.outer_class.slots['tp_name'], self.name)) + ## tp_call support + try: + call_method = self.methods['__call__'] + except KeyError: + pass + else: + dict_.setdefault("tp_call", call_method.wrapper_actual_name) + self.pytype.generate(code_sink) @@ -1485,7 +1496,8 @@ typedef struct { utils.call_with_error_handling(overload.generate, (code_sink,), {}, overload) except utils.SkipWrapper: continue - method_defs.append(overload.get_py_method_def(meth_name)) + if meth_name not in ['__call__']: # need to be converted to slots + method_defs.append(overload.get_py_method_def(meth_name)) code_sink.writeln() method_defs.extend(parent_caller_methods) ## generate the method table diff --git a/tests/foo.h b/tests/foo.h index 87a3256..6b3b8a3 100644 --- a/tests/foo.h +++ b/tests/foo.h @@ -256,6 +256,12 @@ public: return message.size (); } + // -#- @message(direction=inout) -#- + int operator() (std::string& message) { + message = m_prefix + message; + return message.size (); + } + // -------- Virtual methods ---------- virtual std::string get_prefix () const { return m_prefix; diff --git a/tests/foomodulegen.py b/tests/foomodulegen.py index c55e83c..f3d4e7b 100755 --- a/tests/foomodulegen.py +++ b/tests/foomodulegen.py @@ -119,6 +119,12 @@ int %s::custom_method_added_by_a_hook(int x) SomeObject.add_constructor([Parameter.new('std::string', 'prefix')]) SomeObject.add_constructor([Parameter.new('int', 'prefix_len')]) + SomeObject.add_method('operator()', ReturnValue.new('int'), + [Parameter.new('std::string&', 'message', + direction=Parameter.DIRECTION_INOUT)], + custom_name='__call__') + + # --- some virtual methods --- SomeObject.add_method('get_prefix', ReturnValue.new('std::string'), [], is_virtual=True, is_const=True) diff --git a/tests/footest.py b/tests/footest.py index 3ec7d87..2de5a2a 100644 --- a/tests/footest.py +++ b/tests/footest.py @@ -773,6 +773,12 @@ class TestFoo(unittest.TestCase): self.fail() else: self.assertEqual(foo.IntegerTypeNameGet(), 'int') + + def test_operator_call(self): + obj = foo.SomeObject("xpto_") + l, s = obj("hello") + self.assertEqual(s, "xpto_hello") + self.assertEqual(l, len("xpto_hello")) if __name__ == '__main__':