From 2816574be2b94b4a1f373708317894e8e2f8543f Mon Sep 17 00:00:00 2001 From: Fabio Erculiani Date: Sun, 4 Oct 2009 18:23:27 +0200 Subject: [PATCH] [entropy.const] improve const_convert_to_unicode and add const_convert_to_rawstring --- libraries/entropy/const.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/libraries/entropy/const.py b/libraries/entropy/const.py index 8c4f22d87..7eb9b7456 100644 --- a/libraries/entropy/const.py +++ b/libraries/entropy/const.py @@ -1365,13 +1365,27 @@ def const_isunicode(obj): else: return isinstance(obj, unicode) -def const_convert_to_unicode(obj): +def const_convert_to_unicode(obj, enctype = 'raw_unicode_escape'): + """ + Convert generic string to unicode format, this function supports both + Python 2.x and Python 3.x unicode bullshit. + + @param obj: generic string object + @type obj: string + @return: unicode string object + @rtype: unicode object + """ if const_isunicode(obj): return obj if sys.hexversion >= 0x3000000: - return str(obj, 'raw_unicode_escape') + return str(obj, enctype) else: - return unicode(obj, 'raw_unicode_escape') + return unicode(obj, enctype) + +def const_convert_to_rawstring(obj, from_enctype = 'raw_unicode_escape'): + if not const_isunicode(obj): + return obj + return obj.encode(from_enctype) def const_islive(): """