Use pickle.HIGHEST_PROTOCOL by default (#1254)

main
Bo Bayles 5 years ago committed by GitHub
parent 5859339a51
commit f0846a7645
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,9 +1,15 @@
from functools import partial
import pickle import pickle
from .compat import string_types from .compat import string_types
from .utils import import_attribute from .utils import import_attribute
class DefaultSerializer:
dumps = partial(pickle.dumps, protocol=pickle.HIGHEST_PROTOCOL)
loads = pickle.loads
def resolve_serializer(serializer): def resolve_serializer(serializer):
"""This function checks the user defined serializer for ('dumps', 'loads') methods """This function checks the user defined serializer for ('dumps', 'loads') methods
It returns a default pickle serializer if not found else it returns a MySerializer It returns a default pickle serializer if not found else it returns a MySerializer
@ -11,7 +17,7 @@ def resolve_serializer(serializer):
Also accepts a string path to serializer that will be loaded as the serializer Also accepts a string path to serializer that will be loaded as the serializer
""" """
if not serializer: if not serializer:
return pickle return DefaultSerializer
if isinstance(serializer, string_types): if isinstance(serializer, string_types):
serializer = import_attribute(serializer) serializer = import_attribute(serializer)

@ -4,10 +4,11 @@ from __future__ import (absolute_import, division, print_function,
import json import json
import pickle import pickle
import pickletools
import queue import queue
import unittest import unittest
from rq.serializers import resolve_serializer from rq.serializers import DefaultSerializer, resolve_serializer
class TestSerializers(unittest.TestCase): class TestSerializers(unittest.TestCase):
@ -15,7 +16,16 @@ class TestSerializers(unittest.TestCase):
"""Ensure function resolve_serializer works correctly""" """Ensure function resolve_serializer works correctly"""
serializer = resolve_serializer(None) serializer = resolve_serializer(None)
self.assertIsNotNone(serializer) self.assertIsNotNone(serializer)
self.assertEqual(serializer, pickle) self.assertEqual(serializer, DefaultSerializer)
# Test round trip with default serializer
test_data = {'test': 'data'}
serialized_data = serializer.dumps(test_data)
self.assertEqual(serializer.loads(serialized_data), test_data)
self.assertEqual(
next(pickletools.genops(serialized_data))[1],
pickle.HIGHEST_PROTOCOL
)
# Test using json serializer # Test using json serializer
serializer = resolve_serializer(json) serializer = resolve_serializer(json)

Loading…
Cancel
Save