]> granicus.if.org Git - python/commitdiff
Issue #22098: ctypes' BigEndianStructure and LittleEndianStructure now define an...
authorAntoine Pitrou <solipsis@pitrou.net>
Fri, 29 Aug 2014 22:37:18 +0000 (00:37 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Fri, 29 Aug 2014 22:37:18 +0000 (00:37 +0200)
Patch by Claudiu Popa.

Lib/ctypes/_endian.py
Lib/ctypes/test/test_byteswap.py
Misc/NEWS

index dae65fc2152d7e66a6cad1247e1c99bc577b6119..37444bd6a7dd60263d2833312643b02c899776ea 100644 (file)
@@ -45,6 +45,7 @@ if sys.byteorder == "little":
 
     class BigEndianStructure(Structure, metaclass=_swapped_meta):
         """Structure with big endian byte order"""
+        __slots__ = ()
         _swappedbytes_ = None
 
 elif sys.byteorder == "big":
@@ -53,6 +54,7 @@ elif sys.byteorder == "big":
     BigEndianStructure = Structure
     class LittleEndianStructure(Structure, metaclass=_swapped_meta):
         """Structure with little endian byte order"""
+        __slots__ = ()
         _swappedbytes_ = None
 
 else:
index 427bb8bcc1088d242601ce883bf700517372a2ff..01c97e83ca7af68592232af930442cb5c6436ea5 100644 (file)
@@ -22,6 +22,26 @@ class Test(unittest.TestCase):
             setattr(bits, "i%s" % i, 1)
             dump(bits)
 
+    def test_slots(self):
+        class BigPoint(BigEndianStructure):
+            __slots__ = ()
+            _fields_ = [("x", c_int), ("y", c_int)]
+
+        class LowPoint(LittleEndianStructure):
+            __slots__ = ()
+            _fields_ = [("x", c_int), ("y", c_int)]
+
+        big = BigPoint()
+        little = LowPoint()
+        big.x = 4
+        big.y = 2
+        little.x = 2
+        little.y = 4
+        with self.assertRaises(AttributeError):
+            big.z = 42
+        with self.assertRaises(AttributeError):
+            little.z = 24
+
     def test_endian_short(self):
         if sys.byteorder == "little":
             self.assertIs(c_short.__ctype_le__, c_short)
index 68c5d9052f2dfd04f8c47431503e01f9fe804694..9a27b5ddd1fc30a28828b00fc84f8780b08a205d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -124,6 +124,10 @@ Core and Builtins
 Library
 -------
 
+- 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.
+
 - Issue #22185: Fix an occasional RuntimeError in threading.Condition.wait()
   caused by mutation of the waiters queue without holding the lock.  Patch
   by Doug Zongker.