[Python-checkins] cpython: Issue #23776: Removed asserts from pprint.PrettyPrinter constructor.

serhiy.storchaka python-checkins at python.org
Thu Mar 26 07:45:30 CET 2015


https://hg.python.org/cpython/rev/bf570ff87c60
changeset:   95209:bf570ff87c60
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Thu Mar 26 08:43:21 2015 +0200
summary:
  Issue #23776: Removed asserts from pprint.PrettyPrinter constructor.

files:
  Lib/pprint.py           |   9 ++++++---
  Lib/test/test_pprint.py |  23 ++++++++++++++++++-----
  2 files changed, 24 insertions(+), 8 deletions(-)


diff --git a/Lib/pprint.py b/Lib/pprint.py
--- a/Lib/pprint.py
+++ b/Lib/pprint.py
@@ -124,9 +124,12 @@
         """
         indent = int(indent)
         width = int(width)
-        assert indent >= 0, "indent must be >= 0"
-        assert depth is None or depth > 0, "depth must be > 0"
-        assert width, "width must be != 0"
+        if indent < 0:
+            raise ValueError('indent must be >= 0')
+        if depth is not None and depth <= 0:
+            raise ValueError('depth must be > 0')
+        if not width:
+            raise ValueError('width must be != 0')
         self._depth = depth
         self._indent_per_level = indent
         self._width = width
diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py
--- a/Lib/test/test_pprint.py
+++ b/Lib/test/test_pprint.py
@@ -1,13 +1,14 @@
 # -*- coding: utf-8 -*-
 
+import collections
+import io
+import itertools
 import pprint
+import random
 import test.support
+import test.test_set
+import types
 import unittest
-import test.test_set
-import random
-import collections
-import itertools
-import types
 
 # list, tuple and dict subclasses that do or don't overwrite __repr__
 class list2(list):
@@ -56,6 +57,18 @@
         self.b = list(range(200))
         self.a[-12] = self.b
 
+    def test_init(self):
+        pp = pprint.PrettyPrinter()
+        pp = pprint.PrettyPrinter(indent=4, width=40, depth=5,
+                                  stream=io.StringIO(), compact=True)
+        pp = pprint.PrettyPrinter(4, 40, 5, io.StringIO())
+        with self.assertRaises(TypeError):
+            pp = pprint.PrettyPrinter(4, 40, 5, io.StringIO(), True)
+        self.assertRaises(ValueError, pprint.PrettyPrinter, indent=-1)
+        self.assertRaises(ValueError, pprint.PrettyPrinter, depth=0)
+        self.assertRaises(ValueError, pprint.PrettyPrinter, depth=-1)
+        self.assertRaises(ValueError, pprint.PrettyPrinter, width=0)
+
     def test_basic(self):
         # Verify .isrecursive() and .isreadable() w/o recursion
         pp = pprint.PrettyPrinter()

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


More information about the Python-checkins mailing list