Skip to content

Commit 0841ca7

Browse files
authored
gh-105751: Remove dead code in test_ctypes (#105817)
* Remove "except: print(tp); raise" debug code. * Remove unused NoNullHandle() function. * Remove commented code.
1 parent da911a6 commit 0841ca7

15 files changed

+37
-128
lines changed

Lib/test/test_ctypes/test_as_parameter.py

-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ def test_callbacks(self):
8181
MyCallback = CFUNCTYPE(c_int, c_int)
8282

8383
def callback(value):
84-
#print "called back with", value
8584
return value
8685

8786
cb = MyCallback(callback)
@@ -118,7 +117,6 @@ def test_callbacks_2(self):
118117
f.argtypes = [c_int, MyCallback]
119118

120119
def callback(value):
121-
#print "called back with", value
122120
self.assertEqual(type(value), int)
123121
return value
124122

Lib/test/test_ctypes/test_bitfields.py

-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ class BITS(Structure):
3131
func = CDLL(_ctypes_test.__file__).unpack_bitfields
3232
func.argtypes = POINTER(BITS), c_char
3333

34-
##for n in "ABCDEFGHIMNOPQRS":
35-
## print n, hex(getattr(BITS, n).size), getattr(BITS, n).offset
3634

3735
class C_Test(unittest.TestCase):
3836

Lib/test/test_ctypes/test_callbacks.py

-5
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
class Callbacks(unittest.TestCase):
2020
functype = CFUNCTYPE
2121

22-
## def tearDown(self):
23-
## gc.collect()
24-
2522
def callback(self, *args):
2623
self.got_args = args
2724
return args[-1]
@@ -43,8 +40,6 @@ def check_type(self, typ, arg):
4340
self.assertEqual(self.got_args, (-3, arg))
4441
self.assertEqual(result, arg)
4542

46-
################
47-
4843
def test_byte(self):
4944
self.check_type(c_byte, 42)
5045
self.check_type(c_byte, -42)

Lib/test/test_ctypes/test_find.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def setUpClass(cls):
2323
lib_glu = find_library("GLU")
2424
lib_gle = find_library("gle")
2525

26-
## print, for debugging
26+
# print, for debugging
2727
if test.support.verbose:
2828
print("OpenGL libraries:")
2929
for item in (("GL", lib_gl),

Lib/test/test_ctypes/test_funcptr.py

-22
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,9 @@ class WNDCLASS(Structure):
7373

7474
WNDPROC_2 = WINFUNCTYPE(c_long, c_int, c_int, c_int, c_int)
7575

76-
# This is no longer true, now that WINFUNCTYPE caches created types internally.
77-
## # CFuncPtr subclasses are compared by identity, so this raises a TypeError:
78-
## self.assertRaises(TypeError, setattr, wndclass,
79-
## "lpfnWndProc", WNDPROC_2(wndproc))
80-
# instead:
81-
8276
self.assertIs(WNDPROC, WNDPROC_2)
83-
# 'wndclass.lpfnWndProc' leaks 94 references. Why?
8477
self.assertEqual(wndclass.lpfnWndProc(1, 2, 3, 4), 10)
8578

86-
8779
f = wndclass.lpfnWndProc
8880

8981
del wndclass
@@ -92,24 +84,14 @@ class WNDCLASS(Structure):
9284
self.assertEqual(f(10, 11, 12, 13), 46)
9385

9486
def test_dllfunctions(self):
95-
96-
def NoNullHandle(value):
97-
if not value:
98-
raise ctypes.WinError()
99-
return value
100-
10187
strchr = lib.my_strchr
10288
strchr.restype = c_char_p
10389
strchr.argtypes = (c_char_p, c_char)
10490
self.assertEqual(strchr(b"abcdefghi", b"b"), b"bcdefghi")
10591
self.assertEqual(strchr(b"abcdefghi", b"x"), None)
10692

107-
10893
strtok = lib.my_strtok
10994
strtok.restype = c_char_p
110-
# Neither of this does work: strtok changes the buffer it is passed
111-
## strtok.argtypes = (c_char_p, c_char_p)
112-
## strtok.argtypes = (c_string, c_char_p)
11395

11496
def c_string(init):
11597
size = len(init) + 1
@@ -118,10 +100,6 @@ def c_string(init):
118100
s = b"a\nb\nc"
119101
b = c_string(s)
120102

121-
## b = (c_char * (len(s)+1))()
122-
## b.value = s
123-
124-
## b = c_string(s)
125103
self.assertEqual(strtok(b, b"\n"), b"a")
126104
self.assertEqual(strtok(None, b"\n"), b"b")
127105
self.assertEqual(strtok(None, b"\n"), b"c")

Lib/test/test_ctypes/test_functions.py

-6
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ def test_pointers(self):
227227
result = f(byref(c_int(99)))
228228
self.assertNotEqual(result.contents, 99)
229229

230-
################################################################
231230
def test_shorts(self):
232231
f = dll._testfunc_callback_i_if
233232

@@ -245,9 +244,6 @@ def callback(v):
245244
f(2**18, cb)
246245
self.assertEqual(args, expected)
247246

248-
################################################################
249-
250-
251247
def test_callbacks(self):
252248
f = dll._testfunc_callback_i_if
253249
f.restype = c_int
@@ -256,7 +252,6 @@ def test_callbacks(self):
256252
MyCallback = CFUNCTYPE(c_int, c_int)
257253

258254
def callback(value):
259-
#print "called back with", value
260255
return value
261256

262257
cb = MyCallback(callback)
@@ -289,7 +284,6 @@ def test_callbacks_2(self):
289284
f.argtypes = [c_int, MyCallback]
290285

291286
def callback(value):
292-
#print "called back with", value
293287
self.assertEqual(type(value), int)
294288
return value
295289

Lib/test/test_ctypes/test_internals.py

-6
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,6 @@ class Y(Structure):
8080
y = Y()
8181
y.x = x
8282
self.assertEqual(y._objects, {"0": {"0": s1, "1": s2}})
83-
## x = y.x
84-
## del y
85-
## print x._b_base_._objects
8683

8784
def test_ptr_struct(self):
8885
class X(Structure):
@@ -94,9 +91,6 @@ class X(Structure):
9491

9592
x = X()
9693
x.data = a
97-
##XXX print x._objects
98-
##XXX print x.data[0]
99-
##XXX print x.data._objects
10094

10195

10296
if __name__ == '__main__':

Lib/test/test_ctypes/test_keeprefs.py

-8
Original file line numberDiff line numberDiff line change
@@ -115,19 +115,14 @@ class X(Structure):
115115
c_int(99)
116116
x.p[0]
117117
print(x.p[0])
118-
## del x
119-
## print "2?", sys.getrefcount(i)
120-
## del i
121118
gc.collect()
122119
for i in range(320):
123120
c_int(99)
124121
x.p[0]
125122
print(x.p[0])
126123
print(x.p.contents)
127-
## print x._objects
128124

129125
x.p[0] = "spam spam"
130-
## print x.p[0]
131126
print("+" * 42)
132127
print(x._objects)
133128

@@ -144,9 +139,6 @@ class RECT(Structure):
144139

145140
r.a = pointer(p1)
146141
r.b = pointer(p1)
147-
## from pprint import pprint as pp
148-
## pp(p1._objects)
149-
## pp(r._objects)
150142

151143
r.a[0].x = 42
152144
r.a[0].y = 99

Lib/test/test_ctypes/test_numbers.py

-2
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ class c_int_S(_SimpleCData):
225225

226226

227227
def run_test(rep, msg, func, arg=None):
228-
## items = [None] * rep
229228
items = range(rep)
230229
from time import perf_counter as clock
231230
if arg is not None:
@@ -243,7 +242,6 @@ def run_test(rep, msg, func, arg=None):
243242

244243
def check_perf():
245244
# Construct 5 objects
246-
from ctypes import c_int
247245

248246
REP = 200000
249247

Lib/test/test_ctypes/test_parameters.py

-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ def test_c_wchar(self):
103103
def test_int_pointers(self):
104104
LPINT = POINTER(c_int)
105105

106-
## p = pointer(c_int(42))
107-
## x = LPINT.from_param(p)
108106
x = LPINT.from_param(pointer(c_int(42)))
109107
self.assertEqual(x.contents.value, 42)
110108
self.assertEqual(LPINT(c_int(42)).contents.value, 42)

Lib/test/test_ctypes/test_pep3118.py

+36-46
Original file line numberDiff line numberDiff line change
@@ -31,57 +31,47 @@ def test_native_types(self):
3131
for tp, fmt, shape, itemtp in native_types:
3232
ob = tp()
3333
v = memoryview(ob)
34-
try:
35-
self.assertEqual(normalize(v.format), normalize(fmt))
36-
if shape:
37-
self.assertEqual(len(v), shape[0])
38-
else:
39-
self.assertRaises(TypeError, len, v)
40-
self.assertEqual(v.itemsize, sizeof(itemtp))
41-
self.assertEqual(v.shape, shape)
42-
# XXX Issue #12851: PyCData_NewGetBuffer() must provide strides
43-
# if requested. memoryview currently reconstructs missing
44-
# stride information, so this assert will fail.
45-
# self.assertEqual(v.strides, ())
46-
47-
# they are always read/write
48-
self.assertFalse(v.readonly)
49-
50-
n = 1
51-
for dim in v.shape:
52-
n = n * dim
53-
self.assertEqual(n * v.itemsize, len(v.tobytes()))
54-
except:
55-
# so that we can see the failing type
56-
print(tp)
57-
raise
34+
self.assertEqual(normalize(v.format), normalize(fmt))
35+
if shape:
36+
self.assertEqual(len(v), shape[0])
37+
else:
38+
self.assertRaises(TypeError, len, v)
39+
self.assertEqual(v.itemsize, sizeof(itemtp))
40+
self.assertEqual(v.shape, shape)
41+
# XXX Issue #12851: PyCData_NewGetBuffer() must provide strides
42+
# if requested. memoryview currently reconstructs missing
43+
# stride information, so this assert will fail.
44+
# self.assertEqual(v.strides, ())
45+
46+
# they are always read/write
47+
self.assertFalse(v.readonly)
48+
49+
n = 1
50+
for dim in v.shape:
51+
n = n * dim
52+
self.assertEqual(n * v.itemsize, len(v.tobytes()))
5853

5954
def test_endian_types(self):
6055
for tp, fmt, shape, itemtp in endian_types:
6156
ob = tp()
6257
v = memoryview(ob)
63-
try:
64-
self.assertEqual(v.format, fmt)
65-
if shape:
66-
self.assertEqual(len(v), shape[0])
67-
else:
68-
self.assertRaises(TypeError, len, v)
69-
self.assertEqual(v.itemsize, sizeof(itemtp))
70-
self.assertEqual(v.shape, shape)
71-
# XXX Issue #12851
72-
# self.assertEqual(v.strides, ())
73-
74-
# they are always read/write
75-
self.assertFalse(v.readonly)
76-
77-
n = 1
78-
for dim in v.shape:
79-
n = n * dim
80-
self.assertEqual(n * v.itemsize, len(v.tobytes()))
81-
except:
82-
# so that we can see the failing type
83-
print(tp)
84-
raise
58+
self.assertEqual(v.format, fmt)
59+
if shape:
60+
self.assertEqual(len(v), shape[0])
61+
else:
62+
self.assertRaises(TypeError, len, v)
63+
self.assertEqual(v.itemsize, sizeof(itemtp))
64+
self.assertEqual(v.shape, shape)
65+
# XXX Issue #12851
66+
# self.assertEqual(v.strides, ())
67+
68+
# they are always read/write
69+
self.assertFalse(v.readonly)
70+
71+
n = 1
72+
for dim in v.shape:
73+
n = n * dim
74+
self.assertEqual(n * v.itemsize, len(v.tobytes()))
8575

8676

8777
# define some structure classes

Lib/test/test_ctypes/test_pointers.py

-17
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ def test_pass_pointers(self):
3737
func.restype = c_long
3838

3939
i = c_int(12345678)
40-
## func.argtypes = (POINTER(c_int),)
4140
address = func(byref(i))
4241
self.assertEqual(c_int.from_address(address).value, 12345678)
4342

@@ -80,9 +79,7 @@ def test_callbacks_with_pointers(self):
8079

8180
def func(arg):
8281
for i in range(10):
83-
## print arg[i],
8482
self.result.append(arg[i])
85-
## print
8683
return 0
8784
callback = PROTOTYPE(func)
8885

@@ -92,37 +89,23 @@ def func(arg):
9289
# The int pointer points to a table containing the numbers 1..10
9390
doit = dll._testfunc_callback_with_pointer
9491

95-
## i = c_int(42)
96-
## callback(byref(i))
97-
## self.assertEqual(i.value, 84)
98-
9992
doit(callback)
100-
## print self.result
10193
doit(callback)
102-
## print self.result
10394

10495
def test_basics(self):
10596
for ct, pt in zip(ctype_types, python_types):
10697
i = ct(42)
10798
p = pointer(i)
108-
## print type(p.contents), ct
10999
self.assertIs(type(p.contents), ct)
110100
# p.contents is the same as p[0]
111-
## print p.contents
112-
## self.assertEqual(p.contents, 42)
113-
## self.assertEqual(p[0], 42)
114101

115102
with self.assertRaises(TypeError):
116103
del p[0]
117104

118105
def test_from_address(self):
119106
a = array.array('i', [100, 200, 300, 400, 500])
120107
addr = a.buffer_info()[0]
121-
122108
p = POINTER(POINTER(c_int))
123-
## print dir(p)
124-
## print p.from_address
125-
## print p.from_address(addr)[0][0]
126109

127110
def test_other(self):
128111
class Table(Structure):

Lib/test/test_ctypes/test_refcounts.py

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ def test_1(self):
2020
f.argtypes = [ctypes.c_int, MyCallback]
2121

2222
def callback(value):
23-
#print "called back with", value
2423
return value
2524

2625
self.assertEqual(sys.getrefcount(callback), 2)

Lib/test/test_ctypes/test_strings.py

-3
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,10 @@ def test_create_string_buffer_raw(self):
4646
def test_param_1(self):
4747
BUF = c_char * 4
4848
buf = BUF()
49-
## print c_char_p.from_param(buf)
5049

5150
def test_param_2(self):
5251
BUF = c_char * 4
5352
buf = BUF()
54-
## print BUF.from_param(c_char_p("python"))
55-
## print BUF.from_param(BUF(*"pyth"))
5653

5754
def test_del_segfault(self):
5855
BUF = c_char * 4

0 commit comments

Comments
 (0)