| 1 | n/a | from ctypes import * | 
|---|
| 2 | n/a | import unittest, sys | 
|---|
| 3 | n/a |  | 
|---|
| 4 | n/a | def callback_func(arg): | 
|---|
| 5 | n/a | 42 / arg | 
|---|
| 6 | n/a | raise ValueError(arg) | 
|---|
| 7 | n/a |  | 
|---|
| 8 | n/a | @unittest.skipUnless(sys.platform == "win32", 'Windows-specific test') | 
|---|
| 9 | n/a | class call_function_TestCase(unittest.TestCase): | 
|---|
| 10 | n/a | # _ctypes.call_function is deprecated and private, but used by | 
|---|
| 11 | n/a | # Gary Bishp's readline module.  If we have it, we must test it as well. | 
|---|
| 12 | n/a |  | 
|---|
| 13 | n/a | def test(self): | 
|---|
| 14 | n/a | from _ctypes import call_function | 
|---|
| 15 | n/a | windll.kernel32.LoadLibraryA.restype = c_void_p | 
|---|
| 16 | n/a | windll.kernel32.GetProcAddress.argtypes = c_void_p, c_char_p | 
|---|
| 17 | n/a | windll.kernel32.GetProcAddress.restype = c_void_p | 
|---|
| 18 | n/a |  | 
|---|
| 19 | n/a | hdll = windll.kernel32.LoadLibraryA(b"kernel32") | 
|---|
| 20 | n/a | funcaddr = windll.kernel32.GetProcAddress(hdll, b"GetModuleHandleA") | 
|---|
| 21 | n/a |  | 
|---|
| 22 | n/a | self.assertEqual(call_function(funcaddr, (None,)), | 
|---|
| 23 | n/a | windll.kernel32.GetModuleHandleA(None)) | 
|---|
| 24 | n/a |  | 
|---|
| 25 | n/a | class CallbackTracbackTestCase(unittest.TestCase): | 
|---|
| 26 | n/a | # When an exception is raised in a ctypes callback function, the C | 
|---|
| 27 | n/a | # code prints a traceback. | 
|---|
| 28 | n/a | # | 
|---|
| 29 | n/a | # This test makes sure the exception types *and* the exception | 
|---|
| 30 | n/a | # value is printed correctly. | 
|---|
| 31 | n/a | # | 
|---|
| 32 | n/a | # Changed in 0.9.3: No longer is '(in callback)' prepended to the | 
|---|
| 33 | n/a | # error message - instead an additional frame for the C code is | 
|---|
| 34 | n/a | # created, then a full traceback printed.  When SystemExit is | 
|---|
| 35 | n/a | # raised in a callback function, the interpreter exits. | 
|---|
| 36 | n/a |  | 
|---|
| 37 | n/a | def capture_stderr(self, func, *args, **kw): | 
|---|
| 38 | n/a | # helper - call function 'func', and return the captured stderr | 
|---|
| 39 | n/a | import io | 
|---|
| 40 | n/a | old_stderr = sys.stderr | 
|---|
| 41 | n/a | logger = sys.stderr = io.StringIO() | 
|---|
| 42 | n/a | try: | 
|---|
| 43 | n/a | func(*args, **kw) | 
|---|
| 44 | n/a | finally: | 
|---|
| 45 | n/a | sys.stderr = old_stderr | 
|---|
| 46 | n/a | return logger.getvalue() | 
|---|
| 47 | n/a |  | 
|---|
| 48 | n/a | def test_ValueError(self): | 
|---|
| 49 | n/a | cb = CFUNCTYPE(c_int, c_int)(callback_func) | 
|---|
| 50 | n/a | out = self.capture_stderr(cb, 42) | 
|---|
| 51 | n/a | self.assertEqual(out.splitlines()[-1], | 
|---|
| 52 | n/a | "ValueError: 42") | 
|---|
| 53 | n/a |  | 
|---|
| 54 | n/a | def test_IntegerDivisionError(self): | 
|---|
| 55 | n/a | cb = CFUNCTYPE(c_int, c_int)(callback_func) | 
|---|
| 56 | n/a | out = self.capture_stderr(cb, 0) | 
|---|
| 57 | n/a | self.assertEqual(out.splitlines()[-1][:19], | 
|---|
| 58 | n/a | "ZeroDivisionError: ") | 
|---|
| 59 | n/a |  | 
|---|
| 60 | n/a | def test_FloatDivisionError(self): | 
|---|
| 61 | n/a | cb = CFUNCTYPE(c_int, c_double)(callback_func) | 
|---|
| 62 | n/a | out = self.capture_stderr(cb, 0.0) | 
|---|
| 63 | n/a | self.assertEqual(out.splitlines()[-1][:19], | 
|---|
| 64 | n/a | "ZeroDivisionError: ") | 
|---|
| 65 | n/a |  | 
|---|
| 66 | n/a | def test_TypeErrorDivisionError(self): | 
|---|
| 67 | n/a | cb = CFUNCTYPE(c_int, c_char_p)(callback_func) | 
|---|
| 68 | n/a | out = self.capture_stderr(cb, b"spam") | 
|---|
| 69 | n/a | self.assertEqual(out.splitlines()[-1], | 
|---|
| 70 | n/a | "TypeError: " | 
|---|
| 71 | n/a | "unsupported operand type(s) for /: 'int' and 'bytes'") | 
|---|
| 72 | n/a |  | 
|---|
| 73 | n/a | if __name__ == '__main__': | 
|---|
| 74 | n/a | unittest.main() | 
|---|