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

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#include "pyobjc.h"
#import "OC_PythonUnicode.h"

@implementation OC_PythonUnicode 

+ (instancetype)unicodeWithPythonObject:(PyObject*)v
{
	OC_PythonUnicode* res;

	res = [[OC_PythonUnicode alloc] initWithPythonObject:v];
	[res autorelease];
	return res;
}

- (id)initWithPythonObject:(PyObject*)v
{
	self = [super init];
	if (unlikely(self == nil)) return nil;
	Py_INCREF(v);
	Py_XDECREF(value);
	value = v;
	return self;
}


-(PyObject*)__pyobjc_PythonObject__
{
	Py_INCREF(value);
	return value;
}

-(PyObject*)__pyobjc_PythonTransient__:(int*)cookie
{
	*cookie = 0;
	Py_INCREF(value);
	return value;
}



-(BOOL)supportsWeakPointers { return YES; }

-(oneway void)release
{
	/* There is small race condition when an object is almost deallocated
	 * in one thread and fetched from the registration mapping in another
	 * thread. If we don't get the GIL this object might get a -dealloc
	 * message just as the other thread is fetching us from the mapping.
	 * That's why we need to grab the GIL here (getting it in dealloc is
	 * too late, we'd already be dead).
	 */
	/* FIXME: it should be possible to grab the lock only when really
	 * needed, but the test below isn't good enough. Be heavy handed to
	 * make sure we're right, rather than crashing sometimes anyway.
	 */
	/* FIXME2: in rare occasions we're trying to acquire the GIL during 
	 * shutdown and if we're very unlucky this can happen after the 
	 * GILState machinery has shut down...
	 */
	/* FIXME3: Should switch to __weak on OSX 10.7 or later, that should
	 * fix this issue without a performance penalty.
	 */
	PyObjC_BEGIN_WITH_GIL
		[super release];
	PyObjC_END_WITH_GIL
}

-(void)dealloc
{
	PyObjC_BEGIN_WITH_GIL
		PyObjC_UnregisterObjCProxy(value, self);
		[realObject release];
		realObject = nil;
		Py_CLEAR(value);

#ifdef PyObjC_STR_CACHE_IMP
		imp_length = 0xDEADBEEF;
		imp_charAtIndex = 0xDEADBEEF;
		imp_getCharacters = 0xDEADBEEF;
#endif /* PyObjC_STR_CACHE_IMP */

	PyObjC_END_WITH_GIL

	[super dealloc];
}

/*
 * XXX: The code below should work for PyObjC_UNICODE_FAST_PATH 
 * but causes failures on 64-bit builds on OSX 10.7
 *

-(NSUInteger)length
{
	return (NSUInteger)PyUnicode_GET_SIZE(value);
}

-(unichar)characterAtIndex:(NSUInteger)anIndex
{
	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 (unichar)PyUnicode_AS_UNICODE(value)[anIndex];
}

-(void)getCharacters:(unichar *)buffer range:(NSRange)aRange
{
	if (aRange.location + aRange.length > (NSUInteger)PyUnicode_GET_SIZE(value)) {
		[NSException raise:@"NSRangeException" format:@"Range or index out of bounds"];
	}

	memmove(buffer, 
	       (PyUnicode_AS_UNICODE(value)) + aRange.location,
	       sizeof(unichar) * aRange.length);
}

*/


#if PY_VERSION_HEX >= 0x03030000

-(id)__realObject__
{
	if (!realObject) {
		switch (PyUnicode_KIND(value)) {
		case PyUnicode_1BYTE_KIND:
			if (PyUnicode_IS_ASCII(value)) {
				realObject = [[NSString alloc]
					initWithBytesNoCopy:PyUnicode_1BYTE_DATA(value)
					       length:(NSUInteger)PyUnicode_GET_SIZE(value)
					     encoding:NSASCIIStringEncoding
					 freeWhenDone:NO];
			} else {
				realObject = [[NSString alloc]
					initWithBytesNoCopy:PyUnicode_1BYTE_DATA(value)
					       length:(NSUInteger)PyUnicode_GET_SIZE(value)
					     encoding:NSISOLatin1StringEncoding
					 freeWhenDone:NO];
			
			}
			break;

		case PyUnicode_2BYTE_KIND:
			realObject = [[NSString alloc]
				initWithCharactersNoCopy:PyUnicode_2BYTE_DATA(value)
				       length:(NSUInteger)PyUnicode_GET_SIZE(value)
				 freeWhenDone:NO];
			break;

		case PyUnicode_WCHAR_KIND:
			/* wchar_t representation, treat same
			 * as UCS4 strings
			 */
		case PyUnicode_4BYTE_KIND:
			PyObjC_BEGIN_WITH_GIL
				PyObject* utf8 = PyUnicode_AsUTF8String(value);
				if (!utf8) {
					NSLog(@"failed to encode unicode string to byte string");
					PyErr_Clear();
				} else {
					realObject = [[NSString alloc]
						initWithBytes:PyBytes_AS_STRING(utf8)
						       length:(NSUInteger)PyBytes_GET_SIZE(utf8)
						     encoding:NSUTF8StringEncoding];
					Py_DECREF(utf8);
				}
			PyObjC_END_WITH_GIL
		}
	}
	return realObject;
}

#elif defined(PyObjC_UNICODE_FAST_PATH)

-(id)__realObject__
{
	if (!realObject) {
		realObject = [[NSString alloc]
			initWithCharactersNoCopy:PyUnicode_AS_UNICODE(value)
			       length:(NSUInteger)PyUnicode_GET_SIZE(value)
			 freeWhenDone:NO];
	}
	return realObject;
}

#else // !PyObjC_UNICODE_FAST_PATH */

-(id)__realObject__
{
	if (!realObject) {
		PyObjC_BEGIN_WITH_GIL
			PyObject* utf8 = PyUnicode_AsUTF8String(value);
			if (!utf8) {
				NSLog(@"failed to encode unicode string to byte string");
				PyErr_Clear();
			} else {
				realObject = [[NSString alloc]
					initWithBytes:PyBytes_AS_STRING(utf8)
					       length:(NSUInteger)PyBytes_GET_SIZE(utf8)
					     encoding:NSUTF8StringEncoding];
				Py_DECREF(utf8);
			}
		PyObjC_END_WITH_GIL
	}
	return realObject;
}
#endif	

-(NSUInteger)length
{
#ifdef PyObjC_STR_CACHE_IMP
	if (!imp_length) {
		[self __realObject__];
		imp_length = (__typeof__(imp_length))([realObject methodForSelector:@selector(length)]);
	}
	if (!imp_length) abort();
	if (!realObject) abort();
	return imp_length(realObject, @selector(length));

#else /* !PyObjC_STR_CACHE_IMP */
	return [[self __realObject__] length];

#endif /* !PyObjC_STR_CACHE_IMP */
}

-(unichar)characterAtIndex:(NSUInteger)anIndex
{
#ifdef PyObjC_STR_CACHE_IMP
	if (!imp_charAtIndex) {
		[self __realObject__];
		imp_charAtIndex = (__typeof__(imp_charAtIndex))([realObject methodForSelector:@selector(characterAtIndex:)]);
	}
	if (!imp_charAtIndex) abort();
	if (!realObject) abort();
	return imp_charAtIndex(realObject, @selector(characterAtIndex:), anIndex);

#else /* !PyObjC_STR_CACHE_IMP */
	return [[self __realObject__] characterAtIndex:anIndex];

#endif /* !PyObjC_STR_CACHE_IMP */
}

-(void)getCharacters:(unichar *)buffer range:(NSRange)aRange
{
#ifdef PyObjC_STR_CACHE_IMP
	if (!imp_getCharacters) {
		[self __realObject__];
		imp_getCharacters = (__typeof__(imp_getCharacters))([realObject methodForSelector:@selector(getCharacters:range:)]);
	}
	if (!imp_getCharacters) abort();
	if (!realObject) abort();
	imp_getCharacters(realObject, @selector(getCharacters:range:), buffer, aRange);

#else	/* !PyObjC_STR_CACHE_IMP */
	return [[self __realObject__] getCharacters:buffer range:aRange];

#endif
}

-(void)getCharacters:(unichar*)buffer
{
	return [[self __realObject__] getCharacters:buffer];
}

/*
 * NSCoding support 
 *
 * We need explicit NSCoding support to get full fidelity, otherwise we'll
 * get archived as generic NSStrings.
 */
- (id)initWithCharactersNoCopy:(unichar *)characters 
			length:(NSUInteger)length 
		  freeWhenDone:(BOOL)flag
{
	int byteorder = 0;
	PyObjC_BEGIN_WITH_GIL
		/* Decode as a UTF-16 string in native byteorder */
		value = PyUnicode_DecodeUTF16(
				(const char*)characters,
				length * 2,
				NULL,
				&byteorder); 
		if (value == NULL) {
			PyObjC_GIL_FORWARD_EXC();
		}

	PyObjC_END_WITH_GIL;
	if (flag) {
		free(characters);
	}
	return self;
}

-(id)initWithBytes:(void*)bytes length:(NSUInteger)length encoding:(NSStringEncoding)encoding
{
	char* py_encoding = NULL;
	int byteorder = 0;

	/* Detect some often used single-byte encodings that can be created in Python without
	 * creating an intermediate object.
	 */

	switch (encoding) {
	case NSASCIIStringEncoding: py_encoding = "ascii"; break;
	case NSUTF8StringEncoding: py_encoding = "UTF-8"; break;
	case NSISOLatin1StringEncoding: py_encoding = "latin1"; break;
	}

	if (py_encoding != NULL) {
		PyObjC_BEGIN_WITH_GIL
			value = PyUnicode_Decode(bytes, length, py_encoding, NULL);
			if (value == NULL) {
				PyObjC_GIL_FORWARD_EXC();
			}
		PyObjC_END_WITH_GIL
		return self;
	}

	/* UTF-16 encodings can also be decoded without an intermediate object */
	byteorder = 2;
	switch (encoding) {
	case NSASCIIStringEncoding: byteorder = 0; break;
	case NSUTF8StringEncoding:  byteorder = -1; break;
	case NSISOLatin1StringEncoding:  byteorder = 1; break;
	}
	if (byteorder != 2) {
		PyObjC_BEGIN_WITH_GIL
			/* Decode as a UTF-16 string in native byteorder */
			value = PyUnicode_DecodeUTF16(
					bytes,
					length,
					NULL,
					&byteorder); 
			if (value == NULL) {
				PyObjC_GIL_FORWARD_EXC();
			}

		PyObjC_END_WITH_GIL;
		return self;
	}

	/* And finally: first use the Cocoa decoder to create an NSString, copy the unichars into
	 * a temporary buffer and use that to create a Python unicode string using the UTF16 decoder.
	 *
	 * This can be slightly optimized on systems where sizeof(Py_UNICODE) == sizeof(unichar), but
	 * that's not worth the additional complexity and won't work on Python 3.3 or later anyway.
	 */

	NSString* tmpval = [[NSString alloc] initWithBytes:bytes length:length encoding:encoding];
	Py_ssize_t charcount = [tmpval length];

	/* NOTE: the malloc() call can be avoided when sizeof(unichar) == sizeof(Py_UNICODE) and
	 * we're on python 3.2 or earlier. That's not worth the added complexity.
	 */
	unichar* chars = malloc(charcount*2);

	if (chars == NULL) {
		[self release];
		return nil;
	}
	[tmpval getCharacters:chars];
	[tmpval release];

	PyObjC_BEGIN_WITH_GIL
		/* Decode as a UTF-16 string in native byteorder */
		byteorder = 0;
		value = PyUnicode_DecodeUTF16(
				(const char*)chars,
				length * 2,
				NULL,
				&byteorder); 
		free(chars);
		if (value == NULL) {
			PyObjC_GIL_FORWARD_EXC();
		}

	PyObjC_END_WITH_GIL;
	return self;
}

/* 
 * 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 ver;
	if ([coder allowsKeyedCoding]) {
		ver = [coder decodeInt32ForKey:@"pytype"];
	} else {
		[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) {

		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* v = PyObject_CallFunction(PyObjC_Decoder, "OO", cdr, setValue);
				Py_DECREF(cdr);
				Py_DECREF(setValue);
				Py_DECREF(selfAsPython);

				if (v == NULL) {
					PyObjC_GIL_FORWARD_EXC();
				}

				Py_XDECREF(value);
				value = v;

				NSObject* proxy = PyObjC_FindObjCProxy(value);
				if (proxy == NULL) {
					PyObjC_RegisterObjCProxy(value, self);
				} else {
					[self release];
					[proxy retain];
					self = (OC_PythonUnicode*)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 nil;
	}
}

-(void)encodeWithCoder:(NSCoder*)coder
{
	if (PyUnicode_CheckExact(value)) {
		[super encodeWithCoder:coder];
	} else {
		if ([coder allowsKeyedCoding]) {
			[coder encodeInt32:2 forKey:@"pytype"];
		} else {
			int v = 2;
			[coder encodeValueOfObjCType:@encode(int) at:&v];
		}

		PyObjC_encodeWithCoder(value, coder);
	}
}

-(NSObject*)replacementObjectForArchiver:(NSArchiver*)archiver 
{
	(void)(archiver);
	return self;
}

-(NSObject*)replacementObjectForKeyedArchiver:(NSKeyedArchiver*)archiver
{
	(void)(archiver);
	return self;
}

-(NSObject*)replacementObjectForCoder:(NSCoder*)archiver
{
	(void)(archiver);
	return self;
}

-(NSObject*)replacementObjectForPortCoder:(NSPortCoder*)archiver
{
	(void)(archiver);
	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
{
	if (PyUnicode_CheckExact(value)) {
		return [NSString class];
	} else {
		return [OC_PythonUnicode class];
	}
}

-(Class)classForKeyedArchiver
{
	if (PyUnicode_CheckExact(value)) {
		return [NSString class];
	} else {
		return [OC_PythonUnicode class];
	}
}

-(Class)classForCoder
{
	if (PyUnicode_CheckExact(value)) {
		return [NSString class];
	} else {
		return [OC_PythonUnicode class];
	}
}

-(Class)classForPortCoder
{
	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
 * code.
 */
+(NSArray*)classFallbacksForKeyedArchiver
{
	return [NSArray arrayWithObject:@"NSString"];
}


@end /* implementation OC_PythonUnicode */
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.