summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorEric Wieser <wieser.eric@gmail.com>2019-05-31 09:09:44 -0700
committerEric Wieser <wieser.eric@gmail.com>2019-06-01 13:48:52 -0700
commit87eeb8db860531134b4beac4cb727673098018d9 (patch)
tree89407610c862e5112b2aee383d8379e4ba4aa4d1 /numpy
parent392866d1c8dce0abb8cb327a42f8e134f5d2a05e (diff)
downloadnumpy-87eeb8db860531134b4beac4cb727673098018d9.tar.gz
BUG: Move ndarray.dump to python and make it close the file it opens
As a side-effect, this makes it support kwargs.
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/_methods.py12
-rw-r--r--numpy/core/src/multiarray/methods.c77
2 files changed, 34 insertions, 55 deletions
diff --git a/numpy/core/_methods.py b/numpy/core/_methods.py
index ba6f7d111..269e509b8 100644
--- a/numpy/core/_methods.py
+++ b/numpy/core/_methods.py
@@ -13,6 +13,7 @@ from numpy.core._asarray import asanyarray
from numpy.core import numerictypes as nt
from numpy.core import _exceptions
from numpy._globals import _NoValue
+from numpy.compat import pickle, os_fspath, contextlib_nullcontext
# save those O(100) nanoseconds!
umr_maximum = um.maximum.reduce
@@ -230,3 +231,14 @@ def _ptp(a, axis=None, out=None, keepdims=False):
umr_minimum(a, axis, None, None, keepdims),
out
)
+
+def _dump(self, file, protocol=2):
+ if hasattr(file, 'write'):
+ ctx = contextlib_nullcontext(file)
+ else:
+ ctx = open(os_fspath(file), "wb")
+ with ctx as f:
+ pickle.dump(self, f, protocol=protocol)
+
+def _dumps(self, protocol=2):
+ return pickle.dumps(self, protocol=protocol)
diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c
index b843c7983..3d7e035ac 100644
--- a/numpy/core/src/multiarray/methods.c
+++ b/numpy/core/src/multiarray/methods.c
@@ -2098,37 +2098,22 @@ array_setstate(PyArrayObject *self, PyObject *args)
NPY_NO_EXPORT int
PyArray_Dump(PyObject *self, PyObject *file, int protocol)
{
- PyObject *cpick = NULL;
+ static PyObject *method = NULL;
PyObject *ret;
- if (protocol < 0) {
- protocol = 2;
- }
-
-#if defined(NPY_PY3K)
- cpick = PyImport_ImportModule("pickle");
-#else
- cpick = PyImport_ImportModule("cPickle");
-#endif
- if (cpick == NULL) {
+ npy_cache_import("numpy.core._methods", "_dump", &method);
+ if (method == NULL) {
return -1;
}
- if (PyBytes_Check(file) || PyUnicode_Check(file)) {
- file = npy_PyFile_OpenFile(file, "wb");
- if (file == NULL) {
- Py_DECREF(cpick);
- return -1;
- }
+ if (protocol < 0) {
+ ret = PyObject_CallFunction(method, "OO", self, file);
}
else {
- Py_INCREF(file);
+ ret = PyObject_CallFunction(method, "OOi", self, file, protocol);
}
- ret = PyObject_CallMethod(cpick, "dump", "OOi", self, file, protocol);
- Py_XDECREF(ret);
- Py_DECREF(file);
- Py_DECREF(cpick);
- if (PyErr_Occurred()) {
+ if (ret == NULL) {
return -1;
}
+ Py_DECREF(ret);
return 0;
}
@@ -2136,49 +2121,31 @@ PyArray_Dump(PyObject *self, PyObject *file, int protocol)
NPY_NO_EXPORT PyObject *
PyArray_Dumps(PyObject *self, int protocol)
{
- PyObject *cpick = NULL;
- PyObject *ret;
+ static PyObject *method = NULL;
+ npy_cache_import("numpy.core._methods", "_dumps", &method);
+ if (method == NULL) {
+ return NULL;
+ }
if (protocol < 0) {
- protocol = 2;
+ return PyObject_CallFunction(method, "O", self);
}
-#if defined(NPY_PY3K)
- cpick = PyImport_ImportModule("pickle");
-#else
- cpick = PyImport_ImportModule("cPickle");
-#endif
- if (cpick == NULL) {
- return NULL;
+ else {
+ return PyObject_CallFunction(method, "Oi", self, protocol);
}
- ret = PyObject_CallMethod(cpick, "dumps", "Oi", self, protocol);
- Py_DECREF(cpick);
- return ret;
}
static PyObject *
-array_dump(PyArrayObject *self, PyObject *args)
+array_dump(PyArrayObject *self, PyObject *args, PyObject *kwds)
{
- PyObject *file = NULL;
- int ret;
-
- if (!PyArg_ParseTuple(args, "O:dump", &file)) {
- return NULL;
- }
- ret = PyArray_Dump((PyObject *)self, file, 2);
- if (ret < 0) {
- return NULL;
- }
- Py_RETURN_NONE;
+ NPY_FORWARD_NDARRAY_METHOD("_dump");
}
static PyObject *
-array_dumps(PyArrayObject *self, PyObject *args)
+array_dumps(PyArrayObject *self, PyObject *args, PyObject *kwds)
{
- if (!PyArg_ParseTuple(args, "")) {
- return NULL;
- }
- return PyArray_Dumps((PyObject *)self, 2);
+ NPY_FORWARD_NDARRAY_METHOD("_dumps");
}
@@ -2753,10 +2720,10 @@ NPY_NO_EXPORT PyMethodDef array_methods[] = {
METH_VARARGS, NULL},
{"dumps",
(PyCFunction) array_dumps,
- METH_VARARGS, NULL},
+ METH_VARARGS | METH_KEYWORDS, NULL},
{"dump",
(PyCFunction) array_dump,
- METH_VARARGS, NULL},
+ METH_VARARGS | METH_KEYWORDS, NULL},
{"__complex__",
(PyCFunction) array_complex,