[Patches] [ python-Patches-1713041 ] fix for 1712742: corrects pprint's handling of 'depth'

SourceForge.net noreply at sourceforge.net
Thu May 10 13:56:51 CEST 2007


Patches item #1713041, was opened at 2007-05-04 19:13
Message generated for change (Comment added) made by josm
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1713041&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Library (Lib)
Group: Python 2.6
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Raghuram Devarakonda (draghuram)
Assigned to: Nobody/Anonymous (nobody)
Summary: fix for 1712742: corrects pprint's handling of 'depth'

Initial Comment:

The patch is really simple. Just change the way "maxlevels" is checked. I couldn't find a way to have a test case for this bug.



----------------------------------------------------------------------

Comment By: jos (josm)
Date: 2007-05-10 11:56

Message:
Logged In: YES 
user_id=1776568
Originator: NO

I agree. The code and the doc should be consistent.

New test would be

Index: Lib/test/test_pprint.py
===================================================================
--- Lib/test/test_pprint.py	(revision 55223)
+++ Lib/test/test_pprint.py	(working copy)
@@ -195,7 +195,27 @@
  others.should.not.be: like.this}"""
         self.assertEqual(DottedPrettyPrinter().pformat(o), exp)
 
+    def test_depth(self):
+        nested_tuple = (1, (2, (3, (4, (5, 6)))))
+        nested_dict = {1: {2: {3: {4: {5: {6: 6}}}}}}
+        nested_list = [1, [2, [3, [4, [5, [6, []]]]]]]
 
+        self.assertEqual(pprint.pformat(nested_tuple),
repr(nested_tuple))
+        self.assertEqual(pprint.pformat(nested_dict), repr(nested_dict))
+        self.assertEqual(pprint.pformat(nested_list), repr(nested_list))
+
+        result_tuple = {'lv1': '(...)',
+                        'lv2': '(1, (...))'}
+        result_dict = {'lv1': '{...}',
+                       'lv2': '{1: {...}}'}
+        result_list = {'lv1': '[...]',
+                       'lv2': '[1, [...]]'}
+        for i in [1, 2]:
+            key = 'lv' + `i`
+            self.assertEqual(pprint.pformat(nested_tuple, depth=i),
result_tuple[key])
+            self.assertEqual(pprint.pformat(nested_dict, depth=i),
result_dict[key])
+            self.assertEqual(pprint.pformat(nested_list, depth=i),
result_list[key])
+
 class DottedPrettyPrinter(pprint.PrettyPrinter):
 
     def format(self, object, context, maxlevels, level):


----------------------------------------------------------------------

Comment By: Neal Norwitz (nnorwitz)
Date: 2007-05-10 06:18

Message:
Logged In: YES 
user_id=33168
Originator: NO

Yes, that's the example I'm referring to.  In the doc, it shows 5 numbers
and one ... for 6.  Without your patch it shows 7 numbers and with your
patch it shows 6 numbers.  So even with your patch the doc and code don't
agree (they are off by one, rather than 2 as is currently the case).

----------------------------------------------------------------------

Comment By: Raghuram Devarakonda (draghuram)
Date: 2007-05-09 14:44

Message:
Logged In: YES 
user_id=984087
Originator: YES


Hi Neal,

I assume you are referring to the example 

>>> import parser
>>> tup = parser.ast2tuple(
...     parser.suite(open('pprint.py').read()))[1][1][1]
>>> pp = pprint.PrettyPrinter(depth=6)
>>> pp.pprint(tup)

in the document. Is that correct? I ran this example with and without my
patch. Without the update, the example printed 7 levels which is one level
too deep. With the patch, it printed 6 levels, which seems correct to me.

# without patch. prints 7 levels.
$ ../python/python testdoc.py 
(268, (269, (326, (303, (304, (305, (306, (...))))))))

# with patch. prints 6 levels.
$ ../python/python testdoc.py 
(268, (269, (326, (303, (304, (305, (...)))))))

I am attaching the file testdoc.py which contains the doc example. I just
wanted to confirm that this is what you are referring to.




File Added: testdoc.py

----------------------------------------------------------------------

Comment By: Neal Norwitz (nnorwitz)
Date: 2007-05-09 06:56

Message:
Logged In: YES 
user_id=33168
Originator: NO

I notice in the doc an example which doesn't work with this patch.  It
still prints one level too deep.  The doc seems correct to me, but I don't
have strong feelings any way.  The attached patch makes the doc example
work as expected.  The doc should really be updated with an example more
like:

>>> pp = pprint.PrettyPrinter(depth=6)
>>> pp.pprint((1, (2, (3, (4, (5, (6, 7)))))))
(1, (2, (3, (4, (5, (...))))))
>>> pp = pprint.PrettyPrinter(depth=1)
>>> pp.pprint(1)
1
>>> pp.pprint([1])
[...]

The updated patch causes the new tests to fail.  Could you update the
test/code/doc to all be consistent?
File Added: pp2.diff

----------------------------------------------------------------------

Comment By: Raghuram Devarakonda (draghuram)
Date: 2007-05-07 14:49

Message:
Logged In: YES 
user_id=984087
Originator: YES


josm, thanks for the test case. I incorporated it into the patch. BTW, I
changed "str" to "repr" as I think "repr" is closer to what pformat does.
File Added: pprint.patch

----------------------------------------------------------------------

Comment By: jos (josm)
Date: 2007-05-05 09:02

Message:
Logged In: YES 
user_id=1776568
Originator: NO

Your patch looks good and worked fine.

Wouldn't it be nice to add a test case for this problem to
test_pprint.py?
The following is just draft patch to add the test case.

Index: Lib/test/test_pprint.py
===================================================================
--- Lib/test/test_pprint.py	(revision 55144)
+++ Lib/test/test_pprint.py	(working copy)
@@ -195,7 +195,22 @@
  others.should.not.be: like.this}"""
         self.assertEqual(DottedPrettyPrinter().pformat(o), exp)
 
+    def test_depth(self):
+        nested_tuple = (1, (2, (3, (4, (5, 6)))))
+        nested_dict = {1: {2: {3: {4: {5: {6: 6}}}}}}
+        nested_list = [1, [2, [3, [4, [5, [6, []]]]]]]
 
+        self.assertEqual(pprint.pformat(nested_tuple),
str(nested_tuple))
+        self.assertEqual(pprint.pformat(nested_dict), str(nested_dict))
+        self.assertEqual(pprint.pformat(nested_list), str(nested_list))
+
+        lv1_tuple = '(1, (...))'
+        lv1_dict = '{1: {...}}'
+        lv1_list = '[1, [...]]'
+        self.assertEqual(pprint.pformat(nested_tuple, depth=1),
lv1_tuple)
+        self.assertEqual(pprint.pformat(nested_dict, depth=1), lv1_dict)
+        self.assertEqual(pprint.pformat(nested_list, depth=1), lv1_list)
+


----------------------------------------------------------------------

Comment By: Raghuram Devarakonda (draghuram)
Date: 2007-05-04 19:15

Message:
Logged In: YES 
user_id=984087
Originator: YES

File Added: pprint_test.py

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1713041&group_id=5470


More information about the Patches mailing list