@@ -35,7 +35,7 @@ the XML document.
3535This module uses the :mod: `pyexpat ` module to provide access to the Expat
3636parser. 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+
5679The :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. *encoding *, if specified,
6790 must be a string naming the encoding used by the XML data. Expat doesn't
@@ -108,6 +131,11 @@ The :mod:`!xml.parsers.expat` module contains two functions:
108131 http://www.python.org/ns/ elem1
109132 elem2
110133
134+ *intern *, if given, must be a dictionary.
135+ It is used to intern the names of elements and attributes,
136+ and is available as the :attr: `~xmlparser.intern ` attribute.
137+ By default a new empty dictionary is created for every parser.
138+
111139 Due to limitations in the ``Expat `` library used by :mod: `pyexpat `,
112140 the :class: `xmlparser ` instance returned can only be used to parse a single
113141 XML document. Call ``ParserCreate `` for each document to provide unique
@@ -130,18 +158,24 @@ XMLParser Objects
130158
131159.. method :: xmlparser.Parse(data[, isfinal])
132160
133- Parses the contents of the string *data *, calling the appropriate handler
134- functions to process the parsed data. *isfinal * must be true on the final call
135- to this method; it allows the parsing of a single file in fragments,
161+ Parses the contents of *data *,
162+ calling the appropriate handler functions to process the parsed data.
163+ *data * can be a :term: `bytes-like object ` or a string.
164+ If it is a string, the encoding declaration in the XML data is ignored,
165+ and the data is parsed as already decoded text.
166+ *isfinal * must be true on the final call to this method;
167+ it allows the parsing of a single file in fragments,
136168 not the submission of multiple files.
137- *data * can be the empty string at any time.
169+ *data * can be empty at any time.
138170
139171
140172.. method :: xmlparser.ParseFile(file)
141173
142- Parse XML data reading from the object *file *. *file * only needs to provide
143- the ``read(nbytes) `` method, returning the empty string when there's no more
144- data.
174+ Parse XML data reading from the object *file *.
175+ *file * only needs to provide the ``read(nbytes) `` method,
176+ which returns bytes, and an empty bytes object when there's no more data.
177+ Text files are not supported;
178+ use :meth: `Parse ` for data which is already decoded.
145179
146180
147181.. method :: xmlparser.SetBase(base)
@@ -161,9 +195,15 @@ XMLParser Objects
161195
162196.. method :: xmlparser.GetInputContext()
163197
164- Returns the input data that generated the current event as a string. The data is
165- in the encoding of the entity which contains the text. When called while an
166- event handler is not active, the return value is ``None ``.
198+ Returns the input data which generated the current event
199+ as a :class: `bytes ` object.
200+ The data is in the encoding of the entity which contains the text.
201+ It extends to the end of the currently buffered input,
202+ therefore it can contain also the data of the following events,
203+ and if the event was generated by a large amount of text,
204+ not all of it may be available.
205+ When called while an event handler is not active,
206+ the return value is ``None ``.
167207
168208
169209.. method :: xmlparser.ExternalEntityParserCreate(context[, encoding])
@@ -410,6 +450,22 @@ against some common XML vulnerabilities.
410450 default, this attribute is false; it may be changed at any time.
411451
412452
453+ .. attribute :: xmlparser.intern
454+
455+ The dictionary used to intern the names of elements and attributes.
456+ It is either the dictionary passed as the *intern * argument
457+ of :func: `ParserCreate `, or a new dictionary created for this parser.
458+
459+
460+ .. attribute :: xmlparser.namespace_prefixes
461+
462+ If set to a true value, and namespace processing is enabled,
463+ the namespace prefix is reported as the third part of the expanded name,
464+ separated by the namespace separator.
465+ Names which have no prefix are not affected.
466+ By default, this attribute is false; it may be changed at any time.
467+
468+
413469The following attributes contain values relating to the most recent error
414470encountered by an :class: `xmlparser ` object, and will only have correct values
415471once a call to :meth: `Parse ` or :meth: `ParseFile ` has raised an
@@ -473,8 +529,7 @@ otherwise stated.
473529 encoding of the document text, and an optional "standalone" declaration.
474530 *version * and *encoding * will be strings, and *standalone * will be ``1 `` if the
475531 document is declared standalone, ``0 `` if it is declared not to be standalone,
476- or ``-1 `` if the standalone clause was omitted. This is only available with
477- Expat version 1.95.0 or newer.
532+ or ``-1 `` if the standalone clause was omitted.
478533
479534
480535.. method :: xmlparser.StartDoctypeDeclHandler(doctypeName, systemId, publicId, has_internal_subset)
@@ -483,14 +538,12 @@ otherwise stated.
483538 ... ``). The *doctypeName * is provided exactly as presented. The *systemId * and
484539 *publicId * parameters give the system and public identifiers if specified, or
485540 ``None `` if omitted. *has_internal_subset * will be true if the document
486- contains an internal document declaration subset. This requires Expat version
487- 1.2 or newer.
541+ contains an internal document declaration subset.
488542
489543
490544.. method :: xmlparser.EndDoctypeDeclHandler()
491545
492- Called when Expat is done parsing the document type declaration. This requires
493- Expat version 1.2 or newer.
546+ Called when Expat is done parsing the document type declaration.
494547
495548
496549.. method :: xmlparser.ElementDeclHandler(name, model)
@@ -505,12 +558,16 @@ otherwise stated.
505558 declaration declares three attributes, this handler is called three times, once
506559 for each attribute. *elname * is the name of the element to which the
507560 declaration applies and *attname * is the name of the attribute declared. The
508- attribute type is a string passed as *type *; the possible values are
509- ``'CDATA' ``, ``'ID' ``, ``'IDREF' ``, ... *default * gives the default value for
561+ The attribute type is a string passed as *type *:
562+ ``'CDATA' ``, ``'ID' ``, ``'IDREF' ``, ``'IDREFS' ``, ``'ENTITY' ``,
563+ ``'ENTITIES' ``, ``'NMTOKEN' `` or ``'NMTOKENS' ``,
564+ an enumeration like ``'(x|y)' ``,
565+ or a notation list like ``'NOTATION(n1|n2)' ``.
566+ *default * gives the default value for
510567 the attribute used when the attribute is not specified by the document instance,
511568 or ``None `` if there is no default value (``#IMPLIED `` values). If the
512569 attribute is required to be given in the document instance, *required * will be
513- true. This requires Expat version 1.95.0 or newer.
570+ true.
514571
515572
516573.. method :: xmlparser.StartElementHandler(name, attributes)
@@ -546,10 +603,10 @@ otherwise stated.
546603
547604.. method :: xmlparser.UnparsedEntityDeclHandler(entityName, base, systemId, publicId, notationName)
548605
549- Called for unparsed (NDATA) entity declarations. This is only present for
550- version 1.2 of the Expat library; for more recent versions, use
551- :attr: `EntityDeclHandler ` instead. (The underlying function in the Expat
552- library has been declared obsolete.)
606+ Called for unparsed (NDATA) entity declarations.
607+ If this handler is not set, such declarations are reported by
608+ :attr: `EntityDeclHandler `, which is preferred for new code.
609+ (The underlying function in the Expat library has been declared obsolete.)
553610
554611
555612.. method :: xmlparser.EntityDeclHandler(entityName, is_parameter_entity, value, base, systemId, publicId, notationName)
@@ -560,8 +617,7 @@ otherwise stated.
560617 ``None `` for parsed entities, and the name of the notation for unparsed
561618 entities. *is_parameter_entity * will be true if the entity is a parameter entity
562619 or false for general entities (most applications only need to be concerned with
563- general entities). This is only available starting with version 1.95.0 of the
564- Expat library.
620+ general entities).
565621
566622
567623.. method :: xmlparser.NotationDeclHandler(notationName, base, systemId, publicId)
@@ -615,7 +671,7 @@ otherwise stated.
615671
616672.. method :: xmlparser.DefaultHandlerExpand(data)
617673
618- This is the same as the :func : `DefaultHandler `, but doesn't inhibit expansion
674+ This is the same as the :attr : `DefaultHandler `, but doesn't inhibit expansion
619675 of internal entities. The entity reference will not be passed to the default
620676 handler.
621677
@@ -659,6 +715,16 @@ otherwise stated.
659715 :attr: `DefaultHandler ` callback, if provided.
660716
661717
718+ .. method :: xmlparser.SkippedEntityHandler(entityName, is_parameter_entity)
719+
720+ Called for entity references which are not expanded,
721+ because the parser did not read the declaration of the entity.
722+ This happens when the external DTD subset or an external parameter entity
723+ is not parsed.
724+ *is_parameter_entity * is true for a parameter entity
725+ and false for a general entity.
726+
727+
662728.. _expaterror-objects :
663729
664730ExpatError Exceptions
@@ -759,35 +825,35 @@ The constants in the model type group are:
759825
760826
761827.. data :: XML_CTYPE_ANY
762- :noindex:
763828
764829 The element named by the model name was declared to have a content model of
765830 ``ANY ``.
766831
767832
768833.. data :: XML_CTYPE_CHOICE
769- :noindex:
770834
771835 The named element allows a choice from a number of options; this is used for
772836 content models such as ``(A | B | C) ``.
773837
774838
775839.. data :: XML_CTYPE_EMPTY
776- :noindex:
777840
778841 Elements which are declared to be ``EMPTY `` have this model type.
779842
780843
781844.. data :: XML_CTYPE_MIXED
782- :noindex:
845+
846+ The named element allows character data, optionally interspersed with
847+ the named children; this is used for content models such as
848+ ``(#PCDATA) `` and ``(#PCDATA | A | B)* ``.
783849
784850
785851.. data :: XML_CTYPE_NAME
786- :noindex:
852+
853+ The model names a single element, as for ``A ``.
787854
788855
789856.. data :: XML_CTYPE_SEQ
790- :noindex:
791857
792858 Models which represent a series of models which follow one after the other are
793859 indicated with this model type. This is used for models such as ``(A, B, C) ``.
@@ -796,25 +862,21 @@ The constants in the quantifier group are:
796862
797863
798864.. data :: XML_CQUANT_NONE
799- :noindex:
800865
801866 No modifier is given, so it can appear exactly once, as for ``A ``.
802867
803868
804869.. data :: XML_CQUANT_OPT
805- :noindex:
806870
807871 The model is optional: it can appear once or not at all, as for ``A? ``.
808872
809873
810874.. data :: XML_CQUANT_PLUS
811- :noindex:
812875
813876 The model must occur one or more times (like ``A+ ``).
814877
815878
816879.. data :: XML_CQUANT_REP
817- :noindex:
818880
819881 The model must occur zero or more times, as for ``A* ``.
820882
@@ -898,7 +960,7 @@ The ``errors`` module has the following attributes:
898960.. data :: XML_ERROR_NO_ELEMENTS
899961
900962 The document contains no elements (XML requires all documents to contain exactly
901- one top-level element)..
963+ one top-level element).
902964
903965
904966.. data :: XML_ERROR_NO_MEMORY
@@ -1001,7 +1063,7 @@ The ``errors`` module has the following attributes:
10011063
10021064.. data :: XML_ERROR_XML_DECL
10031065
1004- The document contained no document element at all .
1066+ There was an error parsing the XML declaration .
10051067
10061068
10071069.. data :: XML_ERROR_TEXT_DECL
0 commit comments