Skip to content

Fix: Context variables shared between code don't work with greenlet > 0.4.1 - #1062

Merged
sebastinas merged 1 commit into
bpython:mainfrom
goransh-walia:fix/issue-852-305
Nov 14, 2026
Merged

sebastinas merged 1 commit into
bpython:mainfrom
goransh-walia:fix/issue-852-305

Conversation

@goransh-walia

Copy link
Copy Markdown
Contributor

Fixes #852

Summary

Fixes an issue where context variables (such as contextvars.ContextVar) cannot be shared across multiple lines of input when running greenlet > 0.4.17.

Root cause

_unload_code() was clearing self.code_context by setting it to None every time code finished running for a line, preventing the execution context from persisting across code runs.

Changes

  • Removed self.code_context = None from _unload_code() in bpython/curtsiesfrontend/coderunner.py.
  • Explicitly set self.code_context = None only when handling Done, Unfinished, or SystemExitRequest responses in run_code().
  • Updated sigint_handler() in bpython/curtsiesfrontend/coderunner.py to check that self.code_context is truthy before comparing it to the current greenlet.

Testing

Syntax check passed on the changed file(s). The full test suite was not run locally, so I'm relying on CI to confirm.

AI-assisted.

@codecov

codecov Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 25.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.29%. Comparing base (1a919d3) to head (d613024).
⚠️ Report is 57 commits behind head on main.

Files with missing lines Patch % Lines
bpython/curtsiesfrontend/coderunner.py 25.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1062      +/-   ##
==========================================
- Coverage   76.51%   76.29%   -0.22%     
==========================================
  Files          62       61       -1     
  Lines        8308     8459     +151     
==========================================
+ Hits         6357     6454      +97     
- Misses       1951     2005      +54     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sebastinas
sebastinas merged commit f6e4b40 into bpython:main Nov 14, 2026
6 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Context variables shared between code don't work with greenlet > 0.4.17

2 participants