GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — vm: use truthiness of the __exit__ result in WITH_CLEANUP by drew-512 · Pull Request #269 · go-python/gpython

Conversation

@drew-512

@drew-512 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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@drew-512
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

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

vm: with statement ignores a true non-bool __exit__ result

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@drew-512
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.