Commits

Ronald Oussoren committed 8727efb

- Add test-cases that check the DeprecationWarnings for the NSAutoreleasePool
addition and syncFromNSString
- Silence these deprecation-warnings in the regular tests
- Make sure setting DeprecationWarnings to errors actually work in the
NSAutoreleasePool additions.
- Add more missing function wrappers

Comments (0)

Files changed (10)

pyobjc/Lib/AppKit/test/test_nsview.py

     def knowsPageRange_(self):
         return  objc.YES, (1, 10)
 
+    def rectForPage_(self, page):
+        return ((1,1),(2,2))
+
 class TestNSView (unittest.TestCase):
 
     def test_knowsPageRange(self):
         method = ObjCTestNSView_KnowPageRange.knowsPageRange_
         self.assertEquals(method.signature, "c@:o^{_NSRange=II}")
 
+    def test_rectForPage(self):
+        method = ObjCTestNSView_KnowPageRange.rectForPage_
+        self.assertEquals(method.signature, "{_NSRect={_NSPoint=ff}{_NSSize=ff}}12@0:4i8")
+
 if __name__ == "__main__":
     unittest.main()

pyobjc/Lib/Foundation/test/test_globals.py

 import unittest
+import sys
+import struct
 
 import Foundation
 
 class GlobalFunctionTest (unittest.TestCase):
+    if sys.platform == 'darwin':
+        def testNSFileTypeForHFSTypeCode(self):
+            self.assertEquals("'rtfx'",
+                    Foundation.NSFileTypeForHFSTypeCode('rtfx'))
     
+            # The cannonical representation for four-character-codes in python
+            # is a string of 4 characters, but at least some ObjC API's return
+            # longs (because these methods haven't been wrapped correctly yet).
+            # NSFileTypeForHFSTypeCode therefore also accepts integers.
+            fourchar = struct.unpack('i', 'rtfx')[0]
+            self.assertEquals("'rtfx'",
+                    Foundation.NSFileTypeForHFSTypeCode(fourchar))
+
+        def testNSHFSTypeCodeFromFileType(self):
+            self.assertEquals("rtfx",
+                    Foundation.NSHFSFTypeCodeFromFileType("'rtfx'"))
+
+
     def testMakeNSRect(self):
         self.assert_(hasattr(Foundation, 'NSMakeRect'))
 
 
         self.assertRaises(TypeError, Foundation.NSMakeRect, 1.0, 2.0, 3.0, '4')
 
+    def test_NSDivideRect(self):
+        rect1 = Foundation.NSMakeRect(1.0, 2.0, 3.0, 4.0)
+        
+        slice, rem = Foundation.NSDivideRect(rect1, 0.5, Foundation.NSMinXEdge)
+        self.assertEquals(slice, ((1.0, 2.0), (0.5, 4.0)))
+        self.assertEquals(rem,   ((1.5, 2.0), (2.5, 4.0)))
+
+        slice, rem = Foundation.NSDivideRect(rect1, 0.5, Foundation.NSMinYEdge)
+        self.assertEquals(slice, ((1.0, 2.0), (3.0, 0.5)))
+        self.assertEquals(rem,   ((1.0, 2.5), (3.0, 3.5)))
+
     def testMisc(self):
         self.assert_(hasattr(Foundation, 'NSLogPageSize'))
         self.assert_(hasattr(Foundation, 'NSRangeFromString'))

pyobjc/Lib/Foundation/test/test_nsautoreleasepool.py

         finally:
             del warnings.filters[0]
 
+    def testDeprecation(self):
+        import warnings
+        warnings.filterwarnings('error', category=DeprecationWarning)
+
+        try:
+            self.assertRaises(DeprecationWarning, NSAutoreleasePool.pyobjcPushPool)
+        finally:
+            del warnings.filters[0]
+
+
 if __name__ == '__main__':
     unittest.main( )

pyobjc/Lib/Foundation/test/test_nsstring.py

         ocStr.appendString_(" world")
         self.assertEquals(pyStr, "hello")
 
-        pyStr.syncFromNSString()
+        import warnings
+        warnings.filterwarnings('ignore', category=DeprecationWarning)
+        try:
+            pyStr.syncFromNSString()
+        finally:
+            del warnings.filters[0]
 
         self.assertEquals(pyStr, "hello world")
 
+    def testDeprecation(self):
+        import warnings
+
+        o = NSMutableString.stringWithString_("hello")
+        warnings.filterwarnings('error', category=DeprecationWarning)
+        try:
+            self.assertRaises(DeprecationWarning, o.syncFromNSString)
+        finally:
+            del warnings.filters[0]
+
 class TestPickle(unittest.TestCase):
     """
     Testcases for pickling of Objective-C strings. Those are pickled as

pyobjc/Modules/AppKit/_AppKit.m

 
 	if (arrayToken == -1) return NULL;
 
-	NSRectFillList(rects, rectCount);
+	NS_DURING
+		NSRectFillList(rects, rectCount);
+	NS_HANDLER
+		PyObjCErr_FromObjC(localException);
+	NS_ENDHANDLER
 
 	PyObjC_FreeCArray(arrayToken, rects);
 
+	if (PyErr_Occurred()) {
+		return NULL;
+	}
+
+	Py_INCREF(Py_None);
+	return Py_None; 
+}
+
+static int
+Convert_NSCompositingOperation(PyObject* arg, void* out)
+{
+	int r;
+
+	r = PyObjC_PythonToObjC(@encode(NSCompositingOperation), arg, out);
+	if (r == -1) {
+		return 0;
+	}
+	return 1;
+}
+
+static PyObject*
+objc_NSRectFillListUsingOperation(PyObject* self __attribute__((__unused__)), PyObject* args, PyObject* kwds)
+{
+static char* keywords[] = { "rects", "count", "operation", 0 };
+	PyObject* pyList;
+	PyObject* pyCount = NULL;
+	NSRect* rects;
+	int rectCount;
+	int arrayToken;
+	NSCompositingOperation operation;
+
+	if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOO&", keywords, &pyList, &pyCount, Convert_NSCompositingOperation, &operation)) {
+		return NULL;
+	}
+
+	arrayToken = PyObjC_PythonToCArray(
+		@encode(NSRect), pyList, pyCount, (void**)&rects, &rectCount);
+
+	if (arrayToken == -1) return NULL;
+
+	NS_DURING
+		NSRectFillListUsingOperation(rects, rectCount, operation);
+	NS_HANDLER
+		PyObjCErr_FromObjC(localException);
+	NS_ENDHANDLER
+
+	PyObjC_FreeCArray(arrayToken, rects);
+
+	if (PyErr_Occurred()) {
+		return NULL;
+	}
+
+	Py_INCREF(Py_None);
+	return Py_None; 
+}
+
+static PyObject*
+objc_NSRectFillListWithColors(PyObject* self __attribute__((__unused__)), PyObject* args, PyObject* kwds)
+{
+static char* keywords[] = { "rects", "colors", "count", 0 };
+	PyObject* pyList;
+	PyObject* pyColors;
+	PyObject* pyCount = NULL;
+	NSRect* rects;
+	id* colors;
+	int rectCount;
+	int colorCount;
+	int rectToken;
+	int colorToken;
+
+	if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|O", keywords, &pyList, &pyColors, &pyCount)) {
+		return NULL;
+	}
+
+	rectToken = PyObjC_PythonToCArray(
+		@encode(NSRect), pyList, pyCount, (void**)&rects, &rectCount);
+
+	if (rectToken == -1) return NULL;
+
+	colorToken = PyObjC_PythonToCArray(
+		@encode(id), pyColors, pyCount, (void**)&colors, &colorCount);
+
+	if (colorToken == -1)  {
+		PyObjC_FreeCArray(rectToken, rects);
+		return NULL;
+	}
+
+	if (colorCount != rectCount) {
+		PyErr_Format(PyExc_ValueError,
+				"Passing %d rects and %d colors",
+				rectCount, colorCount);
+		PyObjC_FreeCArray(rectToken, rects);
+		PyObjC_FreeCArray(colorToken, colors);
+		return NULL;
+	}
+		
+
+	NS_DURING
+		NSRectFillListWithColors(rects, colors, rectCount);
+	NS_HANDLER
+		PyObjCErr_FromObjC(localException);
+	NS_ENDHANDLER
+
+	PyObjC_FreeCArray(rectToken, rects);
+	PyObjC_FreeCArray(colorToken, colors);
+
+	if (PyErr_Occurred()) {
+		return NULL;
+	}
+
+	Py_INCREF(Py_None);
+	return Py_None; 
+}
+
+static PyObject*
+objc_NSRectFillListWithColorsUsingOperation(PyObject* self __attribute__((__unused__)), PyObject* args, PyObject* kwds)
+{
+static char* keywords[] = { "rects", "colors", "count", "operation", 0 };
+	PyObject* pyList;
+	PyObject* pyColors;
+	PyObject* pyCount = NULL;
+	NSRect* rects;
+	id* colors;
+	int rectCount;
+	int colorCount;
+	int rectToken;
+	int colorToken;
+	NSCompositingOperation operation;
+
+	if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOO&", keywords, &pyList, &pyColors, &pyCount, Convert_NSCompositingOperation, &operation)) {
+		return NULL;
+	}
+
+	rectToken = PyObjC_PythonToCArray(
+		@encode(NSRect), pyList, pyCount, (void**)&rects, &rectCount);
+
+	if (rectToken == -1) return NULL;
+
+	colorToken = PyObjC_PythonToCArray(
+		@encode(id), pyColors, pyCount, (void**)&colors, &colorCount);
+
+	if (colorToken == -1)  {
+		PyObjC_FreeCArray(rectToken, rects);
+		return NULL;
+	}
+
+	if (colorCount != rectCount) {
+		PyErr_Format(PyExc_ValueError,
+				"Passing %d rects and %d colors",
+				rectCount, colorCount);
+		PyObjC_FreeCArray(rectToken, rects);
+		PyObjC_FreeCArray(colorToken, colors);
+		return NULL;
+	}
+		
+
+	NS_DURING
+		NSRectFillListWithColorsUsingOperation(rects, colors, rectCount, operation);
+	NS_HANDLER
+		PyObjCErr_FromObjC(localException);
+	NS_ENDHANDLER
+
+	PyObjC_FreeCArray(rectToken, rects);
+	PyObjC_FreeCArray(colorToken, colors);
+
+	if (PyErr_Occurred()) {
+		return NULL;
+	}
+
+	Py_INCREF(Py_None);
+	return Py_None; 
+}
+
+static PyObject*
+objc_NSRectFillListWithGrays(PyObject* self __attribute__((__unused__)), PyObject* args, PyObject* kwds)
+{
+static char* keywords[] = { "rects", "grays", "count", 0 };
+	PyObject* pyList;
+	PyObject* pyGrays;
+	PyObject* pyCount = NULL;
+	NSRect* rects;
+	float* grays;
+	int rectCount;
+	int grayCount;
+	int rectToken;
+	int grayToken;
+
+	if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|O", keywords, &pyList, &pyGrays, &pyCount)) {
+		return NULL;
+	}
+
+	rectToken = PyObjC_PythonToCArray(
+		@encode(NSRect), pyList, pyCount, (void**)&rects, &rectCount);
+
+	if (rectToken == -1) return NULL;
+
+	grayToken = PyObjC_PythonToCArray(
+		@encode(float), pyGrays, pyCount, (void**)&grays, &grayCount);
+
+	if (grayToken == -1)  {
+		PyObjC_FreeCArray(rectToken, rects);
+		return NULL;
+	}
+
+	if (grayCount != rectCount) {
+		PyErr_Format(PyExc_ValueError,
+				"Passing %d rects and %d colors",
+				rectCount, grayCount);
+		PyObjC_FreeCArray(rectToken, rects);
+		PyObjC_FreeCArray(grayToken, grays);
+		return NULL;
+	}
+		
+
+	NS_DURING
+		NSRectFillListWithGrays(rects, grays, rectCount);
+	NS_HANDLER
+		PyObjCErr_FromObjC(localException);
+	NS_ENDHANDLER
+
+	PyObjC_FreeCArray(rectToken, rects);
+	PyObjC_FreeCArray(grayToken, grays);
+
+	if (PyErr_Occurred()) {
+		return NULL;
+	}
+
 	Py_INCREF(Py_None);
 	return Py_None; 
 }
 		"NSApplicationMain", 
 		(PyCFunction)objc_NSApplicationMain, 
 		METH_VARARGS|METH_KEYWORDS, 
-		NULL
+		"int NSApplicationMain(int argc, const char *argv[]);"
 	},
 	{ 
 		"NSApp", 
 		(PyCFunction)objc_NSApp, 
 		METH_VARARGS|METH_KEYWORDS, 
-		NULL
+		"NSApplication* NSApp(void);"
 	},
 	{ 
 		"NSCountWindows", 
 		(PyCFunction)objc_NSCountWindows, 
 		METH_VARARGS|METH_KEYWORDS, 
-		NULL
+		"void NSCountWindows(int *count);"
 	},
 	{ 
 		"NSCountWindowsForContext", 
 		(PyCFunction)objc_NSCountWindowsForContext, 
 		METH_VARARGS|METH_KEYWORDS, 
-		NULL
+		"void NSCountWindowsForContext(int context, int *count);"
 	},
 	{
 	        "NSRectFillList",
 		(PyCFunction)objc_NSRectFillList,
 		METH_VARARGS,
-		NULL
+		"void NSRectFillList(const NSRect *rects, int count);"
+	},
+	{
+	        "NSRectFillListWithColors",
+		(PyCFunction)objc_NSRectFillListWithColors,
+		METH_VARARGS,
+		"void NSRectFillListWithColors(const NSRect *rects, NSColor **colors, int count);"
+	},
+	{
+	        "NSRectFillListWithColorsUsingOperation",
+		(PyCFunction)objc_NSRectFillListWithColorsUsingOperation,
+		METH_VARARGS,
+		"void NSRectFillListWithColorsUsingOperation(const NSRect *rects, NSColor **colors, int count, NSCompositingOperation op)"
+	},
+	{
+	        "NSRectFillListWithGrays",
+		(PyCFunction)objc_NSRectFillListWithGrays,
+		METH_VARARGS,
+		"void NSRectFillListWithGrays(const NSRect *rects, const float *grays, int count);"
+	},
+	{
+	        "NSRectFillListUsingOperation",
+		(PyCFunction)objc_NSRectFillListUsingOperation,
+		METH_VARARGS,
+		"void NSRectFillListUsingOperation(const NSRect *rects, int count, NSCompositingOperation op);"
 	},
 	{
 	        "NSAvailableWindowDepths",
 		(PyCFunction)objc_NSAvailableWindowDepths,
 		METH_VARARGS,
-		NULL
+		"const NSWindowDepth *NSAvailableWindowDepths(void);"
 	},
 	{
 		"NSGetWindowServerMemory",
 		(PyCFunction)objc_NSGetWindowServerMemory,
 		METH_VARARGS,
-		NULL
+		"int NSGetWindowServerMemory(int context, int *virtualMemory, int *windowBackingMemory, NSString **windowDumpString);"
 	},
 
 	METHOD_TABLE_ENTRIES
 	if (_pyobjc_install_NSBitmap() < 0) return;
 	if (_pyobjc_install_NSBitmapImageRep() < 0) return;
 	if (_pyobjc_install_NSFont() < 0) return;
-    if (_pyobjc_install_NSGraphicsContext() < 0) return;
+	if (_pyobjc_install_NSGraphicsContext() < 0) return;
 	if (_pyobjc_install_NSLayoutManager() < 0) return;
 	if (_pyobjc_install_NSMatrix() < 0) return;
 	if (_pyobjc_install_NSMovie() < 0) return;

pyobjc/Modules/Foundation/NSAutoreleasePoolSupport.m

  * PyObjC couldn't be used to create autoreleasepools in the regular way,
  * that is no longer a problem.
  */
-#include <Python.h>
-
-#import <Foundation/Foundation.h>
-
 static NSString *_threadPoolIdentifier = @"PyObjC:  NSThread AutoreleasePool Identifier.";
 
 @interface NSAutoreleasePool(PyObjCPushPopSupport)
 	NSMutableDictionary *threadDictionary = [[NSThread currentThread] threadDictionary];
 	NSMutableArray *poolStack;
 
-
 	poolStack = [threadDictionary objectForKey: _threadPoolIdentifier];
 	if (!poolStack) {
 		poolStack = [NSMutableArray array];
 	PyErr_Warn(PyExc_DeprecationWarning, 
 		"NSAutoreleasePool.pyobjcPushPool() is deprecated: Use NSAutoreleasePool.alloc().init() instead");
 	if (PyErr_Occurred()) {
-		PyErr_Print();
+		PyObjCErr_ToObjC();
 	}
 
 	p = [[NSAutoreleasePool alloc] init];

pyobjc/Modules/Foundation/_Foundation.m

 #include "pyobjc-api.h"
 #include "wrapper-const-table.h"
 
+#include "NSAutoreleasePoolSupport.m"
+
 #include "_Fnd_Functions.inc"
 
-#define NSLocalizedString_doc 0
-static PyObject* objc_NSLocalizedString(PyObject* self __attribute__((__unused__)), PyObject* args, PyObject* kwds)
-{
-static	char* keywords[] = { "key", "comment", NULL };
-	PyObject*  result;
-	NSString* oc_result;
-	NSString* oc_key;
-	NSString* oc_comment;
-
-	if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&O&:NSLocalizedString",
-			keywords, PyObjCObject_Convert, &oc_key, 
-			PyObjCObject_Convert, &oc_comment)) {
-		return NULL;
-	}
-
-	oc_result = NSLocalizedString(oc_key, oc_comment);
-
-	result = PyObjC_IdToPython(oc_result);
-	return result;
-}
-
-#define NSLocalizedStringFromTable_doc 0
-static PyObject* objc_NSLocalizedStringFromTable(PyObject* self __attribute__((__unused__)), PyObject* args, PyObject* kwds)
-{
-static	char* keywords[] = { "key", "tableName", "comment", NULL };
-	PyObject*  result;
-	NSString* oc_result;
-	NSString* oc_key;
-	NSString* oc_tableName;
-	NSString* oc_comment;
-
-	if (!PyArg_ParseTupleAndKeywords(args, kwds, 
-			"O&O&O&:NSLocalizedStringFromTable",
-			keywords, 
-			PyObjCObject_Convert, &oc_tableName, 
-			PyObjCObject_Convert, &oc_key, 
-			PyObjCObject_Convert, &oc_comment)) {
-		return NULL;
-	}
-
-	oc_result = NSLocalizedStringFromTable(
-		oc_key, oc_tableName, oc_comment);
-	result = PyObjC_IdToPython(oc_result);
-	return result;
-}
-
-#define NSLocalizedStringFromTableInBundle_doc 0
-static PyObject* objc_NSLocalizedStringFromTableInBundle(PyObject* self __attribute__((__unused__)), PyObject* args, PyObject* kwds)
-{
-static	char* keywords[] = { "key", "tableName", "comment", "bundle", NULL };
-	PyObject* result;
-	NSString* oc_result;
-	NSString* oc_key;
-	id        oc_bundle;
-	NSString* oc_tableName;
-	NSString* oc_comment;
-
-	if (!PyArg_ParseTupleAndKeywords(args, kwds, 
-			"O&O&O&O:NSLocalizedStringFromTableInBundle",
-			keywords, 
-			PyObjCObject_Convert, &oc_tableName, 
-			PyObjCObject_Convert, &oc_key, 
-			PyObjCObject_Convert, &oc_comment, 
-			PyObjCObject_Convert, &oc_bundle)) {
-		return NULL;
-	}
-
-	oc_result = NSLocalizedStringFromTableInBundle(
-			oc_key, oc_tableName, oc_bundle, oc_comment);
-
-	result = PyObjC_IdToPython(oc_result);
-	return result;
-}
-
 #ifdef MACOSX
 
 
 	OSType hfsTypeCode;
 
 	if (!PyArg_ParseTupleAndKeywords(args, kwds, 
-			"O&:NSFileTypeForHFSTypeCode",
-			keywords, PyMac_GetOSType, &hfsTypeCode)) {
-		return NULL;
+			"i:NSFileTypeForHFSTypeCode",
+			keywords, &hfsTypeCode)) {
+		PyErr_Clear();
+		if (!PyArg_ParseTupleAndKeywords(args, kwds, 
+				"O&:NSFileTypeForHFSTypeCode",
+				keywords, PyMac_GetOSType, &hfsTypeCode)) {
+			return NULL;
+		}
 	}
 	
 	NS_DURING
 	},
 
 #endif /* MACOSX */
-	{ 
-		"NSLocalizedString", 
-		(PyCFunction)objc_NSLocalizedString, 
-		METH_VARARGS|METH_KEYWORDS, 
-		NSLocalizedString_doc 
-	},
-	{ 
-		"NSLocalizedStringFromTable", 
-		(PyCFunction)objc_NSLocalizedStringFromTable, 
-		METH_VARARGS|METH_KEYWORDS, 
-		NSLocalizedStringFromTable_doc 
-	},
-	{ 
-		"NSLocalizedStringFromTableInBundle", 
-		(PyCFunction)objc_NSLocalizedStringFromTableInBundle, 
-		METH_VARARGS|METH_KEYWORDS, 
-		NSLocalizedStringFromTable_doc 
-	},
 	{
 		"NSDivideRect",
 		(PyCFunction)objc_NSDivideRect,

pyobjc/Modules/objc/objc_util.m

 	if (*typestr++ != _C_ARY_B) {
 		return '\0';
 	}
-	while (isdigit(typestr++)) 
+	while (isdigit(*typestr)) typestr++;
 
 	if (*typestr == _C_ARY_E) {
 		return '\0';

pyobjc/Scripts/CodeGenerators/cocoa_generator.py

                 'FOUNDATION_STATIC_INLINE', 
                 FOUNDATION_IGNORE_LIST)
 
+
         func_builder.INT_ALIASES.extend([
             'NSSearchPathDomainMask', 'NSCalculationError',
             'NSComparisonResult', 'NSInsertionPosition',
 
         funcs = func_builder.process_list(fd , file('build/codegen/Foundation.prototypes'))
         funcs2 = func_builder.process_list(fd , file('build/codegen/Foundation.prototype2'))
-        func_builder.gen_method_table_entries(fd, funcs + funcs2)
+
+        # These are macro's
+        funcs3 = func_builder.process_list(fd, [
+            'NSString* NSLocalizedString(NSString* key, NSString* comment);',
+            'NSString* NSLocalizedStringFromTable(NSString* key, NSString* tableName, NSString* comment);',
+            'NSString* NSLocalizedStringFromTableInBundle(NSString* key, NSString* tableName, NSString* comment, NSBundle* bunlde);',
+        ])
+        func_builder.gen_method_table_entries(fd, funcs + funcs2 + funcs3)
         fd = None
         for s in structs:
             del func_builder.SIMPLE_TYPES[s]
                 APPKIT_PREFIX, 
                 APPKIT_IGNORE_LIST)
 
+        func_builder.FUNC_MAP['NSShowAnimationEffect'] = BeginSheetMapper
+
         fd = dupfile('build/codegen/_App_Functions.inc', 'w')
         structs = ['NSAffineTransformStruct', 'NSRect', 'NSPoint']
         for s in structs:
 
         fd.write('typedef void* PYOBJC_VOIDPTR;\n')
         funcs = func_builder.process_list(fd, file('build/codegen/AppKit.prototypes'))
-        func_builder.gen_method_table_entries(fd, funcs)
+
+        funcs2 = func_builder.process_list(fd, [
+
+                # This is a very ugly macro, it access 'glyphs' in the enclosing
+                # block ???
+                #'void NSGlyphInfoAtIndex(int IX);',
+            ])
+
+        func_builder.gen_method_table_entries(fd, funcs + funcs2)
         for s in structs:
             del func_builder.SIMPLE_TYPES[s]
 
           Extension("_Foundation",
                     [
                         "Modules/Foundation/_Foundation.m",
-                        "Modules/Foundation/NSAutoreleasePoolSupport.m"
                     ],
                     extra_compile_args=[
                         "-IModules/objc",