1/* 2 * This module is used in test_exceptions 3 */ 4#include "Python.h" 5#include "pyobjc-api.h" 6 7#import <Foundation/Foundation.h> 8 9static NSString* addSomeUnicode(NSString* input) 10{ 11 return [NSString stringWithFormat:@"%@%C%C", input, (short)0x1234, (short)0x2049]; 12} 13 14@interface PyObjCTestExceptions : NSObject 15{ 16} 17-(void)raiseSimple; 18-(void)raiseSimpleWithInfo; 19-(void)raiseUnicodeName; 20-(void)raiseUnicodeReason; 21-(void)raiseUnicodeWithInfo; 22-(void)raiseAString; 23@end 24 25@implementation PyObjCTestExceptions 26 27-(void)raiseSimple 28{ 29 [NSException raise:@"SimpleException" format:@"hello world"]; 30} 31 32-(void)raiseSimpleWithInfo 33{ 34 [[NSException exceptionWithName:@"InfoException" 35 reason:@"Reason string" 36 userInfo:[NSDictionary dictionaryWithObjectsAndKeys: 37 @"value1", @"key1", 38 @"value2", @"key2", 39 NULL]] raise]; 40} 41 42-(void)raiseUnicodeName 43{ 44 [NSException 45 raise:addSomeUnicode(@"SimpleException") 46 format:@"hello world"]; 47} 48 49-(void)raiseUnicodeReason 50{ 51 [NSException 52 raise:@"SimpleException" 53 format:@"%@", addSomeUnicode(@"hello world")]; 54} 55 56-(void)raiseUnicodeWithInfo 57{ 58 [[NSException exceptionWithName:addSomeUnicode(@"InfoException") 59 reason:addSomeUnicode(@"Reason string") 60 userInfo:[NSDictionary dictionaryWithObjectsAndKeys: 61 addSomeUnicode(@"value1"), 62 addSomeUnicode(@"key1"), 63 addSomeUnicode(@"value2"), 64 addSomeUnicode(@"key2"), 65 NULL]] raise]; 66} 67 68-(void)raiseAString 69{ 70 @throw @"thrown string"; 71} 72 73@end 74 75 76static PyMethodDef mod_methods[] = { 77 { 0, 0, 0, 0 } 78}; 79 80#if PY_VERSION_HEX >= 0x03000000 81 82static struct PyModuleDef mod_module = { 83 PyModuleDef_HEAD_INIT, 84 "exceptions", 85 NULL, 86 0, 87 mod_methods, 88 NULL, 89 NULL, 90 NULL, 91 NULL 92}; 93 94#define INITERROR() return NULL 95#define INITDONE() return m 96 97PyObject* PyInit_exceptions(void); 98 99PyObject* 100PyInit_exceptions(void) 101 102#else 103 104#define INITERROR() return 105#define INITDONE() return 106 107void initexceptions(void); 108 109void 110initexceptions(void) 111#endif 112{ 113 PyObject* m; 114 115#if PY_VERSION_HEX >= 0x03000000 116 m = PyModule_Create(&mod_module); 117#else 118 m = Py_InitModule4("exceptions", mod_methods, 119 NULL, NULL, PYTHON_API_VERSION); 120#endif 121 if (!m) { 122 INITERROR(); 123 } 124 125 if (PyObjC_ImportAPI(m) < 0) { 126 INITERROR(); 127 } 128 if (PyModule_AddObject(m, "PyObjCTestExceptions", 129 PyObjCClass_New([PyObjCTestExceptions class])) < 0) { 130 INITERROR(); 131 } 132 INITDONE(); 133} 134