Example #1
0
 def decode(x):
     if isinstance(x, compat.text_type):
         return x
     elif not isinstance(x, compat.binary_type):
         return compat.text_type(str(x), encoding=key)
     else:
         return compat.text_type(x, encoding=key)
Example #2
0
 def decode(x):
     if isinstance(x, compat.text_type):
         return x
     elif not isinstance(x, compat.binary_type):
         return compat.text_type(str(x), encoding=key)
     else:
         return compat.text_type(x, encoding=key)
Example #3
0
 def _init_message(self):
     """Find a unicode representation of self.error"""
     try:
         self.message = compat.text_type(self.error)
     except UnicodeError:
         try:
             self.message = str(self.error)
         except UnicodeEncodeError:
             # Fallback to args as neither unicode nor
             # str(Exception(u'\xe6')) work in Python < 2.6
             self.message = self.error.args[0]
     if not isinstance(self.message, compat.text_type):
         self.message = compat.text_type(self.message, "ascii", "replace")
Example #4
0
def raises(except_cls, message=None):
    try:
        yield
        success = False
    except except_cls as e:
        if message:
            assert re.search(message, compat.text_type(e), re.UNICODE), \
                            "%r !~ %s" % (message, e)
            print(compat.text_type(e).encode('utf-8'))
        success = True

    # assert outside the block so it works for AssertionError too !
    assert success, "Callable did not raise an exception"
Example #5
0
 def _init_message(self):
     """Find a unicode representation of self.error"""
     try:
         self.message = compat.text_type(self.error)
     except UnicodeError:
         try:
             self.message = str(self.error)
         except UnicodeEncodeError:
             # Fallback to args as neither unicode nor
             # str(Exception(u'\xe6')) work in Python < 2.6
             self.message = self.error.args[0]
     if not isinstance(self.message, compat.text_type):
         self.message = compat.text_type(self.message, 'ascii', 'replace')
Example #6
0
def raises(except_cls, message=None):
    try:
        yield
        success = False
    except except_cls as e:
        if message:
            assert re.search(message, compat.text_type(e), re.UNICODE), \
                            "%r !~ %s" % (message, e)
            print(compat.text_type(e).encode('utf-8'))
        success = True

    # assert outside the block so it works for AssertionError too !
    assert success, "Callable did not raise an exception"
Example #7
0
def _as_unicode(arg):
    if isinstance(arg, compat.string_types):
        return compat.text_type(arg)
    elif isinstance(arg, dict):
        return dict((_as_unicode(k), _as_unicode(v)) for k, v in arg.items())
    else:
        return arg
Example #8
0
 def __init__(self, codepoint2name, name2codepoint):
     self.codepoint2entity = dict(
         [
             (c, compat.text_type("&%s;" % n))
             for c, n in codepoint2name.items()
         ]
     )
     self.name2codepoint = name2codepoint
Example #9
0
def _as_unicode(arg):
    if isinstance(arg, compat.string_types):
        return compat.text_type(arg)
    elif isinstance(arg, dict):
        return dict(
            (_as_unicode(k), _as_unicode(v))
            for k, v in arg.items()
        )
    else:
        return arg
Example #10
0
    def escape(self, text):
        """Replace characters with their character references.

        Replace characters by their named entity references.
        Non-ASCII characters, if they do not have a named entity reference,
        are replaced by numerical character references.

        The return value is guaranteed to be ASCII.
        """
        return self.__escapable.sub(self.__escape, compat.text_type(text)).encode("ascii")
Example #11
0
    def escape(self, text):
        """Replace characters with their character references.

        Replace characters by their named entity references.
        Non-ASCII characters, if they do not have a named entity reference,
        are replaced by numerical character references.

        The return value is guaranteed to be ASCII.
        """
        return self.__escapable.sub(self.__escape, compat.text_type(text)
                                    ).encode('ascii')
Example #12
0
def htmlentityreplace_errors(ex):
    """An encoding error handler.

    This python `codecs`_ error handler replaces unencodable
    characters with HTML entities, or, if no HTML entity exists for
    the character, XML character references.

    >>> u'The cost was \u20ac12.'.encode('latin1', 'htmlentityreplace')
    'The cost was &euro;12.'
    """
    if isinstance(ex, UnicodeEncodeError):
        # Handle encoding errors
        bad_text = ex.object[ex.start:ex.end]
        text = _html_entities_escaper.escape(bad_text)
        return (compat.text_type(text), ex.end)
    raise ex
Example #13
0
 def __init__(self, codepoint2name, name2codepoint):
     self.codepoint2entity = dict([(c, compat.text_type('&%s;' % n))
                                   for c, n in codepoint2name.items()])
     self.name2codepoint = name2codepoint
Example #14
0
    def escape_entities(self, text):
        """Replace characters with their character entity references.

        Only characters corresponding to a named entity are replaced.
        """
        return compat.text_type(text).translate(self.codepoint2entity)