@@ -143,29 +143,38 @@ PyArray_SetBaseObject(PyArrayObject *arr, PyObject *obj)
143
143
}
144
144
145
145
/*
146
- * Don't allow chains of views, always set the base
147
- * to the owner of the data. That is, either the first object
148
- * which isn't an array, or the first object which owns
149
- * its own data.
146
+ * Don't allow infinite chains of views, always set the base
147
+ * to the first owner of the data.
148
+ * That is, either the first object which isn't an array,
149
+ * or the first object which owns its own data.
150
150
*/
151
+
151
152
while (PyArray_Check (obj ) && (PyObject * )arr != obj ) {
152
153
PyArrayObject * obj_arr = (PyArrayObject * )obj ;
153
154
PyObject * tmp ;
154
155
155
156
/* Propagate WARN_ON_WRITE through views. */
156
157
if (PyArray_FLAGS (obj_arr ) & NPY_ARRAY_WARN_ON_WRITE ) {
157
158
PyArray_ENABLEFLAGS (arr , NPY_ARRAY_WARN_ON_WRITE );
158
- }
159
+ }
159
160
160
161
/* If this array owns its own data, stop collapsing */
161
162
if (PyArray_CHKFLAGS (obj_arr , NPY_ARRAY_OWNDATA )) {
162
163
break ;
163
- }
164
- /* If there's no base, stop collapsing */
164
+ }
165
+
165
166
tmp = PyArray_BASE (obj_arr );
167
+ /* If there's no base, stop collapsing */
166
168
if (tmp == NULL ) {
167
169
break ;
168
170
}
171
+ /* Stop the collapse for array sub-classes if new base
172
+ * would not be of the same type.
173
+ */
174
+ if (!(PyArray_CheckExact (arr )) & (Py_TYPE (tmp ) != Py_TYPE (arr ))) {
175
+ break ;
176
+ }
177
+
169
178
170
179
Py_INCREF (tmp );
171
180
Py_DECREF (obj );
0 commit comments