Skip to content

Commit e2f2cd0

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 e503114 commit e2f2cd0

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
@@ -75,12 +75,15 @@ for the feature and property names.
7575
optionally do not report original prefixed names (default).
7676
| access: (parsing) read-only; (not parsing) read/write
7777
78+
The parser based on :mod:`xml.parsers.expat` does not support this feature.
79+
7880

7981
.. data:: feature_string_interning
8082

8183
| value: ``"http://xml.org/sax/features/string-interning"``
8284
| true: All element names, prefixes, attribute names, Namespace URIs, and
83-
local names are interned using the built-in intern function.
85+
local names are interned in a dictionary
86+
(see :data:`property_interning_dict`).
8487
| false: Names are not necessarily interned, although they may be (default).
8588
| access: (parsing) read-only; (not parsing) read/write
8689
@@ -93,6 +96,9 @@ for the feature and property names.
9396
| false: Do not report validation errors.
9497
| access: (parsing) read-only; (not parsing) read/write
9598
99+
The parser based on :mod:`xml.parsers.expat` does not support this feature,
100+
because Expat is a non-validating parser.
101+
96102

97103
.. data:: feature_external_ges
98104

@@ -119,6 +125,8 @@ for the feature and property names.
119125
DTD subset.
120126
| access: (parsing) read-only; (not parsing) read/write
121127
128+
The parser based on :mod:`xml.parsers.expat` does not support this feature.
129+
122130

123131
.. data:: all_features
124132

@@ -128,7 +136,7 @@ for the feature and property names.
128136
.. data:: property_lexical_handler
129137

130138
| value: ``"http://xml.org/sax/properties/lexical-handler"``
131-
| data type: xml.sax.handler.LexicalHandler (not supported in Python 2)
139+
| data type: :class:`~xml.sax.handler.LexicalHandler`
132140
| description: An optional extension handler for lexical events like
133141
comments.
134142
| access: read/write
@@ -137,28 +145,54 @@ for the feature and property names.
137145
.. data:: property_declaration_handler
138146

139147
| value: ``"http://xml.org/sax/properties/declaration-handler"``
140-
| data type: xml.sax.sax2lib.DeclHandler (not supported in Python 2)
148+
| data type: an object implementing the SAX2 ``DeclHandler`` interface
141149
| description: An optional extension handler for DTD-related events other
142150
than notations and unparsed entities.
143151
| access: read/write
144152
153+
No parser in the standard library supports this property,
154+
and the standard library provides no such handler.
155+
145156

146157
.. data:: property_dom_node
147158

148159
| value: ``"http://xml.org/sax/properties/dom-node"``
149-
| data type: org.w3c.dom.Node (not supported in Python 2)
160+
| data type: :class:`xml.dom.Node`
150161
| description: When parsing, the current DOM node being visited if this is
151162
a DOM iterator; when not parsing, the root DOM node for iteration.
152163
| access: (parsing) read-only; (not parsing) read/write
153164
165+
No parser in the standard library supports this property.
166+
154167

155168
.. data:: property_xml_string
156169

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

164198
.. data:: all_properties

Doc/library/xml.sax.reader.rst

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

221221

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

224233
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
@@ -32,10 +32,10 @@ the SAX API.
3232
:meth:`~xml.sax.xmlreader.XMLReader.setFeature` on the parser object
3333
and argument :data:`~xml.sax.handler.feature_external_ges`.
3434

35-
The convenience functions are:
35+
The convenience functions and data are:
3636

3737

38-
.. function:: make_parser(parser_list=[])
38+
.. function:: make_parser(parser_list=())
3939

4040
Create and return a SAX :class:`~xml.sax.xmlreader.XMLReader` object. The
4141
first parser found will
@@ -47,18 +47,23 @@ The convenience functions are:
4747
The *parser_list* argument can be any iterable, not just a list.
4848

4949

50-
.. function:: parse(filename_or_stream, handler, error_handler=handler.ErrorHandler())
50+
.. function:: parse(filename_or_stream, handler, errorHandler=handler.ErrorHandler())
5151

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

6065

61-
.. function:: parseString(string, handler, error_handler=handler.ErrorHandler())
66+
.. function:: parseString(string, handler, errorHandler=handler.ErrorHandler())
6267

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

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

141155

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

144166
`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
@@ -84,6 +84,15 @@ or as base classes.
8484
override specific methods to modify the event stream or the configuration
8585
requests as they pass through.
8686

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

8897
.. function:: prepare_input_source(source, base='')
8998

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)