Re: [Pyobjc-dev] In and out of NSData
Brought to you by:
ronaldoussoren
From: Bob I. <bo...@re...> - 2003-01-07 15:30:18
|
I updated to latest CVS.. it seems that you may now initialize NSData/NSMutableData with a python string or an array, but it breaks for big ones. >>> from Foundation import * >>> import array >>> bytes = array.array('b', 'abcdef') >>> NSData.alloc().initWithBytes_length_(bytes, len(bytes)) <a07e> >>> NSMutableData.alloc().initWithBytes_length_(bytes, len(bytes)) <a07eda48 00000001 00000000 00000054 007db1e0 00000000 00000000 00000001 76304034 3a385e76 31324931 36000001 76304034 3a384931 32000000 00010002 76> >>> bytes = array.array('b', 'abcdef' * 1000) >>> NSData.alloc().initWithBytes_length_(bytes, len(bytes)) Segmentation fault >>> from Foundation import * >>> NSData.alloc().initWithBytes_length_('a'*1000, 1000) Segmentation fault >>> from Foundation import * >>> for n in range(2**10): ... print n ... NSData.alloc().initWithBytes_length_('a' * n, n) ^^ failed at 31 (30 being the last one that initialized successfully) Is there some reason the unittest seems to skirt around this issue (other than not wanting to crash ;)? It does work (probably unreliably) with small strings/arrays. Maybe there should be some convenience methods in the ObjC core, or additions to the NSData/NSMutableData proxy objects so you can just get an NSData/NSMutableData directly to and from str, array, and numeric arrays? I think that would cover most bases, and I don't think people would mind having to use convenience methods so long as they're documented. It seems kind of silly to require you to specify the length.. it's not very 'pythonic' since it's to easy to slice sequence types to whatever size they need to be and not safe to specify lengths any longer than that. oh.. and I think these segfaults are doing something really nasty to my system (though I don't see exactly why it should), as my front terminal window (that I was using for Python) was going between normal display and completely white and unresponsive at a cycle time of about 5 seconds.. I did manage to close it eventually, and the other terminals seem fine. I dunno if this is related, but I have never seen it happen before. -bob |