Skip to content

gh-105751: test_ctypes avoids "from ctypes import *" #105768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/ctypes/_endian.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from ctypes import *
from ctypes import Array, Structure, Union

_array_type = type(Array)

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_ctypes/test_anon.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest
import test.support
from ctypes import *
from ctypes import c_int, Union, Structure, sizeof

class AnonTest(unittest.TestCase):

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_ctypes/test_array_in_pointer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
from ctypes import *
from ctypes import c_byte, Structure, POINTER, cast
from binascii import hexlify
import re

Expand Down
5 changes: 4 additions & 1 deletion Lib/test/test_ctypes/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
import sys
import unittest
import warnings
from ctypes import *
from ctypes import (Structure, Array, sizeof, addressof,
create_string_buffer, create_unicode_buffer,
c_char, c_wchar, c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint,
c_long, c_ulonglong, c_float, c_double, c_longdouble)
from test.support import bigmemtest, _2G

from test.test_ctypes import need_symbol
Expand Down
10 changes: 7 additions & 3 deletions Lib/test/test_ctypes/test_as_parameter.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import unittest
import _ctypes_test
import ctypes
from ctypes import *
import unittest
from ctypes import (Structure, CDLL, CFUNCTYPE,
POINTER, pointer, byref,
c_short, c_int, c_long, c_longlong,
c_byte, c_wchar, c_float, c_double,
ArgumentError)
from test.test_ctypes import need_symbol
import _ctypes_test

dll = CDLL(_ctypes_test.__file__)

Expand Down
6 changes: 5 additions & 1 deletion Lib/test/test_ctypes/test_bitfields.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from ctypes import *
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.test_ctypes import need_symbol
from test import support
import unittest
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_ctypes/test_buffers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from ctypes import *
from ctypes import (create_string_buffer, create_unicode_buffer, sizeof,
c_char, c_wchar)
from test.test_ctypes import need_symbol
import unittest

Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_ctypes/test_bytes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Test where byte objects are accepted"""
import unittest
import sys
from ctypes import *
import unittest
from ctypes import Structure, c_char, c_char_p, c_wchar, c_wchar_p

class BytesTest(unittest.TestCase):
def test_c_char(self):
Expand Down
8 changes: 7 additions & 1 deletion Lib/test/test_ctypes/test_byteswap.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import sys, unittest, struct, math, ctypes
from binascii import hexlify

from ctypes import *
from ctypes import (Structure, Union, LittleEndianUnion, BigEndianUnion,
BigEndianStructure, LittleEndianStructure,
POINTER, sizeof, cast,
c_byte, c_ubyte, c_char, c_wchar, c_void_p,
c_short, c_ushort, c_int, c_uint,
c_long, c_ulong, c_longlong, c_ulonglong,
c_uint32, c_float, c_double)

def bin(s):
return hexlify(memoryview(s)).decode().upper()
Expand Down
7 changes: 6 additions & 1 deletion Lib/test/test_ctypes/test_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
from test import support

import ctypes
from ctypes import *
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 test.test_ctypes import need_symbol
from _ctypes import CTYPES_MAX_ARGCOUNT
import _ctypes_test
Expand Down
10 changes: 6 additions & 4 deletions Lib/test/test_ctypes/test_cast.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from ctypes import *
from test.test_ctypes import need_symbol
import unittest
import sys
import unittest
from ctypes import (Structure, Union, POINTER, cast, sizeof, addressof,
c_void_p, c_char_p, c_wchar_p,
c_byte, c_short, c_int)
from test.test_ctypes import need_symbol

class Test(unittest.TestCase):

Expand All @@ -12,7 +14,7 @@ def test_array2pointer(self):
ptr = cast(array, POINTER(c_int))
self.assertEqual([ptr[i] for i in range(3)], [42, 17, 2])

if 2*sizeof(c_short) == sizeof(c_int):
if 2 * sizeof(c_short) == sizeof(c_int):
ptr = cast(array, POINTER(c_short))
if sys.byteorder == "little":
self.assertEqual([ptr[i] for i in range(6)],
Expand Down
12 changes: 7 additions & 5 deletions Lib/test/test_ctypes/test_cfuncs.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# A lot of failures in these tests on Mac OS X.
# Byte order related?

import unittest
import ctypes
from ctypes import *
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)
from test.test_ctypes import need_symbol

import _ctypes_test


class CFunctions(unittest.TestCase):
_dll = CDLL(_ctypes_test.__file__)

Expand Down Expand Up @@ -210,5 +211,6 @@ def __getattr__(self, name):
class stdcallCFunctions(CFunctions):
_dll = stdcall_dll(_ctypes_test.__file__)


if __name__ == '__main__':
unittest.main()
9 changes: 6 additions & 3 deletions Lib/test/test_ctypes/test_checkretval.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import unittest

import ctypes
from ctypes import *
import unittest
from ctypes import CDLL, c_int
from test.test_ctypes import need_symbol


class CHECKED(c_int):
def _check_retval_(value):
# Receives a CHECKED instance.
return str(value.value)
_check_retval_ = staticmethod(_check_retval_)


class Test(unittest.TestCase):

def test_checkretval(self):
Expand All @@ -32,5 +33,7 @@ def test_oledll(self):
oleaut32 = ctypes.oledll.oleaut32
self.assertRaises(OSError, oleaut32.CreateTypeLib2, 0, None, None)



if __name__ == "__main__":
unittest.main()
5 changes: 4 additions & 1 deletion Lib/test/test_ctypes/test_delattr.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import unittest
from ctypes import *
from ctypes import Structure, c_char, c_int


class X(Structure):
_fields_ = [("foo", c_int)]


class TestCase(unittest.TestCase):
def test_simple(self):
self.assertRaises(TypeError,
Expand All @@ -17,5 +19,6 @@ def test_struct(self):
self.assertRaises(TypeError,
delattr, X(), "foo")


if __name__ == "__main__":
unittest.main()
6 changes: 4 additions & 2 deletions Lib/test/test_ctypes/test_errno.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
import threading

import ctypes
from ctypes import *
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")
if libc_name is None:
raise unittest.SkipTest("Unable to find C library")
self.skipTest("Unable to find C library")

libc = CDLL(libc_name, use_errno=True)
if os.name == "nt":
libc_open = libc._open
Expand Down Expand Up @@ -73,5 +74,6 @@ def _worker():

ctypes.set_last_error(0)


if __name__ == "__main__":
unittest.main()
11 changes: 7 additions & 4 deletions Lib/test/test_ctypes/test_find.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import unittest
import unittest.mock
import os.path
import sys
import test.support
from test.support import os_helper
from ctypes import *
import unittest
import unittest.mock
from ctypes import CDLL, RTLD_GLOBAL
from ctypes.util import find_library
from test.support import os_helper


# On some systems, loading the OpenGL libraries needs the RTLD_GLOBAL mode.
class Test_OpenGL_libs(unittest.TestCase):
Expand Down Expand Up @@ -36,11 +37,13 @@ def setUpClass(cls):
cls.gl = CDLL(lib_gl, mode=RTLD_GLOBAL)
except OSError:
pass

if lib_glu:
try:
cls.glu = CDLL(lib_glu, RTLD_GLOBAL)
except OSError:
pass

if lib_gle:
try:
cls.gle = CDLL(lib_gle)
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_ctypes/test_frombuffer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from ctypes import *
import array
import gc
import unittest
from ctypes import Structure, Union, Array, sizeof, c_char, c_int

class X(Structure):
_fields_ = [("c_int", c_int)]
Expand Down
13 changes: 8 additions & 5 deletions Lib/test/test_ctypes/test_funcptr.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import unittest
import _ctypes_test
import ctypes
from ctypes import *
import unittest
from ctypes import (CDLL, Structure, CFUNCTYPE, sizeof,
c_void_p, c_char_p, c_char, c_int, c_uint, c_long)

try:
WINFUNCTYPE = ctypes.WINFUNCTYPE
except AttributeError:
# fake to enable this test on Linux
WINFUNCTYPE = CFUNCTYPE

import _ctypes_test
lib = CDLL(_ctypes_test.__file__)


class CFuncPtrTestCase(unittest.TestCase):
def test_basic(self):
X = WINFUNCTYPE(c_int, c_int, c_int)
Expand All @@ -21,8 +23,8 @@ def func(*args):
x = X(func)
self.assertEqual(x.restype, c_int)
self.assertEqual(x.argtypes, (c_int, c_int))
self.assertEqual(sizeof(x), sizeof(c_voidp))
self.assertEqual(sizeof(X), sizeof(c_voidp))
self.assertEqual(sizeof(x), sizeof(c_void_p))
self.assertEqual(sizeof(X), sizeof(c_void_p))

def test_first(self):
StdCallback = WINFUNCTYPE(c_int, c_int, c_int)
Expand Down Expand Up @@ -129,5 +131,6 @@ def test_abstract(self):

self.assertRaises(TypeError, _CFuncPtr, 13, "name", 42, "iid")


if __name__ == '__main__':
unittest.main()
6 changes: 5 additions & 1 deletion Lib/test/test_ctypes/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
"""

import ctypes
from ctypes import *
from ctypes import (CDLL, Structure, Array, CFUNCTYPE,
byref, POINTER, pointer, ArgumentError,
c_char, c_wchar, c_byte, c_char_p,
c_short, c_int, c_long, c_longlong,
c_float, c_double, c_longdouble)
from test.test_ctypes import need_symbol
import sys, unittest

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_ctypes/test_incomplete.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ctypes
import unittest
import warnings
from ctypes import *
from ctypes import Structure, POINTER, pointer, c_char_p

################################################################
#
Expand Down
5 changes: 4 additions & 1 deletion Lib/test/test_ctypes/test_init.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from ctypes import *
import unittest
from ctypes import Structure, c_int


class X(Structure):
_fields_ = [("a", c_int),
Expand All @@ -15,6 +16,7 @@ def __init__(self):
self.a = 9
self.b = 12


class Y(Structure):
_fields_ = [("x", X)]

Expand All @@ -36,5 +38,6 @@ def test_get(self):
self.assertEqual((y.x.a, y.x.b), (9, 12))
self.assertEqual(y.x.new_was_called, False)


if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion Lib/test/test_ctypes/test_internals.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This tests the internal _objects attribute
import unittest
from ctypes import *
from ctypes import Structure, POINTER, c_char_p, c_int
from sys import getrefcount as grc

# XXX This test must be reviewed for correctness!!!
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_ctypes/test_keeprefs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ctypes import *
from ctypes import Structure, POINTER, pointer, c_char_p, c_int
import unittest

class SimpleTestCase(unittest.TestCase):
Expand Down
7 changes: 5 additions & 2 deletions Lib/test/test_ctypes/test_libc.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import unittest

from ctypes import *
import _ctypes_test
from ctypes import (CDLL, CFUNCTYPE, POINTER, create_string_buffer, sizeof,
c_void_p, c_char, c_int, c_double, c_size_t)


import _ctypes_test
lib = CDLL(_ctypes_test.__file__)


def three_way_cmp(x, y):
"""Return -1 if x < y, 0 if x == y and 1 if x > y"""
return (x > y) - (x < y)
Expand Down
Loading