Registry / serialization / whatthepatch

whatthepatch

JSON →
library1.0.7pypypi✓ verified 24d ago

What The Patch is a Python library for parsing and applying patch files. It is currently at version 1.0.7, with its latest release on November 16, 2024. While the functions are stable and reliable, the project has historically had limited active development from the maintainer, though pull requests are considered and released.

pip install whatthepatch
INSTALL
IMPORT
SIG · WHATTHEPATCH
W
whatthepatch
serializationpythonv1.0.7
Install
1.6s avg
Import
37ms
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.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
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.040s · 17.9MB
glibc
py 3.103.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}")
Debug
Known issues
breakingVersion 1.0.0 dropped support for Python 2 and Python 3.4. Users on these older Python versions must use `whatthepatch<1.0.0` or upgrade their Python environment.
fix
Upgrade Python to 3.9+ or pin `whatthepatch` version to `<1.0.0`.
affects: <1.0.0 to 1.0.0
gotchaAn incorrect regular expression matching `diffcmd` was fixed in version 1.0.7. Older versions might misinterpret diff commands in some patch files, leading to parsing errors.
fix
Upgrade to `whatthepatch` version 1.0.7 or newer.
affects: <1.0.7
gotchaIssues with parsing binary diffs and context diffs were fixed in versions 1.0.5 and 1.0.1 respectively. Users dealing with these specific patch formats in older library versions might encounter parsing failures.
fix
Upgrade to `whatthepatch` version 1.0.5 or newer to ensure correct parsing of binary and context diffs.
affects: <1.0.5
gotchaThe project's maintainer notes limited active development; while pull requests are considered, direct issue fixes by the maintainer are not guaranteed. Users encountering bugs may need to contribute fixes.
fix
Be prepared to contribute pull requests for bug fixes or rely on community contributions.
affects: All versions
gotchaOpen issues exist regarding the parser not discarding 'a/' and 'b/' prefixes in paths for certain Git patches (issue #43) and incorrect parsing of colored diffs (issue #28). These might affect specific Git patch workflows or diff outputs.
fix
Check relevant GitHub issues for workarounds or community-contributed solutions if encountering these specific parsing challenges.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'whatthepatch'
The 'whatthepatch' library is not installed in the current Python environment.
fix
Install 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.
fix
Decode 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.
fix
Convert 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.
fix
Access 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.
Agent activity
13 hits · last 30 days
node
10
Amazon
1
OpenAI (training)
1
Resources