def error(i, e): var = getvar(cls, 'error') if isinstance(var, staticmethod): r = var.__func__(i, e) if ret: return r return e
def output(i, o): var = getvar(cls, 'output') if isinstance(var, staticmethod): r = var.__func__(i, o) if ret: return r return o
def input(i): var = getvar(cls, 'input') if isinstance(var, staticmethod): r = var.__func__(i) if ret: return r return i
def super_new(mcls, var=getvar(__class__, '__new__')): if var is not None: var = var.__get__(None, mcls) else: var = super(__class__, mcls).__new__ if getattr(var, '__self__', None) is object: var = lambda mcls, *args, **kwargs: object.__new__(mcls) return var
def super_init(self, var=getvar(__class__, '__init__')): if var is not None: var = var.__get__(self, type(self)) else: var = super(__class__, self).__init__ if getattr(var, '__objclass__', None) is object: var = lambda self, *args, **kwargs: object.__init__( self, *args, **kwargs) return var
def wrapper(self, *args, **kwargs): with Lock(self): assert getvar(self, '__withas__', d=0) == i and setvar( self, '__withas__', i + 1) try: return function(self, *args, **kwargs) except BaseException: with Lock(self): assert setvar(self, '__withas__', -1) raise
def __init__(self, *args, **kwargs): if type(self) is __class__: with Lock(self): with Lock(instance): instances = getvar(instance, '__instance__')[cid] assert self in instances.values() try: assert hasvar(self, '__old__') or setvar( self, '__old__', super_init(self)(*args, **kwargs)) return getvar(self, '__old__') except BaseException: with Lock(instance): for k in tuple(instances.keys()): if instances[k] is self: del instances[k] raise else: return super_init(self)(*args, **kwargs)
def __throw(self): generics = self.generics while isinstance(generics, Closure): generics = generics() if inspect.ismodule(generics): name = '__THROW__' elif inspect.isclass(generics): name = '__Throw__' else: name = '__throw__' assert hasvar(generics, name) or setvar(generics, name, set()) return getvar(generics, name)
def __lock(self): generics = self.generics k = self.k while isinstance(generics, Closure): generics = generics() if inspect.ismodule(generics): name = '__LOCK__' elif inspect.isclass(generics): name = '__Lock__' else: name = '__lock__' with Lock.LOCK: assert hasvar(generics, name) or setvar(generics, name, dict()) var = getvar(generics, name) if k not in var: var[k] = threading.RLock() return var[k]
def __new__(mcls, *args, **kwargs): if mcls is __class__: sid = pickle.dumps(( args, sorted(kwargs.items(), key=lambda i: i[0]), )) with Lock(instance): assert hasvar(instance, '__instance__') or setvar( instance, '__instance__', dict()) INSTANCES = getvar(instance, '__instance__') if cid not in INSTANCES: INSTANCES[cid] = dict() instances = INSTANCES[cid] if sid not in instances: instances[sid] = super_new(mcls)(mcls, *args, **kwargs) return instances[sid] else: return super_new(mcls)(mcls, *args, **kwargs)
def Cursor(connect): assert hasvar(connect, '_cursors') or setvar(connect, '_cursors', set()) var = getvar(connect, '_cursors') cursor = connect.cursor(cursor=pymysql.cursors.DictCursor) var.add(cursor) try: yield cursor except BaseException: var.remove(cursor) if not var: connect.rollback() cursor.close() raise else: var.remove(cursor) if not var: connect.commit() cursor.close()