ayon-core/openpype/lib/python_2_comp.py
Jakub Trllo 248336bb0d
General: Lib code cleanup (#5003)
* implemented 'is_func_signature_supported' function

* 'WeakMethod' can be imported from 'python_2_comp' all the time

* simplified events logic for callback registration

* modified docstrings in publish lib

* removed unused imports

* fixed 'run_openpype_process' docstring
2023-05-24 10:51:35 +02:00

44 lines
1.3 KiB
Python

import weakref
WeakMethod = getattr(weakref, "WeakMethod", None)
if WeakMethod is None:
class _WeakCallable:
def __init__(self, obj, func):
self.im_self = obj
self.im_func = func
def __call__(self, *args, **kws):
if self.im_self is None:
return self.im_func(*args, **kws)
else:
return self.im_func(self.im_self, *args, **kws)
class WeakMethod:
""" Wraps a function or, more importantly, a bound method in
a way that allows a bound method's object to be GCed, while
providing the same interface as a normal weak reference. """
def __init__(self, fn):
try:
self._obj = weakref.ref(fn.im_self)
self._meth = fn.im_func
except AttributeError:
# It's not a bound method
self._obj = None
self._meth = fn
def __call__(self):
if self._dead():
return None
return _WeakCallable(self._getobj(), self._meth)
def _dead(self):
return self._obj is not None and self._obj() is None
def _getobj(self):
if self._obj is None:
return None
return self._obj()