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

Pickle support and fix failed build #41

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

install_requires = [
'attrs>=17.3.0',
'cattrs>=0.5.0',
'cattrs>=0.5.0,<0.7',
'jsonschema-extractor>=0.6.0',
'schematics>=2.0.0',
'six',
Expand Down
4 changes: 3 additions & 1 deletion transmute_core/contenttype_serializers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from .interface import ContentTypeSerializer
from .json_serializer import JsonSerializer
from .yaml_serializer import YamlSerializer
from .pickle_serializer import PickleSerializer
from .serializer_set import SerializerSet, NoSerializerFound


def get_default_serializer_set():
return SerializerSet([
JsonSerializer(),
YamlSerializer()
YamlSerializer(),
PickleSerializer(),
])
43 changes: 43 additions & 0 deletions transmute_core/contenttype_serializers/pickle_serializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from .interface import ContentTypeSerializer
from ..exceptions import SerializationException
try:
import cPickle as pickle
except ImportError:
import pickle


class PickleSerializer(ContentTypeSerializer):

content_type = ["application/pickle"]

@staticmethod
def dump(data):
"""
should return back a bytes (or string in python 2),
representation of your object, to be used in e.g. response
bodies.
"""
return pickle.dumps(data, protocol=-1)

@property
def main_type(self):
return self.content_type[0]

@staticmethod
def load(raw_bytes):
"""
given a bytes object, should return a base python data
structure that represents the object.
"""
try:
return pickle.loads(raw_bytes)
except pickle.UnpicklingError as e:
raise SerializationException(str(e))

@staticmethod
def can_handle(content_type_name):
"""
given a content type, returns true if this serializer
can convert bodies of the given type.
"""
return "pickle" in content_type_name
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ def test_default_serializer_yaml(serializer_set):
assert serializer.load(expected_to) == frm


def test_default_serializer_pickle(serializer_set):
frm = {"foo": "bar"}
serializer = serializer_set["application/pickle"]
assert serializer.load(serializer.dump(frm)) == frm


def test_default_serializer_prop(serializer_set):
assert serializer_set.default.main_type == "application/json"

Expand All @@ -27,7 +33,8 @@ def test_no_serializer_found_raises_exception(serializer_set):

@pytest.mark.parametrize("content_type,bad_input", [
("application/yaml", b"[a, !eafia']atedntad}"),
("application/json", b"{\"ooga")
("application/json", b"{\"ooga"),
("application/pickle", b"fdafd"),
])
def test_bad_object_raises_serialization_exception(serializer_set, content_type, bad_input):
""" a bad object serialization should raise a serialization exception """
Expand All @@ -37,5 +44,5 @@ def test_bad_object_raises_serialization_exception(serializer_set, content_type,

def test_keys(serializer_set):
assert serializer_set.keys() == [
"application/json", "application/x-yaml"
"application/json", "application/x-yaml", "application/pickle"
]