Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
23da6e65
Commit
23da6e65
authored
May 12, 2008
by
Georg Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#1713041: fix pprint's handling of maximum depth.
parent
103f19d2
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
26 additions
and
6 deletions
+26
-6
Doc/library/pprint.rst
Doc/library/pprint.rst
+2
-3
Lib/pprint.py
Lib/pprint.py
+7
-3
Lib/test/test_pprint.py
Lib/test/test_pprint.py
+15
-0
Misc/NEWS
Misc/NEWS
+2
-0
No files found.
Doc/library/pprint.rst
View file @
23da6e65
...
...
@@ -66,8 +66,7 @@ The :mod:`pprint` module defines one class:
... ('parrot', ('fresh fruit',))))))))
>>> pp = pprint.PrettyPrinter(depth=6)
>>> pp.pprint(tup)
('spam',
('eggs', ('lumberjack', ('knights', ('ni', ('dead', ('parrot', (...,))))))))
('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...)))))))
The :class:`PrettyPrinter` class supports several derivative functions:
...
...
@@ -220,7 +219,7 @@ This example demonstrates several uses of the :func:`pprint` function and its pa
['cccccccccccccccccccc', 'dddddddddddddddddddd']]
>>> pprint.pprint(stuff, depth=3)
['aaaaaaaaaa',
('spam', ('eggs', (
'lumberjack', (...)
))),
('spam', ('eggs', (
...
))),
['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
['cccccccccccccccccccc', 'dddddddddddddddddddd']]
>>> pprint.pprint(stuff, width=60)
...
...
Lib/pprint.py
View file @
23da6e65
...
...
@@ -131,6 +131,10 @@ class PrettyPrinter:
sepLines
=
_len
(
rep
)
>
(
self
.
_width
-
1
-
indent
-
allowance
)
write
=
stream
.
write
if
self
.
_depth
and
level
>
self
.
_depth
:
write
(
rep
)
return
r
=
getattr
(
typ
,
"__repr__"
,
None
)
if
issubclass
(
typ
,
dict
)
and
r
is
dict
.
__repr__
:
write
(
'{'
)
...
...
@@ -211,8 +215,8 @@ class PrettyPrinter:
write
(
','
)
write
(
endchar
)
return
write
(
rep
)
write
(
rep
)
def
_repr
(
self
,
object
,
context
,
level
):
repr
,
readable
,
recursive
=
self
.
format
(
object
,
context
.
copy
(),
...
...
@@ -259,7 +263,7 @@ def _safe_repr(object, context, maxlevels, level):
if
not
object
:
return
"{}"
,
True
,
False
objid
=
_id
(
object
)
if
maxlevels
and
level
>
maxlevels
:
if
maxlevels
and
level
>
=
maxlevels
:
return
"{...}"
,
False
,
objid
in
context
if
objid
in
context
:
return
_recursion
(
object
),
False
,
True
...
...
@@ -293,7 +297,7 @@ def _safe_repr(object, context, maxlevels, level):
return
"()"
,
True
,
False
format
=
"(%s)"
objid
=
_id
(
object
)
if
maxlevels
and
level
>
maxlevels
:
if
maxlevels
and
level
>
=
maxlevels
:
return
format
%
"..."
,
False
,
objid
in
context
if
objid
in
context
:
return
_recursion
(
object
),
False
,
True
...
...
Lib/test/test_pprint.py
View file @
23da6e65
...
...
@@ -387,6 +387,21 @@ class QueryTestCase(unittest.TestCase):
cubo
=
test
.
test_set
.
linegraph
(
cube
)
self
.
assertEqual
(
pprint
.
pformat
(
cubo
),
cubo_repr_tgt
)
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
))
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
)
class
DottedPrettyPrinter
(
pprint
.
PrettyPrinter
):
...
...
Misc/NEWS
View file @
23da6e65
...
...
@@ -26,6 +26,8 @@ Extension Modules
Library
-------
- #1713041: fix pprint's handling of maximum depth.
- The timing module has been deprecated for removal in Python 3.0.
- The sv module has been deprecated for removal in Python 3.0.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment