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
216dc35f
Commit
216dc35f
authored
Jul 28, 2012
by
Meador Inge
Browse files
Options
Browse Files
Download
Plain Diff
Issue #15402: Simplify Struct.__sizeof__ and make tests more precise.
parents
61b95104
cc6afead
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
16 deletions
+43
-16
Lib/test/test_struct.py
Lib/test/test_struct.py
+41
-10
Modules/_struct.c
Modules/_struct.c
+2
-6
No files found.
Lib/test/test_struct.py
View file @
216dc35f
...
@@ -3,7 +3,7 @@ import unittest
...
@@ -3,7 +3,7 @@ import unittest
import
struct
import
struct
import
sys
import
sys
from
test.support
import
run_unittest
from
test.support
import
run_unittest
,
cpython_only
ISBIGENDIAN
=
sys
.
byteorder
==
"big"
ISBIGENDIAN
=
sys
.
byteorder
==
"big"
IS32BIT
=
sys
.
maxsize
==
0x7fffffff
IS32BIT
=
sys
.
maxsize
==
0x7fffffff
...
@@ -40,6 +40,33 @@ def bigendian_to_native(value):
...
@@ -40,6 +40,33 @@ def bigendian_to_native(value):
return
string_reverse
(
value
)
return
string_reverse
(
value
)
class
StructTest
(
unittest
.
TestCase
):
class
StructTest
(
unittest
.
TestCase
):
def
setUp
(
self
):
# due to missing size_t information from struct, it is assumed that
# sizeof(Py_ssize_t) = sizeof(void*)
self
.
header
=
'PP'
if
hasattr
(
sys
,
"gettotalrefcount"
):
self
.
header
+=
'2P'
def
check_sizeof
(
self
,
format_str
,
number_of_codes
):
def
size
(
fmt
):
"""Wrapper around struct.calcsize which enforces the alignment
of the end of a structure to the alignment requirement of pointer.
Note: This wrapper should only be used if a pointer member is
included and no member with a size larger than a pointer exists.
"""
return
struct
.
calcsize
(
fmt
+
'0P'
)
struct_obj
=
struct
.
Struct
(
format_str
)
# The size of 'PyStructObject'
totalsize
=
size
(
self
.
header
+
'5P'
)
# The size taken up by the 'formatcode' dynamic array
totalsize
+=
size
(
'3P'
)
*
(
number_of_codes
+
1
)
result
=
sys
.
getsizeof
(
struct_obj
)
msg
=
'wrong size for %s: got %d, expected %d'
\
%
(
type
(
struct_obj
),
result
,
totalsize
)
self
.
assertEqual
(
result
,
totalsize
,
msg
)
def
test_isbigendian
(
self
):
def
test_isbigendian
(
self
):
self
.
assertEqual
((
struct
.
pack
(
'=i'
,
1
)[
0
]
==
0
),
ISBIGENDIAN
)
self
.
assertEqual
((
struct
.
pack
(
'=i'
,
1
)[
0
]
==
0
),
ISBIGENDIAN
)
...
@@ -572,15 +599,19 @@ class StructTest(unittest.TestCase):
...
@@ -572,15 +599,19 @@ class StructTest(unittest.TestCase):
s
=
struct
.
Struct
(
'i'
)
s
=
struct
.
Struct
(
'i'
)
s
.
__init__
(
'ii'
)
s
.
__init__
(
'ii'
)
def
test_sizeof
(
self
):
@
cpython_only
self
.
assertGreater
(
sys
.
getsizeof
(
struct
.
Struct
(
'BHILfdspP'
)),
def
test__sizeof__
(
self
):
sys
.
getsizeof
(
struct
.
Struct
(
'B'
)))
for
code
in
integer_codes
:
self
.
assertGreater
(
sys
.
getsizeof
(
struct
.
Struct
(
'123B'
)),
self
.
check_sizeof
(
code
,
1
)
sys
.
getsizeof
(
struct
.
Struct
(
'B'
)))
self
.
check_sizeof
(
'BHILfdspP'
,
9
)
self
.
assertGreater
(
sys
.
getsizeof
(
struct
.
Struct
(
'B'
*
1234
)),
self
.
check_sizeof
(
'B'
*
1234
,
1234
)
sys
.
getsizeof
(
struct
.
Struct
(
'123B'
)))
self
.
check_sizeof
(
'fd'
,
2
)
self
.
assertGreater
(
sys
.
getsizeof
(
struct
.
Struct
(
'1234B'
)),
self
.
check_sizeof
(
'xxxxxxxxxxxxxx'
,
0
)
sys
.
getsizeof
(
struct
.
Struct
(
'123B'
)))
self
.
check_sizeof
(
'100H'
,
100
)
self
.
check_sizeof
(
'187s'
,
1
)
self
.
check_sizeof
(
'20p'
,
1
)
self
.
check_sizeof
(
'0s'
,
1
)
self
.
check_sizeof
(
'0c'
,
0
)
def
test_main
():
def
test_main
():
run_unittest
(
StructTest
)
run_unittest
(
StructTest
)
...
...
Modules/_struct.c
View file @
216dc35f
...
@@ -1756,15 +1756,11 @@ PyDoc_STRVAR(s_sizeof__doc__,
...
@@ -1756,15 +1756,11 @@ PyDoc_STRVAR(s_sizeof__doc__,
"S.__sizeof__() -> size of S in memory, in bytes"
);
"S.__sizeof__() -> size of S in memory, in bytes"
);
static
PyObject
*
static
PyObject
*
s_sizeof
(
PyStructObject
*
self
)
s_sizeof
(
PyStructObject
*
self
,
void
*
unused
)
{
{
Py_ssize_t
size
;
Py_ssize_t
size
;
formatcode
*
code
;
size
=
sizeof
(
PyStructObject
)
+
sizeof
(
formatcode
);
size
=
sizeof
(
PyStructObject
)
+
sizeof
(
formatcode
)
*
(
self
->
s_len
+
1
);
for
(
code
=
self
->
s_codes
;
code
->
fmtdef
!=
NULL
;
code
++
)
{
size
+=
sizeof
(
formatcode
);
}
return
PyLong_FromSsize_t
(
size
);
return
PyLong_FromSsize_t
(
size
);
}
}
...
...
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