Skip to content

Commit b51f1a5

Browse files
committed
deepcopy stored on module for AK_ArrayDeepCopy
1 parent 1c86934 commit b51f1a5

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/_arraykit.c

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ AK_ResolveDTypeIter(PyObject *dtypes)
150150
return resolved;
151151
}
152152

153-
// Perform a deepcopy on an array, using an optional memo dictionary, and specialized to depend on immutable arrays.
153+
// 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.
154154
PyObject*
155-
AK_ArrayDeepCopy(PyArrayObject *array, PyObject *memo)
155+
AK_ArrayDeepCopy(PyObject* m, PyArrayObject *array, PyObject *memo)
156156
{
157157
PyObject *id = PyLong_FromVoidPtr((PyObject*)array);
158158
if (!id) {
@@ -176,12 +176,7 @@ AK_ArrayDeepCopy(PyArrayObject *array, PyObject *memo)
176176
PyArray_Descr *dtype = PyArray_DESCR(array); // borrowed ref
177177

178178
if (PyDataType_ISOBJECT(dtype)) {
179-
PyObject *copy = PyImport_ImportModule("copy");
180-
if (!copy) {
181-
goto error;
182-
}
183-
PyObject *deepcopy = PyObject_GetAttrString(copy, "deepcopy");
184-
Py_DECREF(copy);
179+
PyObject *deepcopy = PyObject_GetAttrString(m, "deepcopy");
185180
if (!deepcopy) {
186181
goto error;
187182
}
@@ -327,7 +322,7 @@ static char *array_deepcopy_kwarg_names[] = {
327322

328323
// Specialized array deepcopy that stores immutable arrays in an optional memo dict that can be provided with kwargs.
329324
static PyObject *
330-
array_deepcopy(PyObject *Py_UNUSED(m), PyObject *args, PyObject *kwargs)
325+
array_deepcopy(PyObject *m, PyObject *args, PyObject *kwargs)
331326
{
332327
PyObject *array;
333328
PyObject *memo = NULL;
@@ -338,7 +333,7 @@ array_deepcopy(PyObject *Py_UNUSED(m), PyObject *args, PyObject *kwargs)
338333
return NULL;
339334
}
340335
AK_CHECK_NUMPY_ARRAY(array);
341-
return AK_ArrayDeepCopy((PyArrayObject*)array, memo);
336+
return AK_ArrayDeepCopy(m, (PyArrayObject*)array, memo);
342337
}
343338

344339
//------------------------------------------------------------------------------
@@ -784,11 +779,26 @@ PyInit__arraykit(void)
784779
{
785780
import_array();
786781
PyObject *m = PyModule_Create(&arraykit_module);
782+
783+
PyObject *copy = PyImport_ImportModule("copy");
784+
if (!copy) {
785+
Py_DECREF(m);
786+
return NULL;
787+
}
788+
PyObject *deepcopy = PyObject_GetAttrString(copy, "deepcopy");
789+
Py_DECREF(copy);
790+
if (!deepcopy) {
791+
Py_DECREF(m);
792+
return NULL;
793+
}
794+
787795
if (!m ||
788796
PyModule_AddStringConstant(m, "__version__", Py_STRINGIFY(AK_VERSION)) ||
789797
PyType_Ready(&ArrayGOType) ||
790-
PyModule_AddObject(m, "ArrayGO", (PyObject *) &ArrayGOType))
798+
PyModule_AddObject(m, "ArrayGO", (PyObject *) &ArrayGOType) ||
799+
PyModule_AddObject(m, "deepcopy", deepcopy))
791800
{
801+
Py_DECREF(deepcopy);
792802
Py_XDECREF(m);
793803
return NULL;
794804
}

tasks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ def test(context):
3636

3737

3838
@invoke.task(build)
39-
def performance(context):
40-
context.run(f'{sys.executable} -m performance', echo=True, pty=True)
39+
def performance(context, names=''):
40+
context.run(f'{sys.executable} -m performance {"--names" if names else ""} {names}', echo=True, pty=True)
4141

4242

4343
@invoke.task

0 commit comments

Comments
 (0)