Skip to content

Commit 734a09d

Browse files
committed
add PyExpat_CheckCompatibility
1 parent dbe4090 commit 734a09d

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

Include/pyexpat.h

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,29 @@
33

44
/* note: you must import expat.h before importing this module! */
55

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+
*/
617
#define PyExpat_CAPI_MAGIC "pyexpat.expat_CAPI 1.1"
718
#define PyExpat_CAPSULE_NAME "pyexpat.expat_CAPI"
819

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) */
1323
int MAJOR_VERSION;
1424
int MINOR_VERSION;
1525
int MICRO_VERSION;
1626
/* pointers to selected expat functions. add new functions at
1727
the end, if needed */
18-
const XML_LChar * (*ErrorString)(enum XML_Error code);
28+
const XML_LChar *(*ErrorString)(enum XML_Error code);
1929
enum XML_Error (*GetErrorCode)(XML_Parser parser);
2030
XML_Size (*GetErrorColumnNumber)(XML_Parser parser);
2131
XML_Size (*GetErrorLineNumber)(XML_Parser parser);
@@ -63,5 +73,22 @@ struct PyExpat_CAPI
6373
XML_Bool (*SetBillionLaughsAttackProtectionMaximumAmplification)(
6474
XML_Parser parser, float maxAmplificationFactor);
6575
/* 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+
}
6790

91+
#ifdef __cplusplus
92+
}
93+
#endif
94+
#endif /* !PyExpat_CAPI_H */
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Expose :c:func:`!PyExpat_CheckCompatibility` for checking Expat C API
2+
compatibility. Patch by Bénédikt Tran.

0 commit comments

Comments
 (0)