Am 25. Oktober 2011 10:18 schrieb Iustin Pop <[email protected]>:
> My questions is whether we need to "generate" code at all, instead of
> parsing the descriptor tables and doing the serialisation at run time,
> e.g.:
> […]
Short version: it is possible, but makes debugging harder and the code
difficult to understand. My proposal is to proceed with the generated
code and change if and when we find an acceptable solution.
As discussed we would have to override the normal case at least for
“test_delay”, and possibly others as well. To test this I defined two
classes (only representing the bare minimum here):
class TestRunnerBase(object):
def __getattr__(self, name):
…
class TestRunner(TestRunnerBase):
def call_test_delay(self, *args):
return TestRunnerBase.call_test_delay(self, *args)
When trying to call “call_test_delay” on the derived class it fails
with “'TestRunnerBase' has no attribute 'call_test_delay'”. The reason
is that the function is actually only available on instances of
TestRunnerBase, not on the class itself (as used here). There are two
ways around this:
- Define “getattr” in a metaclasses: This will be tricky with
bound/unbound methods. I specifically wanted to avoid using
metaclasses as they lead to magic.
- Call base class' “__getattr__” explicitely:
TestRunnerBase.__getattr__(self, "call_test_delay")…. While it works,
it's not nice.
Therefore I'd rather proceed with code generated at build time for
now, which behaves like any other standard Python code at runtime.
There is no magic involved.
Michael