title: Cython and deepcopy() woes with referenced methods/functions. Any alternative ideas?
body: <p>I've been playing with Cython recently for the speed ups, but my project inherits a module that has a <code>copy()</code> method which uses <code>deepcopy()</code>. I tried implementing the <code>deepcopy()</code> within an overrided version of <code>copy()</code>, and I thought I had it working, but it doesn't appear to be anymore.</p>
  
  <pre><code>TypeError: object.__new__(cython_binding_builtin_function_or_method) is not safe,
     use cython_binding_builtin_function_or_method.__new__()
  </code></pre>
  
  <p>This is occuring in python/lib/copy_reg.py here:</p>
  
  <pre><code>return cls.__new__(cls, *args)
  </code></pre>
  
  <p>I'm on Python 2.7 here. Is it possible that a newer version of Python returns from <code>deepcopy()</code> in a "safe" way? I'm also on the latest version of Cython, 0.15.1.</p>
  
  <h2>Update3</h2>
  
  <p><em>Note that I've removed the previous updates to keep this as simple as possible.</em></p>
  
  <p>Ok! I think I found the incompatibility but I don't really know what to do about it.</p>
  
  <pre><code>class CythonClass:
      def __init__(self):
          self._handle = self._handles.get("handle_method")
  
      def call_handle(self):
          self._handle(self)
  
      def handle_method(self):
          print "I'm a little handle!"
  
      handles = {"handle_method", handle_method}
  </code></pre>
  
  <p>Then in my main app:</p>
  
  <pre><code>from cython1 import CythonClass
  from copy import deepcopy
  
  if __name__ == "__main__":
      gc1 = CythonClass()
      gc1.call_handle()
      gc2 = deepcopy(gc1)
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>I'm a little handle!
  
  Traceback (most recent call last):
    File "cythontest.py", line 8, in &lt;module&gt;
      gc2 = deepcopy(gc1)
    File "C:\python26\lib\copy.py", line 162, in deepcopy
      y = copier(x, memo)
    File "C:\python26\lib\copy.py", line 292, in _deepcopy_inst
      state = deepcopy(state, memo)
    File "C:\python26\lib\copy.py", line 162, in deepcopy
      y = copier(x, memo)
    File "C:\python26\lib\copy.py", line 255, in _deepcopy_dict
      y[deepcopy(key, memo)] = deepcopy(value, memo)
    File "C:\python26\lib\copy.py", line 189, in deepcopy
      y = _reconstruct(x, rv, 1, memo)
    File "C:\python26\lib\copy.py", line 323, in _reconstruct
      y = callable(*args)
    File "C:\python26\lib\copy_reg.py", line 93, in __newobj__
      return cls.__new__(cls, *args)
  TypeError: object.__new__(cython_binding_builtin_function_or_method) is not safe, use cython_binding_builtin_function_or_method.__new__()
  </code></pre>
  
  <p>The key is the function/handle reference:</p>
  
  <pre><code>handles = {"handle_method", handle_method}
  </code></pre>
  
  <p>If I don't include the method/function reference, Cython will not blow up during deepcopy. If I include one, it doesn't like how deepcopy/copy_reg copies the reference over.</p>
  
  <p>Any ideas besides not using method/function references? I have a bit of untangling to do if that the simple answer. (which I'm already working on as I finish typing this)</p>
  
  <p>Thanks!</p>