]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-105751: test_ctypes.test_numbers uses top level imports (#105762)
authorVictor Stinner <vstinner@python.org>
Wed, 14 Jun 2023 02:47:01 +0000 (04:47 +0200)
committerGitHub <noreply@github.com>
Wed, 14 Jun 2023 02:47:01 +0000 (04:47 +0200)
Moroever, c_ulonglong and c_bool are always available.

Lib/test/test_ctypes/test_numbers.py

index db500e812beb15e927074b7925408c433bcf8a8f..aad6af4b8671ce1e4bae96273d240132b42384d8 100644 (file)
@@ -1,6 +1,10 @@
-from ctypes import *
-import unittest
 import struct
+import sys
+import unittest
+from array import array
+from operator import truth
+from ctypes import *
+from ctypes import _SimpleCData
 
 def valid_ranges(*types):
     # given a sequence of numeric types, collect their _type_
@@ -21,29 +25,11 @@ def valid_ranges(*types):
 
 ArgType = type(byref(c_int(0)))
 
-unsigned_types = [c_ubyte, c_ushort, c_uint, c_ulong]
+unsigned_types = [c_ubyte, c_ushort, c_uint, c_ulong, c_ulonglong]
 signed_types = [c_byte, c_short, c_int, c_long, c_longlong]
-
-bool_types = []
-
+bool_types = [c_bool]
 float_types = [c_double, c_float]
 
-try:
-    c_ulonglong
-    c_longlong
-except NameError:
-    pass
-else:
-    unsigned_types.append(c_ulonglong)
-    signed_types.append(c_longlong)
-
-try:
-    c_bool
-except NameError:
-    pass
-else:
-    bool_types.append(c_bool)
-
 unsigned_ranges = valid_ranges(*unsigned_types)
 signed_ranges = valid_ranges(*signed_types)
 bool_values = [True, False, 0, 1, -1, 5000, 'test', [], [1]]
@@ -71,7 +57,6 @@ class NumberTestCase(unittest.TestCase):
             self.assertEqual(t(h).value, h)
 
     def test_bool_values(self):
-        from operator import truth
         for t, v in zip(bool_types, bool_values):
             self.assertEqual(t(v).value, truth(v))
 
@@ -161,7 +146,6 @@ class NumberTestCase(unittest.TestCase):
                                  (code, align))
 
     def test_int_from_address(self):
-        from array import array
         for t in signed_types + unsigned_types:
             # the array module doesn't support all format codes
             # (no 'q' or 'Q')
@@ -182,7 +166,6 @@ class NumberTestCase(unittest.TestCase):
 
 
     def test_float_from_address(self):
-        from array import array
         for t in float_types:
             a = array(t._type_, [3.14])
             v = t.from_address(a.buffer_info()[0])
@@ -193,9 +176,6 @@ class NumberTestCase(unittest.TestCase):
             self.assertIs(type(v), t)
 
     def test_char_from_address(self):
-        from ctypes import c_char
-        from array import array
-
         a = array('b', [0])
         a[0] = ord('x')
         v = c_char.from_address(a.buffer_info()[0])
@@ -208,8 +188,6 @@ class NumberTestCase(unittest.TestCase):
     # array does not support c_bool / 't'
     @unittest.skip('test disabled')
     def test_bool_from_address(self):
-        from ctypes import c_bool
-        from array import array
         a = array(c_bool._type_, [True])
         v = t.from_address(a.buffer_info()[0])
         self.assertEqual(v.value, a[0])
@@ -225,7 +203,6 @@ class NumberTestCase(unittest.TestCase):
         self.assertRaises(TypeError, c_int, c_long(42))
 
     def test_float_overflow(self):
-        import sys
         big_int = int(sys.float_info.max) * 2
         for t in float_types + [c_longdouble]:
             self.assertRaises(OverflowError, t, big_int)
@@ -238,7 +215,6 @@ class NumberTestCase(unittest.TestCase):
     def test_perf(self):
         check_perf()
 
-from ctypes import _SimpleCData
 class c_int_S(_SimpleCData):
     _type_ = "i"
     __slots__ = []