Ronald Oussoren avatar Ronald Oussoren committed 7d0aff7

Store objects where type(object) == unicode as plain NSStrings in an archive

With this patch it is no longer necessary to explicitly convert the
arguments for sending a Growl notification to an NSString.

Comments (0)

Files changed (2)

pyobjc-core/Modules/objc/OC_PythonUnicode.m

 		[coder decodeValueOfObjCType:@encode(int) at:&ver];
 	}
 	if (ver == 1) {
+		/* Version 1: plain unicode string (not subclass).
+		 * emitted by some versions of PyObjC (< 2.4.1, < 2.5.1, <2.6)
+		 */
 		self = [super initWithCoder:coder];
 		return self;
 	} else if (ver == 2) {
 -(void)encodeWithCoder:(NSCoder*)coder
 {
 	if (PyUnicode_CheckExact(value)) {
-		if ([coder allowsKeyedCoding]) {
-			[coder encodeInt32:1 forKey:@"pytype"];
-		} else {
-			int v = 1;
-			[coder encodeValueOfObjCType:@encode(int) at:&v];
-		}
 		[super encodeWithCoder:coder];
 	} else {
 		if ([coder allowsKeyedCoding]) {
 	return self;
 }
 
+/* 
+ * Plain unicode objects (not subclasses) are archived as "real"
+ * NSString objects. This means you won't get the same object type back
+ * when reading them back, but does allow for better interop with code
+ * that uses a non-keyed archiver.
+ */
 -(Class)classForArchiver
 {
-	return [OC_PythonUnicode class];
+	if (PyUnicode_CheckExact(value)) {
+		return [NSString class];
+	} else {
+		return [OC_PythonUnicode class];
+	}
 }
 
 -(Class)classForKeyedArchiver
 {
-	return [OC_PythonUnicode class];
+	if (PyUnicode_CheckExact(value)) {
+		return [NSString class];
+	} else {
+		return [OC_PythonUnicode class];
+	}
 }
 
 -(Class)classForCoder
 {
-	return [OC_PythonUnicode class];
+	if (PyUnicode_CheckExact(value)) {
+		return [NSString class];
+	} else {
+		return [OC_PythonUnicode class];
+	}
 }
 
 -(Class)classForPortCoder
 {
-	return [OC_PythonUnicode class];
+	if (PyUnicode_CheckExact(value)) {
+		return [NSString class];
+	} else {
+		return [OC_PythonUnicode class];
+	}
 }
 
 /* Ensure that we can be unarchived as a generic string by pure ObjC

pyobjc-core/NEWS.txt

      It is highly unlikely that real code would run into this, found
      while working on PyObjC 3.x.
 
+- When writing a python unicode object to an NSArchiver or NSKeyedArchiver
+  the object is now stored exactly the same as a normal NSString, and will
+  be read back as such.
+
+  This increases interoperability with code that expects to read back a
+  non-keyed archive in a different proces. An example of this is the use
+  of Growl.
+
+  Instances of subclasses of unicode are not affected by this change, and
+  can only be read back by other PyObjC programs.
+
 Version 2.5
 -----------
 
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.