Dump initial cut of code into github.

This commit is contained in:
Graham Dumpleton
2013-08-05 20:58:35 +08:00
parent 4b9cd87e2f
commit bc16501fa1
17 changed files with 1702 additions and 0 deletions

5
.gitignore vendored
View File

@@ -18,6 +18,11 @@ develop-eggs
lib
lib64
man
include
.Python
MANIFEST
# Installer logs
pip-log.txt

11
setup.py Normal file
View File

@@ -0,0 +1,11 @@
from distutils.core import setup
setup(name = 'wrapt',
version = '0.9',
description = 'Module for decorators, wrappers and monkey patching.',
author = 'Graham Dumpleton',
author_email = 'Graham.Dumpleton@gmail.com',
license = 'BSD',
url = 'https://github.com/GrahamDumpleton/wrapt',
packages = ['wrapt'],
)

7
tox.ini Normal file
View File

@@ -0,0 +1,7 @@
[tox]
envlist =
[testenv]
deps = nose
commands =
nosetests -v []

5
wrapt/__init__.py Normal file
View File

@@ -0,0 +1,5 @@
from .wrappers import GenericWrapper, FunctionWrapper, InstanceMethodWrapper
from .decorators import (generic_decorator, function_decorator,
instancemethod_decorator)
from .exceptions import (UnexpectedDefaultParameters, MissingDefaultParameter,
UnexpectedParameters)

159
wrapt/decorators.py Normal file
View File

@@ -0,0 +1,159 @@
"""This module implements a set of decorators for implementing other
decorators. These can be generic decorators where there is never a specific
requirement to reliably process the inbound arguments being passed to the
wrapped function, or more specific decorators designed just for functions,
instance methods etc.
"""
from functools import wraps, partial
from inspect import getargspec
from .wrappers import GenericWrapper, FunctionWrapper, InstanceMethodWrapper
from .exceptions import (UnexpectedDefaultParameters, MissingDefaultParameter,
UnexpectedParameters)
def _decorator_factory(wrapper_type):
# This decorator factory serves as a way of parameterising our
# decorators based on the wrapper type. The wrapper type determines
# whether or not descriptor behaviour is supported and the number of
# arguments which are then in turn passed to the wrapper function
# supplied by the user, which implements their decorator. In other
# words, it saves us from duplicating all of this code for the
# different decorator types we provide for constructing the users
# decorators.
def _decorator_binder(wrapper=None, adapter=None, **default_params):
# The binder works out whether the user decorator will have its
# own parameters. Parameters for the user decorator must always
# be specified using keyword arguments and must always have
# defaults. The user cannot use 'wrapper' or 'adapter' for their
# own parameters as we use them ourselves and so they are
# effectively reserved. The 'wrapper' argument being how the
# user's wrapper function is passed in. The 'adapter' argument
# is used to optionally denote a function which is an adapter,
# which changes the effective prototype of the wrapped function.
# The latter is used to ensure that any function argument
# specification returned by the final result of any decorator is
# correct and reflects that of the adapter and not the wrapped
# function.
if wrapper is not None:
# The wrapper has been provided, so we must also have any
# optional default keyword parameters for the user decorator
# at this point if they were supplied. Before constructing
# the decorator we validate if the list of supplied default
# parameters are actually the same as what the users wrapper
# function expects.
expected_arglist = wrapper_type.WRAPPER_ARGLIST
complete_arglist = getargspec(wrapper).args
received_names = set(default_params.keys())
expected_names = complete_arglist[len(expected_arglist):]
for name in expected_names:
try:
received_names.remove(name)
except KeyError:
raise MissingDefaultParameter('Expected value for '
'default parameter %r was not supplied for '
'decorator %r.' % (name, wrapper.__name__))
if received_names:
raise UnexpectedDefaultParameters('Unexpected default '
'parameters %r supplied for decorator %r.' % (
list(received_names), wrapper.__name__))
# If we do have default parameters, the final decorator we
# create needs to be constructed a bit differently as when
# that decorator is used, it needs to accept parameters.
# Those parameters need not be supplied, but at least an
# empty argument list needs to be used on the decorator at
# that point. When parameters are supplied, they can be as
# either positional or keyword arguments.
if len(complete_arglist) > len(expected_arglist):
# For the case where the decorator is able to accept
# parameters, return a partial wrapper to collect the
# parameters.
@wraps(wrapper)
def _partial(*decorator_args, **decorator_kwargs):
# Since the supply of parameters is optional due to
# having defaults, we need to construct a final set
# of parameters by overlaying those finally supplied
# to the decorator at the point of use over the
# defaults. As we accept positional parameters, we
# need to translate those back to keyword parameters
# in the process. This allows us to pass just one
# dictionary of parameters and we can validate the
# set of parameters at the point the decorator is
# used and not only let it fail at the time the
# wrapped function is called.
if len(decorator_args) > len(expected_names):
raise UnexpectedParameters('Expected at most %r '
'positional parameters for decorator %r, '
'but received %r.' % (len(expected_names),
wrapper.__name__, len(decorator_args)))
unexpected_params = []
for name in decorator_kwargs:
if name not in default_params:
unexpected_params.append(name)
if unexpected_params:
raise UnexpectedParameters('Unexpected parameters '
'%r supplied for decorator %r.' % (
unexpected_params, wrapper.__name__))
complete_params = dict(default_params)
for i, arg in enumerate(decorator_args):
if expected_names[i] in decorator_kwargs:
raise UnexpectedParameters('Positional parameter '
'%r also supplied as keyword parameter '
'to decorator %r.' % (expected_names[i],
wrapper.__name__))
decorator_kwargs[expected_names[i]] = arg
complete_params.update(decorator_kwargs)
# Now create and return the final wrapper which
# combines the parameters with the wrapped function.
def _wrapper(func):
return wrapper_type(wrapped=func, wrapper=wrapper,
adapter=adapter, params=complete_params)
return _wrapper
# Here is where the partial wrapper is returned. This is
# effectively the users decorator.
return _partial
else:
# No parameters so create and return the final wrapper.
# This is effectively the users decorator.
@wraps(wrapper)
def _wrapper(func):
return wrapper_type(wrapped=func, wrapper=wrapper,
adapter=adapter)
return _wrapper
else:
# The wrapper still has not been provided, so we are just
# collecting the optional default keyword parameters for the
# users decorator at this point. Return the binder again as
# a partial using the collected default parameters and the
# adapter function if one is being used.
return partial(_decorator_binder, adapter=adapter,
**default_params)
return _decorator_binder
generic_decorator = _decorator_factory(GenericWrapper)
function_decorator = _decorator_factory(FunctionWrapper)
instancemethod_decorator = _decorator_factory(InstanceMethodWrapper)

3
wrapt/exceptions.py Normal file
View File

@@ -0,0 +1,3 @@
class UnexpectedDefaultParameters(Exception): pass
class MissingDefaultParameter(Exception): pass
class UnexpectedParameters(Exception): pass

423
wrapt/six.py Normal file
View File

@@ -0,0 +1,423 @@
"""Utilities for writing code that runs on Python 2 and 3"""
# Copyright (c) 2010-2013 Benjamin Peterson
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import operator
import sys
import types
__author__ = "Benjamin Peterson <benjamin@python.org>"
__version__ = "1.3.0"
# Useful for very coarse version differentiation.
PY2 = sys.version_info[0] == 2
PY3 = sys.version_info[0] == 3
if PY3:
string_types = str,
integer_types = int,
class_types = type,
text_type = str
binary_type = bytes
MAXSIZE = sys.maxsize
else:
string_types = basestring,
integer_types = (int, long)
class_types = (type, types.ClassType)
text_type = unicode
binary_type = str
if sys.platform.startswith("java"):
# Jython always uses 32 bits.
MAXSIZE = int((1 << 31) - 1)
else:
# It's possible to have sizeof(long) != sizeof(Py_ssize_t).
class X(object):
def __len__(self):
return 1 << 31
try:
len(X())
except OverflowError:
# 32-bit
MAXSIZE = int((1 << 31) - 1)
else:
# 64-bit
MAXSIZE = int((1 << 63) - 1)
del X
def _add_doc(func, doc):
"""Add documentation to a function."""
func.__doc__ = doc
def _import_module(name):
"""Import module, returning the module after the last dot."""
__import__(name)
return sys.modules[name]
class _LazyDescr(object):
def __init__(self, name):
self.name = name
def __get__(self, obj, tp):
result = self._resolve()
setattr(obj, self.name, result)
# This is a bit ugly, but it avoids running this again.
delattr(tp, self.name)
return result
class MovedModule(_LazyDescr):
def __init__(self, name, old, new=None):
super(MovedModule, self).__init__(name)
if PY3:
if new is None:
new = name
self.mod = new
else:
self.mod = old
def _resolve(self):
return _import_module(self.mod)
class MovedAttribute(_LazyDescr):
def __init__(self, name, old_mod, new_mod, old_attr=None, new_attr=None):
super(MovedAttribute, self).__init__(name)
if PY3:
if new_mod is None:
new_mod = name
self.mod = new_mod
if new_attr is None:
if old_attr is None:
new_attr = name
else:
new_attr = old_attr
self.attr = new_attr
else:
self.mod = old_mod
if old_attr is None:
old_attr = name
self.attr = old_attr
def _resolve(self):
module = _import_module(self.mod)
return getattr(module, self.attr)
class _MovedItems(types.ModuleType):
"""Lazy loading of moved objects"""
_moved_attributes = [
MovedAttribute("cStringIO", "cStringIO", "io", "StringIO"),
MovedAttribute("filter", "itertools", "builtins", "ifilter", "filter"),
MovedAttribute("input", "__builtin__", "builtins", "raw_input", "input"),
MovedAttribute("map", "itertools", "builtins", "imap", "map"),
MovedAttribute("range", "__builtin__", "builtins", "xrange", "range"),
MovedAttribute("reload_module", "__builtin__", "imp", "reload"),
MovedAttribute("reduce", "__builtin__", "functools"),
MovedAttribute("StringIO", "StringIO", "io"),
MovedAttribute("xrange", "__builtin__", "builtins", "xrange", "range"),
MovedAttribute("zip", "itertools", "builtins", "izip", "zip"),
MovedModule("builtins", "__builtin__"),
MovedModule("configparser", "ConfigParser"),
MovedModule("copyreg", "copy_reg"),
MovedModule("http_cookiejar", "cookielib", "http.cookiejar"),
MovedModule("http_cookies", "Cookie", "http.cookies"),
MovedModule("html_entities", "htmlentitydefs", "html.entities"),
MovedModule("html_parser", "HTMLParser", "html.parser"),
MovedModule("http_client", "httplib", "http.client"),
MovedModule("email_mime_multipart", "email.MIMEMultipart", "email.mime.multipart"),
MovedModule("email_mime_text", "email.MIMEText", "email.mime.text"),
MovedModule("email_mime_base", "email.MIMEBase", "email.mime.base"),
MovedModule("BaseHTTPServer", "BaseHTTPServer", "http.server"),
MovedModule("CGIHTTPServer", "CGIHTTPServer", "http.server"),
MovedModule("SimpleHTTPServer", "SimpleHTTPServer", "http.server"),
MovedModule("cPickle", "cPickle", "pickle"),
MovedModule("queue", "Queue"),
MovedModule("reprlib", "repr"),
MovedModule("socketserver", "SocketServer"),
MovedModule("tkinter", "Tkinter"),
MovedModule("tkinter_dialog", "Dialog", "tkinter.dialog"),
MovedModule("tkinter_filedialog", "FileDialog", "tkinter.filedialog"),
MovedModule("tkinter_scrolledtext", "ScrolledText", "tkinter.scrolledtext"),
MovedModule("tkinter_simpledialog", "SimpleDialog", "tkinter.simpledialog"),
MovedModule("tkinter_tix", "Tix", "tkinter.tix"),
MovedModule("tkinter_constants", "Tkconstants", "tkinter.constants"),
MovedModule("tkinter_dnd", "Tkdnd", "tkinter.dnd"),
MovedModule("tkinter_colorchooser", "tkColorChooser",
"tkinter.colorchooser"),
MovedModule("tkinter_commondialog", "tkCommonDialog",
"tkinter.commondialog"),
MovedModule("tkinter_tkfiledialog", "tkFileDialog", "tkinter.filedialog"),
MovedModule("tkinter_font", "tkFont", "tkinter.font"),
MovedModule("tkinter_messagebox", "tkMessageBox", "tkinter.messagebox"),
MovedModule("tkinter_tksimpledialog", "tkSimpleDialog",
"tkinter.simpledialog"),
MovedModule("urllib_robotparser", "robotparser", "urllib.robotparser"),
MovedModule("winreg", "_winreg"),
]
for attr in _moved_attributes:
setattr(_MovedItems, attr.name, attr)
del attr
moves = sys.modules[__name__ + ".moves"] = _MovedItems("moves")
def add_move(move):
"""Add an item to six.moves."""
setattr(_MovedItems, move.name, move)
def remove_move(name):
"""Remove item from six.moves."""
try:
delattr(_MovedItems, name)
except AttributeError:
try:
del moves.__dict__[name]
except KeyError:
raise AttributeError("no such move, %r" % (name,))
if PY3:
_meth_func = "__func__"
_meth_self = "__self__"
_func_closure = "__closure__"
_func_code = "__code__"
_func_defaults = "__defaults__"
_func_globals = "__globals__"
_iterkeys = "keys"
_itervalues = "values"
_iteritems = "items"
_iterlists = "lists"
else:
_meth_func = "im_func"
_meth_self = "im_self"
_func_closure = "func_closure"
_func_code = "func_code"
_func_defaults = "func_defaults"
_func_globals = "func_globals"
_iterkeys = "iterkeys"
_itervalues = "itervalues"
_iteritems = "iteritems"
_iterlists = "iterlists"
try:
advance_iterator = next
except NameError:
def advance_iterator(it):
return it.next()
next = advance_iterator
try:
callable = callable
except NameError:
def callable(obj):
return any("__call__" in klass.__dict__ for klass in type(obj).__mro__)
if PY3:
def get_unbound_function(unbound):
return unbound
create_bound_method = types.MethodType
Iterator = object
else:
def get_unbound_function(unbound):
return unbound.im_func
def create_bound_method(func, obj):
return types.MethodType(func, obj, obj.__class__)
class Iterator(object):
def next(self):
return type(self).__next__(self)
callable = callable
_add_doc(get_unbound_function,
"""Get the function out of a possibly unbound function""")
get_method_function = operator.attrgetter(_meth_func)
get_method_self = operator.attrgetter(_meth_self)
get_function_closure = operator.attrgetter(_func_closure)
get_function_code = operator.attrgetter(_func_code)
get_function_defaults = operator.attrgetter(_func_defaults)
get_function_globals = operator.attrgetter(_func_globals)
def iterkeys(d, **kw):
"""Return an iterator over the keys of a dictionary."""
return iter(getattr(d, _iterkeys)(**kw))
def itervalues(d, **kw):
"""Return an iterator over the values of a dictionary."""
return iter(getattr(d, _itervalues)(**kw))
def iteritems(d, **kw):
"""Return an iterator over the (key, value) pairs of a dictionary."""
return iter(getattr(d, _iteritems)(**kw))
def iterlists(d, **kw):
"""Return an iterator over the (key, [values]) pairs of a dictionary."""
return iter(getattr(d, _iterlists)(**kw))
if PY3:
def b(s):
return s.encode("latin-1")
def u(s):
return s
unichr = chr
if sys.version_info[1] <= 1:
def int2byte(i):
return bytes((i,))
else:
# This is about 2x faster than the implementation above on 3.2+
int2byte = operator.methodcaller("to_bytes", 1, "big")
byte2int = operator.itemgetter(0)
indexbytes = operator.getitem
iterbytes = iter
import io
StringIO = io.StringIO
BytesIO = io.BytesIO
else:
def b(s):
return s
def u(s):
return unicode(s, "unicode_escape")
unichr = unichr
int2byte = chr
def byte2int(bs):
return ord(bs[0])
def indexbytes(buf, i):
return ord(buf[i])
def iterbytes(buf):
return (ord(byte) for byte in buf)
import StringIO
StringIO = BytesIO = StringIO.StringIO
_add_doc(b, """Byte literal""")
_add_doc(u, """Text literal""")
if PY3:
import builtins
exec_ = getattr(builtins, "exec")
def reraise(tp, value, tb=None):
if value.__traceback__ is not tb:
raise value.with_traceback(tb)
raise value
print_ = getattr(builtins, "print")
del builtins
else:
def exec_(_code_, _globs_=None, _locs_=None):
"""Execute code in a namespace."""
if _globs_ is None:
frame = sys._getframe(1)
_globs_ = frame.f_globals
if _locs_ is None:
_locs_ = frame.f_locals
del frame
elif _locs_ is None:
_locs_ = _globs_
exec("""exec _code_ in _globs_, _locs_""")
exec_("""def reraise(tp, value, tb=None):
raise tp, value, tb
""")
def print_(*args, **kwargs):
"""The new-style print function."""
fp = kwargs.pop("file", sys.stdout)
if fp is None:
return
def write(data):
if not isinstance(data, basestring):
data = str(data)
fp.write(data)
want_unicode = False
sep = kwargs.pop("sep", None)
if sep is not None:
if isinstance(sep, unicode):
want_unicode = True
elif not isinstance(sep, str):
raise TypeError("sep must be None or a string")
end = kwargs.pop("end", None)
if end is not None:
if isinstance(end, unicode):
want_unicode = True
elif not isinstance(end, str):
raise TypeError("end must be None or a string")
if kwargs:
raise TypeError("invalid keyword arguments to print()")
if not want_unicode:
for arg in args:
if isinstance(arg, unicode):
want_unicode = True
break
if want_unicode:
newline = unicode("\n")
space = unicode(" ")
else:
newline = "\n"
space = " "
if sep is None:
sep = space
if end is None:
end = newline
for i, arg in enumerate(args):
if i:
write(sep)
write(arg)
write(end)
_add_doc(reraise, """Reraise an exception.""")
def with_metaclass(meta, *bases):
"""Create a base class with a metaclass."""
return meta("NewBase", bases, {})

0
wrapt/tests/__init__.py Normal file
View File

13
wrapt/tests/decorators.py Normal file
View File

@@ -0,0 +1,13 @@
import wrapt
@wrapt.generic_decorator
def passthru_generic_decorator(wrapped, obj, cls, args, kwargs):
return wrapped(*args, **kwargs)
@wrapt.function_decorator
def passthru_function_decorator(wrapped, args, kwargs):
return wrapped(*args, **kwargs)
@wrapt.instancemethod_decorator
def passthru_instancemethod_decorator(wrapped, obj, cls, args, kwargs):
return wrapped(*args, **kwargs)

View File

@@ -0,0 +1,76 @@
from __future__ import print_function
import unittest
import inspect
import wrapt
from .decorators import passthru_function_decorator
def function(arg):
'''documentation'''
return arg
original = function
@passthru_function_decorator
def function(arg):
'''documentation'''
return arg
class TestNamingFunction(unittest.TestCase):
def test_object_name(self):
# Test preservation of function __name__ attribute.
self.assertEqual(function.__name__, original.__name__)
def test_object_qualname(self):
# Test preservation of function __qualname__ attribute.
try:
__qualname__ = original.__qualname__
except AttributeError:
pass
else:
self.assertEqual(function.__qualname__, __qualname__)
def test_module_name(self):
# Test preservation of function __module__ attribute.
self.assertEqual(function.__module__, __name__)
def test_doc_string(self):
# Test preservation of function __doc__ attribute.
self.assertEqual(function.__doc__, original.__doc__)
def test_argspec(self):
# Test preservation of function argument specification.
original_argspec = inspect.getargspec(original)
function_argspec = inspect.getargspec(function)
self.assertEqual(original_argspec, function_argspec)
def test_isinstance(self):
# Test preservation of isinstance() checks.
self.assertTrue(isinstance(function, type(original)))
class TestCallingFunction(unittest.TestCase):
def test_call_function(self):
_args = (1, 2)
_kwargs = { 'one': 1, 'two': 2 }
@wrapt.function_decorator
def _decorator(wrapped, args, kwargs):
return wrapped(*args, **kwargs)
@_decorator
def _function(*args, **kwargs):
return args, kwargs
result = _function(*_args, **_kwargs)
self.assertEqual(result, (_args, _kwargs))

View File

@@ -0,0 +1,105 @@
from __future__ import print_function
import unittest
import inspect
from .decorators import passthru_generic_decorator
class Class(object):
@classmethod
def function(self, arg):
'''documentation'''
return arg
Original = Class
class Class(object):
@passthru_generic_decorator
@classmethod
def function(self, arg):
'''documentation'''
return arg
class TestCase(unittest.TestCase):
def test_class_object_name(self):
# Test preservation of instance method __name__ attribute.
self.assertEqual(Class.function.__name__,
Original.function.__name__)
def test_instance_object_name(self):
# Test preservation of instance method __name__ attribute.
self.assertEqual(Class().function.__name__,
Original().function.__name__)
def test_class_object_qualname(self):
# Test preservation of instance method __qualname__ attribute.
try:
__qualname__ = Original.original.__qualname__
except AttributeError:
pass
else:
self.assertEqual(Class.function.__qualname__, __qualname__)
def test_instance_object_qualname(self):
# Test preservation of instance method __qualname__ attribute.
try:
__qualname__ = Original().original.__qualname__
except AttributeError:
pass
else:
self.assertEqual(Class().function.__qualname__, __qualname__)
def test_class_module_name(self):
# Test preservation of instance method __module__ attribute.
self.assertEqual(Class.function.__module__,
Original.function.__module__)
def test_instance_module_name(self):
# Test preservation of instance method __module__ attribute.
self.assertEqual(Class().function.__module__,
Original().function.__module__)
def test_class_doc_string(self):
# Test preservation of instance method __doc__ attribute.
self.assertEqual(Class.function.__doc__,
Original.function.__doc__)
def test_instance_doc_string(self):
# Test preservation of instance method __doc__ attribute.
self.assertEqual(Class().function.__doc__,
Original().function.__doc__)
def test_class_argspec(self):
# Test preservation of instance method argument specification.
original_argspec = inspect.getargspec(Original.function)
function_argspec = inspect.getargspec(Class.function)
self.assertEqual(original_argspec, function_argspec)
def test_instance_argspec(self):
# Test preservation of instance method argument specification.
original_argspec = inspect.getargspec(Original().function)
function_argspec = inspect.getargspec(Class().function)
self.assertEqual(original_argspec, function_argspec)
def test_class_isinstance(self):
# Test preservation of isinstance() checks.
self.assertTrue(isinstance(Class.function,
type(Original.function)))
def test_instance_isinstance(self):
# Test preservation of isinstance() checks.
self.assertTrue(isinstance(Class().function,
type(Original().function)))

View File

@@ -0,0 +1,105 @@
from __future__ import print_function
import unittest
import inspect
from .decorators import passthru_generic_decorator
class Class(object):
@staticmethod
def function(self, arg):
'''documentation'''
return arg
Original = Class
class Class(object):
@passthru_generic_decorator
@staticmethod
def function(self, arg):
'''documentation'''
return arg
class TestCase(unittest.TestCase):
def test_class_object_name(self):
# Test preservation of instance method __name__ attribute.
self.assertEqual(Class.function.__name__,
Original.function.__name__)
def test_instance_object_name(self):
# Test preservation of instance method __name__ attribute.
self.assertEqual(Class().function.__name__,
Original().function.__name__)
def test_class_module_name(self):
# Test preservation of instance method __module__ attribute.
self.assertEqual(Class.function.__module__,
Original.function.__module__)
def test_class_object_qualname(self):
# Test preservation of instance method __qualname__ attribute.
try:
__qualname__ = Original.original.__qualname__
except AttributeError:
pass
else:
self.assertEqual(Class.function.__qualname__, __qualname__)
def test_instance_object_qualname(self):
# Test preservation of instance method __qualname__ attribute.
try:
__qualname__ = Original().original.__qualname__
except AttributeError:
pass
else:
self.assertEqual(Class().function.__qualname__, __qualname__)
def test_instance_module_name(self):
# Test preservation of instance method __module__ attribute.
self.assertEqual(Class().function.__module__,
Original().function.__module__)
def test_class_doc_string(self):
# Test preservation of instance method __doc__ attribute.
self.assertEqual(Class.function.__doc__,
Original.function.__doc__)
def test_instance_doc_string(self):
# Test preservation of instance method __doc__ attribute.
self.assertEqual(Class().function.__doc__,
Original().function.__doc__)
def test_class_argspec(self):
# Test preservation of instance method argument specification.
original_argspec = inspect.getargspec(Original.function)
function_argspec = inspect.getargspec(Class.function)
self.assertEqual(original_argspec, function_argspec)
def test_instance_argspec(self):
# Test preservation of instance method argument specification.
original_argspec = inspect.getargspec(Original().function)
function_argspec = inspect.getargspec(Class().function)
self.assertEqual(original_argspec, function_argspec)
def test_class_isinstance(self):
# Test preservation of isinstance() checks.
self.assertTrue(isinstance(Class.function,
type(Original.function)))
def test_instance_isinstance(self):
# Test preservation of isinstance() checks.
self.assertTrue(isinstance(Class().function,
type(Original().function)))

View File

@@ -0,0 +1,306 @@
from __future__ import print_function
import unittest
import inspect
import wrapt
from .decorators import passthru_instancemethod_decorator
class OldClass1d():
def function(self, arg):
'''documentation'''
return arg
OldClass1o = OldClass1d
class OldClass1d():
@passthru_instancemethod_decorator
def function(self, arg):
'''documentation'''
return arg
class TestNamingInstanceMethodOldStyle(unittest.TestCase):
def test_class_object_name(self):
# Test preservation of instance method __name__ attribute.
self.assertEqual(OldClass1d.function.__name__,
OldClass1o.function.__name__)
def test_instance_object_name(self):
# Test preservation of instance method __name__ attribute.
self.assertEqual(OldClass1d().function.__name__,
OldClass1o().function.__name__)
def test_class_object_qualname(self):
# Test preservation of instance method __qualname__ attribute.
try:
__qualname__ = OldClass1o.original.__qualname__
except AttributeError:
pass
else:
self.assertEqual(OldClass1d.function.__qualname__, __qualname__)
def test_instance_object_qualname(self):
# Test preservation of instance method __qualname__ attribute.
try:
__qualname__ = OldClass1o().original.__qualname__
except AttributeError:
pass
else:
self.assertEqual(OldClass1d().function.__qualname__, __qualname__)
def test_class_module_name(self):
# Test preservation of instance method __module__ attribute.
self.assertEqual(OldClass1d.function.__module__,
OldClass1o.function.__module__)
def test_instance_module_name(self):
# Test preservation of instance method __module__ attribute.
self.assertEqual(OldClass1d().function.__module__,
OldClass1o().function.__module__)
def test_class_doc_string(self):
# Test preservation of instance method __doc__ attribute.
self.assertEqual(OldClass1d.function.__doc__,
OldClass1o.function.__doc__)
def test_instance_doc_string(self):
# Test preservation of instance method __doc__ attribute.
self.assertEqual(OldClass1d().function.__doc__,
OldClass1o().function.__doc__)
def test_class_argspec(self):
# Test preservation of instance method argument specification.
original_argspec = inspect.getargspec(OldClass1o.function)
function_argspec = inspect.getargspec(OldClass1d.function)
self.assertEqual(original_argspec, function_argspec)
def test_instance_argspec(self):
# Test preservation of instance method argument specification.
original_argspec = inspect.getargspec(OldClass1o().function)
function_argspec = inspect.getargspec(OldClass1d().function)
self.assertEqual(original_argspec, function_argspec)
def test_class_isinstance(self):
# Test preservation of isinstance() checks.
self.assertTrue(isinstance(OldClass1d.function,
type(OldClass1o.function)))
def test_instance_isinstance(self):
# Test preservation of isinstance() checks.
self.assertTrue(isinstance(OldClass1d().function,
type(OldClass1o().function)))
class NewClass1d(object):
def function(self, arg):
'''documentation'''
return arg
NewClass1o = NewClass1d
class NewClass1d(object):
@passthru_instancemethod_decorator
def function(self, arg):
'''documentation'''
return arg
class TestNamingInstanceMethodNewStyle(unittest.TestCase):
def test_class_object_name(self):
# Test preservation of instance method __name__ attribute.
self.assertEqual(NewClass1d.function.__name__,
NewClass1o.function.__name__)
def test_instance_object_name(self):
# Test preservation of instance method __name__ attribute.
self.assertEqual(NewClass1d().function.__name__,
NewClass1o().function.__name__)
def test_class_object_qualname(self):
# Test preservation of instance method __qualname__ attribute.
try:
__qualname__ = NewClass1o.original.__qualname__
except AttributeError:
pass
else:
self.assertEqual(NewClass1d.function.__qualname__, __qualname__)
def test_instance_object_qualname(self):
# Test preservation of instance method __qualname__ attribute.
try:
__qualname__ = NewClass1o().original.__qualname__
except AttributeError:
pass
else:
self.assertEqual(NewClass1d().function.__qualname__, __qualname__)
def test_class_module_name(self):
# Test preservation of instance method __module__ attribute.
self.assertEqual(NewClass1d.function.__module__,
NewClass1o.function.__module__)
def test_instance_module_name(self):
# Test preservation of instance method __module__ attribute.
self.assertEqual(NewClass1d().function.__module__,
NewClass1o().function.__module__)
def test_class_doc_string(self):
# Test preservation of instance method __doc__ attribute.
self.assertEqual(NewClass1d.function.__doc__,
NewClass1o.function.__doc__)
def test_instance_doc_string(self):
# Test preservation of instance method __doc__ attribute.
self.assertEqual(NewClass1d().function.__doc__,
NewClass1o().function.__doc__)
def test_class_argspec(self):
# Test preservation of instance method argument specification.
original_argspec = inspect.getargspec(NewClass1o.function)
function_argspec = inspect.getargspec(NewClass1d.function)
self.assertEqual(original_argspec, function_argspec)
def test_instance_argspec(self):
# Test preservation of instance method argument specification.
original_argspec = inspect.getargspec(NewClass1o().function)
function_argspec = inspect.getargspec(NewClass1d().function)
self.assertEqual(original_argspec, function_argspec)
def test_class_isinstance(self):
# Test preservation of isinstance() checks.
self.assertTrue(isinstance(NewClass1d.function,
type(NewClass1o.function)))
def test_instance_isinstance(self):
# Test preservation of isinstance() checks.
self.assertTrue(isinstance(NewClass1d().function,
type(NewClass1o().function)))
class TestCallingInstanceMethodOldStyle(unittest.TestCase):
def test_class_call_function(self):
# Test calling instancemethod via class and passing in the class
# instance directly. This is bypassing the descriptor protocol.
_args = (1, 2)
_kwargs = { 'one': 1, 'two': 2 }
@wrapt.instancemethod_decorator
def _decorator(wrapped, obj, cls, args, kwargs):
print('_DECORATOR', wrapped, obj, cls, args, kwargs)
self.assertNotEqual(obj, None)
return wrapped(*args, **kwargs)
@_decorator
def _function(*args, **kwargs):
return args, kwargs
class Class():
@_decorator
def _function(self, *args, **kwargs):
return (args, kwargs)
result = Class._function(Class(), *_args, **_kwargs)
self.assertEqual(result, (_args, _kwargs))
def test_instance_call_function(self):
# Test calling instancemethod via class instance.
_args = (1, 2)
_kwargs = { 'one': 1, 'two': 2 }
@wrapt.instancemethod_decorator
def _decorator(wrapped, obj, cls, args, kwargs):
self.assertNotEqual(obj, None)
return wrapped(*args, **kwargs)
@_decorator
def _function(*args, **kwargs):
return args, kwargs
class Class():
@_decorator
def _function(self, *args, **kwargs):
return (args, kwargs)
result = Class()._function(*_args, **_kwargs)
self.assertEqual(result, (_args, _kwargs))
class TestCallingInstanceMethodNewStyle(unittest.TestCase):
def test_class_call_function(self):
# Test calling instancemethod via class and passing in the class
# instance directly. This is bypassing the descriptor protocol.
_args = (1, 2)
_kwargs = { 'one': 1, 'two': 2 }
@wrapt.instancemethod_decorator
def _decorator(wrapped, obj, cls, args, kwargs):
print('_DECORATOR', wrapped, obj, cls, args, kwargs)
self.assertNotEqual(obj, None)
return wrapped(*args, **kwargs)
@_decorator
def _function(*args, **kwargs):
return args, kwargs
class Class(object):
@_decorator
def _function(self, *args, **kwargs):
return (args, kwargs)
result = Class._function(Class(), *_args, **_kwargs)
self.assertEqual(result, (_args, _kwargs))
def test_instance_call_function(self):
# Test calling instancemethod via class instance.
_args = (1, 2)
_kwargs = { 'one': 1, 'two': 2 }
@wrapt.instancemethod_decorator
def _decorator(wrapped, obj, cls, args, kwargs):
self.assertNotEqual(obj, None)
return wrapped(*args, **kwargs)
@_decorator
def _function(*args, **kwargs):
return args, kwargs
class Class(object):
@_decorator
def _function(self, *args, **kwargs):
return (args, kwargs)
result = Class()._function(*_args, **_kwargs)
self.assertEqual(result, (_args, _kwargs))

View File

@@ -0,0 +1,60 @@
from __future__ import print_function
import unittest
import inspect
from .decorators import passthru_function_decorator
def function():
def inner(arg):
'''documentation'''
return arg
return inner
original = function
def function():
@passthru_function_decorator
def inner(arg):
'''documentation'''
return arg
return inner
class TestCase(unittest.TestCase):
def test_object_name(self):
# Test preservation of function __name__ attribute.
self.assertEqual(function().__name__, original().__name__)
def test_object_qualname(self):
# Test preservation of function __qualname__ attribute.
try:
__qualname__ = original().__qualname__
except AttributeError:
pass
else:
self.assertEqual(function().__qualname__, __qualname__)
def test_module_name(self):
# Test preservation of function __module__ attribute.
self.assertEqual(function().__module__, __name__)
def test_doc_string(self):
# Test preservation of function __doc__ attribute.
self.assertEqual(function().__doc__, original().__doc__)
def test_argspec(self):
# Test preservation of function argument specification.
original_argspec = inspect.getargspec(original())
function_argspec = inspect.getargspec(function())
self.assertEqual(original_argspec, function_argspec)
def test_isinstance(self):
# Test preservation of isinstance() checks.
self.assertTrue(isinstance(function(), type(original())))

View File

@@ -0,0 +1,105 @@
from __future__ import print_function
import unittest
import inspect
from .decorators import passthru_generic_decorator
class Class(object):
@classmethod
def function(self, arg):
'''documentation'''
return arg
Original = Class
class Class(object):
@classmethod
@passthru_generic_decorator
def function(self, arg):
'''documentation'''
return arg
class TestCase(unittest.TestCase):
def test_class_object_name(self):
# Test preservation of instance method __name__ attribute.
self.assertEqual(Class.function.__name__,
Original.function.__name__)
def test_instance_object_name(self):
# Test preservation of instance method __name__ attribute.
self.assertEqual(Class().function.__name__,
Original().function.__name__)
def test_class_object_qualname(self):
# Test preservation of instance method __qualname__ attribute.
try:
__qualname__ = Original.original.__qualname__
except AttributeError:
pass
else:
self.assertEqual(Class.function.__qualname__, __qualname__)
def test_instance_object_qualname(self):
# Test preservation of instance method __qualname__ attribute.
try:
__qualname__ = Original().original.__qualname__
except AttributeError:
pass
else:
self.assertEqual(Class().function.__qualname__, __qualname__)
def test_class_module_name(self):
# Test preservation of instance method __module__ attribute.
self.assertEqual(Class.function.__module__,
Original.function.__module__)
def test_instance_module_name(self):
# Test preservation of instance method __module__ attribute.
self.assertEqual(Class().function.__module__,
Original().function.__module__)
def test_class_doc_string(self):
# Test preservation of instance method __doc__ attribute.
self.assertEqual(Class.function.__doc__,
Original.function.__doc__)
def test_instance_doc_string(self):
# Test preservation of instance method __doc__ attribute.
self.assertEqual(Class().function.__doc__,
Original().function.__doc__)
def test_class_argspec(self):
# Test preservation of instance method argument specification.
original_argspec = inspect.getargspec(Original.function)
function_argspec = inspect.getargspec(Class.function)
self.assertEqual(original_argspec, function_argspec)
def test_instance_argspec(self):
# Test preservation of instance method argument specification.
original_argspec = inspect.getargspec(Original().function)
function_argspec = inspect.getargspec(Class().function)
self.assertEqual(original_argspec, function_argspec)
def test_class_isinstance(self):
# Test preservation of isinstance() checks.
self.assertTrue(isinstance(Class.function,
type(Original.function)))
def test_instance_isinstance(self):
# Test preservation of isinstance() checks.
self.assertTrue(isinstance(Class().function,
type(Original().function)))

View File

@@ -0,0 +1,105 @@
from __future__ import print_function
import unittest
import inspect
from .decorators import passthru_generic_decorator
class Class(object):
@staticmethod
def function(self, arg):
'''documentation'''
return arg
Original = Class
class Class(object):
@staticmethod
@passthru_generic_decorator
def function(self, arg):
'''documentation'''
return arg
class TestCase(unittest.TestCase):
def test_class_object_name(self):
# Test preservation of instance method __name__ attribute.
self.assertEqual(Class.function.__name__,
Original.function.__name__)
def test_instance_object_name(self):
# Test preservation of instance method __name__ attribute.
self.assertEqual(Class().function.__name__,
Original().function.__name__)
def test_class_object_qualname(self):
# Test preservation of instance method __qualname__ attribute.
try:
__qualname__ = Original.original.__qualname__
except AttributeError:
pass
else:
self.assertEqual(Class.function.__qualname__, __qualname__)
def test_instance_object_qualname(self):
# Test preservation of instance method __qualname__ attribute.
try:
__qualname__ = Original().original.__qualname__
except AttributeError:
pass
else:
self.assertEqual(Class().function.__qualname__, __qualname__)
def test_class_module_name(self):
# Test preservation of instance method __module__ attribute.
self.assertEqual(Class.function.__module__,
Original.function.__module__)
def test_instance_module_name(self):
# Test preservation of instance method __module__ attribute.
self.assertEqual(Class().function.__module__,
Original().function.__module__)
def test_class_doc_string(self):
# Test preservation of instance method __doc__ attribute.
self.assertEqual(Class.function.__doc__,
Original.function.__doc__)
def test_instance_doc_string(self):
# Test preservation of instance method __doc__ attribute.
self.assertEqual(Class().function.__doc__,
Original().function.__doc__)
def test_class_argspec(self):
# Test preservation of instance method argument specification.
original_argspec = inspect.getargspec(Original.function)
function_argspec = inspect.getargspec(Class.function)
self.assertEqual(original_argspec, function_argspec)
def test_instance_argspec(self):
# Test preservation of instance method argument specification.
original_argspec = inspect.getargspec(Original().function)
function_argspec = inspect.getargspec(Class().function)
self.assertEqual(original_argspec, function_argspec)
def test_class_isinstance(self):
# Test preservation of isinstance() checks.
self.assertTrue(isinstance(Class.function,
type(Original.function)))
def test_instance_isinstance(self):
# Test preservation of isinstance() checks.
self.assertTrue(isinstance(Class().function,
type(Original().function)))

214
wrapt/wrappers.py Normal file
View File

@@ -0,0 +1,214 @@
import functools
from . import six
class WrapperOverrideMethods(object):
@property
def __module__(self):
return self._self_wrapped.__module__
@__module__.setter
def __module__(self, value):
self._self_wrapped.__module__ = value
@property
def __doc__(self):
return self._self_wrapped.__doc__
@__doc__.setter
def __doc__(self, value):
self._self_wrapped.__doc__ = value
class WrapperBaseMetaType(type):
def __new__(cls, name, bases, dictionary):
# We use properties to override the values of __module__ and
# __doc__. If we add these in WrapperBase, the derived class
# __dict__ will still be setup to have string variants of these
# attributes and the rules of descriptors means that they
# appear to take precedence over the properties in the base
# class. To avoid that, we copy the properties into the derived
# class type itself via a meta class. In that way the
# properties will always take precedence.
dictionary.update(vars(WrapperOverrideMethods))
return type.__new__(cls, name, bases, dictionary)
class WrapperBase(six.with_metaclass(WrapperBaseMetaType)):
def __init__(self, wrapped, wrapper, adapter=None, params={}):
self._self_wrapped = wrapped
self._self_wrapper = wrapper
self._self_params = params
# Python 3.2+ has the __wrapped__ attribute which is meant to
# hold a reference to the inner most wrapped object when there
# are multiple decorators. We handle __wrapped__ and also
# duplicate that functionality for Python 2, although it will
# only go as far as what is below our own wrappers when there is
# more than one for Python 2.
if adapter is None:
try:
self._self_target = wrapped.__wrapped__
except AttributeError:
self._self_target = wrapped
else:
self._self_target = adapter
# Python 3.2+ has the __qualname__ attribute, but it does not
# allow it to be overridden using a property and it must instead
# be an actual string object instead.
try:
object.__setattr__(self, '__qualname__', wrapped.__qualname__)
except AttributeError:
pass
# Although __name__ can be overridden with a property in all
# Python versions, updating it writes it back to an internal C
# structure which can be accessed at C code level, so not sure
# if overriding it as a property is sufficient in all cases.
try:
object.__setattr__(self, '__name__', wrapped.__name__)
except AttributeError:
pass
def __setattr__(self, name, value):
if name.startswith('_self_'):
object.__setattr__(self, name, value)
else:
setattr(self._self_wrapped, name, value)
def __getattr__(self, name):
return getattr(self._self_wrapped, name)
@property
def __class__(self):
return self._self_wrapped.__class__
@__class__.setter
def __class__(self, value):
self._self_wrapped.__class__ = value
@property
def __annotations__(self):
return self._self_wrapped.__anotations__
@__annotations__.setter
def __annotations__(self, value):
self._self_wrapped.__annotations__ = value
@property
def __wrapped__(self):
return self._self_target
@__wrapped__.setter
def __wrapped__(self, value):
self._self_wrapped.__wrapped__ = value
def __dir__(self):
return dir(self._self_wrapped)
def __eq__(self, other):
return self._self_target == other
def __ne__(self, other):
result = self.__eq__(other)
if result is NotImplemented:
return result
return not result
def __hash__(self):
return hash(self._self_target)
def __repr__(self):
return '<%s for %s>' % (type(self).__name__, str(self._self_target))
def __enter__(self):
return self._self_wrapped.__enter__()
def __exit__(self, *args, **kwargs):
return self._self_wrapped.__exit__(*args, **kwargs)
def __iter__(self):
return iter(self._self_wrapped)
class BoundGenericWrapper(WrapperBase):
def __init__(self, wrapped, instance, owner, wrapper, adapter=None,
params={}):
self._self_instance = instance
self._self_owner = owner
super(BoundGenericWrapper, self).__init__(wrapped=wrapped,
wrapper=wrapper, adapter=adapter, params=params)
def __call__(self, *args, **kwargs):
return self._self_wrapper(self._self_wrapped, self._self_instance,
self._self_owner, args, kwargs, **self._self_params)
class GenericWrapper(WrapperBase):
WRAPPER_ARGLIST = ('wrapped', 'instance', 'owner', 'args', 'kwargs')
def __get__(self, instance, owner):
descriptor = self._self_wrapped.__get__(instance, owner)
return BoundGenericWrapper(wrapped=descriptor, instance=instance,
owner=owner, wrapper=self._self_wrapper,
adapter=self._self_target, params=self._self_params)
return result
def __call__(self, *args, **kwargs):
return self._self_wrapper(self._self_wrapped, None, None,
args, kwargs, **self._self_params)
class FunctionWrapper(WrapperBase):
WRAPPER_ARGLIST = ('wrapped', 'args', 'kwargs')
def __call__(self, *args, **kwargs):
return self._self_wrapper(self._self_wrapped, args, kwargs,
**self._self_params)
class BoundInstanceMethodWrapper(WrapperBase):
def __init__(self, wrapped, instance, owner, wrapper, adapter=None,
params={}):
self._self_instance = instance
self._self_owner = owner
super(BoundInstanceMethodWrapper, self).__init__(wrapped=wrapped,
wrapper=wrapper, adapter=adapter, params=params)
def __call__(self, *args, **kwargs):
if self._self_instance is None:
# This situation can occur where someone is calling the
# instancemethod via the class type and passing the instance
# as the first argument. This technically is breaking the
# descriptor protocol and could cause problems where the
# decorator was wrapping another decorator which in turn was
# relying on the descriptor protocol to be able to derive
# the instance and is not using a similar workaround to
# this. We can only ensure we ourselves at least ensure our
# wrappers/decorators work and just pass the problem on to
# the next wrapper/decorator.
instance, args = args[0], args[1:]
wrapped = functools.partial(self._self_wrapped, instance)
return self._self_wrapper(wrapped, instance, self._self_owner,
args, kwargs, **self._self_params)
else:
return self._self_wrapper(self._self_wrapped, self._self_instance,
self._self_owner, args, kwargs, **self._self_params)
class InstanceMethodWrapper(WrapperBase):
WRAPPER_ARGLIST = ('wrapped', 'instance', 'owner', 'args', 'kwargs')
def __get__(self, instance, owner):
descriptor = self._self_wrapped.__get__(instance, owner)
return BoundInstanceMethodWrapper(wrapped=descriptor,
instance=instance, owner=owner, wrapper=self._self_wrapper,
adapter=self._self_target, params=self._self_params)
return result