[Python-checkins] cpython (merge 3.1 -> 3.1): Merge updates

senthil.kumaran python-checkins at python.org
Wed Apr 13 03:43:36 CEST 2011


http://hg.python.org/cpython/rev/29868f95519d
changeset:   69303:29868f95519d
branch:      3.1
parent:      69300:8ee48ec69844
parent:      69294:4a1048257995
user:        Senthil Kumaran <orsenthil at gmail.com>
date:        Wed Apr 13 09:35:37 2011 +0800
summary:
  Merge updates

files:
  Doc/library/json.rst          |   6 +++---
  Lib/json/encoder.py           |   2 +-
  Lib/json/tests/test_indent.py |  16 ++++++++++++++++
  Misc/NEWS                     |   3 +++
  4 files changed, 23 insertions(+), 4 deletions(-)


diff --git a/Doc/library/json.rst b/Doc/library/json.rst
--- a/Doc/library/json.rst
+++ b/Doc/library/json.rst
@@ -135,9 +135,9 @@
    using the JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
 
    If *indent* is a non-negative integer, then JSON array elements and object
-   members will be pretty-printed with that indent level.  An indent level of 0
-   will only insert newlines.  ``None`` (the default) selects the most compact
-   representation.
+   members will be pretty-printed with that indent level.  An indent level of 0,
+   or negative, will only insert newlines.  ``None`` (the default) selects the
+   most compact representation.
 
    If *separators* is an ``(item_separator, dict_separator)`` tuple, then it
    will be used instead of the default ``(', ', ': ')`` separators.  ``(',',
diff --git a/Lib/json/encoder.py b/Lib/json/encoder.py
--- a/Lib/json/encoder.py
+++ b/Lib/json/encoder.py
@@ -233,7 +233,7 @@
 
 
         if (_one_shot and c_make_encoder is not None
-                and not self.indent):
+                and self.indent is None):
             _iterencode = c_make_encoder(
                 markers, self.default, _encoder, self.indent,
                 self.key_separator, self.item_separator, self.sort_keys,
diff --git a/Lib/json/tests/test_indent.py b/Lib/json/tests/test_indent.py
--- a/Lib/json/tests/test_indent.py
+++ b/Lib/json/tests/test_indent.py
@@ -2,6 +2,7 @@
 
 import json
 import textwrap
+from io import StringIO
 
 class TestIndent(TestCase):
     def test_indent(self):
@@ -39,3 +40,18 @@
         self.assertEqual(h1, h)
         self.assertEqual(h2, h)
         self.assertEqual(d2, expect)
+
+    def test_indent0(self):
+        h = {3: 1}
+        def check(indent, expected):
+            d1 = json.dumps(h, indent=indent)
+            self.assertEqual(d1, expected)
+
+            sio = StringIO()
+            json.dump(h, sio, indent=indent)
+            self.assertEqual(sio.getvalue(), expected)
+
+        # indent=0 should emit newlines
+        check(0, '{\n"3": 1\n}')
+        # indent=None is more compact
+        check(None, '{"3": 1}')
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -53,6 +53,9 @@
 
 - Issue #11703 - urllib2.geturl() does not return correct url when the original
   url contains #fragment.
+  
+- Issue #10019: Fixed regression in json module where an indent of 0 stopped
+  adding newlines and acted instead like 'None'.
 
 - Issue #5162: Treat services like frozen executables to allow child spawning
   from multiprocessing.forking on Windows.

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


More information about the Python-checkins mailing list