Commits

Bob Ippolito committed 838f9c7

NSStream signature fixes
NSInputStream helpers (replacing the old)
NSOutputStream helpers
Fixed some typos, more apple event support

Comments (0)

Files changed (7)

pyobjc/Lib/AppKit/__init__.py

 """
 
 import Foundation
-import _AppKitSignatures 
+import _AppKitSignatures
 import objc as _objc
 
 # Import contansts and global functions.
 
 
 #
-# (informal) protocols eported for b/w compatibility
+# (informal) protocols exported for b/w compatibility
 #
 from protocols import NSAccessibility, NSChangeSpelling, NSColorPickingCustom, \
                        NSColorPickingDefault, NSComboBoxCellDataSource, NSComboBoxDataSource, \

pyobjc/Lib/objc/_FoundationSignatures.py

 setSignatureForSelector("NSScanner", "scanString:intoString:", "c@:@o^@")
 setSignatureForSelector("NSScanner", "scanUpToCharactersFromSet:intoString:", "c@:@o^@")
 setSignatureForSelector("NSScanner", "scanUpToString:intoString:", "c@:@o^@")
+setSignatureForSelector("NSStream", "getStreamsToHost:port:inputStream:outputStream:", "v@:@io^@o^@")
 setSignatureForSelector("NSString", "completePathIntoString:caseSensitive:matchesIntoArray:filterTypes:", "Io^@co^@@")
 setSignatureForSelector("NSString", "getLineStart:end:contentsEnd:forRange:", "v@:o^Io^Io^I{_NSRange=II}")
 setSignatureForSelector("NSMutableAttributedString", "readFromURL:options:documentAttributes:", "c@:@@o^@")

pyobjc/Modules/Foundation/_Foundation.m

 #include "_FoundationMapping_NSData.m"
 #include "_FoundationMapping_NSDictionary.m"
 #include "_FoundationMapping_NSIndexSet.m"
-#include "_FoundationMapping_NSInputStream.m"
 #include "_FoundationMapping_NSMutableArray.m"
 #include "_FoundationMapping_NSNetService.m"
 #include "_FoundationMapping_NSScriptObjectSpecifier.m"
 #include "_FoundationMapping_NSSet.m"
 #include "_FoundationMapping_NSString.m"
+#include "_FoundationMapping_NSStream.m"
 
 
 void init_Foundation(void);
 	if (_pyobjc_install_NSData() != 0) return;
 	if (_pyobjc_install_NSDictionary() != 0) return;
 	if (_pyobjc_install_NSIndexSet() != 0) return;
-	if (_pyobjc_install_NSInputStream() != 0) return;
 	if (_pyobjc_install_NSMutableArray() != 0) return;
 	if (_pyobjc_install_NSNetService() != 0) return;
 	if (_pyobjc_install_NSScriptObjectSpecifier() != 0) return;
 	if (_pyobjc_install_NSSet() != 0) return;
 	if (_pyobjc_install_NSString() != 0) return;
+	if (_pyobjc_install_NSStream() != 0) return;
 }

pyobjc/Modules/Foundation/_FoundationMapping_NSAppleEventDescriptor.m

 /*
- * Special wrappers for NSAppleEventDescriptor methods with 'difficult' 
- * arguments.
+ * NSAppleEventDescriptor mappings for difficult methods:
  *
- * - aeDesc
- * - initWithAEDescNoCopy:
- *
- * XXX - should also do these
  * + descriptorWithDescriptorType:bytes:length:
  * - initWithDescriptorType:bytes:length:
+ * - initWithAEDescNoCopy:
+ * - aeDesc
+ * 
+ * TODO:
+ * + appleEventWithEventClass:eventID:targetDescriptor:returnID:transactionID:
+ * + descriptorWithDescriptorType:data:
+ * + descriptorWithEnumCode:
+ * + descriptorWithTypeCode:
+ * - initWithDescriptorType:data:
+ * - initWithEventClass:eventID:targetDescriptor:returnID:transactionID:
+ * - descriptorType
+ * - coerceToDescriptorType
+ * - enumCodeValue
+ * - typeCodeValue
+ * - descriptorForKeyword:
+ * - keywordForDescriptorAtIndex:
+ * - removeDescriptorWithKeyword:
+ * - removeParamDescriptorWithKeyword:
+ * - setDescriptor:forKeyword:
+ * - attributeDescriptorForKeyword:
+ * - setAttributeDescriptor:forKeyword:
+ * - eventClass
+ * - eventID
+ * - keywordForDescriptorAtIndex:
+ * 
  */
 #include <Python.h>
 #include <Foundation/Foundation.h>
 #include "pyobjc-api.h"
 #include "pymactoolbox.h"
 
+static PyObject* call_NSAppleEventDescriptor_initWithDescriptorType_bytes_length_(
+	PyObject* method, PyObject* self, PyObject* arguments)
+{
+	char*     bytes;
+	int       bytes_len;
+	int       len;
+	PyObject* result;
+	struct objc_super super;
+	id        objc_result;
+	OSType    descriptorType;
+
+	/* mostly copied from similar NSData mapping */
+	if  (!PyArg_ParseTuple(arguments, "O&t#i", PyMac_GetOSType, &descriptorType, &bytes, &bytes_len, &len)) {
+		return NULL;
+	}
+
+	if (bytes_len < len) {
+		PyErr_SetString(PyExc_ValueError, "Not enough bytes in data");
+		return NULL;
+	}
+
+	NS_DURING
+		PyObjC_InitSuper(&super,
+			PyObjCSelector_GetClass(method),
+			PyObjCObject_GetObject(self));
+
+		objc_result = objc_msgSendSuper(&super,
+				PyObjCSelector_GetSelector(method),
+				bytes, len);
+		result = PyObjC_IdToPython(objc_result);
+
+		/* XXX Ronald: If you try to use the result of 
+		 * PyObjCObject_GetObject(self) after the call to objc_msgSend 
+		 * it will crash with large enough values of len (>=32). 
+		 * Appearently the original self is recycled during the init.
+		 */
+		if (self != result) {
+			PyObjCObject_ClearObject(self);
+		}
+	NS_HANDLER
+		PyObjCErr_FromObjC(localException);
+		result = NULL;
+	NS_ENDHANDLER
+
+	return result;
+}
+
+static PyObject* call_NSAppleEventDescriptor_descriptorWithDescriptorType_bytes_length_(
+        PyObject* method, PyObject* self, PyObject* arguments)
+{
+	char*     bytes;
+	int       bytes_len;
+	int       len;
+	PyObject* result;
+	struct objc_super super;
+	id        objc_result;
+	OSType    descriptorType;
+
+	/* mostly copied from similar NSData mapping */
+	if  (!PyArg_ParseTuple(arguments, "O&t#i", PyMac_GetOSType, &descriptorType, &bytes, &bytes_len, &len)) {
+		return NULL;
+	}
+
+	if (bytes_len < len) {
+		PyErr_SetString(PyExc_ValueError, "Not enough bytes in data");
+		return NULL;
+	}
+
+	NS_DURING
+		PyObjC_InitSuperCls(&super,
+			PyObjCSelector_GetClass(method),
+			PyObjCClass_GetClass(self));
+
+		objc_result = objc_msgSendSuper(&super,
+				PyObjCSelector_GetSelector(method),
+				descriptorType, bytes, len);
+		result = PyObjC_IdToPython(objc_result);
+
+		/* XXX Ronald: If you try to use the result of 
+		 * PyObjCObject_GetObject(self) after the call to objc_msgSend 
+		 * it will crash with large enough values of len (>=32). 
+		 * Appearently the original self is recycled during the init.
+		 */
+		if (self != result) {
+			PyObjCObject_ClearObject(self);
+		}
+	NS_HANDLER
+		PyObjCErr_FromObjC(localException);
+		result = NULL;
+	NS_ENDHANDLER
+
+	return result;
+}
+
 static PyObject*
 call_NSAppleEventDescriptor_initWithAEDescNoCopy_(
 	PyObject* method, PyObject* self, PyObject* arguments)
 {
-    AEDesc *theEvent;
+	AEDesc *theEvent;
 	id res;
 	struct objc_super super;
 	PyObject* retVal;
 
-    if ( (theEvent = PyMem_NEW(AEDesc, 1)) == NULL ) {
-        PyErr_NoMemory();
-        return 0;
-    }
+	if ( (theEvent = PyMem_NEW(AEDesc, 1)) == NULL ) {
+		PyErr_NoMemory();
+		return 0;
+	}
 	if (!PyArg_ParseTuple(arguments, "O&", AEDesc_Convert, theEvent)) { 
         PyMem_DEL(theEvent);
 		return NULL;
 
 	if (PyObjC_RegisterMethodMapping(
 		classNSAppleEventDescriptor,
+        @selector(initWithDescriptorType:bytes:length:),
+		call_NSAppleEventDescriptor_initWithDescriptorType_bytes_length_,
+		PyObjCUnsupportedMethod_IMP) < 0) {
+		return -1;
+	}
+
+	if (PyObjC_RegisterMethodMapping(
+		classNSAppleEventDescriptor,
+        @selector(descriptorWithDescriptorType:bytes:length:),
+		call_NSAppleEventDescriptor_descriptorWithDescriptorType_bytes_length_,
+		PyObjCUnsupportedMethod_IMP) < 0) {
+		return -1;
+	}
+
+	if (PyObjC_RegisterMethodMapping(
+		classNSAppleEventDescriptor,
 		@selector(aeDesc),
 		call_NSAppleEventDescriptor_aeDesc,
 		PyObjCUnsupportedMethod_IMP) < 0) {

pyobjc/Modules/Foundation/_FoundationMapping_NSData.m

  * NSData mappings for 'difficult' methods:
  *
  * -initWithBytes:length:
- * +dataWithBytes:lenght:
+ * +dataWithBytes:length:
  * -bytes
  * -mutableBytes
  *

pyobjc/Modules/Foundation/_FoundationMapping_NSInputStream.m

-/*
- * Special wrappers for NSInputStream methods with 'difficult' arguments.
- *
- * -getBuffer:length:	[call]
- *
- * PLATFORM: MacOS X 10.3
- */
-#include <Python.h>
-#include <Foundation/Foundation.h>
-#include "pyobjc-api.h"
-
-static PyObject*
-call_NSInputStream_getBuffer_length_(PyObject* method,
-	PyObject* self, PyObject* arguments)
-{
-	struct objc_super super;
-	char* buf;
-	int   buflen;
-	BOOL res;
-	PyObject* retVal;
-	PyObject* v;
-
-	if (!PyArg_ParseTuple(arguments, "")) {
-		return NULL;
-	}
-
-	NS_DURING
-		PyObjC_InitSuper(&super,
-			PyObjCSelector_GetClass(method),
-			PyObjCObject_GetObject(self));
-
-		res = (BOOL)(int)objc_msgSendSuper(&super, @selector(getBuffer:length:),
-			&buf, &buflen);
-	NS_HANDLER
-		PyObjCErr_FromObjC(localException);
-		buf = NULL;
-		buflen = -1;
-	NS_ENDHANDLER
-
-	if (buf == NULL && PyErr_Occurred()) {
-		return NULL;
-	}
-
-	retVal = PyTuple_New(3);
-	if (retVal == NULL) {
-		return NULL;
-	}
-
-	v = PyObjC_ObjCToPython(@encode(BOOL), &res);
-	if (v == NULL) {
-		return NULL;
-	}
-	PyTuple_SET_ITEM(retVal, 0, v);
-
-	v = PyBuffer_FromReadWriteMemory(buf, buflen);
-	if (v == NULL) {
-		return NULL;
-	}
-	PyTuple_SET_ITEM(retVal, 1, v);
-
-	v = PyInt_FromLong(buflen);
-	if (v == NULL) {
-		return NULL;
-	}
-	PyTuple_SET_ITEM(retVal, 2, v);
-
-	return retVal;
-}
-
-
-static int 
-_pyobjc_install_NSInputStream(void)
-{
-	Class classNSInputStream = objc_lookUpClass("NSInputStream");
-	if (classNSInputStream == NULL) {
-		return 0;
-	}
-
-	if (PyObjC_RegisterMethodMapping(
-		classNSInputStream,
-		@selector(getBuffer:length:),
-		call_NSInputStream_getBuffer_length_,
-		PyObjCUnsupportedMethod_IMP) < 0) {
-
-		return -1;
-	}
-
-	return 0;
-}

pyobjc/Modules/Foundation/_FoundationMapping_NSStream.m

+/*
+ * NSInputStream mappings for 'difficult' methods:
+ *
+ * -read:maxLength:
+ * -getBuffer:length:
+ * 
+ * NSOutputStream mappings for 'difficult' methods:
+ *
+ * -write:maxLength:
+ * 
+ * Unsupported Methods
+ * 
+ * initToBuffer:capacity:
+ * outputStreamToBuffer:capacity:
+ *
+ */
+#include <Python.h>
+#include <Foundation/Foundation.h>
+#include "pyobjc-api.h"
+
+static PyObject* call_NSOutputStream_write_maxLength_(
+		PyObject* method, PyObject* self, PyObject* arguments)
+{
+	void*		bytes;
+	int			bytes_len;
+	PyObject*	result;
+	struct objc_super super;
+
+	if (!PyArg_ParseTuple(arguments, "s#", &bytes, &bytes_len)) {
+		return NULL;
+	}
+
+	NS_DURING
+		PyObjC_InitSuper(&super,
+			PyObjCSelector_GetClass(method),
+			PyObjCObject_GetObject(self));
+
+		result = PyInt_FromLong((long)objc_msgSendSuper(&super, 
+				PyObjCSelector_GetSelector(method), bytes, bytes_len));
+		
+	NS_HANDLER
+		PyObjCErr_FromObjC(localException);
+		Py_DECREF(result);
+		result = NULL;
+	NS_ENDHANDLER
+
+	return result;
+}
+
+
+static PyObject* call_NSInputStream_read_maxLength_(
+		PyObject* method, PyObject* self, PyObject* arguments)
+{
+	char* buf;
+	int   bytes_len;
+	int	  bytes_read;
+	PyObject*	result;
+	struct objc_super super;
+
+	if (!PyArg_ParseTuple(arguments, "i", &bytes_len)) {
+		return NULL;
+	}
+
+    result = PyString_FromStringAndSize((char *)0, bytes_len);
+	if (result == NULL)
+		return NULL;
+	
+	NS_DURING
+		PyObjC_InitSuper(&super,
+			PyObjCSelector_GetClass(method),
+			PyObjCObject_GetObject(self));
+
+		bytes_read = (int)objc_msgSendSuper(&super, 
+				PyObjCSelector_GetSelector(method), 
+				PyString_AS_STRING(result), bytes_len);
+		
+		if (bytes_read != bytes_len) {
+			if (_PyString_Resize(&result, bytes_read) < 0) {
+				return NULL;
+			}
+		}
+
+	NS_HANDLER
+		PyObjCErr_FromObjC(localException);
+		Py_XDECREF(result);
+		result = NULL;
+	NS_ENDHANDLER
+
+	return result;
+}
+
+static PyObject* call_NSInputStream_getBytes_length_(
+		PyObject* method, PyObject* self, PyObject* arguments)
+{
+	const void* bytes;
+	unsigned    bytes_len;
+	PyObject*	result;
+	struct objc_super super;
+
+	if (!PyArg_ParseTuple(arguments, "")) {
+		return NULL;
+	}
+
+	NS_DURING
+		PyObjC_InitSuper(&super,
+			PyObjCSelector_GetClass(method),
+			PyObjCObject_GetObject(self));
+
+		if (objc_msgSendSuper(&super, 
+				PyObjCSelector_GetSelector(method), &bytes, &bytes_len)) {
+			result = PyBuffer_FromMemory((void*)bytes, bytes_len);
+		} else {
+			Py_INCREF(Py_None);
+			result = Py_None;
+		}
+
+	NS_HANDLER
+		PyObjCErr_FromObjC(localException);
+		result = NULL;
+	NS_ENDHANDLER
+
+	return result;
+}
+
+
+static int 
+_pyobjc_install_NSStream(void)
+{
+	Class classNSInputStream = objc_lookUpClass("NSInputStream");
+	Class classNSOutputStream = objc_lookUpClass("NSOutputStream");
+
+	if (classNSInputStream != NULL) {
+
+		if (PyObjC_RegisterMethodMapping(classNSInputStream, 
+				 @selector(read:maxLength:),
+				 call_NSInputStream_read_maxLength_,
+				 PyObjCUnsupportedMethod_IMP) < 0 ) {
+			return -1;
+		}
+
+		if (PyObjC_RegisterMethodMapping(classNSInputStream, 
+				 @selector(getBytes:length:),
+				 call_NSInputStream_getBytes_length_,
+				 PyObjCUnsupportedMethod_IMP) < 0 ) {
+			return -1;
+		}
+	}
+	if (classNSOutputStream != NULL) {
+		if (PyObjC_RegisterMethodMapping(classNSOutputStream, 
+				 @selector(write:maxLength:),
+				 call_NSOutputStream_write_maxLength_,
+				 PyObjCUnsupportedMethod_IMP) < 0 ) {
+			return -1;
+		}
+		if (PyObjC_RegisterMethodMapping(
+				classNSOutputStream,
+				@selector(outputStreamToBuffer:capacity:),
+				PyObjCUnsupportedMethod_Caller,
+				PyObjCUnsupportedMethod_IMP) < 0) {
+			return -1;
+		}
+		if (PyObjC_RegisterMethodMapping(
+				classNSOutputStream,
+				@selector(initToBuffer:capacity:),
+				PyObjCUnsupportedMethod_Caller,
+				PyObjCUnsupportedMethod_IMP) < 0) {
+			return -1;
+		}
+	}
+
+	return 0;
+}
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.