diff options
author | artkolesnikov <artkolesnikov@yandex-team.ru> | 2022-02-10 16:47:37 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:47:37 +0300 |
commit | 0d7c4ef9025d79c9bba6f62a92b490ee72e80ed0 (patch) | |
tree | c0748b5dcbade83af788c0abfa89c0383d6b779c /contrib/python/MarkupSafe/py2 | |
parent | 8611780b719073fe6c7e6536c71d61e20d57a5d6 (diff) | |
download | ydb-0d7c4ef9025d79c9bba6f62a92b490ee72e80ed0.tar.gz |
Restoring authorship annotation for <artkolesnikov@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'contrib/python/MarkupSafe/py2')
-rw-r--r-- | contrib/python/MarkupSafe/py2/markupsafe/__init__.py | 370 | ||||
-rw-r--r-- | contrib/python/MarkupSafe/py2/markupsafe/_compat.py | 36 | ||||
-rw-r--r-- | contrib/python/MarkupSafe/py2/markupsafe/_constants.py | 14 | ||||
-rw-r--r-- | contrib/python/MarkupSafe/py2/markupsafe/_native.py | 48 | ||||
-rw-r--r-- | contrib/python/MarkupSafe/py2/markupsafe/_speedups.c | 442 | ||||
-rw-r--r-- | contrib/python/MarkupSafe/py2/ya.make | 22 |
6 files changed, 466 insertions, 466 deletions
diff --git a/contrib/python/MarkupSafe/py2/markupsafe/__init__.py b/contrib/python/MarkupSafe/py2/markupsafe/__init__.py index a3697a9477..da05ed328a 100644 --- a/contrib/python/MarkupSafe/py2/markupsafe/__init__.py +++ b/contrib/python/MarkupSafe/py2/markupsafe/__init__.py @@ -1,17 +1,17 @@ -# -*- coding: utf-8 -*- -""" +# -*- coding: utf-8 -*- +""" markupsafe ~~~~~~~~~~ - + Implements an escape function and a Markup string to replace HTML special characters with safe representations. - + :copyright: 2010 Pallets :license: BSD-3-Clause -""" -import re -import string - +""" +import re +import string + from ._compat import int_types from ._compat import iteritems from ._compat import Mapping @@ -19,31 +19,31 @@ from ._compat import PY2 from ._compat import string_types from ._compat import text_type from ._compat import unichr - + __version__ = "1.1.1" - + __all__ = ["Markup", "soft_unicode", "escape", "escape_silent"] - + _striptags_re = re.compile(r"(<!--.*?-->|<[^>]*>)") _entity_re = re.compile(r"&([^& ;]+);") - - -class Markup(text_type): + + +class Markup(text_type): """A string that is ready to be safely inserted into an HTML or XML document, either because it was escaped or because it was marked safe. - + Passing an object to the constructor converts it to text and wraps it to mark it safe without escaping. To escape the text, use the :meth:`escape` class method instead. - + >>> Markup('Hello, <em>World</em>!') Markup('Hello, <em>World</em>!') >>> Markup(42) Markup('42') >>> Markup.escape('Hello, <em>World</em>!') Markup('Hello <em>World</em>!') - + This implements the ``__html__()`` interface that some frameworks use. Passing an object that implements ``__html__()`` will wrap the output of that method, marking it safe. @@ -51,136 +51,136 @@ class Markup(text_type): >>> class Foo: ... def __html__(self): ... return '<a href="/foo">foo</a>' - ... - >>> Markup(Foo()) + ... + >>> Markup(Foo()) Markup('<a href="/foo">foo</a>') - + This is a subclass of the text type (``str`` in Python 3, ``unicode`` in Python 2). It has the same methods as that type, but all methods escape their arguments and return a ``Markup`` instance. - + >>> Markup('<em>%s</em>') % 'foo & bar' Markup('<em>foo & bar</em>') >>> Markup('<em>Hello</em> ') + '<foo>' Markup('<em>Hello</em> <foo>') """ - - __slots__ = () - + + __slots__ = () + def __new__(cls, base=u"", encoding=None, errors="strict"): if hasattr(base, "__html__"): - base = base.__html__() - if encoding is None: - return text_type.__new__(cls, base) - return text_type.__new__(cls, base, encoding, errors) - - def __html__(self): - return self - - def __add__(self, other): + base = base.__html__() + if encoding is None: + return text_type.__new__(cls, base) + return text_type.__new__(cls, base, encoding, errors) + + def __html__(self): + return self + + def __add__(self, other): if isinstance(other, string_types) or hasattr(other, "__html__"): - return self.__class__(super(Markup, self).__add__(self.escape(other))) - return NotImplemented - - def __radd__(self, other): + return self.__class__(super(Markup, self).__add__(self.escape(other))) + return NotImplemented + + def __radd__(self, other): if hasattr(other, "__html__") or isinstance(other, string_types): - return self.escape(other).__add__(self) - return NotImplemented - - def __mul__(self, num): - if isinstance(num, int_types): - return self.__class__(text_type.__mul__(self, num)) - return NotImplemented - - __rmul__ = __mul__ - - def __mod__(self, arg): - if isinstance(arg, tuple): - arg = tuple(_MarkupEscapeHelper(x, self.escape) for x in arg) - else: - arg = _MarkupEscapeHelper(arg, self.escape) - return self.__class__(text_type.__mod__(self, arg)) - - def __repr__(self): + return self.escape(other).__add__(self) + return NotImplemented + + def __mul__(self, num): + if isinstance(num, int_types): + return self.__class__(text_type.__mul__(self, num)) + return NotImplemented + + __rmul__ = __mul__ + + def __mod__(self, arg): + if isinstance(arg, tuple): + arg = tuple(_MarkupEscapeHelper(x, self.escape) for x in arg) + else: + arg = _MarkupEscapeHelper(arg, self.escape) + return self.__class__(text_type.__mod__(self, arg)) + + def __repr__(self): return "%s(%s)" % (self.__class__.__name__, text_type.__repr__(self)) - - def join(self, seq): - return self.__class__(text_type.join(self, map(self.escape, seq))) - - join.__doc__ = text_type.join.__doc__ - - def split(self, *args, **kwargs): - return list(map(self.__class__, text_type.split(self, *args, **kwargs))) - - split.__doc__ = text_type.split.__doc__ - - def rsplit(self, *args, **kwargs): - return list(map(self.__class__, text_type.rsplit(self, *args, **kwargs))) - - rsplit.__doc__ = text_type.rsplit.__doc__ - - def splitlines(self, *args, **kwargs): + + def join(self, seq): + return self.__class__(text_type.join(self, map(self.escape, seq))) + + join.__doc__ = text_type.join.__doc__ + + def split(self, *args, **kwargs): + return list(map(self.__class__, text_type.split(self, *args, **kwargs))) + + split.__doc__ = text_type.split.__doc__ + + def rsplit(self, *args, **kwargs): + return list(map(self.__class__, text_type.rsplit(self, *args, **kwargs))) + + rsplit.__doc__ = text_type.rsplit.__doc__ + + def splitlines(self, *args, **kwargs): return list(map(self.__class__, text_type.splitlines(self, *args, **kwargs))) - splitlines.__doc__ = text_type.splitlines.__doc__ - - def unescape(self): + splitlines.__doc__ = text_type.splitlines.__doc__ + + def unescape(self): """Convert escaped markup back into a text string. This replaces HTML entities with the characters they represent. - + >>> Markup('Main » <em>About</em>').unescape() 'Main » <em>About</em>' - """ + """ from ._constants import HTML_ENTITIES - def handle_match(m): - name = m.group(1) - if name in HTML_ENTITIES: - return unichr(HTML_ENTITIES[name]) - try: + def handle_match(m): + name = m.group(1) + if name in HTML_ENTITIES: + return unichr(HTML_ENTITIES[name]) + try: if name[:2] in ("#x", "#X"): - return unichr(int(name[2:], 16)) + return unichr(int(name[2:], 16)) elif name.startswith("#"): - return unichr(int(name[1:])) - except ValueError: - pass + return unichr(int(name[1:])) + except ValueError: + pass # Don't modify unexpected input. return m.group() - return _entity_re.sub(handle_match, text_type(self)) - - def striptags(self): + return _entity_re.sub(handle_match, text_type(self)) + + def striptags(self): """:meth:`unescape` the markup, remove tags, and normalize whitespace to single spaces. - + >>> Markup('Main »\t<em>About</em>').striptags() 'Main » About' - """ + """ stripped = u" ".join(_striptags_re.sub("", self).split()) - return Markup(stripped).unescape() - - @classmethod - def escape(cls, s): + return Markup(stripped).unescape() + + @classmethod + def escape(cls, s): """Escape a string. Calls :func:`escape` and ensures that for subclasses the correct type is returned. - """ - rv = escape(s) - if rv.__class__ is not cls: - return cls(rv) - return rv - + """ + rv = escape(s) + if rv.__class__ is not cls: + return cls(rv) + return rv + def make_simple_escaping_wrapper(name): # noqa: B902 - orig = getattr(text_type, name) + orig = getattr(text_type, name) + + def func(self, *args, **kwargs): + args = _escape_argspec(list(args), enumerate(args), self.escape) + _escape_argspec(kwargs, iteritems(kwargs), self.escape) + return self.__class__(orig(self, *args, **kwargs)) - def func(self, *args, **kwargs): - args = _escape_argspec(list(args), enumerate(args), self.escape) - _escape_argspec(kwargs, iteritems(kwargs), self.escape) - return self.__class__(orig(self, *args, **kwargs)) + func.__name__ = orig.__name__ + func.__doc__ = orig.__doc__ + return func - func.__name__ = orig.__name__ - func.__doc__ = orig.__doc__ - return func - for method in ( "__getitem__", "capitalize", @@ -199,110 +199,110 @@ class Markup(text_type): "swapcase", "zfill", ): - locals()[method] = make_simple_escaping_wrapper(method) - + locals()[method] = make_simple_escaping_wrapper(method) + def partition(self, sep): return tuple(map(self.__class__, text_type.partition(self, self.escape(sep)))) - + def rpartition(self, sep): return tuple(map(self.__class__, text_type.rpartition(self, self.escape(sep)))) - + def format(self, *args, **kwargs): formatter = EscapeFormatter(self.escape) kwargs = _MagicFormatMapping(args, kwargs) return self.__class__(formatter.vformat(self, args, kwargs)) - + def __html_format__(self, format_spec): if format_spec: raise ValueError("Unsupported format specification " "for Markup.") return self - # not in python 3 + # not in python 3 if hasattr(text_type, "__getslice__"): __getslice__ = make_simple_escaping_wrapper("__getslice__") - - del method, make_simple_escaping_wrapper - - -class _MagicFormatMapping(Mapping): - """This class implements a dummy wrapper to fix a bug in the Python - standard library for string formatting. - - See http://bugs.python.org/issue13598 for information about why - this is necessary. - """ - - def __init__(self, args, kwargs): - self._args = args - self._kwargs = kwargs - self._last_index = 0 - - def __getitem__(self, key): + + del method, make_simple_escaping_wrapper + + +class _MagicFormatMapping(Mapping): + """This class implements a dummy wrapper to fix a bug in the Python + standard library for string formatting. + + See http://bugs.python.org/issue13598 for information about why + this is necessary. + """ + + def __init__(self, args, kwargs): + self._args = args + self._kwargs = kwargs + self._last_index = 0 + + def __getitem__(self, key): if key == "": - idx = self._last_index - self._last_index += 1 - try: - return self._args[idx] - except LookupError: - pass - key = str(idx) - return self._kwargs[key] - - def __iter__(self): - return iter(self._kwargs) - - def __len__(self): - return len(self._kwargs) - - + idx = self._last_index + self._last_index += 1 + try: + return self._args[idx] + except LookupError: + pass + key = str(idx) + return self._kwargs[key] + + def __iter__(self): + return iter(self._kwargs) + + def __len__(self): + return len(self._kwargs) + + if hasattr(text_type, "format"): - class EscapeFormatter(string.Formatter): - def __init__(self, escape): - self.escape = escape - - def format_field(self, value, format_spec): + class EscapeFormatter(string.Formatter): + def __init__(self, escape): + self.escape = escape + + def format_field(self, value, format_spec): if hasattr(value, "__html_format__"): - rv = value.__html_format__(format_spec) + rv = value.__html_format__(format_spec) elif hasattr(value, "__html__"): - if format_spec: + if format_spec: raise ValueError( "Format specifier {0} given, but {1} does not" " define __html_format__. A class that defines" " __html__ must define __html_format__ to work" " with format specifiers.".format(format_spec, type(value)) ) - rv = value.__html__() - else: + rv = value.__html__() + else: # We need to make sure the format spec is unicode here as # otherwise the wrong callback methods are invoked. For # instance a byte string there would invoke __str__ and # not __unicode__. rv = string.Formatter.format_field(self, value, text_type(format_spec)) - return text_type(self.escape(rv)) - - -def _escape_argspec(obj, iterable, escape): - """Helper for various string-wrapped functions.""" - for key, value in iterable: + return text_type(self.escape(rv)) + + +def _escape_argspec(obj, iterable, escape): + """Helper for various string-wrapped functions.""" + for key, value in iterable: if hasattr(value, "__html__") or isinstance(value, string_types): - obj[key] = escape(value) - return obj - - -class _MarkupEscapeHelper(object): - """Helper for Markup.__mod__""" - - def __init__(self, obj, escape): - self.obj = obj - self.escape = escape - + obj[key] = escape(value) + return obj + + +class _MarkupEscapeHelper(object): + """Helper for Markup.__mod__""" + + def __init__(self, obj, escape): + self.obj = obj + self.escape = escape + def __getitem__(self, item): return _MarkupEscapeHelper(self.obj[item], self.escape) - + def __str__(self): return text_type(self.escape(self.obj)) - + __unicode__ = __str__ def __repr__(self): @@ -315,13 +315,13 @@ class _MarkupEscapeHelper(object): return float(self.obj) -# we have to import it down here as the speedups and native -# modules imports the markup type which is define above. -try: +# we have to import it down here as the speedups and native +# modules imports the markup type which is define above. +try: from ._speedups import escape, escape_silent, soft_unicode -except ImportError: +except ImportError: from ._native import escape, escape_silent, soft_unicode - -if not PY2: - soft_str = soft_unicode + +if not PY2: + soft_str = soft_unicode __all__.append("soft_str") diff --git a/contrib/python/MarkupSafe/py2/markupsafe/_compat.py b/contrib/python/MarkupSafe/py2/markupsafe/_compat.py index 22afe4eda5..bc05090f9e 100644 --- a/contrib/python/MarkupSafe/py2/markupsafe/_compat.py +++ b/contrib/python/MarkupSafe/py2/markupsafe/_compat.py @@ -1,31 +1,31 @@ -# -*- coding: utf-8 -*- -""" +# -*- coding: utf-8 -*- +""" markupsafe._compat ~~~~~~~~~~~~~~~~~~ - + :copyright: 2010 Pallets :license: BSD-3-Clause -""" -import sys - -PY2 = sys.version_info[0] == 2 - -if not PY2: - text_type = str - string_types = (str,) - unichr = chr - int_types = (int,) +""" +import sys + +PY2 = sys.version_info[0] == 2 + +if not PY2: + text_type = str + string_types = (str,) + unichr = chr + int_types = (int,) def iteritems(x): return iter(x.items()) from collections.abc import Mapping -else: - text_type = unicode - string_types = (str, unicode) - unichr = unichr - int_types = (int, long) +else: + text_type = unicode + string_types = (str, unicode) + unichr = unichr + int_types = (int, long) def iteritems(x): return x.iteritems() diff --git a/contrib/python/MarkupSafe/py2/markupsafe/_constants.py b/contrib/python/MarkupSafe/py2/markupsafe/_constants.py index 1a14c3cde1..7c57c2d294 100644 --- a/contrib/python/MarkupSafe/py2/markupsafe/_constants.py +++ b/contrib/python/MarkupSafe/py2/markupsafe/_constants.py @@ -1,13 +1,13 @@ -# -*- coding: utf-8 -*- -""" +# -*- coding: utf-8 -*- +""" markupsafe._constants ~~~~~~~~~~~~~~~~~~~~~ - + :copyright: 2010 Pallets :license: BSD-3-Clause -""" - -HTML_ENTITIES = { +""" + +HTML_ENTITIES = { "AElig": 198, "Aacute": 193, "Acirc": 194, @@ -261,4 +261,4 @@ HTML_ENTITIES = { "zeta": 950, "zwj": 8205, "zwnj": 8204, -} +} diff --git a/contrib/python/MarkupSafe/py2/markupsafe/_native.py b/contrib/python/MarkupSafe/py2/markupsafe/_native.py index 773556d290..cd08752cd8 100644 --- a/contrib/python/MarkupSafe/py2/markupsafe/_native.py +++ b/contrib/python/MarkupSafe/py2/markupsafe/_native.py @@ -1,18 +1,18 @@ -# -*- coding: utf-8 -*- -""" +# -*- coding: utf-8 -*- +""" markupsafe._native ~~~~~~~~~~~~~~~~~~ - + Native Python implementation used when the C module is not compiled. - + :copyright: 2010 Pallets :license: BSD-3-Clause -""" +""" from . import Markup from ._compat import text_type - - -def escape(s): + + +def escape(s): """Replace the characters ``&``, ``<``, ``>``, ``'``, and ``"`` in the string with HTML-safe sequences. Use this if you need to display text that might contain such characters in HTML. @@ -22,7 +22,7 @@ def escape(s): :param s: An object to be converted to a string and escaped. :return: A :class:`Markup` string with the escaped text. - """ + """ if hasattr(s, "__html__"): return Markup(s.__html__()) return Markup( @@ -32,10 +32,10 @@ def escape(s): .replace("<", "<") .replace("'", "'") .replace('"', """) - ) - - -def escape_silent(s): + ) + + +def escape_silent(s): """Like :func:`escape` but treats ``None`` as the empty string. Useful with optional values, as otherwise you get the string ``'None'`` when the value is ``None``. @@ -44,13 +44,13 @@ def escape_silent(s): Markup('None') >>> escape_silent(None) Markup('') - """ - if s is None: - return Markup() - return escape(s) - - -def soft_unicode(s): + """ + if s is None: + return Markup() + return escape(s) + + +def soft_unicode(s): """Convert an object to a string if it isn't already. This preserves a :class:`Markup` string rather than converting it back to a basic string, so it will still be marked as safe and won't be escaped @@ -63,7 +63,7 @@ def soft_unicode(s): Markup('&lt;User 1&gt;') >>> escape(soft_unicode(value)) Markup('<User 1>') - """ - if not isinstance(s, text_type): - s = text_type(s) - return s + """ + if not isinstance(s, text_type): + s = text_type(s) + return s diff --git a/contrib/python/MarkupSafe/py2/markupsafe/_speedups.c b/contrib/python/MarkupSafe/py2/markupsafe/_speedups.c index 5dc15ddbe7..fabfdc98a1 100644 --- a/contrib/python/MarkupSafe/py2/markupsafe/_speedups.c +++ b/contrib/python/MarkupSafe/py2/markupsafe/_speedups.c @@ -1,118 +1,118 @@ -/** - * markupsafe._speedups - * ~~~~~~~~~~~~~~~~~~~~ - * +/** + * markupsafe._speedups + * ~~~~~~~~~~~~~~~~~~~~ + * * C implementation of escaping for better performance. Used instead of * the native Python implementation when compiled. - * + * * :copyright: 2010 Pallets * :license: BSD-3-Clause - */ -#include <Python.h> - + */ +#include <Python.h> + #if PY_MAJOR_VERSION < 3 -#define ESCAPED_CHARS_TABLE_SIZE 63 -#define UNICHR(x) (PyUnicode_AS_UNICODE((PyUnicodeObject*)PyUnicode_DecodeASCII(x, strlen(x), NULL))); - +#define ESCAPED_CHARS_TABLE_SIZE 63 +#define UNICHR(x) (PyUnicode_AS_UNICODE((PyUnicodeObject*)PyUnicode_DecodeASCII(x, strlen(x), NULL))); + static Py_ssize_t escaped_chars_delta_len[ESCAPED_CHARS_TABLE_SIZE]; static Py_UNICODE *escaped_chars_repl[ESCAPED_CHARS_TABLE_SIZE]; -#endif - -static PyObject* markup; - -static int -init_constants(void) -{ - PyObject *module; +#endif + +static PyObject* markup; + +static int +init_constants(void) +{ + PyObject *module; #if PY_MAJOR_VERSION < 3 /* mapping of characters to replace */ - escaped_chars_repl['"'] = UNICHR("""); - escaped_chars_repl['\''] = UNICHR("'"); - escaped_chars_repl['&'] = UNICHR("&"); - escaped_chars_repl['<'] = UNICHR("<"); - escaped_chars_repl['>'] = UNICHR(">"); - - /* lengths of those characters when replaced - 1 */ - memset(escaped_chars_delta_len, 0, sizeof (escaped_chars_delta_len)); - escaped_chars_delta_len['"'] = escaped_chars_delta_len['\''] = \ - escaped_chars_delta_len['&'] = 4; - escaped_chars_delta_len['<'] = escaped_chars_delta_len['>'] = 3; + escaped_chars_repl['"'] = UNICHR("""); + escaped_chars_repl['\''] = UNICHR("'"); + escaped_chars_repl['&'] = UNICHR("&"); + escaped_chars_repl['<'] = UNICHR("<"); + escaped_chars_repl['>'] = UNICHR(">"); + + /* lengths of those characters when replaced - 1 */ + memset(escaped_chars_delta_len, 0, sizeof (escaped_chars_delta_len)); + escaped_chars_delta_len['"'] = escaped_chars_delta_len['\''] = \ + escaped_chars_delta_len['&'] = 4; + escaped_chars_delta_len['<'] = escaped_chars_delta_len['>'] = 3; #endif - /* import markup type so that we can mark the return value */ - module = PyImport_ImportModule("markupsafe"); - if (!module) - return 0; - markup = PyObject_GetAttrString(module, "Markup"); - Py_DECREF(module); - - return 1; -} - + /* import markup type so that we can mark the return value */ + module = PyImport_ImportModule("markupsafe"); + if (!module) + return 0; + markup = PyObject_GetAttrString(module, "Markup"); + Py_DECREF(module); + + return 1; +} + #if PY_MAJOR_VERSION < 3 -static PyObject* -escape_unicode(PyUnicodeObject *in) -{ - PyUnicodeObject *out; - Py_UNICODE *inp = PyUnicode_AS_UNICODE(in); - const Py_UNICODE *inp_end = PyUnicode_AS_UNICODE(in) + PyUnicode_GET_SIZE(in); - Py_UNICODE *next_escp; - Py_UNICODE *outp; - Py_ssize_t delta=0, erepl=0, delta_len=0; - - /* First we need to figure out how long the escaped string will be */ - while (*(inp) || inp < inp_end) { - if (*inp < ESCAPED_CHARS_TABLE_SIZE) { - delta += escaped_chars_delta_len[*inp]; - erepl += !!escaped_chars_delta_len[*inp]; - } - ++inp; - } - - /* Do we need to escape anything at all? */ - if (!erepl) { - Py_INCREF(in); - return (PyObject*)in; - } - - out = (PyUnicodeObject*)PyUnicode_FromUnicode(NULL, PyUnicode_GET_SIZE(in) + delta); - if (!out) - return NULL; - - outp = PyUnicode_AS_UNICODE(out); - inp = PyUnicode_AS_UNICODE(in); - while (erepl-- > 0) { - /* look for the next substitution */ - next_escp = inp; - while (next_escp < inp_end) { - if (*next_escp < ESCAPED_CHARS_TABLE_SIZE && - (delta_len = escaped_chars_delta_len[*next_escp])) { - ++delta_len; - break; - } - ++next_escp; - } - - if (next_escp > inp) { - /* copy unescaped chars between inp and next_escp */ - Py_UNICODE_COPY(outp, inp, next_escp-inp); - outp += next_escp - inp; - } - - /* escape 'next_escp' */ - Py_UNICODE_COPY(outp, escaped_chars_repl[*next_escp], delta_len); - outp += delta_len; - - inp = next_escp + 1; - } - if (inp < inp_end) - Py_UNICODE_COPY(outp, inp, PyUnicode_GET_SIZE(in) - (inp - PyUnicode_AS_UNICODE(in))); - - return (PyObject*)out; -} +static PyObject* +escape_unicode(PyUnicodeObject *in) +{ + PyUnicodeObject *out; + Py_UNICODE *inp = PyUnicode_AS_UNICODE(in); + const Py_UNICODE *inp_end = PyUnicode_AS_UNICODE(in) + PyUnicode_GET_SIZE(in); + Py_UNICODE *next_escp; + Py_UNICODE *outp; + Py_ssize_t delta=0, erepl=0, delta_len=0; + + /* First we need to figure out how long the escaped string will be */ + while (*(inp) || inp < inp_end) { + if (*inp < ESCAPED_CHARS_TABLE_SIZE) { + delta += escaped_chars_delta_len[*inp]; + erepl += !!escaped_chars_delta_len[*inp]; + } + ++inp; + } + + /* Do we need to escape anything at all? */ + if (!erepl) { + Py_INCREF(in); + return (PyObject*)in; + } + + out = (PyUnicodeObject*)PyUnicode_FromUnicode(NULL, PyUnicode_GET_SIZE(in) + delta); + if (!out) + return NULL; + + outp = PyUnicode_AS_UNICODE(out); + inp = PyUnicode_AS_UNICODE(in); + while (erepl-- > 0) { + /* look for the next substitution */ + next_escp = inp; + while (next_escp < inp_end) { + if (*next_escp < ESCAPED_CHARS_TABLE_SIZE && + (delta_len = escaped_chars_delta_len[*next_escp])) { + ++delta_len; + break; + } + ++next_escp; + } + + if (next_escp > inp) { + /* copy unescaped chars between inp and next_escp */ + Py_UNICODE_COPY(outp, inp, next_escp-inp); + outp += next_escp - inp; + } + + /* escape 'next_escp' */ + Py_UNICODE_COPY(outp, escaped_chars_repl[*next_escp], delta_len); + outp += delta_len; + + inp = next_escp + 1; + } + if (inp < inp_end) + Py_UNICODE_COPY(outp, inp, PyUnicode_GET_SIZE(in) - (inp - PyUnicode_AS_UNICODE(in))); + + return (PyObject*)out; +} #else /* PY_MAJOR_VERSION < 3 */ - + #define GET_DELTA(inp, inp_end, delta) \ while (inp < inp_end) { \ switch (*inp++) { \ @@ -127,7 +127,7 @@ escape_unicode(PyUnicodeObject *in) break; \ } \ } - + #define DO_ESCAPE(inp, inp_end, outp) \ { \ Py_ssize_t ncopy = 0; \ @@ -184,7 +184,7 @@ escape_unicode(PyUnicodeObject *in) memcpy(outp, inp-ncopy, sizeof(*outp)*ncopy); \ } -static PyObject* +static PyObject* escape_unicode_kind1(PyUnicodeObject *in) { Py_UCS1 *inp = PyUnicode_1BYTE_DATA(in); @@ -281,11 +281,11 @@ escape_unicode(PyUnicodeObject *in) #endif /* PY_MAJOR_VERSION < 3 */ static PyObject* -escape(PyObject *self, PyObject *text) -{ +escape(PyObject *self, PyObject *text) +{ static PyObject *id_html; - PyObject *s = NULL, *rv = NULL, *html; - + PyObject *s = NULL, *rv = NULL, *html; + if (id_html == NULL) { #if PY_MAJOR_VERSION < 3 id_html = PyString_InternFromString("__html__"); @@ -297,127 +297,127 @@ escape(PyObject *self, PyObject *text) } } - /* we don't have to escape integers, bools or floats */ - if (PyLong_CheckExact(text) || -#if PY_MAJOR_VERSION < 3 - PyInt_CheckExact(text) || -#endif - PyFloat_CheckExact(text) || PyBool_Check(text) || - text == Py_None) - return PyObject_CallFunctionObjArgs(markup, text, NULL); - - /* if the object has an __html__ method that performs the escaping */ + /* we don't have to escape integers, bools or floats */ + if (PyLong_CheckExact(text) || +#if PY_MAJOR_VERSION < 3 + PyInt_CheckExact(text) || +#endif + PyFloat_CheckExact(text) || PyBool_Check(text) || + text == Py_None) + return PyObject_CallFunctionObjArgs(markup, text, NULL); + + /* if the object has an __html__ method that performs the escaping */ html = PyObject_GetAttr(text ,id_html); - if (html) { + if (html) { s = PyObject_CallObject(html, NULL); - Py_DECREF(html); + Py_DECREF(html); if (s == NULL) { return NULL; } /* Convert to Markup object */ rv = PyObject_CallFunctionObjArgs(markup, (PyObject*)s, NULL); Py_DECREF(s); - return rv; - } - - /* otherwise make the object unicode if it isn't, then escape */ - PyErr_Clear(); - if (!PyUnicode_Check(text)) { -#if PY_MAJOR_VERSION < 3 - PyObject *unicode = PyObject_Unicode(text); -#else - PyObject *unicode = PyObject_Str(text); -#endif - if (!unicode) - return NULL; - s = escape_unicode((PyUnicodeObject*)unicode); - Py_DECREF(unicode); - } - else - s = escape_unicode((PyUnicodeObject*)text); - - /* convert the unicode string into a markup object. */ - rv = PyObject_CallFunctionObjArgs(markup, (PyObject*)s, NULL); - Py_DECREF(s); - return rv; -} - - -static PyObject* -escape_silent(PyObject *self, PyObject *text) -{ - if (text != Py_None) - return escape(self, text); - return PyObject_CallFunctionObjArgs(markup, NULL); -} - - -static PyObject* -soft_unicode(PyObject *self, PyObject *s) -{ - if (!PyUnicode_Check(s)) -#if PY_MAJOR_VERSION < 3 - return PyObject_Unicode(s); -#else - return PyObject_Str(s); -#endif - Py_INCREF(s); - return s; -} - - -static PyMethodDef module_methods[] = { - {"escape", (PyCFunction)escape, METH_O, - "escape(s) -> markup\n\n" - "Convert the characters &, <, >, ', and \" in string s to HTML-safe\n" - "sequences. Use this if you need to display text that might contain\n" - "such characters in HTML. Marks return value as markup string."}, - {"escape_silent", (PyCFunction)escape_silent, METH_O, - "escape_silent(s) -> markup\n\n" - "Like escape but converts None to an empty string."}, - {"soft_unicode", (PyCFunction)soft_unicode, METH_O, - "soft_unicode(object) -> string\n\n" - "Make a string unicode if it isn't already. That way a markup\n" - "string is not converted back to unicode."}, - {NULL, NULL, 0, NULL} /* Sentinel */ -}; - - -#if PY_MAJOR_VERSION < 3 - -#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ -#define PyMODINIT_FUNC void -#endif -PyMODINIT_FUNC + return rv; + } + + /* otherwise make the object unicode if it isn't, then escape */ + PyErr_Clear(); + if (!PyUnicode_Check(text)) { +#if PY_MAJOR_VERSION < 3 + PyObject *unicode = PyObject_Unicode(text); +#else + PyObject *unicode = PyObject_Str(text); +#endif + if (!unicode) + return NULL; + s = escape_unicode((PyUnicodeObject*)unicode); + Py_DECREF(unicode); + } + else + s = escape_unicode((PyUnicodeObject*)text); + + /* convert the unicode string into a markup object. */ + rv = PyObject_CallFunctionObjArgs(markup, (PyObject*)s, NULL); + Py_DECREF(s); + return rv; +} + + +static PyObject* +escape_silent(PyObject *self, PyObject *text) +{ + if (text != Py_None) + return escape(self, text); + return PyObject_CallFunctionObjArgs(markup, NULL); +} + + +static PyObject* +soft_unicode(PyObject *self, PyObject *s) +{ + if (!PyUnicode_Check(s)) +#if PY_MAJOR_VERSION < 3 + return PyObject_Unicode(s); +#else + return PyObject_Str(s); +#endif + Py_INCREF(s); + return s; +} + + +static PyMethodDef module_methods[] = { + {"escape", (PyCFunction)escape, METH_O, + "escape(s) -> markup\n\n" + "Convert the characters &, <, >, ', and \" in string s to HTML-safe\n" + "sequences. Use this if you need to display text that might contain\n" + "such characters in HTML. Marks return value as markup string."}, + {"escape_silent", (PyCFunction)escape_silent, METH_O, + "escape_silent(s) -> markup\n\n" + "Like escape but converts None to an empty string."}, + {"soft_unicode", (PyCFunction)soft_unicode, METH_O, + "soft_unicode(object) -> string\n\n" + "Make a string unicode if it isn't already. That way a markup\n" + "string is not converted back to unicode."}, + {NULL, NULL, 0, NULL} /* Sentinel */ +}; + + +#if PY_MAJOR_VERSION < 3 + +#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ +#define PyMODINIT_FUNC void +#endif +PyMODINIT_FUNC init10markupsafe9_speedups(void) -{ - if (!init_constants()) - return; - - Py_InitModule3("markupsafe._speedups", module_methods, ""); -} - -#else /* Python 3.x module initialization */ - -static struct PyModuleDef module_definition = { - PyModuleDef_HEAD_INIT, - "markupsafe._speedups", - NULL, - -1, - module_methods, - NULL, - NULL, - NULL, - NULL -}; - -PyMODINIT_FUNC +{ + if (!init_constants()) + return; + + Py_InitModule3("markupsafe._speedups", module_methods, ""); +} + +#else /* Python 3.x module initialization */ + +static struct PyModuleDef module_definition = { + PyModuleDef_HEAD_INIT, + "markupsafe._speedups", + NULL, + -1, + module_methods, + NULL, + NULL, + NULL, + NULL +}; + +PyMODINIT_FUNC PyInit_10markupsafe9_speedups(void) -{ - if (!init_constants()) - return NULL; - - return PyModule_Create(&module_definition); -} - -#endif +{ + if (!init_constants()) + return NULL; + + return PyModule_Create(&module_definition); +} + +#endif diff --git a/contrib/python/MarkupSafe/py2/ya.make b/contrib/python/MarkupSafe/py2/ya.make index c70ee05cad..0e773ee33a 100644 --- a/contrib/python/MarkupSafe/py2/ya.make +++ b/contrib/python/MarkupSafe/py2/ya.make @@ -1,19 +1,19 @@ OWNER(g:python-contrib) - + PY2_LIBRARY() - + LICENSE(BSD-3-Clause) VERSION(1.1.1) -PY_SRCS( - TOP_LEVEL - markupsafe/_compat.py - markupsafe/_constants.py - markupsafe/__init__.py - markupsafe/_native.py -) - +PY_SRCS( + TOP_LEVEL + markupsafe/_compat.py + markupsafe/_constants.py + markupsafe/__init__.py + markupsafe/_native.py +) + #SRCS( # markupsafe/_speedups.c #) @@ -32,7 +32,7 @@ RESOURCE_FILES( .dist-info/top_level.txt ) -END() +END() RECURSE_FOR_TESTS( tests |