#24 rpmfluff fails on object has no attribute 'encode' with new rpm
Closed: Fixed by jhutar. Opened by ksrot.

Due to this change
https://github.com/rpm-software-management/rpm/commit/84920f898315d09a57a3f1067433eaeb7de5e830
rpmfluff stops working.

The workaround is to use
$ sed -ie "s:.encode('utf-8')::g" rpmfluff.py
however backwards compatible fix would be more appropriate.


I have used following code to workaround it locally:

transaction_set = rpm.TransactionSet()
db_result = transaction_set.dbMatch('name', 'rpm')
package = list(db_result)[0]
if hasattr(package['version'], 'decode'):
    UTF8ENCODE = True
else:
    UTF8ENCODE = False
def utf8Encode(s):
    if UTF8ENCODE:
        return s.encode('utf-8')
    else:
        return s

and replace all occurrences of something.encode('utf-8') with utf8Encode(something).

Hello @ksrot. Could You please test https://pagure.io/rpmfluff/c/9fce96a8d0eb33e164eea3ddb5eac9f4442b1776?branch=master if it works for you and if it is backwards compatible?

Yep, it works both for old and updated rpm.

The only thing I am not sure about is

+    if UTF8ENCODE is None:
+         transaction_set = rpm.TransactionSet()
+         db_result = transaction_set.dbMatch('name', 'rpm')
+         package = list(db_result)[0]
+         UTF8ENCODE = hasattr(package['version'], 'decode') 

I have copied it from the bug report but someone familiar with rpm could probably propose more simple way how to detect rpm behaviour. Still, current approach works.

This should do it on any rpm >= 4.8 version:

h = rpm.hdr()
s = 'test'
h['name'] = s
UTF8ENCODE = (s != h['name'])

If you need to support older versions, it's easy to detect as that attempt to instantiate a header will fail with TypeError on rpm < 4.8, those versions only work with python2 so the behavior is known.

Thank you both. Change applied finally! Sorry for the lag.

https://pagure.io/rpmfluff/c/3148594b69614f00c0d266f886be84add1ba0fa8?branch=master

Metadata Update from @jhutar:
- Issue close_status updated to: Fixed
- Issue status updated to: Closed (was: Open)

Metadata