sha256 hash operates with bytes and in Python3 all strings are unicode by default, we must encode the data before hashing to ensure they are bytes in Python3 Signed-off-by: Robert Kuska <rkuska@xxxxxxxxxx> --- sepolgen/src/sepolgen/util.py | 14 ++++++++++++++ sepolgen/src/sepolgen/yacc.py | 10 ++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/sepolgen/src/sepolgen/util.py b/sepolgen/src/sepolgen/util.py index 74a11f5..2edbf8c 100644 --- a/sepolgen/src/sepolgen/util.py +++ b/sepolgen/src/sepolgen/util.py @@ -76,6 +76,20 @@ def first(s, sorted=False): for x in s: return x +def encode_input(text): + import locale + """Encode given text via preferred system encoding""" + # locale will often find out the correct encoding + encoding = locale.getpreferredencoding() + try: + encoded_text = text.encode(encoding) + except UnicodeError: + # if it fails to find correct encoding then ascii is used + # which may lead to UnicodeError if `text` contains non ascii signs + # utf-8 is our guess to fix the situation + encoded_text = text.encode('utf-8') + return encoded_text + if __name__ == "__main__": import sys import time diff --git a/sepolgen/src/sepolgen/yacc.py b/sepolgen/src/sepolgen/yacc.py index cd113fc..b681785 100644 --- a/sepolgen/src/sepolgen/yacc.py +++ b/sepolgen/src/sepolgen/yacc.py @@ -69,6 +69,8 @@ error_count = 3 # Number of symbols that must be shifted to leave import re, types, sys, cStringIO, hashlib, os.path +from . import util + # Exception raised for yacc-related errors class YaccError(Exception): pass @@ -1962,7 +1964,7 @@ def yacc(method=default_lr, debug=yaccdebug, module=None, tabmodule=tab_module, # Add parsing method to signature - Signature.update(method) + Signature.update(util.encode_input(method)) # If a "module" parameter was supplied, extract its dictionary. # Note: a module may in fact be an instance as well. @@ -1995,7 +1997,7 @@ def yacc(method=default_lr, debug=yaccdebug, module=None, tabmodule=tab_module, if not start: start = ldict.get("start",None) if start: - Signature.update(start) + Signature.update(util.encode_input(start)) # If running in optimized mode. We're going to @@ -2064,7 +2066,7 @@ def yacc(method=default_lr, debug=yaccdebug, module=None, tabmodule=tab_module, if not (isinstance(prec,types.ListType) or isinstance(prec,types.TupleType)): raise YaccError,"precedence must be a list or tuple." add_precedence(prec) - Signature.update(repr(prec)) + Signature.update(util.encode_input(repr(prec))) for n in tokens: if not Precedence.has_key(n): @@ -2112,7 +2114,7 @@ def yacc(method=default_lr, debug=yaccdebug, module=None, tabmodule=tab_module, # Make a signature of the docstrings for f in symbols: if f.__doc__: - Signature.update(f.__doc__) + Signature.update(util.encode_input(f.__doc__)) lr_init_vars() -- 2.4.3 _______________________________________________ Selinux mailing list Selinux@xxxxxxxxxxxxx To unsubscribe, send email to Selinux-leave@xxxxxxxxxxxxx. To get help, send an email containing "help" to Selinux-request@xxxxxxxxxxxxx.