greatscottgadgets / libgreat

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

AttributeError: 'array.array' object has no attribute 'tobytes' #1

Closed szczys closed 5 years ago

szczys commented 5 years ago

Executing greatfet info results in error:

  File "/usr/local/bin/greatfet_info", line 10, in <module>
    sys.exit(main())
  File "/usr/local/lib/python2.7/dist-packages/greatfet/commands/greatfet_info.py", line 94, in main
    devices = GreatFET(find_all=True)
  File "/usr/local/lib/python2.7/dist-packages/greatfet/greatfet.py", line 29, in GreatFET
    return GreatFETBoard.autodetect_all(**board_identifiers)
  File "/usr/local/lib/python2.7/dist-packages/pygreat/board.py", line 105, in autodetect_all
    subclass_devices = subclass.all_accepted_devices(**device_identifiers)
  File "/usr/local/lib/python2.7/dist-packages/pygreat/board.py", line 151, in all_accepted_devices
    if cls.accepts_connected_device(**identifiers):
  File "/usr/local/lib/python2.7/dist-packages/pygreat/board.py", line 168, in accepts_connected_device
    potential_device = cls(**device_identifiers)
  File "/usr/local/lib/python2.7/dist-packages/greatfet/boards/rad1o.py", line 24, in __init__
    super(Rad1oBadge, self).__init__(**device_identifiers)
  File "/usr/local/lib/python2.7/dist-packages/pygreat/board.py", line 245, in __init__
    if self.HANDLED_BOARD_IDS and (self.board_id() not in self.HANDLED_BOARD_IDS):
  File "/usr/local/lib/python2.7/dist-packages/pygreat/board.py", line 274, in board_id
    return self.apis.core.read_board_id()
  File "/usr/local/lib/python2.7/dist-packages/pygreat/comms.py", line 1088, in method
    timeout=timeout, max_response_length=max_response_length, encoding=encoding, *arguments)
  File "/usr/local/lib/python2.7/dist-packages/pygreat/comms.py", line 1243, in execute_command
    out_format, *arguments, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pygreat/comms.py", line 761, in execute_command
    None, max_response_length, comms_timeout, pretty_name, rephrase_errors)
  File "/usr/local/lib/python2.7/dist-packages/pygreat/comms_backends/usb.py", line 288, in execute_raw_command
    return response.tobytes()
AttributeError: 'array.array' object has no attribute 'tobytes'

System info:

mossmann commented 5 years ago

There is probably a Python 2 solution to this, but I'm pretty sure the quick fix is to use Python 3. We hope to support Python 2 for a while, but the Python developers plan to stop supporting it at the end of 2019.

mossmann commented 5 years ago

I am able to reproduce the error by switching to Python 2.

mossmann commented 5 years ago

It looks like one solution would be to replace tobytes() with tostring(). The latter is a deprecated alias in Python 3 for the Python 2 function name.

szczys commented 5 years ago

I had also tried installing on python 3 (sudo pip3 install --upgrade greatfet) but was met with the following error:

Collecting greatfet
  Downloading https://files.pythonhosted.org/packages/cc/93/b1713cc4bb43b86a2d8c27425e4a525dc3e1191f561b8d18b6427ae3861e/GreatFET-2019.5.1.dev0-py3-none-any.whl (125kB)
    100% |████████████████████████████████| 133kB 1.7MB/s 
Collecting future (from greatfet)
  Downloading https://files.pythonhosted.org/packages/90/52/e20466b85000a181e1e144fd8305caf2cf475e2f9674e797b222f8105f5f/future-0.17.1.tar.gz (829kB)
    100% |████████████████████████████████| 829kB 1.5MB/s 
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
    ModuleNotFoundError: No module named 'setuptools'

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-b4bd42_q/future/

Looks like I didn't have python3 setuptool: sudo apt-get install python3-setuptools

Now things are working. Thanks!

mossmann commented 5 years ago

Oh, nice! I'm reopening because we either need to fix this bug or drop Python 2 support.

mossmann commented 5 years ago

Using the bitstring module would be another solution. I'm not sure it's really warranted in this case, but bitstring.Bits(array.array('B', [0, 0])).bytes does the same thing in both 2 and 3.

ktemkin commented 5 years ago

This should be fixed as of dfcd6a4.