Repository: cupy/cupy
Pull request: Support weak reference to CuPy array
fix #1346

Problem statement:
Support weak reference of CuPy array 
The following code fails with 'TypeError: cannot create weak reference to 'cupy.core.core.ndarray' object'
```
import numpy as np

import weakref
import chainer
from chainer.backends import cuda

x_np = np.array([3.2, 9.1])
# this works
np_weak_ref = weakref.ref(x_np)

x_cp = cuda.cupy.array([3.2, 9.1])
# this fails
cp_weak_ref = weakref.ref(x_cp)
```

Hints from issue discussion:

Commit message: edits

Patch to review:
diff --git a/cupy/core/core.pxd b/cupy/core/core.pxd
index 2dc7b0d6097..548584fdd9a 100644
--- a/cupy/core/core.pxd
+++ b/cupy/core/core.pxd
@@ -4,6 +4,7 @@ from cupy.cuda cimport memory
 from cupy.cuda.function cimport CPointer
 
 cdef class ndarray:
+    cdef object __weakref__
     cdef:
         readonly Py_ssize_t size
         public vector.vector[Py_ssize_t] _shape


Code review comments:
diff_hunk: @@ -4,6 +4,7 @@ from cupy.cuda cimport memory
 from cupy.cuda.function cimport CPointer
 
 cdef class ndarray:
+    cdef object __weakref__
path: cupy/core/core.pxd
text: @user1:
You can just put `object __weakref__` under `cdef:` block below.