Skip to content

Commit d16a691

Browse files
gh-156821: Fix inaccuracies in the xml.parsers.expat documentation (GH-156822)
Corrected, in the documentation and in the docstrings: * GetInputContext() returns bytes, and its result extends to the end of the buffered input; * Parse() accepts a bytes-like object as well as a string, and ignores the encoding declaration for a string; * ParseFile() only supports binary files; * XML_ERROR_XML_DECL was described as XML_ERROR_NO_ELEMENTS; * UnparsedEntityDeclHandler is not restricted to Expat 1.2; * GetSpecifiedAttributeCount() needs ordered_attributes. Documented the intern parameter and attribute, namespace_prefixes, SkippedEntityHandler, XML_CTYPE_MIXED and XML_CTYPE_NAME, and EXPAT_VERSION, version_info and features. Removed obsolete notes about Expat 1.2 and 1.95.0, and :noindex: from the content model constants, which are not documented elsewhere; completed the list of attribute types in AttlistDeclHandler.
1 parent db92901 commit d16a691

3 files changed

Lines changed: 133 additions & 57 deletions

File tree

Doc/library/pyexpat.rst

Lines changed: 102 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ the XML document.
3535
This module uses the :mod:`pyexpat` module to provide access to the Expat
3636
parser. Direct use of the :mod:`pyexpat` module is deprecated.
3737

38-
This module provides one exception and one type object:
38+
This module provides the following exception, type object and data items:
3939

4040

4141
.. exception:: ExpatError
@@ -53,6 +53,29 @@ This module provides one exception and one type object:
5353

5454
The type of the return values from the :func:`ParserCreate` function.
5555

56+
57+
.. data:: EXPAT_VERSION
58+
59+
The version string of the Expat library loaded by the interpreter,
60+
like ``'expat_2.8.4'``.
61+
62+
63+
.. data:: version_info
64+
65+
The version of the Expat library loaded by the interpreter,
66+
as a tuple of three integers: major, minor and micro version.
67+
68+
69+
.. data:: features
70+
71+
The list of the features with which the loaded Expat library
72+
was compiled, as ``(name, value)`` pairs.
73+
The value is only meaningful for features which have one,
74+
like ``'XML_CONTEXT_BYTES'`` or the default protection limits
75+
``'XML_BLAP_ACT_THRES'`` and ``'XML_AT_MAX_AMP'``;
76+
for other features, like ``'XML_DTD'`` and ``'XML_NS'``,
77+
the value is ``0`` and only the presence of the name is significant.
78+
5679
The :mod:`!xml.parsers.expat` module contains two functions:
5780

5881

@@ -61,7 +84,7 @@ The :mod:`!xml.parsers.expat` module contains two functions:
6184
Returns an explanatory string for a given error number *errno*.
6285

6386

64-
.. function:: ParserCreate(encoding=None, namespace_separator=None)
87+
.. function:: ParserCreate(encoding=None, namespace_separator=None, intern=None)
6588

6689
Creates and returns a new :class:`xmlparser` object.
6790
*encoding* [1]_, if specified, must be a string naming the encoding
@@ -122,6 +145,11 @@ The :mod:`!xml.parsers.expat` module contains two functions:
122145
http://www.python.org/ns/ elem1
123146
elem2
124147

148+
*intern*, if given, must be a dictionary.
149+
It is used to intern the names of elements and attributes,
150+
and is available as the :attr:`~xmlparser.intern` attribute.
151+
By default a new empty dictionary is created for every parser.
152+
125153
Due to limitations in the ``Expat`` library used by :mod:`pyexpat`,
126154
the :class:`xmlparser` instance returned can only be used to parse a single
127155
XML document. Call ``ParserCreate`` for each document to provide unique
@@ -143,18 +171,24 @@ XMLParser Objects
143171

144172
.. method:: xmlparser.Parse(data[, isfinal])
145173

146-
Parses the contents of the string *data*, calling the appropriate handler
147-
functions to process the parsed data. *isfinal* must be true on the final call
148-
to this method; it allows the parsing of a single file in fragments,
174+
Parses the contents of *data*,
175+
calling the appropriate handler functions to process the parsed data.
176+
*data* can be a :term:`bytes-like object` or a string.
177+
If it is a string, the encoding declaration in the XML data is ignored,
178+
and the data is parsed as already decoded text.
179+
*isfinal* must be true on the final call to this method;
180+
it allows the parsing of a single file in fragments,
149181
not the submission of multiple files.
150-
*data* can be the empty string at any time.
182+
*data* can be empty at any time.
151183

152184

153185
.. method:: xmlparser.ParseFile(file)
154186

155-
Parse XML data reading from the object *file*. *file* only needs to provide
156-
the ``read(nbytes)`` method, returning the empty string when there's no more
157-
data.
187+
Parse XML data reading from the object *file*.
188+
*file* only needs to provide the ``read(nbytes)`` method,
189+
which returns bytes, and an empty bytes object when there's no more data.
190+
Text files are not supported;
191+
use :meth:`Parse` for data which is already decoded.
158192

159193

160194
.. method:: xmlparser.SetBase(base)
@@ -188,9 +222,15 @@ XMLParser Objects
188222

189223
.. method:: xmlparser.GetInputContext()
190224

191-
Returns the input data that generated the current event as a string. The data is
192-
in the encoding of the entity which contains the text. When called while an
193-
event handler is not active, the return value is ``None``.
225+
Returns the input data which generated the current event
226+
as a :class:`bytes` object.
227+
The data is in the encoding of the entity which contains the text.
228+
It extends to the end of the currently buffered input,
229+
therefore it can contain also the data of the following events,
230+
and if the event was generated by a large amount of text,
231+
not all of it may be available.
232+
When called while an event handler is not active,
233+
the return value is ``None``.
194234

195235

196236
.. method:: xmlparser.ExternalEntityParserCreate(context[, encoding])
@@ -437,6 +477,22 @@ against some common XML vulnerabilities.
437477
default, this attribute is false; it may be changed at any time.
438478

439479

480+
.. attribute:: xmlparser.intern
481+
482+
The dictionary used to intern the names of elements and attributes.
483+
It is either the dictionary passed as the *intern* argument
484+
of :func:`ParserCreate`, or a new dictionary created for this parser.
485+
486+
487+
.. attribute:: xmlparser.namespace_prefixes
488+
489+
If set to a true value, and namespace processing is enabled,
490+
the namespace prefix is reported as the third part of the expanded name,
491+
separated by the namespace separator.
492+
Names which have no prefix are not affected.
493+
By default, this attribute is false; it may be changed at any time.
494+
495+
440496
The following attributes contain values relating to the most recent error
441497
encountered by an :class:`xmlparser` object, and will only have correct values
442498
once a call to :meth:`Parse` or :meth:`ParseFile` has raised an
@@ -500,8 +556,7 @@ otherwise stated.
500556
encoding of the document text, and an optional "standalone" declaration.
501557
*version* and *encoding* will be strings, and *standalone* will be ``1`` if the
502558
document is declared standalone, ``0`` if it is declared not to be standalone,
503-
or ``-1`` if the standalone clause was omitted. This is only available with
504-
Expat version 1.95.0 or newer.
559+
or ``-1`` if the standalone clause was omitted.
505560

506561

507562
.. method:: xmlparser.StartDoctypeDeclHandler(doctypeName, systemId, publicId, has_internal_subset)
@@ -510,14 +565,12 @@ otherwise stated.
510565
...``). The *doctypeName* is provided exactly as presented. The *systemId* and
511566
*publicId* parameters give the system and public identifiers if specified, or
512567
``None`` if omitted. *has_internal_subset* will be true if the document
513-
contains an internal document declaration subset. This requires Expat version
514-
1.2 or newer.
568+
contains an internal document declaration subset.
515569

516570

517571
.. method:: xmlparser.EndDoctypeDeclHandler()
518572

519-
Called when Expat is done parsing the document type declaration. This requires
520-
Expat version 1.2 or newer.
573+
Called when Expat is done parsing the document type declaration.
521574

522575

523576
.. method:: xmlparser.ElementDeclHandler(name, model)
@@ -532,12 +585,16 @@ otherwise stated.
532585
declaration declares three attributes, this handler is called three times, once
533586
for each attribute. *elname* is the name of the element to which the
534587
declaration applies and *attname* is the name of the attribute declared. The
535-
attribute type is a string passed as *type*; the possible values are
536-
``'CDATA'``, ``'ID'``, ``'IDREF'``, ... *default* gives the default value for
588+
The attribute type is a string passed as *type*:
589+
``'CDATA'``, ``'ID'``, ``'IDREF'``, ``'IDREFS'``, ``'ENTITY'``,
590+
``'ENTITIES'``, ``'NMTOKEN'`` or ``'NMTOKENS'``,
591+
an enumeration like ``'(x|y)'``,
592+
or a notation list like ``'NOTATION(n1|n2)'``.
593+
*default* gives the default value for
537594
the attribute used when the attribute is not specified by the document instance,
538595
or ``None`` if there is no default value (``#IMPLIED`` values). If the
539596
attribute is required to be given in the document instance, *required* will be
540-
true. This requires Expat version 1.95.0 or newer.
597+
true.
541598

542599

543600
.. method:: xmlparser.StartElementHandler(name, attributes)
@@ -573,10 +630,10 @@ otherwise stated.
573630

574631
.. method:: xmlparser.UnparsedEntityDeclHandler(entityName, base, systemId, publicId, notationName)
575632

576-
Called for unparsed (NDATA) entity declarations. This is only present for
577-
version 1.2 of the Expat library; for more recent versions, use
578-
:attr:`EntityDeclHandler` instead. (The underlying function in the Expat
579-
library has been declared obsolete.)
633+
Called for unparsed (NDATA) entity declarations.
634+
If this handler is not set, such declarations are reported by
635+
:attr:`EntityDeclHandler`, which is preferred for new code.
636+
(The underlying function in the Expat library has been declared obsolete.)
580637

581638

582639
.. method:: xmlparser.EntityDeclHandler(entityName, is_parameter_entity, value, base, systemId, publicId, notationName)
@@ -587,8 +644,7 @@ otherwise stated.
587644
``None`` for parsed entities, and the name of the notation for unparsed
588645
entities. *is_parameter_entity* will be true if the entity is a parameter entity
589646
or false for general entities (most applications only need to be concerned with
590-
general entities). This is only available starting with version 1.95.0 of the
591-
Expat library.
647+
general entities).
592648

593649

594650
.. method:: xmlparser.NotationDeclHandler(notationName, base, systemId, publicId)
@@ -642,7 +698,7 @@ otherwise stated.
642698

643699
.. method:: xmlparser.DefaultHandlerExpand(data)
644700

645-
This is the same as the :func:`DefaultHandler`, but doesn't inhibit expansion
701+
This is the same as the :attr:`DefaultHandler`, but doesn't inhibit expansion
646702
of internal entities. The entity reference will not be passed to the default
647703
handler.
648704

@@ -686,6 +742,16 @@ otherwise stated.
686742
:attr:`DefaultHandler` callback, if provided.
687743

688744

745+
.. method:: xmlparser.SkippedEntityHandler(entityName, is_parameter_entity)
746+
747+
Called for entity references which are not expanded,
748+
because the parser did not read the declaration of the entity.
749+
This happens when the external DTD subset or an external parameter entity
750+
is not parsed.
751+
*is_parameter_entity* is true for a parameter entity
752+
and false for a general entity.
753+
754+
689755
.. _expaterror-objects:
690756

691757
ExpatError Exceptions
@@ -786,35 +852,35 @@ The constants in the model type group are:
786852

787853

788854
.. data:: XML_CTYPE_ANY
789-
:noindex:
790855

791856
The element named by the model name was declared to have a content model of
792857
``ANY``.
793858

794859

795860
.. data:: XML_CTYPE_CHOICE
796-
:noindex:
797861

798862
The named element allows a choice from a number of options; this is used for
799863
content models such as ``(A | B | C)``.
800864

801865

802866
.. data:: XML_CTYPE_EMPTY
803-
:noindex:
804867

805868
Elements which are declared to be ``EMPTY`` have this model type.
806869

807870

808871
.. data:: XML_CTYPE_MIXED
809-
:noindex:
872+
873+
The named element allows character data, optionally interspersed with
874+
the named children; this is used for content models such as
875+
``(#PCDATA)`` and ``(#PCDATA | A | B)*``.
810876

811877

812878
.. data:: XML_CTYPE_NAME
813-
:noindex:
879+
880+
The model names a single element, as for ``A``.
814881

815882

816883
.. data:: XML_CTYPE_SEQ
817-
:noindex:
818884

819885
Models which represent a series of models which follow one after the other are
820886
indicated with this model type. This is used for models such as ``(A, B, C)``.
@@ -823,25 +889,21 @@ The constants in the quantifier group are:
823889

824890

825891
.. data:: XML_CQUANT_NONE
826-
:noindex:
827892

828893
No modifier is given, so it can appear exactly once, as for ``A``.
829894

830895

831896
.. data:: XML_CQUANT_OPT
832-
:noindex:
833897

834898
The model is optional: it can appear once or not at all, as for ``A?``.
835899

836900

837901
.. data:: XML_CQUANT_PLUS
838-
:noindex:
839902

840903
The model must occur one or more times (like ``A+``).
841904

842905

843906
.. data:: XML_CQUANT_REP
844-
:noindex:
845907

846908
The model must occur zero or more times, as for ``A*``.
847909

@@ -925,7 +987,7 @@ The ``errors`` module has the following attributes:
925987
.. data:: XML_ERROR_NO_ELEMENTS
926988

927989
The document contains no elements (XML requires all documents to contain exactly
928-
one top-level element)..
990+
one top-level element).
929991

930992

931993
.. data:: XML_ERROR_NO_MEMORY
@@ -1028,7 +1090,7 @@ The ``errors`` module has the following attributes:
10281090

10291091
.. data:: XML_ERROR_XML_DECL
10301092

1031-
The document contained no document element at all.
1093+
There was an error parsing the XML declaration.
10321094

10331095

10341096
.. data:: XML_ERROR_TEXT_DECL

Modules/clinic/pyexpat.c.h

Lines changed: 14 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)