Skip to content

Commit

Permalink
gh-105751: Cleanup test_ctypes imports (#105803)
Browse files Browse the repository at this point in the history
* Move imports at top level and sort imports.
* Replace c_buffer() with create_string_buffer(): c_buffer is a
  deprecated alias.
* PEP 8: Add empty lines for readability between imports and classes.
  • Loading branch information
vstinner authored Jun 14, 2023
1 parent d1b0297 commit 698a0da
Show file tree
Hide file tree
Showing 45 changed files with 250 additions and 240 deletions.
2 changes: 2 additions & 0 deletions Lib/test/test_ctypes/test_anon.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import test.support
from ctypes import c_int, Union, Structure, sizeof


class AnonTest(unittest.TestCase):

def test_anon(self):
Expand Down Expand Up @@ -69,5 +70,6 @@ class Y(Structure):
self.assertEqual(Y._.offset, sizeof(c_int))
self.assertEqual(Y.y.offset, sizeof(c_int) * 2)


if __name__ == "__main__":
unittest.main()
10 changes: 7 additions & 3 deletions Lib/test/test_ctypes/test_array_in_pointer.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import binascii
import re
import unittest
from ctypes import c_byte, Structure, POINTER, cast
from binascii import hexlify
import re


def dump(obj):
# helper function to dump memory contents in hex, with a hyphen
# between the bytes.
h = hexlify(memoryview(obj)).decode()
h = binascii.hexlify(memoryview(obj)).decode()
return re.sub(r"(..)", r"\1-", h)[:-1]


class Value(Structure):
_fields_ = [("val", c_byte)]


class Container(Structure):
_fields_ = [("pvalues", POINTER(Value))]


class Test(unittest.TestCase):
def test(self):
# create an array of 4 values
Expand Down Expand Up @@ -60,5 +63,6 @@ def test_2(self):
([1, 2, 3, 4], "01-02-03-04")
)


if __name__ == "__main__":
unittest.main()
10 changes: 2 additions & 8 deletions Lib/test/test_ctypes/test_as_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
# fake to enable this test on Linux
CALLBACK_FUNCTYPE = CFUNCTYPE


class POINT(Structure):
_fields_ = [("x", c_int), ("y", c_int)]


class BasicWrapTestCase(unittest.TestCase):
def wrap(self, param):
return param
Expand Down Expand Up @@ -71,8 +73,6 @@ def callback(v):
f(self.wrap(2**18), self.wrap(cb))
self.assertEqual(args, expected)

################################################################

def test_callbacks(self):
f = dll._testfunc_callback_i_if
f.restype = c_int
Expand Down Expand Up @@ -194,8 +194,6 @@ class S8I(Structure):
(9*2, 8*3, 7*4, 6*5, 5*6, 4*7, 3*8, 2*9))

def test_recursive_as_param(self):
from ctypes import c_int

class A(object):
pass

Expand All @@ -205,16 +203,13 @@ class A(object):
c_int.from_param(a)


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

class AsParamWrapper(object):
def __init__(self, param):
self._as_parameter_ = param

class AsParamWrapperTestCase(BasicWrapTestCase):
wrap = AsParamWrapper

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

class AsParamPropertyWrapper(object):
def __init__(self, param):
Expand All @@ -227,7 +222,6 @@ def getParameter(self):
class AsParamPropertyWrapperTestCase(BasicWrapTestCase):
wrap = AsParamPropertyWrapper

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

if __name__ == '__main__':
unittest.main()
10 changes: 5 additions & 5 deletions Lib/test/test_ctypes/test_bitfields.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import _ctypes_test
import os
import unittest
from ctypes import (CDLL, Structure, sizeof, POINTER, byref, alignment,
LittleEndianStructure, BigEndianStructure,
c_byte, c_ubyte, c_char, c_char_p, c_void_p, c_wchar,
c_uint32, c_uint64,
c_short, c_ushort, c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong)
from test import support
import unittest
import os

import _ctypes_test

class BITS(Structure):
_fields_ = [("A", c_int, 1),
Expand Down Expand Up @@ -56,6 +56,7 @@ def test_shorts(self):
setattr(b, name, i)
self.assertEqual(getattr(b, name), func(byref(b), name.encode('ascii')))


signed_int_types = (c_byte, c_short, c_int, c_long, c_longlong)
unsigned_int_types = (c_ubyte, c_ushort, c_uint, c_ulong, c_ulonglong)
int_types = unsigned_int_types + signed_int_types
Expand Down Expand Up @@ -117,7 +118,6 @@ class X(Structure):
x.a, x.b = 0, -1
self.assertEqual((c_typ, x.a, x.b, x.c), (c_typ, 0, 7, 0))


def fail_fields(self, *fields):
return self.get_except(type(Structure), "X", (),
{"_fields_": fields})
Expand Down Expand Up @@ -194,7 +194,6 @@ class X(Structure):
self.assertEqual(X.b.offset, sizeof(c_short)*1)
self.assertEqual(X.c.offset, sizeof(c_short)*2)


def get_except(self, func, *args, **kw):
try:
func(*args, **kw)
Expand Down Expand Up @@ -291,5 +290,6 @@ class Big(BigEndianStructure):
x.c = 2
self.assertEqual(b, b'\xab\xcd\xef\x12')


if __name__ == "__main__":
unittest.main()
4 changes: 2 additions & 2 deletions Lib/test/test_ctypes/test_buffers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import unittest
from ctypes import (create_string_buffer, create_unicode_buffer, sizeof,
c_char, c_wchar)
import unittest

class StringBufferTestCase(unittest.TestCase):

class StringBufferTestCase(unittest.TestCase):
def test_buffer(self):
b = create_string_buffer(32)
self.assertEqual(len(b), 32)
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_ctypes/test_bytes.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""Test where byte objects are accepted"""
import sys
import unittest
from _ctypes import _SimpleCData
from ctypes import Structure, c_char, c_char_p, c_wchar, c_wchar_p


class BytesTest(unittest.TestCase):
def test_c_char(self):
x = c_char(b"x")
Expand Down Expand Up @@ -55,7 +57,6 @@ class X(Structure):

@unittest.skipUnless(sys.platform == "win32", 'Windows-specific test')
def test_BSTR(self):
from _ctypes import _SimpleCData
class BSTR(_SimpleCData):
_type_ = "X"

Expand Down
14 changes: 10 additions & 4 deletions Lib/test/test_ctypes/test_byteswap.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import sys, unittest, struct, math, ctypes
from binascii import hexlify

import binascii
import ctypes
import math
import struct
import sys
import unittest
from ctypes import (Structure, Union, LittleEndianUnion, BigEndianUnion,
BigEndianStructure, LittleEndianStructure,
POINTER, sizeof, cast,
Expand All @@ -9,8 +12,10 @@
c_long, c_ulong, c_longlong, c_ulonglong,
c_uint32, c_float, c_double)


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


# Each *simple* type that supports different byte orders has an
# __ctype_be__ attribute that specifies the same type in BIG ENDIAN
Expand Down Expand Up @@ -366,5 +371,6 @@ class TestUnion(parent):
self.assertEqual(s.point.x, 1)
self.assertEqual(s.point.y, 2)


if __name__ == "__main__":
unittest.main()
19 changes: 7 additions & 12 deletions Lib/test/test_ctypes/test_callbacks.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import _ctypes_test
import ctypes
import functools
import gc
import math
import sys
import unittest
from test import support

import ctypes
from _ctypes import CTYPES_MAX_ARGCOUNT
from ctypes import (CDLL, cdll, Structure, CFUNCTYPE,
ArgumentError, POINTER, sizeof,
c_byte, c_ubyte, c_char, c_char_p,
c_short, c_ushort, c_int, c_uint,
c_long, c_longlong, c_ulonglong, c_ulong,
c_float, c_double, c_longdouble, py_object)
from _ctypes import CTYPES_MAX_ARGCOUNT
import _ctypes_test
from ctypes.util import find_library
from test import support


class Callbacks(unittest.TestCase):
functype = CFUNCTYPE

## def tearDown(self):
## import gc
## gc.collect()

def callback(self, *args):
Expand Down Expand Up @@ -81,7 +82,6 @@ def test_ulonglong(self):

def test_float(self):
# only almost equal: double -> float -> double
import math
self.check_type(c_float, math.e)
self.check_type(c_float, -math.e)

Expand Down Expand Up @@ -138,7 +138,6 @@ def func(self): pass
def __init__(self):
self.v = proto(self.func)

import gc
for i in range(32):
X()
gc.collect()
Expand All @@ -147,7 +146,6 @@ def __init__(self):
self.assertEqual(len(live), 0)

def test_issue12483(self):
import gc
class Nasty:
def __del__(self):
gc.collect()
Expand All @@ -172,8 +170,6 @@ class StdcallCallbacks(Callbacks):
functype = ctypes.WINFUNCTYPE


################################################################

class SampleCallbacksTestCase(unittest.TestCase):

def test_integrate(self):
Expand All @@ -197,7 +193,6 @@ def func(x):
self.assertLess(diff, 0.01, "%s not less than 0.01" % diff)

def test_issue_8959_a(self):
from ctypes.util import find_library
libc_path = find_library("c")
if not libc_path:
self.skipTest('could not find libc')
Expand Down
5 changes: 3 additions & 2 deletions Lib/test/test_ctypes/test_cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
c_void_p, c_char_p, c_wchar_p,
c_byte, c_short, c_int)

class Test(unittest.TestCase):

class Test(unittest.TestCase):
def test_array2pointer(self):
array = (c_int * 3)(42, 17, 2)

Expand Down Expand Up @@ -80,7 +80,7 @@ def test_char_p(self):
def test_wchar_p(self):
s = c_wchar_p("hiho")
self.assertEqual(cast(cast(s, c_void_p), c_wchar_p).value,
"hiho")
"hiho")

def test_bad_type_arg(self):
# The type argument must be a ctypes pointer type.
Expand All @@ -95,5 +95,6 @@ class MyUnion(Union):
_fields_ = [("a", c_int)]
self.assertRaises(TypeError, cast, array, MyUnion)


if __name__ == "__main__":
unittest.main()
5 changes: 3 additions & 2 deletions Lib/test/test_ctypes/test_cfuncs.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import unittest
import _ctypes_test
import ctypes
import unittest
from ctypes import (CDLL,
c_byte, c_ubyte, c_char,
c_short, c_ushort, c_int, c_uint,
c_long, c_ulong, c_longlong, c_ulonglong,
c_float, c_double, c_longdouble)
import _ctypes_test


class CFunctions(unittest.TestCase):
Expand Down Expand Up @@ -190,6 +190,7 @@ def test_void(self):
self.assertEqual(self._dll.tv_i(-42), None)
self.assertEqual(self.S(), -42)


# The following repeats the above tests with stdcall functions (where
# they are available)
if hasattr(ctypes, 'WinDLL'):
Expand Down
5 changes: 1 addition & 4 deletions Lib/test/test_ctypes/test_checkretval.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import _ctypes_test
import ctypes
import unittest
from ctypes import CDLL, c_int
Expand All @@ -11,10 +12,7 @@ def _check_retval_(value):


class Test(unittest.TestCase):

def test_checkretval(self):

import _ctypes_test
dll = CDLL(_ctypes_test.__file__)
self.assertEqual(42, dll._testfunc_p_p(42))

Expand All @@ -34,6 +32,5 @@ def test_oledll(self):
self.assertRaises(OSError, oleaut32.CreateTypeLib2, 0, None, None)



if __name__ == "__main__":
unittest.main()
8 changes: 5 additions & 3 deletions Lib/test/test_ctypes/test_errno.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import unittest, os, errno
import threading

import ctypes
import errno
import os
import threading
import unittest
from ctypes import CDLL, c_int, c_char_p, c_wchar_p, get_errno, set_errno
from ctypes.util import find_library


class Test(unittest.TestCase):
def test_open(self):
libc_name = find_library("c")
Expand Down
Loading

0 comments on commit 698a0da

Please sign in to comment.