Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f3fa308

Browse files
Issue #23776: Removed asserts from pprint.PrettyPrinter constructor.
1 parent e6bb7eb commit f3fa308

2 files changed

Lines changed: 23 additions & 7 deletions

File tree

Lib/pprint.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,12 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, *,
124124
"""
125125
indent = int(indent)
126126
width = int(width)
127-
assert indent >= 0, "indent must be >= 0"
128-
assert depth is None or depth > 0, "depth must be > 0"
129-
assert width, "width must be != 0"
127+
if indent < 0:
128+
raise ValueError('indent must be >= 0')
129+
if depth is not None and depth <= 0:
130+
raise ValueError('depth must be > 0')
131+
if not width:
132+
raise ValueError('width must be != 0')
130133
self._depth = depth
131134
self._indent_per_level = indent
132135
self._width = width

Lib/test/test_pprint.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# -*- coding: utf-8 -*-
22

3+
import collections
4+
import io
5+
import itertools
36
import pprint
7+
import random
48
import test.support
5-
import unittest
69
import test.test_set
7-
import random
8-
import collections
9-
import itertools
1010
import types
11+
import unittest
1112

1213
# list, tuple and dict subclasses that do or don't overwrite __repr__
1314
class list2(list):
@@ -56,6 +57,18 @@ def setUp(self):
5657
self.b = list(range(200))
5758
self.a[-12] = self.b
5859

60+
def test_init(self):
61+
pp = pprint.PrettyPrinter()
62+
pp = pprint.PrettyPrinter(indent=4, width=40, depth=5,
63+
stream=io.StringIO(), compact=True)
64+
pp = pprint.PrettyPrinter(4, 40, 5, io.StringIO())
65+
with self.assertRaises(TypeError):
66+
pp = pprint.PrettyPrinter(4, 40, 5, io.StringIO(), True)
67+
self.assertRaises(ValueError, pprint.PrettyPrinter, indent=-1)
68+
self.assertRaises(ValueError, pprint.PrettyPrinter, depth=0)
69+
self.assertRaises(ValueError, pprint.PrettyPrinter, depth=-1)
70+
self.assertRaises(ValueError, pprint.PrettyPrinter, width=0)
71+
5972
def test_basic(self):
6073
# Verify .isrecursive() and .isreadable() w/o recursion
6174
pp = pprint.PrettyPrinter()

0 commit comments

Comments
 (0)