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
891ecdc7
Commit
891ecdc7
authored
Feb 07, 2007
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Check for a common user error with defaultdict().
parent
6adcc0c7
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
13 additions
and
4 deletions
+13
-4
Lib/test/test_defaultdict.py
Lib/test/test_defaultdict.py
+4
-3
Misc/NEWS
Misc/NEWS
+2
-0
Modules/collectionsmodule.c
Modules/collectionsmodule.c
+7
-1
No files found.
Lib/test/test_defaultdict.py
View file @
891ecdc7
...
@@ -47,6 +47,7 @@ class TestDefaultDict(unittest.TestCase):
...
@@ -47,6 +47,7 @@ class TestDefaultDict(unittest.TestCase):
self
.
assertEqual
(
err
.
args
,
(
15
,))
self
.
assertEqual
(
err
.
args
,
(
15
,))
else
:
else
:
self
.
fail
(
"d2[15] didn't raise KeyError"
)
self
.
fail
(
"d2[15] didn't raise KeyError"
)
self
.
assertRaises
(
TypeError
,
defaultdict
,
1
)
def
test_missing
(
self
):
def
test_missing
(
self
):
d1
=
defaultdict
()
d1
=
defaultdict
()
...
@@ -60,10 +61,10 @@ class TestDefaultDict(unittest.TestCase):
...
@@ -60,10 +61,10 @@ class TestDefaultDict(unittest.TestCase):
self
.
assertEqual
(
repr
(
d1
),
"defaultdict(None, {})"
)
self
.
assertEqual
(
repr
(
d1
),
"defaultdict(None, {})"
)
d1
[
11
]
=
41
d1
[
11
]
=
41
self
.
assertEqual
(
repr
(
d1
),
"defaultdict(None, {11: 41})"
)
self
.
assertEqual
(
repr
(
d1
),
"defaultdict(None, {11: 41})"
)
d2
=
defaultdict
(
0
)
d2
=
defaultdict
(
int
)
self
.
assertEqual
(
d2
.
default_factory
,
0
)
self
.
assertEqual
(
d2
.
default_factory
,
int
)
d2
[
12
]
=
42
d2
[
12
]
=
42
self
.
assertEqual
(
repr
(
d2
),
"defaultdict(
0
, {12: 42})"
)
self
.
assertEqual
(
repr
(
d2
),
"defaultdict(
<type 'int'>
, {12: 42})"
)
def
foo
():
return
43
def
foo
():
return
43
d3
=
defaultdict
(
foo
)
d3
=
defaultdict
(
foo
)
self
.
assert_
(
d3
.
default_factory
is
foo
)
self
.
assert_
(
d3
.
default_factory
is
foo
)
...
...
Misc/NEWS
View file @
891ecdc7
...
@@ -103,6 +103,8 @@ Core and builtins
...
@@ -103,6 +103,8 @@ Core and builtins
Extension
Modules
Extension
Modules
-----------------
-----------------
-
collections
.
defaultdict
()
now
verifies
that
the
factory
function
is
callable
.
-
Bug
#
1486663
:
don
't reject keyword arguments for subclasses of builtin
-
Bug
#
1486663
:
don
't reject keyword arguments for subclasses of builtin
types.
types.
...
...
Modules/collectionsmodule.c
View file @
891ecdc7
...
@@ -1252,8 +1252,14 @@ defdict_init(PyObject *self, PyObject *args, PyObject *kwds)
...
@@ -1252,8 +1252,14 @@ defdict_init(PyObject *self, PyObject *args, PyObject *kwds)
newargs
=
PyTuple_New
(
0
);
newargs
=
PyTuple_New
(
0
);
else
{
else
{
Py_ssize_t
n
=
PyTuple_GET_SIZE
(
args
);
Py_ssize_t
n
=
PyTuple_GET_SIZE
(
args
);
if
(
n
>
0
)
if
(
n
>
0
)
{
newdefault
=
PyTuple_GET_ITEM
(
args
,
0
);
newdefault
=
PyTuple_GET_ITEM
(
args
,
0
);
if
(
!
PyCallable_Check
(
newdefault
))
{
PyErr_SetString
(
PyExc_TypeError
,
"first argument must be callable"
);
return
-
1
;
}
}
newargs
=
PySequence_GetSlice
(
args
,
1
,
n
);
newargs
=
PySequence_GetSlice
(
args
,
1
,
n
);
}
}
if
(
newargs
==
NULL
)
if
(
newargs
==
NULL
)
...
...
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