Last active
September 7, 2022 18:17
-
-
Save dutc/c11a2d93443cb328f419355076fd8879 to your computer and use it in GitHub Desktop.
`raise StopIteration` in generator body results in `RuntimeError` in Python ≥3.7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
code="$(<<-EOF | |
#!/usr/bin/env python3 | |
from logging import getLogger, INFO, basicConfig | |
from sys import version_info | |
def g(): | |
raise StopIteration() | |
yield | |
if __name__ == '__main__': | |
logger = getLogger(__name__) | |
basicConfig(level=INFO) | |
logger.info('Version: %s', version_info) | |
try: | |
[_ for _ in g()] | |
except RuntimeError: | |
if version_info.minor >= 7: | |
logger.info('raised RuntimeError (as expected)') | |
else: | |
raise AssertionError('should not raise RuntimeError on Python <3.7') | |
else: | |
if version_info.minor >= 7: | |
raise AssertionError('should raise RuntimeError on Python ≥3.7') | |
else: | |
logger.info('did not raise RuntimeError (as expected)') | |
EOF | |
)" | |
for ver in 3.5 3.6 3.7 3.8 3.9 3.10 3.11; do | |
docker run --rm -i python:$ver <<< "${code}" | |
done | |
\ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
code="$(<<-EOF | |
#!/usr/bin/env python3 | |
from logging import getLogger, INFO, basicConfig | |
from sys import version_info | |
class T: | |
def __getattr__(self, key): | |
raise Exception() | |
if __name__ == '__main__': | |
logger = getLogger(__name__) | |
basicConfig(level=INFO) | |
logger.info('Version: %s', version_info) | |
obj = T() | |
try: | |
assert not hasattr(obj, 'attr') | |
except Exception: | |
if version_info.major == 2: | |
raise AssertionError('should not raise Exception on Python 2.7') | |
else: | |
logger.info('raised Exception (as expected)') | |
else: | |
if version_info.major == 2: | |
logger.info('did not raise Exception (as expected)') | |
else: | |
raise AssertionError('should raise Exception on Python >2.7') | |
EOF | |
)" | |
for ver in 2.7 3.2 3.10; do | |
docker run --rm -i python:$ver <<< "${code}" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See Python 3.7 Changelog and PEP-479 — (PEP-479: https://peps.python.org/pep-0479/)