Skip to content

Commit 1c51a14

Browse files
committed
simplify instructions
1 parent e20466a commit 1c51a14

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

Doc/c-api/expat.rst

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,25 @@ the module state:
1515
1616
struct PyExpat_CAPI *capi = NULL;
1717
capi = (struct PyExpat_CAPI *)PyCapsule_Import(PyExpat_CAPSULE_NAME, 0);
18-
if (capi == NULL) { goto error; }
19-
20-
/* check if the C API is compatible with the version of Expat version */
21-
if (strcmp(capi->magic, PyExpat_CAPI_MAGIC) != 0
22-
|| (size_t)capi->size < sizeof(struct PyExpat_CAPI)
23-
|| capi->MAJOR_VERSION != XML_MAJOR_VERSION
24-
|| capi->MINOR_VERSION != XML_MINOR_VERSION
25-
|| capi->MICRO_VERSION != XML_MICRO_VERSION
26-
) {
18+
if (capi == NULL) {
19+
goto error;
20+
}
21+
if (!PyExpat_CheckCompatibility(capi)) {
2722
PyErr_SetString(PyExc_ImportError, "pyexpat version is incompatible");
2823
goto error;
2924
}
3025
3126
27+
.. c:function:: int PyExpat_CheckCompatibility(struct PyExpat_API *api)
28+
29+
Return ``1`` if *api* is compatible with the linked Expat library,
30+
and ``0`` otherwise. This function never sets a Python exception.
31+
32+
*api* must not be ``NULL``.
33+
34+
.. versionadded:: next
35+
36+
3237
.. c:macro:: PyExpat_CAPI_MAGIC
3338
3439
The Expat C API magic number.

Include/pyexpat.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
#define PyExpat_CAPI_MAGIC "pyexpat.expat_CAPI 1.1"
77
#define PyExpat_CAPSULE_NAME "pyexpat.expat_CAPI"
88

9-
struct PyExpat_CAPI
10-
{
11-
char* magic; /* set to PyExpat_CAPI_MAGIC */
9+
struct PyExpat_CAPI {
10+
char *magic; /* set to PyExpat_CAPI_MAGIC */
1211
int size; /* set to sizeof(struct PyExpat_CAPI) */
1312
int MAJOR_VERSION;
1413
int MINOR_VERSION;
@@ -65,3 +64,15 @@ struct PyExpat_CAPI
6564
/* always add new stuff to the end! */
6665
};
6766

67+
static inline int
68+
PyExpat_CheckCompatibility(struct PyExpat_CAPI *api)
69+
{
70+
return (
71+
api != NULL
72+
&& strcmp(api->magic, PyExpat_CAPI_MAGIC) == 0
73+
&& (size_t)api->size >= sizeof(struct PyExpat_CAPI)
74+
&& api->MAJOR_VERSION == XML_MAJOR_VERSION
75+
&& api->MINOR_VERSION == XML_MINOR_VERSION
76+
&& api->MICRO_VERSION == XML_MICRO_VERSION
77+
);
78+
}

0 commit comments

Comments
 (0)