gh-105751: Remove dead code in test_ctypes (#105817)

* Remove "except: print(tp); raise" debug code.
* Remove unused NoNullHandle() function.
* Remove commented code.
This commit is contained in:
Victor Stinner 2023-06-15 11:31:09 +02:00 committed by GitHub
parent da911a6b22
commit 0841ca7932
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 33 additions and 124 deletions

View file

@ -81,7 +81,6 @@ class BasicWrapTestCase(unittest.TestCase):
MyCallback = CFUNCTYPE(c_int, c_int) MyCallback = CFUNCTYPE(c_int, c_int)
def callback(value): def callback(value):
#print "called back with", value
return value return value
cb = MyCallback(callback) cb = MyCallback(callback)
@ -118,7 +117,6 @@ class BasicWrapTestCase(unittest.TestCase):
f.argtypes = [c_int, MyCallback] f.argtypes = [c_int, MyCallback]
def callback(value): def callback(value):
#print "called back with", value
self.assertEqual(type(value), int) self.assertEqual(type(value), int)
return value return value

View file

@ -31,8 +31,6 @@ class BITS(Structure):
func = CDLL(_ctypes_test.__file__).unpack_bitfields func = CDLL(_ctypes_test.__file__).unpack_bitfields
func.argtypes = POINTER(BITS), c_char func.argtypes = POINTER(BITS), c_char
##for n in "ABCDEFGHIMNOPQRS":
## print n, hex(getattr(BITS, n).size), getattr(BITS, n).offset
class C_Test(unittest.TestCase): class C_Test(unittest.TestCase):

View file

@ -19,9 +19,6 @@ from test import support
class Callbacks(unittest.TestCase): class Callbacks(unittest.TestCase):
functype = CFUNCTYPE functype = CFUNCTYPE
## def tearDown(self):
## gc.collect()
def callback(self, *args): def callback(self, *args):
self.got_args = args self.got_args = args
return args[-1] return args[-1]
@ -43,8 +40,6 @@ class Callbacks(unittest.TestCase):
self.assertEqual(self.got_args, (-3, arg)) self.assertEqual(self.got_args, (-3, arg))
self.assertEqual(result, arg) self.assertEqual(result, arg)
################
def test_byte(self): def test_byte(self):
self.check_type(c_byte, 42) self.check_type(c_byte, 42)
self.check_type(c_byte, -42) self.check_type(c_byte, -42)

View file

@ -23,7 +23,7 @@ class Test_OpenGL_libs(unittest.TestCase):
lib_glu = find_library("GLU") lib_glu = find_library("GLU")
lib_gle = find_library("gle") lib_gle = find_library("gle")
## print, for debugging # print, for debugging
if test.support.verbose: if test.support.verbose:
print("OpenGL libraries:") print("OpenGL libraries:")
for item in (("GL", lib_gl), for item in (("GL", lib_gl),

View file

@ -73,17 +73,9 @@ class CFuncPtrTestCase(unittest.TestCase):
WNDPROC_2 = WINFUNCTYPE(c_long, c_int, c_int, c_int, c_int) WNDPROC_2 = WINFUNCTYPE(c_long, c_int, c_int, c_int, c_int)
# This is no longer true, now that WINFUNCTYPE caches created types internally.
## # CFuncPtr subclasses are compared by identity, so this raises a TypeError:
## self.assertRaises(TypeError, setattr, wndclass,
## "lpfnWndProc", WNDPROC_2(wndproc))
# instead:
self.assertIs(WNDPROC, WNDPROC_2) self.assertIs(WNDPROC, WNDPROC_2)
# 'wndclass.lpfnWndProc' leaks 94 references. Why?
self.assertEqual(wndclass.lpfnWndProc(1, 2, 3, 4), 10) self.assertEqual(wndclass.lpfnWndProc(1, 2, 3, 4), 10)
f = wndclass.lpfnWndProc f = wndclass.lpfnWndProc
del wndclass del wndclass
@ -92,24 +84,14 @@ class CFuncPtrTestCase(unittest.TestCase):
self.assertEqual(f(10, 11, 12, 13), 46) self.assertEqual(f(10, 11, 12, 13), 46)
def test_dllfunctions(self): def test_dllfunctions(self):
def NoNullHandle(value):
if not value:
raise ctypes.WinError()
return value
strchr = lib.my_strchr strchr = lib.my_strchr
strchr.restype = c_char_p strchr.restype = c_char_p
strchr.argtypes = (c_char_p, c_char) strchr.argtypes = (c_char_p, c_char)
self.assertEqual(strchr(b"abcdefghi", b"b"), b"bcdefghi") self.assertEqual(strchr(b"abcdefghi", b"b"), b"bcdefghi")
self.assertEqual(strchr(b"abcdefghi", b"x"), None) self.assertEqual(strchr(b"abcdefghi", b"x"), None)
strtok = lib.my_strtok strtok = lib.my_strtok
strtok.restype = c_char_p strtok.restype = c_char_p
# Neither of this does work: strtok changes the buffer it is passed
## strtok.argtypes = (c_char_p, c_char_p)
## strtok.argtypes = (c_string, c_char_p)
def c_string(init): def c_string(init):
size = len(init) + 1 size = len(init) + 1
@ -118,10 +100,6 @@ class CFuncPtrTestCase(unittest.TestCase):
s = b"a\nb\nc" s = b"a\nb\nc"
b = c_string(s) b = c_string(s)
## b = (c_char * (len(s)+1))()
## b.value = s
## b = c_string(s)
self.assertEqual(strtok(b, b"\n"), b"a") self.assertEqual(strtok(b, b"\n"), b"a")
self.assertEqual(strtok(None, b"\n"), b"b") self.assertEqual(strtok(None, b"\n"), b"b")
self.assertEqual(strtok(None, b"\n"), b"c") self.assertEqual(strtok(None, b"\n"), b"c")

View file

@ -227,7 +227,6 @@ class FunctionTestCase(unittest.TestCase):
result = f(byref(c_int(99))) result = f(byref(c_int(99)))
self.assertNotEqual(result.contents, 99) self.assertNotEqual(result.contents, 99)
################################################################
def test_shorts(self): def test_shorts(self):
f = dll._testfunc_callback_i_if f = dll._testfunc_callback_i_if
@ -245,9 +244,6 @@ class FunctionTestCase(unittest.TestCase):
f(2**18, cb) f(2**18, cb)
self.assertEqual(args, expected) self.assertEqual(args, expected)
################################################################
def test_callbacks(self): def test_callbacks(self):
f = dll._testfunc_callback_i_if f = dll._testfunc_callback_i_if
f.restype = c_int f.restype = c_int
@ -256,7 +252,6 @@ class FunctionTestCase(unittest.TestCase):
MyCallback = CFUNCTYPE(c_int, c_int) MyCallback = CFUNCTYPE(c_int, c_int)
def callback(value): def callback(value):
#print "called back with", value
return value return value
cb = MyCallback(callback) cb = MyCallback(callback)
@ -289,7 +284,6 @@ class FunctionTestCase(unittest.TestCase):
f.argtypes = [c_int, MyCallback] f.argtypes = [c_int, MyCallback]
def callback(value): def callback(value):
#print "called back with", value
self.assertEqual(type(value), int) self.assertEqual(type(value), int)
return value return value

View file

@ -80,9 +80,6 @@ class ObjectsTestCase(unittest.TestCase):
y = Y() y = Y()
y.x = x y.x = x
self.assertEqual(y._objects, {"0": {"0": s1, "1": s2}}) self.assertEqual(y._objects, {"0": {"0": s1, "1": s2}})
## x = y.x
## del y
## print x._b_base_._objects
def test_ptr_struct(self): def test_ptr_struct(self):
class X(Structure): class X(Structure):
@ -94,9 +91,6 @@ class ObjectsTestCase(unittest.TestCase):
x = X() x = X()
x.data = a x.data = a
##XXX print x._objects
##XXX print x.data[0]
##XXX print x.data._objects
if __name__ == '__main__': if __name__ == '__main__':

View file

@ -115,19 +115,14 @@ class DeletePointerTestCase(unittest.TestCase):
c_int(99) c_int(99)
x.p[0] x.p[0]
print(x.p[0]) print(x.p[0])
## del x
## print "2?", sys.getrefcount(i)
## del i
gc.collect() gc.collect()
for i in range(320): for i in range(320):
c_int(99) c_int(99)
x.p[0] x.p[0]
print(x.p[0]) print(x.p[0])
print(x.p.contents) print(x.p.contents)
## print x._objects
x.p[0] = "spam spam" x.p[0] = "spam spam"
## print x.p[0]
print("+" * 42) print("+" * 42)
print(x._objects) print(x._objects)
@ -144,9 +139,6 @@ class PointerToStructure(unittest.TestCase):
r.a = pointer(p1) r.a = pointer(p1)
r.b = pointer(p1) r.b = pointer(p1)
## from pprint import pprint as pp
## pp(p1._objects)
## pp(r._objects)
r.a[0].x = 42 r.a[0].x = 42
r.a[0].y = 99 r.a[0].y = 99

View file

@ -225,7 +225,6 @@ class c_int_S(_SimpleCData):
def run_test(rep, msg, func, arg=None): def run_test(rep, msg, func, arg=None):
## items = [None] * rep
items = range(rep) items = range(rep)
from time import perf_counter as clock from time import perf_counter as clock
if arg is not None: if arg is not None:
@ -243,7 +242,6 @@ def run_test(rep, msg, func, arg=None):
def check_perf(): def check_perf():
# Construct 5 objects # Construct 5 objects
from ctypes import c_int
REP = 200000 REP = 200000

View file

@ -103,8 +103,6 @@ class SimpleTypesTestCase(unittest.TestCase):
def test_int_pointers(self): def test_int_pointers(self):
LPINT = POINTER(c_int) LPINT = POINTER(c_int)
## p = pointer(c_int(42))
## x = LPINT.from_param(p)
x = LPINT.from_param(pointer(c_int(42))) x = LPINT.from_param(pointer(c_int(42)))
self.assertEqual(x.contents.value, 42) self.assertEqual(x.contents.value, 42)
self.assertEqual(LPINT(c_int(42)).contents.value, 42) self.assertEqual(LPINT(c_int(42)).contents.value, 42)

View file

@ -31,57 +31,47 @@ class Test(unittest.TestCase):
for tp, fmt, shape, itemtp in native_types: for tp, fmt, shape, itemtp in native_types:
ob = tp() ob = tp()
v = memoryview(ob) v = memoryview(ob)
try: self.assertEqual(normalize(v.format), normalize(fmt))
self.assertEqual(normalize(v.format), normalize(fmt)) if shape:
if shape: self.assertEqual(len(v), shape[0])
self.assertEqual(len(v), shape[0]) else:
else: self.assertRaises(TypeError, len, v)
self.assertRaises(TypeError, len, v) self.assertEqual(v.itemsize, sizeof(itemtp))
self.assertEqual(v.itemsize, sizeof(itemtp)) self.assertEqual(v.shape, shape)
self.assertEqual(v.shape, shape) # XXX Issue #12851: PyCData_NewGetBuffer() must provide strides
# XXX Issue #12851: PyCData_NewGetBuffer() must provide strides # if requested. memoryview currently reconstructs missing
# if requested. memoryview currently reconstructs missing # stride information, so this assert will fail.
# stride information, so this assert will fail. # self.assertEqual(v.strides, ())
# self.assertEqual(v.strides, ())
# they are always read/write # they are always read/write
self.assertFalse(v.readonly) self.assertFalse(v.readonly)
n = 1 n = 1
for dim in v.shape: for dim in v.shape:
n = n * dim n = n * dim
self.assertEqual(n * v.itemsize, len(v.tobytes())) self.assertEqual(n * v.itemsize, len(v.tobytes()))
except:
# so that we can see the failing type
print(tp)
raise
def test_endian_types(self): def test_endian_types(self):
for tp, fmt, shape, itemtp in endian_types: for tp, fmt, shape, itemtp in endian_types:
ob = tp() ob = tp()
v = memoryview(ob) v = memoryview(ob)
try: self.assertEqual(v.format, fmt)
self.assertEqual(v.format, fmt) if shape:
if shape: self.assertEqual(len(v), shape[0])
self.assertEqual(len(v), shape[0]) else:
else: self.assertRaises(TypeError, len, v)
self.assertRaises(TypeError, len, v) self.assertEqual(v.itemsize, sizeof(itemtp))
self.assertEqual(v.itemsize, sizeof(itemtp)) self.assertEqual(v.shape, shape)
self.assertEqual(v.shape, shape) # XXX Issue #12851
# XXX Issue #12851 # self.assertEqual(v.strides, ())
# self.assertEqual(v.strides, ())
# they are always read/write # they are always read/write
self.assertFalse(v.readonly) self.assertFalse(v.readonly)
n = 1 n = 1
for dim in v.shape: for dim in v.shape:
n = n * dim n = n * dim
self.assertEqual(n * v.itemsize, len(v.tobytes())) self.assertEqual(n * v.itemsize, len(v.tobytes()))
except:
# so that we can see the failing type
print(tp)
raise
# define some structure classes # define some structure classes

View file

@ -37,7 +37,6 @@ class PointersTestCase(unittest.TestCase):
func.restype = c_long func.restype = c_long
i = c_int(12345678) i = c_int(12345678)
## func.argtypes = (POINTER(c_int),)
address = func(byref(i)) address = func(byref(i))
self.assertEqual(c_int.from_address(address).value, 12345678) self.assertEqual(c_int.from_address(address).value, 12345678)
@ -80,9 +79,7 @@ class PointersTestCase(unittest.TestCase):
def func(arg): def func(arg):
for i in range(10): for i in range(10):
## print arg[i],
self.result.append(arg[i]) self.result.append(arg[i])
## print
return 0 return 0
callback = PROTOTYPE(func) callback = PROTOTYPE(func)
@ -92,25 +89,15 @@ class PointersTestCase(unittest.TestCase):
# The int pointer points to a table containing the numbers 1..10 # The int pointer points to a table containing the numbers 1..10
doit = dll._testfunc_callback_with_pointer doit = dll._testfunc_callback_with_pointer
## i = c_int(42)
## callback(byref(i))
## self.assertEqual(i.value, 84)
doit(callback) doit(callback)
## print self.result
doit(callback) doit(callback)
## print self.result
def test_basics(self): def test_basics(self):
for ct, pt in zip(ctype_types, python_types): for ct, pt in zip(ctype_types, python_types):
i = ct(42) i = ct(42)
p = pointer(i) p = pointer(i)
## print type(p.contents), ct
self.assertIs(type(p.contents), ct) self.assertIs(type(p.contents), ct)
# p.contents is the same as p[0] # p.contents is the same as p[0]
## print p.contents
## self.assertEqual(p.contents, 42)
## self.assertEqual(p[0], 42)
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
del p[0] del p[0]
@ -118,11 +105,7 @@ class PointersTestCase(unittest.TestCase):
def test_from_address(self): def test_from_address(self):
a = array.array('i', [100, 200, 300, 400, 500]) a = array.array('i', [100, 200, 300, 400, 500])
addr = a.buffer_info()[0] addr = a.buffer_info()[0]
p = POINTER(POINTER(c_int)) p = POINTER(POINTER(c_int))
## print dir(p)
## print p.from_address
## print p.from_address(addr)[0][0]
def test_other(self): def test_other(self):
class Table(Structure): class Table(Structure):

View file

@ -20,7 +20,6 @@ class RefcountTestCase(unittest.TestCase):
f.argtypes = [ctypes.c_int, MyCallback] f.argtypes = [ctypes.c_int, MyCallback]
def callback(value): def callback(value):
#print "called back with", value
return value return value
self.assertEqual(sys.getrefcount(callback), 2) self.assertEqual(sys.getrefcount(callback), 2)

View file

@ -46,13 +46,10 @@ class StringArrayTestCase(unittest.TestCase):
def test_param_1(self): def test_param_1(self):
BUF = c_char * 4 BUF = c_char * 4
buf = BUF() buf = BUF()
## print c_char_p.from_param(buf)
def test_param_2(self): def test_param_2(self):
BUF = c_char * 4 BUF = c_char * 4
buf = BUF() buf = BUF()
## print BUF.from_param(c_char_p("python"))
## print BUF.from_param(BUF(*"pyth"))
def test_del_segfault(self): def test_del_segfault(self):
BUF = c_char * 4 BUF = c_char * 4

View file

@ -381,9 +381,6 @@ class StructureTestCase(unittest.TestCase):
self.assertEqual((cls, msg), (TypeError, "abstract class")) self.assertEqual((cls, msg), (TypeError, "abstract class"))
def test_methods(self): def test_methods(self):
## class X(Structure):
## _fields_ = []
self.assertIn("in_dll", dir(type(Structure))) self.assertIn("in_dll", dir(type(Structure)))
self.assertIn("from_address", dir(type(Structure))) self.assertIn("from_address", dir(type(Structure)))
self.assertIn("in_dll", dir(type(Structure))) self.assertIn("in_dll", dir(type(Structure)))
@ -770,8 +767,6 @@ class PointerMemberTestCase(unittest.TestCase):
s.array[0] = 1 s.array[0] = 1
## s.array[1] = 42
items = [s.array[i] for i in range(3)] items = [s.array[i] for i in range(3)]
self.assertEqual(items, [1, 2, 3]) self.assertEqual(items, [1, 2, 3])