mirror of
https://github.com/python/cpython.git
synced 2025-07-25 12:14:38 +00:00
Issue #10590: xml.sax.parseString() now supports string argument.
This commit is contained in:
parent
f8aa133cce
commit
778db289b5
4 changed files with 19 additions and 4 deletions
|
@ -47,7 +47,11 @@ The convenience functions are:
|
||||||
.. function:: parseString(string, handler, error_handler=handler.ErrorHandler())
|
.. function:: parseString(string, handler, error_handler=handler.ErrorHandler())
|
||||||
|
|
||||||
Similar to :func:`parse`, but parses from a buffer *string* received as a
|
Similar to :func:`parse`, but parses from a buffer *string* received as a
|
||||||
parameter.
|
parameter. *string* must be a :class:`str` instance or a
|
||||||
|
:term:`bytes-like object`.
|
||||||
|
|
||||||
|
.. versionchanged:: 3.5
|
||||||
|
Added support of :class:`str` instances.
|
||||||
|
|
||||||
A typical SAX application uses three kinds of objects: readers, handlers and
|
A typical SAX application uses three kinds of objects: readers, handlers and
|
||||||
input sources. "Reader" in this context is another term for parser, i.e. some
|
input sources. "Reader" in this context is another term for parser, i.e. some
|
||||||
|
|
|
@ -200,6 +200,13 @@ class ParseTest(unittest.TestCase):
|
||||||
parseString(s, XMLGenerator(result, 'utf-8'))
|
parseString(s, XMLGenerator(result, 'utf-8'))
|
||||||
self.assertEqual(result.getvalue(), xml_str(self.data, 'utf-8'))
|
self.assertEqual(result.getvalue(), xml_str(self.data, 'utf-8'))
|
||||||
|
|
||||||
|
def test_parseString_text(self):
|
||||||
|
encodings = ('us-ascii', 'iso-8859-1', 'utf-8',
|
||||||
|
'utf-16', 'utf-16le', 'utf-16be')
|
||||||
|
for encoding in encodings:
|
||||||
|
self.check_parseString(xml_str(self.data, encoding))
|
||||||
|
self.check_parseString(self.data)
|
||||||
|
|
||||||
def test_parseString_bytes(self):
|
def test_parseString_bytes(self):
|
||||||
# UTF-8 is default encoding, US-ASCII is compatible with UTF-8,
|
# UTF-8 is default encoding, US-ASCII is compatible with UTF-8,
|
||||||
# UTF-16 is autodetected
|
# UTF-16 is autodetected
|
||||||
|
|
|
@ -33,8 +33,7 @@ def parse(source, handler, errorHandler=ErrorHandler()):
|
||||||
parser.parse(source)
|
parser.parse(source)
|
||||||
|
|
||||||
def parseString(string, handler, errorHandler=ErrorHandler()):
|
def parseString(string, handler, errorHandler=ErrorHandler()):
|
||||||
from io import BytesIO
|
import io
|
||||||
|
|
||||||
if errorHandler is None:
|
if errorHandler is None:
|
||||||
errorHandler = ErrorHandler()
|
errorHandler = ErrorHandler()
|
||||||
parser = make_parser()
|
parser = make_parser()
|
||||||
|
@ -42,7 +41,10 @@ def parseString(string, handler, errorHandler=ErrorHandler()):
|
||||||
parser.setErrorHandler(errorHandler)
|
parser.setErrorHandler(errorHandler)
|
||||||
|
|
||||||
inpsrc = InputSource()
|
inpsrc = InputSource()
|
||||||
inpsrc.setByteStream(BytesIO(string))
|
if isinstance(string, str):
|
||||||
|
inpsrc.setCharacterStream(io.StringIO(string))
|
||||||
|
else:
|
||||||
|
inpsrc.setByteStream(io.BytesIO(string))
|
||||||
parser.parse(inpsrc)
|
parser.parse(inpsrc)
|
||||||
|
|
||||||
# this is the parser list used by the make_parser function if no
|
# this is the parser list used by the make_parser function if no
|
||||||
|
|
|
@ -19,6 +19,8 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #10590: xml.sax.parseString() now supports string argument.
|
||||||
|
|
||||||
- Issue #23338: Fixed formatting ctypes error messages on Cygwin.
|
- Issue #23338: Fixed formatting ctypes error messages on Cygwin.
|
||||||
Patch by Makoto Kato.
|
Patch by Makoto Kato.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue