@@ -155,9 +155,9 @@ AK_ResolveDTypeIter(PyObject *dtypes)
155
155
return resolved ;
156
156
}
157
157
158
- // Perform a deepcopy on an array, using an optional memo dictionary, and specialized to depend on immutable arrays.
158
+ // Perform a deepcopy on an array, using an optional memo dictionary, and specialized to depend on immutable arrays. This depends on the module object to get the deepcopy method.
159
159
PyObject *
160
- AK_ArrayDeepCopy (PyArrayObject * array , PyObject * memo )
160
+ AK_ArrayDeepCopy (PyObject * m , PyArrayObject * array , PyObject * memo )
161
161
{
162
162
PyObject * id = PyLong_FromVoidPtr ((PyObject * )array );
163
163
if (!id ) {
@@ -181,12 +181,7 @@ AK_ArrayDeepCopy(PyArrayObject *array, PyObject *memo)
181
181
PyArray_Descr * dtype = PyArray_DESCR (array ); // borrowed ref
182
182
183
183
if (PyDataType_ISOBJECT (dtype )) {
184
- PyObject * copy = PyImport_ImportModule ("copy" );
185
- if (!copy ) {
186
- goto error ;
187
- }
188
- PyObject * deepcopy = PyObject_GetAttrString (copy , "deepcopy" );
189
- Py_DECREF (copy );
184
+ PyObject * deepcopy = PyObject_GetAttrString (m , "deepcopy" );
190
185
if (!deepcopy ) {
191
186
goto error ;
192
187
}
@@ -333,7 +328,7 @@ static char *array_deepcopy_kwarg_names[] = {
333
328
334
329
// Specialized array deepcopy that stores immutable arrays in an optional memo dict that can be provided with kwargs.
335
330
static PyObject *
336
- array_deepcopy (PyObject * Py_UNUSED ( m ) , PyObject * args , PyObject * kwargs )
331
+ array_deepcopy (PyObject * m , PyObject * args , PyObject * kwargs )
337
332
{
338
333
PyObject * array ;
339
334
PyObject * memo = NULL ;
@@ -344,7 +339,7 @@ array_deepcopy(PyObject *Py_UNUSED(m), PyObject *args, PyObject *kwargs)
344
339
return NULL ;
345
340
}
346
341
AK_CHECK_NUMPY_ARRAY (array );
347
- return AK_ArrayDeepCopy ((PyArrayObject * )array , memo );
342
+ return AK_ArrayDeepCopy (m , (PyArrayObject * )array , memo );
348
343
}
349
344
350
345
//------------------------------------------------------------------------------
@@ -782,10 +777,10 @@ static PyMethodDef arraykit_methods[] = {
782
777
};
783
778
784
779
static struct PyModuleDef arraykit_module = {
785
- PyModuleDef_HEAD_INIT ,
786
- .m_name = "_arraykit" ,
787
- .m_doc = NULL ,
788
- .m_size = -1 ,
780
+ PyModuleDef_HEAD_INIT ,
781
+ .m_name = "_arraykit" ,
782
+ .m_doc = NULL ,
783
+ .m_size = -1 ,
789
784
.m_methods = arraykit_methods ,
790
785
};
791
786
@@ -794,11 +789,26 @@ PyInit__arraykit(void)
794
789
{
795
790
import_array ();
796
791
PyObject * m = PyModule_Create (& arraykit_module );
792
+
793
+ PyObject * copy = PyImport_ImportModule ("copy" );
794
+ if (!copy ) {
795
+ Py_XDECREF (m );
796
+ return NULL ;
797
+ }
798
+ PyObject * deepcopy = PyObject_GetAttrString (copy , "deepcopy" );
799
+ Py_DECREF (copy );
800
+ if (!deepcopy ) {
801
+ Py_XDECREF (m );
802
+ return NULL ;
803
+ }
804
+
797
805
if (!m ||
798
806
PyModule_AddStringConstant (m , "__version__" , Py_STRINGIFY (AK_VERSION )) ||
799
807
PyType_Ready (& ArrayGOType ) ||
800
- PyModule_AddObject (m , "ArrayGO" , (PyObject * ) & ArrayGOType ))
808
+ PyModule_AddObject (m , "ArrayGO" , (PyObject * ) & ArrayGOType ) ||
809
+ PyModule_AddObject (m , "deepcopy" , deepcopy ))
801
810
{
811
+ Py_DECREF (deepcopy );
802
812
Py_XDECREF (m );
803
813
return NULL ;
804
814
}
0 commit comments