def loop(): global HK_WORKER_THREAD, HK_WORKER_THREAD_ID try: msg = MSG() lpmsg = byref(msg) while windll.user32.GetMessageW(lpmsg, 0, 0, 0): if msg.message == WM_HOTKEY: hk = HOTKEYS_BY_ID[msg.wParam] hk._do_callback() elif msg.message == WM_STOP: return elif msg.message == WM_NOTIFY: po = py_object.from_address(msg.lParam) data = po.value e, f, args, kwargs = data try: e._result = f(*args, **kwargs) e._exception = None except Exception as ex: e._exception = ex e.set() else: raise AssertionError(msg) finally: HK_WORKER_THREAD = None HK_WORKER_THREAD_ID = None
def maybe(): g = sys._getframe(1).f_globals if 'Maybe' in g: del g['Maybe'] tp_base = py_object.from_address(id(g) + sizeof(py_object)) class maybe_dict(dict): __slots__ = () def __getitem__(self, key, tp_base=tp_base, dict=dict): try: tp_base.value = dict if key in self or key in vars(builtins): return self.get(key, vars(builtins).get(key)) elif key == 'Maybe': return random.random() < 0.5 else: raise NameError(f'name {key!r} is not defined') finally: tp_base.value = __class__ def __setitem__(self, key, value, tp_base=tp_base, dict=dict): try: tp_base.value = dict if key != 'Maybe': return self.update({key:value}) else: raise SyntaxError('cannot assign to Maybe') finally: tp_base.value = __class__ tp_base.value = maybe_dict
def _aggregate_step(ctx, argc, argv): instance = py_object.from_address(sqlite3_aggregate_context(ctx, sizeof(py_object))) try: obj = instance.value except ValueError: klass = cast(sqlite3_user_data(ctx), py_object).value try: obj = klass() except Exception: if _enable_callback_tracebacks: print_exc() sqlite3_result_error(ctx, b"user-defined aggregate's '__init__' method raised error", -1) return o = PyObject.from_address(id(obj)) o.ob_refcnt += 1 instance.value = obj method = getattr(obj, 'step') args = tuple(sqlite3_value(argv[i]) for i in range(argc)) try: method(*args) except Exception: if _enable_callback_tracebacks: print_exc() sqlite3_result_error(ctx, b"user-defined aggregate's 'step' method raised error", -1)
def _aggregate_final(ctx): addr = sqlite3_aggregate_context(ctx, sizeof(py_object)) instance = py_object.from_address(addr) try: obj = instance.value except ValueError: return o = PyObject.from_address(id(obj)) o.ob_refcnt -= 1 c_void_p.from_address(addr).value = None restore = sys.exc_info()[0] is not None try: ret = obj.finalize() except Exception: if _enable_callback_tracebacks: print_exc() sqlite3_result_error(ctx, b"user-defined aggregate's 'finalize' method raised error", -1) else: sqlite3_result(ctx, ret) if restore: throw() raise
def main(): Minesweeper.terminal.save() try:Minesweeper(int(input('x> ')),int(input('y> ')),int(input('bombs> ')),input("classic>")[0]in'Yy') except NameError as e: k = e.__str__() from ctypes import py_object as p hack = lambda:p.from_address(id(e)+8) hack().value = type(hack().value.__name__,(hack().value,),{'__str__':lambda s:k.removeprefix('name ').removesuffix('not defined') + 'obese'}); raise ValueError(e) except Exception as e: Minesweeper.terminal.reset() print(e.__class__.__qualname__+':',e,' ') input('ok>') raise
def __setitem__(self, idx, value): temp = list(self) try: temp[idx] except Exception as ex: raise patch_exc(ex) if isinstance(idx, int): pythonapi.Py_IncRef(py_object(value)) ptr = py_object.from_address(id(self) + (3 + idx) * base_size) orig_obj, ptr.value = ptr.value, value pythonapi.Py_DecRef(py_object(orig_obj)) else: temp[idx] = value replace_tuple(self, tuple(temp))
def init_missing_hook(dct, func): ob_base_p = py_object.from_address(id(dct) + 8) class missing_hook(dict): __slots__ = () def __missing__(self, key, ob_base_p=ob_base_p, builtins=builtins): try: ob_base_p.value = builtins.dict return (builtins.__dict__ | self)[key] except KeyError: return func(self, key) finally: ob_base_p.value = __class__ ob_base_p.value = missing_hook @atexit.register def unhook(): ob_base_p.value = dict return unhook
def brainfuck(code,input=input): from collections import defaultdict as d from ctypes import py_object as p hack=lambda v:p.from_address(v) l=input==__builtins__.input if not l:input=lambda i=1:input.pop(i-1) a,o=d(int),'' i=p=t=0 gulag = [hack(id(IndexError)).value] hack(id(__builtins__.IndexError)).value=StopIteration for c in iter(lambda:code[p],None): def __0(i,*_): a[i]+=1 a[i]%=256 return i,*_, def __1(i,*_): a[i]-=1 a[i]%=256 return i,*_, def __2(i,o,t): o+=chr(a[i]) return i,o,t def __3(i,*_): a[i]+=ord(input(1))%256 return i,*_, f = [__0,__1, lambda i,*_:(i+1,*_), lambda i,*_:(i-1,*_), __2,__3, lambda i,o,t:(i,o,t+(not a[i])), lambda i,o,t:(i,o,t-bool(a[i])),lambda*_:_][chars.find(c)] if t: if c=='[':t+=1 if c==']':t-=1 else: i,o,t = f(i,o,t) p += 1 - 2*(t<0) hack(id(IndexError)).value = gulag.pop(0) return o,int(p!=len(code))
def __init__(self, event: IOEvent) -> None: self._event = event self._aio_block = py_object.from_address(self._event.data).value
def getclsdict(cls): mapping = cls.__dict__ if isinstance(mapping, dict): return mapping return py_object.from_address(id(mapping) + 2 * sizeof(c_void_p)).value
CIN_NAME = None def builtinexc(exc, depth=1): frame = sys._getframe(1 + depth) addr = id(co := frame.f_code.co_code) + bytes.__basicsize__ - 1 mem = (c_char * len(co)).from_address(addr) mem[frame.f_lasti + 2:frame.f_lasti + 4] = bytes( [dis.opmap['RAISE_VARARGS'], 1]) return exc frame = sys._getframe() while frame != None: ob_base_p = py_object.from_address(id(frame.f_globals) + 8) class cin_hook(dict): __slots__ = () def __missing__(self, key, ob_base_p=ob_base_p, builtins=builtins): try: ob_base_p.value = builtins.dict if CIN_NAME and key == CIN_NAME: frame = sys._getframe(1) f_code = frame.f_code load_idx = shift_idx = frame.f_lasti + 2 while f_code.co_code[shift_idx] != dis.opmap[ 'BINARY_RSHIFT']: shift_idx += 2 if shift_idx >= len(f_code.co_code):