Commits

Ronald Oussoren committed c62e5d9 Merge

merge

Comments (0)

Files changed (5)

pyobjc-core/Modules/objc/OC_PythonDictionary.m

 
 - (void)encodeWithCoder:(NSCoder*)coder
 {
-	if (1 && PyDict_CheckExact(value)) {
+	if (PyDict_CheckExact(value)) {
 		if ([coder allowsKeyedCoding]) {
 			[coder encodeInt32:1 forKey:@"pytype"];
 		} else {

pyobjc-core/Modules/objc/OC_PythonNumber.m

 
 - (void)encodeWithCoder:(NSCoder*)coder
 {
-	PyObjC_encodeWithCoder(value, coder);
+	int use_super = 0;
+
+	PyObjC_BEGIN_WITH_GIL
+		if (PyFloat_CheckExact(value)) {
+			/* Float is a C double and can be roundtripped using 
+			 * NSNumber.
+			 */
+			use_super = 1;
+
+#if PY_MAJOR_VERSION == 2
+		} else if (PyInt_CheckExact(value)) {
+			/* Int is a C double and can be roundtripped using 
+			 * NSNumber.
+			 */
+			use_super = 1;
+#endif
+		} else if (PyLong_CheckExact(value)) {
+			/* Long object that fits in a long long */
+			(void)PyLong_AsLongLong(value);
+			if (PyErr_Occurred()) {
+				PyErr_Clear();
+				use_super = 0;
+			} else {
+				use_super = 1;
+			}
+		}
+	PyObjC_END_WITH_GIL
+
+	if (use_super) {
+		[super encodeWithCoder:coder];
+	} else {
+		PyObjC_encodeWithCoder(value, coder);
+	}
 }
 
 
 
 -(Class)classForArchiver
 {
-	return [OC_PythonNumber class];
+	PyObjC_BEGIN_WITH_GIL
+		if (PyFloat_CheckExact(value)) {
+			/* Float is a C double and can be roundtripped using 
+			 * NSNumber.
+			 */
+			PyObjC_GIL_RETURN([NSNumber class]);
+
+#if PY_MAJOR_VERSION == 2
+		} else if (PyInt_CheckExact(value)) {
+			/* Int is a C double and can be roundtripped using 
+			 * NSNumber.
+			 */
+			PyObjC_GIL_RETURN([NSNumber class]);
+#endif
+		} else if (PyLong_CheckExact(value)) {
+			/* Long object that fits in a long long */
+			(void)PyLong_AsLongLong(value);
+			if (PyErr_Occurred()) {
+				PyErr_Clear();
+				PyObjC_GIL_RETURN([OC_PythonNumber class]);
+			} else {
+				PyObjC_GIL_RETURN([NSNumber class]);
+			}
+
+		} else {
+			PyObjC_GIL_RETURN([OC_PythonNumber class]);
+		}
+	PyObjC_END_WITH_GIL
 }
 
 -(Class)classForKeyedArchiver
 {
-	return [OC_PythonNumber class];
+	return [self classForArchiver];
 }
 
 +(Class)classForUnarchiver
 
 -(Class)classForCoder
 {
-	return [OC_PythonNumber class];
+	return [self classForArchiver];
 }
 
 -(Class)classForPortCoder
 {
-	return [OC_PythonNumber class];
+	return [self classForArchiver];
 }
 
 -(id)copy

pyobjc-core/Modules/objc/OC_PythonUnicode.m

 
 -(void)encodeWithCoder:(NSCoder*)coder
 {
-	if (PyUnicode_CheckExact(value)) {
-		[super encodeWithCoder:coder];
-	} else {
-		if ([coder allowsKeyedCoding]) {
-			[coder encodeInt32:2 forKey:@"pytype"];
+	PyObjC_BEGIN_WITH_GIL
+		if (PyUnicode_CheckExact(value)) {
+			[super encodeWithCoder:coder];
 		} else {
-			int v = 2;
-			[coder encodeValueOfObjCType:@encode(int) at:&v];
+			if ([coder allowsKeyedCoding]) {
+				[coder encodeInt32:2 forKey:@"pytype"];
+			} else {
+				int v = 2;
+				[coder encodeValueOfObjCType:@encode(int) at:&v];
+			}
+
+			PyObjC_encodeWithCoder(value, coder);
 		}
-
-		PyObjC_encodeWithCoder(value, coder);
-	}
+	PyObjC_END_WITH_GIL
 }
 
 -(NSObject*)replacementObjectForArchiver:(NSArchiver*)archiver 
  */
 -(Class)classForArchiver
 {
-	if (PyUnicode_CheckExact(value)) {
-		return [NSString class];
-	} else {
-		return [OC_PythonUnicode class];
-	}
+	PyObjC_BEGIN_WITH_GIL
+		if (PyUnicode_CheckExact(value)) {
+			PyObjC_GIL_RETURN([NSString class]);
+		} else {
+			PyObjC_GIL_RETURN([OC_PythonUnicode class]);
+		}
+	PyObjC_END_WITH_GIL
 }
 
 -(Class)classForKeyedArchiver
 {
-	if (PyUnicode_CheckExact(value)) {
-		return [NSString class];
-	} else {
-		return [OC_PythonUnicode class];
-	}
+	return [self classForArchiver];
 }
 
 -(Class)classForCoder
 {
-	if (PyUnicode_CheckExact(value)) {
-		return [NSString class];
-	} else {
-		return [OC_PythonUnicode class];
-	}
+	return [self classForArchiver];
 }
 
 -(Class)classForPortCoder
 {
-	if (PyUnicode_CheckExact(value)) {
-		return [NSString class];
-	} else {
-		return [OC_PythonUnicode class];
-	}
+	return [self classForArchiver];
 }
 
 /* Ensure that we can be unarchived as a generic string by pure ObjC

pyobjc-core/PyObjCTest/test_archive_python.py

 
     def test_unknown_type(self):
         try:
-            orig = pycoder.decode_dispatch[pycoder.kOP_FLOAT_STR]
-            del pycoder.decode_dispatch[pycoder.kOP_FLOAT_STR]
+            orig = pycoder.decode_dispatch[pycoder.kOP_GLOBAL]
+            del pycoder.decode_dispatch[pycoder.kOP_GLOBAL]
 
-            o = 14.2
+            o = TestKeyedArchiveSimple
             buf = self.archiverClass.archivedDataWithRootObject_(o)
             self.assertRaises(pickle.UnpicklingError, self.unarchiverClass.unarchiveObjectWithData_, buf)
 
 
         finally:
-            pycoder.decode_dispatch[pycoder.kOP_FLOAT_STR] = orig
+            pycoder.decode_dispatch[pycoder.kOP_GLOBAL] = orig
 
 
 

pyobjc-core/PyObjCTest/test_testsupport.py

 
     def test_assert_cftype(self):
         self.assertRaises(AssertionError, self.assertIsCFType, long)
-        self.assertRaises(AssertionError, self.assertIsCFType, objc.lookUpClass('NSObject'))
         self.assertRaises(AssertionError, self.assertIsCFType, objc.lookUpClass('NSCFType'))
 
+        self.assertIsCFType(objc.lookUpClass('NSObject'))
+        #self.assertRaises(AssertionError, self.assertIsCFType, objc.lookUpClass('NSObject'))
+
         class OC_OPAQUE_TEST_1 (objc.lookUpClass('NSCFType')): pass
         try:
             self.assertIsCFType(OC_OPAQUE_TEST_1)