PIO Remote Agent - a bytes-like object is required, not 'str'

Hello,

I’m looking to use pio for CI/CD process and have been doing some investigation into using pio installed within a Docker imager, running on a Ubuntu server that has physical devices Arduino etc connected via USB.

I have a docker image that runs ok, and I can pipe the USB device into the docker image ok, but when I try to run commands remotely I keep running into the following error on the remote client:

a bytes-like object is required, not 'str'

Which gives the following stack dump on the remote agent:

PIO Plus (https://pioplus.com) v2.5.2
2019-07-23 08:55:14 [info] Name: platformio-test01
2019-07-23 08:55:14 [info] Connecting to PIO Remote Cloud
2019-07-23 08:55:14 [info] Successfully connected
2019-07-23 08:55:14 [info] Authenticating
2019-07-23 08:55:15 [info] Successfully authorized
2019-07-23 08:55:19 [info] Remote command received: b'device.list'
Peer will receive following PB traceback:
Unhandled Error
Traceback (most recent call last):
  File "/home/isa56k/.platformio/packages/contrib-pysite/twisted/spread/banana.py", line 173, in gotItem
    self.callExpressionReceived(item)
  File "/home/isa56k/.platformio/packages/contrib-pysite/twisted/spread/banana.py", line 136, in callExpressionReceived
    self.expressionReceived(obj)
  File "/home/isa56k/.platformio/packages/contrib-pysite/twisted/spread/pb.py", line 621, in expressionReceived
    method(*sexp[1:])
  File "/home/isa56k/.platformio/packages/contrib-pysite/twisted/spread/pb.py", line 1025, in proto_message
    self._recvMessage(self.localObjectForID, requestID, objectID, message, answerRequired, netArgs, netKw)
--- <exception caught here> ---
  File "/home/isa56k/.platformio/packages/contrib-pysite/twisted/spread/pb.py", line 1054, in _recvMessage
    netResult = object.remoteMessageReceived(self, message, netArgs, netKw)
  File "/home/isa56k/.platformio/packages/contrib-pysite/twisted/spread/flavors.py", line 125, in remoteMessageReceived
    state = method(*args, **kw)
  File "<string>", line 52, in remote_cmd
    
builtins.TypeError: a bytes-like object is required, not 'str'

I found an article here that references “Ran into issues with the dreader bytes/string problems”. Which I am assuming is a bit of a common problem?

I also noted a github issue here where it looks like this is fixed:

I have run pio update on both ends agent/client but still seem to get this error when issuing any remote commands. e.g. pio remote device list / pio remote test -e uno etc…

I have also tested it outside of Docker and can reproduce the same behaviour.

I am using Ubuntu Server 19.04 / python3 / pio v4.0.0

Any ideas? Has anyone hit similar issues?

Do you use Python 3 on the both sides? I can’t reproduce this issue. Which PIO Remote command do you use?

Hi!

Yep, python 3.6 on both sides…

See screen shot of details… is there anything else I can provide?

So I don’t think I was using v3.6 of python that I thought I was!

I switched to v3.6 on my local machine, then reinstalled platformIO using pip3.

Once I had done this, I was able to connect ok.

@ivankravets - Thanks for the pointer! :slight_smile:

1 Like

This is actually a bug when Python2 and Python3 were used together for the session. Could you file a new issue Issues · platformio/platformio-core · GitHub? We will take a look later. Thanks!

Python makes a clear distinction between bytes and strings . Bytes objects contain raw data — a sequence of octets — whereas strings are Unicode sequences . Conversion between these two types is explicit: you encode a string to get bytes, specifying an encoding (which defaults to UTF-8); and you decode bytes to get a string. Clients of these functions should be aware that such conversions may fail, and should consider how failures are handled.

We can convert bytes to string using bytes class decode() instance method, So you need to decode the bytes object to produce a string. In Python 3 , the default encoding is “utf-8” , so you can use directly:

b"python byte to string".decode("utf-8")