gh-105733: Deprecate ctypes SetPointerType() and ARRAY() (#105734) · miss-islington/cpython@2211454 (original) (raw)
5 files changed
lines changed
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -140,6 +140,12 @@ Deprecated | ||
| 140 | 140 | Use the ``'w'`` format code instead. |
| 141 | 141 | (contributed by Hugo van Kemenade in :gh:`80480`) |
| 142 | 142 | |
| 143 | +* :mod:`ctypes`: Deprecate undocumented :func:`!ctypes.SetPointerType` | |
| 144 | + and :func:`!ctypes.ARRAY` functions. | |
| 145 | + Replace ``ctypes.SetPointerType(item_type, size)`` with ``item_type * size``. | |
| 146 | + (Contributed by Victor Stinner in :gh:`105733`.) | |
| 147 | + | |
| 148 | + | |
| 143 | 149 | Removed |
| 144 | 150 | ======= |
| 145 | 151 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -302,8 +302,9 @@ def create_unicode_buffer(init, size=None): | ||
| 302 | 302 | raise TypeError(init) |
| 303 | 303 | |
| 304 | 304 | |
| 305 | -# XXX Deprecated | |
| 306 | 305 | def SetPointerType(pointer, cls): |
| 306 | +import warnings | |
| 307 | +warnings._deprecated("ctypes.SetPointerType", remove=(3, 15)) | |
| 307 | 308 | if _pointer_type_cache.get(cls, None) is not None: |
| 308 | 309 | raise RuntimeError("This type already exists in the cache") |
| 309 | 310 | if id(pointer) not in _pointer_type_cache: |
| @@ -312,8 +313,9 @@ def SetPointerType(pointer, cls): | ||
| 312 | 313 | _pointer_type_cache[cls] = pointer |
| 313 | 314 | del _pointer_type_cache[id(pointer)] |
| 314 | 315 | |
| 315 | -# XXX Deprecated | |
| 316 | 316 | def ARRAY(typ, len): |
| 317 | +import warnings | |
| 318 | +warnings._deprecated("ctypes.ARRAY", remove=(3, 15)) | |
| 317 | 319 | return typ * len |
| 318 | 320 | |
| 319 | 321 | ################################################################ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| 1 | -import unittest | |
| 2 | -from test.support import bigmemtest, _2G | |
| 1 | +import ctypes | |
| 3 | 2 | import sys |
| 3 | +import unittest | |
| 4 | +import warnings | |
| 4 | 5 | from ctypes import * |
| 6 | +from test.support import bigmemtest, _2G | |
| 5 | 7 | |
| 6 | 8 | from test.test_ctypes import need_symbol |
| 7 | 9 | |
| @@ -10,6 +12,14 @@ | ||
| 10 | 12 | formats = c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, \ |
| 11 | 13 | c_long, c_ulonglong, c_float, c_double, c_longdouble |
| 12 | 14 | |
| 15 | + | |
| 16 | +def ARRAY(*args): | |
| 17 | +# ignore DeprecationWarning in tests | |
| 18 | +with warnings.catch_warnings(): | |
| 19 | +warnings.simplefilter('ignore', DeprecationWarning) | |
| 20 | +return ctypes.ARRAY(*args) | |
| 21 | + | |
| 22 | + | |
| 13 | 23 | class ArrayTestCase(unittest.TestCase): |
| 14 | 24 | def test_simple(self): |
| 15 | 25 | # create classes holding simple numeric types, and check |
| @@ -234,5 +244,10 @@ def test_bpo36504_signed_int_overflow(self): | ||
| 234 | 244 | def test_large_array(self, size): |
| 235 | 245 | c_char * size |
| 236 | 246 | |
| 247 | +def test_deprecation(self): | |
| 248 | +with self.assertWarns(DeprecationWarning): | |
| 249 | +CharArray = ctypes.ARRAY(c_char, 3) | |
| 250 | + | |
| 251 | + | |
| 237 | 252 | if __name__ == '__main__': |
| 238 | 253 | unittest.main() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,28 @@ | ||
| 1 | +import ctypes | |
| 1 | 2 | import unittest |
| 3 | +import warnings | |
| 2 | 4 | from ctypes import * |
| 3 | 5 | |
| 4 | 6 | ################################################################ |
| 5 | 7 | # |
| 6 | 8 | # The incomplete pointer example from the tutorial |
| 7 | 9 | # |
| 8 | 10 | |
| 9 | -class MyTestCase(unittest.TestCase): | |
| 11 | +class TestSetPointerType(unittest.TestCase): | |
| 12 | + | |
| 13 | +def tearDown(self): | |
| 14 | +# to not leak references, we must clean _pointer_type_cache | |
| 15 | +ctypes._reset_cache() | |
| 10 | 16 | |
| 11 | 17 | def test_incomplete_example(self): |
| 12 | 18 | lpcell = POINTER("cell") |
| 13 | 19 | class cell(Structure): |
| 14 | 20 | _fields_ = [("name", c_char_p), |
| 15 | 21 | ("next", lpcell)] |
| 16 | 22 | |
| 17 | -SetPointerType(lpcell, cell) | |
| 23 | +with warnings.catch_warnings(): | |
| 24 | +warnings.simplefilter('ignore', DeprecationWarning) | |
| 25 | +ctypes.SetPointerType(lpcell, cell) | |
| 18 | 26 | |
| 19 | 27 | c1 = cell() |
| 20 | 28 | c1.name = b"foo" |
| @@ -32,9 +40,14 @@ class cell(Structure): | ||
| 32 | 40 | p = p.next[0] |
| 33 | 41 | self.assertEqual(result, [b"foo", b"bar"] * 4) |
| 34 | 42 | |
| 35 | -# to not leak references, we must clean _pointer_type_cache | |
| 36 | -from ctypes import _pointer_type_cache | |
| 37 | -del _pointer_type_cache[cell] | |
| 43 | +def test_deprecation(self): | |
| 44 | +lpcell = POINTER("cell") | |
| 45 | +class cell(Structure): | |
| 46 | +_fields_ = [("name", c_char_p), | |
| 47 | + ("next", lpcell)] | |
| 48 | + | |
| 49 | +with self.assertWarns(DeprecationWarning): | |
| 50 | +ctypes.SetPointerType(lpcell, cell) | |
| 38 | 51 | |
| 39 | 52 | ################################################################ |
| 40 | 53 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| 1 | +:mod:`ctypes`: Deprecate undocumented :func:`!ctypes.SetPointerType` and | |
| 2 | +:func:`!ctypes.ARRAY` functions. Patch by Victor Stinner. |