The `leb128` Python library provides functionality for encoding and decoding integers using the LEB128 (Little Endian Base 128) variable-length compression format. This format is commonly used in contexts like the DWARF debug file format and WebAssembly binary encoding for integer literals. The library supports both unsigned and signed LEB128 operations. It is currently at version 1.0.9 and appears to have a stable, though infrequent, release cadence, with the latest release in January 2026.
pip install leb128Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates encoding and decoding both unsigned and signed integers using the `leb128` library. It uses `io.BytesIO` as a file-like object for writing and reading the LEB128 encoded bytes, highlighting the necessity to `seek(0)` the buffer before decoding after an encode operation.
Use `leb128.u.encode`/`leb128.u.decode` for unsigned integers and `leb128.i.encode`/`leb128.i.decode` for signed integers.
After `encode()` and before `decode()` on the same `io.BytesIO` object, insert `buffer_object.seek(0)`.
Validate the magnitude of integers being encoded against the limitations of the system that will ultimately decode and use them. Python itself will handle arbitrary sizes without `OverflowError` for its own operations.
Install the library using pip: `pip install leb128`
Ensure that the value passed to the encoding function is an actual integer: `leb128.u.encode(12345)`
Access the encoding/decoding functions through `leb128.u` for unsigned or `leb128.s` for signed operations: `leb128.u.encode(value)` or `leb128.s.decode(bytes_data)`
Provide a bytes-like object (e.g., `bytes` or `bytearray`) to the decoding function: `leb128.u.decode(b'\xe5\x8e\x26')`
Ensure the input bytes are a valid, complete LEB128 encoded sequence. You might need to check the source of the bytes or handle cases where the input stream is unexpectedly cut short.
No dependency data recorded yet.