Skip to content

Commit f739ca6

Browse files
authored
Merge pull request #53 from InvestmentSystems/52/array-deepcopy-module-import
deepcopy stored on module for AK_ArrayDeepCopy
2 parents 33bb90b + 1445a88 commit f739ca6

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

src/_arraykit.c

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
159159
PyObject*
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.
335330
static 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

784779
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,
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
}

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)