Title: py/with macro doesn't bind the return value of __enter__ method · Issue #273 · clj-python/libpython-clj · GitHub
Open Graph Title: py/with macro doesn't bind the return value of __enter__ method · Issue #273 · clj-python/libpython-clj
X Title: py/with macro doesn't bind the return value of __enter__ method · Issue #273 · clj-python/libpython-clj
Description: The py/with macro implementation doesn't correctly follow Python's with statement semantics. According to the Python documentation, the value bound in a with statement should be the return value of calling __enter__() on the context mana...
Open Graph Description: The py/with macro implementation doesn't correctly follow Python's with statement semantics. According to the Python documentation, the value bound in a with statement should be the return value of...
X Description: The py/with macro implementation doesn't correctly follow Python's with statement semantics. According to the Python documentation, the value bound in a with statement should be the return ...
Opengraph URL: https://github.com/clj-python/libpython-clj/issues/273
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"py/with macro doesn't bind the return value of __enter__ method","articleBody":"The `py/with` macro implementation doesn't correctly follow Python's `with` statement semantics. According to the [Python documentation](https://docs.python.org/3/reference/compound_stmts.html#the-with-statement), the value bound in a `with` statement should be the return value of calling `__enter__()` on the context manager:\n\n```python\nmanager = (EXPRESSION)\nenter = type(manager).__enter__\nexit = type(manager).__exit__\nvalue = enter(manager) # This value should be bound to TARGET\nhit_except = False\n\ntry:\n TARGET = value # TARGET gets the return value of enter(manager)\n SUITE\n...\n```\n\n**Current Implementation**\n\nThe current macro ([with.clj#L45-L63](https://github.com/clj-python/libpython-clj/blob/7f572215a96ac6fb14b5928ee6258b889cdee375/src/libpython_clj2/python/with.clj#L45C1-L63C60)) calls `__enter__` but discards its return value:\n\n```clojure\n(py-fn/call-attr ~varname \"__enter__\" nil) ; Return value is ignored\n(try\n (let [retval# (do ~@body)]\n ...\n```\n\n**Expected Behavior**\n\nThe bound variable should receive the return value of `__enter__()`, not the context manager itself:\n\n```clojure\n(let [enter-result# (py-fn/call-attr ~varname \"__enter__\" nil)]\n (try\n (let [~varname enter-result# ; Rebind to __enter__'s return value\n retval# (do ~@body)]\n ...\n```\n\n**Why Current Tests Pass**\n\nThe existing test in [python_test.clj#L167](https://github.com/clj-python/libpython-clj/blob/master/test/libpython_clj2/python_test.clj#L167) uses a `WithObjClass` where `__enter__()` returns `None` (implicitly), and the test methods are called on the original object. This masks the bug.\n\n**Real-World Example**\n\nA common Python pattern that fails with the current implementation:\n\n```python\nwith open('file.txt', 'r') as f:\n content = f.read() # f should be the file object returned by __enter__\n```\n\nWith the current macro, `f` would be bound to the unopened file path/manager rather than the file object returned by `__enter__()`.\n\n**Next Steps**\n\n1. Add test cases that verify this behavior (e.g., update `WithObjClass.__enter__` to return a different object, or test with Python's built-in file context manager)\n2. Update the macro to correctly bind the return value of `__enter__()`","author":{"url":"https://github.com/lidorcg","@type":"Person","name":"lidorcg"},"datePublished":"2025-11-10T16:57:54.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/273/libpython-clj/issues/273"}
| 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:b1c420f6-5bb7-dfb5-8cd1-c7260b990568 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8DF4:1A1383:315C40B:443F3E1:6A60CC13 |
| html-safe-nonce | 900d6fd66d76279ca84ca67f634096433b867ba0e1e418a7db7085806ffd1f59 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4REY0OjFBMTM4MzozMTVDNDBCOjQ0M0YzRTE6NkE2MENDMTMiLCJ2aXNpdG9yX2lkIjoiNjk5NDYzMTc4NDEwNzM5NjExNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 1e33462730a5d913e5b832c59cffafd9259291ce3d92be3520082703f81b1e70 |
| hovercard-subject-tag | issue:3608896885 |
| 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/clj-python/libpython-clj/273/issue_layout |
| twitter:image | https://opengraph.githubassets.com/21dc15a918acf0dcfe1853872820c3eb784f6c9a23945a219a91dc8117cb7329/clj-python/libpython-clj/issues/273 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/21dc15a918acf0dcfe1853872820c3eb784f6c9a23945a219a91dc8117cb7329/clj-python/libpython-clj/issues/273 |
| og:image:alt | The py/with macro implementation doesn't correctly follow Python's with statement semantics. According to the Python documentation, the value bound in a with statement should be the return value of... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | lidorcg |
| hostname | github.com |
| expected-hostname | github.com |
| None | a9556c67fd2e63f5fecafc192915a3b089ef8170a9384ff6b59d7b4a2f6b73f8 |
| turbo-cache-control | no-preview |
| go-import | github.com/clj-python/libpython-clj git https://github.com/clj-python/libpython-clj.git |
| octolytics-dimension-user_id | 60827878 |
| octolytics-dimension-user_login | clj-python |
| octolytics-dimension-repository_id | 187095617 |
| octolytics-dimension-repository_nwo | clj-python/libpython-clj |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 187095617 |
| octolytics-dimension-repository_network_root_nwo | clj-python/libpython-clj |
| 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 | f464e59c3a800187a50f3c89c36ee16df9992c1b |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width