Install & Compatibility
Where this runs
tested against v1.0.7 · 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
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.040s · 17.9MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.6s · import 0.034s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
whatthepatch
✓ import whatthepatch
parse_patch
✓ from whatthepatch import parse_patch
Often accessed via `whatthepatch.parse_patch` after `import whatthepatch`.
apply_diff
✓ from whatthepatch import apply_diff
Often accessed via `whatthepatch.apply_diff` after `import whatthepatch`.
This example demonstrates how to parse a patch string and then apply the first detected diff to an original file's content. It prints the parsed changes and the resulting patched content.
import whatthepatch
import io
patch_content = """--- lao\t2012-12-26 23:16:54.000000000 -0600
+++ tzu\t2012-12-26 23:16:50.000000000 -0600
@@ -1,7 +1,6 @@
-The Way that can be told of is not the eternal Way;
-The name that can be named is not the eternal name.
The Nameless is the origin of Heaven and Earth;
-The Named is the mother of all things.
+The named is the mother of all things.
Therefore let there always be non-being, so we may see their subtlety,
And let there always be being,
@@ -9,3 +8,6 @@
The two are the same,
But after they are produced,
they have different names.
+They both may be called deep and profound.
+Deeper and more profound,
+The door of all subtleties!"""
original_file_content = """The Way that can be told of is not the eternal Way;
The name that can be named is not the eternal name.
The Nameless is the origin of Heaven and Earth;
The Named is the mother of all things.
Therefore let there always be non-being, so we may see their subtlety,
And let there always be being,
The two are the same,
But after they are produced,
they have different names."""
# Parse the patch
print("--- Parsing Patch ---")
patches = list(whatthepatch.parse_patch(patch_content))
for diff in patches:
print(f"Diff for file: {diff.header.old_path} -> {diff.header.new_path}")
for change in diff.changes:
print(f" Change: old={change.old}, new={change.new}, line='{change.line.strip()}'")
# Apply the patch (example for the first diff in the patch)
if patches:
first_diff = patches[0]
print("\n--- Applying Patch ---")
# Simulate reading the original file lines
original_lines = original_file_content.splitlines(keepends=True)
try:
patched_lines = whatthepatch.apply_diff(first_diff, original_lines)
print("Patch applied successfully. New content:")
print("".join(patched_lines))
except whatthepatch.exceptions.PatchException as e:
print(f"Failed to apply patch: {e}")
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'whatthepatch'
The 'whatthepatch' library is not installed in the current Python environment.
fixInstall the library using pip: `pip install whatthepatch`
TypeError: parse_patch() argument 'patch_text' must be str, not bytes
The `parse_patch` function expects a string containing the patch content, but bytes were provided instead.
fixDecode the bytes into a string (e.g., using `.decode('utf-8')`) before passing them to `parse_patch`. TypeError: apply() argument 'root' must be Path or None, not str
The `apply` method's `root` argument expects a `pathlib.Path` object or `None`, but a string was provided.
fixConvert the root path string to a `pathlib.Path` object using `Path('your/path/string')` before passing it to the `apply` method. AttributeError: 'Diff' object has no attribute 'old_file_name'
The `Diff` object does not have a direct `old_file_name` attribute; file names are accessed through the `header_old` and `header_new` attributes.
fixAccess the old file name via `diff.header_old.file_name` and the new file name via `diff.header_new.file_name`.
Upgrade
Version history
1.0.7latest on PyPI · released Nov 16, 2024
Audit
Dependencies
pythonrequiredRequires Python 3.9 or newer.