Skip to content

aioitertools.zip is doing strange things with exceptions #153

@filip-zyzniewski

Description

@filip-zyzniewski

Description

What happens:

$ python3 -m venv aioitertools_zip_exception
$ cat aioitertools_zip_exception.py 
import asyncio
import platform
import sys

import aioitertools


async def gen():
    for i in range(10):
        if i == 9:
            assert False
        await asyncio.sleep(1)
        yield i


async def gen_pairs():
    for i in range(10):
        if i == 9:
            assert False
        await asyncio.sleep(1)
        yield i, "a"


async def main():
    print("platform version", platform.version())
    print("python version", sys.version)
    print("aioitertools version", aioitertools.__version__)

    print("the exception is getting swallowed:")
    async for (i, j) in aioitertools.zip(gen(), gen()):
        print(i, j)

    print("this attempts to unpack the exception:")
    async for (i, j), (k, l) in aioitertools.zip(gen_pairs(), gen_pairs()):
        print(i, j, k, l)


asyncio.run(main())
$ . ./aioitertools_zip_exception/bin/activate
(aioitertools_zip_exception) $ python3 -m pip install aioitertools
Collecting aioitertools
  Downloading aioitertools-0.11.0-py3-none-any.whl (23 kB)
Installing collected packages: aioitertools
Successfully installed aioitertools-0.11.0
(aioitertools_zip_exception) $ python3 aioitertools_zip_exception.py 
platform version Darwin Kernel Version 22.6.0: Wed Jul  5 22:22:05 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T6000
python version 3.11.5 (main, Aug 24 2023, 15:09:45) [Clang 14.0.3 (clang-1403.0.22.14.1)]
aioitertools version 0.11.0
the exception is getting swallowed:
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
 
this attempts to unpack the exception:
0 a 0 a
1 a 1 a
2 a 2 a
3 a 3 a
4 a 4 a
5 a 5 a
6 a 6 a
7 a 7 a
8 a 8 a
Traceback (most recent call last):
  File "/Users/Filip.Zyzniewski/aioitertools_zip_exception.py", line 38, in <module>
    asyncio.run(main())
  File "/opt/homebrew/Cellar/[email protected]/3.11.5/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.11.5/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.11.5/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/Users/Filip.Zyzniewski/aioitertools_zip_exception.py", line 34, in main
    async for (i, j), (k, l) in aioitertools.zip(gen_pairs(), gen_pairs()):
              ^^^^^^
TypeError: cannot unpack non-iterable AssertionError object
(aioitertools_zip_exception) $ 

What I expected to happen: I expected the exception to percolate up the call stack and be raised at the top level.

Details

  • OS: Darwin Kernel Version 22.6.0: Wed Jul 5 22:22:05 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T6000
  • Python version: 3.11.5 (main, Aug 24 2023, 15:09:45) [Clang 14.0.3 (clang-1403.0.22.14.1)]
  • aioitertools version: 0.11.0
  • Can you repro on master? yes: https://gist.github.com/filip-zyzniewski/1bb9c2dbde5da1dfeea5154f6e8a81f7
  • Can you repro in a clean virtualenv? done above

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions