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.
Projecttesting-cabal/mock
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
| 360 | 360 | return "<%s name=%r id='%s'>" % (type(self).__name__, name, id(self)) | |
| 361 | 361 | ||
| 362 | 362 | 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) | |
| 363 | 369 | if name in _unsupported_magics: | |
| 364 | 370 | msg = 'Attempting to set unsupported magic method %r.' % name | |
| 365 | 371 | raise AttributeError(msg) | |
| ⋯ | |||
| 763 | 769 | '__instancecheck__', '__subclasscheck__', | |
| 764 | 770 | '__del__' | |
| 765 | 771 | ]) | |
| 772 | + | ||
| 773 | + | _mock_attributes = set(dir(Mock())) | |
| 766 | 774 | ||
| 767 | 775 | _calculate_return_value = { | |
| 768 | 776 | '__hash__': lambda self: object.__hash__(self), | |
The check that tells the two apart
fail→pass·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
- 2023-04-16_get_signature_object
- 2022-12-28create_autospec
- 2020-01-29_patch_dict
- 2020-01-29_patch_object
- 2012-01-22_Call
- 2011-07-19Fix callargs call comparisons