]> granicus.if.org Git - python/commitdiff
Issue #21986: Idle now matches interpreter in not pickling user code objects.
authorTerry Jan Reedy <tjreedy@udel.edu>
Fri, 10 Oct 2014 23:33:45 +0000 (19:33 -0400)
committerTerry Jan Reedy <tjreedy@udel.edu>
Fri, 10 Oct 2014 23:33:45 +0000 (19:33 -0400)
Patch by Claudiu Popa

Lib/idlelib/rpc.py

index 9c51b8f6b52102ef803e8395b55720610ada1940..f2aae516c837d60567e5281bc2a4ecf52f883a61 100644 (file)
@@ -29,6 +29,7 @@ accomplished in Idle.
 
 import sys
 import os
+import io
 import socket
 import select
 import socketserver
@@ -53,16 +54,15 @@ def pickle_code(co):
     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 @@ class SocketIO(object):
     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