Commits

Bob Ippolito  committed b00215c

Fix problem returning nil in some places (such as from +alloc).

See: [Pyobjc-dev] Returning nil from alloc()

  • Participants
  • Parent commits 2a6ef71
  • Branches pyobjc-ancient

Comments (0)

Files changed (4)

File Lib/objc/test/test_classandinst.py

 import unittest
 import objc
-from objc.test.testclassandinst import PyObjC_TestClassAndInstance
+from objc.test.testclassandinst import PyObjC_TestClassAndInstance, PyObjC_TestUnallocatable
 
 class PyObjC_TestClassAndInstanceSubclass(PyObjC_TestClassAndInstance):
     """Simple subclass, just make sure it still works"""
         self.assertEquals(PyObjC_TestClassAndInstance.pyobjc_classMethods.isInstance(), objc.NO)
         self.assertEquals(PyObjC_TestClassAndInstance.alloc().init().isInstance(), objc.YES)
 
+    def testUnallocatable(self):
+        self.assertEquals(PyObjC_TestUnallocatable.alloc(), None)
+
 if __name__ == '__main__':
     unittest.main()

File Modules/objc/objc-object.m

 	descrgetfunc f;
 	PyObject **dictptr;
 	char*      namestr;
-	Class obj_class;
 	id obj_inst;
 
 	if (!PyString_Check(name)){
 		goto done;
 	}
 
-	obj_class = GETISA(obj_inst);
-	tp = (PyTypeObject*)PyObjCClass_New(obj_class);
+	tp = (PyTypeObject*)PyObjCClass_New(GETISA(obj_inst));
 
 	descr = NULL;
 
 PyObject* 
 _PyObjCObject_NewDeallocHelper(id objc_object)
 {
-	Class cls; 
 	PyObject* res;
 	PyTypeObject* cls_type;
 
 	assert(objc_object != nil);
-	cls = GETISA(objc_object);
-	cls_type = (PyTypeObject*)PyObjCClass_New(cls);
+	cls_type = (PyTypeObject*)PyObjCClass_New(GETISA(objc_object));
 	if (cls_type == NULL) {
 		return NULL;
 	}
 
 	PyObjCClass_CheckMethodList((PyObject*)res->ob_type, 1);
 	
-	assert(objc_object != nil);
 	((PyObjCObject*)res)->objc_object = objc_object;
 	((PyObjCObject*)res)->flags = PyObjCObject_kDEALLOC_HELPER;
 	return res;
 	res = PyObjC_FindPythonProxy(objc_object);
 	if (res) return res;
 
-	if (objc_object == NULL) {
-		Py_INCREF(Py_None);
-		return Py_None;
-	}
+	assert(objc_object != nil);
 
 	cls_type = (PyTypeObject*)PyObjCClass_New(cls);
 	if (cls_type == NULL) {
 	 */
 	PyObjCClass_CheckMethodList((PyObject*)res->ob_type, 1);
 	
-	assert(objc_object != nil);
 	((PyObjCObject*)res)->objc_object = objc_object;
 	((PyObjCObject*)res)->flags = 0;
 
 	res = PyObjC_FindPythonProxy(objc_object);
 	if (res) return res;
 
-	if (objc_object == NULL) {
-		Py_INCREF(Py_None);
-		return Py_None;
-	}
+	assert(objc_object != nil);
 
 	cls_type = (PyTypeObject*)PyObjCClass_New(cls);
 	if (cls_type == NULL) {
 	 */
 	PyObjCClass_CheckMethodList((PyObject*)res->ob_type, 1);
 	
-	assert(objc_object != nil);
 	((PyObjCObject*)res)->objc_object = objc_object;
 	((PyObjCObject*)res)->flags = PyObjCObject_kCLASSIC;
 
 PyObject* 
 PyObjCObject_NewUnitialized(id objc_object)
 {
-	Class cls = GETISA(objc_object);
 	PyTypeObject* cls_type;
 	PyObject*     res;
 
 	res = PyObjC_FindPythonProxy(objc_object);
 	if (res) return res;
 
-	if (objc_object == NULL) {
-		Py_INCREF(Py_None);
-		return Py_None;
-	}
+	assert(objc_object != nil);
 
-	cls_type = (PyTypeObject*)PyObjCClass_New(cls);
+	cls_type = (PyTypeObject*)PyObjCClass_New(GETISA(objc_object));
 	if (cls_type == NULL) {
 		return NULL;
 	}
 	 */
 	PyObjCClass_CheckMethodList((PyObject*)res->ob_type, 1);
 	
-	assert(objc_object != nil);
 	((PyObjCObject*)res)->objc_object = objc_object;
 	((PyObjCObject*)res)->flags = 0;
 

File Modules/objc/proxy-registry.m

 {
 	PyObject* v;
 	
-	v = NSMapGet(python_proxies, original);
+    if (original == nil) {
+        v = Py_None;
+    } else {
+        v = NSMapGet(python_proxies, original);
+    }
 	Py_XINCREF(v);
 	return v;
 }
 
 id PyObjC_FindObjCProxy(PyObject* original)
 {
-	return NSMapGet(objc_proxies, original);
+    if (original == Py_None) {
+        return nil;
+    } else {
+        return NSMapGet(objc_proxies, original);
+    }
 }

File Modules/objc/test/testclassandinst.m

 #include <objc/objc-runtime.h>
 #endif
 
+@interface PyObjC_TestUnallocatable : NSObject
+{
+}
+@end
+
+@implementation PyObjC_TestUnallocatable
++ allocWithZone:(NSZone *)zone
+{
+    return nil;
+}
+@end
+
 @interface PyObjC_TestClassAndInstance: NSObject
 {
 }
 
 	PyModule_AddObject(m, "PyObjC_TestClassAndInstance", 
 		PyObjCClass_New([PyObjC_TestClassAndInstance class]));
+
+	PyModule_AddObject(m, "PyObjC_TestUnallocatable", 
+		PyObjCClass_New([PyObjC_TestUnallocatable class]));
 }