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