Re: [Pyobjc-dev] In and out of NSData
Brought to you by:
ronaldoussoren
From: <bb...@ma...> - 2003-01-07 03:58:22
|
On Monday, Jan 6, 2003, at 20:03 US/Eastern, Bob Ippolito wrote: > Which is ok, and works fine. But how the heck do you do the opposite > (short of writing to a tempfile)? I can get an ObjCPointer to > mutableData or the like, but there aren't any useful methods in it > because I can't use python to read data from a pointer (unless there's > some sketchy stuff in objc that I haven't found yet). If you grab the source from the cvs repository, you can now do stuff like the following (this is the unit test, btw). This will, of course, be in the next version of PyObjC (sometime in February or March, maybe). Be forewarned, there is a bit of a dichotomy between the behavior of array, buffer, str, and other buffer like things in PyObjC. There is a PEP (296, I believe) that will nicely fix this, but it may never actually be implemented. b.bum import unittest import objc import array from Foundation import NSData, NSMutableData rawBytes = "a\x13b\x00cd\xFFef\xEFgh" otherBytes = array.array('c') otherBytes.fromstring('12345678901234567890' * 5) class TestNSData(unittest.TestCase): def assertDataContents(self, d1, d2, rawData): self.assertEquals(len(d1), d1.length(), "d1: len() and -length didn't match.") self.assertEquals(len(d1), len(rawData), "d1: len(<data>) and len(<input>) didn't match.") self.assertEquals(len(d2), d2.length(), "d2: len() and -length didn't match.") self.assertEquals(len(d2), len(rawData), "d2: len(<data>) and len(<input>) didn't match.") def testDataWithBytes_length_(self): """Test +dataWithBytes:length:""" data = NSData.dataWithBytes_length_(rawBytes, len(rawBytes)) mutableData = NSMutableData.dataWithBytes_length_(rawBytes, len(rawBytes)) self.assertDataContents(data, mutableData, rawBytes) def testInitWithBytes_length_(self): """Test -initWithBytes:length:""" data = NSData.alloc().initWithBytes_length_(rawBytes, len(rawBytes)) mutableData = NSMutableData.alloc().initWithBytes_length_(rawBytes, len(rawBytes)) self.assertDataContents(data, mutableData, rawBytes) def testBytes(self): """Test -bytes""" data = NSData.alloc().initWithBytes_length_(rawBytes, len(rawBytes)) bytes = data.bytes() self.assertEquals(len(bytes), len(rawBytes), "bytes() and rawBytes not equal length.") for i in range(0,len(bytes)): self.assertEquals(rawBytes[i], bytes[i], "byte %s of bytes and rawBytes are not equal." % i) try: bytes[3] = 0xAE except TypeError, r: if str(r).find('buffer is read-only') is not 0: raise def testMutableBytes(self): """Test -mutableBytes""" mutableData = NSMutableData.dataWithBytes_length_(rawBytes, len(rawBytes)) mutableBytes = mutableData.mutableBytes() for i in range(0, len(mutableBytes)): mutableBytes[i] = otherBytes[i] mutableBytes[1:8] = otherBytes[1:8] try: mutableBytes[2:10] = otherBytes[1:5] except TypeError, r: if str(r).find('right operand length must match slice length') is not 0: raise def testVariousDataLengths(self): """Test data of different lengths. Data of different lengths may be stored in different subclasses within the class cluster. """ testFactor = [1, 10, 1000, 10000, 1000000] for aFactor in testFactor: bigRawBytes = "1234567890" * aFactor mutableData = NSMutableData.dataWithBytes_length_(bigRawBytes, len(bigRawBytes)) data = NSData.dataWithBytes_length_(bigRawBytes, len(bigRawBytes)) self.assertDataContents(data, mutableData, bigRawBytes) mutableBytes = mutableData.mutableBytes() bytes = data.bytes() self.assertEquals(len(bytes), data.length()) self.assertEquals(len(mutableBytes), mutableData.length()) self.assertEquals(bytes, mutableBytes) mutableBytes[0:len(mutableBytes)] = bytes[0:len(bytes)] def suite(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(TestNSData)) return suite if __name__ == '__main__': unittest.main( ) |