From 60fbc2beaef10245d34e672fbf37c235d5b180d6 Mon Sep 17 00:00:00 2001 From: Mark Bokil Date: Sun, 26 Mar 2023 21:42:16 -0400 Subject: [PATCH 1/5] added CardServiceNotFoundException exception --- smartcard/Exceptions.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/smartcard/Exceptions.py b/smartcard/Exceptions.py index 3385c1be..3ca3d6f6 100644 --- a/smartcard/Exceptions.py +++ b/smartcard/Exceptions.py @@ -78,6 +78,10 @@ class CardServiceStoppedException(SmartcardException): """Raised when the CardService was stopped""" pass +class CardServiceNotFoundException(SmartcardException): + """Raised when the CardService is not found""" + pass + class InvalidATRMaskLengthException(SmartcardException): """Raised when an ATR mask does not match an ATR length.""" From 47230d214f7cb4f1e0e97bbbfa1621f4a73812a2 Mon Sep 17 00:00:00 2001 From: Mark Bokil Date: Sun, 26 Mar 2023 21:44:13 -0400 Subject: [PATCH 2/5] added CardServiceNotFoundException exception --- smartcard/pcsc/PCSCReader.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/smartcard/pcsc/PCSCReader.py b/smartcard/pcsc/PCSCReader.py index d33efd8f..118c4d1e 100644 --- a/smartcard/pcsc/PCSCReader.py +++ b/smartcard/pcsc/PCSCReader.py @@ -46,6 +46,8 @@ def __PCSCreaders__(hcontext, groups=[]): readers = [] elif hresult == SCARD_E_SERVICE_STOPPED: raise CardServiceStoppedException() + elif hresult == SCARD_E_NO_SERVICE: + raise CardServiceNotFoundException() else: raise ListReadersException(hresult) @@ -114,6 +116,10 @@ def readers(groups=[]): hcontext = PCSCContext.renewContext() pcsc_readers = __PCSCreaders__(hcontext, groups) + except CardServiceNotFoundException: + hcontext = PCSCContext.renewContext() + pcsc_readers = __PCSCreaders__(hcontext, groups) + for reader in pcsc_readers: creaders.append(PCSCReader.Factory.create(reader)) return creaders From 43e40b995dcc11014aeb3b285726d23b373ab0bd Mon Sep 17 00:00:00 2001 From: Mark Bokil Date: Sun, 26 Mar 2023 21:46:27 -0400 Subject: [PATCH 3/5] added SCARD_E_NO_SERVICE calls renewContext --- smartcard/pcsc/PCSCCardRequest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/smartcard/pcsc/PCSCCardRequest.py b/smartcard/pcsc/PCSCCardRequest.py index 8f242d01..82d05d11 100644 --- a/smartcard/pcsc/PCSCCardRequest.py +++ b/smartcard/pcsc/PCSCCardRequest.py @@ -89,7 +89,7 @@ def getReaderNames(self): # get inserted readers hresult, pcscreaders = SCardListReaders(self.hcontext, []) - if SCARD_E_SERVICE_STOPPED == hresult: + if SCARD_E_SERVICE_STOPPED == hresult or SCARD_E_NO_SERVICE == hresult: self.hcontext = PCSCContext().renewContext() hresult, pcscreaders = SCardListReaders(self.hcontext, []) if SCARD_E_NO_READERS_AVAILABLE == hresult: From 453f8cd2653c8e4aad3a4d67db7e6dab046b42f5 Mon Sep 17 00:00:00 2001 From: Mark Bokil Date: Wed, 29 Mar 2023 10:06:48 -0400 Subject: [PATCH 4/5] Update PCSCCardRequest.py change from review --- smartcard/pcsc/PCSCCardRequest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/smartcard/pcsc/PCSCCardRequest.py b/smartcard/pcsc/PCSCCardRequest.py index 82d05d11..d8fa2488 100644 --- a/smartcard/pcsc/PCSCCardRequest.py +++ b/smartcard/pcsc/PCSCCardRequest.py @@ -89,7 +89,7 @@ def getReaderNames(self): # get inserted readers hresult, pcscreaders = SCardListReaders(self.hcontext, []) - if SCARD_E_SERVICE_STOPPED == hresult or SCARD_E_NO_SERVICE == hresult: + if hresult in (SCARD_E_SERVICE_STOPPED, SCARD_E_NO_SERVICE): self.hcontext = PCSCContext().renewContext() hresult, pcscreaders = SCardListReaders(self.hcontext, []) if SCARD_E_NO_READERS_AVAILABLE == hresult: From 68bad93dc405ccb4a0566a214fd49f4e3f555877 Mon Sep 17 00:00:00 2001 From: Mark Bokil Date: Wed, 29 Mar 2023 10:07:47 -0400 Subject: [PATCH 5/5] Update PCSCReader.py change from review --- smartcard/pcsc/PCSCReader.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/smartcard/pcsc/PCSCReader.py b/smartcard/pcsc/PCSCReader.py index 118c4d1e..c721e9cb 100644 --- a/smartcard/pcsc/PCSCReader.py +++ b/smartcard/pcsc/PCSCReader.py @@ -112,11 +112,7 @@ def readers(groups=[]): try: pcsc_readers = __PCSCreaders__(hcontext, groups) - except CardServiceStoppedException: - hcontext = PCSCContext.renewContext() - pcsc_readers = __PCSCreaders__(hcontext, groups) - - except CardServiceNotFoundException: + except (CardServiceStoppedException, CardServiceNotFoundException): hcontext = PCSCContext.renewContext() pcsc_readers = __PCSCreaders__(hcontext, groups)