[Python-checkins] gh-105751: test_ctypes.test_numbers uses top level imports (#105762)

vstinner webhook-mailer at python.org
Tue Jun 13 22:47:08 EDT 2023


https://github.com/python/cpython/commit/381a1dc26162e9fcbec48ac2eee5ac8adb806b07
commit: 381a1dc26162e9fcbec48ac2eee5ac8adb806b07
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2023-06-14T04:47:01+02:00
summary:

gh-105751: test_ctypes.test_numbers uses top level imports (#105762)

Moroever, c_ulonglong and c_bool are always available.

files:
M Lib/test/test_ctypes/test_numbers.py

diff --git a/Lib/test/test_ctypes/test_numbers.py b/Lib/test/test_ctypes/test_numbers.py
index db500e812beb..aad6af4b8671 100644
--- a/Lib/test/test_ctypes/test_numbers.py
+++ b/Lib/test/test_ctypes/test_numbers.py
@@ -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 @@ def test_signed_values(self):
             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 @@ def test_alignments(self):
                                  (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 @@ def test_int_from_address(self):
 
 
     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 @@ def test_float_from_address(self):
             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 @@ def test_char_from_address(self):
     # 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 @@ def test_init(self):
         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 @@ def test_float_overflow(self):
     def test_perf(self):
         check_perf()
 
-from ctypes import _SimpleCData
 class c_int_S(_SimpleCData):
     _type_ = "i"
     __slots__ = []



More information about the Python-checkins mailing list