@@ -155,9 +155,9 @@ AK_ResolveDTypeIter(PyObject *dtypes)
155155 return resolved ;
156156}
157157
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.
159159PyObject *
160- AK_ArrayDeepCopy (PyArrayObject * array , PyObject * memo )
160+ AK_ArrayDeepCopy (PyObject * m , PyArrayObject * array , PyObject * memo )
161161{
162162 PyObject * id = PyLong_FromVoidPtr ((PyObject * )array );
163163 if (!id ) {
@@ -181,12 +181,7 @@ AK_ArrayDeepCopy(PyArrayObject *array, PyObject *memo)
181181 PyArray_Descr * dtype = PyArray_DESCR (array ); // borrowed ref
182182
183183 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" );
190185 if (!deepcopy ) {
191186 goto error ;
192187 }
@@ -333,7 +328,7 @@ static char *array_deepcopy_kwarg_names[] = {
333328
334329// Specialized array deepcopy that stores immutable arrays in an optional memo dict that can be provided with kwargs.
335330static PyObject *
336- array_deepcopy (PyObject * Py_UNUSED ( m ) , PyObject * args , PyObject * kwargs )
331+ array_deepcopy (PyObject * m , PyObject * args , PyObject * kwargs )
337332{
338333 PyObject * array ;
339334 PyObject * memo = NULL ;
@@ -344,7 +339,7 @@ array_deepcopy(PyObject *Py_UNUSED(m), PyObject *args, PyObject *kwargs)
344339 return NULL ;
345340 }
346341 AK_CHECK_NUMPY_ARRAY (array );
347- return AK_ArrayDeepCopy ((PyArrayObject * )array , memo );
342+ return AK_ArrayDeepCopy (m , (PyArrayObject * )array , memo );
348343}
349344
350345//------------------------------------------------------------------------------
@@ -782,10 +777,10 @@ static PyMethodDef arraykit_methods[] = {
782777};
783778
784779static 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 ,
789784 .m_methods = arraykit_methods ,
790785};
791786
@@ -794,11 +789,26 @@ PyInit__arraykit(void)
794789{
795790 import_array ();
796791 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+
797805 if (!m ||
798806 PyModule_AddStringConstant (m , "__version__" , Py_STRINGIFY (AK_VERSION )) ||
799807 PyType_Ready (& ArrayGOType ) ||
800- PyModule_AddObject (m , "ArrayGO" , (PyObject * ) & ArrayGOType ))
808+ PyModule_AddObject (m , "ArrayGO" , (PyObject * ) & ArrayGOType ) ||
809+ PyModule_AddObject (m , "deepcopy" , deepcopy ))
801810 {
811+ Py_DECREF (deepcopy );
802812 Py_XDECREF (m );
803813 return NULL ;
804814 }
0 commit comments