from consolation.StaticClass import StaticClass
import sys
import binascii

__copying__="""
Written by Thomas Hori

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/."""

class Compat3k(StaticClass):
    @classmethod
    def str_to_bytes(cls, s):
        """Convert a string of either width to a byte string."""
        try:
            try:
                if type(s) == bytes: #pylint: disable = unidiomatic-typecheck
                    return s
                return bytes(s)
            except NameError:
                return str(s)
        except ValueError:
            pass #Not ASCII?  Not really a problem...
        except TypeError:
            pass #I didn't specify an encoding?  Oh, boo hoo...
        return s.encode("latin1") #Not utf-8, m'kay...
    @classmethod
    def prompt_user(cls, s="", file=None):
        """Substitute of py2k's raw_input()."""
        (file or sys.stderr).write(s)
        (file or sys.stderr).flush()
        return sys.stdin.readline().rstrip("\r\n")
    #XXXXXXXXXXX FIX THESE
    @classmethod
    def hexlify(cls, s):
        return binascii.hexlify(s.encode("latin1"))
    @classmethod
    def unhexlify(cls, s):
        return binascii.unhexlify(s).decode("latin1")
    

@@ -343,24 +343,101 @@
 ticClass):%0D%0A
+    try:%0D%0A        bytes = bytes%0D%0A    except NameError:%0D%0A        bytes = str%0D%0A
     @classme
@@ -1255,227 +1255,739 @@
    #
-XXXXXXXXXXX FIX THESE%0D%0A    @classmethod%0D%0A    def hexlify(cls, s):%0D%0A        return binascii.hexlify(s.encode(%22latin1%22))%0D%0A    @classmethod%0D%0A    def unhexlify(cls, s):%0D%0A        return binascii.unhexlify(s).decode(%22latin1%22)
+Use unidiomatic (type(...)) type-checks to avoid issues where%0D%0A    #one of the types in question may be a subtype of the other.%0D%0A    @classmethod%0D%0A    def hexlify(cls, s):%0D%0A        if type(s)!=cls.bytes: #pylint: disable = unidiomatic-typecheck%0D%0A            s=s.encode(%22latin1%22)%0D%0A        r=binascii.hexlify(s)%0D%0A        if type(r)!=str: #pylint: disable = unidiomatic-typecheck%0D%0A            r=r.decode(%22ascii%22)%0D%0A        return r%0D%0A    @classmethod%0D%0A    def unhexlify(cls, s):%0D%0A        if type(s)!=cls.bytes: #pylint: disable = unidiomatic-typecheck%0D%0A            s=s.encode(%22ascii%22)%0D%0A        r=binascii.unhexlify(s)%0D%0A        if type(r)!=str: #pylint: disable = unidiomatic-typecheck%0D%0A            r=r.decode(%22latin1%22)%0D%0A        return r
 %0D%0A  

Fix Py3k support this time.