[elbe-devel] [PATCH v2] fix use of encode instead of decode

Yegor Yefremov yegorslists at googlemail.com
Mon Jan 20 11:28:14 CET 2020


On Thu, Jan 16, 2020 at 9:23 AM Torben Hohn <torben.hohn at linutronix.de> wrote:
>
> On Wed, Jan 15, 2020 at 09:27:18PM +0100, bage at linutronix.de wrote:
> > From: Bastian Germann <bage at linutronix.de>
> >
> > 91b938d2491 ("Python3: use encode instead of unicode() function") changed the
> > Python 2 only unicode() calls to encode() calls.  However, decode is the right
> > function to be used here because a u"" string is expected as a result.
> >
> > Signed-off-by: Bastian Germann <bage at linutronix.de>
> > ---
> >  elbepack/efilesystem.py | 4 ++--
> >  elbepack/licencexml.py  | 2 +-
> >  2 files changed, 3 insertions(+), 3 deletions(-)
> >
> > diff --git a/elbepack/efilesystem.py b/elbepack/efilesystem.py
> > index 231471777..83551f2f1 100644
> > --- a/elbepack/efilesystem.py
> > +++ b/elbepack/efilesystem.py
> > @@ -154,10 +154,10 @@ class ElbeFilesystem(Filesystem):
> >                  lic_text = "Error while processing license file %s: '%s'" % (
> >                      copyright_file, e.strerror)
> >
> > -            lic_text = lic_text.encode(encoding='utf-8', errors='replace')
> > +            lic_text = lic_text.decode(encoding='utf-8', errors='replace')
>
> this one is correct. lic_text is supposed to be bytes before.
> although in case of an IOError, it would be 'str' which is ok in
> python2, but not ok in python3.
>
> Note: a python3 string only has an encode method. decode is a method of
> bytes.
>
> so... you must make sure, that
>
> lic_text = "Error while processing license file %s: '%s'" % (copyright_file, e.strerror)
>
> also yields bytes, or move that into the try: block.
>
>
>
> >
> >              if f is not None:
> > -                f.write(pkg.encode(encoding='utf-8', errors='replace'))
> > +                f.write(pkg.decode(encoding='utf-8', errors='replace'))
>
> pkg is a str and the encoding should already be set as a file attribute.
> i dont think, that encode is actually necessary here.

This leads to the following error:

Traceback (most recent call last):
  File "/var/cache/elbe/devel/elbepack/asyncworker.py", line 186, in execute
    skip_pbuild=self.skip_pbuilder)
  File "/var/cache/elbe/devel/elbepack/elbeproject.py", line 585, in build
    self.buildenv.rfs.write_licenses(f, pkgnames, lic_xml_fname)
  File "/var/cache/elbe/devel/elbepack/efilesystem.py", line 161, in
write_licenses
    f.write(pkg.encode(encoding='utf-8', errors='replace'))
TypeError: write() argument 1 must be unicode, not str

Yegor

>
> >                  f.write(u":\n======================================"
> >                          "==========================================")
> >                  f.write(u"\n")
> > diff --git a/elbepack/licencexml.py b/elbepack/licencexml.py
> > index 09ab3cff7..ac84b330c 100644
> > --- a/elbepack/licencexml.py
> > +++ b/elbepack/licencexml.py
> > @@ -60,7 +60,7 @@ class copyright_xml (object):
> >          txtnode = xmlpkg.append('text')
> >          txtnode.et.text = copyright_text
> >
> > -        bytesio = io.StringIO(txtnode.et.text.encode(encoding='utf-8', errors='replace'))
> > +        bytesio = io.StringIO(txtnode.et.text.decode(encoding='utf-8', errors='replace'))
>
> txtnode.et.text is a string also.
> i believe, that this was correct.
>
> >          try:
> >              c = Copyright(bytesio)
> >              files = []
> > --
> > 2.20.1
> >
>
> --
> Torben Hohn
> Linutronix GmbH | Bahnhofstrasse 3 | D-88690 Uhldingen-Mühlhofen
> Phone: +49 7556 25 999 18; Fax.: +49 7556 25 999 99
>
> Hinweise zum Datenschutz finden Sie hier (Informations on data privacy
> can be found here): https://linutronix.de/kontakt/Datenschutz.php
>
> Linutronix GmbH | Firmensitz (Registered Office): Uhldingen-Mühlhofen |
> Registergericht (Registration Court): Amtsgericht Freiburg i.Br., HRB700
> 806 | Geschäftsführer (Managing Directors): Heinz Egger, Thomas Gleixner
>
> _______________________________________________
> elbe-devel mailing list
> elbe-devel at linutronix.de
> https://lists.linutronix.de/mailman/listinfo/elbe-devel



More information about the elbe-devel mailing list