greatscottgadgets / libgreat

a library to help you do Great things
BSD 3-Clause "New" or "Revised" License
31 stars 25 forks source link

Fixes a type bug returned by the regex match method in Python 3.7 #13

Closed FlUxIuS closed 5 years ago

FlUxIuS commented 5 years ago

Hi,

I recently got an exception on Python 3.7 with the following error:

  File "/<path>/site-packages/pygreat/comms.py", line 403, in _get_bytes_consumed_by_format
    return int(match.groups(1))
TypeError: unexpected return RPC `read_setup`; innner message: int() argument must be a string, a bytes-like object or a number, not 'tuple'; format: <8X

So I've patched this line to check if the value returned by match.groups() is a tuple or not, and if so it takes the first element of the tuple.

Thanks in advance!

Cheers!

Qyriad commented 5 years ago

Can I ask what you were doing when you ran into this issue and the exception was thrown? I'm trying to reproduce the issue on Python 3.7.4.

FlUxIuS commented 5 years ago

Hi,

Yes sure and sorry I've completely forgot to include the context.

Actually I was using this script to emulate a mass storage: https://github.com/usb-tools/Facedancer/blob/master/facedancer-umass.py

The regex processing the format string for hexbytes returns a tuple with the length, format type and the index provided in the 'groups()' method of the matched result.

ktemkin commented 5 years ago

Thanks for the PR! Merged.

FlUxIuS commented 5 years ago

No problem! Thanks! :)

giorgio-zamparelli commented 5 years ago

I just faced the same error using running https://github.com/usb-tools/Facedancer/blob/master/facedancer-serial.py and this change fixed the problem.

Thanks for the fix @FlUxIuS and thank you @ktemkin for merging!

I git cloned the latest commit from the usb-tools/Facedancer repo but the old unpatched version of libgreat is still being used.

It seems like Facedancer is calling the libgreat code from /usr/local/lib/python3.7/site-packages/pygreat/comms.py

I'm very new to Python and it's not clear to me how to update the libgreat/pygreat library. I had to edit manually the file /usr/local/lib/python3.7/site-packages/pygreat/comms.py with the change from FlUxIuS

DETAILS TO REPRODUCE for @Qyriad

OS: macOS High Sierra 10.13.6 Python: 3.7.4 hardware: GreatFET One GreatFet Firmware version: v2019.9.1 (just updated the firmware from v2018.12.x, not sure if this has to do with the fact that I have an older version of libreat/pygreat in /usr/local/lib/python3.7/site-packages/pygreat) script: Facedancer/facedancer-serial.py


  File "./facedancer-keyboard.py", line 14, in <module>
    d.run()
  File "/Users/giorgiozamparelli/git/Facedancer/facedancer/USBDevice.py", line 166, in run
    self.scheduler.run()
  File "/Users/giorgiozamparelli/git/Facedancer/facedancer/core.py", line 506, in run
    task()
  File "/Users/giorgiozamparelli/git/Facedancer/facedancer/USBDevice.py", line 84, in <lambda>
    self.scheduler.add_task(lambda : self.maxusb_app.service_irqs())
  File "/Users/giorgiozamparelli/git/Facedancer/facedancer/backends/GreatDancerApp.py", line 755, in service_irqs
    self._handle_setup_events()
  File "/Users/giorgiozamparelli/git/Facedancer/facedancer/backends/GreatDancerApp.py", line 362, in _handle_setup_events
    self._handle_setup_event_on_endpoint(i)
  File "/Users/giorgiozamparelli/git/Facedancer/facedancer/backends/GreatDancerApp.py", line 377, in _handle_setup_event_on_endpoint
    data    = bytearray(self.api.read_setup(endpoint_number))
  File "/usr/local/lib/python3.7/site-packages/pygreat/comms.py", line 1107, in method
    timeout=timeout, max_response_length=max_response_length, encoding=encoding, *arguments)
  File "/usr/local/lib/python3.7/site-packages/pygreat/comms.py", line 1262, in execute_command
    out_format, *arguments, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/pygreat/comms.py", line 787, in execute_command
    future_utils.raise_with_traceback(outer_exception, sys.exc_info()[2])
  File "/usr/local/lib/python3.7/site-packages/future/utils/__init__.py", line 421, in raise_with_traceback
    raise exc.with_traceback(traceback)
  File "/usr/local/lib/python3.7/site-packages/pygreat/comms.py", line 780, in execute_command
    result = self.unpack(out_format, raw_result)
  File "/usr/local/lib/python3.7/site-packages/pygreat/comms.py", line 617, in unpack
    bytes_consumed, raw_bytes = cls._split_off_bytes_for_format(subformat, raw_bytes)
  File "/usr/local/lib/python3.7/site-packages/pygreat/comms.py", line 425, in _split_off_bytes_for_format
    num_bytes_consumed = cls._get_bytes_consumed_by_format(format_string, raw_bytes)
  File "/usr/local/lib/python3.7/site-packages/pygreat/comms.py", line 402, in _get_bytes_consumed_by_format
    return int(match.groups(1))
TypeError: unexpected return RPC `read_setup`; innner message: int() argument must be a string, a bytes-like object or a number, not 'tuple'; format: <8X```
giorgio-zamparelli commented 5 years ago

ktemkin answered me on Discord (https://discord.gg/HKAhHub) suggesting I can update the greatfet library on my Mac with pip --upgrade greatfet :D