Skip to content

Commit 82cd0c9

Browse files
committed
Custom parser for adler32
1 parent 9442353 commit 82cd0c9

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

src/isal/isal_zlib.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -156,32 +156,31 @@ isal_zlib_adler32(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
156156
Py_buffer data = {NULL, NULL};
157157
uint32_t value = 1;
158158

159-
if (!_PyArg_CheckPositional("adler32", nargs, 1, 2)) {
160-
goto exit;
159+
if (nargs < 1 || nargs > 2) {
160+
PyErr_Format(
161+
PyExc_TypeError,
162+
"adler32 takes exactly 1 or 2 arguments, got %d",
163+
nargs);
164+
return NULL;
161165
}
162166
if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
163-
goto exit;
167+
return NULL;
164168
}
165169
if (!PyBuffer_IsContiguous(&data, 'C')) {
166-
_PyArg_BadArgument("adler32", "argument 1", "contiguous buffer", args[0]);
167-
goto exit;
168-
}
169-
if (nargs < 2) {
170-
goto skip_optional;
170+
PyErr_SetString(PyExc_ValueError, "data is not a contiguous buffer");
171+
PyBuffer_Release(&data);
172+
return NULL;
171173
}
172-
value = (uint32_t)PyLong_AsUnsignedLongMask(args[1]);
173-
if (value == (uint32_t)-1 && PyErr_Occurred()) {
174-
goto exit;
174+
if (nargs > 1) {
175+
value = (uint32_t)PyLong_AsUnsignedLongMask(args[1]);
176+
if (value == (uint32_t)-1 && PyErr_Occurred()) {
177+
PyBuffer_Release(&data);
178+
return NULL;
179+
}
175180
}
176-
skip_optional:
177181
value = isal_adler32(value, data.buf, (uint64_t)data.len);
178182
return_value = PyLong_FromUnsignedLong(value & 0xffffffffU);
179-
180-
exit:
181-
/* Cleanup for data */
182-
if (data.obj) {
183-
PyBuffer_Release(&data);
184-
}
183+
PyBuffer_Release(&data);
185184
return return_value;
186185
}
187186

0 commit comments

Comments
 (0)