[PyObjC-svn] r2464 - in trunk/pyobjc/pyobjc-core: Lib/objc Modules/objc PyObjCTest

[email protected] Wed, 14 Apr 2010 16:45:45 -0500
Newsgroups gmane.comp.python.pyobjc.cvs
Message-ID <[email protected]>
Author: ronaldoussoren
Date: Wed Apr 14 16:45:45 2010
New Revision: 2464

Log:
Initial implementation of tests of the list/tuple
API for NSArray's.

All tuple-related tests pass, I'm still working on
the list-related ones.

Known issues:
* Running the tests causes a crash in one test, 
  I haven't investigated this yet.
* NSArray.count is different from list.count,
  due to the existence of 'NSArray -count' in
  ObjC. I haven't though about a solution to this
  yet.
* Need to write documentation, especially about
  differences between native sequences and the Cocoa
  ones.


Added:
   trunk/pyobjc/pyobjc-core/PyObjCTest/test3_array_interface.py   (contents, props changed)
Modified:
   trunk/pyobjc/pyobjc-core/Lib/objc/_convenience.py
   trunk/pyobjc/pyobjc-core/Modules/objc/OC_PythonObject.m

Modified: trunk/pyobjc/pyobjc-core/Lib/objc/_convenience.py
==============================================================================
--- trunk/pyobjc/pyobjc-core/Lib/objc/_convenience.py	(original)
+++ trunk/pyobjc/pyobjc-core/Lib/objc/_convenience.py	Wed Apr 14 16:45:45 2010
@@ -338,15 +338,44 @@
     ('extend', extend_addObjectsFromArray_),
 )
 
-def index_indexOfObject_(self, item):
+_index_sentinel=object()
+def index_indexOfObject_inRange_(self, item, start=0, stop=_index_sentinel):
     from Foundation import NSNotFound
-    res = self.indexOfObject_(container_wrap(item))
-    if res == NSNotFound:
-        raise ValueError, "%s.index(x): x not in list" % (type(self).__name__,)
+    if start == 0 and stop is _index_sentinel:
+        res = self.indexOfObject_(container_wrap(item))
+        if res == NSNotFound:
+            raise ValueError("%s.index(x): x not in list" % (type(self).__name__,))
+    else:
+        l = len(self)
+        if start < 0:
+            start = l + start
+            if start < 0:
+                start = 0
+
+        if stop is not _index_sentinel:
+            if stop < 0:
+                stop = l + stop
+                if stop < 0:
+                    stop = 0
+        else:
+            stop = l
+
+        if stop <= start:
+            ln = 0 
+        else:
+            ln = stop - start
+
+
+        if ln == 0:
+            raise ValueError("%s.index(x): x not in list" % (type(self).__name__,))
+
+        res = self.indexOfObject_inRange_(item, (start, ln))
+        if res == NSNotFound:
+            raise ValueError("%s.index(x): x not in list" % (type(self).__name__,))
     return res
 
-CONVENIENCE_METHODS[b'indexOfObject:'] = (
-    ('index', index_indexOfObject_),
+CONVENIENCE_METHODS[b'indexOfObject:inRange:'] = (
+    ('index', index_indexOfObject_inRange_),
 )
 
 def insert_insertObject_atIndex_(self, idx, item):
@@ -856,6 +885,58 @@
     collections.Mapping.register(lookUpClass('NSDictionary'))
     collections.MutableMapping.register(lookUpClass('NSMutableDictionary'))
 
+    def nsarray_new(cls, sequence=None):
+        if not sequence:
+            return cls.array()
+
+        elif isinstance(sequence, (str, unicode)):
+            return cls.arrayWithArray_(list(sequence))
+
+        else:
+            if not isinstance(sequence, (list, tuple)):
+                # FIXME: teach bridge to treat range and other list-lik
+                # types correctly
+                return cls.arrayWithArray_(list(sequence))
+
+            return cls.arrayWithArray_(sequence)
+
+    NSMutableArray = lookUpClass('NSMutableArray')
+    def nsarray_add(self, other):
+        result = NSMutableArray.arrayWithArray_(self)
+        result.extend(other)
+        return result
+
+    def nsarray_radd(self, other):
+        result = NSMutableArray.arrayWithArray_(other)
+        result.extend(self)
+        return result
+
+    def nsarray_mul(self, other):
+        """
+        This tries to implement anNSArray * N
+        somewhat efficently (and definitely more
+        efficient that repeated appending).
+        """
+        result = NSMutableArray.array()
+
+        if other <= 0:
+            return result
+
+        n = 1
+        tmp = self
+        while other:
+            if other & n != 0:
+                result.extend(tmp)
+                other -= n
+
+            if other:
+                n <<= 1
+                tmp = tmp.arrayByAddingObjectsFromArray_(tmp)
+
+        #for n in xrange(other):
+            #result.extend(self)
+        return result
+
 
     def nsdict_new(cls, *args, **kwds):
         if len(args) == 0:
@@ -914,6 +995,14 @@
             ('fromkeys', classmethod(nsmutabledict_fromkeys)),
         )
 
+        CLASS_METHODS['NSArray'] = (
+            ('__new__', nsarray_new),
+            ('__add__', nsarray_add),
+            ('__radd__', nsarray_radd),
+            ('__mul__', nsarray_mul),
+            ('__rmul__', nsarray_mul),
+        )
+
     else:
         CLASS_METHODS['NSDictionary'] = (
             ('__new__', nsdict_new),
@@ -926,4 +1015,10 @@
         CLASS_METHODS['NSMutableDictionary'] = (
             ('__new__', nsdict_new),
         )
-
+        CLASS_METHODS['NSArray'] = (
+            ('__new__', nsarray_new),
+            ('__add__', nsarray_add),
+            ('__radd__', nsarray_radd),
+            ('__mul__', nsarray_mul),
+            ('__rmul__', nsarray_mul),
+        )

Modified: trunk/pyobjc/pyobjc-core/Modules/objc/OC_PythonObject.m
==============================================================================
--- trunk/pyobjc/pyobjc-core/Modules/objc/OC_PythonObject.m	(original)
+++ trunk/pyobjc/pyobjc-core/Modules/objc/OC_PythonObject.m	Wed Apr 14 16:45:45 2010
@@ -1454,10 +1454,39 @@
  * This is needed to be able to add a python object to a
  * NSArray and then use array.description()
  */
+-(BOOL)isNSArray__
+{
+	        return NO;
+}
+-(BOOL)isNSDictionary__
+{
+	        return NO;
+}
+-(BOOL)isNSSet__
+{
+	        return NO;
+}
+-(BOOL)isNSNumber__
+{
+	        return NO;
+}
+-(BOOL)isNSData__
+{
+	        return NO;
+}
+-(BOOL)isNSDate__
+{
+	        return NO;
+}
 -(BOOL)isNSString__
 {
-	return NO;
+	        return NO;
 }
+-(BOOL)isNSValue__
+{
+	        return NO;
+}
+
 
 +classFallbacksForKeyedArchiver
 {

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev