kmpm / nodemcu-uploader

Upload files to your esp8266 running nodeMcu
MIT License
319 stars 89 forks source link

"upload --verify" fails when the file contains non-ascii characters #80

Open MarkMartinec opened 6 years ago

MarkMartinec commented 6 years ago

Using the latest nodemcu-uploader 0.4.3 (serial 3.2.1) from github, Python 2.7.14.

Trying to upload a Lua file containing UTF-8 characters, the nodemcu-uploader fails when option --verify is used. Without the --verify the upload succeeds (luckily!).

Example file to be uploaded:

function print_pm(pm2_5, pm10)
  local fmt = string.format
  print(fmt("PM2.5: %4.1f μg/m³, PM10: %4.1f μg/m³", pm2_5, pm10))
end
$ nodemcu-uploader.py --port /dev/cuaU1 upload -v sha1 test.lua
opening port /dev/cuaU1 with 115200 baud
Preparing esp for transfer.
Transferring test.lua as test.lua
Verifying using sha1...
Remote SHA1: 6c872418ff59355d196a1762f52ab53b3079318a
Traceback (most recent call last):
  File "nodemcu_uploader/nodemcu-uploader.py", line 11, in <module>
    main.main_func()
  File "nodemcu_uploader/main.py", line 267, in main_func
    args.restart)
  File "nodemcu_uploader/main.py", line 56, in operation_upload
    uploader.write_file(filename, dst, verify)
  File "nodemcu_uploader/uploader.py", line 294, in write_file
    self.verify_file(path, destination, verify)
  File "nodemcu_uploader/uploader.py", line 316, in verify_file
    filehashhex = hashlib.sha1(content.encode(ENCODING)).hexdigest()
UnicodeDecodeError: 'ascii' codec can't decode byte 0xce in position 67: ordinal not in range(128)

The python code should be treating the contents of the file to be uploaded as bytes, not trying to attribute a character set to it - and if any at all, it should not be limited to ASCII, but at least to UTF-8. After all, Lua is 8-bit clean as far as strings are concerned, so an uploader should not be posing unnecessary restrictions.

kmpm commented 3 years ago

Some of this will be solved with the drop of support for python 2.7 in the next version