Whole file

testing-cabal/mock

The author described this change as Attempting to *set* an attribute on a class with a spec will raise an if the spec doesn't have that attribute. It counts as a record because the check below fails on the code as it stood at fcd6555c3 and passes on 17c557a94, with nothing else changed between the two runs.

Fix saved2010-10-18
Sharing licenceBSD-2-Clause · LICENSE.txt
Change size+8 0

What the code was meant to do, written into the code itself as a save note

Attempting to *set* an attribute on a class with a spec will raise an if the spec doesn't have that attribute

The change

360360 return "<%s name=%r id='%s'>" % (type(self).__name__, name, id(self))
361361
362362 def __setattr__(self, name, value):
363+ if not 'method_calls' in self.__dict__:
364+ # allow all attribute setting until initialisation is complete
365+ return object.__setattr__(self, name, value)
366+ if (self._methods is not None and name not in self._methods and name
367+ not in _mock_attributes):
368+ raise AttributeError("Mock object has no attribute '%s'" % name)
363369 if name in _unsupported_magics:
364370 msg = 'Attempting to set unsupported magic method %r.' % name
365371 raise AttributeError(msg)
763769 '__instancecheck__', '__subclasscheck__',
764770 '__del__'
765771 ])
772+
773+_mock_attributes = set(dir(Mock()))
766774
767775 _calculate_return_value = {
768776 '__hash__': lambda self: object.__hash__(self),

The check that tells the two apart

failpass·tests/testmock.py::MockTest::testSettingAttributeWithSpec

Check file tests/testmock.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.

Origin and history

The code before itfcd6555c317d02d2b2f4f4c5a4ed679b89cb654d
Broken version dated2010-10-18
Modulemock
Units changedMock
Fingerprint1c0af390d439adac
Checked2026-08-18 by goldset/0.1

Every field above is generated by our program. None of it is written by hand.

Other bugs found in testing-cabal/mock