def escape(cls, s): """Escape the string. Works like :func:`escape` with the difference that for subclasses of :class:`Markup` this function would return the correct subclass. """ rv = escape(s) if rv.__class__ is not cls: return cls(rv) return rv
def test_exception(self): # Compilation try: from package1 import syntax_error # noqa except SyntaxError: s = format_exc() self.assertRegexpMatches( s, r'File "{}/package1/syntax_error.py", line 1\n' r' one two\n' r' \^\n' r'SyntaxError: invalid syntax\n$'.format(asset_path(APP_ZIP))) else: self.fail() # Module execution try: from package1 import recursive_import_error # noqa except ImportError: s = format_exc() self.assertRegexpMatches( s, r'File "{}/package1/recursive_import_error.py", line 1, in <module>\n' r' from os import nonexistent\n' r"ImportError: cannot import name '?nonexistent'?\n$".format( asset_path(APP_ZIP))) else: self.fail() # After import complete class C(object): __html__ = None try: from markupsafe import _native _native.escape(C) except TypeError: s = format_exc() self.assertRegexpMatches( s, r'File "{}/markupsafe/_native.py", line 21, in escape\n'. format(asset_path(REQS_COMMON_ZIP)) + r' return s.__html__\(\)\n' r"TypeError: 'NoneType' object is not callable\n$") else: self.fail()
def test_exception(self): test_frame = ( fr' File "{asset_path(APP_ZIP)}/chaquopy/test/test_android.py", ' fr'line \d+, in test_exception\n' fr' .+?\n') # Source code line from this file. import_frame = r' File "import.pxi", line \d+, in java.chaquopy.import_override\n' # Compilation try: from package1 import syntax_error # noqa except SyntaxError: self.assertRegexpMatches( format_exc(), test_frame + import_frame + fr' File "{asset_path(APP_ZIP)}/package1/syntax_error.py", line 1\n' fr' one two\n' fr' \^\n' fr'SyntaxError: invalid syntax\n$') else: self.fail() # Module execution try: from package1 import recursive_import_error # noqa except ImportError: self.assertRegexpMatches( format_exc(), test_frame + import_frame + fr' File "{asset_path(APP_ZIP)}/package1/recursive_import_error.py", ' fr'line 1, in <module>\n' fr' from os import nonexistent\n' fr"ImportError: cannot import name 'nonexistent'\n$") else: self.fail() # Module execution (recursive import) try: from package1 import recursive_other_error # noqa except ValueError: self.assertRegexpMatches( format_exc(), test_frame + import_frame + fr' File "{asset_path(APP_ZIP)}/package1/recursive_other_error.py", ' fr'line 1, in <module>\n' fr' from . import other_error # noqa: F401\n' + import_frame + fr' File "{asset_path(APP_ZIP)}/package1/other_error.py", ' fr'line 1, in <module>\n' fr' int\("hello"\)\n' fr"ValueError: invalid literal for int\(\) with base 10: 'hello'\n$" ) else: self.fail() # After import complete. # Frames from pre-compiled requirements should have no source code. class C(object): __html__ = None try: from markupsafe import _native _native.escape(C) except TypeError: self.assertRegexpMatches( format_exc(), test_frame + fr' File "{asset_path(REQS_COMMON_ZIP)}/markupsafe/_native.py", ' fr'line 21, in escape\n' fr"TypeError: 'NoneType' object is not callable\n$") else: self.fail() # Frames from pre-compiled stdlib should have no source code. try: import json json.loads("hello") except json.JSONDecodeError: self.assertRegexpMatches( format_exc(), test_frame + r' File "stdlib/json/__init__.py", line 354, in loads\n' r' File "stdlib/json/decoder.py", line 339, in decode\n' r' File "stdlib/json/decoder.py", line 357, in raw_decode\n' r'json.decoder.JSONDecodeError: Expecting value: line 1 column 1 \(char 0\)\n$' ) else: self.fail()
def __radd__(self, other): if hasattr(other, '__html__') or isinstance(other, str): return self.__class__(str(escape(other)) + str(self)) return NotImplemented
def _escape_argspec(obj, iterable): """Helper for various string-wrapped functions.""" for key, value in iterable: if hasattr(value, '__html__') or isinstance(value, str): obj[key] = escape(value) return obj
def rpartition(self, sep): return tuple(map(self.__class__, str.rpartition(self, escape(sep))))