Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
change to check the type's mutablility by type.__flags__
  • Loading branch information
aisk committed Jan 8, 2024
commit 0cd6f8813141da21fb79c35247d8202ac30942ef
4 changes: 4 additions & 0 deletions Lib/test/test_ctypes/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@
PyCArrayType = type(Array)
PyCSimpleType = type(_SimpleCData)
PyCFuncPtrType = type(CFuncPtr)

# type flags
PY_TPFLAGS_DISALLOW_INSTANTIATION = 1 << 7
PY_TPFLAGS_IMMUTABLETYPE = 1 << 8
Comment thread
aisk marked this conversation as resolved.
Outdated
18 changes: 7 additions & 11 deletions Lib/test/test_ctypes/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
c_char, c_wchar, c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint,
c_long, c_ulonglong, c_float, c_double, c_longdouble)
from test.support import bigmemtest, _2G
from .support import _CData, PyCArrayType
from .support import (_CData, PyCArrayType, PY_TPFLAGS_DISALLOW_INSTANTIATION,
PY_TPFLAGS_IMMUTABLETYPE)


formats = "bBhHiIlLqQfd"
Expand All @@ -30,17 +31,12 @@ def test_inheritance_hierarchy(self):
self.assertEqual(PyCArrayType.__name__, "PyCArrayType")
self.assertEqual(type(PyCArrayType), type)

def test_instantiation(self):
with self.assertRaisesRegex(TypeError, "abstract class"):
Array()
def test_type_flags(self):
self.assertTrue(Array.__flags__ & PY_TPFLAGS_IMMUTABLETYPE)
self.assertFalse(Array.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION)

PyCArrayType("Foo", (Array,), {"_length_": 1, "_type_": c_int})

def test_immutability(self):
for t in [Array, PyCArrayType]:
msg = "cannot set 'foo' attribute of immutable type"
with self.assertRaisesRegex(TypeError, msg):
t.foo = "bar"
self.assertTrue(PyCArrayType.__flags__ & PY_TPFLAGS_IMMUTABLETYPE)
self.assertFalse(PyCArrayType.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION)

def test_simple(self):
# create classes holding simple numeric types, and check
Expand Down
18 changes: 7 additions & 11 deletions Lib/test/test_ctypes/test_funcptr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import unittest
from ctypes import (CDLL, Structure, CFUNCTYPE, sizeof, _CFuncPtr,
c_void_p, c_char_p, c_char, c_int, c_uint, c_long)
from .support import _CData, PyCFuncPtrType
from .support import (_CData, PyCFuncPtrType, PY_TPFLAGS_DISALLOW_INSTANTIATION,
PY_TPFLAGS_IMMUTABLETYPE)


try:
Expand All @@ -22,17 +23,12 @@ def test_inheritance_hierarchy(self):
self.assertEqual(PyCFuncPtrType.__name__, "PyCFuncPtrType")
self.assertEqual(type(PyCFuncPtrType), type)

def test_abstract_class(self):
with self.assertRaisesRegex(TypeError, "abstract class"):
_CFuncPtr()
def test_type_flags(self):
self.assertTrue(_CFuncPtr.__flags__ & PY_TPFLAGS_IMMUTABLETYPE)
self.assertFalse(_CFuncPtr.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION)

PyCFuncPtrType("Foo", (_CFuncPtr,), {"_flags_": 0})

def test_immutability(self):
for t in [_CFuncPtr, PyCFuncPtrType]:
msg = "cannot set 'foo' attribute of immutable type"
with self.assertRaisesRegex(TypeError, msg):
t.foo = "bar"
self.assertTrue(PyCFuncPtrType.__flags__ & PY_TPFLAGS_IMMUTABLETYPE)
self.assertFalse(PyCFuncPtrType.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION)

def test_basic(self):
X = WINFUNCTYPE(c_int, c_int, c_int)
Expand Down
19 changes: 7 additions & 12 deletions Lib/test/test_ctypes/test_simplesubclasses.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest
from ctypes import Structure, CFUNCTYPE, c_int, _SimpleCData
from .support import _CData, PyCSimpleType
from .support import (_CData, PyCSimpleType, PY_TPFLAGS_DISALLOW_INSTANTIATION,
PY_TPFLAGS_IMMUTABLETYPE)


class MyInt(c_int):
Expand All @@ -19,18 +20,12 @@ def test_inheritance_hierarchy(self):

self.assertEqual(c_int.mro(), [c_int, _SimpleCData, _CData, object])

def test_abstract_class(self):
with self.assertRaisesRegex(TypeError, "abstract class"):
_SimpleCData()

PyCSimpleType("Foo", (_SimpleCData,), {"_type_": "i"})

def test_immutability(self):
for t in [_SimpleCData, PyCSimpleType]:
msg = "cannot set 'foo' attribute of immutable type"
with self.assertRaisesRegex(TypeError, msg):
t.foo = "bar"
def test_type_flags(self):
self.assertTrue(_SimpleCData.__flags__ & PY_TPFLAGS_IMMUTABLETYPE)
self.assertFalse(_SimpleCData.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION)

self.assertTrue(PyCSimpleType.__flags__ & PY_TPFLAGS_IMMUTABLETYPE)
self.assertFalse(PyCSimpleType.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION)

def test_compare(self):
self.assertEqual(MyInt(3), MyInt(3))
Expand Down
17 changes: 7 additions & 10 deletions Lib/test/test_ctypes/test_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
from struct import calcsize
from collections import namedtuple
from test import support
from .support import _CData, PyCStructType
from .support import (_CData, PyCStructType, PY_TPFLAGS_DISALLOW_INSTANTIATION,
PY_TPFLAGS_IMMUTABLETYPE)


class SubclassesTest(unittest.TestCase):
Expand Down Expand Up @@ -77,17 +78,13 @@ def test_inheritance_hierarchy(self):
self.assertEqual(PyCStructType.__name__, "PyCStructType")
self.assertEqual(type(PyCStructType), type)

def test_instantiation(self):
with self.assertRaisesRegex(TypeError, "abstract class"):
Structure()

PyCStructType("Foo", (Structure,), {})
def test_type_flags(self):
self.assertTrue(Structure.__flags__ & PY_TPFLAGS_IMMUTABLETYPE)
self.assertFalse(Structure.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION)

def test_immutability(self):
for t in [Structure, PyCStructType]:
msg = "cannot set 'foo' attribute of immutable type"
with self.assertRaisesRegex(TypeError, msg):
t.foo = "bar"
self.assertTrue(PyCStructType.__flags__ & PY_TPFLAGS_IMMUTABLETYPE)
self.assertFalse(PyCStructType.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION)

def test_simple_structs(self):
for code, tp in self.formats.items():
Expand Down
22 changes: 7 additions & 15 deletions Lib/test/test_ctypes/test_unions.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import ctypes
import sys
import unittest
import warnings
from ctypes import Union
from .support import _CData, UnionType
from .support import (_CData, UnionType, PY_TPFLAGS_DISALLOW_INSTANTIATION,
PY_TPFLAGS_IMMUTABLETYPE)


class ArrayTestCase(unittest.TestCase):
Expand All @@ -13,15 +11,9 @@ def test_inheritance_hierarchy(self):
self.assertEqual(UnionType.__name__, "UnionType")
self.assertEqual(type(UnionType), type)

def test_instantiation(self):
with self.assertRaisesRegex(TypeError, "abstract class"):
Union()
def test_type_flags(self):
self.assertTrue(Union.__flags__ & PY_TPFLAGS_IMMUTABLETYPE)
self.assertFalse(Union.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION)

UnionType("Foo", (Union,), {})

def test_immutability(self):
Union.foo = "bar"

msg = "cannot set 'foo' attribute of immutable type"
with self.assertRaisesRegex(TypeError, msg):
UnionType.foo = "bar"
self.assertTrue(UnionType.__flags__ & PY_TPFLAGS_IMMUTABLETYPE)
self.assertFalse(UnionType.__flags__ & PY_TPFLAGS_DISALLOW_INSTANTIATION)