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 succeeed... self.failUnlessEqual(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 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)
class TestWindows(BaseCTypesTestChecker): def test_callconv_1(self): # Testing stdcall function IsWindow = windll.user32.IsWindow # ValueError: Procedure probably called with not enough arguments (4 bytes missing) with pytest.raises(ValueError): IsWindow() # This one should succeeed... assert IsWindow(0) == 0 # ValueError: Procedure probably called with too many arguments (8 bytes in excess) with pytest.raises(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 with pytest.raises(ValueError): IsWindow(None) 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. with pytest.raises(WindowsError): windll.kernel32.GetModuleHandleA(32)
class FunctionCallTestCase(unittest.TestCase): if is_resource_enabled('SEH'): def test_SEH(self): self.assertRaises(WindowsError, windll.kernel32.GetModuleHandleA, 32) def test_noargs(self): windll.user32.GetDesktopWindow()
class PythonAPITestCase(unittest.TestCase): def test_PyString_FromStringAndSize(self): PyString_FromStringAndSize = pythonapi.PyString_FromStringAndSize PyString_FromStringAndSize.restype = py_object PyString_FromStringAndSize.argtypes = (c_char_p, c_py_ssize_t) self.assertEqual(PyString_FromStringAndSize('abcdefghi', 3), 'abc') def test_PyString_FromString(self): pythonapi.PyString_FromString.restype = py_object pythonapi.PyString_FromString.argtypes = (c_char_p,) s = 'abc' refcnt = grc(s) pyob = pythonapi.PyString_FromString(s) self.assertEqual(grc(s), refcnt) self.assertEqual(s, pyob) del pyob self.assertEqual(grc(s), refcnt) if is_resource_enabled('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) 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) def test_PyObj_FromPtr(self): s = 'abc def ghi jkl' ref = grc(s) pyobj = PyObj_FromPtr(id(s)) self.assertIs(s, pyobj) self.assertEqual(grc(s), ref + 1) del pyobj self.assertEqual(grc(s), ref) def test_PyOS_snprintf(self): PyOS_snprintf = pythonapi.PyOS_snprintf PyOS_snprintf.argtypes = (POINTER(c_char), c_size_t, c_char_p) buf = c_buffer(256) PyOS_snprintf(buf, sizeof(buf), 'Hello from %s', 'ctypes') self.assertEqual(buf.value, 'Hello from ctypes') PyOS_snprintf(buf, sizeof(buf), 'Hello from %s', 'ctypes', 1, 2, 3) self.assertEqual(buf.value, 'Hello from ctypes') self.assertRaises(TypeError, PyOS_snprintf, buf) def test_pyobject_repr(self): self.assertEqual(repr(py_object()), 'py_object(<NULL>)') self.assertEqual(repr(py_object(42)), 'py_object(42)') self.assertEqual(repr(py_object(object)), 'py_object(%r)' % object)
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()
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
def test_load_library(self): 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
def test_load_library(self): if not universal_crt: 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
from ctypes import * import unittest, sys from test import support from ctypes.test import is_resource_enabled, xfail ################################################################ # This section should be moved into ctypes\__init__.py, when it's ready. from _ctypes import PyObj_FromPtr ################################################################ if is_resource_enabled("refcount"): from sys import getrefcount as grc if sys.version_info > (2, 4): c_py_ssize_t = c_size_t else: c_py_ssize_t = c_int class PythonAPITestCase(unittest.TestCase): @xfail def test_PyBytes_FromStringAndSize(self): PyBytes_FromStringAndSize = pythonapi.PyBytes_FromStringAndSize PyBytes_FromStringAndSize.restype = py_object PyBytes_FromStringAndSize.argtypes = c_char_p, c_py_ssize_t self.assertEqual(PyBytes_FromStringAndSize(b"abcdefghi", 3), b"abc") @support.refcount_test
import sys, unittest import os, StringIO from ctypes.util import find_library from ctypes.test import is_resource_enabled libc_name = None if os.name == "nt": libc_name = "msvcrt" elif os.name == "ce": libc_name = "coredll" elif sys.platform == "cygwin": libc_name = "cygwin1.dll" else: libc_name = find_library("c") if True or is_resource_enabled("printing"): print >> sys.stderr, "\tfind_library('c') -> ", find_library('c') print >> sys.stderr, "\tfind_library('m') -> ", find_library('m') 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) if libc_name is not None and os.path.basename(libc_name) == "libc.so.6": def test_load_version(self):
libc_name = None if os.name == "nt": libc_name = find_library("c") if support.universal_crt: # CRT is no longer directly loadable. See issue23606 for the # discussion about alternative approaches. libc_name = None elif os.name == "ce": libc_name = "coredll" elif sys.platform == "cygwin": libc_name = "cygwin1.dll" else: libc_name = find_library("c") if is_resource_enabled("printing"): print "libc_name is", libc_name class LoaderTest(unittest.TestCase): unknowndll = "xxrandomnamexx" @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) @support.requires_unicode @unittest.skipUnless(libc_name is not None, 'could not find libc')
import sys, unittest import os from ctypes.util import find_library from ctypes.test import is_resource_enabled libc_name = None if os.name == "nt": libc_name = find_library("c") elif os.name == "ce": libc_name = "coredll" elif sys.platform == "cygwin": libc_name = "cygwin1.dll" else: libc_name = find_library("c") if is_resource_enabled("printing"): print "libc_name is", libc_name 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) if libc_name is not None and os.path.basename(libc_name) == "libc.so.6":
import sys, unittest import os, StringIO from ctypes.util import find_library from ctypes.test import is_resource_enabled libc_name = None if os.name == "nt": libc_name = "msvcrt" elif os.name == "ce": libc_name = "coredll" elif sys.platform == "cygwin": libc_name = "cygwin1.dll" else: libc_name = find_library("c") if True or is_resource_enabled("printing"): print >> sys.stderr, "\tfind_library('c') -> ", find_library('c') print >> sys.stderr, "\tfind_library('m') -> ", find_library('m') 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)
class PythonAPITestCase(unittest.TestCase): def test_PyString_FromStringAndSize(self): PyString_FromStringAndSize = pythonapi.PyString_FromStringAndSize PyString_FromStringAndSize.restype = py_object PyString_FromStringAndSize.argtypes = c_char_p, c_py_ssize_t self.failUnlessEqual(PyString_FromStringAndSize("abcdefghi", 3), "abc") def test_PyString_FromString(self): pythonapi.PyString_FromString.restype = py_object pythonapi.PyString_FromString.argtypes = (c_char_p, ) s = "abc" refcnt = grc(s) pyob = pythonapi.PyString_FromString(s) self.failUnlessEqual(grc(s), refcnt) self.failUnlessEqual(s, pyob) del pyob self.failUnlessEqual(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.failUnlessEqual(pythonapi.PyInt_FromLong(42), 42) self.failUnlessEqual(grc(42), ref42) pythonapi.PyInt_AsLong.argtypes = (py_object, ) pythonapi.PyInt_AsLong.restype = c_long res = pythonapi.PyInt_AsLong(42) self.failUnlessEqual(grc(res), ref42 + 1) del res self.failUnlessEqual(grc(42), ref42) def test_PyObj_FromPtr(self): s = "abc def ghi jkl" ref = grc(s) # id(python-object) is the address pyobj = PyObj_FromPtr(id(s)) self.failUnless(s is pyobj) self.failUnlessEqual(grc(s), ref + 1) del pyobj self.failUnlessEqual(grc(s), ref) def test_PyOS_snprintf(self): PyOS_snprintf = pythonapi.PyOS_snprintf PyOS_snprintf.argtypes = POINTER(c_char), c_size_t, c_char_p buf = c_buffer(256) PyOS_snprintf(buf, sizeof(buf), "Hello from %s", "ctypes") self.failUnlessEqual(buf.value, "Hello from ctypes") PyOS_snprintf(buf, sizeof(buf), "Hello from %s", "ctypes", 1, 2, 3) self.failUnlessEqual(buf.value, "Hello from ctypes") # not enough arguments self.failUnlessRaises(TypeError, PyOS_snprintf, buf) def test_pyobject_repr(self): self.failUnlessEqual(repr(py_object()), "py_object(<NULL>)") self.failUnlessEqual(repr(py_object(42)), "py_object(42)") self.failUnlessEqual(repr(py_object(object)), "py_object(%r)" % object)
# Embedded file name: scripts/common/Lib/ctypes/test/test_loading.py from ctypes import * import sys, unittest import os from ctypes.util import find_library from ctypes.test import is_resource_enabled libc_name = None if os.name == 'nt': libc_name = find_library('c') elif os.name == 'ce': libc_name = 'coredll' elif sys.platform == 'cygwin': libc_name = 'cygwin1.dll' else: libc_name = find_library('c') if is_resource_enabled('printing'): print 'libc_name is', libc_name 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) 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')