Add fix for @inject decorator with classes on PyPy and Py3

This commit is contained in:
Roman Mogilatov 2015-09-28 18:45:15 +03:00
parent 2c85b38113
commit 512544ea9f

View File

@ -1,5 +1,6 @@
"""Injections module."""
import sys
import six
from .utils import is_provider
@ -9,6 +10,13 @@ from .utils import get_injectable_kwargs
from .errors import Error
IS_PYPY = '__pypy__' in sys.builtin_module_names
if IS_PYPY or six.PY3: # pragma: no cover
OBJECT_INIT = six.get_unbound_function(object.__init__)
else: # pragma: no cover
OBJECT_INIT = None
class Injection(object):
"""Base injection class."""
@ -68,11 +76,11 @@ def inject(*args, **kwargs):
cls = callback
try:
cls_init = six.get_unbound_function(cls.__init__)
assert cls_init is not object.__init__
assert cls_init is not OBJECT_INIT
except (AttributeError, AssertionError):
raise Error(
'Class {0} has no __init__() '.format(cls.__module__,
cls.__name__) +
'Class {0}.{1} has no __init__() '.format(cls.__module__,
cls.__name__) +
'method and could not be decorated with @inject decorator')
cls.__init__ = decorator(cls_init)
return cls