nxp-mcuxpresso / pypemicro

Other
4 stars 3 forks source link

Error raised on Apple M1 systems #3

Closed flit closed 3 years ago

flit commented 3 years ago

The error below is raised from pyocd list -vv when running on an Apple M1 MacBook Air.

Pypemicro needs to gracefully handle situations where it's run on a system for which it does not have a valid library. Or at least raise a distinct PEMicroUnsupportedArchitecture exception so that pyocd-pemicro can catch and gracefully handle it.

0000177:DEBUG:pemicro:There is no PEMICRO library.
0000177:CRITICAL:__main__:PEMicro Exception with error code:PEMicro Exception with error code:PEMicro Exception with error code:Unable to find any usable library in the system!
Traceback (most recent call last):
  File "/Users/creed/projects/pyocd/venv/mac39/lib/python3.9/site-packages/pypemicro/pemicro.py", line 411, in get_pemicro_lib
    filename = PyPemicro.get_newest_lib_filename(libs_list)
  File "/Users/creed/projects/pyocd/venv/mac39/lib/python3.9/site-packages/pypemicro/pemicro.py", line 389, in get_newest_lib_filename
    raise PEMicroException("Unable to find any usable library in the system!")
pypemicro.pemicro.PEMicroException: PEMicro Exception with error code:Unable to find any usable library in the system!

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/creed/projects/pyocd/venv/mac39/lib/python3.9/site-packages/pypemicro/pemicro.py", line 427, in list_ports
    lib = PyPemicro.get_pemicro_lib()
  File "/Users/creed/projects/pyocd/venv/mac39/lib/python3.9/site-packages/pypemicro/pemicro.py", line 415, in get_pemicro_lib
    raise PEMicroException(str(exc))
pypemicro.pemicro.PEMicroException: PEMicro Exception with error code:PEMicro Exception with error code:Unable to find any usable library in the system!

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/creed/projects/pyocd-pemicro/pyocd_pemicro/pemicro_probe.py", line 78, in get_all_connected_probes
    port_list = pemicro.list_ports()
  File "/Users/creed/projects/pyocd/venv/mac39/lib/python3.9/site-packages/pypemicro/pemicro.py", line 429, in list_ports
    raise PEMicroException(str(exc))
pypemicro.pemicro.PEMicroException: PEMicro Exception with error code:PEMicro Exception with error code:PEMicro Exception with error code:Unable to find any usable library in the system!

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/creed/projects/pyocd/pyocd/__main__.py", line 402, in run
    self._COMMANDS[self._args.cmd](self)
  File "/Users/creed/projects/pyocd/pyocd/__main__.py", line 462, in do_list
    ConnectHelper.list_connected_probes()
  File "/Users/creed/projects/pyocd/pyocd/core/helpers.py", line 109, in list_connected_probes
    allProbes = ConnectHelper.get_all_connected_probes(blocking=False)
  File "/Users/creed/projects/pyocd/pyocd/core/helpers.py", line 82, in get_all_connected_probes
    allProbes = DebugProbeAggregator.get_all_connected_probes(unique_id=unique_id)
  File "/Users/creed/projects/pyocd/pyocd/probe/aggregator.py", line 64, in get_all_connected_probes
    probes += cls.get_all_connected_probes(unique_id, is_explicit)
  File "/Users/creed/projects/pyocd-pemicro/pyocd_pemicro/pemicro_probe.py", line 83, in get_all_connected_probes
    six.raise_from(cls._convert_exception(exc), exc)
  File "<string>", line 3, in raise_from
pyocd.core.exceptions.ProbeError: PEMicro Exception with error code:PEMicro Exception with error code:PEMicro Exception with error code:Unable to find any usable library in the system!
Gargy007 commented 3 years ago

Hi Chris, good point - I will look at at it and add detection of unsupported systems.

Gargy007 commented 3 years ago

I keep it as exception (but better handled), for the PyOCD purposes this is fixed (by you :-) )in pyocd-pemicro package, that is correct.

In this package the exception MUST be raised.

Thank you Petr

flit commented 3 years ago

I agree the exception should still be raised. Can you please raise a specific exception class for this error? Having pyocd-pemicro look for a particular string in the error message is a very bad way of handling this, but is currently the only option.

Instead of always using the generic PEMicroException class, I recommend creating these exception subclasses:

Also, for some errors within the methods of PyPemicro, regular Python exceptions should be raised. Like in PyPemicro.read_block(), you should raise ValueError if parameters are invalid.

You can probably also use asserts for verifying invariants of methods, such as requiring the library to be loaded and a valid connection, instead of raising exceptions. (This is always a difficult design decision. 😄 )