def __new__(cls, *targs, **kargs): from types import MethodType as method msh = object.__new__(cls) if cls is Marshal or 'dump' not in cls.__dict__: msh.dump = method(_marshal.dump, msh, cls) if cls is Marshal or 'load' not in cls.__dict__: msh.load = method(_marshal.load, msh, cls) return msh
def __new__(cls, *targs, **kargs): from types import MethodType as method pkl = object.__new__(cls) if cls is Pickle or 'dump' not in cls.__dict__: pkl.dump = method(_pickle.dump, pkl, cls) if cls is Pickle or 'load' not in cls.__dict__: pkl.load = method(_pickle.load, pkl, cls) return pkl
def testNormalFunctions(self): #{{{ '''Normal function or unbound method''' _ = lambda: None ub = method(_, None, None) for f in (_, ub): cw = CallableWrapper(f) self.assertFalse(cw._get_ismethod())
def testNormalFunction(self): #{{{ '''Return back normal functions and unbound methods''' _ = (lambda: None) ub_ = method(_, None, None) for f in (_, ub_): cw = CallableWrapper(f) ret = cw._getref()() self.assertEqual(ret, f)
def testUnboundMethod(self): #{{{ '''Unbound method''' class A(object): pass def _(s): #{{{ pass # End def #}}} _ = method(_, None, None) self.assertEqual(methodtype(_), METHODTYPE_UNBOUND)
def testClassMethodWrap(self): # {{{ """Wrapping class method""" class A(object): def helloworld(self, a, b): return "hello world" w = CallableWrapper(A.helloworld) w.wrap(DummyReplacement) A.helloworld = method(w, None, A) z = CallableWrapper(A.helloworld) z.wrap(DummyReplacement) A.helloworld = method(z, None, A) a = A() self.assertEqual(a.helloworld(1, 2), "HELLO: HELLO: hello world :WORLD :WORLD")
def copymethod(meth, name = None): if isinstance(meth, method): if not name: name = meth.func_name tempmeth = copyfunction(meth, name) return method(tempmeth, None) names = tuple(o.__class__.__name__ for o in (meth, name)) raise TypeError("Cannot copy method = %s with name = %s" %names)
def testInstanceMethodWrap(self): # {{{ """Wrapping instance method""" class A(object): def helloworld(self, a, b): return "hello world" a = A() w = CallableWrapper(a.helloworld) w.wrap(DummyReplacement) a.helloworld = method(w, a, A) z = CallableWrapper(a.helloworld) z.wrap(DummyReplacement) a.helloworld = method(z, a, A) self.assertEqual(a.helloworld(1, 2), "HELLO: HELLO: hello world :WORLD :WORLD") self.assertEqual(A().helloworld(1, 2), "hello world")
def wrap(self, func): #{{{ if not iscallable(func): raise TypeError('Argument must be a valid callable object') newfunc = func(self._newcall) if not iscallable(newfunc): raise TypeError('Return value of wrapping callable must be a valid callable object') if not _ism(newfunc): newfunc = method(newfunc, self, self.__class__) self._newcall = newfunc
def testNormalFunction(self): #{{{ '''Normal functions and unbound methods''' def _(): #{{{ pass # End def #}}} funcs = ((_, METHODTYPE_NOTMETHOD), (method(_, None, None), METHODTYPE_UNBOUND)) for f, mt in funcs: cw = CallableWrapper(f) self.assertTrue(cw._object is None) self.assertTrue(cw._function) self.assertTrue(isinstance(cw._function, cref)) self.assertEqual(cw._function(), f) self.assertEqual(cw._methodtype, mt)
def _get_original(self): #{{{ if self.isdead: raise ValueError("Cannot retrieve original: dead reference") im_self = None im_class = None cwo = self._object if cwo: obj = cwo() if isclass(obj): im_class = obj else: im_self, im_class = obj, obj.__class__ gc_ret = self._getcallable() if im_self or im_class: return method(gc_ret, im_self, im_class) return gc_ret
def testUnWrap(self): # {{{ """Unwrap""" class A(object): def helloworld(self, a, b): return "hello world" a = A() w = CallableWrapper(a.helloworld) w.wrap(DummyReplacement) a.helloworld = method(w, a, A) self.assertEqual(a.helloworld(1, 2), "HELLO: hello world :WORLD") self.assertEqual(A().helloworld(1, 2), "hello world") w.unwrap() self.assertEqual(A().helloworld(1, 2), a.helloworld(1, 2))
def __get__(self, obj, type_): if obj is None: return self.klass return method(self.klass, obj)
def testNonInstClass(self): #{{{ '''Non-method instance/class objects returns None''' nonmeth = lambda s: None unbound = method(nonmeth, None, None) for nm in (nonmeth, unbound): self.assertTrue(methodname(nm) is None)
def main(): class A(OverridesBase): def __init__(self, **kwds): "You're average constructor" self.__dict__.update(kwds) def b(mcls, self): "You're average method" pass def c(self): "You're average function" pass @classmethod def d(self): "You're average classmethod" pass @property def e(self): "You're average property" return None A.b = method(A.b, type) try: class B(A): @override def b(self): print("b") except Exception as e: print(type(e).__name__, "raised while overriding method 'b' via class B:", e) try: class C(A): @override def __init__(self, h=None, **kwds): super().__init__(**{'h':a, **kwds}) @override def c(self): print("c") except Exception as e: print(type(e).__name__, "raised while overriding function 'c':", e) try: class D(A): @override @classmethod def d(self): print("d") except Exception as e: print(type(e).__name__, "raised while overriding classmethod 'd':", e) try: class E(A): @override @property def e(self): return "e" except Exception as e: print(type(e).__name__, "raised while overriding property 'e':", e) try: class F(A): @override @classmethod def b(mcls, self): return "f" except Exception as e: print(type(e).__name__, "raised while overriding method 'b' via class F:", e)