Skip to content

Commit 09320b2

Browse files
committed
Restrict ourselves to the limited API for 3.11
Also quite a bit of a rewrite to always use PyObject *self and ScalerObject *scaler.
1 parent eeb288c commit 09320b2

1 file changed

Lines changed: 108 additions & 83 deletions

File tree

‎src/scaler.c‎

Lines changed: 108 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,64 @@
11
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION // For PyUFunc_GiveFloatingpointErrors
2+
#define Py_LIMITED_API 0x030B0000
23
#define PY_SSIZE_T_CLEAN
34
#include <Python.h>
4-
#if Py_Version < 0x03120000
5-
#include <structmember.h> // for PyMemberDef, not in Python.h for python <= 3.11
6-
#endif
75
#include <numpy/arrayobject.h>
86
#include <numpy/arrayscalars.h>
97
#include <numpy/ndarrayobject.h>
108
#include <numpy/ufuncobject.h>
119
#include <stddef.h> // for offsetof()
1210

11+
// Once we're at 3.12, move SCALER_TP_FLAGS to its use, and remove this whole block.
12+
#if Py_LIMITED_API + 0 >= 0x030C0000
13+
#define SCALER_TP_FLAGS \
14+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_VECTORCALL | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE
15+
#else // work-arounds for python 3.11 limited API
16+
#include <structmember.h> // for PyMemberDef
17+
#define PyType_GetModuleByDef(type, unused) PyType_GetModule(type) // hence, cannot subclass
18+
#define vectorcallfunc void *
19+
#define Py_TPFLAGS_HAVE_VECTORCALL (1UL << 11)
20+
#define SCALER_TP_FLAGS Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_VECTORCALL | Py_TPFLAGS_HAVE_GC
21+
#ifndef Py_T_DOUBLE
22+
// Most of these are macros whose availability depends on which python we're compiling on,
23+
// but which do not influence support in different versions (tested by compiling on
24+
// 3.11 and running tests on 3.14 with the resulting .so file).
25+
// But more annoyingly, we also need to define our own PyVectorcall_Call;
26+
// Note that what is below is specific to our class; it cannot be used generally!
27+
#define Py_T_DOUBLE T_DOUBLE
28+
#define Py_READONLY READONLY
29+
#define Py_T_PYSSIZET T_PYSSIZET
30+
#define PY_VECTORCALL_ARGUMENTS_OFFSET (_Py_STATIC_CAST(size_t, 1) << (8 * sizeof(size_t) - 1))
31+
#define PyVectorcall_NARGS(nargsf) (Py_ssize_t)(nargsf & ~PY_VECTORCALL_ARGUMENTS_OFFSET)
32+
static PyObject *Scaler_vectorcall(
33+
PyObject *self, PyObject *const *args, size_t len_args, PyObject *kwnames
34+
);
35+
static PyObject *PyVectorcall_Call(PyObject *self, PyObject *tuple, PyObject *dict)
36+
{
37+
if (dict != NULL && PyDict_Size(dict) > 0) {
38+
Py_ssize_t pos = 0;
39+
PyObject *key, *value;
40+
PyDict_Next(dict, &pos, &key, &value);
41+
PyErr_Format(PyExc_TypeError, "scaler() got an unexpected keyword argument %R", key);
42+
return NULL;
43+
}
44+
if (PyTuple_Size(tuple) != 1) {
45+
PyErr_Format(
46+
PyExc_TypeError,
47+
"scaler() takes 1 positional argument but %d were given",
48+
PyTuple_Size(tuple)
49+
);
50+
return NULL;
51+
}
52+
PyObject *args[1];
53+
args[0] = PyTuple_GetItem(tuple, 0);
54+
if (args[0] == NULL) {
55+
return NULL;
56+
}
57+
return Scaler_vectorcall(self, args, 1, NULL);
58+
}
59+
#endif
60+
#endif
61+
1362
PyDoc_STRVAR(scaler_module_doc, "Compiled module providing the inspectable Scaler class.");
1463
PyDoc_STRVAR(
1564
Scaler_doc,
@@ -45,9 +94,7 @@ typedef struct {
4594

4695
// Multiply a contiguous array directly with the factor, using np.multiply's
4796
// loop function.
48-
static inline PyObject *use_contiguous_loop(
49-
ScalerObject *self, PyArrayObject *arr, char *factor_ptr
50-
)
97+
static inline PyObject *use_contiguous_loop(PyObject *self, PyArrayObject *arr, char *factor_ptr)
5198
{
5299
// Get loops from module state.
53100
PyObject *m = PyType_GetModuleByDef(Py_TYPE(self), &scaler_module);
@@ -100,27 +147,27 @@ static inline PyObject *use_contiguous_loop(
100147
}
101148

102149
// Get the factor as a PyFloat, caching it as needed.
103-
static inline PyObject *get_o_factor(ScalerObject *self)
150+
static inline PyObject *get_o_factor(ScalerObject *scaler)
104151
{
105-
if (self->O_factor == NULL) {
106-
self->O_factor = PyFloat_FromDouble(self->factor);
107-
if (self->O_factor == NULL) {
152+
if (scaler->O_factor == NULL) {
153+
scaler->O_factor = PyFloat_FromDouble(scaler->factor);
154+
if (scaler->O_factor == NULL) {
108155
return NULL;
109156
}
110157
}
111-
return self->O_factor;
158+
return scaler->O_factor;
112159
}
113160

114161
// Scale the input with the factor, using shortcuts where possible.
115162
static PyObject *Scaler_vectorcall(
116-
ScalerObject *self, PyObject *const *args, size_t len_args, PyObject *kwnames
163+
PyObject *self, PyObject *const *args, size_t len_args, PyObject *kwnames
117164
)
118165
{
119-
if (kwnames != NULL && PyTuple_GET_SIZE(kwnames) > 0) {
166+
if (kwnames != NULL && PyTuple_Size(kwnames) > 0) {
120167
PyErr_Format(
121168
PyExc_TypeError,
122169
"scaler() got an unexpected keyword argument %R",
123-
PyTuple_GET_ITEM(kwnames, 0)
170+
PyTuple_GetItem(kwnames, 0)
124171
);
125172
return NULL;
126173
}
@@ -132,11 +179,11 @@ static PyObject *Scaler_vectorcall(
132179
);
133180
return NULL;
134181
}
135-
182+
ScalerObject *scaler = (ScalerObject *)self;
136183
PyObject *const obj = args[0];
137184
// Fast paths for python double.
138185
if (PyFloat_CheckExact(obj)) {
139-
return PyFloat_FromDouble(PyFloat_AS_DOUBLE(obj) * self->factor);
186+
return PyFloat_FromDouble(PyFloat_AsDouble(obj) * scaler->factor);
140187
}
141188
// Fast path for plain ndarray, with special care for contiguous data.
142189
if (PyArray_CheckExact(obj)) {
@@ -145,15 +192,15 @@ static PyObject *Scaler_vectorcall(
145192
npy_bool needs_float = type_num == NPY_FLOAT || type_num == NPY_CFLOAT;
146193
npy_bool supported_fast_type =
147194
(needs_float || type_num == NPY_DOUBLE || type_num == NPY_CDOUBLE);
148-
char *f_ptr = needs_float ? (char *)&self->factor_f : (char *)&self->factor;
195+
char *f_ptr = needs_float ? (char *)&scaler->factor_f : (char *)&scaler->factor;
149196
// Pass contiguous float or complex arrays directly to multiply loop,
150197
// bypassing ufunc setup.
151198
if (supported_fast_type && PyArray_ISONESEGMENT(arr) && PyArray_ISNOTSWAPPED(arr)) {
152199
return use_contiguous_loop(self, arr, f_ptr);
153200
}
154201
// If not, convert factor to array here, since that makes ufunc
155202
// call substantially faster. Use cached version if available.
156-
PyObject **A_factor = needs_float ? &self->A_factor_f : &self->A_factor;
203+
PyObject **A_factor = needs_float ? &scaler->A_factor_f : &scaler->A_factor;
157204
if (*A_factor == NULL) {
158205
const npy_intp dims[1] = {0};
159206
*A_factor =
@@ -162,41 +209,43 @@ static PyObject *Scaler_vectorcall(
162209
return NULL;
163210
}
164211
}
165-
return Py_TYPE(obj)->tp_as_number->nb_multiply(obj, *A_factor);
212+
return PyNumber_Multiply(obj, *A_factor);
166213
}
167214
// Fast paths for numpy double and float scalars; particularly useful for
168215
// float32, where it avoids double->float conversion.
169-
// Note: keep PyArray_VAL and ASSIGN where they are: they prevent the compiler
216+
// Note: keep the PyArray_Scalar* calls where they are: they prevent the compiler
170217
// from reordering the multiplication with the fperr calls.
171218
#define FAST_PATH_NUMPY_SCALAR(obj, Type, npy_type, factor) \
172219
if (PyArray_IsScalar(obj, Type)) { \
173-
PyObject *res = PyArrayScalar_New(Type); \
220+
npy_type out; \
221+
PyUFunc_clearfperr(); \
222+
PyArray_ScalarAsCtype(obj, (char *)&out); \
223+
out *= factor; \
224+
PyArray_Descr *dtype = PyArray_DescrFromScalar(obj); \
225+
PyObject *res = PyArray_Scalar((char *)&out, dtype, NULL); \
226+
Py_DECREF(dtype); \
174227
if (res == NULL) { \
175228
return NULL; \
176229
} \
177-
PyUFunc_clearfperr(); \
178-
npy_type x = PyArrayScalar_VAL(obj, Type); \
179-
npy_type out = x * factor; \
180-
PyArrayScalar_ASSIGN(res, Type, out); \
181230
int fpe_errors = PyUFunc_getfperr(); \
182231
if (fpe_errors && PyUFunc_GiveFloatingpointErrors("scalar multiply", fpe_errors) < 0) { \
183232
Py_CLEAR(res); \
184233
} \
185234
return res; \
186235
}
187-
FAST_PATH_NUMPY_SCALAR(obj, Double, npy_double, self->factor);
188-
FAST_PATH_NUMPY_SCALAR(obj, Float, npy_float, self->factor_f);
236+
FAST_PATH_NUMPY_SCALAR(obj, Double, npy_double, scaler->factor);
237+
FAST_PATH_NUMPY_SCALAR(obj, Float, npy_float, scaler->factor_f);
189238
#undef FAST_PATH_NUMPY_SCALAR
190239
// Fast path for python integers.
191240
if (PyLong_CheckExact(obj)) {
192241
double d = PyLong_AsDouble(obj);
193242
if (d == -1.0 && PyErr_Occurred()) {
194243
return NULL;
195244
}
196-
return PyFloat_FromDouble(d * self->factor);
245+
return PyFloat_FromDouble(d * scaler->factor);
197246
}
198247
// For cases without special treatment, we need the float object.
199-
PyObject *O_factor = get_o_factor(self);
248+
PyObject *O_factor = get_o_factor(scaler);
200249
if (O_factor == NULL) {
201250
return NULL;
202251
}
@@ -206,18 +255,6 @@ static PyObject *Scaler_vectorcall(
206255
PyObject_HasAttrString(obj, "dtype") ||
207256
PyObject_HasAttrString(obj, "__array_namespace__")) {
208257
// Generally, should be possible to go directly for the slot.
209-
if (Py_TYPE(obj)->tp_as_number != NULL) {
210-
binaryfunc slotv = Py_TYPE(obj)->tp_as_number->nb_multiply;
211-
if (slotv != NULL) {
212-
PyObject *res = slotv(obj, O_factor);
213-
if (res != Py_NotImplemented) {
214-
return res;
215-
}
216-
// Fall back to slow path, which will likely raise,
217-
// thus giving a reasonable error message.
218-
Py_DECREF(res);
219-
}
220-
}
221258
return PyNumber_Multiply(obj, O_factor);
222259
}
223260
// If obj is not a known type, try converting it to an array.
@@ -244,31 +281,32 @@ static PyObject *Scaler_vectorcall(
244281

245282
static inline PyObject *Scaler_from_factor(PyTypeObject *type, double factor)
246283
{
247-
ScalerObject *self = (ScalerObject *)type->tp_alloc(type, 0);
284+
PyObject *self = PyType_GenericAlloc(type, 0);
248285
if (self == NULL) {
249286
return NULL;
250287
}
251-
self->vectorcall = (vectorcallfunc)&Scaler_vectorcall;
252-
self->factor = factor;
253-
self->factor_f = (float)factor;
288+
ScalerObject *scaler = (ScalerObject *)self;
289+
scaler->vectorcall = (vectorcallfunc)&Scaler_vectorcall;
290+
scaler->factor = factor;
291+
scaler->factor_f = (float)factor;
254292
// No need to explicitly set to zero, since zeroed by tp_alloc.
255-
// self->O_factor = NULL; // initialized as needed in call.
256-
// self->A_factor = NULL;
257-
// self->A_factor_f = NULL;
258-
return (PyObject *)self;
293+
// scaler->O_factor = NULL; // initialized as needed in call.
294+
// scaler->A_factor = NULL;
295+
// scaler->A_factor_f = NULL;
296+
return self;
259297
}
260298

261299
static PyObject *Scaler_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
262300
{
263301
double factor;
264-
Py_ssize_t nargs = PyTuple_GET_SIZE(args);
302+
Py_ssize_t nargs = PyTuple_Size(args);
265303
if (nargs != 1 || kwds != NULL) {
266304
// Use parser to give error message.
267305
char *const kwlist[] = {"", NULL};
268306
PyArg_ParseTupleAndKeywords(args, kwds, "d:Scaler", kwlist, &factor);
269307
return NULL;
270308
}
271-
factor = PyFloat_AsDouble(PyTuple_GET_ITEM(args, 0));
309+
factor = PyFloat_AsDouble(PyTuple_GetItem(args, 0));
272310
if (factor == -1.0 && PyErr_Occurred()) {
273311
return NULL;
274312
}
@@ -288,39 +326,40 @@ static PyObject *Scaler_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
288326

289327
// traverse: Visit all references from an object, including its type
290328
// (since we're a heap type, which can get deallocated).
291-
static int Scaler_traverse(ScalerObject *self, visitproc visit, void *arg)
329+
static int Scaler_traverse(PyObject *self, visitproc visit, void *arg)
292330
{
293331
// Visit the type
294332
Py_VISIT(Py_TYPE(self));
295333

296334
// Visit attributes that may hold references.
297-
Py_VISIT(self->O_factor);
298-
Py_VISIT(self->A_factor);
299-
Py_VISIT(self->A_factor_f);
335+
ScalerObject *scaler = (ScalerObject *)self;
336+
Py_VISIT(scaler->O_factor);
337+
Py_VISIT(scaler->A_factor);
338+
Py_VISIT(scaler->A_factor_f);
300339
return 0;
301340
}
302341

303342
// Clear internal references; called from finalize and dealloc.
304-
static int Scaler_clear(ScalerObject *self)
343+
static int Scaler_clear(PyObject *self)
305344
{
306-
Py_CLEAR(self->O_factor);
307-
Py_CLEAR(self->A_factor);
308-
Py_CLEAR(self->A_factor_f);
345+
ScalerObject *scaler = (ScalerObject *)self;
346+
Py_CLEAR(scaler->O_factor);
347+
Py_CLEAR(scaler->A_factor);
348+
Py_CLEAR(scaler->A_factor_f);
309349
return 0;
310350
}
311351

312-
static void Scaler_dealloc(ScalerObject *self)
352+
static void Scaler_dealloc(PyObject *self)
313353
{
314354
PyObject_GC_UnTrack(self);
315355
Scaler_clear(self);
316-
PyTypeObject *type = Py_TYPE(self);
317-
type->tp_free(self);
318-
Py_DECREF(type);
356+
PyObject_GC_Del(self);
357+
Py_DECREF(Py_TYPE(self));
319358
}
320359

321-
static PyObject *Scaler_repr(ScalerObject *self)
360+
static PyObject *Scaler_repr(PyObject *self)
322361
{
323-
PyObject *O_factor = get_o_factor(self);
362+
PyObject *O_factor = get_o_factor((ScalerObject *)self);
324363
if (O_factor == NULL) {
325364
return NULL;
326365
}
@@ -352,27 +391,18 @@ static Py_hash_t Scaler_hash(PyObject *self)
352391
return (PyObject_Hash((PyObject *)Py_TYPE(self)) ^ PyObject_Hash(O_factor));
353392
}
354393

355-
static PyObject *Scaler___reduce__(ScalerObject *self)
394+
static PyObject *Scaler___reduce__(PyObject *self)
356395
{
357-
return Py_BuildValue("(O(d))", Py_TYPE(self), self->factor);
396+
return Py_BuildValue("(O(d))", Py_TYPE(self), ((ScalerObject *)self)->factor);
358397
}
359398

360399
static PyMemberDef Scaler_members[] = {
361-
#if Py_Version < 0x03120000
362-
{"factor",
363-
T_DOUBLE,
364-
offsetof(ScalerObject, factor),
365-
READONLY,
366-
"Factor with which input is multiplied."},
367-
{"__vectorcalloffset__", T_PYSSIZET, offsetof(ScalerObject, vectorcall), READONLY},
368-
#else
369400
{"factor",
370401
Py_T_DOUBLE,
371402
offsetof(ScalerObject, factor),
372403
Py_READONLY,
373404
"Factor with which input is multiplied."},
374405
{"__vectorcalloffset__", Py_T_PYSSIZET, offsetof(ScalerObject, vectorcall), Py_READONLY},
375-
#endif
376406
{NULL},
377407
};
378408

@@ -399,8 +429,7 @@ static PyType_Slot Scaler_slots[] = {
399429
static PyType_Spec Scaler_spec = {
400430
.name = "scaler.Scaler",
401431
.basicsize = sizeof(ScalerObject),
402-
.flags =
403-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_VECTORCALL | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE,
432+
.flags = SCALER_TP_FLAGS,
404433
.slots = Scaler_slots,
405434
};
406435

@@ -494,11 +523,7 @@ static void scaler_module_free(PyObject *m)
494523

495524
static PyModuleDef_Slot scaler_module_slots[] = {
496525
{Py_mod_exec, scaler_module_exec},
497-
#if Py_Version >= 0x03120000
498-
// Just use this while using static types
499-
{Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED},
500-
#endif
501-
{0, NULL}
526+
{0, NULL},
502527
};
503528

504529
static PyModuleDef scaler_module = {

0 commit comments

Comments
 (0)