A function contained within some type.
| 67 | return Function.pyentry(self.type, self.args, self.name, self.docs) + s(body, indent=4) |
| 68 | |
| 69 | class Method(Function): |
| 70 | '''A function contained within some type.''' |
| 71 | def __init__(self, type, container, method_name, args, body='', docs='', pyname=None, static=False, getter=False): |
| 72 | self.container = container |
| 73 | self.method_name = method_name |
| 74 | self.static = static |
| 75 | super().__init__(type, f'{self.container}_{self.method_name}', args, body, docs) |
| 76 | if pyname is None: |
| 77 | self.pyname = self.method_name |
| 78 | else: |
| 79 | self.pyname = pyname |
| 80 | self.getter = getter |
| 81 | |
| 82 | def to_swig(self): |
| 83 | result = s(f'''\ |
| 84 | %newobject {self.method_name}; |
| 85 | {self.type.to_swig()} {self.method_name}({', '.join(a.to_swig() for a in self.args[1:])}); |
| 86 | ''') |
| 87 | return result |
| 88 | |
| 89 | def to_python(self): |
| 90 | if self.static: |
| 91 | args = self.args |
| 92 | else: |
| 93 | args = [Var(self.args[0].type, 'self')] + self.args[1:] |
| 94 | pyargs = ', '.join(a.type.wrap_python_value(a.name) for a in args) |
| 95 | |
| 96 | body = f'result = _lib.{self.name}({pyargs})\n' |
| 97 | body += '_check_errors()\n' |
| 98 | body += self.type.python_postfix() |
| 99 | body += 'return result\n' |
| 100 | if self.static: |
| 101 | pre = '@staticmethod\n' |
| 102 | elif self.getter: |
| 103 | pre = '@property\n' |
| 104 | else: |
| 105 | pre = '' |
| 106 | return pre + Function.pyentry(self.type, args, self.pyname, self.docs) + s(body, indent=4) |
| 107 | |
| 108 | class FunctionWrapper(Function): |
| 109 | def __init__(self, program, type, name, args): |