From c992ac85df612d859d7a176b9061ab5b81567f13 Mon Sep 17 00:00:00 2001 From: Jakub Zelenka Date: Tue, 19 Nov 2024 17:22:56 +0100 Subject: [PATCH] Introduce lib finalize method This is to allow explicit calling of C_Finalize --- pkcs11/_pkcs11.pyx | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/pkcs11/_pkcs11.pyx b/pkcs11/_pkcs11.pyx index 343293f..903358e 100644 --- a/pkcs11/_pkcs11.pyx +++ b/pkcs11/_pkcs11.pyx @@ -1435,6 +1435,8 @@ cdef class lib: pkcs11.types. """ + cdef bint _finalized + cdef public str so cdef public str manufacturer_id cdef public str library_description @@ -1509,6 +1511,7 @@ cdef class lib: assertRV(_funclist.C_Initialize(NULL)) def __init__(self, so): + self._finalized = False self.so = so cdef CK_INFO info @@ -1648,14 +1651,22 @@ cdef class lib: return Slot(self, slot_id, slotDescription, manufacturerID, info.hardwareVersion, info.firmwareVersion, info.flags) + def finalize(self): + if _funclist != NULL and not self._finalized: + with nogil: + assertRV(_funclist.C_Finalize(NULL)) + self._finalized = True + def reinitialize(self): if _funclist != NULL: with nogil: - assertRV(_funclist.C_Finalize(NULL)) + if not self._finalized: + assertRV(_funclist.C_Finalize(NULL)) assertRV(_funclist.C_Initialize(NULL)) + self._finalized = False def __dealloc__(self): - if _funclist != NULL: + if _funclist != NULL and not self._finalized: with nogil: assertRV(_funclist.C_Finalize(NULL))