Skip to content
Permalink
Browse files
Raise an exception if object being created has __slots__ set
  • Loading branch information
jstasiak committed Sep 14, 2013
1 parent 3a17b07 commit b7f6fc7c9e86e63230ad776b34cd4f7d2482fae9
Showing with 24 additions and 2 deletions.
  1. +4 −0 CHANGES.md
  2. +5 −2 injector.py
  3. +15 −0 injector_test.py
@@ -1,5 +1,9 @@
# ChangeLog for Injector

## 0.7.8

- Exception is raised when Injector can't install itself into a class instance due to __slots__ presence

## 0.7.7

- Made AssistedBuilder behave more explicitly: it can build either innstance of a concrete class (``AssistedBuilder(cls=Class)``) or it will follow Injector bindings (if exist) and construct instance of a class pointed by an interface (``AssistedBuilder(interface=Interface)``). ``AssistedBuilder(X)`` behaviour remains the same, it's equivalent to ``AssistedBuilder(interface=X)``
@@ -584,8 +584,11 @@ def create_object(self, cls, additional_kwargs=None):
try:
self.install_into(instance)
except AttributeError:
# Some builtin types can not be modified.
pass
if hasattr(instance, '__slots__'):
raise Error('Can\'t create an instance of type %r due to presence of __slots__, '
'remove __slots__ to fix that' % (cls,))

# Else do nothing - some builtin types can not be modified.
try:
instance.__init__(**additional_kwargs)
except TypeError as e:
@@ -1024,3 +1024,18 @@ def __init__(self, s):

injector = Injector()
injector.get(X)


def test_injection_fails_when_injector_cant_install_itself_into_an_object_with_slots():
try:
class ClassName(object):
__slots__ = ()

injector = Injector()
injector.get(ClassName)
except Exception as e:
for part in ('ClassName', '__slots__'):
assert part in str(e), (
'%r should be present in the exception message: %s' % (part, e))
else:
assert False, 'Should have raised an exception and it didn\'t'

0 comments on commit b7f6fc7

Please sign in to comment.