Skip to content

Commit e16d235

Browse files
serhiy-storchakamiss-islington
authored andcommitted
gh-156829: Fix inaccuracies in the xml.sax documentation (GH-156830)
Corrected: * the name of the errorHandler argument of parse() and parseString(); * the default value of the parser_list argument of make_parser(); * the description of the source of parse(); * the types of the declaration-handler and dom-node properties; * the way in which names are interned; * the description of IncrementalParser. Documented: * default_parser_list and SAXReaderNotAvailable; * property_encoding and property_interning_dict; * IncrementalParser.prepareParser(); * XMLFilterBase.getParent() and setParent(); * which features and properties the parser based on Expat does not support. Added missing docstrings to parse(), parseString(), AttributesImpl and XMLGenerator. (cherry picked from commit 6f7941e) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent e9585e2 commit e16d235

7 files changed

Lines changed: 98 additions & 13 deletions

File tree

Doc/library/xml.sax.handler.rst

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,15 @@ for the feature and property names.
7272
optionally do not report original prefixed names (default).
7373
| access: (parsing) read-only; (not parsing) read/write
7474
75+
The parser based on :mod:`xml.parsers.expat` does not support this feature.
76+
7577

7678
.. data:: feature_string_interning
7779

7880
| value: ``"http://xml.org/sax/features/string-interning"``
7981
| true: All element names, prefixes, attribute names, Namespace URIs, and
80-
local names are interned using the built-in intern function.
82+
local names are interned in a dictionary
83+
(see :data:`property_interning_dict`).
8184
| false: Names are not necessarily interned, although they may be (default).
8285
| access: (parsing) read-only; (not parsing) read/write
8386
@@ -90,6 +93,9 @@ for the feature and property names.
9093
| false: Do not report validation errors.
9194
| access: (parsing) read-only; (not parsing) read/write
9295
96+
The parser based on :mod:`xml.parsers.expat` does not support this feature,
97+
because Expat is a non-validating parser.
98+
9399

94100
.. data:: feature_external_ges
95101

@@ -116,6 +122,8 @@ for the feature and property names.
116122
DTD subset.
117123
| access: (parsing) read-only; (not parsing) read/write
118124
125+
The parser based on :mod:`xml.parsers.expat` does not support this feature.
126+
119127

120128
.. data:: all_features
121129

@@ -125,7 +133,7 @@ for the feature and property names.
125133
.. data:: property_lexical_handler
126134

127135
| value: ``"http://xml.org/sax/properties/lexical-handler"``
128-
| data type: xml.sax.handler.LexicalHandler (not supported in Python 2)
136+
| data type: :class:`~xml.sax.handler.LexicalHandler`
129137
| description: An optional extension handler for lexical events like
130138
comments.
131139
| access: read/write
@@ -134,28 +142,54 @@ for the feature and property names.
134142
.. data:: property_declaration_handler
135143

136144
| value: ``"http://xml.org/sax/properties/declaration-handler"``
137-
| data type: xml.sax.sax2lib.DeclHandler (not supported in Python 2)
145+
| data type: an object implementing the SAX2 ``DeclHandler`` interface
138146
| description: An optional extension handler for DTD-related events other
139147
than notations and unparsed entities.
140148
| access: read/write
141149
150+
No parser in the standard library supports this property,
151+
and the standard library provides no such handler.
152+
142153

143154
.. data:: property_dom_node
144155

145156
| value: ``"http://xml.org/sax/properties/dom-node"``
146-
| data type: org.w3c.dom.Node (not supported in Python 2)
157+
| data type: :class:`xml.dom.Node`
147158
| description: When parsing, the current DOM node being visited if this is
148159
a DOM iterator; when not parsing, the root DOM node for iteration.
149160
| access: (parsing) read-only; (not parsing) read/write
150161
162+
No parser in the standard library supports this property.
163+
151164

152165
.. data:: property_xml_string
153166

154167
| value: ``"http://xml.org/sax/properties/xml-string"``
155168
| data type: Bytes
156169
| description: The literal string of characters that was the source for the
157170
current event.
158-
| access: read-only
171+
| access: read-only, and only during a handler callback
172+
173+
174+
.. data:: property_encoding
175+
176+
| value: ``"http://www.python.org/sax/properties/encoding"``
177+
| data type: String
178+
| description: The name of the encoding to assume for input data.
179+
| access: read/write
180+
181+
No parser in the standard library supports this property.
182+
183+
184+
.. data:: property_interning_dict
185+
186+
| value: ``"http://www.python.org/sax/properties/interning-dict"``
187+
| data type: Dictionary
188+
| description: The dictionary used to intern names,
189+
or ``None`` if names are not interned.
190+
Setting it enables interning, as does the
191+
:data:`feature_string_interning` feature.
192+
| access: read/write
159193
160194

161195
.. data:: all_properties

Doc/library/xml.sax.reader.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,15 @@ Instances of :class:`IncrementalParser` offer the following additional methods:
216216
allocated during parsing.
217217

218218

219+
.. method:: IncrementalParser.prepareParser(source)
220+
221+
Prepare the parser for parsing *source*, an
222+
:class:`InputSource` instance.
223+
It is called by :meth:`~XMLReader.parse` before feeding the data.
224+
The parser implementation must override this method;
225+
the default implementation raises :exc:`NotImplementedError`.
226+
227+
219228
.. method:: IncrementalParser.reset()
220229

221230
This method is called after close has been called to reset the parser so that it

Doc/library/xml.sax.rst

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ the SAX API.
2828
:meth:`~xml.sax.xmlreader.XMLReader.setFeature` on the parser object
2929
and argument :data:`~xml.sax.handler.feature_external_ges`.
3030

31-
The convenience functions are:
31+
The convenience functions and data are:
3232

3333

34-
.. function:: make_parser(parser_list=[])
34+
.. function:: make_parser(parser_list=())
3535

3636
Create and return a SAX :class:`~xml.sax.xmlreader.XMLReader` object. The
3737
first parser found will
@@ -43,18 +43,23 @@ The convenience functions are:
4343
The *parser_list* argument can be any iterable, not just a list.
4444

4545

46-
.. function:: parse(filename_or_stream, handler, error_handler=handler.ErrorHandler())
46+
.. function:: parse(filename_or_stream, handler, errorHandler=handler.ErrorHandler())
4747

4848
Create a SAX parser and use it to parse a document. The document, passed in as
49-
*filename_or_stream*, can be a filename or a file object. The *handler*
49+
*filename_or_stream*, can be a system identifier (a string identifying the
50+
input source -- typically a file name or a URL),
51+
a :term:`path-like <path-like object>` object, or a file object.
52+
A system identifier which does not refer to an existing file
53+
is opened with :func:`urllib.request.urlopen`.
54+
The *handler*
5055
parameter needs to be a SAX :class:`~handler.ContentHandler` instance. If
51-
*error_handler* is given, it must be a SAX :class:`~handler.ErrorHandler`
56+
*errorHandler* is given, it must be a SAX :class:`~handler.ErrorHandler`
5257
instance; if
5358
omitted, :exc:`SAXParseException` will be raised on all errors. There is no
5459
return value; all work must be done by the *handler* passed in.
5560

5661

57-
.. function:: parseString(string, handler, error_handler=handler.ErrorHandler())
62+
.. function:: parseString(string, handler, errorHandler=handler.ErrorHandler())
5863

5964
Similar to :func:`parse`, but parses from a buffer *string* received as a
6065
parameter. *string* must be a :class:`str` instance or a
@@ -63,6 +68,15 @@ The convenience functions are:
6368
.. versionchanged:: 3.5
6469
Added support of :class:`str` instances.
6570

71+
72+
.. data:: default_parser_list
73+
74+
The list of the names of modules which are tried by :func:`make_parser`
75+
after the modules named in its *parser_list* argument.
76+
It contains ``'xml.sax.expatreader'``, or, if the
77+
:envvar:`!PY_SAX_PARSER` environment variable is set and the environment
78+
is not ignored, the comma-separated list of module names taken from it.
79+
6680
A typical SAX application uses three kinds of objects: readers, handlers and
6781
input sources. "Reader" in this context is another term for parser, i.e. some
6882
piece of code that reads the bytes or characters from the input source, and
@@ -135,6 +149,14 @@ classes.
135149
class for similar purposes.
136150

137151

152+
.. exception:: SAXReaderNotAvailable(msg, exception=None)
153+
154+
Subclass of :exc:`SAXNotSupportedException` raised when no parser is
155+
available. A parser module raises it when it is imported or during
156+
parsing if the parser it provides cannot be used, and :func:`make_parser`
157+
raises it if no module from the tried ones provides a usable parser.
158+
159+
138160
.. seealso::
139161

140162
`SAX: The Simple API for XML <http://www.saxproject.org/>`_

Doc/library/xml.sax.utils.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ or as base classes.
8181
override specific methods to modify the event stream or the configuration
8282
requests as they pass through.
8383

84+
.. method:: getParent()
85+
86+
Return the parent reader, or ``None`` if it is not set.
87+
88+
89+
.. method:: setParent(parent)
90+
91+
Set the parent reader, which the events are read from.
92+
8493

8594
.. function:: prepare_input_source(source, base='')
8695

Lib/xml/sax/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,21 @@
2727

2828

2929
def parse(source, handler, errorHandler=ErrorHandler()):
30+
"""Parse an XML document with the default parser.
31+
32+
source is a system identifier, a path-like object or a file object,
33+
handler is a ContentHandler instance, and errorHandler is an
34+
ErrorHandler instance. All work is done by the handler."""
3035
parser = make_parser()
3136
parser.setContentHandler(handler)
3237
parser.setErrorHandler(errorHandler)
3338
parser.parse(source)
3439

3540
def parseString(string, handler, errorHandler=ErrorHandler()):
41+
"""Parse an XML document from a string with the default parser.
42+
43+
string is a str or a bytes-like object, the other arguments are the
44+
same as for parse()."""
3645
import io
3746
if errorHandler is None:
3847
errorHandler = ErrorHandler()

Lib/xml/sax/saxutils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ def __getattr__(self, name):
110110
write_through=True)
111111

112112
class XMLGenerator(handler.ContentHandler):
113+
"""Content handler which writes the events back as an XML document."""
113114

114115
def __init__(self, out=None, encoding="iso-8859-1", short_empty_elements=False):
115116
handler.ContentHandler.__init__(self)

Lib/xml/sax/xmlreader.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def setProperty(self, name, value):
8989
raise SAXNotRecognizedException("Property '%s' not recognized" % name)
9090

9191
class IncrementalParser(XMLReader):
92-
"""This interface adds three extra methods to the XMLReader
92+
"""This interface adds four extra methods to the XMLReader
9393
interface that allow XML parsers to support incremental
9494
parsing. Support for this interface is optional, since not all
9595
underlying XML parsers support this functionality.
@@ -104,7 +104,7 @@ class IncrementalParser(XMLReader):
104104
is, after parse has been called and before it returns.
105105
106106
By default, the class also implements the parse method of the XMLReader
107-
interface using the feed, close and reset methods of the
107+
interface using the prepareParser, feed and close methods of the
108108
IncrementalParser interface as a convenience to SAX 2.0 driver
109109
writers."""
110110

@@ -274,6 +274,7 @@ def getCharacterStream(self):
274274
# ===== ATTRIBUTESIMPL =====
275275

276276
class AttributesImpl:
277+
"""Implementation of the Attributes interface."""
277278

278279
def __init__(self, attrs):
279280
"""Non-NS-aware implementation.

0 commit comments

Comments
 (0)