From af4e5bb18cdf498af481e07c6aff2d65ef60630e Mon Sep 17 00:00:00 2001 From: Philipp Maier Date: Tue, 10 Oct 2023 12:29:14 +0200 Subject: [PATCH] transport: do not catch exceptions in init_reader We currently catch any exceptions that may occur when the card reader is initialized. Then we print the exception string or the exception type when no string is available. However, a failure during the reader initialization is usually a severe problem, so a traceback would provde a lot of helpful information to debug the issue. So lets not catch any exceptions at this level so that we get the full backtrace. Related: OS#6210 Change-Id: I4c4807576fe63cf71a7d33b243a3f8fea0b7ff23 --- pySim-prog.py | 2 -- pySim-read.py | 2 -- pySim-shell.py | 2 -- pySim/transport/__init__.py | 49 +++++++++++++++---------------------- 4 files changed, 20 insertions(+), 35 deletions(-) diff --git a/pySim-prog.py b/pySim-prog.py index a16928c0..2911b464 100755 --- a/pySim-prog.py +++ b/pySim-prog.py @@ -794,8 +794,6 @@ if __name__ == '__main__': # Init card reader driver sl = init_reader(opts) - if sl is None: - exit(1) # Create command layer scc = SimCardCommands(transport=sl) diff --git a/pySim-read.py b/pySim-read.py index bafaf262..d34ddc35 100755 --- a/pySim-read.py +++ b/pySim-read.py @@ -74,8 +74,6 @@ if __name__ == '__main__': # Init card reader driver sl = init_reader(opts) - if sl is None: - exit(1) # Create command layer scc = SimCardCommands(transport=sl) diff --git a/pySim-shell.py b/pySim-shell.py index 56655ba0..afc76539 100755 --- a/pySim-shell.py +++ b/pySim-shell.py @@ -1015,8 +1015,6 @@ if __name__ == '__main__': # Init card reader driver sl = init_reader(opts, proactive_handler = Proact()) - if sl is None: - exit(1) # Create a card handler (for bulk provisioning) if opts.card_handler_config: diff --git a/pySim/transport/__init__.py b/pySim/transport/__init__.py index 0b50a4f8..581f8e59 100644 --- a/pySim/transport/__init__.py +++ b/pySim/transport/__init__.py @@ -292,35 +292,26 @@ def argparse_add_reader_args(arg_parser): return arg_parser -def init_reader(opts, **kwargs) -> Optional[LinkBase]: +def init_reader(opts, **kwargs) -> LinkBase: """ Init card reader driver """ - sl = None # type : :Optional[LinkBase] - try: - if opts.pcsc_dev is not None: - print("Using PC/SC reader interface") - from pySim.transport.pcsc import PcscSimLink - sl = PcscSimLink(opts.pcsc_dev, **kwargs) - elif opts.osmocon_sock is not None: - print("Using Calypso-based (OsmocomBB) reader interface") - from pySim.transport.calypso import CalypsoSimLink - sl = CalypsoSimLink(sock_path=opts.osmocon_sock, **kwargs) - elif opts.modem_dev is not None: - print("Using modem for Generic SIM Access (3GPP TS 27.007)") - from pySim.transport.modem_atcmd import ModemATCommandLink - sl = ModemATCommandLink( - device=opts.modem_dev, baudrate=opts.modem_baud, **kwargs) - else: # Serial reader is default - print("Using serial reader interface") - from pySim.transport.serial import SerialSimLink - sl = SerialSimLink(device=opts.device, - baudrate=opts.baudrate, **kwargs) - return sl - except Exception as e: - if str(e): - print("Card reader initialization failed with exception:\n" + str(e)) - else: - print( - "Card reader initialization failed with an exception of type:\n" + str(type(e))) - return None + if opts.pcsc_dev is not None: + print("Using PC/SC reader interface") + from pySim.transport.pcsc import PcscSimLink + sl = PcscSimLink(opts.pcsc_dev, **kwargs) + elif opts.osmocon_sock is not None: + print("Using Calypso-based (OsmocomBB) reader interface") + from pySim.transport.calypso import CalypsoSimLink + sl = CalypsoSimLink(sock_path=opts.osmocon_sock, **kwargs) + elif opts.modem_dev is not None: + print("Using modem for Generic SIM Access (3GPP TS 27.007)") + from pySim.transport.modem_atcmd import ModemATCommandLink + sl = ModemATCommandLink( + device=opts.modem_dev, baudrate=opts.modem_baud, **kwargs) + else: # Serial reader is default + print("Using serial reader interface") + from pySim.transport.serial import SerialSimLink + sl = SerialSimLink(device=opts.device, + baudrate=opts.baudrate, **kwargs) + return sl