Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load from pickle without _cache #56

Merged
merged 1 commit into from
Feb 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions tests/test_url.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
import pickle
import pytest
from urllib.parse import SplitResult
from multidict import MultiDict, MultiDictProxy
Expand Down Expand Up @@ -1355,3 +1356,28 @@ def test_semicolon_as_value():
u = URL('http://127.0.0.1/?a=1%3Bb=2')
assert len(u.query) == 1
assert u.query['a'] == '1;b=2'


# serialize

def test_pickle():
u1 = URL('test')
hash(u1)
v = pickle.dumps(u1)
u2 = pickle.loads(v)
assert u1._cache
assert not u2._cache
assert hash(u1) == hash(u2)


def test_default_style_state():
u = URL('test')
hash(u)
u.__setstate__((None, {
'_val': 'test',
'_strict': False,
'_cache': {'hash': 1},
}))
assert not u._cache
assert u._val == 'test'
assert u._strict is False
12 changes: 12 additions & 0 deletions yarl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,18 @@ def __truediv__(self, name):
self._val._replace(path=new_path, query='', fragment=''),
encoded=True)

def __getstate__(self):
return self._val, self._strict

def __setstate__(self, state):
if state[0] is None and isinstance(state[1], dict):
# default style pickle
self._val = state[1]['_val']
self._strict = state[1]['_strict']
else:
self._val, self._strict = state
self._cache = {}

def is_absolute(self):
"""A check for absolute URLs.

Expand Down