[Python-checkins] cpython (merge 3.4 -> default): Merge with 3.4: #21986, don't pickle user code objects.

terry.reedy python-checkins at python.org
Sat Oct 11 01:35:04 CEST 2014


https://hg.python.org/cpython/rev/cb94764bf8be
changeset:   92944:cb94764bf8be
parent:      92941:af0104aed5b1
parent:      92943:90c62e1f3658
user:        Terry Jan Reedy <tjreedy at udel.edu>
date:        Fri Oct 10 19:34:38 2014 -0400
summary:
  Merge with 3.4: #21986, don't pickle user code objects.

files:
  Lib/idlelib/rpc.py |  20 ++++++++++----------
  1 files changed, 10 insertions(+), 10 deletions(-)


diff --git a/Lib/idlelib/rpc.py b/Lib/idlelib/rpc.py
--- a/Lib/idlelib/rpc.py
+++ b/Lib/idlelib/rpc.py
@@ -29,6 +29,7 @@
 
 import sys
 import os
+import io
 import socket
 import select
 import socketserver
@@ -53,16 +54,15 @@
     ms = marshal.dumps(co)
     return unpickle_code, (ms,)
 
-# XXX KBK 24Aug02 function pickling capability not used in Idle
-#  def unpickle_function(ms):
-#      return ms
+def dumps(obj, protocol=None):
+    f = io.BytesIO()
+    p = CodePickler(f, protocol)
+    p.dump(obj)
+    return f.getvalue()
 
-#  def pickle_function(fn):
-#      assert isinstance(fn, type.FunctionType)
-#      return repr(fn)
-
-copyreg.pickle(types.CodeType, pickle_code, unpickle_code)
-# copyreg.pickle(types.FunctionType, pickle_function, unpickle_function)
+class CodePickler(pickle.Pickler):
+    dispatch_table = {types.CodeType: pickle_code}
+    dispatch_table.update(copyreg.dispatch_table)
 
 BUFSIZE = 8*1024
 LOCALHOST = '127.0.0.1'
@@ -329,7 +329,7 @@
     def putmessage(self, message):
         self.debug("putmessage:%d:" % message[0])
         try:
-            s = pickle.dumps(message)
+            s = dumps(message)
         except pickle.PicklingError:
             print("Cannot pickle:", repr(message), file=sys.__stderr__)
             raise

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list