changeset: 95432:fca669149d8a user: Serhiy Storchaka date: Sat Apr 04 10:12:26 2015 +0300 files: Doc/library/xml.sax.rst Lib/test/test_sax.py Lib/xml/sax/__init__.py Misc/NEWS description: Issue #10590: xml.sax.parseString() now supports string argument. diff -r 3eb3a6d45251 -r fca669149d8a Doc/library/xml.sax.rst --- a/Doc/library/xml.sax.rst Sat Apr 04 10:06:58 2015 +0300 +++ b/Doc/library/xml.sax.rst Sat Apr 04 10:12:26 2015 +0300 @@ -47,7 +47,11 @@ .. function:: parseString(string, handler, error_handler=handler.ErrorHandler()) 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 input sources. "Reader" in this context is another term for parser, i.e. some diff -r 3eb3a6d45251 -r fca669149d8a Lib/test/test_sax.py --- a/Lib/test/test_sax.py Sat Apr 04 10:06:58 2015 +0300 +++ b/Lib/test/test_sax.py Sat Apr 04 10:12:26 2015 +0300 @@ -200,6 +200,13 @@ parseString(s, XMLGenerator(result, '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): # UTF-8 is default encoding, US-ASCII is compatible with UTF-8, # UTF-16 is autodetected diff -r 3eb3a6d45251 -r fca669149d8a Lib/xml/sax/__init__.py --- a/Lib/xml/sax/__init__.py Sat Apr 04 10:06:58 2015 +0300 +++ b/Lib/xml/sax/__init__.py Sat Apr 04 10:12:26 2015 +0300 @@ -33,8 +33,7 @@ parser.parse(source) def parseString(string, handler, errorHandler=ErrorHandler()): - from io import BytesIO - + import io if errorHandler is None: errorHandler = ErrorHandler() parser = make_parser() @@ -42,7 +41,10 @@ parser.setErrorHandler(errorHandler) inpsrc = InputSource() - inpsrc.setByteStream(BytesIO(string)) + if isinstance(string, str): + inpsrc.setCharacterStream(io.StringIO(string)) + else: + inpsrc.setByteStream(io.BytesIO(string)) parser.parse(inpsrc) # this is the parser list used by the make_parser function if no diff -r 3eb3a6d45251 -r fca669149d8a Misc/NEWS --- a/Misc/NEWS Sat Apr 04 10:06:58 2015 +0300 +++ b/Misc/NEWS Sat Apr 04 10:12:26 2015 +0300 @@ -19,6 +19,8 @@ Library ------- +- Issue #10590: xml.sax.parseString() now supports string argument. + - Issue #23338: Fixed formatting ctypes error messages on Cygwin. Patch by Makoto Kato.