Commits

Ronald Oussoren committed 366555e

Some more cleanups, nothing shocking.

This doesn't solve the crashes I'm seeing on my 10.2 box ...

Comments (0)

Files changed (8)

Modules/objc/OC_PythonArray.m

 
 - initWithPythonObject:(PyObject*)v;
 {
+	self = [super init];
+	if (!self) return nil;
+
 	Py_INCREF(v);
 	Py_XDECREF(value);
 	value = v;

Modules/objc/OC_PythonDictionary.m

 
 -initWithPythonObject:(PyObject*)v;
 {
+	self = [super init];
+	if (!self) return nil;
+
 	value = PySequence_Fast(v, 
 		"pyObject of OC_PythonDictionaryEnumerator must be a sequence");
 	cur   = 0;
 
 -initWithPythonObject:(PyObject*)v;
 {
+	self = [super init];
+	if (!self) return nil;
+
+	Py_INCREF(v);
+	Py_XDECREF(value);
 	value = v;
-	Py_INCREF(v);
 	return self;
 }
 
 		}
 
 		v = PyDict_GetItem(value, k);
+		Py_DECREF(k);
 
 		if (!v) {
-			Py_DECREF(k);
 			PyErr_Clear();
 			PyObjC_GIL_RETURN(nil);
 		}
 
 		err = depythonify_c_value("@", v, &result);
-		Py_DECREF(k);
 		if (err == -1) {
 			PyObjC_GIL_FORWARD_EXC();
 		}
 	PyObject* k = NULL;
 
 	PyObjC_BEGIN_WITH_GIL
-		v = pythonify_c_value("@", &val);
+		v = PyObjC_IdToPython(val);
 		if (v == NULL) {
-			Py_XDECREF(k);
 			PyObjC_GIL_FORWARD_EXC();
 		}
 
-		k = pythonify_c_value("@", &key);
+		k = PyObjC_IdToPython(key);
 		if (k == NULL) {
 			Py_XDECREF(v);
-			Py_XDECREF(k);
 			PyObjC_GIL_FORWARD_EXC();
 		}
 
 	PyObject* k;
 
 	PyObjC_BEGIN_WITH_GIL
-		k = pythonify_c_value("@", &key);
+		k = PyObjC_IdToPython(key);
 		if (k == NULL) {
 			PyObjC_GIL_FORWARD_EXC();
 		}
 
 	PyObjC_BEGIN_WITH_GIL
 		keys = PyDict_Keys(value);
+		if (keys == NULL) {
+			PyObjC_GIL_FORWARD_EXC();
+		}
 		result = [OC_PythonDictionaryEnumerator 
 				newWithPythonObject:keys];
 		Py_DECREF(keys);

Modules/objc/OC_PythonObject.m

 {
 	PyCodeObject *func_code;
 
-	if (!pymethod) {
-		return NULL;
-	}
 	if (PyFunction_Check(pymethod)) {
         	func_code = (PyCodeObject *)PyFunction_GetCode(pymethod);
 		if (argcount == func_code->co_argcount) {
 	pymethod = PyObject_GetAttrString(obj, 
 			PyObjC_SELToPythonName(
 				aSelector, pymeth_name, sizeof(pymeth_name)));
+	if (pymethod == NULL) {
+		return NULL;	
+	}
+
 	return check_argcount(pymethod, argcount);
 }
 
 		}
 	
 	PyObjC_END_WITH_GIL
-
 }
 
 
 		}
 
 
-		if (PyMethod_Check (pymethod)) {
-			func_code = (PyCodeObject *) PyFunction_GetCode(
+		if (PyMethod_Check(pymethod)) {
+			func_code = (PyCodeObject*) PyFunction_GetCode(
 				PyMethod_Function (pymethod));
 			argcount = func_code->co_argcount-1;
 
 		} else {
-			func_code = (PyCodeObject *) PyFunction_GetCode(
+			func_code = (PyCodeObject*) PyFunction_GetCode(
 				pymethod);
 			argcount = func_code->co_argcount;
 		}
 
 	PyObject* keyName;
 	PyObject* val;
-	id res;
+	id res = nil;
 
 	PyObjC_BEGIN_WITH_GIL
 
 			}
 		}
 
-		keyName = pythonify_c_value(@encode(id), &key);
+		keyName = PyObjC_IdToPython(key);
 		if (keyName == NULL) {
 			PyObjC_GIL_FORWARD_EXC();
 		}
 			}
 		}
 
-		keyName = pythonify_c_value(@encode(id), &key);
+		keyName = PyObjC_IdToPython(key);
 		if (keyName == NULL) {
 			PyObjC_GIL_FORWARD_EXC();
 		}
 
-		pyValue = pythonify_c_value(@encode(id), &value);
+		pyValue = PyObjC_IdToPython(value);
 		if (pyValue == NULL) {
 			Py_DECREF(keyName);
 			PyObjC_GIL_FORWARD_EXC();

Modules/objc/OC_PythonString.h

-#ifndef OC_PythonString_h
-#define OC_PythonString_h
-
-#import <CoreFoundation/CoreFoundation.h>
-#import <Foundation/NSString.h>
-
-/*
- * OC_PythonString - Objective-C proxy class for Python strings
- *
- * Instances of this class are used as proxies for Python strings 
- * when these are passed to Objective-C code. Because this class is
- * a subclass of NSString Python sequences can be used everywhere
- * where NSString is used.  Python strings are immutable.
- */
-
-@interface OC_PythonString:NSString
-{
-    PyObject* value;
-    PyObject* _internalRep;
-    CFStringRef stringValue;
-}
-
-+newWithPythonObject:(PyObject*)value;
--initWithPythonObject:(PyObject*)value;
--(void)dealloc;
--(PyObject*)__pyobjc_PythonObject__;
-@end
-#endif

Modules/objc/class-builder.m

  * NOTE1: the meta_class field is first because poseAs: copies the class but
  *        not the meta class (on MacOS X <= 10.2)
  * NOTE2: That doesn't help, test_posing still crashes.
+ * XXX: Is this still relevant? The current code only refers to the struct
+ *      during the construction of the class, which means poseAs: probably 
+ *      works just fine (to be checked after 1.1)
  */
 #define MAGIC 0xDEADBEEF
 #define CLASS_WRAPPER(cls) ((struct class_wrapper*)(cls))
 /*
  * First step of creating a python subclass of an objective-C class
  *
- * Returns NULL or the newly created objective-C klass. 'class_dict' may
+ * Returns NULL or the newly created objective-C class. 'class_dict' may
  * be modified by this function.
  *
  * TODO:

Modules/objc/objc-class.m

  *
  *    The key is the Objective-C class, the value is its wrapper.
  */
-static NSMapTable*	class_registry = NULL;
+static NSMapTable* class_registry = NULL;
 
 
 /*!
 	info->delmethod = delmethod;
 	info->hasPythonImpl = 1;
 
-
 	PyObjCClass_SetClass(objc_class, res);
 
 	var = class_getInstanceVariable(objc_class, "__dict__");
 			}
 			info->method_magic = magic;
 			if (info->sel_to_py) {
-				Py_DECREF(info->sel_to_py);
+				Py_XDECREF(info->sel_to_py);
 				info->sel_to_py = PyDict_New();
 			}
 		}
 
 #ifdef  PyObjC_COMPILING_ON_MACOSX_10_1
 	/* On MacOSX we get hard crashes for the method signature of this
-	* method. This seems to be a problem with the Objective-C runtime 
+	 * method. This seems to be a problem with the Objective-C runtime 
 	 * and/or Cocoa because a simple ObjC program gives the same error.
 	 * 
 	 * As this method should never be forwarded to Python anyway we can
 			continue;
 		}
 	}
-
+	Py_DECREF(keys);
 	Py_DECREF(args);
 
 	return 0;
 }
-

Modules/objc/objc-object.m

 
 #include <objc/Object.h>
 
-
-
-/*
- * Basic freelist. 
- * - to delete an object: obj_freelist[obj_freelist_top++] = OBJ
- * - to create an object: OBJ = obj_freelist[--obj_freelist_top];
- */
-#if 0
-#define FREELIST_SIZE 1024
-
-static PyObject* obj_freelist[FREELIST_SIZE];
-static int obj_freelist_top = 0;
-#endif
-
-
 static NSMapTable* proxy_dict = NULL;
 
 static PyObject* 
 		}
 	}
 
-
 	NSMapInsert(proxy_dict, objc_obj, proxy_obj);
 
 	return 0;
 static void
 object_dealloc(PyObject* obj)
 {
-	/* XXX: This should not be necessary, but if we don't do this we
-	 * sometimes loose exception information...
+	/*
+  	 * Save exception information, needed because releasing the object
+	 * might clear or modify the exception state.
 	 */
 	PyObject* ptype, *pvalue, *ptraceback;
 	PyErr_Fetch(&ptype, &pvalue, &ptraceback);
 	if (PyObjCObject_GetFlags(obj) != PyObjCObject_kDEALLOC_HELPER 
 			&& PyObjCObject_GetObject(obj) != nil) {
 		/* Release the proxied object, we don't have to do this when
-		 * there is no proxied object!
+		 * there is no proxied object.
 		 */
 		unregister_proxy(PyObjCObject_GetObject(obj));
 
-
-		/* If the object is not yet initialized we try to initialize 
-		 * it before releasing the reference. This is necessary 
-		 * because of a misfeature of MacOS X: 
-		 * [[NSTextView alloc] release] crashes (at least upto 10.2)
-		 * and this is not a bug according to Apple.
-		 */
 		if (PyObjCObject_IsClassic(obj)) {
 			/* pass */
 
 		}
 	}
 
-#ifdef FREELIST_SIZE
-	/* Push self onto the freelist */
-	if (obj_freelist_top == FREELIST_SIZE) {
-		obj->ob_type->tp_free(obj);
-	} else {
-		obj_freelist[obj_freelist_top++] = obj;
-		obj->ob_refcnt = 0xDEADBEEF;
-	}
-#else
 	obj->ob_type->tp_free(obj);
-#endif
 
 	PyErr_Restore(ptype, pvalue, ptraceback);
 }
 		goto done;
 	}
 
-
 	obj_class = GETISA(obj_inst);
 	tp = (PyTypeObject*)PyObjCClass_New(obj_class);
 
 	descr = NULL;
+
 	if (tp != obj->ob_type) {
 		/* Workaround for KVO implementation feature */
 		PyObject* dict;
 	     "'%.50s' object has no attribute '%.400s'",
 	     tp->tp_name, namestr);
 
-  done:
+done:
 	if (res != NULL) {
 		/* class methods cannot be accessed through instances */
 		if (PyObjCSelector_Check(res) 
 	PyObject** dictptr;
 	int res = -1;
 	
-
-
-	if (!PyString_Check(name)){
+	if (!PyString_Check(name)) {
 #ifdef Py_USING_UNICODE
 		/* The Unicode to string conversion is done here because the
 		   existing tp_setattro slots expect a string object as name
 				name->ob_type->tp_name);
 			return -1;
 		}
+	} else {
+		Py_INCREF(name);
 	}
-	else
-		Py_INCREF(name);
 
 	if (PyObjCObject_GetObject(obj) == nil) {
 		PyErr_Format(PyExc_AttributeError,
 			*dictptr = dict;
 		}
 		if (dict != NULL) {
-			if (value == NULL)
+			if (value == NULL) {
 				res = PyDict_DelItem(dict, name);
-			else
+			} else {
 				res = PyDict_SetItem(dict, name, value);
-			if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
+			}
+			if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) {
 				PyErr_SetObject(PyExc_AttributeError, name);
+			}
 			goto done;
 		}
 	}
 		     "'%.50s' object attribute '%.400s' is read-only",
 		     tp->tp_name, PyString_AS_STRING(name));
   done:
-
-#if 0
-	/* XXX: This would introduce some form of support for KeyValueObserving
-	 * but, I'm not sure if this is the right approach. If this is, this 
-	 * code needs more work: the name must be transformed to 
-	 * a KeyValueCoding key (e.g. '_name' -> 'name')
-	 */
-
-#if defined(MACOSX) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
-	/* KeyValueObserving support. If an attribute is changed by assignment,
-	 * send out the notification.
-	 */
-	if (!PyErr_Occurred()) {
-		id key = PyObjC_PythonToId(name);
-		if (key == NULL && PyErr_Occurred()) {
-			/* Cannot convert the name, ignore this */
-			PyErr_Clear();
-		} else {
-			[PyObjCObject_GetObject(obj) didChangeValueForKey:key];
-		}
-	}
-#endif
-
-#endif
-
 	Py_DECREF(name);
 	return res;
 }
 
 PyDoc_STRVAR(objc_get_real_class_doc, "Return the current ISA of the object");
-static PyObject* objc_get_real_class(PyObject* self, void* closure __attribute__((__unused__)))
+static PyObject* 
+objc_get_real_class(PyObject* self, void* closure __attribute__((__unused__)))
 {
 	id obj_object;
 	PyObject* ret;
 	assert(obj_object != nil);
 	ret = PyObjCClass_New(GETISA(obj_object));
 	if (ret != (PyObject*)self->ob_type) {
-		/* XXX doesn't this leak a reference to the original ob_type? */
+		Py_DECREF(self->ob_type);
 		self->ob_type = (PyTypeObject*)ret;
 		Py_INCREF(ret);
 	}
 		return NULL;
 	}
 
-#ifdef FREELIST_SIZE
-	if (obj_freelist_top == 0) {
-		res = cls_type->tp_alloc(cls_type, 0);
-		Py_DECREF(cls_type);
-		if (res == NULL) {
-			return NULL;
-		}
-	} else {
-		res = obj_freelist[obj_freelist_top-1];
-		obj_freelist_top -= 1;
-		if (res->ob_refcnt != 0xDEADBEEF) abort();
-		res->ob_refcnt = 1;
-		res->ob_type = cls_type;
-	}
-#else
 	res = cls_type->tp_alloc(cls_type, 0);
 	Py_DECREF(cls_type);
 	if (res == NULL) {
 		return NULL;
 	}
-#endif
 
 	PyObjCClass_CheckMethodList((PyObject*)res->ob_type, 1);
 	
 		return NULL;
 	}
 
-#ifdef FREELIST_SIZE
-	if (obj_freelist_top == 0) {
-		res = cls_type->tp_alloc(cls_type, 0);
-		Py_DECREF(cls_type);
-		if (res == NULL) {
-			return NULL;
-		}
-	} else {
-		res = obj_freelist[obj_freelist_top-1];
-		obj_freelist_top -= 1;
-		if (res->ob_refcnt != 0xDEADBEEF) abort();
-		res->ob_refcnt = 1;
-		res->ob_type = cls_type;
-	}
-#else
 	res = cls_type->tp_alloc(cls_type, 0);
 	Py_DECREF(cls_type);
 	if (res == NULL) {
 		return NULL;
 	}
-#endif
 
 	/* This should be in the tp_alloc for the new class, but 
 	 * adding a tp_alloc to PyObjCClass_Type doesn't seem to help
 	((PyObjCObject*)res)->objc_object = objc_object;
 	((PyObjCObject*)res)->flags = 0;
 
-
-
 	if (strcmp(GETISA(objc_object)->name, "NSAutoreleasePool") != 0) {
 		/* NSAutoreleasePool doesn't like retain */
 		/* XXX: Technicly we shouldn't call retain either if this
 		return NULL;
 	}
 
-#ifdef FREELIST_SIZE
-	if (obj_freelist_top == 0) {
-		res = cls_type->tp_alloc(cls_type, 0);
-		Py_DECREF(cls_type);
-		if (res == NULL) {
-			return NULL;
-		}
-	} else {
-		res = obj_freelist[obj_freelist_top-1];
-		obj_freelist_top -= 1;
-		if (res->ob_refcnt != 0xDEADBEEF) abort();
-		res->ob_refcnt = 1;
-		res->ob_type = cls_type;
-	}
-#else
 	res = cls_type->tp_alloc(cls_type, 0);
 	Py_DECREF(cls_type);
 	if (res == NULL) {
 		return NULL;
 	}
-#endif
 
 	/* This should be in the tp_alloc for the new class, but 
 	 * adding a tp_alloc to PyObjCClass_Type doesn't seem to help
 	PyTypeObject* cls_type;
 	PyObject*     res;
 
-
 	res = find_existing_proxy(objc_object);
 	if (res) return res;
 
 void        
 PyObjCObject_ClearObject(PyObject* object)
 {
-	if (object == NULL) abort();
 	if (!PyObjCObject_Check(object)) {
 		PyErr_Format(PyExc_TypeError,
 			"'objc.objc_object' expected, got '%s'",

Modules/objc/objc-runtime-apple.h

 #ifndef PyObjC_RUNTIME_APPLE_H
 #define PyObjC_RUNTIME_APPLE_H
 
-
 #import <Foundation/NSObject.h>
 #import <Foundation/NSString.h>
 
 #include <objc/objc-runtime.h>
 
-static inline int PyObjCRT_SameSEL(SEL a, SEL b)
+static inline int 
+PyObjCRT_SameSEL(SEL a, SEL b)
 {
 	return a == b;
 }
 static inline void 
 PyObjCRT_InitMethod(Method m, SEL name, const char* types, IMP imp)
 {
+	memset(m, 0, sizeof(*m));
 	m->method_name = name;
 	m->method_types = strdup((char*)types);
 	m->method_imp = imp;
 typedef Method PyObjCRT_Method_t;
 typedef Ivar PyObjCRT_Ivar_t;
 
-#define GETISA(c)       (c)->isa
+#define GETISA(c)       ((c)->isa)
 
-#define RECEIVER(c)     (c).receiver
+#define RECEIVER(c)     ((c).receiver)
 
 #define _C_CONST    'r'
 #define _C_IN       'n'
 static inline const char *
 get_selector_encoding (id self, SEL sel)
 {
-  struct objc_method* m = class_getInstanceMethod(self->isa, sel);
+	struct objc_method* m = class_getInstanceMethod(self->isa, sel);
 
-  if (!m)
-    return NULL;
-  else
-    return m->method_types;
+	if (!m) {
+		return NULL;
+	} else {
+		return m->method_types;
+	}
 }
 
 #endif /* PyObjC_RUNTIME_APPLE_H */
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.