Skip to content

Commit fd6094d

Browse files
committed
fix: raise hashlib-compatible error for None input
Add Py_None check in _get_buffer_or_str to raise: TypeError: object supporting the buffer API required matching hashlib.md5(None) exactly. Tests updated to verify.
1 parent 0c81b98 commit fd6094d

3 files changed

Lines changed: 14 additions & 5 deletions

File tree

src/_xxhash.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
static Py_ALWAYS_INLINE int
5555
_get_buffer_or_str(PyObject *obj, Py_buffer *buf)
5656
{
57+
if (obj == Py_None) {
58+
PyErr_SetString(PyExc_TypeError,
59+
"object supporting the buffer API required");
60+
return -1;
61+
}
5762
if (PyUnicode_Check(obj)) {
5863
PyErr_SetString(PyExc_TypeError,
5964
"Strings must be encoded before hashing");

tests/test_hashlib_compat.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ def test_str_rejected(self):
3232
'Strings must be encoded before hashing'):
3333
fn(data='hello')
3434
# None
35-
with self.assertRaises(TypeError):
35+
with self.assertRaisesRegex(TypeError,
36+
'object supporting the buffer API required'):
3637
fn(None)
3738

3839
def test_str_rejected_constructor(self):
@@ -47,9 +48,11 @@ def test_str_rejected_constructor(self):
4748
'Strings must be encoded before hashing'):
4849
cls(data='hello')
4950
# None
50-
with self.assertRaises(TypeError):
51+
with self.assertRaisesRegex(TypeError,
52+
'object supporting the buffer API required'):
5153
cls(None)
52-
with self.assertRaises(TypeError):
54+
with self.assertRaisesRegex(TypeError,
55+
'object supporting the buffer API required'):
5356
cls(data=None)
5457

5558
def test_str_rejected_update(self):
@@ -62,7 +65,8 @@ def test_str_rejected_update(self):
6265
obj.update(b'hello')
6366
self.assertIsInstance(obj.intdigest(), int)
6467
# None
65-
with self.assertRaises(TypeError):
68+
with self.assertRaisesRegex(TypeError,
69+
'object supporting the buffer API required'):
6670
obj.update(None)
6771

6872
# ── data keyword ───────────────────────────────────────────────

xxhash/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
VERSION = "3.8.0.dev4"
1+
VERSION = "3.8.0.dev6"
22
#: Deprecated, will be removed in the next major release
33
VERSION_TUPLE = (3, 8, 0)

0 commit comments

Comments
 (0)