diff --git a/Lib/test/test_ctypes/test_as_parameter.py b/Lib/test/test_ctypes/test_as_parameter.py index e842d526c53af0..39f70e864757d5 100644 --- a/Lib/test/test_ctypes/test_as_parameter.py +++ b/Lib/test/test_ctypes/test_as_parameter.py @@ -81,7 +81,6 @@ def test_callbacks(self): MyCallback = CFUNCTYPE(c_int, c_int) def callback(value): - #print "called back with", value return value cb = MyCallback(callback) @@ -118,7 +117,6 @@ def test_callbacks_2(self): f.argtypes = [c_int, MyCallback] def callback(value): - #print "called back with", value self.assertEqual(type(value), int) return value diff --git a/Lib/test/test_ctypes/test_bitfields.py b/Lib/test/test_ctypes/test_bitfields.py index d8eb2d668a6a93..d43c56ad371fbd 100644 --- a/Lib/test/test_ctypes/test_bitfields.py +++ b/Lib/test/test_ctypes/test_bitfields.py @@ -31,8 +31,6 @@ class BITS(Structure): func = CDLL(_ctypes_test.__file__).unpack_bitfields 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): diff --git a/Lib/test/test_ctypes/test_callbacks.py b/Lib/test/test_ctypes/test_callbacks.py index 582677b832e5cb..98341bc5610b79 100644 --- a/Lib/test/test_ctypes/test_callbacks.py +++ b/Lib/test/test_ctypes/test_callbacks.py @@ -19,9 +19,6 @@ class Callbacks(unittest.TestCase): functype = CFUNCTYPE -## def tearDown(self): -## gc.collect() - def callback(self, *args): self.got_args = args return args[-1] @@ -43,8 +40,6 @@ def check_type(self, typ, arg): self.assertEqual(self.got_args, (-3, arg)) self.assertEqual(result, arg) - ################ - def test_byte(self): self.check_type(c_byte, 42) self.check_type(c_byte, -42) diff --git a/Lib/test/test_ctypes/test_find.py b/Lib/test/test_ctypes/test_find.py index 7a1658b63cbac7..66ff23e72b5e10 100644 --- a/Lib/test/test_ctypes/test_find.py +++ b/Lib/test/test_ctypes/test_find.py @@ -23,7 +23,7 @@ def setUpClass(cls): lib_glu = find_library("GLU") lib_gle = find_library("gle") - ## print, for debugging + # print, for debugging if test.support.verbose: print("OpenGL libraries:") for item in (("GL", lib_gl), diff --git a/Lib/test/test_ctypes/test_funcptr.py b/Lib/test/test_ctypes/test_funcptr.py index 0ea3f3581bbaa6..2ad40647e0cfbb 100644 --- a/Lib/test/test_ctypes/test_funcptr.py +++ b/Lib/test/test_ctypes/test_funcptr.py @@ -73,17 +73,9 @@ class WNDCLASS(Structure): 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) - # 'wndclass.lpfnWndProc' leaks 94 references. Why? self.assertEqual(wndclass.lpfnWndProc(1, 2, 3, 4), 10) - f = wndclass.lpfnWndProc del wndclass @@ -92,24 +84,14 @@ class WNDCLASS(Structure): self.assertEqual(f(10, 11, 12, 13), 46) def test_dllfunctions(self): - - def NoNullHandle(value): - if not value: - raise ctypes.WinError() - return value - strchr = lib.my_strchr strchr.restype = c_char_p strchr.argtypes = (c_char_p, c_char) self.assertEqual(strchr(b"abcdefghi", b"b"), b"bcdefghi") self.assertEqual(strchr(b"abcdefghi", b"x"), None) - strtok = lib.my_strtok 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): size = len(init) + 1 @@ -118,10 +100,6 @@ def c_string(init): s = b"a\nb\nc" 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(None, b"\n"), b"b") self.assertEqual(strtok(None, b"\n"), b"c") diff --git a/Lib/test/test_ctypes/test_functions.py b/Lib/test/test_ctypes/test_functions.py index f8591463c18ad0..9cf680f16620ac 100644 --- a/Lib/test/test_ctypes/test_functions.py +++ b/Lib/test/test_ctypes/test_functions.py @@ -227,7 +227,6 @@ def test_pointers(self): result = f(byref(c_int(99))) self.assertNotEqual(result.contents, 99) - ################################################################ def test_shorts(self): f = dll._testfunc_callback_i_if @@ -245,9 +244,6 @@ def callback(v): f(2**18, cb) self.assertEqual(args, expected) - ################################################################ - - def test_callbacks(self): f = dll._testfunc_callback_i_if f.restype = c_int @@ -256,7 +252,6 @@ def test_callbacks(self): MyCallback = CFUNCTYPE(c_int, c_int) def callback(value): - #print "called back with", value return value cb = MyCallback(callback) @@ -289,7 +284,6 @@ def test_callbacks_2(self): f.argtypes = [c_int, MyCallback] def callback(value): - #print "called back with", value self.assertEqual(type(value), int) return value diff --git a/Lib/test/test_ctypes/test_internals.py b/Lib/test/test_ctypes/test_internals.py index 763a5e8b0f0858..94c9a86c2d06df 100644 --- a/Lib/test/test_ctypes/test_internals.py +++ b/Lib/test/test_ctypes/test_internals.py @@ -80,9 +80,6 @@ class Y(Structure): y = Y() y.x = x self.assertEqual(y._objects, {"0": {"0": s1, "1": s2}}) -## x = y.x -## del y -## print x._b_base_._objects def test_ptr_struct(self): class X(Structure): @@ -94,9 +91,6 @@ class X(Structure): x = X() x.data = a -##XXX print x._objects -##XXX print x.data[0] -##XXX print x.data._objects if __name__ == '__main__': diff --git a/Lib/test/test_ctypes/test_keeprefs.py b/Lib/test/test_ctypes/test_keeprefs.py index f93e3f2676945c..92dd1a08b69200 100644 --- a/Lib/test/test_ctypes/test_keeprefs.py +++ b/Lib/test/test_ctypes/test_keeprefs.py @@ -115,19 +115,14 @@ class X(Structure): c_int(99) x.p[0] print(x.p[0]) -## del x -## print "2?", sys.getrefcount(i) -## del i gc.collect() for i in range(320): c_int(99) x.p[0] print(x.p[0]) print(x.p.contents) -## print x._objects x.p[0] = "spam spam" -## print x.p[0] print("+" * 42) print(x._objects) @@ -144,9 +139,6 @@ class RECT(Structure): r.a = 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].y = 99 diff --git a/Lib/test/test_ctypes/test_memfunctions.py b/Lib/test/test_ctypes/test_memfunctions.py index 2ff95f619e0895..112b27ba48e07e 100644 --- a/Lib/test/test_ctypes/test_memfunctions.py +++ b/Lib/test/test_ctypes/test_memfunctions.py @@ -9,16 +9,15 @@ class MemFunctionsTest(unittest.TestCase): - @unittest.skip('test disabled') def test_overflow(self): # string_at and wstring_at must use the Python calling # convention (which acquires the GIL and checks the Python # error flag). Provoke an error and catch it; see also issue - # #3554: + # gh-47804. self.assertRaises((OverflowError, MemoryError, SystemError), - lambda: wstring_at(u"foo", sys.maxint - 1)) + lambda: wstring_at(u"foo", sys.maxsize - 1)) self.assertRaises((OverflowError, MemoryError, SystemError), - lambda: string_at("foo", sys.maxint - 1)) + lambda: string_at("foo", sys.maxsize - 1)) def test_memmove(self): # large buffers apparently increase the chance that the memory diff --git a/Lib/test/test_ctypes/test_numbers.py b/Lib/test/test_ctypes/test_numbers.py index 052900f519c0b9..7cf0c3235d188c 100644 --- a/Lib/test/test_ctypes/test_numbers.py +++ b/Lib/test/test_ctypes/test_numbers.py @@ -225,7 +225,6 @@ class c_int_S(_SimpleCData): def run_test(rep, msg, func, arg=None): -## items = [None] * rep items = range(rep) from time import perf_counter as clock if arg is not None: @@ -243,7 +242,6 @@ def run_test(rep, msg, func, arg=None): def check_perf(): # Construct 5 objects - from ctypes import c_int REP = 200000 diff --git a/Lib/test/test_ctypes/test_parameters.py b/Lib/test/test_ctypes/test_parameters.py index f43819f004996c..40979212a627d8 100644 --- a/Lib/test/test_ctypes/test_parameters.py +++ b/Lib/test/test_ctypes/test_parameters.py @@ -103,8 +103,6 @@ def test_c_wchar(self): def test_int_pointers(self): LPINT = POINTER(c_int) -## p = pointer(c_int(42)) -## x = LPINT.from_param(p) x = LPINT.from_param(pointer(c_int(42))) self.assertEqual(x.contents.value, 42) self.assertEqual(LPINT(c_int(42)).contents.value, 42) diff --git a/Lib/test/test_ctypes/test_pep3118.py b/Lib/test/test_ctypes/test_pep3118.py index caecbc8def8815..06b2ccecade44e 100644 --- a/Lib/test/test_ctypes/test_pep3118.py +++ b/Lib/test/test_ctypes/test_pep3118.py @@ -31,57 +31,47 @@ def test_native_types(self): for tp, fmt, shape, itemtp in native_types: ob = tp() v = memoryview(ob) - try: - self.assertEqual(normalize(v.format), normalize(fmt)) - if shape: - self.assertEqual(len(v), shape[0]) - else: - self.assertRaises(TypeError, len, v) - self.assertEqual(v.itemsize, sizeof(itemtp)) - self.assertEqual(v.shape, shape) - # XXX Issue #12851: PyCData_NewGetBuffer() must provide strides - # if requested. memoryview currently reconstructs missing - # stride information, so this assert will fail. - # self.assertEqual(v.strides, ()) - - # they are always read/write - self.assertFalse(v.readonly) - - n = 1 - for dim in v.shape: - n = n * dim - self.assertEqual(n * v.itemsize, len(v.tobytes())) - except: - # so that we can see the failing type - print(tp) - raise + self.assertEqual(normalize(v.format), normalize(fmt)) + if shape: + self.assertEqual(len(v), shape[0]) + else: + self.assertRaises(TypeError, len, v) + self.assertEqual(v.itemsize, sizeof(itemtp)) + self.assertEqual(v.shape, shape) + # XXX Issue #12851: PyCData_NewGetBuffer() must provide strides + # if requested. memoryview currently reconstructs missing + # stride information, so this assert will fail. + # self.assertEqual(v.strides, ()) + + # they are always read/write + self.assertFalse(v.readonly) + + n = 1 + for dim in v.shape: + n = n * dim + self.assertEqual(n * v.itemsize, len(v.tobytes())) def test_endian_types(self): for tp, fmt, shape, itemtp in endian_types: ob = tp() v = memoryview(ob) - try: - self.assertEqual(v.format, fmt) - if shape: - self.assertEqual(len(v), shape[0]) - else: - self.assertRaises(TypeError, len, v) - self.assertEqual(v.itemsize, sizeof(itemtp)) - self.assertEqual(v.shape, shape) - # XXX Issue #12851 - # self.assertEqual(v.strides, ()) - - # they are always read/write - self.assertFalse(v.readonly) - - n = 1 - for dim in v.shape: - n = n * dim - self.assertEqual(n * v.itemsize, len(v.tobytes())) - except: - # so that we can see the failing type - print(tp) - raise + self.assertEqual(v.format, fmt) + if shape: + self.assertEqual(len(v), shape[0]) + else: + self.assertRaises(TypeError, len, v) + self.assertEqual(v.itemsize, sizeof(itemtp)) + self.assertEqual(v.shape, shape) + # XXX Issue #12851 + # self.assertEqual(v.strides, ()) + + # they are always read/write + self.assertFalse(v.readonly) + + n = 1 + for dim in v.shape: + n = n * dim + self.assertEqual(n * v.itemsize, len(v.tobytes())) # define some structure classes diff --git a/Lib/test/test_ctypes/test_pointers.py b/Lib/test/test_ctypes/test_pointers.py index fd30109aa521b2..8410174358c19d 100644 --- a/Lib/test/test_ctypes/test_pointers.py +++ b/Lib/test/test_ctypes/test_pointers.py @@ -37,7 +37,6 @@ def test_pass_pointers(self): func.restype = c_long i = c_int(12345678) -## func.argtypes = (POINTER(c_int),) address = func(byref(i)) self.assertEqual(c_int.from_address(address).value, 12345678) @@ -80,9 +79,7 @@ def test_callbacks_with_pointers(self): def func(arg): for i in range(10): -## print arg[i], self.result.append(arg[i]) -## print return 0 callback = PROTOTYPE(func) @@ -92,25 +89,15 @@ def func(arg): # The int pointer points to a table containing the numbers 1..10 doit = dll._testfunc_callback_with_pointer -## i = c_int(42) -## callback(byref(i)) -## self.assertEqual(i.value, 84) - doit(callback) -## print self.result doit(callback) -## print self.result def test_basics(self): for ct, pt in zip(ctype_types, python_types): i = ct(42) p = pointer(i) -## print type(p.contents), ct self.assertIs(type(p.contents), ct) # 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): del p[0] @@ -118,11 +105,7 @@ def test_basics(self): def test_from_address(self): a = array.array('i', [100, 200, 300, 400, 500]) addr = a.buffer_info()[0] - p = POINTER(POINTER(c_int)) -## print dir(p) -## print p.from_address -## print p.from_address(addr)[0][0] def test_other(self): class Table(Structure): diff --git a/Lib/test/test_ctypes/test_refcounts.py b/Lib/test/test_ctypes/test_refcounts.py index a1fe89105aee9c..a90588ca9bb1b6 100644 --- a/Lib/test/test_ctypes/test_refcounts.py +++ b/Lib/test/test_ctypes/test_refcounts.py @@ -20,7 +20,6 @@ def test_1(self): f.argtypes = [ctypes.c_int, MyCallback] def callback(value): - #print "called back with", value return value self.assertEqual(sys.getrefcount(callback), 2) diff --git a/Lib/test/test_ctypes/test_strings.py b/Lib/test/test_ctypes/test_strings.py index 26f036adfb7585..3ecc6fe180adf7 100644 --- a/Lib/test/test_ctypes/test_strings.py +++ b/Lib/test/test_ctypes/test_strings.py @@ -1,5 +1,6 @@ import unittest -from ctypes import create_string_buffer, sizeof, byref, c_char, c_wchar +from ctypes import (create_string_buffer, create_unicode_buffer, + sizeof, byref, c_char, c_wchar) class StringArrayTestCase(unittest.TestCase): @@ -46,13 +47,10 @@ def test_create_string_buffer_raw(self): def test_param_1(self): BUF = c_char * 4 buf = BUF() -## print c_char_p.from_param(buf) def test_param_2(self): BUF = c_char * 4 buf = BUF() -## print BUF.from_param(c_char_p("python")) -## print BUF.from_param(BUF(*"pyth")) def test_del_segfault(self): BUF = c_char * 4 @@ -91,41 +89,35 @@ def test_wchar(self): repr(byref(c_wchar("x"))) c_wchar("x") - @unittest.skip('test disabled') def test_basic_wstrings(self): - cs = c_wstring("abcdef") - - # XXX This behaviour is about to change: - # len returns the size of the internal buffer in bytes. - # This includes the terminating NUL character. - self.assertEqual(sizeof(cs), 14) - - # The value property is the string up to the first terminating NUL. + cs = create_unicode_buffer("abcdef") self.assertEqual(cs.value, "abcdef") - self.assertEqual(c_wstring("abc\000def").value, "abc") - self.assertEqual(c_wstring("abc\000def").value, "abc") + # value can be changed + cs.value = "abc" + self.assertEqual(cs.value, "abc") + + # string is truncated at NUL character + cs.value = "def\0z" + self.assertEqual(cs.value, "def") - # The raw property is the total buffer contents: - self.assertEqual(cs.raw, "abcdef\000") - self.assertEqual(c_wstring("abc\000def").raw, "abc\000def\000") + self.assertEqual(create_unicode_buffer("abc\0def").value, "abc") - # We can change the value: - cs.value = "ab" - self.assertEqual(cs.value, "ab") - self.assertEqual(cs.raw, "ab\000\000\000\000\000") + # created with an empty string + cs = create_unicode_buffer(3) + self.assertEqual(cs.value, "") - self.assertRaises(TypeError, c_wstring, "123") - self.assertRaises(ValueError, c_wstring, 0) + cs.value = "abc" + self.assertEqual(cs.value, "abc") - @unittest.skip('test disabled') def test_toolong(self): - cs = c_wstring("abcdef") - # Much too long string: - self.assertRaises(ValueError, setattr, cs, "value", "123456789012345") + cs = create_unicode_buffer("abc") + with self.assertRaises(ValueError): + cs.value = "abcdef" - # One char too long values: - self.assertRaises(ValueError, setattr, cs, "value", "1234567") + cs = create_unicode_buffer(4) + with self.assertRaises(ValueError): + cs.value = "abcdef" def run_test(rep, msg, func, arg): diff --git a/Lib/test/test_ctypes/test_structures.py b/Lib/test/test_ctypes/test_structures.py index e656d18934779f..46bf90054be66a 100644 --- a/Lib/test/test_ctypes/test_structures.py +++ b/Lib/test/test_ctypes/test_structures.py @@ -381,9 +381,6 @@ class X(Structure): self.assertEqual((cls, msg), (TypeError, "abstract class")) def test_methods(self): -## class X(Structure): -## _fields_ = [] - self.assertIn("in_dll", dir(type(Structure))) self.assertIn("from_address", dir(type(Structure))) self.assertIn("in_dll", dir(type(Structure))) @@ -770,8 +767,6 @@ class S(Structure): s.array[0] = 1 -## s.array[1] = 42 - items = [s.array[i] for i in range(3)] self.assertEqual(items, [1, 2, 3])