DeprecationWarning: The default pickle serializer is deprecated as of Pyramid 1.9 and it will be changed to use pyramid.session.JSONSerializer in version 2.0. Explicitly set the serializer to avoid future incompatibilities
authorLaurent Peuch <cortex@worlddomination.be>
Wed, 08 May 2019 21:30:44 +0200
changeset 12589 85f82900f1c3
parent 12588 fa292e905edc
child 12590 a5d7fcde74c9
DeprecationWarning: The default pickle serializer is deprecated as of Pyramid 1.9 and it will be changed to use pyramid.session.JSONSerializer in version 2.0. Explicitly set the serializer to avoid future incompatibilities . See "Upcoming Changes to ISession in Pyramid 2.0" for more information about this change. As describe here https://docs.pylonsproject.org/projects/pyramid/en/1.10-branch/narr/sessions.html#pickle-session-deprecation use a serializer that fallback on pickle if needed to avoid impacting users.
cubicweb/pyramid/session.py
--- a/cubicweb/pyramid/session.py	Wed May 08 20:53:49 2019 +0200
+++ b/cubicweb/pyramid/session.py	Wed May 08 21:30:44 2019 +0200
@@ -89,7 +89,7 @@
 from contextlib import contextmanager
 
 from pyramid.compat import pickle
-from pyramid.session import SignedCookieSessionFactory
+from pyramid.session import SignedCookieSessionFactory, JSONSerializer, PickleSerializer
 
 from cubicweb import (
     Binary,
@@ -129,6 +129,24 @@
             yield cnx
 
 
+class JSONSerializerWithPickleFallback(object):
+    def __init__(self):
+        self.json = JSONSerializer()
+        self.pickle = PickleSerializer()
+
+    def dumps(self, value):
+        # maybe catch serialization errors here and keep using pickle
+        # while finding spots in your app that are not storing
+        # JSON-serializable objects, falling back to pickle
+        return self.json.dumps(value)
+
+    def loads(self, value):
+        try:
+            return self.json.loads(value)
+        except ValueError:
+            return self.pickle.loads(value)
+
+
 def CWSessionFactory(
         secret,
         cookie_name='session',
@@ -177,7 +195,7 @@
         reissue_time=reissue_time,
         hashalg=hashalg,
         salt=salt,
-        serializer=serializer)
+        serializer=serializer if serializer else JSONSerializerWithPickleFallback())
 
     class CWSession(SignedCookieSession):
         def __init__(self, request):