New submission from Daniel Andersson <[email protected]>:
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 <[email protected]>
<https://bugs.python.org/issue44410>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com