Registry / serialization / cdifflib

cdifflib

JSON →
library1.2.9pypypi✓ verified 87d ago

cdifflib is a Python library that provides a C implementation of parts of Python's standard `difflib` module, specifically focusing on `SequenceMatcher`. It creates a `CSequenceMatcher` type which inherits most functions from `difflib.SequenceMatcher`, offering up to 4x speed improvement when diffing large streams. The current version is 1.2.9, with irregular but ongoing maintenance releases to support newer Python versions and address issues.

pip install cdifflib
INSTALL
IMPORT
SIG · CDIFFLIB
C
cdifflib
serializationpythonv1.2.9
Install
Import
Disk
Pass rate
0/ 10
Env Coverage0 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v? · pip install
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.920 runs
build_error
glibc
py 3.103.920 runs
build_error
Code
Verified usage

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

CSequenceMatcher
from cdifflib import CSequenceMatcher
import cdifflib
The primary class CSequenceMatcher is directly imported from the cdifflib package, not as an attribute of the top-level package itself.
SequenceMatcher_monkey_patch
import difflib from cdifflib import CSequenceMatcher difflib.SequenceMatcher = CSequenceMatcher
To transparently replace the standard library's SequenceMatcher with the faster C version for other libraries.

This quickstart demonstrates how to instantiate `CSequenceMatcher` and use its `find_longest_match` and `ratio` methods, similar to `difflib.SequenceMatcher`.

from cdifflib import CSequenceMatcher # Example 1: Basic sequence matching s = CSequenceMatcher(None, ' abcd', 'abcd abcd') match = s.find_longest_match(0, 5, 0, 9) print(f"Longest match: {match}") # Example 2: With custom junk filter s2 = CSequenceMatcher(lambda x: x == " ", "private Thread currentThread;", "private volatile Thread currentThread;") ratio = round(s2.ratio(), 3) print(f"Similarity ratio: {ratio}")
Debug
Known issues
gotchaThe C implementation of `CSequenceMatcher` internally converts input sequences (`a` and `b`) to `list` type if they are not already lists. While convenient, this implicit conversion can incur performance or memory overhead for very large iterables if not anticipated.
fix
For optimal performance with large datasets, ensure that `a` and `b` are already `list` instances before passing them to the `CSequenceMatcher` constructor.
affects: All versions
gotchaElements within the input sequences (`a` and `b`) must be hashable. The underlying C implementation performs hashing on sequence items. Passing sequences containing unhashable types (e.g., mutable lists or dictionaries) will result in a `TypeError: unhashable type: ...`.
fix
Verify that all items in your input sequences are hashable. Convert unhashable elements (like lists or dictionaries) to hashable equivalents (e.g., tuples or strings) if necessary before creating the `CSequenceMatcher`.
affects: All versions
breakingPrior to version 1.2.0, `cdifflib` had installation issues on Python 3, particularly when installing from source or without pre-compiled wheels. This often led to `AttributeError: module 'cdifflib' has no attribute 'CSequenceMatcher'` as the C extension failed to build correctly.
fix
Upgrade to `cdifflib` version 1.2.0 or newer. If installing from source, ensure your environment has a compatible C compiler installed.
affects: <1.2.0
Errors
Common errors & fixes
AttributeError: module 'cdifflib' has no attribute 'CSequenceMatcher'
This typically occurs in older `cdifflib` versions (pre-1.2.0) on Python 3 environments where the C extension was not properly built and linked during installation, preventing `CSequenceMatcher` from being exposed.
fix
Ensure you are using `cdifflib>=1.2.0`. If the problem persists, ensure your system has a C compiler (e.g., `build-essential` on Linux, Xcode on macOS, C++ Build Tools on Windows) if a pre-compiled wheel is not available for your platform. Then, try `pip install --upgrade cdifflib`.
TypeError: unhashable type: 'list'
The C implementation of `CSequenceMatcher` requires individual elements within the `a` and `b` sequences to be hashable. When a sequence contains mutable types like lists or dictionaries, this error is raised.
fix
Modify your input sequences so that all their elements are hashable. For example, convert lists to tuples: `sm = CSequenceMatcher(None, [('a',), ('b',)], [('a',), ('c',)])`.
error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
On Windows, if a pre-compiled wheel for your specific Python version and architecture is not available, `pip` attempts to compile `cdifflib` from source, which requires a compatible C/C++ compiler.
fix
Install the "Build Tools for Visual Studio" from the provided Microsoft link, ensuring you select the 'Desktop development with C++' workload. Alternatively, try to use a Python version for which `cdifflib` provides pre-compiled wheels.
Upgrade
Version history
1.2.9latest on PyPI · released Jan 13, 2025
Audit
Dependencies

No dependency data recorded yet.

Agent activity
15 hits · last 30 days
node
14
OpenAI (training)
1
Resources
cdifflib — pip install cdifflib · libregistry