gh-105733: Deprecate ctypes SetPointerType() and ARRAY() (#105734)

This commit is contained in:
Victor Stinner 2023-06-13 20:16:26 +02:00 committed by GitHub
parent b97e14a806
commit 2211454fe2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 9 deletions

View File

@ -140,6 +140,12 @@ Deprecated
Use the ``'w'`` format code instead.
(contributed by Hugo van Kemenade in :gh:`80480`)
* :mod:`ctypes`: Deprecate undocumented :func:`!ctypes.SetPointerType`
and :func:`!ctypes.ARRAY` functions.
Replace ``ctypes.SetPointerType(item_type, size)`` with ``item_type * size``.
(Contributed by Victor Stinner in :gh:`105733`.)
Removed
=======

View File

@ -302,8 +302,9 @@ def create_unicode_buffer(init, size=None):
raise TypeError(init)
# XXX Deprecated
def SetPointerType(pointer, cls):
import warnings
warnings._deprecated("ctypes.SetPointerType", remove=(3, 15))
if _pointer_type_cache.get(cls, None) is not None:
raise RuntimeError("This type already exists in the cache")
if id(pointer) not in _pointer_type_cache:
@ -312,8 +313,9 @@ def SetPointerType(pointer, cls):
_pointer_type_cache[cls] = pointer
del _pointer_type_cache[id(pointer)]
# XXX Deprecated
def ARRAY(typ, len):
import warnings
warnings._deprecated("ctypes.ARRAY", remove=(3, 15))
return typ * len
################################################################

View File

@ -1,7 +1,9 @@
import unittest
from test.support import bigmemtest, _2G
import ctypes
import sys
import unittest
import warnings
from ctypes import *
from test.support import bigmemtest, _2G
from test.test_ctypes import need_symbol
@ -10,6 +12,14 @@ formats = "bBhHiIlLqQfd"
formats = c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, \
c_long, c_ulonglong, c_float, c_double, c_longdouble
def ARRAY(*args):
# ignore DeprecationWarning in tests
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
return ctypes.ARRAY(*args)
class ArrayTestCase(unittest.TestCase):
def test_simple(self):
# create classes holding simple numeric types, and check
@ -234,5 +244,10 @@ class ArrayTestCase(unittest.TestCase):
def test_large_array(self, size):
c_char * size
def test_deprecation(self):
with self.assertWarns(DeprecationWarning):
CharArray = ctypes.ARRAY(c_char, 3)
if __name__ == '__main__':
unittest.main()

View File

@ -1,4 +1,6 @@
import ctypes
import unittest
import warnings
from ctypes import *
################################################################
@ -6,7 +8,11 @@ from ctypes import *
# The incomplete pointer example from the tutorial
#
class MyTestCase(unittest.TestCase):
class TestSetPointerType(unittest.TestCase):
def tearDown(self):
# to not leak references, we must clean _pointer_type_cache
ctypes._reset_cache()
def test_incomplete_example(self):
lpcell = POINTER("cell")
@ -14,7 +20,9 @@ class MyTestCase(unittest.TestCase):
_fields_ = [("name", c_char_p),
("next", lpcell)]
SetPointerType(lpcell, cell)
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
ctypes.SetPointerType(lpcell, cell)
c1 = cell()
c1.name = b"foo"
@ -32,9 +40,14 @@ class MyTestCase(unittest.TestCase):
p = p.next[0]
self.assertEqual(result, [b"foo", b"bar"] * 4)
# to not leak references, we must clean _pointer_type_cache
from ctypes import _pointer_type_cache
del _pointer_type_cache[cell]
def test_deprecation(self):
lpcell = POINTER("cell")
class cell(Structure):
_fields_ = [("name", c_char_p),
("next", lpcell)]
with self.assertWarns(DeprecationWarning):
ctypes.SetPointerType(lpcell, cell)
################################################################

View File

@ -0,0 +1,2 @@
:mod:`ctypes`: Deprecate undocumented :func:`!ctypes.SetPointerType` and
:func:`!ctypes.ARRAY` functions. Patch by Victor Stinner.