Skip to content

Instantly share code, notes, and snippets.

@dutc
Last active September 7, 2022 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dutc/c11a2d93443cb328f419355076fd8879 to your computer and use it in GitHub Desktop.
Save dutc/c11a2d93443cb328f419355076fd8879 to your computer and use it in GitHub Desktop.
`raise StopIteration` in generator body results in `RuntimeError` in Python ≥3.7
#!/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
\
#!/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
@dutc
Copy link
Author

dutc commented Sep 7, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment