Skip to content

Conversation

@hroncok
Copy link

@hroncok hroncok commented May 22, 2025

Fixes #716

Summary

Checklist

Documentation and Tests

Release Management

  • Added "Fixes #NNN" in the PR body, referencing the issue (#NNN) it closes.
  • Added a comment to issue #NNN, linking back to this PR.
  • Requested a review.

@hroncok
Copy link
Author

hroncok commented May 22, 2025

OK, this makes the tests pass. However, I am not entirely sure this is correct. I created it by trial and error.

@mmckerns
Copy link
Member

This is on my short list to get a fix in for, so thanks for submitting. I'll review, test, and follow up here.

@mmckerns
Copy link
Member

See also #586

@mmckerns
Copy link
Member

mmckerns commented May 24, 2025

This fix only works when the Context is empty. If you add a ContextVar, so the Context is not empty, it fails on the pickling of a ContextVar. Simple test code:

import contextvars as cv
var = cv.ContextVar('var')
t0 = var.set(0)
foo = cv.ContextVar('foo', default=0)
t1 = foo.set(1)
zap = cv.ContextVar('zap', default=3)
t4 = zap.set(4)
zap.reset(t4)
qux = cv.ContextVar('qux', default=0)
t2 = qux.set(2)
t3 = qux.set(3)
qux.reset(t3)
ctx = cv.copy_context()
print(list(ctx.items()))

True, the tests pass with an empty Context, and I believe you function is correct, it's not a full solution. Potentially we can merge this, and open a new ticket for a ContextVar and Token. However, I think ContextVar and Token can probably be handled with a petty simple registration function utilizing get and set. The tricky part may be whether a ContextVar and Token get referenced appropriately in the enclosing Context.

@mmckerns mmckerns added this to the dill-0.4.2 milestone Oct 14, 2025
@eli-schwartz
Copy link

Looks like this was silently included in #726?

@mmckerns
Copy link
Member

mmckerns commented Dec 14, 2025

Yeah... I think I started the new branch with the edited version of _dill.py and it slid in. The issue with the ContectVar still remains. We will still get that in with this PR.

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.

Python 3.14.0b1: TypeError: cannot pickle '_contextvars.Context' object

3 participants