From a7cc1f53b27e8cb7f403cdc42e373f869ce90718 Mon Sep 17 00:00:00 2001 From: Fabio Erculiani Date: Wed, 7 Oct 2009 15:00:24 +0200 Subject: [PATCH] [entropy.const] more Python 3.x/2.x compat work --- libraries/entropy/const.py | 40 +++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/libraries/entropy/const.py b/libraries/entropy/const.py index 6527cd508..378b1689a 100644 --- a/libraries/entropy/const.py +++ b/libraries/entropy/const.py @@ -624,13 +624,20 @@ def const_default_settings(rootdir): 'ssl_ca_cert': default_etp_confdir+"/socket_server.CA.crt", 'ssl_ca_pkey': default_etp_confdir+"/socket_server.CA.key", 'answers': { - 'ok': chr(0)+"OK"+chr(0), # command run - 'er': chr(0)+"ER"+chr(1), # execution error - 'no': chr(0)+"NO"+chr(2), # not allowed - 'cl': chr(0)+"CL"+chr(3), # close connection - 'mcr': chr(0)+"MCR"+chr(4), # max connections reached - 'eos': chr(0), # end of size, - 'noop': chr(0)+"NOOP"+chr(0) + # command run + 'ok': const_convert_to_rawstring(chr(0)+"OK"+chr(0)), + # execution error + 'er': const_convert_to_rawstring(chr(0)+"ER"+chr(1)), + # not allowed + 'no': const_convert_to_rawstring(chr(0)+"NO"+chr(2)), + # close connection + 'cl': const_convert_to_rawstring(chr(0)+"CL"+chr(3)), + # max connections reached + 'mcr': const_convert_to_rawstring(chr(0)+"MCR"+chr(4)), + # end of size + 'eos': const_convert_to_rawstring(chr(0)), + # no operation + 'noop': const_convert_to_rawstring(chr(0)+"NOOP"+chr(0)), }, }, @@ -1392,6 +1399,11 @@ def const_convert_to_unicode(obj, enctype = 'raw_unicode_escape'): @return: unicode string object @rtype: unicode object """ + if isinstance(obj, int): + if sys.hexversion >= 0x3000000: + return str(obj) + else: + return unicode(obj) if isinstance(obj, const_get_buffer()): if sys.hexversion >= 0x3000000: return str(obj.tobytes(), enctype) @@ -1419,6 +1431,11 @@ def const_convert_to_rawstring(obj, from_enctype = 'raw_unicode_escape'): @return: raw string @rtype: bytes """ + if const_isnumber(obj): + if sys.hexversion >= 0x3000000: + return bytes(str(obj), from_enctype) + else: + return str(obj) if isinstance(obj, const_get_buffer()): if sys.hexversion >= 0x3000000: return obj.tobytes() @@ -1447,6 +1464,15 @@ def const_isfileobj(obj): else: return isinstance(obj, file) +def const_isnumber(obj): + """ + Return whether obj is an int, long object. + """ + if sys.hexversion >= 0x3000000: + return isinstance(obj, int) + else: + return isinstance(obj, (int, long,)) + def const_cmp(a, b): """ cmp() is gone in Python 3.x provide our own implementation.