Add tp_call support to the python API; corresponding gccxml support is TODO.
This commit is contained in:
1 parent
6d97fbf2f3
commit
c00f408eb4
4 files changed
+31
-1
No files matched your search
+13
-1
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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__':
|
||||
|
||||
Reference in new issue
Block a user