vm: use truthiness of the __exit__ result in WITH_CLEANUP by drew-512 · Pull Request #269 · go-python/gpython
Repository navigation
vm: use truthiness of the __exit__ result in WITH_CLEANUP - #269
Open
drew-512 wants to merge 1 commit into go-python:main from drew-512:vm-with-cleanup-truth
Open
vm: use truthiness of the __exit__ result in WITH_CLEANUP #269 drew-512 wants to merge 1 commit into go-python:main from drew-512:vm-with-cleanup-truth
Conversation
drew-512 commented Nov 10, 2026
Copy link Copy Markdown
Contributor
WITH_CLEANUP only silenced the exception when __exit__ returned the True singleton, so a context manager whose __exit__ returns e.g. 1 let the exception through.
CPython 3.4 tests the result with PyObject_IsTrue and jumps to the error path if that raises. This uses py.ObjectIsTrue, which follows PyObject_IsTrue (including __bool__/__len__ on class instances since #222 ), and returns its error.
Tests: vm/tests/with.py now checks that true non-bool results (1, "potato", [0], 0.5, object()) silence the error, false ones (None, False, 0, "", [], 0.0, and instances with a false __bool__ or zero __len__) don't, and that an exception raised by the result's __bool__ propagates. The new cases fail on main and pass here; the file also passes under CPython. go test -race ./... passes on Go 1.18, 1.19 and 1.27.
Fixes #267 .
vm: use truthiness of the __exit__ result in WITH_CLEANUP
d0d9fe6
WITH_CLEANUP only silenced the exception when __exit__ returned the True singleton, so a true non-bool result such as 1 let the exception propagate. Test the result with ObjectIsTrue, as CPython 3.4 does with PyObject_IsTrue, and return any error raised while doing so.Sign up for free to join this conversation on GitHub . Already have an account? Sign in to comment