Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Doc/c-api/concrete.rst
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,4 @@ C API for extension modules

curses.rst
datetime.rst
expat.rst
193 changes: 193 additions & 0 deletions Doc/c-api/expat.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
.. highlight:: c

Expat C API
-----------

:mod:`!pyexpat` exposes a C interface for extension modules.
Consumers must include the header file :file:`pyexpat.h` (which is not
included by default by :file:`Python.h`) and ``expat.h`` for Expat.

To use the C API, consider adding the following code in your extension
module initialization function and store the pointer to the C API in
the module state:

.. code-block::

struct PyExpat_CAPI *capi = NULL;
capi = (struct PyExpat_CAPI *)PyCapsule_Import(PyExpat_CAPSULE_NAME, 0);
if (capi == NULL) {
goto error;
}
if (!(
strcmp(capi->magic, PyExpat_CAPI_MAGIC) == 0
&& (size_t)capi->size >= sizeof(struct PyExpat_CAPI)
&& capi->MAJOR_VERSION == XML_MAJOR_VERSION
&& capi->MINOR_VERSION == XML_MINOR_VERSION
&& capi->MICRO_VERSION == XML_MICRO_VERSION
)) {
PyErr_SetString(PyExc_ImportError, "pyexpat version is incompatible");
goto error;
}


.. c:macro:: PyExpat_CAPI_MAGIC

Check warning on line 33 in Doc/c-api/expat.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

Duplicate C declaration, also defined at c-api/pyexpat:18.

The Expat C API magic number.


.. c:macro:: PyExpat_CAPSULE_NAME

Check warning on line 38 in Doc/c-api/expat.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

Duplicate C declaration, also defined at c-api/pyexpat:11.

The Expat C API capsule name to import via :c:func:`PyCapsule_Import`.


.. c:struct:: PyExpat_CAPI

Check warning on line 43 in Doc/c-api/expat.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

Duplicate C declaration, also defined at c-api/pyexpat:26.

The Expat C API structure.

.. below are members that were added in Python 2.x

.. c:member:: char *magic

Check warning on line 49 in Doc/c-api/expat.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

Duplicate C declaration, also defined at c-api/pyexpat:35.

Set to :c:macro:`PyExpat_CAPI_MAGIC`.

.. c:member:: int size

Check warning on line 53 in Doc/c-api/expat.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

Duplicate C declaration, also defined at c-api/pyexpat:39.

Set to ``sizeof(struct PyExpat_CAPI)``.

.. c:member:: int MAJOR_VERSION

Check warning on line 57 in Doc/c-api/expat.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

Duplicate C declaration, also defined at c-api/pyexpat:44.

Check warning on line 57 in Doc/c-api/expat.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

Duplicate C declaration, also defined at c-api/pyexpat:44.

Check warning on line 57 in Doc/c-api/expat.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

Duplicate C declaration, also defined at c-api/pyexpat:44.
int MINOR_VERSION
int MICRO_VERSION

Set to ``XML_MAJOR_VERSION``, ``XML_MINOR_VERSION``,
and ``XML_MICRO_VERSION`` respectively of the linked
Expat library.

The following members expose the C API as provided by Expat.
See the `official documentation <https://libexpat.github.io/doc/api/latest>`_
for details (some names may slightly differ but the signature and intent
will not).

.. c:member:: const XML_LChar *(*ErrorString)(enum XML_Error errno)

Check warning on line 70 in Doc/c-api/expat.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

Duplicate C declaration, also defined at c-api/pyexpat:75.
.. c:member:: enum XML_Error (*GetErrorCode)(XML_Parser parser)

Check warning on line 71 in Doc/c-api/expat.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

Duplicate C declaration, also defined at c-api/pyexpat:75.

.. c:member:: XML_Size (*GetErrorColumnNumber)(XML_Parser parser)
.. c:member:: XML_Size (*GetErrorLineNumber)(XML_Parser parser)

.. c:member:: enum XML_Status (*Parse)(\
XML_Parser parser,\
const char *s, int len, int isFinal)

.. c:member:: XML_Parser (*ParserCreate_MM)(\
const XML_Char *encoding,\
const XML_Memory_Handling_Suite *memsuite,\
const XML_Char *sep)

.. c:member:: void (*ParserFree)(XML_Parser parser)

.. c:member:: void (*SetCharacterDataHandler)(\
XML_Parser parser,\
XML_CharacterDataHandler handler)

.. c:member:: void (*SetCommentHandler)(\
XML_Parser parser,\
XML_CommentHandler handler)

.. c:member:: void (*SetDefaultHandlerExpand)(\
XML_Parser parser,\
XML_DefaultHandler handler)

.. c:member:: void (*SetElementHandler)(\
XML_Parser parser,\
XML_StartElementHandler start,\
XML_EndElementHandler end)

.. c:member:: void (*SetNamespaceDeclHandler)(\
XML_Parser parser,\
XML_StartNamespaceDeclHandler start,\
XML_EndNamespaceDeclHandler end)

.. c:member:: void (*SetProcessingInstructionHandler)(\
XML_Parser parser,\
XML_ProcessingInstructionHandler handler)

.. c:member:: void (*SetUnknownEncodingHandler)(\
XML_Parser parser,\
XML_UnknownEncodingHandler handler,\
void *encodingHandlerData)

.. c:member:: void (*SetUserData)(\
XML_Parser parser,\
void *userData)

.. below are members that were added after Python 3.0

.. c:member:: void (*SetStartDoctypeDeclHandler)(\
XML_Parser parser,\
XML_StartDoctypeDeclHandler start)

.. versionadded:: 3.2

.. c:member:: enum XML_Status (*SetEncoding)(\
XML_Parser parser,\
const XML_Char *encoding)

.. versionadded:: 3.3

.. c:member:: int (*DefaultUnknownEncodingHandler)(\
void *encodingHandlerData,\
const XML_Char *name,\
XML_Encoding *info)

.. versionadded:: 3.3

.. c:member:: int (*SetHashSalt)(\
XML_Parser parser,\
unsigned long hash_salt)

Might be ``NULL`` for Expat versions prior to 2.1.0.

.. versionadded:: 3.4

.. c:member:: XML_Bool (*SetReparseDeferralEnabled)(\
XML_Parser parser,\
XML_Bool enabled)

Might be ``NULL`` for Expat versions prior to 2.6.0.

.. versionadded:: 3.8

.. c:member:: XML_Bool (*SetAllocTrackerActivationThreshold)(\
XML_Parser parser,\
unsigned long long activationThresholdBytes)

Might be ``NULL`` for Expat versions prior to 2.7.2.

.. uncomment this when the backport is done
.. versionadded:: 3.10

.. c:member:: XML_Bool (*SetAllocTrackerMaximumAmplification)(\
XML_Parser parser,\
float maxAmplificationFactor)

Might be ``NULL`` for Expat versions prior to 2.7.2.

.. uncomment this when the backport is done
.. versionadded:: 3.10

.. c:member:: XML_Bool (*SetBillionLaughsAttackProtectionActivationThreshold)(\
XML_Parser parser,\
unsigned long long activationThresholdBytes)

Might be ``NULL`` for Expat versions prior to 2.4.0.

.. uncomment this when the backport is done
.. versionadded:: 3.10

.. c:member:: XML_Bool (*SetBillionLaughsAttackProtectionMaximumAmplification)(\
XML_Parser parser,\
float maxAmplificationFactor)

Might be ``NULL`` for Expat versions prior to 2.4.0.

.. uncomment this when the backport is done
.. versionadded:: 3.10
24 changes: 24 additions & 0 deletions Doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,30 @@
('envvar', 'USERPROFILE'),
]

# Vendored identifiers without intersphinx support.
nitpick_ignore += [
# used in c-api/expat.rst
('c:identifier', 'XML_Bool'),
('c:identifier', 'XML_Char'),
('c:identifier', 'XML_CharacterDataHandler'),
('c:identifier', 'XML_CommentHandler'),
('c:identifier', 'XML_DefaultHandler'),
('c:identifier', 'XML_Encoding'),
('c:identifier', 'XML_EndElementHandler'),
('c:identifier', 'XML_EndNamespaceDeclHandler'),
('c:identifier', 'XML_Error'),
('c:identifier', 'XML_LChar'),
('c:identifier', 'XML_Memory_Handling_Suite'),
('c:identifier', 'XML_Parser'),
('c:identifier', 'XML_ProcessingInstructionHandler'),
('c:identifier', 'XML_Size'),
('c:identifier', 'XML_StartDoctypeDeclHandler'),
('c:identifier', 'XML_StartElementHandler'),
('c:identifier', 'XML_StartNamespaceDeclHandler'),
('c:identifier', 'XML_Status'),
('c:identifier', 'XML_UnknownEncodingHandler'),
]

# Temporary undocumented names.
# In future this list must be empty.
nitpick_ignore += [
Expand Down
6 changes: 2 additions & 4 deletions Include/pyexpat.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
#define PyExpat_CAPI_MAGIC "pyexpat.expat_CAPI 1.1"
#define PyExpat_CAPSULE_NAME "pyexpat.expat_CAPI"

struct PyExpat_CAPI
{
char* magic; /* set to PyExpat_CAPI_MAGIC */
struct PyExpat_CAPI {
char *magic; /* set to PyExpat_CAPI_MAGIC */
Comment thread
picnixz marked this conversation as resolved.
Comment on lines +9 to +10

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid making code formatting changes in this PR.

int size; /* set to sizeof(struct PyExpat_CAPI) */
int MAJOR_VERSION;
int MINOR_VERSION;
Expand Down Expand Up @@ -67,4 +66,3 @@ struct PyExpat_CAPI
XML_Parser parser, const uint8_t entropy[16]);
/* always add new stuff to the end! */
};

Loading