def handle_text(self):
        '''
        Takes care of converting body text to unicode, if its text at all.
        Sets self.original_encoding to original char encoding, and converts body
        to unicode if possible. Must come after handle_compression, and after
        self.mediaType is valid.
        '''
        self.encoding = None
        # if the body is text
        if (self.mediaType and
            (self.mediaType.type == 'text' or
                (self.mediaType.type == 'application' and
                 self.mediaType.subtype in 'xml'))):
            # if there was a charset parameter in HTTP header, store it
            if 'charset' in self.mediaType.params:
                override_encodings = [self.mediaType.params['charset']]
            else:
                override_encodings = []
            # if there even is data (otherwise,
            # dammit.originalEncoding might be None)
            if self.body != '':
                if UnicodeDammit:
                    # honestly, I don't mind not abiding by RFC 2023.
                    # UnicodeDammit just does what makes sense, and if the
                    # content is remotely standards-compliant, it will do the
                    # right thing.
                    dammit = UnicodeDammit(self.body, override_encodings)
                    # if unicode was found
                    if dammit.unicode:
                        self.text = dammit.unicode
                        self.originalEncoding = dammit.originalEncoding
                    else:
                        # unicode could not be decoded, at all
                        # HAR can't write data, but body might still
                        # be useful as-is
                        pass
                else:
                    # try the stupid version, just guess content-type or utf-8
                    u = None
                    # try our list of encodings + utf8 with strict errors
                    for e in override_encodings + ['utf8', 'iso-8859-1']:
                        try:
                            u = self.body.decode(e, 'strict')
                            self.originalEncoding = e
                            break  # if ^^ didn't throw, we're done
                        except UnicodeError:
                            pass
                    # if none of those worked, try utf8
                    # with 'replace' error mode
                    if not u:
                        # unicode has failed
                        u = self.body.decode('utf8', 'replace')
                        self.originalEncoding = None  # ???
                    self.text = u or None
        else:
            # body is not text
            # base64 encode it and set self.encoding
            # TODO: check with list that this is right
            self.text = b64encode(self.body)
            self.encoding = 'base64'
dataset/ETHPy150Open andrewf/pcap2har/pcap2har/http/response.py/Response.handle_text/swapped operands of `in`