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
b0d56afc
Commit
b0d56afc
authored
Mar 03, 2009
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Give dict views a helpful __repr__.
parent
ce57cdbe
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
24 additions
and
6 deletions
+24
-6
Lib/test/test_dict.py
Lib/test/test_dict.py
+3
-3
Misc/NEWS
Misc/NEWS
+3
-0
Objects/dictobject.c
Objects/dictobject.c
+18
-3
No files found.
Lib/test/test_dict.py
View file @
b0d56afc
...
...
@@ -36,16 +36,16 @@ class DictTest(unittest.TestCase):
k
=
d
.
keys
()
self
.
assert_
(
'a'
in
d
)
self
.
assert_
(
'b'
in
d
)
self
.
assertRaises
(
TypeError
,
d
.
keys
,
None
)
self
.
assertEqual
(
repr
(
dict
(
a
=
1
).
keys
()),
"dict_keys(['a'])"
)
def
test_values
(
self
):
d
=
{}
self
.
assertEqual
(
set
(
d
.
values
()),
set
())
d
=
{
1
:
2
}
self
.
assertEqual
(
set
(
d
.
values
()),
{
2
})
self
.
assertRaises
(
TypeError
,
d
.
values
,
None
)
self
.
assertEqual
(
repr
(
dict
(
a
=
1
).
values
()),
"dict_values([1])"
)
def
test_items
(
self
):
d
=
{}
...
...
@@ -53,8 +53,8 @@ class DictTest(unittest.TestCase):
d
=
{
1
:
2
}
self
.
assertEqual
(
set
(
d
.
items
()),
{(
1
,
2
)})
self
.
assertRaises
(
TypeError
,
d
.
items
,
None
)
self
.
assertEqual
(
repr
(
dict
(
a
=
1
).
items
()),
"dict_items([('a', 1)])"
)
def
test_contains
(
self
):
d
=
{}
...
...
Misc/NEWS
View file @
b0d56afc
...
...
@@ -13,6 +13,9 @@ Core and Builtins
-----------------
=======
- Give dict views an informative __repr__.
- Issue #5247: Improve error message when unknown format codes are
used when using str.format() with str, int, and float arguments.
...
...
Objects/dictobject.c
View file @
b0d56afc
...
...
@@ -2563,6 +2563,21 @@ dictview_richcompare(PyObject *self, PyObject *other, int op)
return
result
;
}
static
PyObject
*
dictview_repr
(
dictviewobject
*
dv
)
{
PyObject
*
seq
;
PyObject
*
result
;
seq
=
PySequence_List
((
PyObject
*
)
dv
);
if
(
seq
==
NULL
)
return
NULL
;
result
=
PyUnicode_FromFormat
(
"%s(%R)"
,
Py_TYPE
(
dv
)
->
tp_name
,
seq
);
Py_DECREF
(
seq
);
return
result
;
}
/*** dict_keys ***/
static
PyObject
*
...
...
@@ -2700,7 +2715,7 @@ PyTypeObject PyDictKeys_Type = {
0
,
/* tp_getattr */
0
,
/* tp_setattr */
0
,
/* tp_reserved */
0
,
/* tp_repr */
(
reprfunc
)
dictview_repr
,
/* tp_repr */
&
dictviews_as_number
,
/* tp_as_number */
&
dictkeys_as_sequence
,
/* tp_as_sequence */
0
,
/* tp_as_mapping */
...
...
@@ -2784,7 +2799,7 @@ PyTypeObject PyDictItems_Type = {
0
,
/* tp_getattr */
0
,
/* tp_setattr */
0
,
/* tp_reserved */
0
,
/* tp_repr */
(
reprfunc
)
dictview_repr
,
/* tp_repr */
&
dictviews_as_number
,
/* tp_as_number */
&
dictitems_as_sequence
,
/* tp_as_sequence */
0
,
/* tp_as_mapping */
...
...
@@ -2849,7 +2864,7 @@ PyTypeObject PyDictValues_Type = {
0
,
/* tp_getattr */
0
,
/* tp_setattr */
0
,
/* tp_reserved */
0
,
/* tp_repr */
(
reprfunc
)
dictview_repr
,
/* tp_repr */
0
,
/* tp_as_number */
&
dictvalues_as_sequence
,
/* tp_as_sequence */
0
,
/* tp_as_mapping */
...
...
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