Skip to content

Commit

Permalink
Add gil_release_mode to CRC32CHash.__init__
Browse files Browse the repository at this point in the history
This makes the API more symmetric with crc32c, offering its full set of
possibilities to users.

Signed-off-by: Rodrigo Tobar <rtobar@icrar.org>
  • Loading branch information
rtobar committed Aug 11, 2024
1 parent 9f46efa commit 60256ef
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Development

* Added a `gil_relese_mode` parameter to the `CRC32CHash` constructor
used by all underlying `crc32c` function calls (#51).

## [2.6]

* Added new `crc32c.CRC32CHash` class
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ It can be set to the following values:
* Positive: Always release the GIL

On top of the ``crc32c`` function,
a ``CRC32CHash`` class is also offered.
a ``CRC32CHash(data=b"", gil_release_mode=-1)`` class is also offered.
It is modelled after the "hash objects" of the ``hashlib`` module
of the standard library:

Expand Down
11 changes: 8 additions & 3 deletions src/crc32c/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,23 @@ def name(self) -> str:
"""
return "crc32c"

def __init__(self, data: Buffer = b"") -> None:
def __init__(self, data: Buffer = b"", gil_release_mode: int = -1) -> None:
"""
Initialise the hash object with an optional bytes-like object.
Uses the given GIL release mode on each checksum calculation.
"""
self._checksum = crc32c(data)
self._checksum = crc32c(data, gil_release_mode=gil_release_mode)
self._gil_release_mode = gil_release_mode

def update(self, data: Buffer) -> None:
"""
Update the hash object with the bytes-like object.
Repeated calls are equivalent to a single call with the concatenation of all the arguments:
m.update(a); m.update(b) is equivalent to m.update(a+b).
"""
self._checksum = crc32c(data, self._checksum)
self._checksum = crc32c(
data, self._checksum, gil_release_mode=self._gil_release_mode
)

def digest(self) -> bytes:
"""
Expand All @@ -72,4 +76,5 @@ def copy(self) -> Self:
"""
res = type(self)()
res._checksum = self._checksum
res._gil_release_mode = self._gil_release_mode
return res

0 comments on commit 60256ef

Please sign in to comment.