Commits

Ronald Oussoren committed 0ad6de1

Small updates w.r.t testing and py3k

Comments (0)

Files changed (9)

pyobjc-core/Lib/objc/_lazyimport.py

                 bundle_path=frameworkPath,
                 scan_classes=False)
 
-        return bundle
+    return bundle
 
 class GetAttrMap (object):
     __slots__ = ('_container',)
                         self.__dict__[name] = v
                 continue
 
-            if tollfree:
-                for nm in tollfree.split(','):
-                    try:
-                        objc.lookUpClass(nm)
-                    except objc.error:
-                        pass
-                    else:
-                        tollfree = nm
-                        break
-
-                try:
-                    v = objc.registerCFSignature(name, type, func(), tollfree)
-                except objc.nosuchclass_error:
-                    v = objc.registerCFSignature(name, type, func())
-            else:
-                v = objc.registerCFSignature(name, type, func())
+            v = objc.registerCFSignature(name, type, func())
             if v is not None:
                 self.__dict__[name] = v

pyobjc-core/Lib/objc/_properties.py

                     instance_methods.add(setprop)
 
                     # Use dynamic setter to avoid problems when subclassing
-                    self.__setprop = _dynamic_setter(setterName)
+                    self.__setprop = _dynamic_setter(setterName.decode('ascii'))
             else:
                 setprop = selector(
                     self._setter,
                 instance_methods.add(setprop)
 
                 # Use dynamic setter to avoid problems when subclassing
-                self.__setprop = _dynamic_setter(setterName)
+                self.__setprop = _dynamic_setter(setterName.decode('ascii'))
 
         if self._typestr in (_C_NSBOOL, _C_BOOL):
             getterName = b'is' + name[0].upper().encode('latin1') + name[1:].encode('latin1')
             raise ValueError("Property '%s' is read-only"%(self._name,))
 
         try:
-            v = iter(self).next()
+            v = next(iter(self))
         except StopIteration:
             raise KeyError("Empty set")
 

pyobjc-core/Modules/objc/OC_PythonData.m

 	return res;
 }
 
+
 - (id)initWithPythonObject:(PyObject*)v
 {
 	self = [super init];
 	return rval;
 }
 
+-(Class)classForCoder
+{
+	return [OC_PythonData class];
+}
+
+
+-(void)encodeWithCoder:(NSCoder*)coder
+{
+
+	PyObjC_BEGIN_WITH_GIL
+		if (PyBytes_CheckExact(value)) {
+			if (unlikely(PyObject_AsReadBuffer(value, &buffer, &buffer_len) == -1)) {
+				PyObjC_GIL_FORWARD_EXC();
+			}
+
+
+			if ([coder allowsKeyedCoding]) {
+				[coder encodeInt32:1 forKey:@"pytype"];
+				[coder encodeBytes:buffer length:buffer_len forKey: @"pybytes"];
+
+			} else {
+				int v = 1;
+				[coder encodeValueOfObjCType:@encode(int) at:&v];
+				[coder encodeBytes:buffer length:buffer_len];
+			}
+			
+		} else {
+			if ([coder allowsKeyedCoding]) {
+				[coder encodeInt32:2 forKey:@"pytype"];
+			} else {
+				int v = 2;
+				[coder encodeValueOfObjCType:@encode(int) at:&v];
+			}
+
+			PyObjC_encodeWithCoder(value, coder);
+		}
+	PyObjC_END_WITH_GIL
+}
+
+/* 
+ * Helper method for initWithCoder, needed to deal with
+ * recursive objects (e.g. o.value = o)
+ */
+-(void)pyobjcSetValue:(NSObject*)other
+{
+	PyObjC_BEGIN_WITH_GIL
+		PyObject* v = PyObjC_IdToPython(other);
+		Py_XDECREF(value);
+		value = v;
+	PyObjC_END_WITH_GIL
+}
+
+- (id)initWithCoder:(NSCoder*)coder
+{
+	int v;
+	
+	if ([coder allowsKeyedCoding]) {
+
+		v = [coder decodeInt32ForKey:@"pytype"];
+	} else {
+		[coder decodeValueOfObjCType:@encode(int) at:&v];
+	}
+	if (v == 1) {
+		self = [super init];
+		if (unlikely(self == nil)) return nil;
+
+		void *bytes;
+		NSUInteger length;
+
+		if ([coder allowsKeyedCoding]) {
+			bytes = [coder decodeBytesForKey:@"pybytes" returnedLength:&length];
+		} else {
+			bytes = [coder decodeBytesWithReturnedLength:&length];
+		}
+		PyObjC_BEGIN_WITH_GIL
+			value = PyBytes_FromStringAndSize(bytes, length);
+			if (value == NULL) {
+				[super dealloc];
+				PyObjC_GIL_FORWARD_EXC();
+			}
+		PyObjC_END_WITH_GIL;
+		return self;
+
+	} else if (v == 2) {
+
+		if (PyObjC_Decoder != NULL) {
+			PyObjC_BEGIN_WITH_GIL
+				PyObject* cdr = PyObjC_IdToPython(coder);
+				if (cdr == NULL) {
+					PyObjC_GIL_FORWARD_EXC();
+				}
+
+				PyObject* setValue;
+				PyObject* selfAsPython = PyObjCObject_New(self, 0, YES);
+				setValue = PyObject_GetAttrString(selfAsPython, "pyobjcSetValue_");
+
+				PyObject* v2 = PyObject_CallFunction(PyObjC_Decoder, "OO", cdr, setValue);
+				Py_DECREF(cdr);
+				Py_DECREF(setValue);
+				Py_DECREF(selfAsPython);
+
+				if (v2 == NULL) {
+					PyObjC_GIL_FORWARD_EXC();
+				}
+
+				Py_XDECREF(value);
+				value = v2;
+
+				NSObject* proxy = PyObjC_FindObjCProxy(value);
+				if (proxy == NULL) {
+					PyObjC_RegisterObjCProxy(value, self);
+				} else {
+					[self release];
+					[proxy retain];
+					self = (OC_PythonData*)proxy;
+				}
+
+
+			PyObjC_END_WITH_GIL
+
+			return self;
+
+		} else {
+			[NSException raise:NSInvalidArgumentException
+					format:@"decoding Python objects is not supported"];
+			return nil;
+
+		}
+
+	} else {
+		[NSException raise:NSInvalidArgumentException
+				format:@"encoding Python objects is not supported"];
+	}
+	return self;
+}
+
+
 @end /* implementation OC_PythonData */

pyobjc-core/Modules/objc/OC_PythonObject.m

 				r = -1;
 			}
 		}
-#endif /* ! Python3 */
+#endif /* !Python3 */
 	} else if (PyObject_CheckReadBuffer(argument)) {
 		rval = [OC_PythonData
 			dataWithPythonObject:argument];
 - Using NSArchiver or NSKeyedArchiver to encode and decode (pure) python objects didn't always
   work correctly. Found by improved unittests.
 
+- Using NSArchiver or NSKeyedArchiver to encode and decode bytes objects in Python 3 would
+  result in an instance of NSData instead of bytes.
+
 - The implementation of cmp() for NSSet instances now matches the behavior of regular python
   sets, that is calling ``cmp(anNSSet, aValue)`` will raise a TypeError exception unless
   both arguments are the same object (``anNSSet is aValue``).

pyobjc-core/PyObjCTest/test_archive_python.py

         self.assertIsInstance(buf, NSData)
         v = self.unarchiverClass.unarchiveObjectWithData_(buf)
         self.assertIsInstance(v, newstyle_with_setstate)
-        self.assertEquals(v.state, {'a': 1, 'b': 2})
+        self.assertEqual(v.state, {'a': 1, 'b': 2})
         
     def test_reduce_as_global(self):
         # Test class where __reduce__ returns a string (the name of a global)

pyobjc-core/PyObjCTest/test_bridgesupport.py

 
         metadata_registry = {}
         def registerMetaDataForSelector(class_, selector, metadata):
-            self.assertIsInstance(class_, str)
-            self.assertIsInstance(selector, str)
+            self.assertIsInstance(class_, bytes)
+            self.assertIsInstance(selector, bytes)
             self.assertIsInstance(metadata, dict)
 
             # XXX: It might be nice to validate the contents of
 
             self.assertEqual(_meta_updates, [True, False])
             self.assertEqual(metadata_registry, {
-                (b'class1', 'sel1:'): {
+                (b'class1', b'sel1:'): {
                     'arguments': {
                         2: { 'null_accepted': False, 'type_modifier': b'o' }
                     }

pyobjc-core/PyObjCTest/test_convenience.py

         self.assertEqual(o._['host'], 'www.python.org')
         self.assertRaises(TypeError, lambda: o._[42])
         self.assertEqual(repr(o._), '<KVC accessor for %r>'%(o,))
-        self.assertRaises(AttributeError, o._.nosuchattr)
+        self.assertRaises(AttributeError, getattr, o._, 'nosuchattr')
         self.assertRaises(TypeError, o._.__setitem__, 42) 
 
         o = objc.lookUpClass('NSMutableDictionary').dictionary()

pyobjc-core/PyObjCTest/test_splitsig.py

     # signatures, and therefore all signatures changed by PyObjC, are
     # valid.
         for cls in objc.getClassList():
-            for selName in cls.__dict__.keys():
+            for selName in list(cls.__dict__.keys()):
                 try:
                     sel = getattr(cls, selName.decode('latin1'))
                 except AttributeError: