Title: Iterating over ctypes pointer causes hang or segmentation fault · Issue #92347 · python/cpython · GitHub
Open Graph Title: Iterating over ctypes pointer causes hang or segmentation fault · Issue #92347 · python/cpython
X Title: Iterating over ctypes pointer causes hang or segmentation fault · Issue #92347 · python/cpython
Description: Trying to create iterable (list, tuple, set etc.) from PyCPointer instance leads to hang or segmentation fault. This code causes hang and memory leak which goes until oom-killer kills python process: import ctypes x = ctypes.c_int32(1234...
Open Graph Description: Trying to create iterable (list, tuple, set etc.) from PyCPointer instance leads to hang or segmentation fault. This code causes hang and memory leak which goes until oom-killer kills python proces...
X Description: Trying to create iterable (list, tuple, set etc.) from PyCPointer instance leads to hang or segmentation fault. This code causes hang and memory leak which goes until oom-killer kills python proces...
Opengraph URL: https://github.com/python/cpython/issues/92347
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Iterating over ctypes pointer causes hang or segmentation fault","articleBody":"Trying to create iterable (`list`, `tuple`, `set` etc.) from `PyCPointer` instance leads to hang or segmentation fault.\r\nThis code causes hang and memory leak which goes until oom-killer kills python process:\r\n```\r\nimport ctypes\r\nx = ctypes.c_int32(12345)\r\nbuf4_p_t = ctypes.POINTER(ctypes.ARRAY(ctypes.c_byte, 4))\r\nbuf_p = ctypes.cast(ctypes.byref(x), buf4_p_t)\r\nlist(buf_p)\r\n```\r\nSimilar code that causes segmentation fault:\r\n```\r\nimport ctypes\r\nx = ctypes.c_int32(54321)\r\nxp = ctypes.pointer(x)\r\nlist(xp)\r\n```\r\nIterating over pointer using `for` statement leads to an infinite loop:\r\n`for i in buf_p: print(i)`\r\n`for i in xp: print(i)`\r\n`list(for i in buf_p)`\r\n\r\nTried on 3.10.3 and 3.11.0a6+, fails at both. \r\n\r\nThe possible reason is that `PyCPointer_Type` defines `sq_item` without defining `tp_iter`. \r\nDespite I can't imagine any reason why anyone might need to use pointer as iterable/sequence, raising `TypeError` in such situation would be a better option.\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-126318\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/chgnrdv","@type":"Person","name":"chgnrdv"},"datePublished":"2022-05-05T19:34:50.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/92347/cpython/issues/92347"}
| 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:7a5bc93b-54b2-9c97-c37b-d9d6e6bad5f5 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 91E4:2496BF:9C4D54:D82037:696A82BB |
| html-safe-nonce | b08bb0618c7f8d80471d84b94922fb3e10f99d0b2dd3876a114a5267b9665808 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MUU0OjI0OTZCRjo5QzRENTQ6RDgyMDM3OjY5NkE4MkJCIiwidmlzaXRvcl9pZCI6IjQwMzQ0MTk1NzkyNzI0NjMwMzUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | cf81a8dbb0e3540fc04fb0f4d78bd965452542a7dbf951dea8a638fa6cd266f9 |
| hovercard-subject-tag | issue:1227096426 |
| 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/92347/issue_layout |
| twitter:image | https://opengraph.githubassets.com/bf4a6b96424f8774c45688b0e8c8df8afa9733e5c29dee9abd8c77c18b1ebf83/python/cpython/issues/92347 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/bf4a6b96424f8774c45688b0e8c8df8afa9733e5c29dee9abd8c77c18b1ebf83/python/cpython/issues/92347 |
| og:image:alt | Trying to create iterable (list, tuple, set etc.) from PyCPointer instance leads to hang or segmentation fault. This code causes hang and memory leak which goes until oom-killer kills python proces... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | chgnrdv |
| hostname | github.com |
| expected-hostname | github.com |
| None | 913560fa317c3c5a71e34f9b19253c9f09d02b4b958a86c2a56f4c8541116377 |
| 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 | 5998c30593994bf2589055aef7b22d368a499367 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width