Registry / serialization / leb128

leb128

JSON →
library1.0.9pypypi✓ verified 24d ago

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 leb128
INSTALL
IMPORT
SIG · LEB128
L
leb128
serializationpythonv1.0.9
Install
1.5s avg
Import
11ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.9 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.012s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.006s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

u
from leb128 import u
Imports the unsigned LEB128 encoder/decoder object.
i
from leb128 import i
Imports the signed LEB128 encoder/decoder object.
leb128
import leb128
Imports the module, then use `leb128.u` and `leb128.i`.

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.

import io import leb128 # --- Unsigned LEB128 --- # Encode an unsigned integer unsigned_value = 624485 buf_u = io.BytesIO() leb128.u.encode(buf_u, unsigned_value) print(f"Encoded {unsigned_value} (unsigned): {buf_u.getvalue().hex()}") # To decode, reset buffer position and read buf_u.seek(0) decoded_u = leb128.u.decode(buf_u) print(f"Decoded unsigned: {decod_u}") assert decoded_u == unsigned_value # --- Signed LEB128 --- # Encode a signed integer signed_value = -12345 buf_i = io.BytesIO() leb128.i.encode(buf_i, signed_value) print(f"Encoded {signed_value} (signed): {buf_i.getvalue().hex()}") # To decode, reset buffer position and read buf_i.seek(0) decoded_i = leb128.i.decode(buf_i) print(f"Decoded signed: {decod_i}") assert decoded_i == signed_value print("LEB128 encoding and decoding successful!")
Debug
Known issues
gotchaAlways distinguish between unsigned (`leb128.u`) and signed (`leb128.i`) encoding/decoding. Using the wrong one will result in incorrect values or decoding errors, as LEB128's interpretation of the sign bit differs.
fix
Use `leb128.u.encode`/`leb128.u.decode` for unsigned integers and `leb128.i.encode`/`leb128.i.decode` for signed integers.
affects: All versions
gotchaWhen using `io.BytesIO` or any file-like object, remember to call `seek(0)` on the buffer after encoding to reset its position before attempting to decode the written bytes. Failure to do so will result in reading from the end of the buffer, leading to unexpected results or errors.
fix
After `encode()` and before `decode()` on the same `io.BytesIO` object, insert `buffer_object.seek(0)`.
affects: All versions
gotchaWhile Python integers support arbitrary precision, be mindful of the maximum integer size supported by the *target system* if these LEB128 encoded bytes are being exchanged with other languages or environments (e.g., C, WebAssembly). Encoding excessively large numbers that exceed the recipient's integer type could lead to truncation or overflow on the receiving end.
fix
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.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'leb128'
The `leb128` library is not installed in your Python environment or the Python interpreter cannot locate it in its search path.
fix
Install the library using pip: `pip install leb128`
TypeError: an integer is required (got type str)
The `leb128.u.encode()` or `leb128.s.encode()` function was called with a non-integer argument, such as a string, when it expects an integer for encoding.
fix
Ensure that the value passed to the encoding function is an actual integer: `leb128.u.encode(12345)`
AttributeError: module 'leb128' has no attribute 'encode'
You are trying to call `encode` or `decode` directly on the `leb128` module, but these functions are typically accessed via the unsigned (`u`) or signed (`s`) sub-modules.
fix
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)`
TypeError: expected bytes, bytearray or memoryview, not str
The `leb128.u.decode()` or `leb128.s.decode()` function was called with a string argument instead of a bytes-like object.
fix
Provide a bytes-like object (e.g., `bytes` or `bytearray`) to the decoding function: `leb128.u.decode(b'\xe5\x8e\x26')`
ValueError: Incomplete LEB128 sequence
The input byte sequence provided to a decoding function (`leb128.u.decode()` or `leb128.s.decode()`) is malformed, truncated, or does not represent a complete LEB128 integer.
fix
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.
Upgrade
Version history
1.0.9latest on PyPI · released Jan 9, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Resources
leb128 — pip install leb128 · libregistry