jsatt / mock

Automatically exported from code.google.com/p/mock
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

Fix __delattr__ of mock to allow multiple creation and deletion of attributes. #221

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

>>> a = Mock()
>>> a.this = ‘that’
>>> del a.this
>>> a.this = ‘that’

>>> del a.this
Traceback (most recent call last):
...
AttributeError: this

What is the expected output? What do you see instead?
I would expect it to happily delete the attribute this. Instead it raises and 
attribute error.

What version of the product are you using? On what operating system?
mock-1.0.1-py2.7
Red Hat Enterprise Linux Server release 5.5

Please provide any additional information below.

suggested change:

Previous:

    def __delattr__(self, name):
        if name in _all_magics and name in type(self).__dict__:
            delattr(type(self), name)
            if name not in self.__dict__:
                # for magic methods that are still MagicProxy objects and
                # not set on the instance itself
                return

        if name in self.__dict__:
            object.__delattr__(self, name)

        obj = self._mock_children.get(name, _missing)
        if obj is _deleted:
            raise AttributeError(name)
        if obj is not _missing:
            del self._mock_children[name]
        self._mock_children[name] = _deleted

Change:

    def __delattr__(self, name):
        if name in _all_magics and name in type(self).__dict__:
            delattr(type(self), name)
            if name not in self.__dict__:
                # for magic methods that are still MagicProxy objects and
                # not set on the instance itself
                return

        obj = self._mock_children.get(name, _missing)
        if name in self.__dict__:
            object.__delattr__(self, name)
        elif obj is _deleted:
            raise AttributeError(name)
        if obj is not _missing:
            del self._mock_children[name]
        self._mock_children[name] = _deleted

Incidentally the if ‘obj is not _missing’ line seems superfluous.

Original issue reported on code.google.com by lukeddse...@gmail.com on 13 Jan 2014 at 11:09

GoogleCodeExporter commented 8 years ago
Thanks for the report. I've posted it on the Python bug tracker (as mock is in 
the Python standard library as well) as http://bugs.python.org/issue20239

Original comment by fuzzyman on 13 Jan 2014 at 11:15