Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5ce8f35

Browse files
committed
Issue #22098: ctypes' BigEndianStructure and LittleEndianStructure now define an empty __slots__ so that subclasses don't always get an instance dict.
Patch by Claudiu Popa.
1 parent fa9211b commit 5ce8f35

3 files changed

Lines changed: 26 additions & 0 deletions

File tree

Lib/ctypes/_endian.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def __setattr__(self, attrname, value):
4545

4646
class BigEndianStructure(Structure, metaclass=_swapped_meta):
4747
"""Structure with big endian byte order"""
48+
__slots__ = ()
4849
_swappedbytes_ = None
4950

5051
elif sys.byteorder == "big":
@@ -53,6 +54,7 @@ class BigEndianStructure(Structure, metaclass=_swapped_meta):
5354
BigEndianStructure = Structure
5455
class LittleEndianStructure(Structure, metaclass=_swapped_meta):
5556
"""Structure with little endian byte order"""
57+
__slots__ = ()
5658
_swappedbytes_ = None
5759

5860
else:

Lib/ctypes/test/test_byteswap.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,26 @@ def test_X(self):
2222
setattr(bits, "i%s" % i, 1)
2323
dump(bits)
2424

25+
def test_slots(self):
26+
class BigPoint(BigEndianStructure):
27+
__slots__ = ()
28+
_fields_ = [("x", c_int), ("y", c_int)]
29+
30+
class LowPoint(LittleEndianStructure):
31+
__slots__ = ()
32+
_fields_ = [("x", c_int), ("y", c_int)]
33+
34+
big = BigPoint()
35+
little = LowPoint()
36+
big.x = 4
37+
big.y = 2
38+
little.x = 2
39+
little.y = 4
40+
with self.assertRaises(AttributeError):
41+
big.z = 42
42+
with self.assertRaises(AttributeError):
43+
little.z = 24
44+
2545
def test_endian_short(self):
2646
if sys.byteorder == "little":
2747
self.assertIs(c_short.__ctype_le__, c_short)

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ Core and Builtins
124124
Library
125125
-------
126126

127+
- Issue #22098: ctypes' BigEndianStructure and LittleEndianStructure now
128+
define an empty __slots__ so that subclasses don't always get an instance
129+
dict. Patch by Claudiu Popa.
130+
127131
- Issue #22185: Fix an occasional RuntimeError in threading.Condition.wait()
128132
caused by mutation of the waiters queue without holding the lock. Patch
129133
by Doug Zongker.

0 commit comments

Comments
 (0)