[New-bugs-announce] [issue44410] Exception in AsyncMock side_effect cases incorrect refcount
Daniel Andersson
report at bugs.python.org
Sun Jun 13 10:52:01 EDT 2021
New submission from Daniel Andersson <daniel.4ndersson at gmail.com>:
Dear maintainers,
I discovered an unexpected behavior when the `side_effect` of an `AsyncMock` includes an exception. The test case below fails but I expected it to pass:
```
import sys
import unittest
from unittest.mock import AsyncMock
class A:
async def foobar(self):
while True:
try:
return await self.mock()
except Exception:
continue
class TestA(unittest.IsolatedAsyncioTestCase):
async def test_refcount(self):
a = A()
a.mock = AsyncMock(side_effect=[Exception(), None])
refc = sys.getrefcount(a)
await a.foobar()
self.assertEqual(refc, sys.getrefcount(a))
if __name__ == "__main__":
unittest.main()
```
If `side_effect=[Exception(), None]` is changed to `side_effect=[None, None]` the test case pass.
I discovered this in a bigger codebase while debugging why a weakref.finalize did not trigger as expected.
----------
components: asyncio
messages: 395752
nosy: asvetlov, penlect, yselivanov
priority: normal
severity: normal
status: open
title: Exception in AsyncMock side_effect cases incorrect refcount
type: behavior
versions: Python 3.9
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44410>
_______________________________________
More information about the New-bugs-announce
mailing list