Skip to content

Commit b95de96

Browse files
authored
gh-105751: test_ctypes avoids "from ctypes import *" (#105768)
Using "import *" prevents linters like pyflakes to detect undefined names (usually missing imports). Replace c_voidp with c_void_p.
1 parent 381a1dc commit b95de96

Some content is hidden

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

48 files changed

+179
-83
lines changed

Lib/ctypes/_endian.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import sys
2-
from ctypes import *
2+
from ctypes import Array, Structure, Union
33

44
_array_type = type(Array)
55

Lib/test/test_ctypes/test_anon.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22
import test.support
3-
from ctypes import *
3+
from ctypes import c_int, Union, Structure, sizeof
44

55
class AnonTest(unittest.TestCase):
66

Lib/test/test_ctypes/test_array_in_pointer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import unittest
2-
from ctypes import *
2+
from ctypes import c_byte, Structure, POINTER, cast
33
from binascii import hexlify
44
import re
55

Lib/test/test_ctypes/test_arrays.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
import sys
33
import unittest
44
import warnings
5-
from ctypes import *
5+
from ctypes import (Structure, Array, sizeof, addressof,
6+
create_string_buffer, create_unicode_buffer,
7+
c_char, c_wchar, c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint,
8+
c_long, c_ulonglong, c_float, c_double, c_longdouble)
69
from test.support import bigmemtest, _2G
710

811
from test.test_ctypes import need_symbol

Lib/test/test_ctypes/test_as_parameter.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
import unittest
1+
import _ctypes_test
22
import ctypes
3-
from ctypes import *
3+
import unittest
4+
from ctypes import (Structure, CDLL, CFUNCTYPE,
5+
POINTER, pointer, byref,
6+
c_short, c_int, c_long, c_longlong,
7+
c_byte, c_wchar, c_float, c_double,
8+
ArgumentError)
49
from test.test_ctypes import need_symbol
5-
import _ctypes_test
610

711
dll = CDLL(_ctypes_test.__file__)
812

Lib/test/test_ctypes/test_bitfields.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
from ctypes import *
1+
from ctypes import (CDLL, Structure, sizeof, POINTER, byref, alignment,
2+
LittleEndianStructure, BigEndianStructure,
3+
c_byte, c_ubyte, c_char, c_char_p, c_void_p, c_wchar,
4+
c_uint32, c_uint64,
5+
c_short, c_ushort, c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong)
26
from test.test_ctypes import need_symbol
37
from test import support
48
import unittest

Lib/test/test_ctypes/test_buffers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from ctypes import *
1+
from ctypes import (create_string_buffer, create_unicode_buffer, sizeof,
2+
c_char, c_wchar)
23
from test.test_ctypes import need_symbol
34
import unittest
45

Lib/test/test_ctypes/test_bytes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Test where byte objects are accepted"""
2-
import unittest
32
import sys
4-
from ctypes import *
3+
import unittest
4+
from ctypes import Structure, c_char, c_char_p, c_wchar, c_wchar_p
55

66
class BytesTest(unittest.TestCase):
77
def test_c_char(self):

Lib/test/test_ctypes/test_byteswap.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import sys, unittest, struct, math, ctypes
22
from binascii import hexlify
33

4-
from ctypes import *
4+
from ctypes import (Structure, Union, LittleEndianUnion, BigEndianUnion,
5+
BigEndianStructure, LittleEndianStructure,
6+
POINTER, sizeof, cast,
7+
c_byte, c_ubyte, c_char, c_wchar, c_void_p,
8+
c_short, c_ushort, c_int, c_uint,
9+
c_long, c_ulong, c_longlong, c_ulonglong,
10+
c_uint32, c_float, c_double)
511

612
def bin(s):
713
return hexlify(memoryview(s)).decode().upper()

Lib/test/test_ctypes/test_callbacks.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
from test import support
44

55
import ctypes
6-
from ctypes import *
6+
from ctypes import (CDLL, cdll, Structure, CFUNCTYPE,
7+
ArgumentError, POINTER, sizeof,
8+
c_byte, c_ubyte, c_char, c_char_p,
9+
c_short, c_ushort, c_int, c_uint,
10+
c_long, c_longlong, c_ulonglong, c_ulong,
11+
c_float, c_double, c_longdouble, py_object)
712
from test.test_ctypes import need_symbol
813
from _ctypes import CTYPES_MAX_ARGCOUNT
914
import _ctypes_test

Lib/test/test_ctypes/test_cast.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
from ctypes import *
2-
from test.test_ctypes import need_symbol
3-
import unittest
41
import sys
2+
import unittest
3+
from ctypes import (Structure, Union, POINTER, cast, sizeof, addressof,
4+
c_void_p, c_char_p, c_wchar_p,
5+
c_byte, c_short, c_int)
6+
from test.test_ctypes import need_symbol
57

68
class Test(unittest.TestCase):
79

@@ -12,7 +14,7 @@ def test_array2pointer(self):
1214
ptr = cast(array, POINTER(c_int))
1315
self.assertEqual([ptr[i] for i in range(3)], [42, 17, 2])
1416

15-
if 2*sizeof(c_short) == sizeof(c_int):
17+
if 2 * sizeof(c_short) == sizeof(c_int):
1618
ptr = cast(array, POINTER(c_short))
1719
if sys.byteorder == "little":
1820
self.assertEqual([ptr[i] for i in range(6)],

Lib/test/test_ctypes/test_cfuncs.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
# A lot of failures in these tests on Mac OS X.
2-
# Byte order related?
3-
41
import unittest
52
import ctypes
6-
from ctypes import *
3+
from ctypes import (CDLL,
4+
c_byte, c_ubyte, c_char,
5+
c_short, c_ushort, c_int, c_uint,
6+
c_long, c_ulong, c_longlong, c_ulonglong,
7+
c_float, c_double, c_longdouble)
78
from test.test_ctypes import need_symbol
8-
99
import _ctypes_test
1010

11+
1112
class CFunctions(unittest.TestCase):
1213
_dll = CDLL(_ctypes_test.__file__)
1314

@@ -210,5 +211,6 @@ def __getattr__(self, name):
210211
class stdcallCFunctions(CFunctions):
211212
_dll = stdcall_dll(_ctypes_test.__file__)
212213

214+
213215
if __name__ == '__main__':
214216
unittest.main()

Lib/test/test_ctypes/test_checkretval.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
import unittest
2-
31
import ctypes
4-
from ctypes import *
2+
import unittest
3+
from ctypes import CDLL, c_int
54
from test.test_ctypes import need_symbol
65

6+
77
class CHECKED(c_int):
88
def _check_retval_(value):
99
# Receives a CHECKED instance.
1010
return str(value.value)
1111
_check_retval_ = staticmethod(_check_retval_)
1212

13+
1314
class Test(unittest.TestCase):
1415

1516
def test_checkretval(self):
@@ -32,5 +33,7 @@ def test_oledll(self):
3233
oleaut32 = ctypes.oledll.oleaut32
3334
self.assertRaises(OSError, oleaut32.CreateTypeLib2, 0, None, None)
3435

36+
37+
3538
if __name__ == "__main__":
3639
unittest.main()

Lib/test/test_ctypes/test_delattr.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import unittest
2-
from ctypes import *
2+
from ctypes import Structure, c_char, c_int
3+
34

45
class X(Structure):
56
_fields_ = [("foo", c_int)]
67

8+
79
class TestCase(unittest.TestCase):
810
def test_simple(self):
911
self.assertRaises(TypeError,
@@ -17,5 +19,6 @@ def test_struct(self):
1719
self.assertRaises(TypeError,
1820
delattr, X(), "foo")
1921

22+
2023
if __name__ == "__main__":
2124
unittest.main()

Lib/test/test_ctypes/test_errno.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
import threading
33

44
import ctypes
5-
from ctypes import *
5+
from ctypes import CDLL, c_int, c_char_p, c_wchar_p, get_errno, set_errno
66
from ctypes.util import find_library
77

88
class Test(unittest.TestCase):
99
def test_open(self):
1010
libc_name = find_library("c")
1111
if libc_name is None:
12-
raise unittest.SkipTest("Unable to find C library")
12+
self.skipTest("Unable to find C library")
13+
1314
libc = CDLL(libc_name, use_errno=True)
1415
if os.name == "nt":
1516
libc_open = libc._open
@@ -73,5 +74,6 @@ def _worker():
7374

7475
ctypes.set_last_error(0)
7576

77+
7678
if __name__ == "__main__":
7779
unittest.main()

Lib/test/test_ctypes/test_find.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import unittest
2-
import unittest.mock
31
import os.path
42
import sys
53
import test.support
6-
from test.support import os_helper
7-
from ctypes import *
4+
import unittest
5+
import unittest.mock
6+
from ctypes import CDLL, RTLD_GLOBAL
87
from ctypes.util import find_library
8+
from test.support import os_helper
9+
910

1011
# On some systems, loading the OpenGL libraries needs the RTLD_GLOBAL mode.
1112
class Test_OpenGL_libs(unittest.TestCase):
@@ -36,11 +37,13 @@ def setUpClass(cls):
3637
cls.gl = CDLL(lib_gl, mode=RTLD_GLOBAL)
3738
except OSError:
3839
pass
40+
3941
if lib_glu:
4042
try:
4143
cls.glu = CDLL(lib_glu, RTLD_GLOBAL)
4244
except OSError:
4345
pass
46+
4447
if lib_gle:
4548
try:
4649
cls.gle = CDLL(lib_gle)

Lib/test/test_ctypes/test_frombuffer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from ctypes import *
21
import array
32
import gc
43
import unittest
4+
from ctypes import Structure, Union, Array, sizeof, c_char, c_int
55

66
class X(Structure):
77
_fields_ = [("c_int", c_int)]

Lib/test/test_ctypes/test_funcptr.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
import unittest
1+
import _ctypes_test
22
import ctypes
3-
from ctypes import *
3+
import unittest
4+
from ctypes import (CDLL, Structure, CFUNCTYPE, sizeof,
5+
c_void_p, c_char_p, c_char, c_int, c_uint, c_long)
46

57
try:
68
WINFUNCTYPE = ctypes.WINFUNCTYPE
79
except AttributeError:
810
# fake to enable this test on Linux
911
WINFUNCTYPE = CFUNCTYPE
1012

11-
import _ctypes_test
1213
lib = CDLL(_ctypes_test.__file__)
1314

15+
1416
class CFuncPtrTestCase(unittest.TestCase):
1517
def test_basic(self):
1618
X = WINFUNCTYPE(c_int, c_int, c_int)
@@ -21,8 +23,8 @@ def func(*args):
2123
x = X(func)
2224
self.assertEqual(x.restype, c_int)
2325
self.assertEqual(x.argtypes, (c_int, c_int))
24-
self.assertEqual(sizeof(x), sizeof(c_voidp))
25-
self.assertEqual(sizeof(X), sizeof(c_voidp))
26+
self.assertEqual(sizeof(x), sizeof(c_void_p))
27+
self.assertEqual(sizeof(X), sizeof(c_void_p))
2628

2729
def test_first(self):
2830
StdCallback = WINFUNCTYPE(c_int, c_int, c_int)
@@ -129,5 +131,6 @@ def test_abstract(self):
129131

130132
self.assertRaises(TypeError, _CFuncPtr, 13, "name", 42, "iid")
131133

134+
132135
if __name__ == '__main__':
133136
unittest.main()

Lib/test/test_ctypes/test_functions.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
"""
77

88
import ctypes
9-
from ctypes import *
9+
from ctypes import (CDLL, Structure, Array, CFUNCTYPE,
10+
byref, POINTER, pointer, ArgumentError,
11+
c_char, c_wchar, c_byte, c_char_p,
12+
c_short, c_int, c_long, c_longlong,
13+
c_float, c_double, c_longdouble)
1014
from test.test_ctypes import need_symbol
1115
import sys, unittest
1216

Lib/test/test_ctypes/test_incomplete.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import ctypes
22
import unittest
33
import warnings
4-
from ctypes import *
4+
from ctypes import Structure, POINTER, pointer, c_char_p
55

66
################################################################
77
#

Lib/test/test_ctypes/test_init.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from ctypes import *
21
import unittest
2+
from ctypes import Structure, c_int
3+
34

45
class X(Structure):
56
_fields_ = [("a", c_int),
@@ -15,6 +16,7 @@ def __init__(self):
1516
self.a = 9
1617
self.b = 12
1718

19+
1820
class Y(Structure):
1921
_fields_ = [("x", X)]
2022

@@ -36,5 +38,6 @@ def test_get(self):
3638
self.assertEqual((y.x.a, y.x.b), (9, 12))
3739
self.assertEqual(y.x.new_was_called, False)
3840

41+
3942
if __name__ == "__main__":
4043
unittest.main()

Lib/test/test_ctypes/test_internals.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This tests the internal _objects attribute
22
import unittest
3-
from ctypes import *
3+
from ctypes import Structure, POINTER, c_char_p, c_int
44
from sys import getrefcount as grc
55

66
# XXX This test must be reviewed for correctness!!!

Lib/test/test_ctypes/test_keeprefs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from ctypes import *
1+
from ctypes import Structure, POINTER, pointer, c_char_p, c_int
22
import unittest
33

44
class SimpleTestCase(unittest.TestCase):

Lib/test/test_ctypes/test_libc.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import unittest
22

3-
from ctypes import *
4-
import _ctypes_test
3+
from ctypes import (CDLL, CFUNCTYPE, POINTER, create_string_buffer, sizeof,
4+
c_void_p, c_char, c_int, c_double, c_size_t)
5+
56

7+
import _ctypes_test
68
lib = CDLL(_ctypes_test.__file__)
79

10+
811
def three_way_cmp(x, y):
912
"""Return -1 if x < y, 0 if x == y and 1 if x > y"""
1013
return (x > y) - (x < y)

0 commit comments

Comments
 (0)