Re: [Fuego] [PATCH 4/8] Use decode() to convert bytes to string in python3

"Bird, Tim" <[email protected]> Fri, 13 May 2022 19:06:05 +0000
Newsgroups dev.linux.lists.fuego
Message-ID <BYAPR13MB250305A33C2C8BBE5DFC2B6DFDCA9@BYAPR13MB2503.namprd13.prod.outlook.com>
See comments inline below.

> -----Original Message-----
> From: [email protected] <[email protected]=
m>
>=20
> From: Shivanand Kunijadar <[email protected]>
>=20
> The file read operation in python3 converts data to bytes.
> Use decode() to convert bytes to string in python3 which is
> compatible with python2 and python3.
>=20
> Signed-off-by: Shivanand Kunijadar <[email protected]>
> ---
>  scripts/ftc | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>=20
> diff --git a/scripts/ftc b/scripts/ftc
> index ce34b08..3723e7e 100755
> --- a/scripts/ftc
> +++ b/scripts/ftc
> @@ -3960,7 +3960,7 @@ def ftc_exec_command(command, timeout):
>              log.flush()
>              data =3D os.read(tail_fd, 4096)
>              if data:
> -                sys.stdout.write(data)
> +                sys.stdout.write(data.decode("utf8"))

I didn't like this solution, because it invokes a 'decode' operation on dat=
a
that may not actually be in 'utf' format.  If any of the test programs
(executed by the chain of processes under 'ftc_exec_command') produce
non-UTF-compatible output, then this might cause a UnicodeDecode exception.

Really, I just want the data to pass from the test program stdout to ftc st=
dout
unmodified in any way.  The decode might modify the data, or raise an
exception.

Instead of this, I changed the code to use this sequence:
stdout_fd =3D sys.stdout.fileno()
os.write(stdout_fd, data)

os.write() takes bytes, which is what os.read() returns, and this appears t=
o work
for both python3 and python2.

>                  sys.stdout.flush()
>=20
>              time.sleep(.1)
> @@ -4813,7 +4813,7 @@ def do_run_test(conf, options):
>      # drain the log, in case there's more there
>      data =3D os.read(tail_fd, 4096)
>      while data:
> -        sys.stdout.write(data)
> +        sys.stdout.write(data.decode("utf8"))
>          data =3D os.read(tail_fd, 4096)
>=20
>      # record success or failure
> --
> 2.20.1
>=20

The locations you pointed out have been changed with a commit that I made.
It should be compatible with both Python2 and Python3, but please give it
a test.

I gave you a "Reported by" credit in the commit.

Thanks for the report.
 -- Tim