1/* 2 * This module is used in the unittests for object initialize. 3 */ 4#include "Python.h" 5#include "pyobjc-api.h" 6 7#import <Foundation/Foundation.h> 8 9static int numUninitialized = 0; 10 11@interface OC_TestInitialize : NSObject 12{ 13 int isInitialized; 14} 15-(instancetype)init; 16-(instancetype)retain; 17-(void)release; 18-(instancetype)autorelease; 19-(int)isInitialized; 20+(int)numUninitialized; 21-(id)dummy; 22+(id)makeInstance; 23 24/* completely unrelated ... */ 25-(oneway void)onewayVoidMethod; 26 27@end 28 29@implementation OC_TestInitialize 30 31-(instancetype)init 32{ 33 self = [super init]; 34 if (!self) return self; 35 36 isInitialized = 1; 37 return self; 38} 39 40-(instancetype)retain 41{ 42 if (!isInitialized) { 43 numUninitialized ++; 44 } 45 return [super retain]; 46} 47 48-(void)release 49{ 50 if (!isInitialized) { 51 numUninitialized ++; 52 } 53 [super release]; 54} 55 56-(instancetype)autorelease 57{ 58 if (!isInitialized) { 59 numUninitialized ++; 60 } 61 return [super autorelease]; 62} 63 64-(int)isInitialized 65{ 66 return isInitialized; 67} 68 69+(int)numUninitialized 70{ 71 return numUninitialized; 72} 73 74-(id)dummy 75{ 76 return @"hello"; 77} 78 79+(id)makeInstance 80{ 81 return [[[self alloc] init] autorelease]; 82} 83 84-(oneway void)onewayVoidMethod 85{ 86 isInitialized=-1; 87} 88 89@end 90 91 92static PyMethodDef mod_methods[] = { 93 { 0, 0, 0, 0 } 94}; 95 96#if PY_VERSION_HEX >= 0x03000000 97 98static struct PyModuleDef mod_module = { 99 PyModuleDef_HEAD_INIT, 100 "initialize", 101 NULL, 102 0, 103 mod_methods, 104 NULL, 105 NULL, 106 NULL, 107 NULL 108}; 109 110#define INITERROR() return NULL 111#define INITDONE() return m 112 113PyObject* PyInit_initialize(void); 114 115PyObject* 116PyInit_initialize(void) 117 118#else 119 120#define INITERROR() return 121#define INITDONE() return 122 123void initinitialize(void); 124 125void 126initinitialize(void) 127#endif 128{ 129 PyObject* m; 130 131#if PY_VERSION_HEX >= 0x03000000 132 m = PyModule_Create(&mod_module); 133#else 134 m = Py_InitModule4("initialize", mod_methods, 135 NULL, NULL, PYTHON_API_VERSION); 136#endif 137 if (!m) { 138 INITERROR(); 139 } 140 141 if (PyObjC_ImportAPI(m) < 0) { 142 INITERROR(); 143 } 144 if (PyModule_AddObject(m, "OC_TestInitialize", 145 PyObjCClass_New([OC_TestInitialize class])) < 0) { 146 INITERROR(); 147 } 148 149 INITDONE(); 150} 151