MCPcopy Create free account
hub / github.com/battlecode/battlecode-2018 / to_python

Method to_python

bindings/frankenswig/struct.py:270–316  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

268 return definition
269
270 def to_python(self):
271 start = s(f'''\
272 class {sanitize_rust_name(self.name)}(object):
273 __slots__ = ['_ptr']
274 ''')
275
276 if self.constructor_:
277 cargs = [Var(self.type, 'self')] + self.constructor_.args
278 cinit = Function.pyentry(
279 self.type,
280 cargs,
281 '__init__',
282 self.constructor_.docs
283 )
284 cpyargs = ', '.join(a.type.wrap_python_value(a.name) for a in cargs[1:])
285 cbody = f'ptr = _lib.{self.constructor_.name}({cpyargs})\n'
286 cbody += 'if ptr != _ffi.NULL: self._ptr = ptr\n'
287 cbody += '_check_errors()\n'
288 else:
289 cinit = Function.pyentry(
290 self.type,
291 [Var(self.type, 'self')],
292 '__init__',
293 'INVALID: this object cannot be constructed from Python code!'
294 )
295 cbody = 'raise TypeError("This object cannot be constructed from Python code!")'
296
297 constructor = cinit + s(cbody, indent=4) + '\n'
298
299 dinit = Function.pyentry(void.type, [Var(self.type, 'self')], '__del__', 'Clean up the object.')
300 dbody = s(f'''\
301 if hasattr(self, '_ptr'):
302 # if there was an error in the constructor, we'll have no _ptr
303 _lib.{self.destructor.name}(self._ptr)
304 _check_errors()
305 ''', indent=4)
306
307 definition = dinit + dbody
308
309 definition += '\n'.join('@property\n' + getter.to_python() for getter in self.getters) + '\n'
310 definition += '\n'.join(f'@{setter.pyname}.setter\n' + setter.to_python()
311 for setter in self.setters) + '\n'
312 definition += '\n'.join(method.to_python() for method in self.methods) + '\n'
313
314 extra = '\n' + '\n'.join(self.pyextra_)
315
316 return start + s(constructor + definition + extra, indent=4)

Callers 4

serializeMethod · 0.45
debugMethod · 0.45
cloneMethod · 0.45
eqMethod · 0.45

Calls 5

sFunction · 0.85
sanitize_rust_nameFunction · 0.85
VarClass · 0.85
pyentryMethod · 0.80
wrap_python_valueMethod · 0.45

Tested by

no test coverage detected