Skip to content

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

Open
@c-py

Description

@c-py

greenlet 0.4.17 introduced support for ContextVars.

It seems the result of this, is that the following code no longer works in bpython:

>>> import contextvars
>>> help = contextvars.ContextVar("helpme")
>>> token = help.set("value")
>>> help.reset(token)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    help.reset(token)
ValueError: <Token var=<ContextVar name='helpme' at 0x10f84b770> at 0x10f603940> was created in a different Context

The workaround is to put all this code in a single function

>>> def do_the_stuff():
...     help = contextvars.ContextVar("helpme")
...     token = help.set("value")
...     help.reset(token)
...     
... 
>>> do_the_stuff()

I believe this might be caused because a new greenlet is created for each line of code in bpython.
Is it possible for everything to run in the same context so that the repl behaviors similar to how a python script would run?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions