Commits

Ronald Oussoren  committed df792c7

Some string updates

* Ensure !FAST_PATH works for python 2.x (already did so for 3.x)

* Remove (already disabled) implementation for some private APIs
in the NSString class cluster

* Disable FAST_PATH support for now, I get unexplained test failures
when FAST_PATH is enabled (in particular: CFStringTrim fails when
running on OSX with the x86_64 architecture when the second
argument is an OC_PythonUnicode object that uses FAST_PATH).

  • Participants
  • Parent commits 622dc2f

Comments (0)

Files changed (3)

File pyobjc-core/Modules/objc/OC_PythonString.m

 			length:(NSUInteger)length 
 		  freeWhenDone:(BOOL)flag
 {
-#ifndef PyObjC_UNICODE_FAST_PATH
-# error "Wide UNICODE builds are not supported at the moment"
-#endif
 	PyObjC_BEGIN_WITH_GIL
+		int byteorder = 0;
 		PyObject* v;
-		v = PyUnicode_FromUnicode((Py_UNICODE*)characters, length);
+		/* Decode as a UTF-16 string in native byteorder */
+		v = PyUnicode_DecodeUTF16(
+			(const char*)characters,
+			length * 2,
+			NULL,
+			&byteorder);
 		if (v == NULL) {
 			PyObjC_GIL_FORWARD_EXC();
 		}

File pyobjc-core/Modules/objc/OC_PythonUnicode.m

 	/* FIXME3: Should switch to __weak on OSX 10.7 or later, that should
 	 * fix this issue without a performance penalty.
 	 */
-#if 0
-	if ([self retainCount] == 1) {
-#endif
-		PyObjC_BEGIN_WITH_GIL
-			[super release];
-		PyObjC_END_WITH_GIL
-#if 0
-	} else {
+	PyObjC_BEGIN_WITH_GIL
 		[super release];
-	}
-#endif
+	PyObjC_END_WITH_GIL
 }
 
 -(void)dealloc
 
 -(unichar)characterAtIndex:(NSUInteger)anIndex
 {
+	Py_ssize_t offset;
 	if (anIndex > PY_SSIZE_T_MAX) {
 		[NSException raise:@"NSRangeException" format:@"Range or index out of bounds"];
 	}
 	if (anIndex >= (NSUInteger)PyUnicode_GET_SIZE(value)) {
 		[NSException raise:@"NSRangeException" format:@"Range or index out of bounds"];
 	}
-	return PyUnicode_AS_UNICODE(value)[anIndex];
+
+	offset = (Py_ssize_t)anIndex;
+	unichar ch = PyUnicode_AS_UNICODE(value)[offset];
+	return ch;
 }
 
 -(void)getCharacters:(unichar *)buffer range:(NSRange)aRange
 {
+	Py_ssize_t offset;
+	size_t length;
+
 	if (aRange.location + aRange.length > (NSUInteger)PyUnicode_GET_SIZE(value)) {
 		[NSException raise:@"NSRangeException" format:@"Range or index out of bounds"];
 	}
+
+	offset = (Py_ssize_t)(aRange.location);
+	length = (size_t)aRange.length;
+
 	memcpy(buffer, 
-	       PyUnicode_AS_UNICODE(value) + aRange.location, 
-	       sizeof(unichar) * aRange.length);
+	       (PyUnicode_AS_UNICODE(value)) + offset,
+	       2 * length);
 }
 
-#if 0 /* Experimantal support for private APIs. Should not be necessary */
-
--(const char*)_fastCStringContents:(BOOL)nullTerminated
-{
-	return NULL;
-}
-
--(const UniChar*)_fastCharacterContents
-{
-	return PyUnicode_AS_UNICODE(value);
-}
-
--(CFStringEncoding)_fastestEncodingInCFStringEncoding
-{
-	return kCFStringEncodingUTF8;
-}
-#endif
-
-
 #else /* !PyObjC_UNICODE_FAST_PATH */
 
 -(id)__realObject__
 
 -(unichar)characterAtIndex:(NSUInteger)anIndex
 {
-	return [((NSString *)[self __realObject__]) characterAtIndex:anIndex];
+
+	unichar ch = [((NSString *)[self __realObject__]) characterAtIndex:anIndex];
+	return ch;
 }
 
 -(void)getCharacters:(unichar *)buffer range:(NSRange)aRange
 	[((NSString *)[self __realObject__]) getCharacters:buffer range:aRange];
 }
 
-#if 0 /* Experimantal support for private APIs. Should not be necessary */
--(const char*)_fastCStringContents:(BOOL)nullTerminated
-{
-	return [[self __realObject__] _fastCStringContents:nullTerminated];
-}
-
--(const UniChar*)_fastCharacterContents
-{
-	return [[self __realObject__] _fastCharacterContents];
-}
-
--(CFStringEncoding)_fastestEncodingInCFStringEncoding
-{
-	return [[self __realObject__] _fastestEncodingInCFStringEncoding];
-}
-#endif
-
-
 #endif /* PyObjC_UNICODE_FAST_PATH */
 
 /*

File pyobjc-core/Modules/objc/pyobjc.h

 #define PyObjC_UNICODE_FAST_PATH
 #endif
 
+/* 
+ * XXX: disable the fast path for
+ * unicode strings due to unexplained
+ * test failures.
+ */
+#ifdef PyObjC_UNICODE_FAST_PATH
+#undef PyObjC_UNICODE_FAST_PATH
+#endif
+
 #include "objc-runtime-compat.h"
 #include "proxy-registry.h"
 #include "objc_support.h"