all repos — mgba @ 7b66a702cfe96f34ef2842b1c9c6f6826738be5e

mGBA Game Boy Advance Emulator

Python: Reduce boilerplate
Vicki Pfau vi@endrift.com
Wed, 31 May 2017 17:04:24 -0700
commit

7b66a702cfe96f34ef2842b1c9c6f6826738be5e

parent

06d89aee48c7b985f95e49185bbf2eef99ace94e

2 files changed, 17 insertions(+), 4 deletions(-)

jump to
M src/platform/python/mgba/__init__.pysrc/platform/python/mgba/__init__.py

@@ -0,0 +1,15 @@

+# Copyright (c) 2013-2017 Jeffrey Pfau +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +from ._pylib import ffi, lib + +def createCallback(structName, cbName, funcName=None): + funcName = funcName or "_py{}{}".format(structName, cbName.capitalize()) + fullStruct = "struct {}*".format(structName) + def cb(handle, *args): + h = ffi.cast(fullStruct, handle) + getattr(ffi.from_handle(h.pyobj), cbName)(*args) + + return ffi.def_extern(name=funcName)(cb)
M src/platform/python/mgba/log.pysrc/platform/python/mgba/log.py

@@ -4,11 +4,9 @@ # This Source Code Form is subject to the terms of the Mozilla Public

# License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. from ._pylib import ffi, lib +from . import createCallback -@ffi.def_extern() -def _pyLog(logger, category, level, message): - l = ffi.cast("struct mLoggerPy*", logger) - ffi.from_handle(l.pyobj).log(category, level, ffi.string(message).decode('UTF-8')) +createCallback("mLoggerPy", "log", "_pyLog") def installDefault(logger): lib.mLogSetDefaultLogger(logger._native)