Skip to content

Commit

Permalink
pythongh-105751: test_ctypes.test_numbers uses top level imports
Browse files Browse the repository at this point in the history
c_ulonglong and c_bool are now always available.
  • Loading branch information
vstinner committed Jun 14, 2023
1 parent f3266c0 commit bcaed4f
Showing 1 changed file with 8 additions and 32 deletions.
40 changes: 8 additions & 32 deletions Lib/test/test_ctypes/test_numbers.py
Original file line number Diff line number Diff line change
@@ -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_
Expand All @@ -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]]
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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')
Expand All @@ -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])
Expand All @@ -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])
Expand All @@ -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])
Expand All @@ -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)
Expand All @@ -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__ = []
Expand Down

0 comments on commit bcaed4f

Please sign in to comment.