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
0ce7a3a3
Commit
0ce7a3a3
authored
Dec 22, 2015
by
Serhiy Storchaka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #25914: Fixed and simplified OrderedDict.__sizeof__.
parent
5af85640
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
45 additions
and
25 deletions
+45
-25
Include/dictobject.h
Include/dictobject.h
+1
-1
Lib/test/test_ordered_dict.py
Lib/test/test_ordered_dict.py
+32
-0
Misc/NEWS
Misc/NEWS
+2
-0
Objects/dictobject.c
Objects/dictobject.c
+9
-3
Objects/odictobject.c
Objects/odictobject.c
+1
-21
No files found.
Include/dictobject.h
View file @
0ce7a3a3
...
...
@@ -98,7 +98,7 @@ PyAPI_FUNC(PyObject *) _PyDict_NewPresized(Py_ssize_t minused);
PyAPI_FUNC
(
void
)
_PyDict_MaybeUntrack
(
PyObject
*
mp
);
PyAPI_FUNC
(
int
)
_PyDict_HasOnlyStringKeys
(
PyObject
*
mp
);
Py_ssize_t
_PyDict_KeysSize
(
PyDictKeysObject
*
keys
);
Py
Object
*
_PyDict_SizeOf
(
PyDictObject
*
);
Py
_ssize_t
_PyDict_SizeOf
(
PyDictObject
*
);
PyObject
*
_PyDict_Pop
(
PyDictObject
*
,
PyObject
*
,
PyObject
*
);
PyObject
*
_PyDict_FromKeys
(
PyObject
*
,
PyObject
*
,
PyObject
*
);
#define _PyDict_HasSplitTable(d) ((d)->ma_values != NULL)
...
...
Lib/test/test_ordered_dict.py
View file @
0ce7a3a3
...
...
@@ -2,6 +2,7 @@ import contextlib
import
copy
import
pickle
from
random
import
randrange
,
shuffle
import
struct
import
sys
import
unittest
from
collections.abc
import
MutableMapping
...
...
@@ -596,6 +597,37 @@ class CPythonOrderedDictTests(OrderedDictTests, unittest.TestCase):
module
=
c_coll
OrderedDict
=
c_coll
.
OrderedDict
check_sizeof
=
support
.
check_sizeof
@
support
.
cpython_only
def
test_sizeof_exact
(
self
):
OrderedDict
=
self
.
OrderedDict
calcsize
=
struct
.
calcsize
size
=
support
.
calcobjsize
check
=
self
.
check_sizeof
basicsize
=
size
(
'n2P'
+
'3PnPn2P'
)
+
calcsize
(
'2nPn'
)
entrysize
=
calcsize
(
'n2P'
)
+
calcsize
(
'P'
)
nodesize
=
calcsize
(
'Pn2P'
)
od
=
OrderedDict
()
check
(
od
,
basicsize
+
8
*
entrysize
)
od
.
x
=
1
check
(
od
,
basicsize
+
8
*
entrysize
)
od
.
update
([(
i
,
i
)
for
i
in
range
(
3
)])
check
(
od
,
basicsize
+
8
*
entrysize
+
3
*
nodesize
)
od
.
update
([(
i
,
i
)
for
i
in
range
(
3
,
10
)])
check
(
od
,
basicsize
+
16
*
entrysize
+
10
*
nodesize
)
check
(
od
.
keys
(),
size
(
'P'
))
check
(
od
.
items
(),
size
(
'P'
))
check
(
od
.
values
(),
size
(
'P'
))
itersize
=
size
(
'iP2n2P'
)
check
(
iter
(
od
),
itersize
)
check
(
iter
(
od
.
keys
()),
itersize
)
check
(
iter
(
od
.
items
()),
itersize
)
check
(
iter
(
od
.
values
()),
itersize
)
def
test_key_change_during_iteration
(
self
):
OrderedDict
=
self
.
OrderedDict
...
...
Misc/NEWS
View file @
0ce7a3a3
...
...
@@ -28,6 +28,8 @@ Core and Builtins
Library
-------
- Issue #25914: Fixed and simplified OrderedDict.__sizeof__.
- Issue #25902: Fixed various refcount issues in ElementTree iteration.
- Issue #25717: Restore the previous behaviour of tolerating most fstat()
...
...
Objects/dictobject.c
View file @
0ce7a3a3
...
...
@@ -2554,7 +2554,7 @@ dict_tp_clear(PyObject *op)
static
PyObject
*
dictiter_new
(
PyDictObject
*
,
PyTypeObject
*
);
Py
Object
*
Py
_ssize_t
_PyDict_SizeOf
(
PyDictObject
*
mp
)
{
Py_ssize_t
size
,
res
;
...
...
@@ -2567,7 +2567,7 @@ _PyDict_SizeOf(PyDictObject *mp)
in the type object. */
if
(
mp
->
ma_keys
->
dk_refcnt
==
1
)
res
+=
sizeof
(
PyDictKeysObject
)
+
(
size
-
1
)
*
sizeof
(
PyDictKeyEntry
);
return
PyLong_FromSsize_t
(
res
)
;
return
res
;
}
Py_ssize_t
...
...
@@ -2576,6 +2576,12 @@ _PyDict_KeysSize(PyDictKeysObject *keys)
return
sizeof
(
PyDictKeysObject
)
+
(
DK_SIZE
(
keys
)
-
1
)
*
sizeof
(
PyDictKeyEntry
);
}
PyObject
*
dict_sizeof
(
PyDictObject
*
mp
)
{
return
PyLong_FromSsize_t
(
_PyDict_SizeOf
(
mp
));
}
PyDoc_STRVAR
(
getitem__doc__
,
"x.__getitem__(y) <==> x[y]"
);
PyDoc_STRVAR
(
sizeof__doc__
,
...
...
@@ -2623,7 +2629,7 @@ static PyMethodDef mapp_methods[] = {
DICT___CONTAINS___METHODDEF
{
"__getitem__"
,
(
PyCFunction
)
dict_subscript
,
METH_O
|
METH_COEXIST
,
getitem__doc__
},
{
"__sizeof__"
,
(
PyCFunction
)
_PyDict_SizeO
f
,
METH_NOARGS
,
{
"__sizeof__"
,
(
PyCFunction
)
dict_sizeo
f
,
METH_NOARGS
,
sizeof__doc__
},
{
"get"
,
(
PyCFunction
)
dict_get
,
METH_VARARGS
,
get__doc__
},
...
...
Objects/odictobject.c
View file @
0ce7a3a3
...
...
@@ -940,27 +940,7 @@ PyDoc_STRVAR(odict_sizeof__doc__, "");
static
PyObject
*
odict_sizeof
(
PyODictObject
*
od
)
{
PyObject
*
pylong
;
Py_ssize_t
res
,
temp
;
pylong
=
_PyDict_SizeOf
((
PyDictObject
*
)
od
);
if
(
pylong
==
NULL
)
return
NULL
;
res
=
PyLong_AsSsize_t
(
pylong
);
Py_DECREF
(
pylong
);
if
(
res
==
-
1
&&
PyErr_Occurred
())
return
NULL
;
/* instance dict */
pylong
=
_PyDict_SizeOf
((
PyDictObject
*
)
od
->
od_inst_dict
);
if
(
pylong
==
NULL
)
return
NULL
;
temp
=
PyLong_AsSsize_t
(
pylong
);
Py_DECREF
(
pylong
);
if
(
temp
==
-
1
&&
PyErr_Occurred
())
return
NULL
;
res
+=
temp
;
Py_ssize_t
res
=
_PyDict_SizeOf
((
PyDictObject
*
)
od
);
res
+=
sizeof
(
_ODictNode
*
)
*
_odict_FAST_SIZE
(
od
);
/* od_fast_nodes */
if
(
!
_odict_EMPTY
(
od
))
{
res
+=
sizeof
(
_ODictNode
)
*
PyODict_SIZE
(
od
);
/* linked-list */
...
...
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