#439 srpm_import: be compatible with rhbz#1693751
Merged 5 years ago by onosek. Opened 5 years ago by praiskup.
praiskup/rpkg compat-for-rhbz-1693751  into  master

file modified
+6 -5
@@ -1248,8 +1248,8 @@ 

          archlist = [pkg.header['arch'] for pkg in hdr.packages]

          if not archlist:

              raise rpkgError('No compatible build arches found in %s' % spec)

-         if six.PY3:

-             return [str(arch, encoding='utf-8') for arch in archlist]

+         if hasattr(archlist[0], 'decode'):

+             return [arch.decode('utf-8') for arch in archlist]

          else:

              return archlist

  
@@ -1339,9 +1339,10 @@ 

              hdr = koji.get_rpm_header(srpm)

              name = hdr[rpm.RPMTAG_NAME]

              contents = hdr[rpm.RPMTAG_FILENAMES]

-             if six.PY3:

-                 name = str(name, encoding='utf-8')

-                 contents = [str(filename, encoding='utf-8')

+             if hasattr(name, 'decode'):

+                 # RPM before rhbz#1693751 returned bytes

+                 name = name.decode('utf-8')

+                 contents = [filename.decode('utf-8')

                              for filename in contents]

          except Exception as e:

              raise rpkgError('Error querying srpm: {0}'.format(str(e)))

    srpm_import: be compatible with rhbz#1693751

    Older RPMs returned 'bytes' objects, newer return 'str'.

    If 'str' object is returned on Python 3 - it doesn't need to be
    decoded, and it actually raises error [1]:

        >>> str('', encoding='utf-8')
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
        TypeError: decoding str is not supported

    This change also OK for Python 2 where str() has decode method
    (str is alternative for bytes).

    [1] https://pagure.io/copr/copr/issue/677

    Signed-off-by: Pavel Raiskup <praiskup@redhat.com>

rebased onto c8e458adf8ea450734c4a650a74bbd4f2e0600b9

5 years ago

rebased onto 3f4ed25c5ef5fb9c05c3b420114889c37bdf6bbb

5 years ago

rebased onto 3d9523b

5 years ago

Looks good to me.

With this change, rpkg has 3 ways to handle the conversion from bytes to str at least:

  • checking if object has method decode
  • if six.PY3
  • if isinstance(s, six.text_type)

It would be good to refactor all of them into one function to make the conversion and reuse it in future. This could be done in another separate patch.

Pull-Request has been merged by onosek

5 years ago