diff options
Diffstat (limited to 'Lib/ctypes')
-rw-r--r-- | Lib/ctypes/__init__.py | 14 | ||||
-rw-r--r-- | Lib/ctypes/_endian.py | 2 | ||||
-rw-r--r-- | Lib/ctypes/test/test_byteswap.py | 20 | ||||
-rw-r--r-- | Lib/ctypes/test/test_loading.py | 4 | ||||
-rw-r--r-- | Lib/ctypes/test/test_pointers.py | 5 | ||||
-rw-r--r-- | Lib/ctypes/test/test_prototypes.py | 5 | ||||
-rw-r--r-- | Lib/ctypes/util.py | 8 |
7 files changed, 45 insertions, 13 deletions
diff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py index 5c803ffcb3..4cb6d0de27 100644 --- a/Lib/ctypes/__init__.py +++ b/Lib/ctypes/__init__.py @@ -237,14 +237,8 @@ _check_size(c_char) class c_char_p(_SimpleCData): _type_ = "z" - if _os.name == "nt": - def __repr__(self): - if not windll.kernel32.IsBadStringPtrA(self, -1): - return "%s(%r)" % (self.__class__.__name__, self.value) - return "%s(%s)" % (self.__class__.__name__, cast(self, c_void_p).value) - else: - def __repr__(self): - return "%s(%s)" % (self.__class__.__name__, cast(self, c_void_p).value) + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, c_void_p.from_buffer(self).value) _check_size(c_char_p, "P") class c_void_p(_SimpleCData): @@ -259,6 +253,8 @@ from _ctypes import POINTER, pointer, _pointer_type_cache class c_wchar_p(_SimpleCData): _type_ = "Z" + def __repr__(self): + return "%s(%s)" % (self.__class__.__name__, c_void_p.from_buffer(self).value) class c_wchar(_SimpleCData): _type_ = "u" @@ -353,7 +349,7 @@ class CDLL(object): self._handle = handle def __repr__(self): - return "<%s '%s', handle %x at %x>" % \ + return "<%s '%s', handle %x at %#x>" % \ (self.__class__.__name__, self._name, (self._handle & (_sys.maxsize*2 + 1)), id(self) & (_sys.maxsize*2 + 1)) diff --git a/Lib/ctypes/_endian.py b/Lib/ctypes/_endian.py index dae65fc215..37444bd6a7 100644 --- a/Lib/ctypes/_endian.py +++ b/Lib/ctypes/_endian.py @@ -45,6 +45,7 @@ if sys.byteorder == "little": class BigEndianStructure(Structure, metaclass=_swapped_meta): """Structure with big endian byte order""" + __slots__ = () _swappedbytes_ = None elif sys.byteorder == "big": @@ -53,6 +54,7 @@ elif sys.byteorder == "big": BigEndianStructure = Structure class LittleEndianStructure(Structure, metaclass=_swapped_meta): """Structure with little endian byte order""" + __slots__ = () _swappedbytes_ = None else: diff --git a/Lib/ctypes/test/test_byteswap.py b/Lib/ctypes/test/test_byteswap.py index 427bb8bcc1..01c97e83ca 100644 --- a/Lib/ctypes/test/test_byteswap.py +++ b/Lib/ctypes/test/test_byteswap.py @@ -22,6 +22,26 @@ class Test(unittest.TestCase): setattr(bits, "i%s" % i, 1) dump(bits) + def test_slots(self): + class BigPoint(BigEndianStructure): + __slots__ = () + _fields_ = [("x", c_int), ("y", c_int)] + + class LowPoint(LittleEndianStructure): + __slots__ = () + _fields_ = [("x", c_int), ("y", c_int)] + + big = BigPoint() + little = LowPoint() + big.x = 4 + big.y = 2 + little.x = 2 + little.y = 4 + with self.assertRaises(AttributeError): + big.z = 42 + with self.assertRaises(AttributeError): + little.z = 24 + def test_endian_short(self): if sys.byteorder == "little": self.assertIs(c_short.__ctype_le__, c_short) diff --git a/Lib/ctypes/test/test_loading.py b/Lib/ctypes/test/test_loading.py index 4fb89642e1..28468c1cd3 100644 --- a/Lib/ctypes/test/test_loading.py +++ b/Lib/ctypes/test/test_loading.py @@ -52,7 +52,9 @@ class LoaderTest(unittest.TestCase): @unittest.skipUnless(os.name in ("nt", "ce"), 'test specific to Windows (NT/CE)') def test_load_library(self): - self.assertIsNotNone(libc_name) + # CRT is no longer directly loadable. See issue23606 for the + # discussion about alternative approaches. + #self.assertIsNotNone(libc_name) if test.support.verbose: print(find_library("kernel32")) print(find_library("user32")) diff --git a/Lib/ctypes/test/test_pointers.py b/Lib/ctypes/test/test_pointers.py index e24a520dbe..40738f78d9 100644 --- a/Lib/ctypes/test/test_pointers.py +++ b/Lib/ctypes/test/test_pointers.py @@ -22,7 +22,10 @@ class PointersTestCase(unittest.TestCase): def test_pass_pointers(self): dll = CDLL(_ctypes_test.__file__) func = dll._testfunc_p_p - func.restype = c_long + if sizeof(c_longlong) == sizeof(c_void_p): + func.restype = c_longlong + else: + func.restype = c_long i = c_int(12345678) ## func.argtypes = (POINTER(c_int),) diff --git a/Lib/ctypes/test/test_prototypes.py b/Lib/ctypes/test/test_prototypes.py index 818c1110e3..cd0c649de3 100644 --- a/Lib/ctypes/test/test_prototypes.py +++ b/Lib/ctypes/test/test_prototypes.py @@ -69,7 +69,10 @@ class CharPointersTestCase(unittest.TestCase): def test_int_pointer_arg(self): func = testdll._testfunc_p_p - func.restype = c_long + if sizeof(c_longlong) == sizeof(c_void_p): + func.restype = c_longlong + else: + func.restype = c_long self.assertEqual(0, func(0)) ci = c_int(0) diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py index 595113bffd..9e74ccdbcf 100644 --- a/Lib/ctypes/util.py +++ b/Lib/ctypes/util.py @@ -19,6 +19,8 @@ if os.name == "nt": i = i + len(prefix) s, rest = sys.version[i:].split(" ", 1) majorVersion = int(s[:-2]) - 6 + if majorVersion >= 13: + majorVersion += 1 minorVersion = int(s[2:3]) / 10.0 # I don't think paths are affected by minor version in version 6 if majorVersion == 6: @@ -36,8 +38,12 @@ if os.name == "nt": return None if version <= 6: clibname = 'msvcrt' - else: + elif version <= 13: clibname = 'msvcr%d' % (version * 10) + else: + # CRT is no longer directly loadable. See issue23606 for the + # discussion about alternative approaches. + return None # If python was built with in debug mode import importlib.machinery |