Skip to content

Commit

Permalink
Merge pull request #1 from ddelange/patch-1
Browse files Browse the repository at this point in the history
Allow protocol=0, default to HIGHEST_PROTOCOL
  • Loading branch information
victorfsf authored Oct 5, 2020
2 parents e808357 + 76bf056 commit 949a4e6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ Pickleclip will copy any python object supported by pickle (or dill) into your c

### Copying from Python 3 to Python 2 using pickle

There are some known compatibility problems with copying and pasting code from Python 3 to Python 2, but some of those problems can be solved by using the `protocol` kwarg:
To get Python 3 objects to unpickle on Python 2, specify `protocol=2` when copying. Although pickle will try to map the new Python 3 names to the old module names used in Python 2, so that the pickle data stream is readable with Python 2, this does not come without limitations.

Going from Python 2 to Python 3 is even less trivial, and will mostly only work for the simplest cases. See also [pickle-compat](https://pypi.org/project/pickle-compat/).

By default, the highest [pickle protocol](https://docs.python.org/3/library/pickle.html#pickle-protocols) available to the interpreter is used. The higher the protocol used, the more recent the version of Python needed to read the pickle produced.

##### Python 3 shell:

Expand All @@ -72,8 +76,6 @@ In [2]: picklec.paste()
Out[2]: {'hello': 'world'}
```

Since the PY2's pickle version can't handle the PY3's protocol number (which is 3), you'll need to force the PY3 pickle to use `protocol = 2`.

### Copying from different Python versions using dill

It should work fine with basic objects (str, int, float, list, set, dict...), but it won't work with nothing too "fancy", (functions, custom objects...).
3 changes: 1 addition & 2 deletions pickleclip/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import six


def copy(obj, protocol=None, serializer=pickle):
protocol = protocol or (2 if six.PY2 else 3)
def copy(obj, protocol=-1, serializer=pickle):
dump = serializer.dumps(obj, protocol=protocol)
pyperclip.copy(dump.decode('latin1'))

Expand Down

0 comments on commit 949a4e6

Please sign in to comment.