Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0be4ba0

Browse files
committed
gh-105733: Deprecate ctypes SetPointerType() and ARRAY()
1 parentb97e14a commit0be4ba0

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
raiseTypeError(init)
303303

304304

305-
# XXX Deprecated
306305
defSetPointerType(pointer,cls):
306+
importwarnings
307+
warnings._deprecated("ctypes.SetPointerType",remove=(3,15))
307308
if_pointer_type_cache.get(cls,None)isnotNone:
308309
raiseRuntimeError("This type already exists in the cache")
309310
ifid(pointer)notin_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
defARRAY(typ,len):
317+
importwarnings
318+
warnings._deprecated("ctypes.ARRAY",remove=(3,15))
317319
returntyp*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-
importunittest
2-
fromtest.supportimportbigmemtest,_2G
1+
importctypes
32
importsys
3+
importunittest
4+
importwarnings
45
fromctypesimport*
6+
fromtest.supportimportbigmemtest,_2G
57

68
fromtest.test_ctypesimportneed_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+
defARRAY(*args):
17+
# ignore DeprecationWarning in tests
18+
withwarnings.catch_warnings():
19+
warnings.simplefilter('ignore',DeprecationWarning)
20+
returnctypes.ARRAY(*args)
21+
22+
1323
classArrayTestCase(unittest.TestCase):
1424
deftest_simple(self):
1525
# create classes holding simple numeric types, and check
@@ -234,5 +244,10 @@ def test_bpo36504_signed_int_overflow(self):
234244
deftest_large_array(self,size):
235245
c_char*size
236246

247+
deftest_deprecation(self):
248+
withself.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+
importctypes
12
importunittest
3+
importwarnings
24
fromctypesimport*
35

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

9-
classMyTestCase(unittest.TestCase):
11+
classTestSetPointerType(unittest.TestCase):
12+
13+
deftearDown(self):
14+
# to not leak references, we must clean _pointer_type_cache
15+
ctypes._reset_cache()
1016

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

17-
SetPointerType(lpcell,cell)
23+
withwarnings.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-
fromctypesimport_pointer_type_cache
37-
del_pointer_type_cache[cell]
43+
deftest_deprecation(self):
44+
lpcell=POINTER("cell")
45+
classcell(Structure):
46+
_fields_= [("name",c_char_p),
47+
("next",lpcell)]
48+
49+
withself.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)

[8]ページ先頭

©2009-2025 Movatter.jp