JeffLIrion / adb_shell

A Python implementation of ADB with shell and FileSync functionality.
Apache License 2.0
530 stars 60 forks source link

device.shell() command raising utf-8 decode issues #189

Closed botherder closed 2 years ago

botherder commented 3 years ago

Description

I am using adb_shell in a project of mine in order to automate file downloads as well as extracting diagnostic information from the device. I encountered an issue with retrieving output from logcat, which raises the following exception.

Log

Traceback (most recent call last):
  File "/home/user/projects/mvt/mvt/mvt/common/module.py", line 143, in run_module
    module.run()
  File "/home/user/projects/mvt/mvt/mvt/android/modules/adb/logcat.py", line 27, in run
    output = self._adb_command("logcat -d")
  File "/home/user/projects/mvt/mvt/mvt/android/modules/adb/base.py", line 109, in _adb_command
    return self.device.shell(command)
  File "/home/user/.local/lib/python3.9/site-packages/adb_shell/adb_device.py", line 792, in shell
    return self._service(b'shell', command.encode('utf8'), transport_timeout_s, read_timeout_s, timeout_s, decode)
  File "/home/user/.local/lib/python3.9/site-packages/adb_shell/adb_device.py", line 681, in _service
    return b''.join(self._streaming_command(service, command, transport_timeout_s, read_timeout_s, timeout_s)).decode('utf8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe0 in position 1666311: invalid continuation byte  
JeffLIrion commented 3 years ago

I need a debug log to see what's going on, since it will include the data transmitted to/from the device. The traceback doesn't contain much useful info.

If you call the shell method with decode=False, it will avoid this issue by returning bytes instead of the decoded string.