From ceb8f35ec3b9484b13d752169c7e0c9ede03203d Mon Sep 17 00:00:00 2001 From: wirawan Date: Wed, 8 Jun 2011 15:29:14 +0000 Subject: [PATCH] * Creating wpylib.py module to contain all python-level hacks. Submodules are available for specialized and more complicated hacks. --- py/__init__.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 py/__init__.py diff --git a/py/__init__.py b/py/__init__.py new file mode 100644 index 0000000..191e6da --- /dev/null +++ b/py/__init__.py @@ -0,0 +1,32 @@ +# $Id: __init__.py,v 1.1 2011-06-08 15:29:14 wirawan Exp $ +# +# wpylib.py module +# Created: 20110608 +# Wirawan Purwanto +# +# Module space for advanced tools requiring low-level python hacks. +# + +import sys +import new + + +def make_unbound_method(method): + """Generates an unbound instance method from a possibly bound + instance method.""" + return new.instancemethod(method.im_func, None, method.im_class) + + +def make_unbound_method(method): + """Generates an unbound instance method from a possibly bound + instance method, or even user-defined function. + This is useful for injecting external, completely unrelated function + as an instance procedure into a class, without having to subclass + the whole thing. + CAVEAT: This is a haram trick. Know what you are doing.""" + try: + return new.instancemethod(method.im_func, None, method.im_class) + except AttributeError: + # Assume this is a static method or user-defined external method + # injected into this class. + return method