-2

I'm porting a python script to .NET to make it easier to mantain and this is the first time i'm actually doing anything in python so I am heavily checking stackoverflow for every line I don't understand and I can't seem to figure this out at all.

I got a handler object which gets set by getattr() when name is None?

As far as I can tell from reading the code the name will never be None as it's set above properly so what does the getattr() actually do here?

if the instruction object was None? (I guess that's like null/Nothing?) wouldn't it raise a exception before hitting the getattr() line and then again I don't believe instruction can ever be None, if mnemonic is None the string would still look like on_ so name will still technically never be None. So handler will never be None then why is it possible here?

    name = 'on_%s' % instruction.mnemonic
    handler = getattr(self, name, None)

    if handler is None:
        self.on_fail('Not implemented', instruction)
        return False

about 30 lines down in python the handler is used one last time here.

    if handler(instruction) is False:
        self.on_fail('Handler skipped', instruction)

Seems now the handler can accept a instruction Class i can't find any handler class that has __init__ with instruction in it either.

Figured out a little it seems it's used with some global function mappers

on_cmovae = on_cmovcc
on_cmova = on_cmovcc
on_cmovbe = on_cmovcc
on_cmovb = on_cmovcc
on_cmovg = on_cmovcc
on_cmovge = on_cmovcc
on_cmovl = on_cmovcc
on_cmovle = on_cmovcc
on_cmove = on_cmovcc
on_cmovne = on_cmovcc
on_cmovs = on_cmovcc

which map back to a function

def on_cmovcc(self, i):
    self.writer.putlnc('if (%s)', i.get_condition_value())
    self.writer.indent()
    self.set_op(i.op1, i.op2.get())
    self.writer.dedent()

Okay it seems getattr() is used to map to function calls by string name

SSpoke
  • 5,656
  • 10
  • 72
  • 124
  • Did you read the documentation? [`getattr`](https://docs.python.org/2/library/functions.html#getattr) gets an attribute from an object and the third argument `None` is a default value in case there is no requested attribute. `getattr` isn not a setter. – gre_gor Sep 11 '15 at 00:42
  • These documentations to me are very hard to understand I can't bridge the connection from english to actual comprehension so I base all my knowledge on trail and error and I usually figure it out – SSpoke Sep 11 '15 at 01:09

1 Answers1

0

Okay it seems that you use getattr() to map function calls by string name to a handler function which is a wrapper for the function you are trying to map over.

SSpoke
  • 5,656
  • 10
  • 72
  • 124