MCPcopy Create free account
hub / github.com/numpy/numpy / array_reduce_ex_picklebuffer

Function array_reduce_ex_picklebuffer

numpy/core/src/multiarray/methods.c:1915–1989  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1913}
1914
1915static PyObject *
1916array_reduce_ex_picklebuffer(PyArrayObject *self, int protocol)
1917{
1918 PyObject *numeric_mod = NULL, *from_buffer_func = NULL;
1919 PyObject *pickle_module = NULL, *picklebuf_class = NULL;
1920 PyObject *picklebuf_args = NULL;
1921 PyObject *buffer = NULL, *transposed_array = NULL;
1922 PyArray_Descr *descr = NULL;
1923 char order;
1924
1925 descr = PyArray_DESCR(self);
1926
1927 /* we expect protocol 5 to be available in Python 3.8 */
1928 pickle_module = PyImport_ImportModule("pickle");
1929 if (pickle_module == NULL){
1930 return NULL;
1931 }
1932 picklebuf_class = PyObject_GetAttrString(pickle_module, "PickleBuffer");
1933 Py_DECREF(pickle_module);
1934 if (picklebuf_class == NULL) {
1935 return NULL;
1936 }
1937
1938 /* Construct a PickleBuffer of the array */
1939
1940 if (!PyArray_IS_C_CONTIGUOUS((PyArrayObject*) self) &&
1941 PyArray_IS_F_CONTIGUOUS((PyArrayObject*) self)) {
1942 /* if the array if Fortran-contiguous and not C-contiguous,
1943 * the PickleBuffer instance will hold a view on the transpose
1944 * of the initial array, that is C-contiguous. */
1945 order = 'F';
1946 transposed_array = PyArray_Transpose((PyArrayObject*)self, NULL);
1947 picklebuf_args = Py_BuildValue("(N)", transposed_array);
1948 }
1949 else {
1950 order = 'C';
1951 picklebuf_args = Py_BuildValue("(O)", self);
1952 }
1953 if (picklebuf_args == NULL) {
1954 Py_DECREF(picklebuf_class);
1955 return NULL;
1956 }
1957
1958 buffer = PyObject_CallObject(picklebuf_class, picklebuf_args);
1959 Py_DECREF(picklebuf_class);
1960 Py_DECREF(picklebuf_args);
1961 if (buffer == NULL) {
1962 /* Some arrays may refuse to export a buffer, in which case
1963 * just fall back on regular __reduce_ex__ implementation
1964 * (gh-12745).
1965 */
1966 PyErr_Clear();
1967 return array_reduce_ex_regular(self, protocol);
1968 }
1969
1970 /* Get the _frombuffer() function for reconstruction */
1971
1972 numeric_mod = PyImport_ImportModule("numpy.core.numeric");

Callers 1

array_reduce_exFunction · 0.85

Calls 3

PyArray_DESCRFunction · 0.85
PyArray_TransposeFunction · 0.85
array_reduce_ex_regularFunction · 0.85

Tested by

no test coverage detected