[Python-checkins] cpython (merge 3.2 -> default): Merge #10019: Fix regression relative to 2.6: add newlines if indent=0

r.david.murray python-checkins at python.org
Wed Apr 13 03:22:34 CEST 2011


http://hg.python.org/cpython/rev/2d0d0850335e
changeset:   69296:2d0d0850335e
parent:      69291:0e6359968b80
parent:      69295:fe8bbaff5a27
user:        R David Murray <rdmurray at bitdance.com>
date:        Tue Apr 12 21:19:20 2011 -0400
summary:
  Merge #10019: Fix regression relative to 2.6: add newlines if indent=0

Patch by Amaury Forgeot d'Arc, updated by Sando Tosi.

files:
  Doc/library/json.rst               |   8 ++++----
  Lib/json/encoder.py                |   2 +-
  Lib/test/json_tests/test_indent.py |  16 ++++++++++++++++
  Misc/NEWS                          |   3 +++
  4 files changed, 24 insertions(+), 5 deletions(-)


diff --git a/Doc/library/json.rst b/Doc/library/json.rst
--- a/Doc/library/json.rst
+++ b/Doc/library/json.rst
@@ -136,10 +136,10 @@
 
    If *indent* is a non-negative integer or string, then JSON array elements and
    object members will be pretty-printed with that indent level.  An indent level
-   of 0 or ``""`` will only insert newlines.  ``None`` (the default) selects the
-   most compact representation. Using an integer indent indents that many spaces
-   per level.  If *indent* is a string (such at '\t'), that string is used to indent
-   each level.
+   of 0, negative, or ``""`` will only insert newlines.  ``None`` (the default)
+   selects the most compact representation. Using a positive integer indent
+   indents that many spaces per level.  If *indent* is a string (such at '\t'),
+   that string is used to indent each level.
 
    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/test/json_tests/test_indent.py b/Lib/test/json_tests/test_indent.py
--- a/Lib/test/json_tests/test_indent.py
+++ b/Lib/test/json_tests/test_indent.py
@@ -2,6 +2,7 @@
 
 import json
 import textwrap
+from io import StringIO
 
 class TestIndent(TestCase):
     def test_indent(self):
@@ -43,3 +44,18 @@
         self.assertEqual(h3, h)
         self.assertEqual(d2, expect.expandtabs(2))
         self.assertEqual(d3, 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
@@ -103,6 +103,9 @@
 Library
 -------
 
+- Issue #10019: Fixed regression in json module where an indent of 0 stopped
+  adding newlines and acted instead like 'None'.
+
 - Issue #11186: pydoc ignores a module if its name contains a surrogate
   character in the index of modules.
 

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


More information about the Python-checkins mailing list