mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
gh-105751: test_ctypes.test_numbers uses top level imports (#105762)
Moroever, c_ulonglong and c_bool are always available.
This commit is contained in:
parent
ac7b551bde
commit
381a1dc261
1 changed files with 8 additions and 32 deletions
|
@ -1,6 +1,10 @@
|
||||||
from ctypes import *
|
|
||||||
import unittest
|
|
||||||
import struct
|
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):
|
def valid_ranges(*types):
|
||||||
# given a sequence of numeric types, collect their _type_
|
# given a sequence of numeric types, collect their _type_
|
||||||
|
@ -21,29 +25,11 @@ def valid_ranges(*types):
|
||||||
|
|
||||||
ArgType = type(byref(c_int(0)))
|
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]
|
signed_types = [c_byte, c_short, c_int, c_long, c_longlong]
|
||||||
|
bool_types = [c_bool]
|
||||||
bool_types = []
|
|
||||||
|
|
||||||
float_types = [c_double, c_float]
|
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)
|
unsigned_ranges = valid_ranges(*unsigned_types)
|
||||||
signed_ranges = valid_ranges(*signed_types)
|
signed_ranges = valid_ranges(*signed_types)
|
||||||
bool_values = [True, False, 0, 1, -1, 5000, 'test', [], [1]]
|
bool_values = [True, False, 0, 1, -1, 5000, 'test', [], [1]]
|
||||||
|
@ -71,7 +57,6 @@ class NumberTestCase(unittest.TestCase):
|
||||||
self.assertEqual(t(h).value, h)
|
self.assertEqual(t(h).value, h)
|
||||||
|
|
||||||
def test_bool_values(self):
|
def test_bool_values(self):
|
||||||
from operator import truth
|
|
||||||
for t, v in zip(bool_types, bool_values):
|
for t, v in zip(bool_types, bool_values):
|
||||||
self.assertEqual(t(v).value, truth(v))
|
self.assertEqual(t(v).value, truth(v))
|
||||||
|
|
||||||
|
@ -161,7 +146,6 @@ class NumberTestCase(unittest.TestCase):
|
||||||
(code, align))
|
(code, align))
|
||||||
|
|
||||||
def test_int_from_address(self):
|
def test_int_from_address(self):
|
||||||
from array import array
|
|
||||||
for t in signed_types + unsigned_types:
|
for t in signed_types + unsigned_types:
|
||||||
# the array module doesn't support all format codes
|
# the array module doesn't support all format codes
|
||||||
# (no 'q' or 'Q')
|
# (no 'q' or 'Q')
|
||||||
|
@ -182,7 +166,6 @@ class NumberTestCase(unittest.TestCase):
|
||||||
|
|
||||||
|
|
||||||
def test_float_from_address(self):
|
def test_float_from_address(self):
|
||||||
from array import array
|
|
||||||
for t in float_types:
|
for t in float_types:
|
||||||
a = array(t._type_, [3.14])
|
a = array(t._type_, [3.14])
|
||||||
v = t.from_address(a.buffer_info()[0])
|
v = t.from_address(a.buffer_info()[0])
|
||||||
|
@ -193,9 +176,6 @@ class NumberTestCase(unittest.TestCase):
|
||||||
self.assertIs(type(v), t)
|
self.assertIs(type(v), t)
|
||||||
|
|
||||||
def test_char_from_address(self):
|
def test_char_from_address(self):
|
||||||
from ctypes import c_char
|
|
||||||
from array import array
|
|
||||||
|
|
||||||
a = array('b', [0])
|
a = array('b', [0])
|
||||||
a[0] = ord('x')
|
a[0] = ord('x')
|
||||||
v = c_char.from_address(a.buffer_info()[0])
|
v = c_char.from_address(a.buffer_info()[0])
|
||||||
|
@ -208,8 +188,6 @@ class NumberTestCase(unittest.TestCase):
|
||||||
# array does not support c_bool / 't'
|
# array does not support c_bool / 't'
|
||||||
@unittest.skip('test disabled')
|
@unittest.skip('test disabled')
|
||||||
def test_bool_from_address(self):
|
def test_bool_from_address(self):
|
||||||
from ctypes import c_bool
|
|
||||||
from array import array
|
|
||||||
a = array(c_bool._type_, [True])
|
a = array(c_bool._type_, [True])
|
||||||
v = t.from_address(a.buffer_info()[0])
|
v = t.from_address(a.buffer_info()[0])
|
||||||
self.assertEqual(v.value, a[0])
|
self.assertEqual(v.value, a[0])
|
||||||
|
@ -225,7 +203,6 @@ class NumberTestCase(unittest.TestCase):
|
||||||
self.assertRaises(TypeError, c_int, c_long(42))
|
self.assertRaises(TypeError, c_int, c_long(42))
|
||||||
|
|
||||||
def test_float_overflow(self):
|
def test_float_overflow(self):
|
||||||
import sys
|
|
||||||
big_int = int(sys.float_info.max) * 2
|
big_int = int(sys.float_info.max) * 2
|
||||||
for t in float_types + [c_longdouble]:
|
for t in float_types + [c_longdouble]:
|
||||||
self.assertRaises(OverflowError, t, big_int)
|
self.assertRaises(OverflowError, t, big_int)
|
||||||
|
@ -238,7 +215,6 @@ class NumberTestCase(unittest.TestCase):
|
||||||
def test_perf(self):
|
def test_perf(self):
|
||||||
check_perf()
|
check_perf()
|
||||||
|
|
||||||
from ctypes import _SimpleCData
|
|
||||||
class c_int_S(_SimpleCData):
|
class c_int_S(_SimpleCData):
|
||||||
_type_ = "i"
|
_type_ = "i"
|
||||||
__slots__ = []
|
__slots__ = []
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue