-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-40995: reprlib.Repr attributes can be overriden in __init__() #20925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,9 @@ debugger and may be useful in other contexts as well. | |
This module provides a class, an instance, and a function: | ||
|
||
|
||
.. class:: Repr() | ||
.. class:: Repr(self, *, maxlevel=6, maxtuple=6, maxlist=6, maxarray=5, \ | ||
maxdict=4, maxset=6, maxfrozenset=6, maxdeque=6, maxstring=30, \ | ||
maxlong=40, maxother=30) | ||
Comment on lines
+21
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please align these lines with the |
||
|
||
Class which provides formatting services useful in implementing functions | ||
similar to the built-in :func:`repr`; size limits for different object types | ||
|
@@ -71,72 +73,75 @@ string instead. | |
Repr Objects | ||
------------ | ||
|
||
:class:`Repr` instances provide several attributes which can be used to provide | ||
size limits for the representations of different object types, and methods | ||
which format specific object types. | ||
.. class:: Repr(self, *, maxlevel=6, maxtuple=6, maxlist=6, maxarray=5, \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
maxdict=4, maxset=6, maxfrozenset=6, maxdeque=6, maxstring=30, \ | ||
maxlong=40, maxother=30) | ||
Comment on lines
+77
to
+78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please align these lines with the |
||
|
||
:class:`Repr` instances provide several attributes which can be used to provide | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please limit lines to 80 characters in reST files (https://devguide.python.org/documenting/#use-of-whitespace). |
||
size limits for the representations of different object types, and methods | ||
which format specific object types. | ||
|
||
.. attribute:: Repr.maxlevel | ||
.. attribute:: maxlevel | ||
|
||
Depth limit on the creation of recursive representations. The default is ``6``. | ||
Depth limit on the creation of recursive representations. The default is ``6``. | ||
|
||
|
||
.. attribute:: Repr.maxdict | ||
Repr.maxlist | ||
Repr.maxtuple | ||
Repr.maxset | ||
Repr.maxfrozenset | ||
Repr.maxdeque | ||
Repr.maxarray | ||
.. attribute:: maxdict | ||
maxlist | ||
maxtuple | ||
maxset | ||
maxfrozenset | ||
maxdeque | ||
maxarray | ||
|
||
Limits on the number of entries represented for the named object type. The | ||
default is ``4`` for :attr:`maxdict`, ``5`` for :attr:`maxarray`, and ``6`` for | ||
the others. | ||
Limits on the number of entries represented for the named object type. The | ||
default is ``4`` for :attr:`maxdict`, ``5`` for :attr:`maxarray`, and ``6`` for | ||
the others. | ||
|
||
|
||
.. attribute:: Repr.maxlong | ||
.. attribute:: maxlong | ||
|
||
Maximum number of characters in the representation for an integer. Digits | ||
are dropped from the middle. The default is ``40``. | ||
Maximum number of characters in the representation for an integer. Digits | ||
are dropped from the middle. The default is ``40``. | ||
|
||
|
||
.. attribute:: Repr.maxstring | ||
.. attribute:: maxstring | ||
|
||
Limit on the number of characters in the representation of the string. Note | ||
that the "normal" representation of the string is used as the character source: | ||
if escape sequences are needed in the representation, these may be mangled when | ||
the representation is shortened. The default is ``30``. | ||
Limit on the number of characters in the representation of the string. Note | ||
that the "normal" representation of the string is used as the character source: | ||
if escape sequences are needed in the representation, these may be mangled when | ||
the representation is shortened. The default is ``30``. | ||
|
||
|
||
.. attribute:: Repr.maxother | ||
.. attribute:: maxother | ||
|
||
This limit is used to control the size of object types for which no specific | ||
formatting method is available on the :class:`Repr` object. It is applied in a | ||
similar manner as :attr:`maxstring`. The default is ``20``. | ||
This limit is used to control the size of object types for which no specific | ||
formatting method is available on the :class:`Repr` object. It is applied in a | ||
similar manner as :attr:`maxstring`. The default is ``20``. | ||
|
||
|
||
.. method:: Repr.repr(obj) | ||
.. method:: repr(obj) | ||
|
||
The equivalent to the built-in :func:`repr` that uses the formatting imposed by | ||
the instance. | ||
The equivalent to the built-in :func:`repr` that uses the formatting imposed by | ||
the instance. | ||
|
||
|
||
.. method:: Repr.repr1(obj, level) | ||
.. method:: repr1(obj, level) | ||
|
||
Recursive implementation used by :meth:`.repr`. This uses the type of *obj* to | ||
determine which formatting method to call, passing it *obj* and *level*. The | ||
type-specific methods should call :meth:`repr1` to perform recursive formatting, | ||
with ``level - 1`` for the value of *level* in the recursive call. | ||
Recursive implementation used by :meth:`.repr`. This uses the type of *obj* to | ||
determine which formatting method to call, passing it *obj* and *level*. The | ||
type-specific methods should call :meth:`repr1` to perform recursive formatting, | ||
with ``level - 1`` for the value of *level* in the recursive call. | ||
|
||
|
||
.. method:: Repr.repr_TYPE(obj, level) | ||
:noindex: | ||
.. method:: repr_TYPE(obj, level) | ||
:noindex: | ||
|
||
Formatting methods for specific types are implemented as methods with a name | ||
based on the type name. In the method name, **TYPE** is replaced by | ||
``'_'.join(type(obj).__name__.split())``. Dispatch to these methods is | ||
handled by :meth:`repr1`. Type-specific methods which need to recursively | ||
format a value should call ``self.repr1(subobj, level - 1)``. | ||
Formatting methods for specific types are implemented as methods with a name | ||
based on the type name. In the method name, **TYPE** is replaced by | ||
``'_'.join(type(obj).__name__.split())``. Dispatch to these methods is | ||
handled by :meth:`repr1`. Type-specific methods which need to recursively | ||
format a value should call ``self.repr1(subobj, level - 1)``. | ||
|
||
|
||
.. _subclassing-reprs: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -216,6 +216,13 @@ def test_unsortable(self): | |
r(y) | ||
r(z) | ||
|
||
def test___init__(self): | ||
a = "a"*40 | ||
r = Repr() | ||
self.assertEqual(r.repr(a), "'aaaaaaaaaaaa...aaaaaaaaaaaaa'") | ||
r = Repr(maxstring=10) | ||
self.assertEqual(r.repr(a), "'aa...aaa'") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think more tests need to be provided. |
||
|
||
def write_file(path, text): | ||
with open(path, 'w', encoding='ASCII') as fp: | ||
fp.write(text) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
:class:`reprlib.Repr` attributes can now be set when creating an instance. | ||
Patch contributed by Rémi Lapeyre. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self
should not be included here.