[elbe-devel] [PATCH v3] license text: fix encoding for both py2 and py3

bage at linutronix.de bage at linutronix.de
Mon Jan 20 17:59:29 CET 2020


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.

As Yegor pointed out, this led 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

Unify the encoding approach over both major Python version so that we get rid of
this error.

Signed-off-by: Christian Teklenborg <chris at linutronix.de>
Signed-off-by: Bastian Germann <bage at linutronix.de>
---
 elbepack/efilesystem.py | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/elbepack/efilesystem.py b/elbepack/efilesystem.py
index 050a95d95..f244ec9f6 100644
--- a/elbepack/efilesystem.py
+++ b/elbepack/efilesystem.py
@@ -147,18 +147,17 @@ class ElbeFilesystem(Filesystem):
             copyright_file = os.path.join('/usr/share/doc', pkg, 'copyright')
             copyright_fname = self.fname(copyright_file)
             try:
-                with io.open(copyright_fname, "rb") as lic:
+                with io.open(copyright_fname, "r",
+                             encoding="utf-8", errors="replace") as lic:
                     lic_text = lic.read()
             except IOError as e:
                 logging.exception("Error while processing license file %s",
                                   copyright_fname)
-                lic_text = "Error while processing license file %s: '%s'" % (
+                lic_text = u"Error while processing license file %s: '%s'" % (
                     copyright_file, e.strerror)
 
-            lic_text = lic_text.encode(encoding='utf-8', errors='replace')
-
             if f is not None:
-                f.write(pkg.encode(encoding='utf-8', errors='replace'))
+                f.write(pkg)
                 f.write(u":\n======================================"
                         "==========================================")
                 f.write(u"\n")
-- 
2.20.1




More information about the elbe-devel mailing list