summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorZachary Ware <zachary.ware@gmail.com>2014-06-13 19:40:16 (GMT)
committerZachary Ware <zachary.ware@gmail.com>2014-06-13 19:40:16 (GMT)
commit04349c602c9dfc70b2d451a8191b21d7f30db3e6 (patch)
tree2b0beb9493872be6eebad33d17d676c924029480 /Lib
parent925c6ad85be18eea450e05fa033b09d8c60e5543 (diff)
downloadcpython-04349c602c9dfc70b2d451a8191b21d7f30db3e6.zip
cpython-04349c602c9dfc70b2d451a8191b21d7f30db3e6.tar.gz
cpython-04349c602c9dfc70b2d451a8191b21d7f30db3e6.tar.bz2
Issue #19493: Backport 6f63fff5c120
Diffstat (limited to 'Lib')
-rw-r--r--Lib/ctypes/test/__init__.py10
-rw-r--r--Lib/ctypes/test/test_arrays.py26
-rw-r--r--Lib/ctypes/test/test_as_parameter.py6
-rw-r--r--Lib/ctypes/test/test_bitfields.py19
-rw-r--r--Lib/ctypes/test/test_buffers.py60
-rw-r--r--Lib/ctypes/test/test_byteswap.py3
-rw-r--r--Lib/ctypes/test/test_callbacks.py47
-rw-r--r--Lib/ctypes/test/test_cast.py15
-rw-r--r--Lib/ctypes/test/test_cfuncs.py9
-rw-r--r--Lib/ctypes/test/test_checkretval.py15
-rw-r--r--Lib/ctypes/test/test_errcheck.py19
-rw-r--r--Lib/ctypes/test/test_find.py69
-rw-r--r--Lib/ctypes/test/test_functions.py70
-rw-r--r--Lib/ctypes/test/test_integers.py5
-rw-r--r--Lib/ctypes/test/test_keeprefs.py3
-rw-r--r--Lib/ctypes/test/test_loading.py138
-rw-r--r--Lib/ctypes/test/test_macholib.py24
-rw-r--r--Lib/ctypes/test/test_memfunctions.py44
-rw-r--r--Lib/ctypes/test/test_numbers.py39
-rw-r--r--Lib/ctypes/test/test_objects.py11
-rw-r--r--Lib/ctypes/test/test_parameters.py19
-rw-r--r--Lib/ctypes/test/test_prototypes.py102
-rw-r--r--Lib/ctypes/test/test_python_api.py32
-rw-r--r--Lib/ctypes/test/test_random_things.py27
-rw-r--r--Lib/ctypes/test/test_slicing.py77
-rw-r--r--Lib/ctypes/test/test_strings.py114
-rw-r--r--Lib/ctypes/test/test_structures.py23
-rw-r--r--Lib/ctypes/test/test_unicode.py255
-rw-r--r--Lib/ctypes/test/test_values.py100
-rw-r--r--Lib/ctypes/test/test_win32.py130
-rw-r--r--Lib/ctypes/test/test_wintypes.py6
31 files changed, 742 insertions, 775 deletions
diff --git a/Lib/ctypes/test/__init__.py b/Lib/ctypes/test/__init__.py
index 808e418..f2cc143 100644
--- a/Lib/ctypes/test/__init__.py
+++ b/Lib/ctypes/test/__init__.py
@@ -2,7 +2,15 @@ import os, sys, unittest, getopt, time
use_resources = []
-class ResourceDenied(Exception):
+import ctypes
+ctypes_symbols = dir(ctypes)
+
+def need_symbol(name):
+ return unittest.skipUnless(name in ctypes_symbols,
+ '{!r} is required'.format(name))
+
+
+class ResourceDenied(unittest.SkipTest):
"""Test skipped because it requested a disallowed resource.
This is raised when a test calls requires() for a resource that
diff --git a/Lib/ctypes/test/test_arrays.py b/Lib/ctypes/test/test_arrays.py
index db49c0c..47e95ee 100644
--- a/Lib/ctypes/test/test_arrays.py
+++ b/Lib/ctypes/test/test_arrays.py
@@ -1,6 +1,8 @@
import unittest
from ctypes import *
+from ctypes.test import need_symbol
+
formats = "bBhHiIlLqQfd"
formats = c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, \
@@ -101,20 +103,16 @@ class ArrayTestCase(unittest.TestCase):
self.assertEqual(sz[1:4:2], "o")
self.assertEqual(sz.value, "foo")
- try:
- create_unicode_buffer
- except NameError:
- pass
- else:
- def test_from_addressW(self):
- p = create_unicode_buffer("foo")
- sz = (c_wchar * 3).from_address(addressof(p))
- self.assertEqual(sz[:], "foo")
- self.assertEqual(sz[::], "foo")
- self.assertEqual(sz[::-1], "oof")
- self.assertEqual(sz[::3], "f")
- self.assertEqual(sz[1:4:2], "o")
- self.assertEqual(sz.value, "foo")
+ @need_symbol('create_unicode_buffer')
+ def test_from_addressW(self):
+ p = create_unicode_buffer("foo")
+ sz = (c_wchar * 3).from_address(addressof(p))
+ self.assertEqual(sz[:], "foo")
+ self.assertEqual(sz[::], "foo")
+ self.assertEqual(sz[::-1], "oof")
+ self.assertEqual(sz[::3], "f")
+ self.assertEqual(sz[1:4:2], "o")
+ self.assertEqual(sz.value, "foo")
def test_cache(self):
# Array types are cached internally in the _ctypes extension,
diff --git a/Lib/ctypes/test/test_as_parameter.py b/Lib/ctypes/test/test_as_parameter.py
index 9388617..f2fe10a 100644
--- a/Lib/ctypes/test/test_as_parameter.py
+++ b/Lib/ctypes/test/test_as_parameter.py
@@ -1,5 +1,6 @@
import unittest
from ctypes import *
+from ctypes.test import need_symbol
import _ctypes_test
dll = CDLL(_ctypes_test.__file__)
@@ -17,11 +18,8 @@ class BasicWrapTestCase(unittest.TestCase):
def wrap(self, param):
return param
+ @need_symbol('c_wchar')
def test_wchar_parm(self):
- try:
- c_wchar
- except NameError:
- return
f = dll._testfunc_i_bhilfd
f.argtypes = [c_byte, c_wchar, c_int, c_long, c_float, c_double]
result = f(self.wrap(1), self.wrap(u"x"), self.wrap(3), self.wrap(4), self.wrap(5.0), self.wrap(6.0))
diff --git a/Lib/ctypes/test/test_bitfields.py b/Lib/ctypes/test/test_bitfields.py
index cbeb4c6..991dbe8 100644
--- a/Lib/ctypes/test/test_bitfields.py
+++ b/Lib/ctypes/test/test_bitfields.py
@@ -1,4 +1,5 @@
from ctypes import *
+from ctypes.test import need_symbol
import unittest
import os
@@ -127,20 +128,18 @@ class BitFieldTest(unittest.TestCase):
result = self.fail_fields(("a", c_char, 1))
self.assertEqual(result, (TypeError, 'bit fields not allowed for type c_char'))
- try:
- c_wchar
- except NameError:
- pass
- else:
- result = self.fail_fields(("a", c_wchar, 1))
- self.assertEqual(result, (TypeError, 'bit fields not allowed for type c_wchar'))
-
class Dummy(Structure):
_fields_ = []
result = self.fail_fields(("a", Dummy, 1))
self.assertEqual(result, (TypeError, 'bit fields not allowed for type Dummy'))
+ @need_symbol('c_wchar')
+ def test_c_wchar(self):
+ result = self.fail_fields(("a", c_wchar, 1))
+ self.assertEqual(result,
+ (TypeError, 'bit fields not allowed for type c_wchar'))
+
def test_single_bitfield_size(self):
for c_typ in int_types:
result = self.fail_fields(("a", c_typ, -1))
@@ -240,7 +239,7 @@ class BitFieldTest(unittest.TestCase):
_anonymous_ = ["_"]
_fields_ = [("_", X)]
- @unittest.skipUnless(hasattr(ctypes, "c_uint32"), "c_int32 is required")
+ @need_symbol('c_uint32')
def test_uint32(self):
class X(Structure):
_fields_ = [("a", c_uint32, 32)]
@@ -250,7 +249,7 @@ class BitFieldTest(unittest.TestCase):
x.a = 0xFDCBA987
self.assertEqual(x.a, 0xFDCBA987)
- @unittest.skipUnless(hasattr(ctypes, "c_uint64"), "c_int64 is required")
+ @need_symbol('c_uint64')
def test_uint64(self):
class X(Structure):
_fields_ = [("a", c_uint64, 64)]
diff --git a/Lib/ctypes/test/test_buffers.py b/Lib/ctypes/test/test_buffers.py
index 3df793d..88d87e9 100644
--- a/Lib/ctypes/test/test_buffers.py
+++ b/Lib/ctypes/test/test_buffers.py
@@ -1,4 +1,5 @@
from ctypes import *
+from ctypes.test import need_symbol
import unittest
class StringBufferTestCase(unittest.TestCase):
@@ -36,39 +37,36 @@ class StringBufferTestCase(unittest.TestCase):
self.assertEqual(b[::2], "ac")
self.assertEqual(b[::5], "a")
- try:
- c_wchar
- except NameError:
- pass
- else:
- def test_unicode_buffer(self):
- b = create_unicode_buffer(32)
- self.assertEqual(len(b), 32)
- self.assertEqual(sizeof(b), 32 * sizeof(c_wchar))
- self.assertIs(type(b[0]), unicode)
+ @need_symbol('c_wchar')
+ def test_unicode_buffer(self):
+ b = create_unicode_buffer(32)
+ self.assertEqual(len(b), 32)
+ self.assertEqual(sizeof(b), 32 * sizeof(c_wchar))
+ self.assertIs(type(b[0]), unicode)
- b = create_unicode_buffer(u"abc")
- self.assertEqual(len(b), 4) # trailing nul char
- self.assertEqual(sizeof(b), 4 * sizeof(c_wchar))
- self.assertIs(type(b[0]), unicode)
- self.assertEqual(b[0], u"a")
- self.assertEqual(b[:], "abc\0")
- self.assertEqual(b[::], "abc\0")
- self.assertEqual(b[::-1], "\0cba")
- self.assertEqual(b[::2], "ac")
- self.assertEqual(b[::5], "a")
+ b = create_unicode_buffer(u"abc")
+ self.assertEqual(len(b), 4) # trailing nul char
+ self.assertEqual(sizeof(b), 4 * sizeof(c_wchar))
+ self.assertIs(type(b[0]), unicode)
+ self.assertEqual(b[0], u"a")
+ self.assertEqual(b[:], "abc\0")
+ self.assertEqual(b[::], "abc\0")
+ self.assertEqual(b[::-1], "\0cba")
+ self.assertEqual(b[::2], "ac")
+ self.assertEqual(b[::5], "a")
- def test_unicode_conversion(self):
- b = create_unicode_buffer("abc")
- self.assertEqual(len(b), 4) # trailing nul char
- self.assertEqual(sizeof(b), 4 * sizeof(c_wchar))
- self.assertIs(type(b[0]), unicode)
- self.assertEqual(b[0], u"a")
- self.assertEqual(b[:], "abc\0")
- self.assertEqual(b[::], "abc\0")
- self.assertEqual(b[::-1], "\0cba")
- self.assertEqual(b[::2], "ac")
- self.assertEqual(b[::5], "a")
+ @need_symbol('c_wchar')
+ def test_unicode_conversion(self):
+ b = create_unicode_buffer("abc")
+ self.assertEqual(len(b), 4) # trailing nul char
+ self.assertEqual(sizeof(b), 4 * sizeof(c_wchar))
+ self.assertIs(type(b[0]), unicode)
+ self.assertEqual(b[0], u"a")
+ self.assertEqual(b[:], "abc\0")
+ self.assertEqual(b[::], "abc\0")
+ self.assertEqual(b[::-1], "\0cba")
+ self.assertEqual(b[::2], "ac")
+ self.assertEqual(b[::5], "a")
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/ctypes/test/test_byteswap.py b/Lib/ctypes/test/test_byteswap.py
index 9c4a697..d36402b 100644
--- a/Lib/ctypes/test/test_byteswap.py
+++ b/Lib/ctypes/test/test_byteswap.py
@@ -14,7 +14,8 @@ def bin(s):
# For Structures and Unions, these types are created on demand.
class Test(unittest.TestCase):
- def X_test(self):
+ @unittest.skip('test disabled')
+ def test_X(self):
print >> sys.stderr, sys.byteorder
for i in range(32):
bits = BITS()
diff --git a/Lib/ctypes/test/test_callbacks.py b/Lib/ctypes/test/test_callbacks.py
index be0c17e..c2e953b 100644
--- a/Lib/ctypes/test/test_callbacks.py
+++ b/Lib/ctypes/test/test_callbacks.py
@@ -1,5 +1,6 @@
import unittest
from ctypes import *
+from ctypes.test import need_symbol
import _ctypes_test
class Callbacks(unittest.TestCase):
@@ -94,9 +95,10 @@ class Callbacks(unittest.TestCase):
# disabled: would now (correctly) raise a RuntimeWarning about
# a memory leak. A callback function cannot return a non-integral
# C type without causing a memory leak.
-## def test_char_p(self):
-## self.check_type(c_char_p, "abc")
-## self.check_type(c_char_p, "def")
+ @unittest.skip('test disabled')
+ def test_char_p(self):
+ self.check_type(c_char_p, "abc")
+ self.check_type(c_char_p, "def")
def test_pyobject(self):
o = ()
@@ -148,13 +150,12 @@ class Callbacks(unittest.TestCase):
CFUNCTYPE(None)(lambda x=Nasty(): None)
-try:
- WINFUNCTYPE
-except NameError:
- pass
-else:
- class StdcallCallbacks(Callbacks):
+@need_symbol('WINFUNCTYPE')
+class StdcallCallbacks(Callbacks):
+ try:
functype = WINFUNCTYPE
+ except NameError:
+ pass
################################################################
@@ -184,7 +185,7 @@ class SampleCallbacksTestCase(unittest.TestCase):
from ctypes.util import find_library
libc_path = find_library("c")
if not libc_path:
- return # cannot test
+ self.skipTest('could not find libc')
libc = CDLL(libc_path)
@CFUNCTYPE(c_int, POINTER(c_int), POINTER(c_int))
@@ -196,23 +197,19 @@ class SampleCallbacksTestCase(unittest.TestCase):
libc.qsort(array, len(array), sizeof(c_int), cmp_func)
self.assertEqual(array[:], [1, 5, 7, 33, 99])
- try:
- WINFUNCTYPE
- except NameError:
- pass
- else:
- def test_issue_8959_b(self):
- from ctypes.wintypes import BOOL, HWND, LPARAM
- global windowCount
- windowCount = 0
+ @need_symbol('WINFUNCTYPE')
+ def test_issue_8959_b(self):
+ from ctypes.wintypes import BOOL, HWND, LPARAM
+ global windowCount
+ windowCount = 0
- @WINFUNCTYPE(BOOL, HWND, LPARAM)
- def EnumWindowsCallbackFunc(hwnd, lParam):
- global windowCount
- windowCount += 1
- return True #Allow windows to keep enumerating
+ @WINFUNCTYPE(BOOL, HWND, LPARAM)
+ def EnumWindowsCallbackFunc(hwnd, lParam):
+ global windowCount
+ windowCount += 1
+ return True #Allow windows to keep enumerating
- windll.user32.EnumWindows(EnumWindowsCallbackFunc, 0)
+ windll.user32.EnumWindows(EnumWindowsCallbackFunc, 0)
def test_callback_register_int(self):
# Issue #8275: buggy handling of callback args under Win64
diff --git a/Lib/ctypes/test/test_cast.py b/Lib/ctypes/test/test_cast.py
index f58d42c..d24e0b5 100644
--- a/Lib/ctypes/test/test_cast.py
+++ b/Lib/ctypes/test/test_cast.py
@@ -1,4 +1,5 @@
from ctypes import *
+from ctypes.test import need_symbol
import unittest
import sys
@@ -75,15 +76,11 @@ class Test(unittest.TestCase):
self.assertEqual(cast(cast(s, c_void_p), c_char_p).value,
"hiho")
- try:
- c_wchar_p
- except NameError:
- pass
- else:
- def test_wchar_p(self):
- s = c_wchar_p("hiho")
- self.assertEqual(cast(cast(s, c_void_p), c_wchar_p).value,
- "hiho")
+ @need_symbol('c_wchar_p')
+ def test_wchar_p(self):
+ s = c_wchar_p("hiho")
+ self.assertEqual(cast(cast(s, c_void_p), c_wchar_p).value,
+ "hiho")
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/ctypes/test/test_cfuncs.py b/Lib/ctypes/test/test_cfuncs.py
index 598776e..765408c 100644
--- a/Lib/ctypes/test/test_cfuncs.py
+++ b/Lib/ctypes/test/test_cfuncs.py
@@ -3,6 +3,7 @@
import unittest
from ctypes import *
+from ctypes.test import need_symbol
import _ctypes_test
@@ -193,7 +194,7 @@ class CFunctions(unittest.TestCase):
try:
WinDLL
except NameError:
- pass
+ def stdcall_dll(*_): pass
else:
class stdcall_dll(WinDLL):
def __getattr__(self, name):
@@ -203,9 +204,9 @@ else:
setattr(self, name, func)
return func
- class stdcallCFunctions(CFunctions):
- _dll = stdcall_dll(_ctypes_test.__file__)
- pass
+@need_symbol('WinDLL')
+class stdcallCFunctions(CFunctions):
+ _dll = stdcall_dll(_ctypes_test.__file__)
if __name__ == '__main__':
unittest.main()
diff --git a/Lib/ctypes/test/test_checkretval.py b/Lib/ctypes/test/test_checkretval.py
index 01ccc57..a0dc534 100644
--- a/Lib/ctypes/test/test_checkretval.py
+++ b/Lib/ctypes/test/test_checkretval.py
@@ -1,6 +1,7 @@
import unittest
from ctypes import *
+from ctypes.test import need_symbol
class CHECKED(c_int):
def _check_retval_(value):
@@ -25,15 +26,11 @@ class Test(unittest.TestCase):
del dll._testfunc_p_p.restype
self.assertEqual(42, dll._testfunc_p_p(42))
- try:
- oledll
- except NameError:
- pass
- else:
- def test_oledll(self):
- self.assertRaises(WindowsError,
- oledll.oleaut32.CreateTypeLib2,
- 0, None, None)
+ @need_symbol('oledll')
+ def test_oledll(self):
+ self.assertRaises(WindowsError,
+ oledll.oleaut32.CreateTypeLib2,
+ 0, None, None)
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/ctypes/test/test_errcheck.py b/Lib/ctypes/test/test_errcheck.py
deleted file mode 100644
index a4913f9..0000000
--- a/Lib/ctypes/test/test_errcheck.py
+++ /dev/null
@@ -1,19 +0,0 @@
-import sys
-from ctypes import *
-
-##class HMODULE(Structure):
-## _fields_ = [("value", c_void_p)]
-
-## def __repr__(self):
-## return "<HMODULE %s>" % self.value
-
-##windll.kernel32.GetModuleHandleA.restype = HMODULE
-
-##print windll.kernel32.GetModuleHandleA("python23.dll")
-##print hex(sys.dllhandle)
-
-##def nonzero(handle):
-## return (GetLastError(), handle)
-
-##windll.kernel32.GetModuleHandleA.errcheck = nonzero
-##print windll.kernel32.GetModuleHandleA("spam")
diff --git a/Lib/ctypes/test/test_find.py b/Lib/ctypes/test/test_find.py
index 77e789f..c5f6ace 100644
--- a/Lib/ctypes/test/test_find.py
+++ b/Lib/ctypes/test/test_find.py
@@ -1,4 +1,5 @@
import unittest
+import os
import sys
from ctypes import *
from ctypes.util import find_library
@@ -40,43 +41,43 @@ class Test_OpenGL_libs(unittest.TestCase):
except OSError:
pass
- if lib_gl:
- def test_gl(self):
- if self.gl:
- self.gl.glClearIndex
+ @unittest.skipUnless(lib_gl, 'lib_gl not available')
+ def test_gl(self):
+ if self.gl:
+ self.gl.glClearIndex
- if lib_glu:
- def test_glu(self):
- if self.glu:
- self.glu.gluBeginCurve
+ @unittest.skipUnless(lib_glu, 'lib_glu not available')
+ def test_glu(self):
+ if self.glu:
+ self.glu.gluBeginCurve
- if lib_gle:
- def test_gle(self):
- if self.gle:
- self.gle.gleGetJoinStyle
+ @unittest.skipUnless(lib_gle, 'lib_gle not available')
+ def test_gle(self):
+ if self.gle:
+ self.gle.gleGetJoinStyle
-##if os.name == "posix" and sys.platform != "darwin":
-
-## # On platforms where the default shared library suffix is '.so',
-## # at least some libraries can be loaded as attributes of the cdll
-## # object, since ctypes now tries loading the lib again
-## # with '.so' appended of the first try fails.
-## #
-## # Won't work for libc, unfortunately. OTOH, it isn't
-## # needed for libc since this is already mapped into the current
-## # process (?)
-## #
-## # On MAC OSX, it won't work either, because dlopen() needs a full path,
-## # and the default suffix is either none or '.dylib'.
-
-## class LoadLibs(unittest.TestCase):
-## def test_libm(self):
-## import math
-## libm = cdll.libm
-## sqrt = libm.sqrt
-## sqrt.argtypes = (c_double,)
-## sqrt.restype = c_double
-## self.assertEqual(sqrt(2), math.sqrt(2))
+# On platforms where the default shared library suffix is '.so',
+# at least some libraries can be loaded as attributes of the cdll
+# object, since ctypes now tries loading the lib again
+# with '.so' appended of the first try fails.
+#
+# Won't work for libc, unfortunately. OTOH, it isn't
+# needed for libc since this is already mapped into the current
+# process (?)
+#
+# On MAC OSX, it won't work either, because dlopen() needs a full path,
+# and the default suffix is either none or '.dylib'.
+@unittest.skip('test disabled')
+@unittest.skipUnless(os.name=="posix" and sys.platform != "darwin",
+ 'test not suitable for this platform')
+class LoadLibs(unittest.TestCase):
+ def test_libm(self):
+ import math
+ libm = cdll.libm
+ sqrt = libm.sqrt
+ sqrt.argtypes = (c_double,)
+ sqrt.restype = c_double
+ self.assertEqual(sqrt(2), math.sqrt(2))
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/ctypes/test/test_functions.py b/Lib/ctypes/test/test_functions.py
index 2c30e97..a374415 100644
--- a/Lib/ctypes/test/test_functions.py
+++ b/Lib/ctypes/test/test_functions.py
@@ -6,6 +6,7 @@ Later...
"""
from ctypes import *
+from ctypes.test import need_symbol
import sys, unittest
try:
@@ -63,22 +64,16 @@ class FunctionTestCase(unittest.TestCase):
pass
+ @need_symbol('c_wchar')
def test_wchar_parm(self):
- try:
- c_wchar
- except NameError:
- return
f = dll._testfunc_i_bhilfd
f.argtypes = [c_byte, c_wchar, c_int, c_long, c_float, c_double]
result = f(1, u"x", 3, 4, 5.0, 6.0)
self.assertEqual(result, 139)
self.assertEqual(type(result), int)
+ @need_symbol('c_wchar')
def test_wchar_result(self):
- try:
- c_wchar
- except NameError:
- return
f = dll._testfunc_i_bhilfd
f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double]
f.restype = c_wchar
@@ -155,11 +150,8 @@ class FunctionTestCase(unittest.TestCase):
self.assertEqual(result, -21)
self.assertEqual(type(result), float)
+ @need_symbol('c_longlong')
def test_longlongresult(self):
- try:
- c_longlong
- except NameError:
- return
f = dll._testfunc_q_bhilfd
f.restype = c_longlong
f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double]
@@ -296,6 +288,7 @@ class FunctionTestCase(unittest.TestCase):
result = f(-10, cb)
self.assertEqual(result, -18)
+ @need_symbol('c_longlong')
def test_longlong_callbacks(self):
f = dll._testfunc_callback_q_qf
@@ -348,16 +341,16 @@ class FunctionTestCase(unittest.TestCase):
s2h = dll.ret_2h_func(inp)
self.assertEqual((s2h.x, s2h.y), (99*2, 88*3))
- if sys.platform == "win32":
- def test_struct_return_2H_stdcall(self):
- class S2H(Structure):
- _fields_ = [("x", c_short),
- ("y", c_short)]
+ @unittest.skipUnless(sys.platform == "win32", 'Windows-specific test')
+ def test_struct_return_2H_stdcall(self):
+ class S2H(Structure):
+ _fields_ = [("x", c_short),
+ ("y", c_short)]
- windll.s_ret_2h_func.restype = S2H
- windll.s_ret_2h_func.argtypes = [S2H]
- s2h = windll.s_ret_2h_func(S2H(99, 88))
- self.assertEqual((s2h.x, s2h.y), (99*2, 88*3))
+ windll.s_ret_2h_func.restype = S2H
+ windll.s_ret_2h_func.argtypes = [S2H]
+ s2h = windll.s_ret_2h_func(S2H(99, 88))
+ self.assertEqual((s2h.x, s2h.y), (99*2, 88*3))
def test_struct_return_8H(self):
class S8I(Structure):
@@ -376,23 +369,24 @@ class FunctionTestCase(unittest.TestCase):
self.assertEqual((s8i.a, s8i.b, s8i.c, s8i.d, s8i.e, s8i.f, s8i.g, s8i.h),
(9*2, 8*3, 7*4, 6*5, 5*6, 4*7, 3*8, 2*9))
- if sys.platform == "win32":
- def test_struct_return_8H_stdcall(self):
- class S8I(Structure):
- _fields_ = [("a", c_int),
- ("b", c_int),
- ("c", c_int),
- ("d", c_int),
- ("e", c_int),
- ("f", c_int),
- ("g", c_int),
- ("h", c_int)]
- windll.s_ret_8i_func.restype = S8I
- windll.s_ret_8i_func.argtypes = [S8I]
- inp = S8I(9, 8, 7, 6, 5, 4, 3, 2)
- s8i = windll.s_ret_8i_func(inp)
- self.assertEqual((s8i.a, s8i.b, s8i.c, s8i.d, s8i.e, s8i.f, s8i.g, s8i.h),
- (9*2, 8*3, 7*4, 6*5, 5*6, 4*7, 3*8, 2*9))
+ @unittest.skipUnless(sys.platform == "win32", 'Windows-specific test')
+ def test_struct_return_8H_stdcall(self):
+ class S8I(Structure):
+ _fields_ = [("a", c_int),
+ ("b", c_int),
+ ("c", c_int),
+ ("d", c_int),
+ ("e", c_int),
+ ("f", c_int),
+ ("g", c_int),
+ ("h", c_int)]
+ windll.s_ret_8i_func.restype = S8I
+ windll.s_ret_8i_func.argtypes = [S8I]
+ inp = S8I(9, 8, 7, 6, 5, 4, 3, 2)
+ s8i = windll.s_ret_8i_func(inp)
+ self.assertEqual(
+ (s8i.a, s8i.b, s8i.c, s8i.d, s8i.e, s8i.f, s8i.g, s8i.h),
+ (9*2, 8*3, 7*4, 6*5, 5*6, 4*7, 3*8, 2*9))
def test_sf1651235(self):
# see http://www.python.org/sf/1651235
diff --git a/Lib/ctypes/test/test_integers.py b/Lib/ctypes/test/test_integers.py
deleted file mode 100644
index 62e4b08..0000000
--- a/Lib/ctypes/test/test_integers.py
+++ /dev/null
@@ -1,5 +0,0 @@
-# superseded by test_numbers.py
-import unittest
-
-if __name__ == '__main__':
- unittest.main()
diff --git a/Lib/ctypes/test/test_keeprefs.py b/Lib/ctypes/test/test_keeprefs.py
index b504c0c..b2a50ab 100644
--- a/Lib/ctypes/test/test_keeprefs.py
+++ b/Lib/ctypes/test/test_keeprefs.py
@@ -94,7 +94,8 @@ class PointerTestCase(unittest.TestCase):
self.assertEqual(x._objects, {'1': i})
class DeletePointerTestCase(unittest.TestCase):
- def X_test(self):
+ @unittest.skip('test disabled')
+ def test_X(self):
class X(Structure):
_fields_ = [("p", POINTER(c_char_p))]
x = X()
diff --git a/Lib/ctypes/test/test_loading.py b/Lib/ctypes/test/test_loading.py
index e415601..81a27e3 100644
--- a/Lib/ctypes/test/test_loading.py
+++ b/Lib/ctypes/test/test_loading.py
@@ -21,18 +21,21 @@ class LoaderTest(unittest.TestCase):
unknowndll = "xxrandomnamexx"
- if libc_name is not None:
- def test_load(self):
- CDLL(libc_name)
- CDLL(os.path.basename(libc_name))
- self.assertRaises(OSError, CDLL, self.unknowndll)
+ @unittest.skipUnless(libc_name is not None, 'could not find libc')
+ def test_load(self):
+ CDLL(libc_name)
+ CDLL(os.path.basename(libc_name))
+ self.assertRaises(OSError, CDLL, self.unknowndll)
- if libc_name is not None and os.path.basename(libc_name) == "libc.so.6":
- def test_load_version(self):
- cdll.LoadLibrary("libc.so.6")
- # linux uses version, libc 9 should not exist
- self.assertRaises(OSError, cdll.LoadLibrary, "libc.so.9")
- self.assertRaises(OSError, cdll.LoadLibrary, self.unknowndll)
+ @unittest.skipUnless(libc_name is not None, 'could not find libc')
+ @unittest.skipUnless(libc_name is not None and
+ os.path.basename(libc_name) == "libc.so.6",
+ 'wrong libc path for test')
+ def test_load_version(self):
+ cdll.LoadLibrary("libc.so.6")
+ # linux uses version, libc 9 should not exist
+ self.assertRaises(OSError, cdll.LoadLibrary, "libc.so.9")
+ self.assertRaises(OSError, cdll.LoadLibrary, self.unknowndll)
def test_find(self):
for name in ("c", "m"):
@@ -41,66 +44,71 @@ class LoaderTest(unittest.TestCase):
cdll.LoadLibrary(lib)
CDLL(lib)
- if os.name in ("nt", "ce"):
- def test_load_library(self):
- self.assertIsNotNone(libc_name)
- if is_resource_enabled("printing"):
- print find_library("kernel32")
- print find_library("user32")
+ @unittest.skipUnless(os.name in ("nt", "ce"),
+ 'test specific to Windows (NT/CE)')
+ def test_load_library(self):
+ self.assertIsNotNone(libc_name)
+ if is_resource_enabled("printing"):
+ print find_library("kernel32")
+ print find_library("user32")
- if os.name == "nt":
- windll.kernel32.GetModuleHandleW
- windll["kernel32"].GetModuleHandleW
- windll.LoadLibrary("kernel32").GetModuleHandleW
- WinDLL("kernel32").GetModuleHandleW
- elif os.name == "ce":
- windll.coredll.GetModuleHandleW
- windll["coredll"].GetModuleHandleW
- windll.LoadLibrary("coredll").GetModuleHandleW
- WinDLL("coredll").GetModuleHandleW
+ if os.name == "nt":
+ windll.kernel32.GetModuleHandleW
+ windll["kernel32"].GetModuleHandleW
+ windll.LoadLibrary("kernel32").GetModuleHandleW
+ WinDLL("kernel32").GetModuleHandleW
+ elif os.name == "ce":
+ windll.coredll.GetModuleHandleW
+ windll["coredll"].GetModuleHandleW
+ windll.LoadLibrary("coredll").GetModuleHandleW
+ WinDLL("coredll").GetModuleHandleW
- def test_load_ordinal_functions(self):
- import _ctypes_test
- dll = WinDLL(_ctypes_test.__file__)
- # We load the same function both via ordinal and name
- func_ord = dll[2]
- func_name = dll.GetString
- # addressof gets the address where the function pointer is stored
- a_ord = addressof(func_ord)
- a_name = addressof(func_name)
- f_ord_addr = c_void_p.from_address(a_ord).value
- f_name_addr = c_void_p.from_address(a_name).value
- self.assertEqual(hex(f_ord_addr), hex(f_name_addr))
+ @unittest.skipUnless(os.name in ("nt", "ce"),
+ 'test specific to Windows (NT/CE)')
+ def test_load_ordinal_functions(self):
+ import _ctypes_test
+ dll = WinDLL(_ctypes_test.__file__)
+ # We load the same function both via ordinal and name
+ func_ord = dll[2]
+ func_name = dll.GetString
+ # addressof gets the address where the function pointer is stored
+ a_ord = addressof(func_ord)
+ a_name = addressof(func_name)
+ f_ord_addr = c_void_p.from_address(a_ord).value
+ f_name_addr = c_void_p.from_address(a_name).value
+ self.assertEqual(hex(f_ord_addr), hex(f_name_addr))
- self.assertRaises(AttributeError, dll.__getitem__, 1234)
+ self.assertRaises(AttributeError, dll.__getitem__, 1234)
- if os.name == "nt":
- def test_1703286_A(self):
- from _ctypes import LoadLibrary, FreeLibrary
- # On winXP 64-bit, advapi32 loads at an address that does
- # NOT fit into a 32-bit integer. FreeLibrary must be able
- # to accept this address.
+ @unittest.skipUnless(os.name == "nt", 'Windows-specific test')
+ def test_1703286_A(self):
+ from _ctypes import LoadLibrary, FreeLibrary
+ # On winXP 64-bit, advapi32 loads at an address that does
+ # NOT fit into a 32-bit integer. FreeLibrary must be able
+ # to accept this address.
- # These are tests for http://www.python.org/sf/1703286
- handle = LoadLibrary("advapi32")
- FreeLibrary(handle)
+ # These are tests for http://www.python.org/sf/1703286
+ handle = LoadLibrary("advapi32")
+ FreeLibrary(handle)
- def test_1703286_B(self):
- # Since on winXP 64-bit advapi32 loads like described
- # above, the (arbitrarily selected) CloseEventLog function
- # also has a high address. 'call_function' should accept
- # addresses so large.
- from _ctypes import call_function
- advapi32 = windll.advapi32
- # Calling CloseEventLog with a NULL argument should fail,
- # but the call should not segfault or so.
- self.assertEqual(0, advapi32.CloseEventLog(None))
- windll.kernel32.GetProcAddress.argtypes = c_void_p, c_char_p
- windll.kernel32.GetProcAddress.restype = c_void_p
- proc = windll.kernel32.GetProcAddress(advapi32._handle, "CloseEventLog")
- self.assertTrue(proc)
- # This is the real test: call the function via 'call_function'
- self.assertEqual(0, call_function(proc, (None,)))
+ @unittest.skipUnless(os.name == "nt", 'Windows-specific test')
+ def test_1703286_B(self):
+ # Since on winXP 64-bit advapi32 loads like described
+ # above, the (arbitrarily selected) CloseEventLog function
+ # also has a high address. 'call_function' should accept
+ # addresses so large.
+ from _ctypes import call_function
+ advapi32 = windll.advapi32
+ # Calling CloseEventLog with a NULL argument should fail,
+ # but the call should not segfault or so.
+ self.assertEqual(0, advapi32.CloseEventLog(None))
+ windll.kernel32.GetProcAddress.argtypes = c_void_p, c_char_p
+ windll.kernel32.GetProcAddress.restype = c_void_p
+ proc = windll.kernel32.GetProcAddress(advapi32._handle,
+ "CloseEventLog")
+ self.assertTrue(proc)
+ # This is the real test: call the function via 'call_function'
+ self.assertEqual(0, call_function(proc, (None,)))
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/ctypes/test/test_macholib.py b/Lib/ctypes/test/test_macholib.py
index cf50f50..9779b2f 100644
--- a/Lib/ctypes/test/test_macholib.py
+++ b/Lib/ctypes/test/test_macholib.py
@@ -45,21 +45,21 @@ def find_lib(name):
raise ValueError("%s not found" % (name,))
class MachOTest(unittest.TestCase):
- if sys.platform == "darwin":
- def test_find(self):
+ @unittest.skipUnless(sys.platform == "darwin", 'OSX-specific test')
+ def test_find(self):
- self.assertEqual(find_lib('pthread'),
- '/usr/lib/libSystem.B.dylib')
+ self.assertEqual(find_lib('pthread'),
+ '/usr/lib/libSystem.B.dylib')
- result = find_lib('z')
- # Issue #21093: dyld default search path includes $HOME/lib and
- # /usr/local/lib before /usr/lib, which caused test failures if
- # a local copy of libz exists in one of them. Now ignore the head
- # of the path.
- self.assertRegexpMatches(result, r".*/lib/libz\..*.*\.dylib")
+ result = find_lib('z')
+ # Issue #21093: dyld default search path includes $HOME/lib and
+ # /usr/local/lib before /usr/lib, which caused test failures if
+ # a local copy of libz exists in one of them. Now ignore the head
+ # of the path.
+ self.assertRegexpMatches(result, r".*/lib/libz\..*.*\.dylib")
- self.assertEqual(find_lib('IOKit'),
- '/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit')
+ self.assertEqual(find_lib('IOKit'),
+ '/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit')
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/ctypes/test/test_memfunctions.py b/Lib/ctypes/test/test_memfunctions.py
index d072603..ba9eea6 100644
--- a/Lib/ctypes/test/test_memfunctions.py
+++ b/Lib/ctypes/test/test_memfunctions.py
@@ -1,17 +1,19 @@
import sys
import unittest
from ctypes import *
+from ctypes.test import need_symbol
class MemFunctionsTest(unittest.TestCase):
-## def test_overflow(self):
-## # string_at and wstring_at must use the Python calling
-## # convention (which acquires the GIL and checks the Python
-## # error flag). Provoke an error and catch it; see also issue
-## # #3554: <http://bugs.python.org/issue3554>
-## self.assertRaises((OverflowError, MemoryError, SystemError),
-## lambda: wstring_at(u"foo", sys.maxint - 1))
-## self.assertRaises((OverflowError, MemoryError, SystemError),
-## lambda: string_at("foo", sys.maxint - 1))
+ @unittest.skip('test disabled')
+ def test_overflow(self):
+ # string_at and wstring_at must use the Python calling
+ # convention (which acquires the GIL and checks the Python
+ # error flag). Provoke an error and catch it; see also issue
+ # #3554: <http://bugs.python.org/issue3554>
+ self.assertRaises((OverflowError, MemoryError, SystemError),
+ lambda: wstring_at(u"foo", sys.maxint - 1))
+ self.assertRaises((OverflowError, MemoryError, SystemError),
+ lambda: string_at("foo", sys.maxint - 1))
def test_memmove(self):
# large buffers apparently increase the chance that the memory
@@ -59,21 +61,17 @@ class MemFunctionsTest(unittest.TestCase):
self.assertEqual(string_at("foo bar", 8), "foo bar\0")
self.assertEqual(string_at("foo bar", 3), "foo")
- try:
- create_unicode_buffer
- except NameError:
- pass
- else:
- def test_wstring_at(self):
- p = create_unicode_buffer("Hello, World")
- a = create_unicode_buffer(1000000)
- result = memmove(a, p, len(p) * sizeof(c_wchar))
- self.assertEqual(a.value, "Hello, World")
+ @need_symbol('create_unicode_buffer')
+ def test_wstring_at(self):
+ p = create_unicode_buffer("Hello, World")
+ a = create_unicode_buffer(1000000)
+ result = memmove(a, p, len(p) * sizeof(c_wchar))
+ self.assertEqual(a.value, "Hello, World")
- self.assertEqual(wstring_at(a), "Hello, World")
- self.assertEqual(wstring_at(a, 5), "Hello")
- self.assertEqual(wstring_at(a, 16), "Hello, World\0\0\0\0")
- self.assertEqual(wstring_at(a, 0), "")
+ self.assertEqual(wstring_at(a), "Hello, World")
+ self.assertEqual(wstring_at(a, 5), "Hello")
+ self.assertEqual(wstring_at(a, 16), "Hello, World\0\0\0\0")
+ self.assertEqual(wstring_at(a, 0), "")
if __name__ == "__main__":
unittest.main()
diff --git a/Lib/ctypes/test/test_numbers.py b/Lib/ctypes/test/test_numbers.py
index 73c2350..d623465 100644
--- a/Lib/ctypes/test/test_numbers.py
+++ b/Lib/ctypes/test/test_numbers.py
@@ -82,12 +82,13 @@ class NumberTestCase(unittest.TestCase):
self.assertRaises(TypeError, t, "")
self.assertRaises(TypeError, t, None)
-## def test_valid_ranges(self):
-## # invalid values of the correct type
-## # raise ValueError (not OverflowError)
-## for t, (l, h) in zip(unsigned_types, unsigned_ranges):
-## self.assertRaises(ValueError, t, l-1)
-## self.assertRaises(ValueError, t, h+1)
+ @unittest.skip('test disabled')
+ def test_valid_ranges(self):
+ # invalid values of the correct type
+ # raise ValueError (not OverflowError)
+ for t, (l, h) in zip(unsigned_types, unsigned_ranges):
+ self.assertRaises(ValueError, t, l-1)
+ self.assertRaises(ValueError, t, h+1)
def test_from_param(self):
# the from_param class method attribute always
@@ -199,16 +200,17 @@ class NumberTestCase(unittest.TestCase):
self.assertEqual(v.value, a[0])
# array does not support c_bool / 't'
- # 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])
- # self.assertEqual(type(v) is t)
- # a[0] = False
- # self.assertEqual(v.value, a[0])
- # self.assertEqual(type(v) is 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])
+ self.assertEqual(type(v) is t)
+ a[0] = False
+ self.assertEqual(v.value, a[0])
+ self.assertEqual(type(v) is t)
def test_init(self):
# c_int() can be initialized from Python's int, and c_int.
@@ -226,8 +228,9 @@ class NumberTestCase(unittest.TestCase):
if (hasattr(t, "__ctype_le__")):
self.assertRaises(OverflowError, t.__ctype_le__, big_int)
-## def test_perf(self):
-## check_perf()
+ @unittest.skip('test disabled')
+ def test_perf(self):
+ check_perf()
from ctypes import _SimpleCData
class c_int_S(_SimpleCData):
diff --git a/Lib/ctypes/test/test_objects.py b/Lib/ctypes/test/test_objects.py
index 4d921d2..a7c5247 100644
--- a/Lib/ctypes/test/test_objects.py
+++ b/Lib/ctypes/test/test_objects.py
@@ -59,12 +59,9 @@ import unittest, doctest, sys
import ctypes.test.test_objects
class TestCase(unittest.TestCase):
- if sys.hexversion > 0x02040000:
- # Python 2.3 has no ELLIPSIS flag, so we don't test with this
- # version:
- def test(self):
- doctest.testmod(ctypes.test.test_objects)
+ def test(self):
+ failures, tests = doctest.testmod(ctypes.test.test_objects)
+ self.assertFalse(failures, 'doctests failed, see output above')
if __name__ == '__main__':
- if sys.hexversion > 0x02040000:
- doctest.testmod(ctypes.test.test_objects)
+ doctest.testmod(ctypes.test.test_objects)
diff --git a/Lib/ctypes/test/test_parameters.py b/Lib/ctypes/test/test_parameters.py
index 22edd83..192d9ed 100644
--- a/Lib/ctypes/test/test_parameters.py
+++ b/Lib/ctypes/test/test_parameters.py
@@ -1,4 +1,5 @@
import unittest, sys
+from ctypes.test import need_symbol
class SimpleTypesTestCase(unittest.TestCase):
@@ -36,10 +37,9 @@ class SimpleTypesTestCase(unittest.TestCase):
self.assertEqual(CVOIDP.from_param("abc"), "abcabc")
self.assertEqual(CCHARP.from_param("abc"), "abcabcabcabc")
- try:
- from ctypes import c_wchar_p
- except ImportError:
- return
+ @need_symbol('c_wchar_p')
+ def test_subclasses_c_wchar_p(self):
+ from ctypes import c_wchar_p
class CWCHARP(c_wchar_p):
def from_param(cls, value):
@@ -68,13 +68,9 @@ class SimpleTypesTestCase(unittest.TestCase):
a = c_char_p("123")
self.assertIs(c_char_p.from_param(a), a)
+ @need_symbol('c_wchar_p')
def test_cw_strings(self):
- from ctypes import byref
- try:
- from ctypes import c_wchar_p
- except ImportError:
-## print "(No c_wchar_p)"
- return
+ from ctypes import byref, c_wchar_p
s = u"123"
if sys.platform == "win32":
self.assertTrue(c_wchar_p.from_param(s)._obj is s)
@@ -144,9 +140,6 @@ class SimpleTypesTestCase(unittest.TestCase):
self.assertRaises(TypeError, LPINT.from_param, c_long*3)
self.assertRaises(TypeError, LPINT.from_param, c_uint*3)
-## def test_performance(self):
-## check_perf()
-
def test_noctypes_argtype(self):
import _ctypes_test
from ctypes import CDLL, c_void_p, ArgumentError
diff --git a/Lib/ctypes/test/test_prototypes.py b/Lib/ctypes/test/test_prototypes.py
index 09ba655..a10317b 100644
--- a/Lib/ctypes/test/test_prototypes.py
+++ b/Lib/ctypes/test/test_prototypes.py
@@ -1,4 +1,5 @@
from ctypes import *
+from ctypes.test import need_symbol
import unittest
# IMPORTANT INFO:
@@ -135,13 +136,14 @@ class CharPointersTestCase(unittest.TestCase):
func(pointer(c_int()))
func((c_int * 3)())
- try:
- func.restype = c_wchar_p
- except NameError:
- pass
- else:
- self.assertEqual(None, func(c_wchar_p(None)))
- self.assertEqual(u"123", func(c_wchar_p(u"123")))
+ @need_symbol('c_wchar_p')
+ def test_c_void_p_arg_with_c_wchar_p(self):
+ func = testdll._testfunc_p_p
+ func.restype = c_wchar_p
+ func.argtypes = c_void_p,
+
+ self.assertEqual(None, func(c_wchar_p(None)))
+ self.assertEqual(u"123", func(c_wchar_p(u"123")))
def test_instance(self):
func = testdll._testfunc_p_p
@@ -156,51 +158,47 @@ class CharPointersTestCase(unittest.TestCase):
func.argtypes = None
self.assertEqual(None, func(X()))
-try:
- c_wchar
-except NameError:
- pass
-else:
- class WCharPointersTestCase(unittest.TestCase):
-
- def setUp(self):
- func = testdll._testfunc_p_p
- func.restype = c_int
- func.argtypes = None
-
-
- def test_POINTER_c_wchar_arg(self):
- func = testdll._testfunc_p_p
- func.restype = c_wchar_p
- func.argtypes = POINTER(c_wchar),
-
- self.assertEqual(None, func(None))
- self.assertEqual(u"123", func(u"123"))
- self.assertEqual(None, func(c_wchar_p(None)))
- self.assertEqual(u"123", func(c_wchar_p(u"123")))
-
- self.assertEqual(u"123", func(c_wbuffer(u"123")))
- ca = c_wchar("a")
- self.assertEqual(u"a", func(pointer(ca))[0])
- self.assertEqual(u"a", func(byref(ca))[0])
-
- def test_c_wchar_p_arg(self):
- func = testdll._testfunc_p_p
- func.restype = c_wchar_p
- func.argtypes = c_wchar_p,
-
- c_wchar_p.from_param(u"123")
-
- self.assertEqual(None, func(None))
- self.assertEqual("123", func(u"123"))
- self.assertEqual(None, func(c_wchar_p(None)))
- self.assertEqual("123", func(c_wchar_p("123")))
-
- # XXX Currently, these raise TypeErrors, although they shouldn't:
- self.assertEqual("123", func(c_wbuffer("123")))
- ca = c_wchar("a")
- self.assertEqual("a", func(pointer(ca))[0])
- self.assertEqual("a", func(byref(ca))[0])
+@need_symbol('c_wchar')
+class WCharPointersTestCase(unittest.TestCase):
+
+ def setUp(self):
+ func = testdll._testfunc_p_p
+ func.restype = c_int
+ func.argtypes = None
+
+
+ def test_POINTER_c_wchar_arg(self):
+ func = testdll._testfunc_p_p
+ func.restype = c_wchar_p
+ func.argtypes = POINTER(c_wchar),
+
+ self.assertEqual(None, func(None))
+ self.assertEqual(u"123", func(u"123"))
+ self.assertEqual(None, func(c_wchar_p(None)))
+ self.assertEqual(u"123", func(c_wchar_p(u"123")))
+
+ self.assertEqual(u"123", func(c_wbuffer(u"123")))
+ ca = c_wchar("a")
+ self.assertEqual(u"a", func(pointer(ca))[0])
+ self.assertEqual(u"a", func(byref(ca))[0])
+
+ def test_c_wchar_p_arg(self):
+ func = testdll._testfunc_p_p
+ func.restype = c_wchar_p
+ func.argtypes = c_wchar_p,
+
+ c_wchar_p.from_param(u"123")
+
+ self.assertEqual(None, func(None))
+ self.assertEqual("123", func(u"123"))
+ self.assertEqual(None, func(c_wchar_p(None)))
+ self.assertEqual("123", func(c_wchar_p("123")))
+
+ # XXX Currently, these raise TypeErrors, although they shouldn't:
+ self.assertEqual("123", func(c_wbuffer("123")))
+ ca = c_wchar("a")
+ self.assertEqual("a", func(pointer(ca))[0])
+ self.assertEqual("a", func(byref(ca))[0])
class ArrayTest(unittest.TestCase):
def test(self):
diff --git a/Lib/ctypes/test/test_python_api.py b/Lib/ctypes/test/test_python_api.py
index 74af04c..62728dd 100644
--- a/Lib/ctypes/test/test_python_api.py
+++ b/Lib/ctypes/test/test_python_api.py
@@ -1,6 +1,6 @@
from ctypes import *
import unittest, sys
-from ctypes.test import is_resource_enabled
+from ctypes.test import requires
################################################################
# This section should be moved into ctypes\__init__.py, when it's ready.
@@ -37,24 +37,24 @@ class PythonAPITestCase(unittest.TestCase):
del pyob
self.assertEqual(grc(s), refcnt)
- if is_resource_enabled("refcount"):
- # This test is unreliable, because it is possible that code in
- # unittest changes the refcount of the '42' integer. So, it
- # is disabled by default.
- def test_PyInt_Long(self):
- ref42 = grc(42)
- pythonapi.PyInt_FromLong.restype = py_object
- self.assertEqual(pythonapi.PyInt_FromLong(42), 42)
+ # This test is unreliable, because it is possible that code in
+ # unittest changes the refcount of the '42' integer. So, it
+ # is disabled by default.
+ @requires("refcount")
+ def test_PyInt_Long(self):
+ ref42 = grc(42)
+ pythonapi.PyInt_FromLong.restype = py_object
+ self.assertEqual(pythonapi.PyInt_FromLong(42), 42)
- self.assertEqual(grc(42), ref42)
+ self.assertEqual(grc(42), ref42)
- pythonapi.PyInt_AsLong.argtypes = (py_object,)
- pythonapi.PyInt_AsLong.restype = c_long
+ pythonapi.PyInt_AsLong.argtypes = (py_object,)
+ pythonapi.PyInt_AsLong.restype = c_long
- res = pythonapi.PyInt_AsLong(42)
- self.assertEqual(grc(res), ref42 + 1)
- del res
- self.assertEqual(grc(42), ref42)
+ res = pythonapi.PyInt_AsLong(42)
+ self.assertEqual(grc(res), ref42 + 1)
+ del res
+ self.assertEqual(grc(42), ref42)
def test_PyObj_FromPtr(self):
s = "abc def ghi jkl"
diff --git a/Lib/ctypes/test/test_random_things.py b/Lib/ctypes/test/test_random_things.py
index 1c217c3..0caffe3 100644
--- a/Lib/ctypes/test/test_random_things.py
+++ b/Lib/ctypes/test/test_random_things.py
@@ -5,23 +5,22 @@ def callback_func(arg):
42 // arg
raise ValueError(arg)
-if sys.platform == "win32":
+@unittest.skipUnless(sys.platform == "win32", 'Windows-specific test')
+class call_function_TestCase(unittest.TestCase):
+ # _ctypes.call_function is deprecated and private, but used by
+ # Gary Bishp's readline module. If we have it, we must test it as well.
- class call_function_TestCase(unittest.TestCase):
- # _ctypes.call_function is deprecated and private, but used by
- # Gary Bishp's readline module. If we have it, we must test it as well.
+ def test(self):
+ from _ctypes import call_function
+ windll.kernel32.LoadLibraryA.restype = c_void_p
+ windll.kernel32.GetProcAddress.argtypes = c_void_p, c_char_p
+ windll.kernel32.GetProcAddress.restype = c_void_p
- def test(self):
- from _ctypes import call_function
- windll.kernel32.LoadLibraryA.restype = c_void_p
- windll.kernel32.GetProcAddress.argtypes = c_void_p, c_char_p
- windll.kernel32.GetProcAddress.restype = c_void_p
+ hdll = windll.kernel32.LoadLibraryA("kernel32")
+ funcaddr = windll.kernel32.GetProcAddress(hdll, "GetModuleHandleA")
- hdll = windll.kernel32.LoadLibraryA("kernel32")
- funcaddr = windll.kernel32.GetProcAddress(hdll, "GetModuleHandleA")
-
- self.assertEqual(call_function(funcaddr, (None,)),
- windll.kernel32.GetModuleHandleA(None))
+ self.assertEqual(call_function(funcaddr, (None,)),
+ windll.kernel32.GetModuleHandleA(None))
class CallbackTracbackTestCase(unittest.TestCase):
# When an exception is raised in a ctypes callback function, the C
diff --git a/Lib/ctypes/test/test_slicing.py b/Lib/ctypes/test/test_slicing.py
index a5632ec..a9ee3a8 100644
--- a/Lib/ctypes/test/test_slicing.py
+++ b/Lib/ctypes/test/test_slicing.py
@@ -1,5 +1,6 @@
import unittest
from ctypes import *
+from ctypes.test import need_symbol
import _ctypes_test
@@ -131,46 +132,42 @@ class SlicesTestCase(unittest.TestCase):
self.assertEqual(p[2:5:-3], s[2:5:-3])
- try:
- c_wchar
- except NameError:
- pass
- else:
- def test_wchar_ptr(self):
- s = u"abcdefghijklmnopqrstuvwxyz\0"
-
- dll = CDLL(_ctypes_test.__file__)
- dll.my_wcsdup.restype = POINTER(c_wchar)
- dll.my_wcsdup.argtypes = POINTER(c_wchar),
- dll.my_free.restype = None
- res = dll.my_wcsdup(s)
- self.assertEqual(res[:len(s)], s)
- self.assertEqual(res[:len(s):], s)
- self.assertEqual(res[len(s)-1:-1:-1], s[::-1])
- self.assertEqual(res[len(s)-1:5:-7], s[:5:-7])
-
- import operator
- self.assertRaises(TypeError, operator.setslice,
- res, 0, 5, u"abcde")
- self.assertRaises(TypeError, operator.setitem,
- res, slice(0, 5), u"abcde")
- dll.my_free(res)
-
- if sizeof(c_wchar) == sizeof(c_short):
- dll.my_wcsdup.restype = POINTER(c_short)
- elif sizeof(c_wchar) == sizeof(c_int):
- dll.my_wcsdup.restype = POINTER(c_int)
- elif sizeof(c_wchar) == sizeof(c_long):
- dll.my_wcsdup.restype = POINTER(c_long)
- else:
- return
- res = dll.my_wcsdup(s)
- tmpl = range(ord("a"), ord("z")+1)
- self.assertEqual(res[:len(s)-1], tmpl)
- self.assertEqual(res[:len(s)-1:], tmpl)
- self.assertEqual(res[len(s)-2:-1:-1], tmpl[::-1])
- self.assertEqual(res[len(s)-2:5:-7], tmpl[:5:-7])
- dll.my_free(res)
+ @need_symbol('c_wchar')
+ def test_wchar_ptr(self):
+ s = u"abcdefghijklmnopqrstuvwxyz\0"
+
+ dll = CDLL(_ctypes_test.__file__)
+ dll.my_wcsdup.restype = POINTER(c_wchar)
+ dll.my_wcsdup.argtypes = POINTER(c_wchar),
+ dll.my_free.restype = None
+ res = dll.my_wcsdup(s)
+ self.assertEqual(res[:len(s)], s)
+ self.assertEqual(res[:len(s):], s)
+ self.assertEqual(res[len(s)-1:-1:-1], s[::-1])
+ self.assertEqual(res[len(s)-1:5:-7], s[:5:-7])
+
+ import operator
+ self.assertRaises(TypeError, operator.setslice,
+ res, 0, 5, u"abcde")
+ self.assertRaises(TypeError, operator.setitem,
+ res, slice(0, 5), u"abcde")
+ dll.my_free(res)
+
+ if sizeof(c_wchar) == sizeof(c_short):
+ dll.my_wcsdup.restype = POINTER(c_short)
+ elif sizeof(c_wchar) == sizeof(c_int):
+ dll.my_wcsdup.restype = POINTER(c_int)
+ elif sizeof(c_wchar) == sizeof(c_long):
+ dll.my_wcsdup.restype = POINTER(c_long)
+ else:
+ self.skipTest('Pointers to c_wchar are not supported')
+ res = dll.my_wcsdup(s)
+ tmpl = range(ord("a"), ord("z")+1)
+ self.assertEqual(res[:len(s)-1], tmpl)
+ self.assertEqual(res[:len(s)-1:], tmpl)
+ self.assertEqual(res[len(s)-2:-1:-1], tmpl[::-1])
+ self.assertEqual(res[len(s)-2:5:-7], tmpl[:5:-7])
+ dll.my_free(res)
################################################################
diff --git a/Lib/ctypes/test/test_strings.py b/Lib/ctypes/test/test_strings.py
index c837d82..4b58e7c 100644
--- a/Lib/ctypes/test/test_strings.py
+++ b/Lib/ctypes/test/test_strings.py
@@ -1,5 +1,6 @@
import unittest
from ctypes import *
+from ctypes.test import need_symbol
from test import test_support
class StringArrayTestCase(unittest.TestCase):
@@ -60,29 +61,26 @@ class StringArrayTestCase(unittest.TestCase):
## print BUF.from_param(c_char_p("python"))
## print BUF.from_param(BUF(*"pyth"))
-try:
- c_wchar
-except NameError:
- pass
-else:
- class WStringArrayTestCase(unittest.TestCase):
- def test(self):
- BUF = c_wchar * 4
+@need_symbol('c_wchar')
+class WStringArrayTestCase(unittest.TestCase):
+ def test(self):
+ BUF = c_wchar * 4
- buf = BUF(u"a", u"b", u"c")
- self.assertEqual(buf.value, u"abc")
+ buf = BUF(u"a", u"b", u"c")
+ self.assertEqual(buf.value, u"abc")
- buf.value = u"ABCD"
- self.assertEqual(buf.value, u"ABCD")
+ buf.value = u"ABCD"
+ self.assertEqual(buf.value, u"ABCD")
- buf.value = u"x"
- self.assertEqual(buf.value, u"x")
+ buf.value = u"x"
+ self.assertEqual(buf.value, u"x")
- buf[1] = u"Z"
- self.assertEqual(buf.value, u"xZCD")
+ buf[1] = u"Z"
+ self.assertEqual(buf.value, u"xZCD")
class StringTestCase(unittest.TestCase):
- def XX_test_basic_strings(self):
+ @unittest.skip('test disabled')
+ def test_basic_strings(self):
cs = c_string("abcdef")
# Cannot call len on a c_string any longer
@@ -108,7 +106,8 @@ class StringTestCase(unittest.TestCase):
self.assertRaises(TypeError, c_string, u"123")
- def XX_test_sized_strings(self):
+ @unittest.skip('test disabled')
+ def test_sized_strings(self):
# New in releases later than 0.4.0:
self.assertRaises(TypeError, c_string, None)
@@ -125,7 +124,8 @@ class StringTestCase(unittest.TestCase):
self.assertEqual(c_string(2).raw[-1], "\000")
self.assertEqual(len(c_string(2).raw), 2)
- def XX_test_initialized_strings(self):
+ @unittest.skip('test disabled')
+ def test_initialized_strings(self):
self.assertEqual(c_string("ab", 4).raw[:2], "ab")
self.assertEqual(c_string("ab", 4).raw[:2:], "ab")
@@ -134,7 +134,8 @@ class StringTestCase(unittest.TestCase):
self.assertEqual(c_string("ab", 4).raw[-1], "\000")
self.assertEqual(c_string("ab", 2).raw, "a\000")
- def XX_test_toolong(self):
+ @unittest.skip('test disabled')
+ def test_toolong(self):
cs = c_string("abcdef")
# Much too long string:
self.assertRaises(ValueError, setattr, cs, "value", "123456789012345")
@@ -142,54 +143,53 @@ class StringTestCase(unittest.TestCase):
# One char too long values:
self.assertRaises(ValueError, setattr, cs, "value", "1234567")
-## def test_perf(self):
-## check_perf()
+ @unittest.skip('test disabled')
+ def test_perf(self):
+ check_perf()
-try:
- c_wchar
-except NameError:
- pass
-else:
- class WStringTestCase(unittest.TestCase):
- def test_wchar(self):
- c_wchar(u"x")
- repr(byref(c_wchar(u"x")))
- c_wchar("x")
+@need_symbol('c_wchar')
+class WStringTestCase(unittest.TestCase):
+ def test_wchar(self):
+ c_wchar(u"x")
+ repr(byref(c_wchar(u"x")))
+ c_wchar("x")
- def X_test_basic_wstrings(self):
- cs = c_wstring(u"abcdef")
+ @unittest.skip('test disabled')
+ def test_basic_wstrings(self):
+ cs = c_wstring(u"abcdef")
- # XXX This behaviour is about to change:
- # len returns the size of the internal buffer in bytes.
- # This includes the terminating NUL character.
- self.assertEqual(sizeof(cs), 14)
+ # XXX This behaviour is about to change:
+ # len returns the size of the internal buffer in bytes.
+ # This includes the terminating NUL character.
+ self.assertEqual(sizeof(cs), 14)
- # The value property is the string up to the first terminating NUL.
- self.assertEqual(cs.value, u"abcdef")
- self.assertEqual(c_wstring(u"abc\000def").value, u"abc")
+ # The value property is the string up to the first terminating NUL.
+ self.assertEqual(cs.value, u"abcdef")
+ self.assertEqual(c_wstring(u"abc\000def").value, u"abc")
- self.assertEqual(c_wstring(u"abc\000def").value, u"abc")
+ self.assertEqual(c_wstring(u"abc\000def").value, u"abc")
- # The raw property is the total buffer contents:
- self.assertEqual(cs.raw, u"abcdef\000")
- self.assertEqual(c_wstring(u"abc\000def").raw, u"abc\000def\000")
+ # The raw property is the total buffer contents:
+ self.assertEqual(cs.raw, u"abcdef\000")
+ self.assertEqual(c_wstring(u"abc\000def").raw, u"abc\000def\000")
- # We can change the value:
- cs.value = u"ab"
- self.assertEqual(cs.value, u"ab")
- self.assertEqual(cs.raw, u"ab\000\000\000\000\000")
+ # We can change the value:
+ cs.value = u"ab"
+ self.assertEqual(cs.value, u"ab")
+ self.assertEqual(cs.raw, u"ab\000\000\000\000\000")
- self.assertRaises(TypeError, c_wstring, "123")
- self.assertRaises(ValueError, c_wstring, 0)
+ self.assertRaises(TypeError, c_wstring, "123")
+ self.assertRaises(ValueError, c_wstring, 0)
- def X_test_toolong(self):
- cs = c_wstring(u"abcdef")
- # Much too long string:
- self.assertRaises(ValueError, setattr, cs, "value", u"123456789012345")
+ @unittest.skip('test disabled')
+ def test_toolong(self):
+ cs = c_wstring(u"abcdef")
+ # Much too long string:
+ self.assertRaises(ValueError, setattr, cs, "value", u"123456789012345")
- # One char too long values:
- self.assertRaises(ValueError, setattr, cs, "value", u"1234567")
+ # One char too long values:
+ self.assertRaises(ValueError, setattr, cs, "value", u"1234567")
def run_test(rep, msg, func, arg):
diff --git a/Lib/ctypes/test/test_structures.py b/Lib/ctypes/test/test_structures.py
index ba7d8c0..2fa42bd 100644
--- a/Lib/ctypes/test/test_structures.py
+++ b/Lib/ctypes/test/test_structures.py
@@ -1,5 +1,6 @@
import unittest
from ctypes import *
+from ctypes.test import need_symbol
from struct import calcsize
import _testcapi
@@ -291,12 +292,8 @@ class StructureTestCase(unittest.TestCase):
self.assertEqual(p.phone.number, "5678")
self.assertEqual(p.age, 5)
+ @need_symbol('c_wchar')
def test_structures_with_wchar(self):
- try:
- c_wchar
- except NameError:
- return # no unicode
-
class PersonW(Structure):
_fields_ = [("name", c_wchar * 12),
("age", c_int)]
@@ -360,14 +357,14 @@ class StructureTestCase(unittest.TestCase):
except Exception, detail:
return detail.__class__, str(detail)
-
-## def test_subclass_creation(self):
-## meta = type(Structure)
-## # same as 'class X(Structure): pass'
-## # fails, since we need either a _fields_ or a _abstract_ attribute
-## cls, msg = self.get_except(meta, "X", (Structure,), {})
-## self.assertEqual((cls, msg),
-## (AttributeError, "class must define a '_fields_' attribute"))
+ @unittest.skip('test disabled')
+ def test_subclass_creation(self):
+ meta = type(Structure)
+ # same as 'class X(Structure): pass'
+ # fails, since we need either a _fields_ or a _abstract_ attribute
+ cls, msg = self.get_except(meta, "X", (Structure,), {})
+ self.assertEqual((cls, msg),
+ (AttributeError, "class must define a '_fields_' attribute"))
def test_abstract_class(self):
class X(Structure):
diff --git a/Lib/ctypes/test/test_unicode.py b/Lib/ctypes/test/test_unicode.py
index 6557479..1da5a25 100644
--- a/Lib/ctypes/test/test_unicode.py
+++ b/Lib/ctypes/test/test_unicode.py
@@ -1,129 +1,138 @@
# coding: latin-1
import unittest
import ctypes
-
-try:
- ctypes.c_wchar
-except AttributeError:
- pass
-else:
- import _ctypes_test
- dll = ctypes.CDLL(_ctypes_test.__file__)
- wcslen = dll.my_wcslen
- wcslen.argtypes = [ctypes.c_wchar_p]
-
-
- class UnicodeTestCase(unittest.TestCase):
- def setUp(self):
- self.prev_conv_mode = ctypes.set_conversion_mode("ascii", "strict")
-
- def tearDown(self):
- ctypes.set_conversion_mode(*self.prev_conv_mode)
-
- def test_ascii_strict(self):
- ctypes.set_conversion_mode("ascii", "strict")
- # no conversions take place with unicode arguments
- self.assertEqual(wcslen(u"abc"), 3)
- self.assertEqual(wcslen(u"ab\u2070"), 3)
- # string args are converted
- self.assertEqual(wcslen("abc"), 3)
- self.assertRaises(ctypes.ArgumentError, wcslen, "abä")
-
- def test_ascii_replace(self):
- ctypes.set_conversion_mode("ascii", "replace")
- self.assertEqual(wcslen(u"abc"), 3)
- self.assertEqual(wcslen(u"ab\u2070"), 3)
- self.assertEqual(wcslen("abc"), 3)
- self.assertEqual(wcslen("abä"), 3)
-
- def test_ascii_ignore(self):
- ctypes.set_conversion_mode("ascii", "ignore")
- self.assertEqual(wcslen(u"abc"), 3)
- self.assertEqual(wcslen(u"ab\u2070"), 3)
- # ignore error mode skips non-ascii characters
- self.assertEqual(wcslen("abc"), 3)
- self.assertEqual(wcslen("äöüß"), 0)
-
- def test_latin1_strict(self):
- ctypes.set_conversion_mode("latin-1", "strict")
- self.assertEqual(wcslen(u"abc"), 3)
- self.assertEqual(wcslen(u"ab\u2070"), 3)
- self.assertEqual(wcslen("abc"), 3)
- self.assertEqual(wcslen("äöüß"), 4)
-
- def test_buffers(self):
- ctypes.set_conversion_mode("ascii", "strict")
- buf = ctypes.create_unicode_buffer("abc")
- self.assertEqual(len(buf), 3+1)
-
- ctypes.set_conversion_mode("ascii", "replace")
- buf = ctypes.create_unicode_buffer("abäöü")
- self.assertEqual(buf[:], u"ab\uFFFD\uFFFD\uFFFD\0")
- self.assertEqual(buf[::], u"ab\uFFFD\uFFFD\uFFFD\0")
- self.assertEqual(buf[::-1], u"\0\uFFFD\uFFFD\uFFFDba")
- self.assertEqual(buf[::2], u"a\uFFFD\uFFFD")
- self.assertEqual(buf[6:5:-1], u"")
-
- ctypes.set_conversion_mode("ascii", "ignore")
- buf = ctypes.create_unicode_buffer("abäöü")
- # is that correct? not sure. But with 'ignore', you get what you pay for..
- self.assertEqual(buf[:], u"ab\0\0\0\0")
- self.assertEqual(buf[::], u"ab\0\0\0\0")
- self.assertEqual(buf[::-1], u"\0\0\0\0ba")
- self.assertEqual(buf[::2], u"a\0\0")
- self.assertEqual(buf[6:5:-1], u"")
-
- import _ctypes_test
- func = ctypes.CDLL(_ctypes_test.__file__)._testfunc_p_p
-
- class StringTestCase(UnicodeTestCase):
- def setUp(self):
- self.prev_conv_mode = ctypes.set_conversion_mode("ascii", "strict")
- func.argtypes = [ctypes.c_char_p]
- func.restype = ctypes.c_char_p
-
- def tearDown(self):
- ctypes.set_conversion_mode(*self.prev_conv_mode)
- func.argtypes = None
- func.restype = ctypes.c_int
-
- def test_ascii_replace(self):
- ctypes.set_conversion_mode("ascii", "strict")
- self.assertEqual(func("abc"), "abc")
- self.assertEqual(func(u"abc"), "abc")
- self.assertRaises(ctypes.ArgumentError, func, u"abä")
-
- def test_ascii_ignore(self):
- ctypes.set_conversion_mode("ascii", "ignore")
- self.assertEqual(func("abc"), "abc")
- self.assertEqual(func(u"abc"), "abc")
- self.assertEqual(func(u"äöüß"), "")
-
- def test_ascii_replace(self):
- ctypes.set_conversion_mode("ascii", "replace")
- self.assertEqual(func("abc"), "abc")
- self.assertEqual(func(u"abc"), "abc")
- self.assertEqual(func(u"äöüß"), "????")
-
- def test_buffers(self):
- ctypes.set_conversion_mode("ascii", "strict")
- buf = ctypes.create_string_buffer(u"abc")
- self.assertEqual(len(buf), 3+1)
-
- ctypes.set_conversion_mode("ascii", "replace")
- buf = ctypes.create_string_buffer(u"abäöü")
- self.assertEqual(buf[:], "ab???\0")
- self.assertEqual(buf[::], "ab???\0")
- self.assertEqual(buf[::-1], "\0???ba")
- self.assertEqual(buf[::2], "a??")
- self.assertEqual(buf[6:5:-1], "")
-
- ctypes.set_conversion_mode("ascii", "ignore")
- buf = ctypes.create_string_buffer(u"abäöü")
- # is that correct? not sure. But with 'ignore', you get what you pay for..
- self.assertEqual(buf[:], "ab\0\0\0\0")
- self.assertEqual(buf[::], "ab\0\0\0\0")
- self.assertEqual(buf[::-1], "\0\0\0\0ba")
+from ctypes.test import need_symbol
+import _ctypes_test
+
+@need_symbol('c_wchar')
+class UnicodeTestCase(unittest.TestCase):
+ @classmethod
+ def setUpClass(cls):
+ dll = ctypes.CDLL(_ctypes_test.__file__)
+ cls.wcslen = dll.my_wcslen
+ cls.wcslen.argtypes = [ctypes.c_wchar_p]
+ def setUp(self):
+ self.prev_conv_mode = ctypes.set_conversion_mode("ascii", "strict")
+
+ def tearDown(self):
+ ctypes.set_conversion_mode(*self.prev_conv_mode)
+
+ def test_ascii_strict(self):
+ wcslen = self.wcslen
+ ctypes.set_conversion_mode("ascii", "strict")
+ # no conversions take place with unicode arguments
+ self.assertEqual(wcslen(u"abc"), 3)
+ self.assertEqual(wcslen(u"ab\u2070"), 3)
+ # string args are converted
+ self.assertEqual(wcslen("abc"), 3)
+ self.assertRaises(ctypes.ArgumentError, wcslen, "abä")
+
+ def test_ascii_replace(self):
+ wcslen = self.wcslen
+ ctypes.set_conversion_mode("ascii", "replace")
+ self.assertEqual(wcslen(u"abc"), 3)
+ self.assertEqual(wcslen(u"ab\u2070"), 3)
+ self.assertEqual(wcslen("abc"), 3)
+ self.assertEqual(wcslen("abä"), 3)
+
+ def test_ascii_ignore(self):
+ wcslen = self.wcslen
+ ctypes.set_conversion_mode("ascii", "ignore")
+ self.assertEqual(wcslen(u"abc"), 3)
+ self.assertEqual(wcslen(u"ab\u2070"), 3)
+ # ignore error mode skips non-ascii characters
+ self.assertEqual(wcslen("abc"), 3)
+ self.assertEqual(wcslen("äöüß"), 0)
+
+ def test_latin1_strict(self):
+ wcslen = self.wcslen
+ ctypes.set_conversion_mode("latin-1", "strict")
+ self.assertEqual(wcslen(u"abc"), 3)
+ self.assertEqual(wcslen(u"ab\u2070"), 3)
+ self.assertEqual(wcslen("abc"), 3)
+ self.assertEqual(wcslen("äöüß"), 4)
+
+ def test_buffers(self):
+ ctypes.set_conversion_mode("ascii", "strict")
+ buf = ctypes.create_unicode_buffer("abc")
+ self.assertEqual(len(buf), 3+1)
+
+ ctypes.set_conversion_mode("ascii", "replace")
+ buf = ctypes.create_unicode_buffer("abäöü")
+ self.assertEqual(buf[:], u"ab\uFFFD\uFFFD\uFFFD\0")
+ self.assertEqual(buf[::], u"ab\uFFFD\uFFFD\uFFFD\0")
+ self.assertEqual(buf[::-1], u"\0\uFFFD\uFFFD\uFFFDba")
+ self.assertEqual(buf[::2], u"a\uFFFD\uFFFD")
+ self.assertEqual(buf[6:5:-1], u"")
+
+ ctypes.set_conversion_mode("ascii", "ignore")
+ buf = ctypes.create_unicode_buffer("abäöü")
+ # is that correct? not sure. But with 'ignore', you get what you pay for..
+ self.assertEqual(buf[:], u"ab\0\0\0\0")
+ self.assertEqual(buf[::], u"ab\0\0\0\0")
+ self.assertEqual(buf[::-1], u"\0\0\0\0ba")
+ self.assertEqual(buf[::2], u"a\0\0")
+ self.assertEqual(buf[6:5:-1], u"")
+
+@need_symbol('c_wchar')
+class StringTestCase(UnicodeTestCase):
+ @classmethod
+ def setUpClass(cls):
+ super(StringTestCase, cls).setUpClass()
+ cls.func = ctypes.CDLL(_ctypes_test.__file__)._testfunc_p_p
+
+ def setUp(self):
+ func = self.func
+ self.prev_conv_mode = ctypes.set_conversion_mode("ascii", "strict")
+ func.argtypes = [ctypes.c_char_p]
+ func.restype = ctypes.c_char_p
+
+ def tearDown(self):
+ func = self.func
+ ctypes.set_conversion_mode(*self.prev_conv_mode)
+ func.argtypes = None
+ func.restype = ctypes.c_int
+
+ def test_ascii_replace(self):
+ func = self.func
+ ctypes.set_conversion_mode("ascii", "strict")
+ self.assertEqual(func("abc"), "abc")
+ self.assertEqual(func(u"abc"), "abc")
+ self.assertRaises(ctypes.ArgumentError, func, u"abä")
+
+ def test_ascii_ignore(self):
+ func = self.func
+ ctypes.set_conversion_mode("ascii", "ignore")
+ self.assertEqual(func("abc"), "abc")
+ self.assertEqual(func(u"abc"), "abc")
+ self.assertEqual(func(u"äöüß"), "")
+
+ def test_ascii_replace(self):
+ func = self.func
+ ctypes.set_conversion_mode("ascii", "replace")
+ self.assertEqual(func("abc"), "abc")
+ self.assertEqual(func(u"abc"), "abc")
+ self.assertEqual(func(u"äöüß"), "????")
+
+ def test_buffers(self):
+ ctypes.set_conversion_mode("ascii", "strict")
+ buf = ctypes.create_string_buffer(u"abc")
+ self.assertEqual(len(buf), 3+1)
+
+ ctypes.set_conversion_mode("ascii", "replace")
+ buf = ctypes.create_string_buffer(u"abäöü")
+ self.assertEqual(buf[:], "ab???\0")
+ self.assertEqual(buf[::], "ab???\0")
+ self.assertEqual(buf[::-1], "\0???ba")
+ self.assertEqual(buf[::2], "a??")
+ self.assertEqual(buf[6:5:-1], "")
+
+ ctypes.set_conversion_mode("ascii", "ignore")
+ buf = ctypes.create_string_buffer(u"abäöü")
+ # is that correct? not sure. But with 'ignore', you get what you pay for..
+ self.assertEqual(buf[:], "ab\0\0\0\0")
+ self.assertEqual(buf[::], "ab\0\0\0\0")
+ self.assertEqual(buf[::-1], "\0\0\0\0ba")
if __name__ == '__main__':
unittest.main()
diff --git a/Lib/ctypes/test/test_values.py b/Lib/ctypes/test/test_values.py
index 4cbfd4b..14d69fe 100644
--- a/Lib/ctypes/test/test_values.py
+++ b/Lib/ctypes/test/test_values.py
@@ -3,6 +3,7 @@ A testcase which accesses *values* in a dll.
"""
import unittest
+import sys
from ctypes import *
import _ctypes_test
@@ -21,62 +22,63 @@ class ValuesTestCase(unittest.TestCase):
ctdll = CDLL(_ctypes_test.__file__)
self.assertRaises(ValueError, c_int.in_dll, ctdll, "Undefined_Symbol")
- class Win_ValuesTestCase(unittest.TestCase):
- """This test only works when python itself is a dll/shared library"""
+@unittest.skipUnless(sys.platform == 'win32', 'Windows-specific test')
+class Win_ValuesTestCase(unittest.TestCase):
+ """This test only works when python itself is a dll/shared library"""
- def test_optimizeflag(self):
- # This test accesses the Py_OptimizeFlag intger, which is
- # exported by the Python dll.
+ def test_optimizeflag(self):
+ # This test accesses the Py_OptimizeFlag intger, which is
+ # exported by the Python dll.
- # It's value is set depending on the -O and -OO flags:
- # if not given, it is 0 and __debug__ is 1.
- # If -O is given, the flag is 1, for -OO it is 2.
- # docstrings are also removed in the latter case.
- opt = c_int.in_dll(pydll, "Py_OptimizeFlag").value
- if __debug__:
- self.assertEqual(opt, 0)
- elif ValuesTestCase.__doc__ is not None:
- self.assertEqual(opt, 1)
- else:
- self.assertEqual(opt, 2)
+ # It's value is set depending on the -O and -OO flags:
+ # if not given, it is 0 and __debug__ is 1.
+ # If -O is given, the flag is 1, for -OO it is 2.
+ # docstrings are also removed in the latter case.
+ opt = c_int.in_dll(pythonapi, "Py_OptimizeFlag").value
+ if __debug__:
+ self.assertEqual(opt, 0)
+ elif ValuesTestCase.__doc__ is not None:
+ self.assertEqual(opt, 1)
+ else:
+ self.assertEqual(opt, 2)
- def test_frozentable(self):
- # Python exports a PyImport_FrozenModules symbol. This is a
- # pointer to an array of struct _frozen entries. The end of the
- # array is marked by an entry containing a NULL name and zero
- # size.
+ def test_frozentable(self):
+ # Python exports a PyImport_FrozenModules symbol. This is a
+ # pointer to an array of struct _frozen entries. The end of the
+ # array is marked by an entry containing a NULL name and zero
+ # size.
- # In standard Python, this table contains a __hello__
- # module, and a __phello__ package containing a spam
- # module.
- class struct_frozen(Structure):
- _fields_ = [("name", c_char_p),
- ("code", POINTER(c_ubyte)),
- ("size", c_int)]
- FrozenTable = POINTER(struct_frozen)
+ # In standard Python, this table contains a __hello__
+ # module, and a __phello__ package containing a spam
+ # module.
+ class struct_frozen(Structure):
+ _fields_ = [("name", c_char_p),
+ ("code", POINTER(c_ubyte)),
+ ("size", c_int)]
+ FrozenTable = POINTER(struct_frozen)
- ft = FrozenTable.in_dll(pydll, "PyImport_FrozenModules")
- # ft is a pointer to the struct_frozen entries:
- items = []
- for entry in ft:
- # This is dangerous. We *can* iterate over a pointer, but
- # the loop will not terminate (maybe with an access
- # violation;-) because the pointer instance has no size.
- if entry.name is None:
- break
- items.append((entry.name, entry.size))
- import sys
- if sys.version_info[:2] >= (2, 3):
- expected = [("__hello__", 104), ("__phello__", -104), ("__phello__.spam", 104)]
- else:
- expected = [("__hello__", 100), ("__phello__", -100), ("__phello__.spam", 100)]
- self.assertEqual(items, expected)
+ ft = FrozenTable.in_dll(pythonapi, "PyImport_FrozenModules")
+ # ft is a pointer to the struct_frozen entries:
+ items = []
+ for entry in ft:
+ # This is dangerous. We *can* iterate over a pointer, but
+ # the loop will not terminate (maybe with an access
+ # violation;-) because the pointer instance has no size.
+ if entry.name is None:
+ break
+ items.append((entry.name, entry.size))
- from ctypes import _pointer_type_cache
- del _pointer_type_cache[struct_frozen]
+ expected = [("__hello__", 104),
+ ("__phello__", -104),
+ ("__phello__.spam", 104)]
+ self.assertEqual(items, expected)
- def test_undefined(self):
- self.assertRaises(ValueError, c_int.in_dll, pydll, "Undefined_Symbol")
+ from ctypes import _pointer_type_cache
+ del _pointer_type_cache[struct_frozen]
+
+ def test_undefined(self):
+ self.assertRaises(ValueError, c_int.in_dll, pythonapi,
+ "Undefined_Symbol")
if __name__ == '__main__':
unittest.main()
diff --git a/Lib/ctypes/test/test_win32.py b/Lib/ctypes/test/test_win32.py
index 77e6fea..8093efc 100644
--- a/Lib/ctypes/test/test_win32.py
+++ b/Lib/ctypes/test/test_win32.py
@@ -1,77 +1,79 @@
# Windows specific tests
from ctypes import *
-from ctypes.test import is_resource_enabled
+from ctypes.test import requires
import unittest, sys
from test import test_support as support
import _ctypes_test
-if sys.platform == "win32" and sizeof(c_void_p) == sizeof(c_int):
- # Only windows 32-bit has different calling conventions.
-
- class WindowsTestCase(unittest.TestCase):
- def test_callconv_1(self):
- # Testing stdcall function
-
- IsWindow = windll.user32.IsWindow
- # ValueError: Procedure probably called with not enough arguments (4 bytes missing)
- self.assertRaises(ValueError, IsWindow)
-
- # This one should succeed...
- self.assertEqual(0, IsWindow(0))
-
- # ValueError: Procedure probably called with too many arguments (8 bytes in excess)
- self.assertRaises(ValueError, IsWindow, 0, 0, 0)
-
- def test_callconv_2(self):
- # Calling stdcall function as cdecl
-
- IsWindow = cdll.user32.IsWindow
-
- # ValueError: Procedure called with not enough arguments (4 bytes missing)
- # or wrong calling convention
- self.assertRaises(ValueError, IsWindow, None)
-
-if sys.platform == "win32":
- class FunctionCallTestCase(unittest.TestCase):
-
- if is_resource_enabled("SEH"):
- def test_SEH(self):
- # Call functions with invalid arguments, and make sure
- # that access violations are trapped and raise an
- # exception.
- self.assertRaises(WindowsError, windll.kernel32.GetModuleHandleA, 32)
-
- def test_noargs(self):
- # This is a special case on win32 x64
- windll.user32.GetDesktopWindow()
-
- class TestWintypes(unittest.TestCase):
- def test_HWND(self):
- from ctypes import wintypes
- self.assertEqual(sizeof(wintypes.HWND), sizeof(c_void_p))
-
- def test_PARAM(self):
- from ctypes import wintypes
- self.assertEqual(sizeof(wintypes.WPARAM),
- sizeof(c_void_p))
- self.assertEqual(sizeof(wintypes.LPARAM),
- sizeof(c_void_p))
-
- def test_COMError(self):
- from _ctypes import COMError
- if support.HAVE_DOCSTRINGS:
- self.assertEqual(COMError.__doc__,
- "Raised when a COM method call failed.")
-
- ex = COMError(-1, "text", ("details",))
- self.assertEqual(ex.hresult, -1)
- self.assertEqual(ex.text, "text")
- self.assertEqual(ex.details, ("details",))
+# Only windows 32-bit has different calling conventions.
+@unittest.skipUnless(sys.platform == "win32", 'Windows-specific test')
+@unittest.skipUnless(sizeof(c_void_p) == sizeof(c_int),
+ "sizeof c_void_p and c_int differ")
+class WindowsTestCase(unittest.TestCase):
+ def test_callconv_1(self):
+ # Testing stdcall function
+
+ IsWindow = windll.user32.IsWindow
+ # ValueError: Procedure probably called with not enough arguments
+ # (4 bytes missing)
+ self.assertRaises(ValueError, IsWindow)
+
+ # This one should succeed...
+ self.assertEqual(0, IsWindow(0))
+
+ # ValueError: Procedure probably called with too many arguments
+ # (8 bytes in excess)
+ self.assertRaises(ValueError, IsWindow, 0, 0, 0)
+
+ def test_callconv_2(self):
+ # Calling stdcall function as cdecl
+
+ IsWindow = cdll.user32.IsWindow
+
+ # ValueError: Procedure called with not enough arguments
+ # (4 bytes missing) or wrong calling convention
+ self.assertRaises(ValueError, IsWindow, None)
+
+@unittest.skipUnless(sys.platform == "win32", 'Windows-specific test')
+class FunctionCallTestCase(unittest.TestCase):
+ @requires("SEH")
+ def test_SEH(self):
+ # Call functions with invalid arguments, and make sure
+ # that access violations are trapped and raise an
+ # exception.
+ self.assertRaises(WindowsError, windll.kernel32.GetModuleHandleA, 32)
+
+ def test_noargs(self):
+ # This is a special case on win32 x64
+ windll.user32.GetDesktopWindow()
+
+@unittest.skipUnless(sys.platform == "win32", 'Windows-specific test')
+class TestWintypes(unittest.TestCase):
+ def test_HWND(self):
+ from ctypes import wintypes
+ self.assertEqual(sizeof(wintypes.HWND), sizeof(c_void_p))
+
+ def test_PARAM(self):
+ from ctypes import wintypes
+ self.assertEqual(sizeof(wintypes.WPARAM),
+ sizeof(c_void_p))
+ self.assertEqual(sizeof(wintypes.LPARAM),
+ sizeof(c_void_p))
+
+ def test_COMError(self):
+ from _ctypes import COMError
+ if support.HAVE_DOCSTRINGS:
+ self.assertEqual(COMError.__doc__,
+ "Raised when a COM method call failed.")
+
+ ex = COMError(-1, "text", ("details",))
+ self.assertEqual(ex.hresult, -1)
+ self.assertEqual(ex.text, "text")
+ self.assertEqual(ex.details, ("details",))
class Structures(unittest.TestCase):
-
def test_struct_by_value(self):
class POINT(Structure):
_fields_ = [("x", c_long),
diff --git a/Lib/ctypes/test/test_wintypes.py b/Lib/ctypes/test/test_wintypes.py
index 806fcce..71442df 100644
--- a/Lib/ctypes/test/test_wintypes.py
+++ b/Lib/ctypes/test/test_wintypes.py
@@ -1,14 +1,12 @@
import sys
import unittest
-if not sys.platform.startswith('win'):
- raise unittest.SkipTest('Windows-only test')
-
from ctypes import *
-from ctypes import wintypes
+@unittest.skipUnless(sys.platform.startswith('win'), 'Windows-only test')
class WinTypesTest(unittest.TestCase):
def test_variant_bool(self):
+ from ctypes import wintypes
# reads 16-bits from memory, anything non-zero is True
for true_value in (1, 32767, 32768, 65535, 65537):
true = POINTER(c_int16)(c_int16(true_value))