Example #1
0
    def __new__(cls, chunk, chunkoffset, patch, patchoffset):
        '''
        Basically ensures that all values are stored
        as python3 bytes (i.e. b'\x01', etc.) and ints
        '''
        # Check offsets
        if not (isinstance(chunkoffset, int) and isinstance(patchoffset, int)):
            raise TypeError('Offsets must be int')

        # Check chunk
        if isinstance(chunk, bytes):
            pass
        elif isinstance(chunk, int):
            chunk = util.int2bytes(chunk)
        elif isinstance(chunk, str):
            chunk = util.str2bytes(chunk)
        else:
            raise TypeError('Chunk not bytes, int or str: {0}'.format(chunk))

        # Check patch
        if isinstance(patch, bytes):
            pass
        elif isinstance(patch, int):
            patch = util.int2bytes(patch)
        elif isinstance(patch, str):
            patch = util.str2bytes(patch)
        elif isinstance(patch, type(None)):
            pass
        else:
            raise TypeError('Patch not bytes, int, str or NoneType: {0}'
                            .format(patch))

        return super(Chunk, cls).__new__(
            cls, chunk, chunkoffset, patch, patchoffset)
Example #2
0
    def __new__(cls, chunk, chunkoffset, patch, patchoffset):
        '''
        Basically ensures that all values are stored
        as python3 bytes (i.e. b'\x01', etc.) and ints
        '''
        # Check offsets
        if not (isinstance(chunkoffset, int) and isinstance(patchoffset, int)):
            raise TypeError('Offsets must be int')

        # Check chunk
        if isinstance(chunk, bytes):
            pass
        elif isinstance(chunk, int):
            chunk = util.int2bytes(chunk)
        elif isinstance(chunk, str):
            chunk = util.str2bytes(chunk)
        else:
            raise TypeError('Chunk not bytes, int or str: {0}'.format(chunk))

        # Check patch
        if isinstance(patch, bytes):
            pass
        elif isinstance(patch, int):
            patch = util.int2bytes(patch)
        elif isinstance(patch, str):
            patch = util.str2bytes(patch)
        elif isinstance(patch, type(None)):
            pass
        else:
            raise TypeError(
                'Patch not bytes, int, str or NoneType: {0}'.format(patch))

        return super(Chunk, cls).__new__(cls, chunk, chunkoffset, patch,
                                         patchoffset)
Example #3
0
 def test_int2bytes(self):
     test1 = -16
     self.assertRaises(TypeError, int2bytes, test1)
     test2 = 1
     test2_res = b'\x01'
     self.assertEqual(int2bytes(test2), test2_res)
     test3 = 15
     test3_res = b'\x0f'
     self.assertEqual(int2bytes(test3), test3_res)
     test4 = 256
     test4_res = b'\x01\x00'
     self.assertEqual(int2bytes(test4), test4_res)
Example #4
0
 def test_int2bytes(self):
     test1 = -16
     self.assertRaises(TypeError, int2bytes, test1)
     test2 = 1
     test2_res = b'\x01'
     self.assertEqual(int2bytes(test2), test2_res)
     test3 = 15
     test3_res = b'\x0f'
     self.assertEqual(int2bytes(test3), test3_res)
     test4 = 256
     test4_res = b'\x01\x00'
     self.assertEqual(int2bytes(test4), test4_res)