Python struct.pack_into() Examples

The following are 30 code examples for showing how to use struct.pack_into(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may want to check out the right sidebar which shows the related API usage.

You may also want to check out all available functions/classes of the module struct , or try the search function .

Example 1
Project: aerospike-admin   Author: aerospike   File: info.py    License: Apache License 2.0 6 votes vote down vote up
def _authenticate(sock, user, password, password_field_id):
    user = str_to_bytes(user)
    password = str_to_bytes(password)
    sz = len(user) + len(password) + 34 # 2 * 5 + 24
    send_buf = _admin_write_header(sz, _AUTHENTICATE, 2)
    fmt_str = "! I B %ds I B %ds" % (len(user), len(password))
    struct.pack_into(fmt_str, send_buf, _HEADER_SIZE,
                     len(user) + 1, _USER_FIELD_ID, user,
                     len(password) + 1, password_field_id, password)
    try:
        # OpenSSL wrapper doesn't support ctypes
        send_buf = _buffer_to_string(send_buf)
        sock.sendall(send_buf)
        recv_buff = _receivedata(sock, _HEADER_SIZE)
        rv = _admin_parse_header(recv_buff)
        return rv[2]
    except Exception as ex:
        raise IOError("Error: %s" % str(ex)) 
Example 2
Project: ironpython2   Author: IronLanguages   File: test_struct.py    License: Apache License 2.0 6 votes vote down vote up
def test_pack_into_fn(self):
        test_string = 'Reykjavik rocks, eow!'
        writable_buf = array.array('c', ' '*100)
        fmt = '21s'
        pack_into = lambda *args: struct.pack_into(fmt, *args)

        # Test without offset.
        pack_into(writable_buf, 0, test_string)
        from_buf = writable_buf.tostring()[:len(test_string)]
        self.assertEqual(from_buf, test_string)

        # Test with offset.
        pack_into(writable_buf, 10, test_string)
        from_buf = writable_buf.tostring()[:len(test_string)+10]
        self.assertEqual(from_buf, test_string[:10] + test_string)

        # Go beyond boundaries.
        small_buf = array.array('c', ' '*10)
        self.assertRaises((ValueError, struct.error), pack_into, small_buf, 0,
                          test_string)
        self.assertRaises((ValueError, struct.error), pack_into, small_buf, 2,
                          test_string) 
Example 3
Project: aiokafka   Author: robinhood   File: default_records.py    License: Apache License 2.0 6 votes vote down vote up
def write_header(self, use_compression_type=True):
        batch_len = len(self._buffer)
        self.HEADER_STRUCT.pack_into(
            self._buffer, 0,
            0,  # BaseOffset, set by broker
            batch_len - self.AFTER_LEN_OFFSET,  # Size from here to end
            self.NO_PARTITION_LEADER_EPOCH,
            self._magic,
            0,  # CRC will be set below, as we need a filled buffer for it
            self._get_attributes(use_compression_type),
            self._last_offset,
            self._first_timestamp or 0,
            self._max_timestamp or 0,
            self._producer_id,
            self._producer_epoch,
            self._base_sequence,
            self._num_records
        )
        crc = calc_crc32c(self._buffer[self.ATTRIBUTES_OFFSET:])
        struct.pack_into(">I", self._buffer, self.CRC_OFFSET, crc) 
Example 4
Project: aiokafka   Author: robinhood   File: test_legacy.py    License: Apache License 2.0 6 votes vote down vote up
def test_read_log_append_time_v1():
    buffer = _make_compressed_batch(1)

    # As Builder does not support creating data with `timestamp_type==1` we
    # patch the result manually

    buffer[ATTRIBUTES_OFFSET] |= TIMESTAMP_TYPE_MASK
    expected_timestamp = 10000000
    struct.pack_into(">q", buffer, TIMESTAMP_OFFSET, expected_timestamp)

    batch = LegacyRecordBatch(buffer, 1)
    msgs = list(batch)

    for offset, msg in enumerate(msgs):
        assert msg.offset == offset
        assert msg.timestamp == expected_timestamp
        assert msg.timestamp_type == 1 
Example 5
Project: BinderFilter   Author: dxwu   File: test_struct.py    License: MIT License 6 votes vote down vote up
def test_pack_into_fn(self):
        test_string = 'Reykjavik rocks, eow!'
        writable_buf = array.array('c', ' '*100)
        fmt = '21s'
        pack_into = lambda *args: struct.pack_into(fmt, *args)

        # Test without offset.
        pack_into(writable_buf, 0, test_string)
        from_buf = writable_buf.tostring()[:len(test_string)]
        self.assertEqual(from_buf, test_string)

        # Test with offset.
        pack_into(writable_buf, 10, test_string)
        from_buf = writable_buf.tostring()[:len(test_string)+10]
        self.assertEqual(from_buf, test_string[:10] + test_string)

        # Go beyond boundaries.
        small_buf = array.array('c', ' '*10)
        self.assertRaises((ValueError, struct.error), pack_into, small_buf, 0,
                          test_string)
        self.assertRaises((ValueError, struct.error), pack_into, small_buf, 2,
                          test_string) 
Example 6
Project: oss-ftp   Author: aliyun   File: test_struct.py    License: MIT License 6 votes vote down vote up
def test_pack_into_fn(self):
        test_string = 'Reykjavik rocks, eow!'
        writable_buf = array.array('c', ' '*100)
        fmt = '21s'
        pack_into = lambda *args: struct.pack_into(fmt, *args)

        # Test without offset.
        pack_into(writable_buf, 0, test_string)
        from_buf = writable_buf.tostring()[:len(test_string)]
        self.assertEqual(from_buf, test_string)

        # Test with offset.
        pack_into(writable_buf, 10, test_string)
        from_buf = writable_buf.tostring()[:len(test_string)+10]
        self.assertEqual(from_buf, test_string[:10] + test_string)

        # Go beyond boundaries.
        small_buf = array.array('c', ' '*10)
        self.assertRaises((ValueError, struct.error), pack_into, small_buf, 0,
                          test_string)
        self.assertRaises((ValueError, struct.error), pack_into, small_buf, 2,
                          test_string) 
Example 7
Project: Fluid-Designer   Author: Microvellum   File: test_struct.py    License: GNU General Public License v3.0 6 votes vote down vote up
def test_pack_into_fn(self):
        test_string = b'Reykjavik rocks, eow!'
        writable_buf = array.array('b', b' '*100)
        fmt = '21s'
        pack_into = lambda *args: struct.pack_into(fmt, *args)

        # Test without offset.
        pack_into(writable_buf, 0, test_string)
        from_buf = writable_buf.tobytes()[:len(test_string)]
        self.assertEqual(from_buf, test_string)

        # Test with offset.
        pack_into(writable_buf, 10, test_string)
        from_buf = writable_buf.tobytes()[:len(test_string)+10]
        self.assertEqual(from_buf, test_string[:10] + test_string)

        # Go beyond boundaries.
        small_buf = array.array('b', b' '*10)
        self.assertRaises((ValueError, struct.error), pack_into, small_buf, 0,
                          test_string)
        self.assertRaises((ValueError, struct.error), pack_into, small_buf, 2,
                          test_string) 
Example 8
Project: Fluid-Designer   Author: Microvellum   File: test_struct.py    License: GNU General Public License v3.0 6 votes vote down vote up
def test_trailing_counter(self):
        store = array.array('b', b' '*100)

        # format lists containing only count spec should result in an error
        self.assertRaises(struct.error, struct.pack, '12345')
        self.assertRaises(struct.error, struct.unpack, '12345', '')
        self.assertRaises(struct.error, struct.pack_into, '12345', store, 0)
        self.assertRaises(struct.error, struct.unpack_from, '12345', store, 0)

        # Format lists with trailing count spec should result in an error
        self.assertRaises(struct.error, struct.pack, 'c12345', 'x')
        self.assertRaises(struct.error, struct.unpack, 'c12345', 'x')
        self.assertRaises(struct.error, struct.pack_into, 'c12345', store, 0,
                           'x')
        self.assertRaises(struct.error, struct.unpack_from, 'c12345', store,
                           0)

        # Mixed format tests
        self.assertRaises(struct.error, struct.pack, '14s42', 'spam and eggs')
        self.assertRaises(struct.error, struct.unpack, '14s42',
                          'spam and eggs')
        self.assertRaises(struct.error, struct.pack_into, '14s42', store, 0,
                          'spam and eggs')
        self.assertRaises(struct.error, struct.unpack_from, '14s42', store, 0) 
Example 9
Project: cle   Author: angr   File: memory.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def pack(self, addr, fmt, *data):
        """
        Use the ``struct`` module to pack `data` into memory at address `addr` with the format `fmt`.
        """

        try:
            start, backer = next(self.backers(addr))
        except StopIteration:
            raise KeyError(addr)

        if start > addr:
            raise KeyError(addr)

        try:
            return struct.pack_into(fmt, backer, addr - start, *data)
        except struct.error as e:
            if len(backer) - (addr - start) >= struct.calcsize(fmt):
                raise e
            raise KeyError(addr) 
Example 10
Project: Miyamoto   Author: aboood40091   File: area.py    License: GNU General Public License v3.0 6 votes vote down vote up
def SaveEntrances(self):
        """
        Saves the entrances back to block 7
        """
        offset = 0
        entstruct = struct.Struct('>HHhhBBBBBBxBHBBBBBx')
        buffer = bytearray(len(self.entrances) * 24)
        zonelist = self.zones
        for entrance in self.entrances:
            zoneID = SLib.MapPositionToZoneID(zonelist, entrance.objx, entrance.objy)
            try:
                entstruct.pack_into(buffer, offset, int(entrance.objx), int(entrance.objy), int(entrance.camerax),
                                    int(entrance.cameray), int(entrance.entid), int(entrance.destarea), int(entrance.destentrance),
                                    int(entrance.enttype), int(entrance.players), zoneID, int(entrance.playerDistance),
                                    int(entrance.entsettings), int(entrance.otherID), int(entrance.coinOrder),
                                    int(entrance.pathID), int(entrance.pathnodeindex), int(entrance.transition))
            except struct.error:
                if zoneID < 0:
                    raise ValueError('Entrance %d at (%d, %d) is too far from any zone\'s boundaries!\nPlease place it near a zone.' % (entrance.entid, int(entrance.objx), int(entrance.objy))) from None
                else:
                    raise ValueError('SaveEntrances struct.error.')
            offset += 24
        self.blocks[6] = bytes(buffer) 
Example 11
Project: Miyamoto   Author: aboood40091   File: area.py    License: GNU General Public License v3.0 6 votes vote down vote up
def SaveLoadedSprites(self):
        """
        Saves the list of loaded sprites back to block 9
        """
        ls = []
        for sprite in self.sprites:
            if sprite.type not in ls: ls.append(sprite.type)
        ls.sort()

        offset = 0
        sprstruct = struct.Struct('>Hxx')
        buffer = bytearray(len(ls) * 4)
        for s in ls:
            sprstruct.pack_into(buffer, offset, int(s))
            offset += 4
        self.blocks[8] = bytes(buffer) 
Example 12
Project: Adafruit_CircuitPython_CCS811   Author: adafruit   File: adafruit_ccs811.py    License: MIT License 6 votes vote down vote up
def set_environmental_data(self, humidity, temperature):
        """Set the temperature and humidity used when computing eCO2 and TVOC values.

        :param int humidity: The current relative humidity in percent.
        :param float temperature: The current temperature in Celsius."""
        # Humidity is stored as an unsigned 16 bits in 1/512%RH. The default
        # value is 50% = 0x64, 0x00. As an example 48.5% humidity would be 0x61,
        # 0x00.
        humidity = int(humidity * 512)

        # Temperature is stored as an unsigned 16 bits integer in 1/512 degrees
        # there is an offset: 0 maps to -25C. The default value is 25C = 0x64,
        # 0x00. As an example 23.5% temperature would be 0x61, 0x00.
        temperature = int((temperature + 25) * 512)

        buf = bytearray(5)
        buf[0] = _ENV_DATA
        struct.pack_into(">HH", buf, 1, humidity, temperature)

        with self.i2c_device as i2c:
            i2c.write(buf) 
Example 13
Project: ironpython3   Author: IronLanguages   File: test_struct.py    License: Apache License 2.0 6 votes vote down vote up
def test_pack_into_fn(self):
        test_string = b'Reykjavik rocks, eow!'
        writable_buf = array.array('b', b' '*100)
        fmt = '21s'
        pack_into = lambda *args: struct.pack_into(fmt, *args)

        # Test without offset.
        pack_into(writable_buf, 0, test_string)
        from_buf = writable_buf.tobytes()[:len(test_string)]
        self.assertEqual(from_buf, test_string)

        # Test with offset.
        pack_into(writable_buf, 10, test_string)
        from_buf = writable_buf.tobytes()[:len(test_string)+10]
        self.assertEqual(from_buf, test_string[:10] + test_string)

        # Go beyond boundaries.
        small_buf = array.array('b', b' '*10)
        self.assertRaises((ValueError, struct.error), pack_into, small_buf, 0,
                          test_string)
        self.assertRaises((ValueError, struct.error), pack_into, small_buf, 2,
                          test_string) 
Example 14
Project: ironpython3   Author: IronLanguages   File: test_struct.py    License: Apache License 2.0 6 votes vote down vote up
def test_trailing_counter(self):
        store = array.array('b', b' '*100)

        # format lists containing only count spec should result in an error
        self.assertRaises(struct.error, struct.pack, '12345')
        self.assertRaises(struct.error, struct.unpack, '12345', '')
        self.assertRaises(struct.error, struct.pack_into, '12345', store, 0)
        self.assertRaises(struct.error, struct.unpack_from, '12345', store, 0)

        # Format lists with trailing count spec should result in an error
        self.assertRaises(struct.error, struct.pack, 'c12345', 'x')
        self.assertRaises(struct.error, struct.unpack, 'c12345', 'x')
        self.assertRaises(struct.error, struct.pack_into, 'c12345', store, 0,
                           'x')
        self.assertRaises(struct.error, struct.unpack_from, 'c12345', store,
                           0)

        # Mixed format tests
        self.assertRaises(struct.error, struct.pack, '14s42', 'spam and eggs')
        self.assertRaises(struct.error, struct.unpack, '14s42',
                          'spam and eggs')
        self.assertRaises(struct.error, struct.pack_into, '14s42', store, 0,
                          'spam and eggs')
        self.assertRaises(struct.error, struct.unpack_from, '14s42', store, 0) 
Example 15
Project: ryu   Author: OpenState-SDN   File: test_icmp.py    License: Apache License 2.0 6 votes vote down vote up
def setUp_with_echo(self):
        self.echo_id = 13379
        self.echo_seq = 1
        self.echo_data = b'\x30\x0e\x09\x00\x00\x00\x00\x00' \
            + b'\x10\x11\x12\x13\x14\x15\x16\x17' \
            + b'\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f' \
            + b'\x20\x21\x22\x23\x24\x25\x26\x27' \
            + b'\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f' \
            + b'\x30\x31\x32\x33\x34\x35\x36\x37'
        self.data = icmp.echo(
            id_=self.echo_id, seq=self.echo_seq, data=self.echo_data)

        self.type_ = icmp.ICMP_ECHO_REQUEST
        self.code = 0
        self.ic = icmp.icmp(self.type_, self.code, self.csum, self.data)

        self.buf = bytearray(struct.pack(
            icmp.icmp._PACK_STR, self.type_, self.code, self.csum))
        self.buf += self.data.serialize()
        self.csum_calc = packet_utils.checksum(self.buf)
        struct.pack_into('!H', self.buf, 2, self.csum_calc) 
Example 16
Project: ryu   Author: OpenState-SDN   File: test_icmp.py    License: Apache License 2.0 6 votes vote down vote up
def setUp_with_dest_unreach(self):
        self.unreach_mtu = 10
        self.unreach_data = b'abc'
        self.unreach_data_len = len(self.unreach_data)
        self.data = icmp.dest_unreach(
            data_len=self.unreach_data_len, mtu=self.unreach_mtu,
            data=self.unreach_data)

        self.type_ = icmp.ICMP_DEST_UNREACH
        self.code = icmp.ICMP_HOST_UNREACH_CODE
        self.ic = icmp.icmp(self.type_, self.code, self.csum, self.data)

        self.buf = bytearray(struct.pack(
            icmp.icmp._PACK_STR, self.type_, self.code, self.csum))
        self.buf += self.data.serialize()
        self.csum_calc = packet_utils.checksum(self.buf)
        struct.pack_into('!H', self.buf, 2, self.csum_calc) 
Example 17
Project: discord.py   Author: Rapptz   File: gateway.py    License: MIT License 5 votes vote down vote up
def initial_connection(self, data):
        state = self._connection
        state.ssrc = data['ssrc']
        state.voice_port = data['port']
        state.endpoint_ip = data['ip']

        packet = bytearray(70)
        struct.pack_into('>I', packet, 0, state.ssrc)
        state.socket.sendto(packet, (state.endpoint_ip, state.voice_port))
        recv = await self.loop.sock_recv(state.socket, 70)
        log.debug('received packet in initial_connection: %s', recv)

        # the ip is ascii starting at the 4th byte and ending at the first null
        ip_start = 4
        ip_end = recv.index(0, ip_start)
        state.ip = recv[ip_start:ip_end].decode('ascii')

        state.port = struct.unpack_from('>H', recv, len(recv) - 2)[0]
        log.debug('detected ip: %s port: %s', state.ip, state.port)

        # there *should* always be at least one supported mode (xsalsa20_poly1305)
        modes = [mode for mode in data['modes'] if mode in self._connection.supported_modes]
        log.debug('received supported encryption modes: %s', ", ".join(modes))

        mode = modes[0]
        await self.select_protocol(state.ip, state.port, mode)
        log.info('selected the voice protocol for use (%s)', mode)

        await self.client_connect() 
Example 18
Project: discord.py   Author: Rapptz   File: voice_client.py    License: MIT License 5 votes vote down vote up
def _get_voice_packet(self, data):
        header = bytearray(12)

        # Formulate rtp header
        header[0] = 0x80
        header[1] = 0x78
        struct.pack_into('>H', header, 2, self.sequence)
        struct.pack_into('>I', header, 4, self.timestamp)
        struct.pack_into('>I', header, 8, self.ssrc)

        encrypt_packet = getattr(self, '_encrypt_' + self.mode)
        return encrypt_packet(header, data) 
Example 19
Project: aerospike-admin   Author: aerospike   File: info.py    License: Apache License 2.0 5 votes vote down vote up
def _admin_write_header(sz, command, field_count):
    send_buf = create_string_buffer(sz)      # from ctypes
    sz = (_ADMIN_MSG_VERSION << 56) | (_ADMIN_MSG_TYPE << 48) | (sz - 8)

    if g_struct_admin_header_out != None:
        g_struct_admin_header_out.pack_into(
            send_buf, 0, sz, 0, 0, command, field_count)
    else:
        struct.pack_into(
            admin_header_fmt, send_buf, 0, sz, 0, 0, command, field_count)

    return send_buf 
Example 20
Project: ironpython2   Author: IronLanguages   File: test_struct.py    License: Apache License 2.0 5 votes vote down vote up
def test_pack_into(self, cls=bytearray, tobytes=str):
        test_string = 'Reykjavik rocks, eow!'
        writable_buf = cls(' '*100)
        fmt = '21s'
        s = struct.Struct(fmt)

        # Test without offset
        s.pack_into(writable_buf, 0, test_string)
        from_buf = tobytes(writable_buf)[:len(test_string)]
        self.assertEqual(from_buf, test_string)

        # Test with offset.
        s.pack_into(writable_buf, 10, test_string)
        from_buf = tobytes(writable_buf)[:len(test_string)+10]
        self.assertEqual(from_buf, test_string[:10] + test_string)

        # Go beyond boundaries.
        small_buf = cls(' '*10)
        self.assertRaises((ValueError, struct.error), s.pack_into, small_buf, 0,
                          test_string)
        self.assertRaises((ValueError, struct.error), s.pack_into, small_buf, 2,
                          test_string)

        # Test bogus offset (issue 3694)
        sb = small_buf
        self.assertRaises((TypeError, struct.error), struct.pack_into, b'', sb,
                          None) 
Example 21
Project: ironpython2   Author: IronLanguages   File: test_struct.py    License: Apache License 2.0 5 votes vote down vote up
def test_pack_into(self):
        # test string format string
        result = array.array('b', [0, 0])
        struct.pack_into('>H', result, 0, 0xABCD)
        self.assertSequenceEqual(result, array.array('b', b"\xAB\xCD"))

        # test bytes format string
        result = array.array('b', [0, 0])
        struct.pack_into(b'>H', result, 0, 0xABCD)
        self.assertSequenceEqual(result, array.array('b', b"\xAB\xCD"))

        # test bytearray
        result = bytearray(b'\x00\x00')
        struct.pack_into('>H', result, 0, 0xABCD)
        self.assertSequenceEqual(result, bytearray(b"\xAB\xCD")) 
Example 22
Project: ringbuffer   Author: bslatkin   File: ringbuffer.py    License: Apache License 2.0 5 votes vote down vote up
def __setitem__(self, i, data):
        data_view = memoryview(data).cast('@B')
        data_size = len(data_view)
        if data_size > self.slot_bytes:
            raise DataTooLargeError('%d bytes too big for slot' % data_size)

        # Avoid copying the input data! Do only a single copy into the slot.
        slot_view = memoryview(self.array[i]).cast('@B')
        struct.pack_into('>I', slot_view, 0, data_size)
        start = self.length_bytes
        slot_view[start:start + data_size] = data_view 
Example 23
Project: ringbuffer   Author: bslatkin   File: perf_test_ringbuffer.py    License: Apache License 2.0 5 votes vote down vote up
def generate_verifiable_data(num_bytes):
    random_size = num_bytes - 4
    random_data = get_random_data(random_size)
    crc = get_crc32(random_data)

    result = bytearray(num_bytes)
    result[:random_size] = random_data
    struct.pack_into('>I', result, random_size, crc)

    return result 
Example 24
Project: buttersink   Author: AmesCornish   File: send.py    License: GNU General Public License v3.0 5 votes vote down vote up
def TLV_PUT(attrs, attrNum, format, value):
    """ Put a tag-length-value encoded attribute. """
    attrView = attrs[attrNum]
    if format == 's':
        format = str(attrView.len) + format
    struct.pack_into(format, attrView.buf, attrView.offset, value) 
Example 25
Project: aiokafka   Author: robinhood   File: test_legacy.py    License: Apache License 2.0 5 votes vote down vote up
def test_read_write_serde_v0_v1_with_compression(compression_type, magic):
    builder = LegacyRecordBatchBuilder(
        magic=magic, compression_type=compression_type, batch_size=1024 * 1024)
    for offset in range(10):
        builder.append(
            offset, timestamp=9999999, key=b"test", value=b"Super")
    buffer = builder.build()

    # Broker will set the offset to a proper last offset value
    struct.pack_into(">q", buffer, 0, 9)

    batch = LegacyRecordBatch(buffer, magic)
    assert batch.validate_crc()

    assert batch.is_control_batch is False
    assert batch.is_transactional is False
    assert batch.producer_id is None
    assert batch.next_offset == 10

    msgs = list(batch)

    for offset, msg in enumerate(msgs):
        assert msg.offset == offset
        assert msg.timestamp == (9999999 if magic else None)
        assert msg.timestamp_type == (0 if magic else None)
        assert msg.key == b"test"
        assert msg.value == b"Super"
        assert msg.checksum == (-2095076219 if magic else 278251978) & \
            0xffffffff 
Example 26
Project: aiokafka   Author: robinhood   File: test_legacy.py    License: Apache License 2.0 5 votes vote down vote up
def test_reader_corrupt_record_v0_v1(magic):
    buffer = _make_compressed_batch(magic)
    len_offset = 8

    # If the wrapper of compressed messages has a key it will just be ignored.
    key_offset = 26 if magic else 18
    new_buffer = (
        buffer[:key_offset] +
        b"\x00\x00\x00\x03123" +  # Insert some KEY into wrapper
        buffer[key_offset + 4:]  # Ignore the 4 byte -1 value for old KEY==None
    )
    struct.pack_into(">i", new_buffer, len_offset, len(new_buffer) - 12)
    batch = LegacyRecordBatch(new_buffer, magic)
    msgs = list(batch)
    for offset, msg in enumerate(msgs):
        assert msg.offset == offset
        assert msg.timestamp == (9999999 if magic else None)
        assert msg.timestamp_type == (0 if magic else None)
        assert msg.key == b"test"
        assert msg.value == b"Super"
        assert msg.checksum == (-2095076219 if magic else 278251978) & \
            0xffffffff

    # If the wrapper does not contain a `value` it's corrupted
    value_offset = 30 if magic else 22
    new_buffer = (
        buffer[:value_offset] +
        b"\xff\xff\xff\xff"  # Set `value` to None by altering size to -1
    )
    struct.pack_into(">i", new_buffer, len_offset, len(new_buffer) - 12)
    with pytest.raises(
            CorruptRecordException,
            match="Value of compressed message is None"):
        batch = LegacyRecordBatch(new_buffer, magic)
        list(batch) 
Example 27
Project: PyCNC   Author: Nikolay-Kha   File: rpgpio_private.py    License: MIT License 5 votes vote down vote up
def write(self, address, fmt, data):
        struct.pack_into(fmt, self._memmap, address, *data) 
Example 28
Project: BinderFilter   Author: dxwu   File: test_struct.py    License: MIT License 5 votes vote down vote up
def test_pack_into(self):
        test_string = 'Reykjavik rocks, eow!'
        writable_buf = array.array('c', ' '*100)
        fmt = '21s'
        s = struct.Struct(fmt)

        # Test without offset
        s.pack_into(writable_buf, 0, test_string)
        from_buf = writable_buf.tostring()[:len(test_string)]
        self.assertEqual(from_buf, test_string)

        # Test with offset.
        s.pack_into(writable_buf, 10, test_string)
        from_buf = writable_buf.tostring()[:len(test_string)+10]
        self.assertEqual(from_buf, test_string[:10] + test_string)

        # Go beyond boundaries.
        small_buf = array.array('c', ' '*10)
        self.assertRaises((ValueError, struct.error), s.pack_into, small_buf, 0,
                          test_string)
        self.assertRaises((ValueError, struct.error), s.pack_into, small_buf, 2,
                          test_string)

        # Test bogus offset (issue 3694)
        sb = small_buf
        self.assertRaises((TypeError, struct.error), struct.pack_into, b'', sb,
                          None) 
Example 29
Project: oss-ftp   Author: aliyun   File: test_struct.py    License: MIT License 5 votes vote down vote up
def test_pack_into(self):
        test_string = 'Reykjavik rocks, eow!'
        writable_buf = array.array('c', ' '*100)
        fmt = '21s'
        s = struct.Struct(fmt)

        # Test without offset
        s.pack_into(writable_buf, 0, test_string)
        from_buf = writable_buf.tostring()[:len(test_string)]
        self.assertEqual(from_buf, test_string)

        # Test with offset.
        s.pack_into(writable_buf, 10, test_string)
        from_buf = writable_buf.tostring()[:len(test_string)+10]
        self.assertEqual(from_buf, test_string[:10] + test_string)

        # Go beyond boundaries.
        small_buf = array.array('c', ' '*10)
        self.assertRaises((ValueError, struct.error), s.pack_into, small_buf, 0,
                          test_string)
        self.assertRaises((ValueError, struct.error), s.pack_into, small_buf, 2,
                          test_string)

        # Test bogus offset (issue 3694)
        sb = small_buf
        self.assertRaises((TypeError, struct.error), struct.pack_into, b'', sb,
                          None) 
Example 30
Project: pyaaf2   Author: markreidvfx   File: mobid.py    License: MIT License 5 votes vote down vote up
def SMPTELabel(self, value):
        struct.pack_into(str('12B'), self.bytes_le, 0, *value)