CP session uses inefficient pickle protocl

guest avatarguest created an issue

CP's session handler uses the inefficient ASCII protocol 0 instead of the more efficient protocol 2 to pickle data. I suggest added a class variable PICKLE_PROTOCOL which is set to HIGHEST_PROTOCOL for file storage and 0 for Postgres storage.

Index: cherrypy/lib/sessions.py
===================================================================
--- cherrypy/lib/sessions.py
+++ cherrypy/lib/sessions.py
@@ -281,6 +281,7 @@
     
     SESSION_PREFIX = 'session-'
     LOCK_SUFFIX = '.lock'
+    PICKLE_PROTOCOL = pickle.HIGHEST_PROTOCOL
     
     def __init__(self, id=None, **kwargs):
         # The 'storage_path' arg is required for file-based sessions.
@@ -336,7 +337,7 @@
     def _save(self, expiration_time):
         f = open(self._get_file_path(), "wb")
         try:
-            pickle.dump((self._data, expiration_time), f)
+            pickle.dump((self._data, expiration_time), f, self.PICKLE_PROTOCOL)
         finally:
             f.close()
     
@@ -409,6 +410,7 @@
     
     You must provide your own get_db function.
     """
+    PICKLE_PROTOCOL = 0 # ASCII protocol 0
     
     def __init__(self, id=None, **kwargs):
         Session.__init__(self, id, **kwargs)
@@ -451,7 +453,7 @@
         return data, expiration_time
     
     def _save(self, expiration_time):
-        pickled_data = pickle.dumps(self._data)
+        pickled_data = pickle.dumps(self._data, self.PICKLE_PROTOCOL)
         self.cursor.execute('update session set data = %s, '
                             'expiration_time = %s where id = %s',
                             (pickled_data, expiration_time, self.id))

Comments (2)

  1. Log in to comment
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.