8000 gh-105733: Deprecate ctypes SetPointerType() and ARRAY() · python/cpython@bc0a349 · GitHub
[go: up one dir, main page]

Skip to content

Commit bc0a349

Browse files
committed
gh-105733: Deprecate ctypes SetPointerType() and ARRAY()
1 parent abfbab6 commit bc0a349

File tree

5 files changed

+47
-9
lines changed

5 files changed

+47
-9
lines changed

Doc/whatsnew/3.13.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ Deprecated
140140
Use the ``'w'`` format code instead.
141141
(contributed by Hugo van Kemenade in :gh:`80480`)
142142

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+
143149
Removed
144150
=======
145151

Lib/ctypes/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,9 @@ def create_unicode_buffer(init, size=None):
302302
raise TypeError(init)
303303

304304

305-
# XXX Deprecated
306305
def SetPointerType(pointer, cls):
306+
import warnings
307+
warnings._deprecated("ctypes.SetPointerType", remove=(3, 15))
307308
if _pointer_type_cache.get(cls, None) is not None:
308309
raise RuntimeError("This type already exists in the cache")
309310
if id(pointer) not in _pointer_type_cache:
@@ -312,8 +313,9 @@ def SetPointerType(pointer, cls):
312313
_pointer_type_cache[cls] = pointer
313314
del _pointer_type_cache[id(pointer)]
314315

315-
# XXX Deprecated
316316
def ARRAY(typ, len):
317+
import warnings
318+
warnings._deprecated("ctypes.ARRAY", remove=(3, 15))
317319
return typ * len
318320

319321
################################################################

Lib/test/test_ctypes/test_arrays.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import unittest
2-
from test.support import bigmemtest, _2G
1+
import ctypes
32
import sys
3+
import unittest
4+
import warnings
45
from ctypes import *
6+
from test.support import bigmemtest, _2G
57

68
from test.test_ctypes import need_symbol
79

@@ -10,6 +12,14 @@
1012
formats = c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, \
1113
c_long, c_ulonglong, c_float, c_double, c_longdouble
1214

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+
1323
class ArrayTestCase(unittest.TestCase):
1424
def test_simple(self):
1525
# create classes holding simple numeric types, and check
@@ -234,5 +244,10 @@ def test_bpo36504_signed_int_overflow(self):
234244
def test_large_array(self, size):
235245
c_char * size
236246

247+
def test_deprecation(self):
248+
with self.assertWarns(DeprecationWarning):
249+
CharArray = ctypes.ARRAY(c_char, 3)
250+
251+
237252
if __name__ == '__main__':
238253
unittest.main()

Lib/test/test_ctypes/test_incomplete.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
1+
import ctypes
12
import unittest
3+
import warnings
24
from ctypes import *
35

46
################################################################
57
#
68
# The incomplete pointer example from the tutorial
79
#
810

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()
1016

1117
def test_incomplete_example(self):
1218
lpcell = POINTER("cell")
1319
class cell(Structure):
1420
_fields_ = [("name", c_char_p),
1521
("next", lpcell)]
1622

17-
SetPointerType(lpcell, cell)
23+
with warnings.catch_warnings():
24+
warnings.simplefilter('ignore', DeprecationWarning)
25+
ctypes.SetPointerType(lpcell, cell)
1826

1927
c1 = cell()
2028
c1.name = b"foo"
@@ -32,9 +40,14 @@ class cell(Structure):
3240
p = p.next[0]
3341
self.assertEqual(result, [b"foo", b"bar"] * 4)
3442

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)
3851

3952
################################################################
4053

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`ctypes`: Deprecate undocumented :func:`!ctypes.SetPointerType` and
2+
:func:`!ctypes.ARRAY` functions. Patch by Victor Stinner.

0 commit comments

Comments
 (0)
0