Refs #33012 -- Moved PickleSerializer to django.core.serializers.base and added tests.

This commit is contained in:
Daniyal Abbasi 2021-09-02 11:15:40 +02:00 committed by Mariusz Felisiak
parent 50ed545e2f
commit dca4c2ff76
3 changed files with 37 additions and 18 deletions

View File

@ -1,20 +1,7 @@
import pickle
from django.core.serializers.base import (
PickleSerializer as BasePickleSerializer,
)
from django.core.signing import JSONSerializer as BaseJSONSerializer
class PickleSerializer:
"""
Simple wrapper around pickle to be used in signing.dumps and
signing.loads.
"""
protocol = pickle.HIGHEST_PROTOCOL
def dumps(self, obj):
return pickle.dumps(obj, self.protocol)
def loads(self, data):
return pickle.loads(data)
JSONSerializer = BaseJSONSerializer
PickleSerializer = BasePickleSerializer

View File

@ -1,6 +1,7 @@
"""
Module for abstract serializer/unserializer base classes.
"""
import pickle
from io import StringIO
from django.core.exceptions import ObjectDoesNotExist
@ -9,6 +10,21 @@ from django.db import models
DEFER_FIELD = object()
class PickleSerializer:
"""
Simple wrapper around pickle to be used in signing.dumps()/loads() and
cache backends.
"""
def __init__(self, protocol=None):
self.protocol = pickle.HIGHEST_PROTOCOL if protocol is None else protocol
def dumps(self, obj):
return pickle.dumps(obj, self.protocol)
def loads(self, data):
return pickle.loads(data)
class SerializerDoesNotExist(KeyError):
"""The requested serializer was not found."""
pass

View File

@ -1,3 +1,4 @@
import pickle
from datetime import datetime
from functools import partialmethod
from io import StringIO
@ -5,7 +6,7 @@ from unittest import mock, skipIf
from django.core import serializers
from django.core.serializers import SerializerDoesNotExist
from django.core.serializers.base import ProgressBar
from django.core.serializers.base import PickleSerializer, ProgressBar
from django.db import connection, transaction
from django.http import HttpResponse
from django.test import SimpleTestCase, override_settings, skipUnlessDBFeature
@ -418,6 +419,21 @@ class SerializersTransactionTestBase:
self.assertEqual(art_obj.author.name, "Agnes")
class PickleSerializerTests(SimpleTestCase):
def test_serializer_protocol(self):
serializer = PickleSerializer(protocol=3)
self.assertEqual(serializer.protocol, 3)
# If protocol is not provided, it defaults to pickle.HIGHEST_PROTOCOL
serializer = PickleSerializer()
self.assertEqual(serializer.protocol, pickle.HIGHEST_PROTOCOL)
def test_serializer_loads_dumps(self):
serializer = PickleSerializer()
test_data = 'test data'
dump = serializer.dumps(test_data)
self.assertEqual(serializer.loads(dump), test_data)
def register_tests(test_class, method_name, test_func, exclude=()):
"""
Dynamically create serializer tests to ensure that all registered