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