Skip to content

Commit a1e15a7

Browse files
authored
bpo-45082: Cleanup ctypes.c_buffer alias (pythonGH-28129)
* Remove commented deprecation of ctypes.c_buffer. * Remove references to ctypes.c_string which doesn't exist. * Remove StringTestCase: it only had skipped test methods.
1 parent 0635e20 commit a1e15a7

File tree

4 files changed

+7
-99
lines changed

4 files changed

+7
-99
lines changed

Doc/library/ctypes.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,9 @@ property::
330330
10 b'Hi\x00lo\x00\x00\x00\x00\x00'
331331
>>>
332332

333-
The :func:`create_string_buffer` function replaces the :func:`c_buffer` function
334-
(which is still available as an alias), as well as the :func:`c_string` function
335-
from earlier ctypes releases. To create a mutable memory block containing
336-
unicode characters of the C type :c:type:`wchar_t` use the
333+
The :func:`create_string_buffer` function replaces the old :func:`c_buffer`
334+
function (which is still available as an alias). To create a mutable memory
335+
block containing unicode characters of the C type :c:type:`wchar_t`, use the
337336
:func:`create_unicode_buffer` function.
338337

339338

Lib/ctypes/__init__.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,8 @@ def create_string_buffer(init, size=None):
6565
return buf
6666
raise TypeError(init)
6767

68-
def c_buffer(init, size=None):
69-
## "deprecated, use create_string_buffer instead"
70-
## import warnings
71-
## warnings.warn("c_buffer is deprecated, use create_string_buffer instead",
72-
## DeprecationWarning, stacklevel=2)
73-
return create_string_buffer(init, size)
68+
# Alias to create_string_buffer() for backward compatibility
69+
c_buffer = create_string_buffer
7470

7571
_c_functype_cache = {}
7672
def CFUNCTYPE(restype, *argtypes, **kw):

Lib/ctypes/test/test_strings.py

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -85,74 +85,6 @@ def test_nonbmp(self):
8585
w = c_wchar(u)
8686
self.assertEqual(w.value, u)
8787

88-
class StringTestCase(unittest.TestCase):
89-
@unittest.skip('test disabled')
90-
def test_basic_strings(self):
91-
cs = c_string("abcdef")
92-
93-
# Cannot call len on a c_string any longer
94-
self.assertRaises(TypeError, len, cs)
95-
self.assertEqual(sizeof(cs), 7)
96-
97-
# The value property is the string up to the first terminating NUL.
98-
self.assertEqual(cs.value, "abcdef")
99-
self.assertEqual(c_string("abc\000def").value, "abc")
100-
101-
# The raw property is the total buffer contents:
102-
self.assertEqual(cs.raw, "abcdef\000")
103-
self.assertEqual(c_string("abc\000def").raw, "abc\000def\000")
104-
105-
# We can change the value:
106-
cs.value = "ab"
107-
self.assertEqual(cs.value, "ab")
108-
self.assertEqual(cs.raw, "ab\000\000\000\000\000")
109-
110-
cs.raw = "XY"
111-
self.assertEqual(cs.value, "XY")
112-
self.assertEqual(cs.raw, "XY\000\000\000\000\000")
113-
114-
self.assertRaises(TypeError, c_string, "123")
115-
116-
@unittest.skip('test disabled')
117-
def test_sized_strings(self):
118-
119-
# New in releases later than 0.4.0:
120-
self.assertRaises(TypeError, c_string, None)
121-
122-
# New in releases later than 0.4.0:
123-
# c_string(number) returns an empty string of size number
124-
self.assertEqual(len(c_string(32).raw), 32)
125-
self.assertRaises(ValueError, c_string, -1)
126-
self.assertRaises(ValueError, c_string, 0)
127-
128-
# These tests fail, because it is no longer initialized
129-
## self.assertEqual(c_string(2).value, "")
130-
## self.assertEqual(c_string(2).raw, "\000\000")
131-
self.assertEqual(c_string(2).raw[-1], "\000")
132-
self.assertEqual(len(c_string(2).raw), 2)
133-
134-
@unittest.skip('test disabled')
135-
def test_initialized_strings(self):
136-
137-
self.assertEqual(c_string("ab", 4).raw[:2], "ab")
138-
self.assertEqual(c_string("ab", 4).raw[:2:], "ab")
139-
self.assertEqual(c_string("ab", 4).raw[:2:-1], "ba")
140-
self.assertEqual(c_string("ab", 4).raw[:2:2], "a")
141-
self.assertEqual(c_string("ab", 4).raw[-1], "\000")
142-
self.assertEqual(c_string("ab", 2).raw, "a\000")
143-
144-
@unittest.skip('test disabled')
145-
def test_toolong(self):
146-
cs = c_string("abcdef")
147-
# Much too long string:
148-
self.assertRaises(ValueError, setattr, cs, "value", "123456789012345")
149-
150-
# One char too long values:
151-
self.assertRaises(ValueError, setattr, cs, "value", "1234567")
152-
153-
@unittest.skip('test disabled')
154-
def test_perf(self):
155-
check_perf()
15688

15789
@need_symbol('c_wchar')
15890
class WStringTestCase(unittest.TestCase):
@@ -208,25 +140,6 @@ def run_test(rep, msg, func, arg):
208140
stop = clock()
209141
print("%20s: %.2f us" % (msg, ((stop-start)*1e6/5/rep)))
210142

211-
def check_perf():
212-
# Construct 5 objects
213-
214-
REP = 200000
215-
216-
run_test(REP, "c_string(None)", c_string, None)
217-
run_test(REP, "c_string('abc')", c_string, 'abc')
218-
219-
# Python 2.3 -OO, win2k, P4 700 MHz:
220-
#
221-
# c_string(None): 1.75 us
222-
# c_string('abc'): 2.74 us
223-
224-
# Python 2.2 -OO, win2k, P4 700 MHz:
225-
#
226-
# c_string(None): 2.95 us
227-
# c_string('abc'): 3.67 us
228-
229143

230144
if __name__ == '__main__':
231-
## check_perf()
232145
unittest.main()

Modules/_ctypes/callproc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,8 +530,8 @@ PyCArg_repr(PyCArgObject *self)
530530
}
531531

532532
/* Hm, are these 'z' and 'Z' codes useful at all?
533-
Shouldn't they be replaced by the functionality of c_string
534-
and c_wstring ?
533+
Shouldn't they be replaced by the functionality of create_string_buffer()
534+
and c_wstring() ?
535535
*/
536536
case 'z':
537537
case 'Z':

0 commit comments

Comments
 (0)