Skip to content

Commit 1730c95

Browse files
authored
[mypyc] Add API and ABI versioning to librt.vecs (#21429)
This was missing from `librt.vecs`. Use the same approach that we've used in other `librt` submodules.
1 parent e80db52 commit 1730c95

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

mypyc/lib-rt/vecs/librt_vecs.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,19 @@ static PyTypeObject *get_vec_type(void) {
958958
return &VecType;
959959
}
960960

961+
static int
962+
vecs_abi_version(void) {
963+
return LIBRT_VECS_ABI_VERSION;
964+
}
965+
966+
static int
967+
vecs_api_version(void) {
968+
return LIBRT_VECS_API_VERSION;
969+
}
970+
961971
static VecCapsule Capsule = {
972+
vecs_abi_version,
973+
vecs_api_version,
962974
&Vec_TAPI,
963975
&Vec_NestedAPI,
964976
&Vec_I64API,

mypyc/lib-rt/vecs/librt_vecs.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@
1010
#include <stdint.h>
1111
#include "mypyc_util.h"
1212

13+
// ABI version -- only an exact match is compatible. This will only be changed in
14+
// very exceptional cases (likely never) due to strict backward compatibility
15+
// requirements.
16+
#define LIBRT_VECS_ABI_VERSION 1
17+
18+
// API version -- more recent versions must maintain backward compatibility, i.e.
19+
// we can add new features but not remove or change existing features (unless
20+
// ABI version is changed, but see the comment above).
21+
#define LIBRT_VECS_API_VERSION 1
22+
1323
#ifdef MYPYC_EXPERIMENTAL
1424

1525
// Magic (native) integer return value on exception. Caller must also
@@ -500,6 +510,8 @@ typedef struct _VecNestedAPI {
500510
} VecNestedAPI;
501511

502512
typedef struct {
513+
int (*abi_version)(void);
514+
int (*api_version)(void);
503515
VecTAPI *t;
504516
VecNestedAPI *nested;
505517
VecI64API *i64;

mypyc/lib-rt/vecs/librt_vecs_api.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,28 @@ import_librt_vecs(void)
2828
if (mod == NULL)
2929
return -1;
3030
Py_DECREF(mod); // we import just for the side effect of making the below work.
31-
VecApi = PyCapsule_Import("librt.vecs._C_API", 0);
32-
if (!VecApi)
31+
VecCapsule *capsule = PyCapsule_Import("librt.vecs._C_API", 0);
32+
if (!capsule)
3333
return -1;
34+
if (capsule->abi_version() != LIBRT_VECS_ABI_VERSION) {
35+
char err[128];
36+
snprintf(err, sizeof(err),
37+
"ABI version conflict for librt.vecs, expected %d, found %d",
38+
LIBRT_VECS_ABI_VERSION,
39+
capsule->abi_version());
40+
PyErr_SetString(PyExc_ValueError, err);
41+
return -1;
42+
}
43+
if (capsule->api_version() < LIBRT_VECS_API_VERSION) {
44+
char err[128];
45+
snprintf(err, sizeof(err),
46+
"API version conflict for librt.vecs, expected %d or newer, found %d (hint: upgrade librt)",
47+
LIBRT_VECS_API_VERSION,
48+
capsule->api_version());
49+
PyErr_SetString(PyExc_ValueError, err);
50+
return -1;
51+
}
52+
VecApi = capsule;
3453
VecI64Api = *VecApi->i64;
3554
VecI32Api = *VecApi->i32;
3655
VecI16Api = *VecApi->i16;

0 commit comments

Comments
 (0)