Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a6dc531

Browse files
andresdelfinotaleinat
authored andcommitted
bpo-34789: make xml.sax.make_parser accept iterables of all types (pythonGH-9576)
1 parent 10cb376 commit a6dc531

4 files changed

Lines changed: 38 additions & 5 deletions

File tree

Doc/library/xml.sax.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ The convenience functions are:
4040

4141
Create and return a SAX :class:`~xml.sax.xmlreader.XMLReader` object. The
4242
first parser found will
43-
be used. If *parser_list* is provided, it must be a sequence of strings which
43+
be used. If *parser_list* is provided, it must be an iterable of strings which
4444
name modules that have a function named :func:`create_parser`. Modules listed
4545
in *parser_list* will be used before modules in the default list of parsers.
4646

47+
.. versionchanged:: 3.8
48+
The *parser_list* argument can be any iterable, not just a list.
49+
4750

4851
.. function:: parse(filename_or_stream, handler, error_handler=handler.ErrorHandler())
4952

Lib/test/test_sax.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,34 @@ def test_make_parser2(self):
254254
from xml.sax import make_parser
255255
p = make_parser()
256256

257+
def test_make_parser3(self):
258+
# Testing that make_parser can handle different types of
259+
# iterables.
260+
make_parser(['module'])
261+
make_parser(('module', ))
262+
make_parser({'module'})
263+
make_parser(frozenset({'module'}))
264+
make_parser({'module': None})
265+
make_parser(iter(['module']))
266+
267+
def test_make_parser4(self):
268+
# Testing that make_parser can handle empty iterables.
269+
make_parser([])
270+
make_parser(tuple())
271+
make_parser(set())
272+
make_parser(frozenset())
273+
make_parser({})
274+
make_parser(iter([]))
275+
276+
def test_make_parser5(self):
277+
# Testing that make_parser can handle iterables with more than
278+
# one item.
279+
make_parser(['module1', 'module2'])
280+
make_parser(('module1', 'module2'))
281+
make_parser({'module1', 'module2'})
282+
make_parser(frozenset({'module1', 'module2'}))
283+
make_parser({'module1': None, 'module2': None})
284+
make_parser(iter(['module1', 'module2']))
257285

258286
# ===========================================================================
259287
#

Lib/xml/sax/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ def parseString(string, handler, errorHandler=ErrorHandler()):
6767
default_parser_list = sys.registry.getProperty(_key).split(",")
6868

6969

70-
def make_parser(parser_list = []):
70+
def make_parser(parser_list=()):
7171
"""Creates and returns a SAX parser.
7272
7373
Creates the first parser it is able to instantiate of the ones
74-
given in the list created by doing parser_list +
75-
default_parser_list. The lists must contain the names of Python
74+
given in the iterable created by chaining parser_list and
75+
default_parser_list. The iterables must contain the names of Python
7676
modules containing both a SAX parser and a create_parser function."""
7777

78-
for parser_name in parser_list + default_parser_list:
78+
for parser_name in list(parser_list) + default_parser_list:
7979
try:
8080
return _create_parser(parser_name)
8181
except ImportError as e:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:func:`xml.sax.make_parser` now accepts any iterable as its *parser_list*
2+
argument. Patch by Andrés Delfino.

0 commit comments

Comments
 (0)