From f0846a764545228d0551ec51c4c7441f2aa2a65a Mon Sep 17 00:00:00 2001 From: Bo Bayles Date: Sat, 16 May 2020 05:42:07 -0500 Subject: [PATCH] Use pickle.HIGHEST_PROTOCOL by default (#1254) --- rq/serializers.py | 8 +++++++- tests/test_serializers.py | 14 ++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/rq/serializers.py b/rq/serializers.py index c4b0e54..27f892f 100644 --- a/rq/serializers.py +++ b/rq/serializers.py @@ -1,9 +1,15 @@ +from functools import partial import pickle from .compat import string_types from .utils import import_attribute +class DefaultSerializer: + dumps = partial(pickle.dumps, protocol=pickle.HIGHEST_PROTOCOL) + loads = pickle.loads + + def resolve_serializer(serializer): """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 @@ -11,7 +17,7 @@ def resolve_serializer(serializer): Also accepts a string path to serializer that will be loaded as the serializer """ if not serializer: - return pickle + return DefaultSerializer if isinstance(serializer, string_types): serializer = import_attribute(serializer) diff --git a/tests/test_serializers.py b/tests/test_serializers.py index 58d093f..1e3e671 100644 --- a/tests/test_serializers.py +++ b/tests/test_serializers.py @@ -4,10 +4,11 @@ from __future__ import (absolute_import, division, print_function, import json import pickle +import pickletools import queue import unittest -from rq.serializers import resolve_serializer +from rq.serializers import DefaultSerializer, resolve_serializer class TestSerializers(unittest.TestCase): @@ -15,7 +16,16 @@ class TestSerializers(unittest.TestCase): """Ensure function resolve_serializer works correctly""" serializer = resolve_serializer(None) 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 serializer = resolve_serializer(json)