-
-
Notifications
You must be signed in to change notification settings - Fork 33.3k
gh-141004: document pyexpat C API
#141259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
picnixz
wants to merge
10
commits into
python:main
Choose a base branch
from
picnixz:docs/capi/expat-141004
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+220
−4
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5b857a4
document Expat C API
picnixz e20466a
suppress some references
picnixz 1c51a14
simplify instructions
picnixz cf35d71
fixup
picnixz e7e0a7d
NULL
picnixz 660a1a1
address review
picnixz cba5d4c
nevermind, `c_id_attributes` isn't for that
picnixz 94f656b
Merge branch 'main' into docs/capi/expat-141004
picnixz 50575b5
fixup
picnixz e6c452c
fix markup
picnixz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -125,3 +125,4 @@ C API for extension modules | |
|
|
||
| curses.rst | ||
| datetime.rst | ||
| expat.rst | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 initilisation 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 | ||
| The Expat C API magic number. | ||
|
|
||
|
|
||
| .. c:macro:: PyExpat_CAPSULE_NAME | ||
| The Expat C API capsule name to import via :c:func:`PyCapsule_Import`. | ||
|
|
||
|
|
||
| .. c:struct:: PyExpat_CAPI | ||
| The Expat C API structure. | ||
|
|
||
| .. below are members that were added in Python 2.x | ||
| .. c:member:: char *magic | ||
| Set to :c:macro:`PyExpat_CAPI_MAGIC`. | ||
|
|
||
| .. c:member:: int size | ||
| Set to ``sizeof(struct PyExpat_CAPI)``. | ||
|
|
||
| .. c:member:: int MAJOR_VERSION | ||
| 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) | ||
| .. c:member:: enum XML_Error (*GetErrorCode)(XML_Parser parser) | ||
| .. 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.