Пример #1
0
Файл: aop.py Проект: ytig/python
 def error(i, e):
     var = getvar(cls, 'error')
     if isinstance(var, staticmethod):
         r = var.__func__(i, e)
         if ret:
             return r
     return e
Пример #2
0
Файл: aop.py Проект: ytig/python
 def output(i, o):
     var = getvar(cls, 'output')
     if isinstance(var, staticmethod):
         r = var.__func__(i, o)
         if ret:
             return r
     return o
Пример #3
0
Файл: aop.py Проект: ytig/python
 def input(i):
     var = getvar(cls, 'input')
     if isinstance(var, staticmethod):
         r = var.__func__(i)
         if ret:
             return r
     return i
Пример #4
0
 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
Пример #5
0
 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
Пример #6
0
 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
Пример #7
0
 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)
Пример #8
0
 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)
Пример #9
0
 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]
Пример #10
0
 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)
Пример #11
0
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()