def __init__(self, variable_argument=False): self._va = variable_argument self.plug = Plug() self.random_names = {}
def __init__(self, variable_argument = False): self._va = variable_argument self.plug = Plug() self.random_names = {}
class Caller(object): def __init__(self, variable_argument=False): self._va = variable_argument self.plug = Plug() self.random_names = {} def _get_func(self, name, types): add = '' if self._va: add = 'VARIABLE_ARGUMENT("KERNEL_%s")' % (name, ) args = ', '.join(["arg%d" % (i, ) for i in xrange(len(types))]) decl = '\n\t'.join( [types[i] % ("arg%d" % (i, ), ) for i in xrange(len(types))]) func = ''' ANONYMOUS("caller_func") %s def caller_func(ret, %s): array(ret, 1) %s try: ret[0] = KERNEL_%s(%s) except exp: return exp return 0 ''' % (add, args, decl, name, args) return self.plug.compile(func)[0] def _put_func(self, func): func.unload() def __getitem__(self, n): if not isinstance(n, str): raise Exception("Not a string") def caller(*args): ret = ctypes.c_buffer('\0' * WORD_SIZE) new_args = [] types = [] bufs = [] for arg in args: if isinstance(arg, str): buf = ctypes.c_buffer(arg + '\0') types.append('buffer(%%s, %d)' % (len(arg) + 1, )) new_args.append(ctypes.addressof(buf)) bufs.append(buf) else: types.append('word(%s)') new_args.append(arg) func = self._get_func(n, types) err = func(ctypes.addressof(ret), *new_args) self._put_func(func) if err: if err >= len(Plug.ERROR_TABLE): raise Exception("Error: 0x%x" % err) raise Exception(Plug.ERROR_TABLE[err]) return struct.unpack("P", ret.raw[:WORD_SIZE])[0] return caller
class Caller(object): def __init__(self, variable_argument = False): self._va = variable_argument self.plug = Plug() self.random_names = {} def _get_func(self, name, types): add = '' if self._va: add = 'VARIABLE_ARGUMENT("KERNEL_%s")' % (name, ) args = ', '.join(["arg%d" % (i, ) for i in xrange(len(types))]) decl = '\n\t'.join([types[i] % ("arg%d" % (i, ), ) for i in xrange(len(types))]) func = ''' ANONYMOUS("caller_func") %s def caller_func(ret, %s): array(ret, 1) %s try: ret[0] = KERNEL_%s(%s) except exp: return exp return 0 ''' % (add, args, decl, name, args) return self.plug.compile(func)[0] def _put_func(self, func): func.unload() def __getitem__(self, n): if not isinstance(n, str): raise Exception("Not a string") def caller(*args): ret = ctypes.c_buffer('\0'*WORD_SIZE) new_args = [] types = [] bufs = [] for arg in args: if isinstance(arg, str): buf = ctypes.c_buffer(arg + '\0') types.append('buffer(%%s, %d)' % (len(arg) + 1, )) new_args.append(ctypes.addressof(buf)) bufs.append(buf) else: types.append('word(%s)') new_args.append(arg) func = self._get_func(n, types) err = func(ctypes.addressof(ret), *new_args) self._put_func(func) if err: if err >= len(Plug.ERROR_TABLE): raise Exception("Error: 0x%x" % err) raise Exception(Plug.ERROR_TABLE[err]) return struct.unpack("P", ret.raw[:WORD_SIZE])[0] return caller