Re: [Pyobjc-dev] Re: [ pyobjc-Bugs-679748 ] NSMutableString gets converted to Python string
Brought to you by:
ronaldoussoren
From: Bob I. <bo...@re...> - 2003-02-03 22:35:58
|
On Monday, Feb 3, 2003, at 17:20 America/New_York, Bill Bumgarner wrote: > On Monday, Feb 3, 2003, at 17:16 US/Eastern, Bob Ippolito wrote: >> What about this: >> class UnicodeNSStringWrapper(unicode): >> def __new__(clazz, myNSString): >> s = >> unicode.__new__(somethingToConvertNSStringInstanceToPyUnicodeObject(my >> NSString)) >> s._objc = myNSString >> return s >> def __getattr__(self, attr): >> try: >> return getattr(self._objc, attr) >> except: >> raise AttributeError, '%r object has no attribute %r' % >> (self.__class__.__name__, self._objc) >> >> It should do anything that unicode() will do, just like the str >> subclass I posted a bit ago.. and you don't lose any of the NSString >> functionality. > > Given that I have to write > somethingToConvertNSStringInstanceToPyUnicodeObject() anyway, I'll do > so first, plug it into this and see what happens. (This old had ObjC > programmer sometimes has to be beaten around the head with the obvious > elegant path that only Python can offer.) PyObject *somethingToConvertNSStringInstanceToPyUnicodeObject(NSString *myNSString) { const char *s; if (myNSString == nil) return NULL; s = [myNSString UTF8String]; return PyUnicode_Decode(s, strlen(s), "utf-8", NULL); } not sure about the NULL for errors, but that just about does it! -bob |