Ejemplo n.º 1
0
 def __init__(self, content, name=None, namespace=None, stacklevel=None,
              get_template=None, default_inherit=None, line_offset=0):
     self.content = content
     self._unicode = is_unicode(content)
     if name is None and stacklevel is not None:
         try:
             caller = sys._getframe(stacklevel)
         except ValueError:
             pass
         else:
             globals = caller.f_globals
             lineno = caller.f_lineno
             if '__file__' in globals:
                 name = globals['__file__']
                 if name.endswith('.pyc') or name.endswith('.pyo'):
                     name = name[:-1]
             elif '__name__' in globals:
                 name = globals['__name__']
             else:
                 name = '<string>'
             if lineno:
                 name += ':%s' % lineno
     self.name = name
     self._parsed = parse(content, name=name, line_offset=line_offset)
     if namespace is None:
         namespace = {}
     self.namespace = namespace
     self.get_template = get_template
     if default_inherit is not None:
         self.default_inherit = default_inherit
Ejemplo n.º 2
0
 def _repr(self, value, pos):
     __traceback_hide__ = True
     try:
         if value is None:
             return ''
         if self._unicode:
             try:
                 value = text_type(value)
             except UnicodeDecodeError:
                 value = bytes(value)
         else:
             if not isinstance(value, basestring_):
                 value = coerce_text(value)
             if (is_unicode(value)
                 and self.default_encoding):
                 value = value.encode(self.default_encoding)
     except:
         exc_info = sys.exc_info()
         e = exc_info[1]
         e.args = (self._add_line_info(e.args[0], pos),)
         reraise((exc_info[0], e, exc_info[2]))
     else:
         if self._unicode and isinstance(value, bytes):
             if not self.default_encoding:
                 raise UnicodeDecodeError(
                     'Cannot decode bytes value %r into unicode '
                     '(no default_encoding provided)' % value)
             try:
                 value = value.decode(self.default_encoding)
             except UnicodeDecodeError as e:
                 raise UnicodeDecodeError(
                     e.encoding,
                     e.object,
                     e.start,
                     e.end,
                     e.reason + ' in string %r' % value)
         elif not self._unicode and is_unicode(value):
             if not self.default_encoding:
                 raise UnicodeEncodeError(
                     'Cannot encode unicode value %r into bytes '
                     '(no default_encoding provided)' % value)
             value = value.encode(self.default_encoding)
         return value
Ejemplo n.º 3
0
 def _repr(self, value, pos):
     __traceback_hide__ = True
     try:
         if value is None:
             return ''
         if self._unicode:
             try:
                 value = text_type(value)
             except UnicodeDecodeError:
                 value = bytes(value)
         else:
             if not isinstance(value, basestring_):
                 value = coerce_text(value)
             if (is_unicode(value) and self.default_encoding):
                 value = value.encode(self.default_encoding)
     except:
         exc_info = sys.exc_info()
         e = exc_info[1]
         e.args = (self._add_line_info(e.args[0], pos), )
         reraise((exc_info[0], e, exc_info[2]))
     else:
         if self._unicode and isinstance(value, bytes):
             if not self.default_encoding:
                 raise UnicodeDecodeError(
                     'Cannot decode bytes value %r into unicode '
                     '(no default_encoding provided)' % value)
             try:
                 value = value.decode(self.default_encoding)
             except UnicodeDecodeError as e:
                 raise UnicodeDecodeError(
                     e.encoding, e.object, e.start, e.end,
                     e.reason + ' in string %r' % value)
         elif not self._unicode and is_unicode(value):
             if not self.default_encoding:
                 raise UnicodeEncodeError(
                     'Cannot encode unicode value %r into bytes '
                     '(no default_encoding provided)' % value)
             value = value.encode(self.default_encoding)
         return value
Ejemplo n.º 4
0
def html_quote(value, force=True):
    if not force and hasattr(value, '__html__'):
        return value.__html__()
    if value is None:
        return ''
    if not isinstance(value, basestring_):
        value = coerce_text(value)
    if sys.version >= "3" and isinstance(value, bytes):
        value = cgi.escape(value.decode('latin1'), 1)
        value = value.encode('latin1')
    else:
        value = cgi.escape(value, 1)
    if sys.version < "3":
        if is_unicode(value):
            value = value.encode('ascii', 'xmlcharrefreplace')
    return value
Ejemplo n.º 5
0
def html_quote(value, force=True):
    if not force and hasattr(value, '__html__'):
        return value.__html__()
    if value is None:
        return ''
    if not isinstance(value, basestring_):
        value = coerce_text(value)
    if sys.version >= "3" and isinstance(value, bytes):
        value = cgi.escape(value.decode('latin1'), 1)
        value = value.encode('latin1')
    else:
        value = cgi.escape(value, 1)
    if sys.version < "3":
        if is_unicode(value):
            value = value.encode('ascii', 'xmlcharrefreplace')
    return value
Ejemplo n.º 6
0
 def __init__(self,
              content,
              name=None,
              namespace=None,
              stacklevel=None,
              get_template=None,
              default_inherit=None,
              line_offset=0):
     self.content = content
     self._unicode = is_unicode(content)
     if name is None and stacklevel is not None:
         try:
             caller = sys._getframe(stacklevel)
         except ValueError:
             pass
         else:
             globals = caller.f_globals
             lineno = caller.f_lineno
             if '__file__' in globals:
                 name = globals['__file__']
                 if name.endswith('.pyc') or name.endswith('.pyo'):
                     name = name[:-1]
             elif '__name__' in globals:
                 name = globals['__name__']
             else:
                 name = '<string>'
             if lineno:
                 name += ':%s' % lineno
     self.name = name
     self._parsed = parse(content, name=name, line_offset=line_offset)
     if namespace is None:
         namespace = {}
     self.namespace = namespace
     self.get_template = get_template
     if default_inherit is not None:
         self.default_inherit = default_inherit
Ejemplo n.º 7
0
def url(v):
    v = coerce_text(v)
    if is_unicode(v):
        v = v.encode('utf8')
    return url_quote(v)
Ejemplo n.º 8
0
def url(v):
    v = coerce_text(v)
    if is_unicode(v):
        v = v.encode('utf8')
    return url_quote(v)