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

Python 3 serialization fixes, serialization improvements #131

Merged
merged 11 commits into from
Dec 5, 2016

Conversation

tomkins
Copy link
Contributor

@tomkins tomkins commented Nov 15, 2016

I was looking at trying to use/resurrect django-pymemcache, however in trying to update it and get tests working on it - pymemcache serialization seems to have some issues with byte/unicode strings. This is mostly a Python 3 problem, but could cause problems elsewhere.

To solve this problem, I've taken a similar approach to https://github.com/linsomniac/python-memcached/pull/100/files:

  • Only byte strings get serialized without a flag
  • Unicode strings get serialized with a different text flag
  • Only specific builtin types get flags added, everything else gets pickled

Also test_serde.py wasn't being picked up with pytest as it didn't have a decorator - so I've added that.

if isinstance(value, str):
# Check against exact types so that subclasses of native types will be
# restored as their native type
if value_type == six.binary_type:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(nits) I think value_type is bytes is more readable.

Copy link
Collaborator

@nichochar nichochar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,
Thanks for fixing the tests (by making them run). Also thanks for the python3 enhancements. We don't use python3 at Pinterest so it's a little hard for us to catch when things go wrong, always a pleasure when the community patches stuff.

There are errors in the automatic test build, you can click on it under the PR comments view, or click here directly.

Those need to be fixed before we can approve.

pass


@pytest.mark.unit()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the manual call to make sure it's called? Thanks for adding!

client.flush_all()

check(b'byte string')
check(u'unicode string')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also check try some harder cases:

  • 'olé' (with and without specifying the u unicode prefix
  • list of strings with unicode in them

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added in defaultdict (a subclass of dict) to test pickle comparisons as well - as it's available in Python 2.5+.

check({'a': 'pickle'})


@pytest.mark.integration()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

def test_str(self):
self.check('value')
def test_bytes(self):
self.check(b'value')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's try this with unicode characters.
Also maybe explicitely ask for FLAG_BYTES as it should default to this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FLAG_BYTES didn't exist before - so I've added that in, even though it's 0.

@tomkins
Copy link
Contributor Author

tomkins commented Nov 23, 2016

Not too sure what to do about the pypy3 failure.

The Travis build environment has changed between 15th-16th November. A newer version of virtualenv is installed, which is trying to install a newer version of pip - which now only supports Python 3.3+ for Python 3. The Travis environment is calling pypy3 v2.4.0 - a Python 3.2 implementation of Python.

I could just disable it, unless there's another option you prefer?

@nichochar
Copy link
Collaborator

nichochar commented Nov 27, 2016

Thanks for raising this, I have opened a ticket in order to drop pypy3.
It's a bit of a head scratcher, but I think I have found a fix I can probably get in before end of week, and get this in

@nichochar
Copy link
Collaborator

After exploring this issue, we have decided to drop support for that interpreter version. Feel free to rebase and push, and we can go ahead with this PR.

Currently this will fail with unicode strings on Python 3, as the pymemcache client will return a byte string - but the deserializer doesn't change it back to a unicode string.
The pymemcache client will return a byte string, so we'll do the same to test that the deserializer works as expected.

This currently fails with Python 3.
- Add text serializer/deserializer
- Now more strict with serializing a type
Works across all versions of Python, no need to use six for it
- A string (will vary on Python version)
- Another unicode string
- A list (pickled)
- A defaultdict (pickled)
Unused in serde itself (it's a default), but used for testing comparisons
@tomkins tomkins force-pushed the serde-serialization branch from dd46704 to 164f4cf Compare December 4, 2016 23:29
@tomkins
Copy link
Contributor Author

tomkins commented Dec 4, 2016

Rebased and pushed.

@nichochar nichochar merged commit e7b99ad into pinterest:master Dec 5, 2016
@nichochar
Copy link
Collaborator

nichochar commented Dec 5, 2016

Thanks for the improvements @tomkins tomkins, we really appreciate them!

@tomkins tomkins deleted the serde-serialization branch December 5, 2016 22:40
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.

3 participants