[Python-checkins] cpython (2.7): Issue #1470548: Do not buffer XMLGenerator output.

serhiy.storchaka python-checkins at python.org
Mon Feb 25 12:32:14 CET 2013


http://hg.python.org/cpython/rev/d707e3345a74
changeset:   82378:d707e3345a74
branch:      2.7
parent:      82375:df57314b93d1
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Mon Feb 25 13:31:29 2013 +0200
summary:
  Issue #1470548: Do not buffer XMLGenerator output.
Add test for fragment producing with XMLGenerator.

files:
  Lib/test/test_sax.py    |  15 +++++++++++++++
  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
--- a/Lib/test/test_sax.py
+++ b/Lib/test/test_sax.py
@@ -403,6 +403,21 @@
         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
--- a/Lib/xml/sax/saxutils.py
+++ b/Lib/xml/sax/saxutils.py
@@ -98,9 +98,13 @@
         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):
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list