13
13
#include "casts.h"
14
14
#include "dtype.h"
15
15
16
-
17
-
18
16
/*
19
17
* Internal helper to create new instances.
20
18
*/
@@ -27,9 +25,8 @@ new_MPFDType_instance(mpfr_prec_t precision)
27
25
* set in that case.
28
26
*/
29
27
if (precision < MPFR_PREC_MIN || precision > MPFR_PREC_MAX ) {
30
- PyErr_Format (PyExc_ValueError ,
31
- "precision must be between %d and %d." ,
32
- MPFR_PREC_MIN , MPFR_PREC_MAX );
28
+ PyErr_Format (PyExc_ValueError , "precision must be between %d and %d." , MPFR_PREC_MIN ,
29
+ MPFR_PREC_MAX );
33
30
return NULL ;
34
31
}
35
32
@@ -43,7 +40,7 @@ new_MPFDType_instance(mpfr_prec_t precision)
43
40
size_t size = mpfr_custom_get_size (precision );
44
41
if (size > NPY_MAX_INT - sizeof (mpf_field )) {
45
42
PyErr_SetString (PyExc_TypeError ,
46
- "storage of single float would be too large for precision." );
43
+ "storage of single float would be too large for precision." );
47
44
}
48
45
new -> base .elsize = sizeof (mpf_storage ) + size ;
49
46
new -> base .alignment = _Alignof(mpf_field );
@@ -52,15 +49,13 @@ new_MPFDType_instance(mpfr_prec_t precision)
52
49
return new ;
53
50
}
54
51
55
-
56
52
static MPFDTypeObject *
57
53
ensure_canonical (MPFDTypeObject * self )
58
54
{
59
55
Py_INCREF (self );
60
56
return self ;
61
57
}
62
58
63
-
64
59
static MPFDTypeObject *
65
60
common_instance (MPFDTypeObject * dtype1 , MPFDTypeObject * dtype2 )
66
61
{
@@ -74,17 +69,15 @@ common_instance(MPFDTypeObject *dtype1, MPFDTypeObject *dtype2)
74
69
}
75
70
}
76
71
77
-
78
72
static PyArray_DTypeMeta *
79
73
common_dtype (PyArray_DTypeMeta * cls , PyArray_DTypeMeta * other )
80
74
{
81
75
/*
82
76
* Typenum is useful for NumPy, but there it can still be convenient.
83
77
* (New-style user dtypes will probably get -1 as type number...)
84
78
*/
85
- if (other -> type_num >= 0
86
- && PyTypeNum_ISNUMBER (other -> type_num )
87
- && !PyTypeNum_ISCOMPLEX (other -> type_num )) {
79
+ if (other -> type_num >= 0 && PyTypeNum_ISNUMBER (other -> type_num ) &&
80
+ !PyTypeNum_ISCOMPLEX (other -> type_num )) {
88
81
/*
89
82
* A (simple) builtin numeric type (not complex) promotes to fixed
90
83
* precision.
@@ -96,18 +89,15 @@ common_dtype(PyArray_DTypeMeta *cls, PyArray_DTypeMeta *other)
96
89
return (PyArray_DTypeMeta * )Py_NotImplemented ;
97
90
}
98
91
99
-
100
92
/*
101
93
* Functions dealing with scalar logic
102
94
*/
103
95
104
96
static PyArray_Descr *
105
- mpf_discover_descriptor_from_pyobject (
106
- PyArray_DTypeMeta * NPY_UNUSED (cls ), PyObject * obj )
97
+ mpf_discover_descriptor_from_pyobject (PyArray_DTypeMeta * NPY_UNUSED (cls ), PyObject * obj )
107
98
{
108
99
if (Py_TYPE (obj ) != & MPFloat_Type ) {
109
- PyErr_SetString (PyExc_TypeError ,
110
- "Can only store MPFloat in a MPFDType array." );
100
+ PyErr_SetString (PyExc_TypeError , "Can only store MPFloat in a MPFDType array." );
111
101
return NULL ;
112
102
}
113
103
mpfr_prec_t prec = get_prec_from_object (obj );
@@ -117,7 +107,6 @@ mpf_discover_descriptor_from_pyobject(
117
107
return (PyArray_Descr * )new_MPFDType_instance (prec );
118
108
}
119
109
120
-
121
110
static int
122
111
mpf_setitem (MPFDTypeObject * descr , PyObject * obj , char * dataptr )
123
112
{
@@ -167,18 +156,14 @@ mpf_getitem(MPFDTypeObject *descr, char *dataptr)
167
156
return (PyObject * )new ;
168
157
}
169
158
170
-
171
159
static PyType_Slot MPFDType_Slots [] = {
172
- {NPY_DT_ensure_canonical , & ensure_canonical },
173
- {NPY_DT_common_instance , & common_instance },
174
- {NPY_DT_common_dtype , & common_dtype },
175
- {NPY_DT_discover_descr_from_pyobject ,
176
- & mpf_discover_descriptor_from_pyobject },
177
- {NPY_DT_setitem , & mpf_setitem },
178
- {NPY_DT_getitem , & mpf_getitem },
179
- {0 , NULL }
180
- };
181
-
160
+ {NPY_DT_ensure_canonical , & ensure_canonical },
161
+ {NPY_DT_common_instance , & common_instance },
162
+ {NPY_DT_common_dtype , & common_dtype },
163
+ {NPY_DT_discover_descr_from_pyobject , & mpf_discover_descriptor_from_pyobject },
164
+ {NPY_DT_setitem , & mpf_setitem },
165
+ {NPY_DT_getitem , & mpf_getitem },
166
+ {0 , NULL }};
182
167
183
168
/*
184
169
* The following defines everything type object related (i.e. not NumPy
@@ -195,59 +180,49 @@ MPFDType_new(PyTypeObject *NPY_UNUSED(cls), PyObject *args, PyObject *kwds)
195
180
196
181
Py_ssize_t precision ;
197
182
198
- if (!PyArg_ParseTupleAndKeywords (
199
- args , kwds , "n:MPFDType" , kwargs_strs , & precision )) {
183
+ if (!PyArg_ParseTupleAndKeywords (args , kwds , "n:MPFDType" , kwargs_strs , & precision )) {
200
184
return NULL ;
201
185
}
202
186
203
187
return (PyObject * )new_MPFDType_instance (precision );
204
188
}
205
189
206
-
207
190
static PyObject *
208
191
MPFDType_repr (MPFDTypeObject * self )
209
192
{
210
- PyObject * res = PyUnicode_FromFormat (
211
- "MPFDType(%ld)" , (long )self -> precision );
193
+ PyObject * res = PyUnicode_FromFormat ("MPFDType(%ld)" , (long )self -> precision );
212
194
return res ;
213
195
}
214
196
215
-
216
197
PyObject *
217
198
MPFDType_get_prec (MPFDTypeObject * self )
218
199
{
219
200
return PyLong_FromLong (self -> precision );
220
201
}
221
202
222
-
223
203
NPY_NO_EXPORT PyGetSetDef mpfdtype_getsetlist [] = {
224
- {"prec" ,
225
- (getter )MPFDType_get_prec ,
226
- NULL ,
227
- NULL , NULL },
228
- {NULL , NULL , NULL , NULL , NULL }, /* Sentinel */
204
+ {"prec" , (getter )MPFDType_get_prec , NULL , NULL , NULL },
205
+ {NULL , NULL , NULL , NULL , NULL }, /* Sentinel */
229
206
};
230
207
231
-
232
208
/*
233
209
* This is the basic things that you need to create a Python Type/Class in C.
234
210
* However, there is a slight difference here because we create a
235
211
* PyArray_DTypeMeta, which is a larger struct than a typical type.
236
212
* (This should get a bit nicer eventually with Python >3.11.)
237
213
*/
238
- PyArray_DTypeMeta MPFDType = {{{
239
- PyVarObject_HEAD_INIT ( NULL , 0 )
240
- .tp_name = "MPFDType.MPFDType" ,
241
- .tp_basicsize = sizeof (MPFDTypeObject ),
242
- .tp_new = MPFDType_new ,
243
- .tp_repr = (reprfunc )MPFDType_repr ,
244
- .tp_str = (reprfunc )MPFDType_repr ,
245
- .tp_getset = mpfdtype_getsetlist ,
246
- }},
247
- /* rest, filled in during DTypeMeta initialization */
214
+ PyArray_DTypeMeta MPFDType = {
215
+ {{
216
+ PyVarObject_HEAD_INIT ( NULL , 0 ) .tp_name = "MPFDType.MPFDType" ,
217
+ .tp_basicsize = sizeof (MPFDTypeObject ),
218
+ .tp_new = MPFDType_new ,
219
+ .tp_repr = (reprfunc )MPFDType_repr ,
220
+ .tp_str = (reprfunc )MPFDType_repr ,
221
+ .tp_getset = mpfdtype_getsetlist ,
222
+ }},
223
+ /* rest, filled in during DTypeMeta initialization */
248
224
};
249
225
250
-
251
226
int
252
227
init_mpf_dtype (void )
253
228
{
@@ -258,7 +233,7 @@ init_mpf_dtype(void)
258
233
PyArrayMethod_Spec * * casts = init_casts ();
259
234
260
235
PyArrayDTypeMeta_Spec MPFDType_DTypeSpec = {
261
- .flags = NPY_DT_PARAMETRIC ,
236
+ .flags = NPY_DT_PARAMETRIC | NPY_DT_NUMERIC ,
262
237
.casts = casts ,
263
238
.typeobj = & MPFloat_Type ,
264
239
.slots = MPFDType_Slots ,
@@ -271,8 +246,7 @@ init_mpf_dtype(void)
271
246
return -1 ;
272
247
}
273
248
274
- if (PyArrayInitDTypeMeta_FromSpec (
275
- & MPFDType , & MPFDType_DTypeSpec ) < 0 ) {
249
+ if (PyArrayInitDTypeMeta_FromSpec (& MPFDType , & MPFDType_DTypeSpec ) < 0 ) {
276
250
free_casts ();
277
251
return -1 ;
278
252
}
0 commit comments