Title: warnings.catch_warnings is async-unsafe · Issue #91505 · python/cpython · GitHub
Open Graph Title: warnings.catch_warnings is async-unsafe · Issue #91505 · python/cpython
X Title: warnings.catch_warnings is async-unsafe · Issue #91505 · python/cpython
Description: import warnings import asyncio async def spam(): with warnings.catch_warnings(record=True) as ws: await asyncio.sleep(0.1) w = Warning("12345") warnings.warn(w) assert w in (ww.message for ww in ws) async def ham(): with warnings.catch_w...
Open Graph Description: import warnings import asyncio async def spam(): with warnings.catch_warnings(record=True) as ws: await asyncio.sleep(0.1) w = Warning("12345") warnings.warn(w) assert w in (ww.message for ww in ws...
X Description: import warnings import asyncio async def spam(): with warnings.catch_warnings(record=True) as ws: await asyncio.sleep(0.1) w = Warning("12345") warnings.warn(w) assert w in (ww.message fo...
Opengraph URL: https://github.com/python/cpython/issues/91505
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"warnings.catch_warnings is async-unsafe","articleBody":"\r\n\r\n```python\r\nimport warnings\r\nimport asyncio\r\n\r\nasync def spam():\r\n\twith warnings.catch_warnings(record=True) as ws:\r\n\t\tawait asyncio.sleep(0.1)\r\n\t\tw = Warning(\"12345\")\r\n\t\twarnings.warn(w)\r\n\t\tassert w in (ww.message for ww in ws)\r\n\r\nasync def ham():\r\n\twith warnings.catch_warnings(record=True) as ws:\r\n\t\tawait asyncio.sleep(0.2)\r\n\r\nasync def main():\r\n\tawait asyncio.gather(spam(), ham())\r\n\r\nasyncio.run(main())\r\n```\r\n\r\nDespite technically being documented (\u003chttps://docs.python.org/3/library/warnings.html#warnings.catch_warnings\u003e), this is very error-prone, surprising and not at all useful. The warnings module should probably use `contextvars` to manage the warning handler.","author":{"url":"https://github.com/fstirlitz","@type":"Person","name":"fstirlitz"},"datePublished":"2022-04-13T12:34:49.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":13},"url":"https://github.com/91505/cpython/issues/91505"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:1463062b-5169-f80f-80d8-9a3d54858e04 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 977A:203A5F:144CAC:1C39FE:6A53676C |
| html-safe-nonce | 0a7266b56ef893bcd2c484c8406aaa9ac6425898d302669413774d94834dc276 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NzdBOjIwM0E1RjoxNDRDQUM6MUMzOUZFOjZBNTM2NzZDIiwidmlzaXRvcl9pZCI6IjQyODA0MzEyOTc2NzI5MDczMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 511227487c771ab8ca1b65580d0ebe9b41ebce9c1b3228a367ea5b23ff283d67 |
| hovercard-subject-tag | issue:1203240855 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/91505/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e003a15d79d945ba7c270dea2ac04b07f2acff60487a122a9540607f52d58e3e/python/cpython/issues/91505 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e003a15d79d945ba7c270dea2ac04b07f2acff60487a122a9540607f52d58e3e/python/cpython/issues/91505 |
| og:image:alt | import warnings import asyncio async def spam(): with warnings.catch_warnings(record=True) as ws: await asyncio.sleep(0.1) w = Warning("12345") warnings.warn(w) assert w in (ww.message for ww in ws... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | fstirlitz |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width