Registry / data / bsdiff4

bsdiff4

JSON →
library1.2.6pypypi✓ verified 89d ago

bsdiff4 is a Python library providing functions for binary diff and patch operations, based on the `bsdiff4` algorithm. It allows computing the difference between two byte sequences or files, and then reconstructing the new sequence/file from the old one and the diff. As of version 1.2.6, it provides a stable interface for managing binary data changes.

pip install bsdiff4
INSTALL
IMPORT
SIG · BSDIFF4
B
bsdiff4
datapythonv1.2.6
Install
1.6s avg
Import
5ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v1.2.6 · 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.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.9MB
glibc
py 3.10–3.920 runs
installs and imports cleanly · install 1.6s · import 0.001s · 18MB
16MB installed
● package 16MB
Code
Verified usage

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

diff
✓ import bsdiff4 # bsdiff4.diff(...)
patch
✓ import bsdiff4 # bsdiff4.patch(...)
file_diff
✓ import bsdiff4 # bsdiff4.file_diff(...)
file_patch
✓ import bsdiff4 # bsdiff4.file_patch(...)

This quickstart demonstrates how to use `bsdiff4` to generate and apply binary differences for both `bytes` objects in memory and actual files on disk. It shows how to compute a `diff` from old and new data, and then apply that `diff` to the old data to reconstruct the new data.

import bsdiff4 import os # Example with bytes old_data = b"This is the old data string." new_data = b"This is the new and updated data string." # Generate a binary diff diff_data = bsdiff4.diff(old_data, new_data) print(f"Diff data length: {len(diff_data)} bytes") # Apply the patch to get back the new data patched_data = bsdiff4.patch(old_data, diff_data) assert patched_data == new_data print(f"Patched data (bytes): {patched_data.decode('utf-8')}\n") # Example with files file_old = 'old_file.txt' file_new = 'new_file.txt' file_diff = 'diff.bin' file_patched = 'patched_file.txt' with open(file_old, 'wb') as f: f.write(old_data) with open(file_new, 'wb') as f: f.write(new_data) # Generate diff between files bsdiff4.file_diff(file_old, file_new, file_diff) print(f"File diff created: {file_diff}") # Apply patch to recreate new file bsdiff4.file_patch(file_old, file_patched, file_diff) with open(file_patched, 'rb') as f: recreated_data = f.read() assert recreated_data == new_data print(f"Patched file created: {file_patched} (content matches new_file.txt)") # Clean up generated files os.remove(file_old) os.remove(file_new) os.remove(file_diff) os.remove(file_patched)
Debug
Known issues
gotchaAll input data (old, new, and diff) for `bsdiff4.diff` and `bsdiff4.patch` functions must be `bytes` objects, not strings. Passing strings will result in `TypeError`.
fix
Ensure all string data is encoded to bytes (e.g., `my_string.encode('utf-8')`) before passing it to bsdiff4 functions.
affects: All versions
gotchaFor very large files, `bsdiff4` can be memory intensive, as it often loads entire files or significant portions into memory during diffing and patching operations. Monitor memory usage for performance-critical applications.
fix
Consider processing data in smaller chunks if feasible, or ensure sufficient RAM is available for operations on large files. The `file_diff` and `file_patch` functions might manage memory slightly better by handling file streams, but the core algorithm still requires memory proportional to the input size.
affects: All versions
gotchaThe `bsdiff4` library implements the `bsdiff4` format, which is a specific version of the bsdiff algorithm. Diffs generated by this library may not be compatible with other bsdiff implementations (e.g., bsdiff3, or other variations) if they use a different format version.
fix
When exchanging diffs with other systems or languages, ensure that all parties are using a `bsdiff4` compatible implementation to avoid parsing errors or incorrect patches.
affects: All versions
gotchaOn some systems, particularly those without pre-built binary wheels available on PyPI (e.g., specific Linux distributions, older macOS versions, or Windows without development tools), `pip install bsdiff4` might fail due to the need for a C compiler to build its native extensions.
fix
Install the necessary build tools and C compiler for your operating system (e.g., `build-essential` on Debian/Ubuntu, Xcode Command Line Tools on macOS, Visual C++ Build Tools on Windows) or use an environment with pre-built wheels.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'bsdiff4'
The 'bsdiff4' package is not installed in your Python environment.
fix
Install the package using pip: `pip install bsdiff4`
ERROR: Command errored out with exit status 1: ... running build_ext building 'bsdiff4.core' extension ... error: command 'gcc' failed with exit status 1
The 'bsdiff4' library includes a C extension that requires a C compiler and Python development headers to be present for successful installation.
fix
On Debian/Ubuntu, install `python3-dev` and `build-essential` (or `gcc`): `sudo apt-get install python3-dev build-essential`. On macOS, install Xcode Command Line Tools: `xcode-select --install`.
TypeError: expected bytes-like object, not str
The `bsdiff4.diff` and `bsdiff4.patch` functions operate on binary data and require byte sequences (`bytes`) as input, but a string (`str`) was provided.
fix
Convert your string data to bytes using the `.encode()` method, specifying an appropriate encoding (e.g., `my_string.encode('utf-8')`).
Upgrade
Version history
1.2.6latest on PyPI · released Feb 19, 2025
Audit
Dependencies

No dependency data recorded yet.

Agent activity
35 hits · last 30 days
node
34
OpenAI (training)
1
Resources
bsdiff4 — pip install bsdiff4 · libregistry