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
bce16668
Commit
bce16668
authored
Jun 17, 2012
by
Martin v. Löwis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #14055: Add __sizeof__ support to _elementtree.
parent
1e5d0ff8
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
53 additions
and
2 deletions
+53
-2
Lib/test/test_xml_etree_c.py
Lib/test/test_xml_etree_c.py
+37
-2
Misc/NEWS
Misc/NEWS
+2
-0
Modules/_elementtree.c
Modules/_elementtree.c
+14
-0
No files found.
Lib/test/test_xml_etree_c.py
View file @
bce16668
# xml.etree test for cElementTree
import
sys
,
struct
from
test
import
support
from
test.support
import
import_fresh_module
import
unittest
...
...
@@ -40,6 +40,40 @@ class TestAcceleratorImported(unittest.TestCase):
self
.
assertEqual
(
cET_alias
.
SubElement
.
__module__
,
'_elementtree'
)
@
unittest
.
skipUnless
(
cET
,
'requires _elementtree'
)
class
SizeofTest
(
unittest
.
TestCase
):
def
setUp
(
self
):
import
_testcapi
gc_headsize
=
_testcapi
.
SIZEOF_PYGC_HEAD
# object header
header
=
'PP'
if
hasattr
(
sys
,
"gettotalrefcount"
):
# debug header
header
=
'PP'
+
header
# fields
element
=
header
+
'5P'
self
.
elementsize
=
gc_headsize
+
struct
.
calcsize
(
element
)
# extra
self
.
extra
=
struct
.
calcsize
(
'PiiP4P'
)
def
test_element
(
self
):
e
=
cET
.
Element
(
'a'
)
self
.
assertEqual
(
sys
.
getsizeof
(
e
),
self
.
elementsize
)
def
test_element_with_attrib
(
self
):
e
=
cET
.
Element
(
'a'
,
href
=
'about:'
)
self
.
assertEqual
(
sys
.
getsizeof
(
e
),
self
.
elementsize
+
self
.
extra
)
def
test_element_with_children
(
self
):
e
=
cET
.
Element
(
'a'
)
for
i
in
range
(
5
):
cET
.
SubElement
(
e
,
'span'
)
# should have space for 8 children now
self
.
assertEqual
(
sys
.
getsizeof
(
e
),
self
.
elementsize
+
self
.
extra
+
struct
.
calcsize
(
'8P'
))
def
test_main
():
from
test
import
test_xml_etree
,
test_xml_etree_c
...
...
@@ -47,7 +81,8 @@ def test_main():
support
.
run_unittest
(
MiscTests
,
TestAliasWorking
,
TestAcceleratorImported
TestAcceleratorImported
,
SizeofTest
,
)
# Run the same test suite as the Python module
...
...
Misc/NEWS
View file @
bce16668
...
...
@@ -29,6 +29,8 @@ Core and Builtins
Library
-------
- Issue #14055: Add __sizeof__ support to _elementtree.
- Issue #15054: A bug in tokenize.tokenize that caused string literals
with '
b
' prefixes to be incorrectly tokenized has been fixed.
Patch by Serhiy Storchaka.
...
...
Modules/_elementtree.c
View file @
bce16668
...
...
@@ -842,6 +842,19 @@ element_deepcopy(ElementObject* self, PyObject* args)
return
NULL
;
}
static
PyObject
*
element_sizeof
(
PyObject
*
_self
,
PyObject
*
args
)
{
ElementObject
*
self
=
(
ElementObject
*
)
_self
;
Py_ssize_t
result
=
sizeof
(
ElementObject
);
if
(
self
->
extra
)
{
result
+=
sizeof
(
ElementObjectExtra
);
if
(
self
->
extra
->
children
!=
self
->
extra
->
_children
)
result
+=
sizeof
(
PyObject
*
)
*
self
->
extra
->
allocated
;
}
return
PyLong_FromSsize_t
(
result
);
}
LOCAL
(
int
)
checkpath
(
PyObject
*
tag
)
{
...
...
@@ -1609,6 +1622,7 @@ static PyMethodDef element_methods[] = {
{
"__copy__"
,
(
PyCFunction
)
element_copy
,
METH_VARARGS
},
{
"__deepcopy__"
,
(
PyCFunction
)
element_deepcopy
,
METH_VARARGS
},
{
"__sizeof__"
,
element_sizeof
,
METH_NOARGS
},
{
NULL
,
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