|
3 | 3 |
|
4 | 4 | /* note: you must import expat.h before importing this module! */ |
5 | 5 |
|
| 6 | +#ifndef PyExpat_CAPI_H |
| 7 | +#define PyExpat_CAPI_H |
| 8 | +#ifdef __cplusplus |
| 9 | +extern "C" { |
| 10 | +#endif |
| 11 | + |
| 12 | +/* |
| 13 | + * Only bump the magic number when PyExpat_CAPI changes but not its size. |
| 14 | + * If the structure changes because of an additional field, the magic number |
| 15 | + * should not be bumped (see https://github.com/python/cpython/issues/115398). |
| 16 | + */ |
6 | 17 | #define PyExpat_CAPI_MAGIC "pyexpat.expat_CAPI 1.1" |
7 | 18 | #define PyExpat_CAPSULE_NAME "pyexpat.expat_CAPI" |
8 | 19 |
|
9 | | -struct PyExpat_CAPI |
10 | | -{ |
11 | | - char* magic; /* set to PyExpat_CAPI_MAGIC */ |
12 | | - int size; /* set to sizeof(struct PyExpat_CAPI) */ |
| 20 | +typedef struct PyExpat_CAPI { |
| 21 | + char *magic; /* set to PyExpat_CAPI_MAGIC */ |
| 22 | + int size; /* set to sizeof(PyExpat_CAPI) */ |
13 | 23 | int MAJOR_VERSION; |
14 | 24 | int MINOR_VERSION; |
15 | 25 | int MICRO_VERSION; |
16 | 26 | /* pointers to selected expat functions. add new functions at |
17 | 27 | the end, if needed */ |
18 | | - const XML_LChar * (*ErrorString)(enum XML_Error code); |
| 28 | + const XML_LChar *(*ErrorString)(enum XML_Error code); |
19 | 29 | enum XML_Error (*GetErrorCode)(XML_Parser parser); |
20 | 30 | XML_Size (*GetErrorColumnNumber)(XML_Parser parser); |
21 | 31 | XML_Size (*GetErrorLineNumber)(XML_Parser parser); |
@@ -63,5 +73,22 @@ struct PyExpat_CAPI |
63 | 73 | XML_Bool (*SetBillionLaughsAttackProtectionMaximumAmplification)( |
64 | 74 | XML_Parser parser, float maxAmplificationFactor); |
65 | 75 | /* always add new stuff to the end! */ |
66 | | -}; |
| 76 | +} PyExpat_CAPI; |
| 77 | + |
| 78 | +static inline int |
| 79 | +PyExpat_CheckCompatibility(const PyExpat_CAPI *capi) |
| 80 | +{ |
| 81 | + assert(capi != NULL); |
| 82 | + return ( |
| 83 | + strcmp(capi->magic, PyExpat_CAPI_MAGIC) == 0 |
| 84 | + && (size_t)capi->size >= sizeof(PyExpat_CAPI) |
| 85 | + && capi->MAJOR_VERSION == XML_MAJOR_VERSION |
| 86 | + && capi->MINOR_VERSION == XML_MINOR_VERSION |
| 87 | + && capi->MICRO_VERSION == XML_MICRO_VERSION |
| 88 | + ); |
| 89 | +} |
67 | 90 |
|
| 91 | +#ifdef __cplusplus |
| 92 | +} |
| 93 | +#endif |
| 94 | +#endif /* !PyExpat_CAPI_H */ |
0 commit comments