]> git.ipfire.org Git - pakfire.git/commitdiff
_pakfire: Fetch digest type by string
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 1 Oct 2022 13:19:54 +0000 (13:19 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 1 Oct 2022 13:19:54 +0000 (13:19 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/_pakfire/file.c

index fff20d8eed244344469a7ecc093b1a7910f851ab..e8384b279287415c4aa604a6cc9e927cbe39da89 100644 (file)
@@ -228,7 +228,21 @@ static int File_set_mtime(FileObject* self, PyObject* value) {
        return 0;
 }
 
-static PyObject* _File_digest(FileObject* self, enum pakfire_digest_types type) {
+static PyObject* File_digest(FileObject* self, PyObject* args) {
+       const char* name = NULL;
+
+       if (!PyArg_ParseTuple(args, "s", &name))
+               return NULL;
+
+       // Fetch the type
+       const enum pakfire_digest_types type = pakfire_digest_get_by_name(name);
+
+       // Raise ValueError if we could not find the type
+       if (!type) {
+               PyErr_Format(PyExc_ValueError, "Unknown digest type: %s", name);
+               return NULL;
+       }
+
        size_t length = 0;
 
        // Fetch the digest
@@ -239,15 +253,6 @@ static PyObject* _File_digest(FileObject* self, enum pakfire_digest_types type)
        return PyBytes_FromStringAndSize((const char*)digest, length);
 }
 
-static PyObject* File_digest(FileObject* self, PyObject* args) {
-       int type = 0;
-
-       if (!PyArg_ParseTuple(args, "i", &type))
-               return NULL;
-
-       return _File_digest(self, type);
-}
-
 static PyObject* _File_hexdigest(FileObject* self, enum pakfire_digest_types type) {
        char* hexdigest = pakfire_file_get_hexdigest(self->file, type);
        if (!hexdigest)