From 946093be61e668dd371978a1715519a0db2137ee Mon Sep 17 00:00:00 2001
From: Dain Nilsson <dain@yubico.com>
Date: Mon, 14 Apr 2025 10:41:46 +0200
Subject: [PATCH] Avoid using PCSCContext if it is unavailable

(cherry picked from commit b951fcd0c6e916a5ccf751307b8dcdf71149b38a)

Upstream-Staus: Backport [https://github.com/Yubico/yubikey-manager/commit/b951fcd0c6e916a5ccf751307b8dcdf71149b38a]
---
 ykman/pcsc/__init__.py | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/ykman/pcsc/__init__.py b/ykman/pcsc/__init__.py
index 39aaf3aa..bb11a66c 100644
--- ./ykman/pcsc/__init__.py
+++ ./ykman/pcsc/__init__.py
@@ -33,7 +33,6 @@ from ..base import YUBIKEY, YkmanDevice
 from smartcard import System
 from smartcard.Exceptions import CardConnectionException
 from smartcard.pcsc.PCSCExceptions import ListReadersException
-from smartcard.pcsc.PCSCContext import PCSCContext
 
 from fido2.pcsc import CtapPcscDevice
 from time import sleep
@@ -150,12 +149,18 @@ def kill_scdaemon():
 def list_readers():
     try:
         return System.readers()
-    except ListReadersException:
+    except ListReadersException as e:
         # If the PCSC system has restarted the context might be stale, try
         # forcing a new context (This happens on Windows if the last reader is
         # removed):
-        PCSCContext.instance = None
-        return System.readers()
+        try:
+            from smartcard.pcsc.PCSCContext import PCSCContext
+
+            PCSCContext.instance = None
+            return System.readers()
+        except ImportError:
+            # As of pyscard 2.2.2 the PCSCContext singleton has been removed
+            raise e
 
 
 def list_devices(name_filter=None):
-- 
2.49.0

