Skip to content

Commit cfcbce7

Browse files
committed
Simplify ISAL_VERSION creation
1 parent d0e2bd2 commit cfcbce7

File tree

1 file changed

+3
-14
lines changed

1 file changed

+3
-14
lines changed

src/isal/_isalmodule.c

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#define PY_SSIZE_T_CLEAN
1212
#include <Python.h>
1313
#include <isa-l.h>
14-
#include <stdio.h>
1514

1615
static struct PyModuleDef _isal_module = {
1716
PyModuleDef_HEAD_INIT,
@@ -34,19 +33,9 @@ PyInit__isal(void)
3433
PyModule_AddIntMacro(m, ISAL_MAJOR_VERSION);
3534
PyModule_AddIntMacro(m, ISAL_MINOR_VERSION);
3635
PyModule_AddIntMacro(m, ISAL_PATCH_VERSION);
37-
// UINT64_MAX is 20 characters long. A version contains:
38-
// 3 numbers, 2 dots, one null byte. So the maximum size
39-
// in charcters is 3x20+2+1=63. Round up to the nearest
40-
// power of 2 is 64.
41-
char version[64];
42-
int length = snprintf(version, 64, "%d.%d.%d",
43-
ISAL_MAJOR_VERSION, ISAL_MINOR_VERSION, ISAL_PATCH_VERSION);
44-
if (length > 64){
45-
// This is extremely unlikely to happen given the calculation above.
46-
PyErr_SetString(PyExc_MemoryError, "Could not allocate enough memory for ISA-L version string");
47-
return NULL;
48-
}
49-
PyObject *isal_version = PyUnicode_DecodeASCII(version, length, "strict");
36+
37+
PyObject *isal_version = PyUnicode_FromFormat(
38+
"%d.%d.%d", ISAL_MAJOR_VERSION, ISAL_MINOR_VERSION, ISAL_PATCH_VERSION);
5039
if (isal_version == NULL)
5140
return NULL;
5241
PyModule_AddObject(m, "ISAL_VERSION", isal_version);

0 commit comments

Comments
 (0)