{"id":2097,"library":"leb128","title":"LEB128 (Little Endian Base 128) Encoder/Decoder","description":"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.","status":"active","version":"1.0.9","language":"python","source_language":"en","source_url":"https://github.com/mohanson/leb128","tags":["encoding","decoding","leb128","variable-length integer","binary","webassembly","dwarf"],"install":[{"cmd":"pip install leb128","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"Imports the unsigned LEB128 encoder/decoder object.","symbol":"u","correct":"from leb128 import u"},{"note":"Imports the signed LEB128 encoder/decoder object.","symbol":"i","correct":"from leb128 import i"},{"note":"Imports the module, then use `leb128.u` and `leb128.i`.","symbol":"leb128","correct":"import leb128"}],"quickstart":{"code":"import io\nimport leb128\n\n# --- Unsigned LEB128 ---\n# Encode an unsigned integer\nunsigned_value = 624485\nbuf_u = io.BytesIO()\nleb128.u.encode(buf_u, unsigned_value)\nprint(f\"Encoded {unsigned_value} (unsigned): {buf_u.getvalue().hex()}\")\n\n# To decode, reset buffer position and read\nbuf_u.seek(0)\ndecoded_u = leb128.u.decode(buf_u)\nprint(f\"Decoded unsigned: {decod_u}\")\nassert decoded_u == unsigned_value\n\n# --- Signed LEB128 ---\n# Encode a signed integer\nsigned_value = -12345\nbuf_i = io.BytesIO()\nleb128.i.encode(buf_i, signed_value)\nprint(f\"Encoded {signed_value} (signed): {buf_i.getvalue().hex()}\")\n\n# To decode, reset buffer position and read\nbuf_i.seek(0)\ndecoded_i = leb128.i.decode(buf_i)\nprint(f\"Decoded signed: {decod_i}\")\nassert decoded_i == signed_value\n\nprint(\"LEB128 encoding and decoding successful!\")","lang":"python","description":"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."},"warnings":[{"fix":"Use `leb128.u.encode`/`leb128.u.decode` for unsigned integers and `leb128.i.encode`/`leb128.i.decode` for signed integers.","message":"Always 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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"After `encode()` and before `decode()` on the same `io.BytesIO` object, insert `buffer_object.seek(0)`.","message":"When 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"While 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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"search_vec":"'1.0.9':64 '128':5,24 '2026':81 'appear':66 'base':4,23 'binari':45,89 'cadenc':74 'common':33 'compress':28 'context':36 'current':61 'debug':40 'decod':16,83 'dwarf':39,91 'encod':14,46,82 'encoder/decoder':6 'endian':3,22 'file':41 'format':29,31,42 'function':12 'infrequ':72 'integ':17,48,88 'januari':80 'latest':77 'leb128':1,8,20,57,84 'length':27,87 'librari':10,51 'like':37 'liter':49 'littl':2,21 'oper':58 'provid':11 'python':9 'releas':73,78 'sign':56 'stabl':70 'support':52 'though':71 'unsign':54 'use':18,34 'variabl':26,86 'variable-length':25,85 'version':63 'webassembl':44,90","created_at":"2026-04-09T18:43:24.190369+00:00","updated_at":"2026-04-16T16:11:09.892279+00:00","problems":[{"fix":"Install the library using pip: `pip install leb128`","cause":"The `leb128` library is not installed in your Python environment or the Python interpreter cannot locate it in its search path.","error":"ModuleNotFoundError: No module named 'leb128'"},{"fix":"Ensure that the value passed to the encoding function is an actual integer: `leb128.u.encode(12345)`","cause":"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.","error":"TypeError: an integer is required (got type str)"},{"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)`","cause":"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.","error":"AttributeError: module 'leb128' has no attribute 'encode'"},{"fix":"Provide a bytes-like object (e.g., `bytes` or `bytearray`) to the decoding function: `leb128.u.decode(b'\\xe5\\x8e\\x26')`","cause":"The `leb128.u.decode()` or `leb128.s.decode()` function was called with a string argument instead of a bytes-like object.","error":"TypeError: expected bytes, bytearray or memoryview, not str"},{"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.","cause":"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.","error":"ValueError: Incomplete LEB128 sequence"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"1.0.9","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/mohanson/leb128","docs":null,"changelog":null,"pypi":"https://pypi.org/project/leb128/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["serialization","data"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-28","next_check":"2026-07-28","install_tag":null}}