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
d59ca8f3
Commit
d59ca8f3
authored
Mar 20, 2006
by
Thomas Heller
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Accessing unaligned structure fields works now on all architectures.
Including unittest.
parent
6c2f9138
Changes
3
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
192 additions
and
84 deletions
+192
-84
Lib/ctypes/test/test_byteswap.py
Lib/ctypes/test/test_byteswap.py
+43
-38
Lib/ctypes/test/test_unaligned_structures.py
Lib/ctypes/test/test_unaligned_structures.py
+45
-0
Modules/_ctypes/cfield.c
Modules/_ctypes/cfield.c
+104
-46
No files found.
Lib/ctypes/test/test_byteswap.py
View file @
d59ca8f3
...
...
@@ -2,7 +2,6 @@ import sys, unittest, struct, math
from
binascii
import
hexlify
from
ctypes
import
*
from
ctypes.test
import
is_resource_enabled
def
bin
(
s
):
return
hexlify
(
buffer
(
s
)).
upper
()
...
...
@@ -222,54 +221,60 @@ class Test(unittest.TestCase):
s2
=
struct
.
pack
(
fmt
,
0x12
,
0x1234
,
0x12345678
,
3.14
)
self
.
failUnlessEqual
(
bin
(
s1
),
bin
(
s2
))
if
is_resource_enabled
(
"unaligned_access"
):
def
test_unaligned_nonnative_struct_fields
(
self
):
if
sys
.
byteorder
==
"little"
:
base
=
BigEndianStructure
fmt
=
">b h xi xd"
else
:
base
=
LittleEndianStructure
fmt
=
"<b h xi xd"
def
test_unaligned_nonnative_struct_fields
(
self
):
if
sys
.
byteorder
==
"little"
:
base
=
BigEndianStructure
fmt
=
">b h xi xd"
else
:
base
=
LittleEndianStructure
fmt
=
"<b h xi xd"
class
S
(
base
):
_pack_
=
1
_fields_
=
[(
"b"
,
c_byte
),
class
S
(
base
):
_pack_
=
1
_fields_
=
[(
"b"
,
c_byte
),
(
"h"
,
c_short
),
(
"h"
,
c_short
),
(
"_1"
,
c_byte
),
(
"i"
,
c_int
),
(
"_1"
,
c_byte
),
(
"i"
,
c_int
),
(
"_2"
,
c_byte
),
(
"d"
,
c_double
)]
(
"_2"
,
c_byte
),
(
"d"
,
c_double
)]
s1
=
S
(
0x12
,
0x1234
,
0
,
0x12345678
,
0
,
3.14
)
s2
=
struct
.
pack
(
fmt
,
0x12
,
0x1234
,
0x12345678
,
3.14
)
self
.
failUnlessEqual
(
bin
(
s1
),
bin
(
s2
))
s1
=
S
()
s1
.
b
=
0x12
s1
.
h
=
0x1234
s1
.
i
=
0x12345678
s1
.
d
=
3.14
s2
=
struct
.
pack
(
fmt
,
0x12
,
0x1234
,
0x12345678
,
3.14
)
self
.
failUnlessEqual
(
bin
(
s1
),
bin
(
s2
))
def
test_unaligned_native_struct_fields
(
self
):
if
sys
.
byteorder
==
"little"
:
fmt
=
"<b h xi xd"
else
:
base
=
LittleEndianStructure
fmt
=
">b h xi xd"
def
test_unaligned_native_struct_fields
(
self
):
if
sys
.
byteorder
==
"little"
:
fmt
=
"<b h xi xd"
else
:
base
=
LittleEndianStructure
fmt
=
">b h xi xd"
class
S
(
Structure
):
_pack_
=
1
_fields_
=
[(
"b"
,
c_byte
),
class
S
(
Structure
):
_pack_
=
1
_fields_
=
[(
"b"
,
c_byte
),
(
"h"
,
c_short
),
(
"h"
,
c_short
),
(
"_1"
,
c_byte
),
(
"i"
,
c_int
),
(
"_1"
,
c_byte
),
(
"i"
,
c_int
),
(
"_2"
,
c_byte
),
(
"d"
,
c_double
)]
(
"_2"
,
c_byte
),
(
"d"
,
c_double
)]
s1
=
S
(
0x12
,
0x1234
,
0
,
0x12345678
,
0
,
3.14
)
s2
=
struct
.
pack
(
fmt
,
0x12
,
0x1234
,
0x12345678
,
3.14
)
self
.
failUnlessEqual
(
bin
(
s1
),
bin
(
s2
))
s1
=
S
()
s1
.
b
=
0x12
s1
.
h
=
0x1234
s1
.
i
=
0x12345678
s1
.
d
=
3.14
s2
=
struct
.
pack
(
fmt
,
0x12
,
0x1234
,
0x12345678
,
3.14
)
self
.
failUnlessEqual
(
bin
(
s1
),
bin
(
s2
))
if
__name__
==
"__main__"
:
unittest
.
main
()
Lib/ctypes/test/test_unaligned_structures.py
0 → 100644
View file @
d59ca8f3
import
sys
,
unittest
from
ctypes
import
*
structures
=
[]
byteswapped_structures
=
[]
if
sys
.
byteorder
==
"little"
:
SwappedStructure
=
BigEndianStructure
else
:
SwappedStructure
=
LittleEndianStructure
for
typ
in
[
c_short
,
c_int
,
c_long
,
c_longlong
,
c_float
,
c_double
,
c_ushort
,
c_uint
,
c_ulong
,
c_ulonglong
]:
class
X
(
Structure
):
_pack_
=
1
_fields_
=
[(
"pad"
,
c_byte
),
(
"value"
,
typ
)]
class
Y
(
SwappedStructure
):
_pack_
=
1
_fields_
=
[(
"pad"
,
c_byte
),
(
"value"
,
typ
)]
structures
.
append
(
X
)
byteswapped_structures
.
append
(
Y
)
class
TestStructures
(
unittest
.
TestCase
):
def
test_native
(
self
):
for
typ
in
structures
:
## print typ.value
self
.
failUnlessEqual
(
typ
.
value
.
offset
,
1
)
o
=
typ
()
o
.
value
=
4
self
.
failUnlessEqual
(
o
.
value
,
4
)
def
test_swapped
(
self
):
for
typ
in
byteswapped_structures
:
## print >> sys.stderr, typ.value
self
.
failUnlessEqual
(
typ
.
value
.
offset
,
1
)
o
=
typ
()
o
.
value
=
4
self
.
failUnlessEqual
(
o
.
value
,
4
)
if
__name__
==
'__main__'
:
unittest
.
main
()
Modules/_ctypes/cfield.c
View file @
d59ca8f3
This diff is collapsed.
Click to expand it.
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