8000 gh-105733: Deprecate ctypes SetPointerType() and ARRAY() by vstinner · Pull Request #105734 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=======

Expand Down
6 changes: 4 additions & 2 deletions Lib/ctypes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Comment on lines 316 to 319
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think there's a high expected maintenance overhead or security risk here?

Per PEP 387:

If the expected maintenance overhead and security risk of the deprecated behavior is small (e.g. an old function is reimplemented in terms of a new, more general one), it can stay indefinitely (or until the situation changes).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not a big maintenance burden nor a securty issue, why do you ask?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ask to see if there's a special reason for remove=(3, 15), or if it can be relaxed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you propose? To postpone the removal to Python 3.16?

On PyPI top 8,000 projects (at 2024-03-16), I found only 2 projects using ctypes.ARRAY. I search for regex ctypes.ARRAY|\<ARRAY\> (and excluded unrelated lines).

  • astroid (3.1.0)
  • pwntools (4.12.0)

It's about two lines:

astroid-3.1.0.tar.gz: astroid-3.1.0/tests/brain/test_ctypes.py: ctypes.ARRAY(3, 2)
pwntools-4.12.0.tar.gz: pwntools-4.12.0/pwnlib/adb/bootloader.py: imgarray = ctypes.ARRAY(img_info, self.num_images)

Before the function was only deprecated with a comment:

# XXX Deprecated
def ARRAY(typ, len):
    return typ * len

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll propose to follow the part of PEP 387 I quoted above. Alternatively, since this does break known projects, to soft-deprecate instead.

But I plan to propose that in the beta period, right now I'm only gathering info. Thanks for the answer!


################################################################
Expand Down
19 changes: 17 additions & 2 deletions Lib/test/test_ctypes/test_arrays.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -10,6 +12,14 @@
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
Expand Down Expand Up @@ -234,5 +244,10 @@ def test_bpo36504_signed_int_overflow(self):
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()
23 changes: 18 additions & 5 deletions Lib/test/test_ctypes/test_incomplete.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import ctypes
import unittest
import warnings
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")
class cell(Structure):
_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"
Expand All @@ -32,9 +40,14 @@ class cell(Structure):
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)

################################################################

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`ctypes`: Deprecate undocumented :func:`!ctypes.SetPointerType` and
:func:`!ctypes.ARRAY` functions. Patch by Victor Stinner.
0