Skip to content

Commit 698a0da

Browse files
authored
gh-105751: Cleanup test_ctypes imports (#105803)
* Move imports at top level and sort imports. * Replace c_buffer() with create_string_buffer(): c_buffer is a deprecated alias. * PEP 8: Add empty lines for readability between imports and classes.
1 parent d1b0297 commit 698a0da

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+250
-240
lines changed

Lib/test/test_ctypes/test_anon.py

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import test.support
33
from ctypes import c_int, Union, Structure, sizeof
44

5+
56
class AnonTest(unittest.TestCase):
67

78
def test_anon(self):
@@ -69,5 +70,6 @@ class Y(Structure):
6970
self.assertEqual(Y._.offset, sizeof(c_int))
7071
self.assertEqual(Y.y.offset, sizeof(c_int) * 2)
7172

73+
7274
if __name__ == "__main__":
7375
unittest.main()

Lib/test/test_ctypes/test_array_in_pointer.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
1+
import binascii
2+
import re
13
import unittest
24
from ctypes import c_byte, Structure, POINTER, cast
3-
from binascii import hexlify
4-
import re
5+
56

67
def dump(obj):
78
# helper function to dump memory contents in hex, with a hyphen
89
# between the bytes.
9-
h = hexlify(memoryview(obj)).decode()
10+
h = binascii.hexlify(memoryview(obj)).decode()
1011
return re.sub(r"(..)", r"\1-", h)[:-1]
1112

1213

1314
class Value(Structure):
1415
_fields_ = [("val", c_byte)]
1516

17+
1618
class Container(Structure):
1719
_fields_ = [("pvalues", POINTER(Value))]
1820

21+
1922
class Test(unittest.TestCase):
2023
def test(self):
2124
# create an array of 4 values
@@ -60,5 +63,6 @@ def test_2(self):
6063
([1, 2, 3, 4], "01-02-03-04")
6164
)
6265

66+
6367
if __name__ == "__main__":
6468
unittest.main()

Lib/test/test_ctypes/test_as_parameter.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
# fake to enable this test on Linux
1717
CALLBACK_FUNCTYPE = CFUNCTYPE
1818

19+
1920
class POINT(Structure):
2021
_fields_ = [("x", c_int), ("y", c_int)]
2122

23+
2224
class BasicWrapTestCase(unittest.TestCase):
2325
def wrap(self, param):
2426
return param
@@ -71,8 +73,6 @@ def callback(v):
7173
f(self.wrap(2**18), self.wrap(cb))
7274
self.assertEqual(args, expected)
7375

74-
################################################################
75-
7676
def test_callbacks(self):
7777
f = dll._testfunc_callback_i_if
7878
f.restype = c_int
@@ -194,8 +194,6 @@ class S8I(Structure):
194194
(9*2, 8*3, 7*4, 6*5, 5*6, 4*7, 3*8, 2*9))
195195

196196
def test_recursive_as_param(self):
197-
from ctypes import c_int
198-
199197
class A(object):
200198
pass
201199

@@ -205,16 +203,13 @@ class A(object):
205203
c_int.from_param(a)
206204

207205

208-
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
209-
210206
class AsParamWrapper(object):
211207
def __init__(self, param):
212208
self._as_parameter_ = param
213209

214210
class AsParamWrapperTestCase(BasicWrapTestCase):
215211
wrap = AsParamWrapper
216212

217-
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
218213

219214
class AsParamPropertyWrapper(object):
220215
def __init__(self, param):
@@ -227,7 +222,6 @@ def getParameter(self):
227222
class AsParamPropertyWrapperTestCase(BasicWrapTestCase):
228223
wrap = AsParamPropertyWrapper
229224

230-
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
231225

232226
if __name__ == '__main__':
233227
unittest.main()

Lib/test/test_ctypes/test_bitfields.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1+
import _ctypes_test
2+
import os
3+
import unittest
14
from ctypes import (CDLL, Structure, sizeof, POINTER, byref, alignment,
25
LittleEndianStructure, BigEndianStructure,
36
c_byte, c_ubyte, c_char, c_char_p, c_void_p, c_wchar,
47
c_uint32, c_uint64,
58
c_short, c_ushort, c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong)
69
from test import support
7-
import unittest
8-
import os
910

10-
import _ctypes_test
1111

1212
class BITS(Structure):
1313
_fields_ = [("A", c_int, 1),
@@ -56,6 +56,7 @@ def test_shorts(self):
5656
setattr(b, name, i)
5757
self.assertEqual(getattr(b, name), func(byref(b), name.encode('ascii')))
5858

59+
5960
signed_int_types = (c_byte, c_short, c_int, c_long, c_longlong)
6061
unsigned_int_types = (c_ubyte, c_ushort, c_uint, c_ulong, c_ulonglong)
6162
int_types = unsigned_int_types + signed_int_types
@@ -117,7 +118,6 @@ class X(Structure):
117118
x.a, x.b = 0, -1
118119
self.assertEqual((c_typ, x.a, x.b, x.c), (c_typ, 0, 7, 0))
119120

120-
121121
def fail_fields(self, *fields):
122122
return self.get_except(type(Structure), "X", (),
123123
{"_fields_": fields})
@@ -194,7 +194,6 @@ class X(Structure):
194194
self.assertEqual(X.b.offset, sizeof(c_short)*1)
195195
self.assertEqual(X.c.offset, sizeof(c_short)*2)
196196

197-
198197
def get_except(self, func, *args, **kw):
199198
try:
200199
func(*args, **kw)
@@ -291,5 +290,6 @@ class Big(BigEndianStructure):
291290
x.c = 2
292291
self.assertEqual(b, b'\xab\xcd\xef\x12')
293292

293+
294294
if __name__ == "__main__":
295295
unittest.main()

Lib/test/test_ctypes/test_buffers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import unittest
12
from ctypes import (create_string_buffer, create_unicode_buffer, sizeof,
23
c_char, c_wchar)
3-
import unittest
44

5-
class StringBufferTestCase(unittest.TestCase):
65

6+
class StringBufferTestCase(unittest.TestCase):
77
def test_buffer(self):
88
b = create_string_buffer(32)
99
self.assertEqual(len(b), 32)

Lib/test/test_ctypes/test_bytes.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""Test where byte objects are accepted"""
22
import sys
33
import unittest
4+
from _ctypes import _SimpleCData
45
from ctypes import Structure, c_char, c_char_p, c_wchar, c_wchar_p
56

7+
68
class BytesTest(unittest.TestCase):
79
def test_c_char(self):
810
x = c_char(b"x")
@@ -55,7 +57,6 @@ class X(Structure):
5557

5658
@unittest.skipUnless(sys.platform == "win32", 'Windows-specific test')
5759
def test_BSTR(self):
58-
from _ctypes import _SimpleCData
5960
class BSTR(_SimpleCData):
6061
_type_ = "X"
6162

Lib/test/test_ctypes/test_byteswap.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import sys, unittest, struct, math, ctypes
2-
from binascii import hexlify
3-
1+
import binascii
2+
import ctypes
3+
import math
4+
import struct
5+
import sys
6+
import unittest
47
from ctypes import (Structure, Union, LittleEndianUnion, BigEndianUnion,
58
BigEndianStructure, LittleEndianStructure,
69
POINTER, sizeof, cast,
@@ -9,8 +12,10 @@
912
c_long, c_ulong, c_longlong, c_ulonglong,
1013
c_uint32, c_float, c_double)
1114

15+
1216
def bin(s):
13-
return hexlify(memoryview(s)).decode().upper()
17+
return binascii.hexlify(memoryview(s)).decode().upper()
18+
1419

1520
# Each *simple* type that supports different byte orders has an
1621
# __ctype_be__ attribute that specifies the same type in BIG ENDIAN
@@ -366,5 +371,6 @@ class TestUnion(parent):
366371
self.assertEqual(s.point.x, 1)
367372
self.assertEqual(s.point.y, 2)
368373

374+
369375
if __name__ == "__main__":
370376
unittest.main()

Lib/test/test_ctypes/test_callbacks.py

+7-12
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1+
import _ctypes_test
2+
import ctypes
13
import functools
4+
import gc
5+
import math
26
import sys
37
import unittest
4-
from test import support
5-
6-
import ctypes
8+
from _ctypes import CTYPES_MAX_ARGCOUNT
79
from ctypes import (CDLL, cdll, Structure, CFUNCTYPE,
810
ArgumentError, POINTER, sizeof,
911
c_byte, c_ubyte, c_char, c_char_p,
1012
c_short, c_ushort, c_int, c_uint,
1113
c_long, c_longlong, c_ulonglong, c_ulong,
1214
c_float, c_double, c_longdouble, py_object)
13-
from _ctypes import CTYPES_MAX_ARGCOUNT
14-
import _ctypes_test
15+
from ctypes.util import find_library
16+
from test import support
1517

1618

1719
class Callbacks(unittest.TestCase):
1820
functype = CFUNCTYPE
1921

2022
## def tearDown(self):
21-
## import gc
2223
## gc.collect()
2324

2425
def callback(self, *args):
@@ -81,7 +82,6 @@ def test_ulonglong(self):
8182

8283
def test_float(self):
8384
# only almost equal: double -> float -> double
84-
import math
8585
self.check_type(c_float, math.e)
8686
self.check_type(c_float, -math.e)
8787

@@ -138,7 +138,6 @@ def func(self): pass
138138
def __init__(self):
139139
self.v = proto(self.func)
140140

141-
import gc
142141
for i in range(32):
143142
X()
144143
gc.collect()
@@ -147,7 +146,6 @@ def __init__(self):
147146
self.assertEqual(len(live), 0)
148147

149148
def test_issue12483(self):
150-
import gc
151149
class Nasty:
152150
def __del__(self):
153151
gc.collect()
@@ -172,8 +170,6 @@ class StdcallCallbacks(Callbacks):
172170
functype = ctypes.WINFUNCTYPE
173171

174172

175-
################################################################
176-
177173
class SampleCallbacksTestCase(unittest.TestCase):
178174

179175
def test_integrate(self):
@@ -197,7 +193,6 @@ def func(x):
197193
self.assertLess(diff, 0.01, "%s not less than 0.01" % diff)
198194

199195
def test_issue_8959_a(self):
200-
from ctypes.util import find_library
201196
libc_path = find_library("c")
202197
if not libc_path:
203198
self.skipTest('could not find libc')

Lib/test/test_ctypes/test_cast.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
c_void_p, c_char_p, c_wchar_p,
55
c_byte, c_short, c_int)
66

7-
class Test(unittest.TestCase):
87

8+
class Test(unittest.TestCase):
99
def test_array2pointer(self):
1010
array = (c_int * 3)(42, 17, 2)
1111

@@ -80,7 +80,7 @@ def test_char_p(self):
8080
def test_wchar_p(self):
8181
s = c_wchar_p("hiho")
8282
self.assertEqual(cast(cast(s, c_void_p), c_wchar_p).value,
83-
"hiho")
83+
"hiho")
8484

8585
def test_bad_type_arg(self):
8686
# The type argument must be a ctypes pointer type.
@@ -95,5 +95,6 @@ class MyUnion(Union):
9595
_fields_ = [("a", c_int)]
9696
self.assertRaises(TypeError, cast, array, MyUnion)
9797

98+
9899
if __name__ == "__main__":
99100
unittest.main()

Lib/test/test_ctypes/test_cfuncs.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import unittest
1+
import _ctypes_test
22
import ctypes
3+
import unittest
34
from ctypes import (CDLL,
45
c_byte, c_ubyte, c_char,
56
c_short, c_ushort, c_int, c_uint,
67
c_long, c_ulong, c_longlong, c_ulonglong,
78
c_float, c_double, c_longdouble)
8-
import _ctypes_test
99

1010

1111
class CFunctions(unittest.TestCase):
@@ -190,6 +190,7 @@ def test_void(self):
190190
self.assertEqual(self._dll.tv_i(-42), None)
191191
self.assertEqual(self.S(), -42)
192192

193+
193194
# The following repeats the above tests with stdcall functions (where
194195
# they are available)
195196
if hasattr(ctypes, 'WinDLL'):

Lib/test/test_ctypes/test_checkretval.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import _ctypes_test
12
import ctypes
23
import unittest
34
from ctypes import CDLL, c_int
@@ -11,10 +12,7 @@ def _check_retval_(value):
1112

1213

1314
class Test(unittest.TestCase):
14-
1515
def test_checkretval(self):
16-
17-
import _ctypes_test
1816
dll = CDLL(_ctypes_test.__file__)
1917
self.assertEqual(42, dll._testfunc_p_p(42))
2018

@@ -34,6 +32,5 @@ def test_oledll(self):
3432
self.assertRaises(OSError, oleaut32.CreateTypeLib2, 0, None, None)
3533

3634

37-
3835
if __name__ == "__main__":
3936
unittest.main()

Lib/test/test_ctypes/test_errno.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import unittest, os, errno
2-
import threading
3-
41
import ctypes
2+
import errno
3+
import os
4+
import threading
5+
import unittest
56
from ctypes import CDLL, c_int, c_char_p, c_wchar_p, get_errno, set_errno
67
from ctypes.util import find_library
78

9+
810
class Test(unittest.TestCase):
911
def test_open(self):
1012
libc_name = find_library("c")

0 commit comments

Comments
 (0)