1
#define NPY_NO_DEPRECATED_API NPY_API_VERSION
2
#define _MULTIARRAYMODULE
3

4
#include <Python.h>
5
#include <numpy/arrayobject.h>
6
#include "npy_pycompat.h"
7
#include "npy_import.h"
8
#include "strfuncs.h"
9

10
static PyObject *PyArray_StrFunction = NULL;
11
static PyObject *PyArray_ReprFunction = NULL;
12

13

14
static void
15 0
npy_PyErr_SetStringChained(PyObject *type, const char *message)
16
{
17
    PyObject *exc, *val, *tb;
18

19 0
    PyErr_Fetch(&exc, &val, &tb);
20 0
    PyErr_SetString(type, message);
21 0
    npy_PyErr_ChainExceptionsCause(exc, val, tb);
22
}
23

24

25
/*NUMPY_API
26
 * Set the array print function to be a Python function.
27
 */
28
NPY_NO_EXPORT void
29 1
PyArray_SetStringFunction(PyObject *op, int repr)
30
{
31 1
    if (repr) {
32
        /* Dispose of previous callback */
33 1
        Py_XDECREF(PyArray_ReprFunction);
34
        /* Add a reference to new callback */
35 1
        Py_XINCREF(op);
36
        /* Remember new callback */
37 1
        PyArray_ReprFunction = op;
38
    }
39
    else {
40
        /* Dispose of previous callback */
41 1
        Py_XDECREF(PyArray_StrFunction);
42
        /* Add a reference to new callback */
43 1
        Py_XINCREF(op);
44
        /* Remember new callback */
45 1
        PyArray_StrFunction = op;
46
    }
47
}
48

49

50
NPY_NO_EXPORT PyObject *
51 1
array_repr(PyArrayObject *self)
52
{
53
    static PyObject *repr = NULL;
54

55 1
    if (PyArray_ReprFunction != NULL) {
56 1
        return PyObject_CallFunctionObjArgs(PyArray_ReprFunction, self, NULL);
57
    }
58

59
    /*
60
     * We need to do a delayed import here as initialization on module load
61
     * leads to circular import problems.
62
     */
63 1
    npy_cache_import("numpy.core.arrayprint", "_default_array_repr", &repr);
64 1
    if (repr == NULL) {
65 0
        npy_PyErr_SetStringChained(PyExc_RuntimeError,
66
                "Unable to configure default ndarray.__repr__");
67 0
        return NULL;
68
    }
69 1
    return PyObject_CallFunctionObjArgs(repr, self, NULL);
70
}
71

72

73
NPY_NO_EXPORT PyObject *
74 1
array_str(PyArrayObject *self)
75
{
76
    static PyObject *str = NULL;
77

78 1
    if (PyArray_StrFunction != NULL) {
79 1
        return PyObject_CallFunctionObjArgs(PyArray_StrFunction, self, NULL);
80
    }
81

82
    /*
83
     * We need to do a delayed import here as initialization on module load leads
84
     * to circular import problems.
85
     */
86 1
    npy_cache_import("numpy.core.arrayprint", "_default_array_str", &str);
87 1
    if (str == NULL) {
88 0
        npy_PyErr_SetStringChained(PyExc_RuntimeError,
89
                "Unable to configure default ndarray.__str__");
90 0
        return NULL;
91
    }
92 1
    return PyObject_CallFunctionObjArgs(str, self, NULL);
93
}
94

95

96
NPY_NO_EXPORT PyObject *
97 1
array_format(PyArrayObject *self, PyObject *args)
98
{
99
    PyObject *format;
100 1
    if (!PyArg_ParseTuple(args, "O:__format__", &format))
101
        return NULL;
102

103
    /* 0d arrays - forward to the scalar type */
104 1
    if (PyArray_NDIM(self) == 0) {
105 1
        PyObject *item = PyArray_ToScalar(PyArray_DATA(self), self);
106
        PyObject *res;
107

108 1
        if (item == NULL) {
109
            return NULL;
110
        }
111 1
        res = PyObject_Format(item, format);
112 1
        Py_DECREF(item);
113
        return res;
114
    }
115
    /* Everything else - use the builtin */
116
    else {
117 1
        return PyObject_CallMethod(
118
            (PyObject *)&PyBaseObject_Type, "__format__", "OO",
119
            (PyObject *)self, format
120
        );
121
    }
122
}

Read our documentation on viewing source code .

Loading