1. Ronald Oussoren
  2. pyobjc

Commits

Ronald Oussoren  committed e97ddb9

Change the interface of PyObjCObject_New, and drop the variants that are no
longer needed.

Also use PyObjCObject_NewTransient in more places. TODO: review all uses of
PyObjCObject_New to see if those should use the transient API's as well.

  • Participants
  • Parent commits 90aa4bb
  • Branches pyobjc-ancient

Comments (0)

Files changed (9)

File Modules/Foundation/decimals.m

View file
  • Ignore whitespace
 }
 
 static PyObject *decimal_get__pyobjc_object__(PyObject *self, void *closure __attribute__((__unused__))) {
-	PyObject *rval = PyObjCObject_New(Decimal_ObjCValue(self));
+	PyObject *rval = PyObjCObject_New(Decimal_ObjCValue(self), 0, YES);
 	return rval;
 }
 

File Modules/objc/alloc_hack.m

View file
  • Ignore whitespace
 		return NULL;
 	}
 
-	return PyObjCObject_NewUnitialized(result);
+	return PyObjCObject_New(result, PyObjCObject_kUNINITIALIZED, NO);
 }
 
 static void 
 	void* callable)
 {
 	PyObject* arglist = NULL;
-	PyObject* v = NULL;
 	PyObject* result = NULL;
+	PyObject* pyself;
+	int cookie;
 
 	PyObjC_BEGIN_WITH_GIL
 
 			PyObjC_GIL_FORWARD_EXC();
 		}
 
-		v = PyObjCObject_NewClassic(*(id*)args[0]);
-		//v = PyObjC_IdToPython(*(id*)args[0]);
-		if (v == NULL) {
+		pyself = PyObjCObject_NewTransient(*(id*)args[0], &cookie);
+		if (pyself == NULL) {
 			Py_DECREF(arglist);
 			PyObjC_GIL_FORWARD_EXC();
 		}
 
-		PyTuple_SET_ITEM(arglist, 0, v);
-		v = NULL;
+		PyTuple_SET_ITEM(arglist, 0, pyself);
+		Py_INCREF(pyself);
 
 		result = PyObject_Call((PyObject*)callable, arglist, NULL);
 		if (result == NULL) {
 			Py_DECREF(arglist);
+			PyObjCObject_ReleaseTransient(pyself, cookie);
 			PyObjC_GIL_FORWARD_EXC();
 		}
 
 		Py_DECREF(arglist); 
+		PyObjCObject_ReleaseTransient(pyself, cookie);
 
 		/* TODO: assert result is None */
 		Py_DECREF(result);
 	void* callable)
 {
 	PyObject* arglist = NULL;
-	PyObject* v = NULL;
 	PyObject* result = NULL;
+	PyObject* pyself;
+	int cookie;
 	int err;
 
 	PyObjC_BEGIN_WITH_GIL
 			PyObjC_GIL_FORWARD_EXC();
 		}
 
-		v = PyObjCObject_NewClassic(*(id*)args[0]);
-		if (v == NULL) {
+		pyself = PyObjCObject_NewTransient(*(id*)args[0], &cookie);
+		if (pyself == NULL) {
 			Py_DECREF(arglist);
 			PyObjC_GIL_FORWARD_EXC();
 		}
 
-		PyTuple_SET_ITEM(arglist, 0, v);
-		v = NULL;
+		PyTuple_SET_ITEM(arglist, 0, pyself);
+		Py_INCREF(pyself);
 
 		result = PyObject_Call((PyObject*)callable, arglist, NULL);
 		if (result == NULL) {
 			Py_DECREF(arglist);
+			PyObjCObject_ReleaseTransient(pyself, cookie);
 			PyObjC_GIL_FORWARD_EXC();
 		}
 
 
 		err = depythonify_c_value(@encode(id), result, resp);
 		Py_DECREF(result); 
+		PyObjCObject_ReleaseTransient(pyself, cookie);
 		if (err == -1) {
 			PyObjC_GIL_FORWARD_EXC();
 		}

File Modules/objc/class-builder.m

View file
  • Ignore whitespace
 
 	PyObjC_BEGIN_WITH_GIL
 		/* First check if we respond */
-		pyself = PyObjCObject_New(self);
+		pyself = PyObjCObject_New(self, PyObjCObject_kDEFAULT, YES);
 		if (pyself == NULL) {
 			*pres = NO;
 			PyObjC_GIL_RETURNVOID;
 	}
 
 	PyObjC_BEGIN_WITH_GIL
-		pyself = PyObjCObject_New(self);
+		pyself = PyObjCObject_New(self, PyObjCObject_kDEFAULT, YES);
 		if (pyself == NULL) {
 			PyErr_Clear();
 			PyObjC_GIL_RETURNVOID;
 	volatile int have_output = 0;
 	PyGILState_STATE state = PyGILState_Ensure();
 
-	pyself = PyObjCObject_New(self);
+	pyself = PyObjCObject_New(self, PyObjCObject_kDEFAULT, YES);
 	if (pyself == NULL) {
 		PyObjCErr_ToObjCWithGILState(&state);
 		return;
 		) && [[self class] accessInstanceVariablesDirectly]) {
 
 			PyGILState_STATE state = PyGILState_Ensure();
-			PyObject* selfObj = PyObjCObject_New(self);
+			PyObject* selfObj = PyObjCObject_New(self, PyObjCObject_kDEFAULT, YES);
 			PyObject *res = NULL;
 			r = -1;
 			do {
 				[localException raise];
 			}
 			PyObject* res = NULL;
-			PyObject* selfObj = PyObjCObject_New(self);
+			PyObject* selfObj = PyObjCObject_New(self, PyObjCObject_kDEFAULT, YES);
 			r = -1;
 			do {
 				char *rawkey = (char *)[[@"_" stringByAppendingString:key] UTF8String];

File Modules/objc/objc-object.h

View file
  • Ignore whitespace
 #ifndef PyObjC_OBJC_OBJECT_H
 #define PyObjC_OBJC_OBJECT_H
 
+#define PyObjCObject_kDEFAULT 0x00
 #define PyObjCObject_kUNINITIALIZED 	0x01
 #define PyObjCObject_kCLASSIC 		0x02
 #define PyObjCObject_kDEALLOC_HELPER	0x04
 extern PyObjCClassObject PyObjCObject_Type;
 #define PyObjCObject_Check(obj) PyObject_TypeCheck(obj, (PyTypeObject*)&PyObjCObject_Type)
 
-PyObject* PyObjCObject_New(id objc_object);
-PyObject* PyObjCObject_NewClassic(id objc_object);
-PyObject* PyObjCObject_NewUnitialized(id objc_object);
+PyObject* PyObjCObject_New(id objc_object, int flags, int retain);
 PyObject* PyObjCObject_FindSelector(PyObject* cls, SEL selector);
 id 	  PyObjCObject_GetObject(PyObject* object);
 void 	  PyObjCObject_ClearObject(PyObject* object);

File Modules/objc/objc-object.m

View file
  • Ignore whitespace
 
 
 PyObject* 
-PyObjCObject_New(id objc_object)
+PyObjCObject_New(id objc_object, int flags, int retain)
 {
 	Class cls = [objc_object class];
 	PyTypeObject* cls_type;
 	PyObjCClass_CheckMethodList((PyObject*)res->ob_type, 1);
 	
 	((PyObjCObject*)res)->objc_object = objc_object;
-	((PyObjCObject*)res)->flags = 0;
+	((PyObjCObject*)res)->flags = flags;
 
-	if (strcmp(GETISA(objc_object)->name, "NSAutoreleasePool") != 0) {
-		/* NSAutoreleasePool doesn't like retain */
-		/* XXX: Technicly we shouldn't call retain either if this
-		 * is an uninitialized object.
-		 */
-		[objc_object retain];
+	if (retain) {
+		if (strcmp(GETISA(objc_object)->name, 
+						"NSAutoreleasePool") != 0) {
+			/* NSAutoreleasePool doesn't like retain */
+			[objc_object retain];
+		}
 	}
 
-	return res;
-}
-
-PyObject* 
-PyObjCObject_NewClassic(id objc_object)
-{
-	Class cls = [objc_object class];
-	PyTypeObject* cls_type;
-	PyObject*     res;
-
-	res = PyObjC_FindPythonProxy(objc_object);
-	if (res) return res;
-
-	assert(objc_object != nil);
-
-	cls_type = (PyTypeObject*)PyObjCClass_New(cls);
-	if (cls_type == NULL) {
-		return NULL;
+	/*
+	 * Don't register if we use the default flags, other parts will do
+	 * that if necessary. I don't like this, but don't want to pollute
+	 * the interface of this function with yet another argument.
+	 */
+	if (flags != PyObjCObject_kDEFAULT) {
+		PyObjC_RegisterPythonProxy(objc_object, res);
 	}
-
-	res = cls_type->tp_alloc(cls_type, 0);
-	Py_DECREF(cls_type);
-	if (res == NULL) {
-		return NULL;
-	}
-
-	/* This should be in the tp_alloc for the new class, but 
-	 * adding a tp_alloc to PyObjCClass_Type doesn't seem to help
-	 */
-	PyObjCClass_CheckMethodList((PyObject*)res->ob_type, 1);
-	
-	((PyObjCObject*)res)->objc_object = objc_object;
-	((PyObjCObject*)res)->flags = PyObjCObject_kCLASSIC;
-
-	return res;
-}
-
-PyObject* 
-PyObjCObject_NewUnitialized(id objc_object)
-{
-	PyTypeObject* cls_type;
-	PyObject*     res;
-
-	res = PyObjC_FindPythonProxy(objc_object);
-	if (res) return res;
-
-	assert(objc_object != nil);
-
-	cls_type = (PyTypeObject*)PyObjCClass_New(GETISA(objc_object));
-	if (cls_type == NULL) {
-		return NULL;
-	}
-
-	res = cls_type->tp_alloc(cls_type, 0);
-	Py_DECREF(cls_type);
-	if (res == NULL) {
-		return NULL;
-	}
-
-	/* This should be in the tp_alloc for the new class, but 
-	 * adding a tp_alloc to PyObjCClass_Type doesn't seem to help
-	 */
-	PyObjCClass_CheckMethodList((PyObject*)res->ob_type, 1);
-	
-	((PyObjCObject*)res)->objc_object = objc_object;
-	/*((PyObjCObject*)res)->flags = 0;*/
-	((PyObjCObject*)res)->flags = PyObjCObject_kUNINITIALIZED;
-
-	PyObjC_RegisterPythonProxy(objc_object, res);
-
 	return res;
 }
 
 	}
 
 	*cookie = 1;
-	result = PyObjCObject_NewUnitialized(objc_object);
-	((PyObjCObject*)result)-> flags |= PyObjCObject_kSHOULD_NOT_RELEASE;
-	((PyObjCObject*)result)-> flags &= ~PyObjCObject_kUNINITIALIZED;
+	result = PyObjCObject_New(objc_object, 
+			PyObjCObject_kSHOULD_NOT_RELEASE, NO);
 	return result;
 }
 

File Modules/objc/objc_support.m

View file
  • Ignore whitespace
 
 	rval = PyObjC_FindPythonProxy(self);
 	if (rval == NULL) {
-		rval = (PyObject *)PyObjCObject_New(self);
+		rval = (PyObject *)PyObjCObject_New(self, 
+				PyObjCObject_kDEFAULT, YES);
 		PyObjC_RegisterPythonProxy(self, rval);
 	}
 
 
 	rval = PyObjC_FindPythonProxy(self);
 	if (rval == NULL) {
-		rval = (PyObject *)PyObjCObject_New(self);
+		rval = (PyObject *)PyObjCObject_New(self,
+				PyObjCObject_kDEFAULT, YES);
 		PyObjC_RegisterPythonProxy(self, rval);
 	}
 	return rval;
 
 	rval = PyObjC_FindPythonProxy(self);
 	if (rval == NULL) {
-		rval = (PyObject *)PyObjCObject_NewClassic(self);
+		rval = (PyObject *)PyObjCObject_New(self,
+				PyObjCObject_kCLASSIC, NO);
 		PyObjC_RegisterPythonProxy(self, rval);
 	}
 	return rval;
 	
 	rval = PyObjC_FindPythonProxy(self);
 	if (rval == NULL) {
-		rval= PyObjCObject_New(self);
+		rval= PyObjCObject_New(self,
+				PyObjCObject_kDEFAULT, YES);
 
 		if (PyObjC_NSNumberWrapper && rval) {
 			PyObject *val = rval;
 
 	rval = PyObjC_FindPythonProxy(self);
 	if (rval == NULL) {
-		rval = (PyObject *)PyObjCObject_New(self);
+		rval = (PyObject *)PyObjCObject_New(self,
+				PyObjCObject_kDEFAULT, YES);
 		PyObjC_RegisterPythonProxy(self, rval);
 	}
 

File Modules/objc/objc_util.m

View file
  • Ignore whitespace
 	PyDict_SetItemString(dict, "reason",  v);
 	Py_DECREF(v);
 	if (userInfo) {
-		v = PyObjCObject_New(userInfo);
+		v = PyObjCObject_New(userInfo, PyObjCObject_kDEFAULT, YES);
 		if (v != NULL) {
 			PyDict_SetItemString(dict, "userInfo", v);
 			Py_DECREF(v);

File Modules/objc/pyobjc-api.h

View file
  • Ignore whitespace
  * - Version 12 adds PyObjCObject_New
  * - Version 13 adds PyObjCCreateOpaquePointerType
  * - Version 14 adds PyObjCObject_NewTransient, PyObjCObject_ReleaseTransient
+ * - Version 15 changes the interface of PyObjCObject_New
  */
-#define PYOBJC_API_VERSION 14
+#define PYOBJC_API_VERSION 15
 
 #define PYOBJC_API_NAME "__C_API__"
 
 	int (*pyobjc_convertchar)(PyObject*,void*);
 
 	/* PyObjCObject_New */
-	PyObject* (*pyobjc_object_new)(id);
+	PyObject* (*pyobjc_object_new)(id, int , int);
 
 	/* PyObjCCreateOpaquePointerType */
 	PyObject* (*pointer_type_new)(const char*, const char*, const char*);

File Modules/objc/unicode-object.m

View file
  • Ignore whitespace
 {
 	PyObjCUnicodeObject* uobj = (PyObjCUnicodeObject*)self;
 	if (uobj->py_nsstr == NULL) {
-		uobj->py_nsstr = PyObjCObject_New(uobj->nsstr);
+		uobj->py_nsstr = PyObjCObject_New(uobj->nsstr, 
+				PyObjCObject_kDEFAULT, YES);
 	}
 	Py_INCREF(uobj->py_nsstr);
 	return uobj->py_nsstr;