Commits

Ronald Oussoren committed adbfb83

First batch of clang cleanups

This fixes a number of issues located by the clang compiler and analyzer,
there will be more fixes in a future checkin.

Also adds a new private function the objc extension, to be used
by a new metadata loader.

Comments (0)

Files changed (15)

pyobjc-core/Modules/objc/OC_PythonArray.m

 			}
 		}
 
-		w = PyObject_CallMethod(value, "insert", "iO", idx, v);
+		w = PyObject_CallMethod(value, "insert", Py_ARG_SIZE_T "O", theIndex, v);
 		if (unlikely(w == NULL)) {
 			Py_DECREF(v);
 			PyObjC_GIL_FORWARD_EXC();

pyobjc-core/Modules/objc/OC_PythonData.m

 		if (unlikely(PyObject_AsReadBuffer(value, &buffer, &buffer_len) == -1)) {
 			PyErr_Clear();
 			rval = 0;
-		}
-		if ((NSUInteger)buffer_len > NSUIntegerMax) {
-			rval = NSUIntegerMax;
 		} else {
-			rval = buffer_len;
+			if ((NSUInteger)buffer_len > NSUIntegerMax) {
+				rval = NSUIntegerMax;
+			} else {
+				rval = buffer_len;
+			}
 		}
 	PyObjC_END_WITH_GIL
 	return rval;

pyobjc-core/Modules/objc/OC_PythonDate.m

 		Py_DECREF(name); name = NULL;
 
 		if (datetime == NULL) {
-			Py_DECREF(datetime);
+			Py_DECREF(name);
 			PyErr_Clear();
 			return nil;
 		}
 
 
 	if (PySequence_Contains(datetime_types, (PyObject*)(Py_TYPE(object)))) {
-		return [[OC_PythonDate alloc] initWithPythonObject:object];
+		return [[[OC_PythonDate alloc] initWithPythonObject:object] autorelease];
 	}
 	return nil;
 }
 				"%Y-%m-%d %H:%M:%S %z");
 			if (v == NULL) {
 				/* Raise ObjC exception */
+				PyObjC_GIL_FORWARD_EXC();
 			}
 
 

pyobjc-core/Modules/objc/OC_PythonEnumerator.m

 
 +enumeratorWithPythonObject:(PyObject*)object
 {
-	return [[self alloc] initWithPythonObject:object];
+	return [[[self alloc] initWithPythonObject:object] autorelease];
 }
 
 -initWithPythonObject:(PyObject*)object

pyobjc-core/Modules/objc/OC_PythonObject.m

 get_method_for_selector(PyObject *obj, SEL aSelector)
 {
 	const char*  meth_name;
-	Py_ssize_t   len;
 	char         pymeth_name[256];
 	Py_ssize_t   argcount;
 	PyObject*    pymethod;
 	}
 
 	meth_name = sel_getName(aSelector);
-	len = strlen(meth_name);
       
 	for (argcount=0, p=meth_name; *p; p++) {
 		if (*p == ':') {
 {
 	NSArray* elems = [keyPath componentsSeparatedByString:@"."];
 	id target;
-	int len;
-	int i;
+	NSInteger len;
+	NSInteger i;
 
 	len = [elems count];
 	target = self;

pyobjc-core/Modules/objc/block_support.h

 
 typedef void (*_block_func_ptr)(void*, ...);
 extern _block_func_ptr PyObjCBlock_GetFunction(void* block);
+extern const char* PyObjCBlock_GetSignature(void* _block);
 extern void* PyObjCBlock_Create(PyObjCMethodSignature* signature, PyObject* callable);
 extern void PyObjCBlock_Release(void* _block);
 extern int PyObjCBlock_Setup(void);

pyobjc-core/Modules/objc/block_support.m

     BLOCK_HAS_CTOR =          (1 << 26), // helpers have C++ code
     BLOCK_IS_GLOBAL =         (1 << 28),
     BLOCK_HAS_DESCRIPTOR =    (1 << 29), // interim until complete world build is accomplished
+    BLOCK_HAS_SIGNATURE  =    (1 << 30), // interim until complete world build is accomplished
 };
 
 
 	/* minimal definition, only contains the definitions we actually use */
 
 enum {
-	BLOCK_HAS_COPY_DISPOSE =  (1 << 25)
+	BLOCK_HAS_COPY_DISPOSE =  (1 << 25),
+   	BLOCK_HAS_SIGNATURE  =    (1 << 30) // interim until complete world build is accomplished
 };
 
 #endif
 	unsigned long int size;
 	void (*copy_helper)(void* dst, void*src);
 	void (*dispose_helper)(void* src);
+	const char* signature;
+};
+
+struct block_descriptor_basic {
+	unsigned long int reserved;
+	unsigned long int size;
+	void* rest[1];
 };
 
 struct block_literal {
 	PyObject* invoke_cleanup;
 };
 
+extern const char* PyObjCBlock_GetSignature(void* _block)
+{
+	struct block_literal* block = (struct block_literal*)_block;
+	struct block_descriptor_basic* descriptor = (struct block_descriptor_basic*)block->descriptor;
+	int offset = 0;
+
+	offset = 0;
+	if (block->flags & BLOCK_HAS_COPY_DISPOSE) {
+		offset += 2;
+	}
+	if (block->flags & BLOCK_HAS_SIGNATURE) {
+		return descriptor->rest[offset];
+	}
+	return NULL;
+}
+
 static void 
 oc_copy_helper(void* _dst, void* _src)
 {
 	0,
 	sizeof(struct block_literal),
 	oc_copy_helper,
-	oc_dispose_helper
+	oc_dispose_helper,
+	0
 };
 
 static struct block_literal gLiteralTemplate = {
 	}
 
 	*block = gLiteralTemplate;
+	/* XXX: block->descriptor needs to be copied to be able to add a signature to it */
 	block->isa = gStackBlockClass;
 	block->invoke = PyObjCFFI_MakeBlockFunction(signature, callable);
 	if (block->invoke == NULL) {

pyobjc-core/Modules/objc/function.m

 
 	result->methinfo= PyObjCMethodSignature_WithMetaData(signature, meta, NO);
 	if (result->methinfo == NULL) {
-		Py_DECREF(result->methinfo);
+		Py_DECREF(result);
 		return NULL;
 	}
 

pyobjc-core/Modules/objc/libffi_support.m

 				resttype = gCharEncoding;
 			}
 
-			error = 0;
 			argument = PyTuple_GET_ITEM (args, py_arg);
 			py_arg ++;
 
 					if (methinfo->argtype[i].printfFormat) {
 						printf_format = argument;
 						Py_INCREF(argument);
-						error = 0;
-
 					} 
 					error = depythonify_c_value (
 						argtype+1, 
 
 					case PyObjC_kNullTerminatedArray:
 						{
-						const char* buf;
-						Py_ssize_t len;
-
-						error = PyObject_AsCharBuffer(argument, &buf, &len);
-						if (error != -1) {
-							byref[i] = PyMem_Malloc(len+1);
-							if (byref[i] == NULL) {
-								PyErr_NoMemory();
+							const char* buf;
+							Py_ssize_t len;
+
+							error = PyObject_AsCharBuffer(argument, &buf, &len);
+							if (error != -1) {
+								byref[i] = PyMem_Malloc(len+1);
+								if (byref[i] == NULL) {
+									PyErr_NoMemory();
+									error = -1;
+								} else {
+									memcpy(byref[i], buf, len);
+									((char*)byref[i])[len] = '\0';
+								}
+							} else {
 								error = -1;
-							} else {
-								memcpy(byref[i], buf, len);
-								((char*)byref[i])[len] = '\0';
 							}
-							Py_DECREF(seq);
-						} else {
-							error = -1;
-						}
 						}
 						break;
 

pyobjc-core/Modules/objc/method-accessor.m

 	void* iterator;
 	char  buf[256];
 	Class    objc_class;
-	PyObject* bound_self;
 
 	if (PyObjCObject_Check(self)) {
 		id obj = PyObjCObject_GetObject(self);
 
 		if (class_method) {
 			cls = object_getClass(obj);
-			bound_self = (PyObject*)Py_TYPE(self);
 			objc_class = object_getClass(cls); 
 		} else {
 			cls = object_getClass(obj);
 			objc_class = cls;
-			bound_self = self;
 		}
 
 	} else if (PyObjCClass_Check(self)) {
 		objc_class = cls;
 		if (class_method) {
 			objc_class = object_getClass(cls);
-			bound_self = self;
-		} else {
-			bound_self = NULL;
 		}
 
 	} else {
 		if (methods == NULL) {
 			objc_class = class_getSuperclass((Class)objc_class);
 			cls = class_getSuperclass((Class)cls);
+			continue;
 		}
 
 		for (i = 0; i < method_count; i++) {

pyobjc-core/Modules/objc/module.m

 #import <mach-o/loader.h>
 #import <objc/Protocol.h>
 
+#include <dlfcn.h>
+
+
 int PyObjC_VerboseLevel = 0;
 int PyObjC_HideProtected = 1;
 BOOL PyObjC_useKVO = YES;
 	for (i = 0; i < protCount; i++) {
 		PyObject *p = PyObjCFormalProtocol_ForProtocol(protlist[i]);
 		if (p == NULL) {
-			Py_DECREF(p);
+			Py_DECREF(protocols);
 			free(protlist);
 			return NULL;
 		}
 }
 #endif
 
+static PyObject*
+PyObjC_LoadConstant(PyObject* self __attribute__((__unused__)),
+	PyObject* args, PyObject* kwds)
+{
+static char* keywords[] = { "name", "type", "magic", NULL };
+	char* name;
+	char* type;
+	int   magic;
+
+	if (!PyArg_ParseTupleAndKeywords(args, kwds, "ssi",
+		keywords, &name, &type, &magic)) {
+		
+		return NULL;
+	}
+
+	void* buf = dlsym(RTLD_DEFAULT, name);
+	if (buf == NULL) {
+		PyErr_SetString(PyExc_AttributeError, name);
+		return NULL;
+	}
+
+	PyObject* v;
+
+	if (magic) {
+		v = PyObjCCF_NewSpecial(type, buf);
+	} else {
+		v = pythonify_c_value(type, buf);
+	}
+
+	if (v == NULL) {
+		return NULL;
+	}
+	return v;
+}
+
 
 static PyMethodDef mod_methods[] = {
 	{
 
 #endif /* PyObjC_BUILD_RELEASE >= 1006 */
 
+	{ "_loadConstant", (PyCFunction)PyObjC_LoadConstant,
+		METH_VARARGS|METH_KEYWORDS, "(PRIVATE)" },
+
 	{ 0, 0, 0, 0 } /* sentinel */
 };
 

pyobjc-core/Modules/objc/objc_util.h

 int PyObjCUtil_Init(PyObject* module);
 
 void PyObjCErr_FromObjC(NSException* localException);
-void PyObjCErr_ToObjC(void);
+void PyObjCErr_ToObjC(void) __attribute__((__noreturn__));
 
-void PyObjCErr_ToObjCWithGILState(PyGILState_STATE* state);
+void PyObjCErr_ToObjCWithGILState(PyGILState_STATE* state) __attribute__((__noreturn__));
 
 NSException* PyObjCErr_AsExc(void);
 

pyobjc-core/Modules/objc/objc_util.m

 		}
 
 		assert(eltsize != 0);
+		if (eltsize == 0) {
+			PyErr_SetString(PyExc_ValueError, "array.array with elements without a size");
+			return -1;
+		}
 		if ((bufsize % eltsize) != 0) {
 			PyErr_SetString(PyExc_ValueError, 
 					"Badly shaped array.array");

pyobjc-core/Modules/objc/parsexml.m

 			av = PyObjC_IntFromLong(idx++);
 			if (av == NULL) {
 				Py_DECREF(a);
-				Py_DECREF(av);
 				Py_DECREF(result);
 				return NULL;
 			}
 				v = PyObjC_IntFromLong(idx++);
 				if (v == NULL) {
 					Py_DECREF(d);
-					Py_DECREF(v);
 					Py_DECREF(result);
 					return NULL;
 				}
 
 	metadata = PyObjC_InternValue(metadata);
 	if (metadata == NULL) {
-		Py_DECREF(nm);
-		Py_DECREF(metadata);
-		Py_DECREF(arguments);
-		Py_DECREF(siglist);
+		goto error;
 	}
 
 	v = PyObjCFunc_New(nm, function, PyBytes_AsString(signature), Py_None, metadata);
 	return 0;
 
 error:
-	Py_DECREF(siglist);
-	Py_DECREF(arguments);
-	Py_DECREF(metadata);
+	Py_XDECREF(siglist);
+	Py_XDECREF(arguments);
+	Py_XDECREF(metadata);
 	xmlFree(name);
 	return -1;
 }

pyobjc-core/Modules/objc/pointer-support.m

 FindWrapper(const char* signature)
 {
 	Py_ssize_t i;
-	Py_ssize_t len;
-
-	len = strlen(signature);
 
 	for (i = 0; i < item_count; i++) {
 		if (strncmp(signature, items[i].signature, items[i].offset) == 0) {