diff options
-rw-r--r-- | Lib/test/test_sax.py | 15 | ||||
-rw-r--r-- | Lib/xml/sax/saxutils.py | 10 |
2 files changed, 22 insertions, 3 deletions
diff --git a/Lib/test/test_sax.py b/Lib/test/test_sax.py index 825c16a..3a20eac 100644 --- a/Lib/test/test_sax.py +++ b/Lib/test/test_sax.py @@ -403,6 +403,21 @@ class XmlgenTest: func(result) self.assertFalse(result.closed) + def test_xmlgen_fragment(self): + result = self.ioclass() + gen = XMLGenerator(result) + + # Don't call gen.startDocument() + gen.startElement("foo", {"a": "1.0"}) + gen.characters("Hello") + gen.endElement("foo") + gen.startElement("bar", {"b": "2.0"}) + gen.endElement("bar") + # Don't call gen.endDocument() + + self.assertEqual(result.getvalue(), + '<foo a="1.0">Hello</foo><bar b="2.0"></bar>') + class StringXmlgenTest(XmlgenTest, unittest.TestCase): ioclass = StringIO diff --git a/Lib/xml/sax/saxutils.py b/Lib/xml/sax/saxutils.py index dad74f5..3d81a8e 100644 --- a/Lib/xml/sax/saxutils.py +++ b/Lib/xml/sax/saxutils.py @@ -98,9 +98,13 @@ def _gettextwriter(out, encoding): except AttributeError: pass # wrap a binary writer with TextIOWrapper - return io.TextIOWrapper(buffer, encoding=encoding, - errors='xmlcharrefreplace', - newline='\n') + class UnbufferedTextIOWrapper(io.TextIOWrapper): + def write(self, s): + super(UnbufferedTextIOWrapper, self).write(s) + self.flush() + return UnbufferedTextIOWrapper(buffer, encoding=encoding, + errors='xmlcharrefreplace', + newline='\n') class XMLGenerator(handler.ContentHandler): |