cloudpipe / cloudpipe/cloudpickle

Cannot pickle functions decorated through contextlib

Open
#509 1 comment 2 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
1.9k
Forks
195
Avg merge
1d 10h
Merged PRs (30d)
1

Description

It's a somewhat little known feature of `contextlib.contextmanager` and `contextlib.asynccontextmanager` that you can use the decorated functions not only as context managers, but also as function decorators:

```python
import asyncio
from contextlib import contextmanager, asynccontextmanager

@contextmanager
def c():
print("before")
yield
print("after")

@c()
def f():
print("hello world")

f()

@asynccontextmanager
async def ac():
print("async before")
yield
print("async after")

@ac()
async def af():
print("async hello world")

asyncio.run(af())
```
```
before
hello world
after
async before
async hello world
async after
```

cloudpickle 2.2.1 fails to serialize functions decorated this way:
```python
>>> pickle.loads(pickle.dumps(f))

>>> pickle.loads(pickle.dumps(af))

>>> cloudpickle.dumps(f)
Traceback (most recent call last):
File "demo.py", line 34, in
cloudpickle.dumps(f)
^^^^^^^^^^^^^^^^^^^^
File "lib/python3.11/site-packages/cloudpickle/cloudpickle_fast.py", line 73, in dumps
cp.dump(obj)
File "lib/python3.11/site-packages/cloudpickle/cloudpickle_fast.py", line 632, in dump
return Pickler.dump(self, obj)
^^^^^^^^^^^^^^^^^^^^^^^
TypeError: cannot pickle 'generator' object

>>> cloudpickle.dumps(af)
Traceback (most recent call last):
File "demo.py", line 35, in
cloudpickle.dumps(af)
^^^^^^^^^^^^^^^^^^^^^
File "lib/python3.11/site-packages/cloudpickle/cloudpickle_fast.py", line 73, in dumps
cp.dump(obj)
File "lib/python3.11/site-packages/cloudpickle/cloudpickle_fast.py", line 632, in dump
return Pickler.dump(self, obj)
^^^^^^^^^^^^^^^^^^^^^^^
TypeError: cannot pickle 'async_generator' object
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.