1
/*
2
 * Provides namedtuples for numpy.core.multiarray.typeinfo
3
 * Unfortunately, we need two different types to cover the cases where min/max
4
 * do and do not appear in the tuple.
5
 */
6
#include "typeinfo.h"
7

8
#if (defined(PYPY_VERSION_NUM) && (PYPY_VERSION_NUM <= 0x07030000))
9
/* PyPy issue 3160 */
10
#include <structseq.h>
11
#endif
12

13
#define NPY_NO_DEPRECATED_API NPY_API_VERSION
14
#define _MULTIARRAYMODULE
15
#include "npy_pycompat.h"
16

17

18
static PyTypeObject PyArray_typeinfoType;
19
static PyTypeObject PyArray_typeinforangedType;
20

21
static PyStructSequence_Field typeinfo_fields[] = {
22
    {"char",      "The character used to represent the type"},
23
    {"num",       "The numeric id assigned to the type"},
24
    {"bits",      "The number of bits in the type"},
25
    {"alignment", "The alignment of the type in bytes"},
26
    {"type",      "The python type object this info is about"},
27
    {NULL, NULL,}
28
};
29

30
static PyStructSequence_Field typeinforanged_fields[] = {
31
    {"char",      "The character used to represent the type"},
32
    {"num",       "The numeric id assigned to the type"},
33
    {"bits",      "The number of bits in the type"},
34
    {"alignment", "The alignment of the type in bytes"},
35
    {"max",       "The maximum value of this type"},
36
    {"min",       "The minimum value of this type"},
37
    {"type",      "The python type object this info is about"},
38
    {NULL, NULL,}
39
};
40

41
static PyStructSequence_Desc typeinfo_desc = {
42
    "numpy.core.multiarray.typeinfo",         /* name          */
43
    "Information about a scalar numpy type",  /* doc           */
44
    typeinfo_fields,                          /* fields        */
45
    5,                                        /* n_in_sequence */
46
};
47

48
static PyStructSequence_Desc typeinforanged_desc = {
49
    "numpy.core.multiarray.typeinforanged",                /* name          */
50
    "Information about a scalar numpy type with a range",  /* doc           */
51
    typeinforanged_fields,                                 /* fields        */
52
    7,                                                     /* n_in_sequence */
53
};
54

55
NPY_NO_EXPORT PyObject *
56 1
PyArray_typeinfo(
57
    char typechar, int typenum, int nbits, int align,
58
    PyTypeObject *type_obj)
59
{
60 1
    PyObject *entry = PyStructSequence_New(&PyArray_typeinfoType);
61 1
    if (entry == NULL)
62
        return NULL;
63 1
    PyStructSequence_SET_ITEM(entry, 0, Py_BuildValue("C", typechar));
64 1
    PyStructSequence_SET_ITEM(entry, 1, Py_BuildValue("i", typenum));
65 1
    PyStructSequence_SET_ITEM(entry, 2, Py_BuildValue("i", nbits));
66 1
    PyStructSequence_SET_ITEM(entry, 3, Py_BuildValue("i", align));
67 1
    PyStructSequence_SET_ITEM(entry, 4, Py_BuildValue("O", (PyObject *) type_obj));
68

69 1
    if (PyErr_Occurred()) {
70 0
        Py_DECREF(entry);
71
        return NULL;
72
    }
73

74
    return entry;
75
}
76

77
NPY_NO_EXPORT PyObject *
78 1
PyArray_typeinforanged(
79
    char typechar, int typenum, int nbits, int align,
80
    PyObject *max, PyObject *min, PyTypeObject *type_obj)
81
{
82 1
    PyObject *entry = PyStructSequence_New(&PyArray_typeinforangedType);
83 1
    if (entry == NULL)
84
        return NULL;
85 1
    PyStructSequence_SET_ITEM(entry, 0, Py_BuildValue("C", typechar));
86 1
    PyStructSequence_SET_ITEM(entry, 1, Py_BuildValue("i", typenum));
87 1
    PyStructSequence_SET_ITEM(entry, 2, Py_BuildValue("i", nbits));
88 1
    PyStructSequence_SET_ITEM(entry, 3, Py_BuildValue("i", align));
89 1
    PyStructSequence_SET_ITEM(entry, 4, max);
90 1
    PyStructSequence_SET_ITEM(entry, 5, min);
91 1
    PyStructSequence_SET_ITEM(entry, 6, Py_BuildValue("O", (PyObject *) type_obj));
92

93 1
    if (PyErr_Occurred()) {
94 0
        Py_DECREF(entry);
95
        return NULL;
96
    }
97

98
    return entry;
99
}
100

101
/* Python version needed for older PyPy */
102
#if (defined(PYPY_VERSION_NUM) && (PYPY_VERSION_NUM < 0x07020000))
103
    static int
104
    PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc) {
105
        PyStructSequence_InitType(type, desc);
106
        if (PyErr_Occurred()) {
107
            return -1;
108
        }
109
        return 0;
110
    }
111
#endif
112

113
NPY_NO_EXPORT int
114 1
typeinfo_init_structsequences(PyObject *multiarray_dict)
115
{
116 1
    if (PyStructSequence_InitType2(
117
            &PyArray_typeinfoType, &typeinfo_desc) < 0) {
118
        return -1;
119
    }
120 1
    if (PyStructSequence_InitType2(
121
            &PyArray_typeinforangedType, &typeinforanged_desc) < 0) {
122
        return -1;
123
    }
124 1
    if (PyDict_SetItemString(multiarray_dict,
125
            "typeinfo", (PyObject *)&PyArray_typeinfoType) < 0) {
126
        return -1;
127
    }
128 1
    if (PyDict_SetItemString(multiarray_dict,
129
            "typeinforanged", (PyObject *)&PyArray_typeinforangedType) < 0) {
130
        return -1;
131
    }
132 1
    return 0;
133
}

Read our documentation on viewing source code .

Loading