def _handle_syntaxerror(self, exception, pkg, import_str):
        """
        Handle properly an syntax error.

        Pass through the error unless silent is set to True.
        """
        if not self.silent:
            reraise(SyntaxError, SyntaxError(*exception.args),
                    sys.exc_info()[2])
Example #2
0
 def __exit__(self, exc_type, exc_value, tb):
     exception_name = self.exc_type.__name__
     if exc_type is None:
         self.test_case.fail('Expected exception of type %r' %
                             exception_name)
     elif not issubclass(exc_type, self.exc_type):
         reraise(exc_type, exc_value, tb)
     self.exc_value = exc_value
     return True
 def start_response(status, response_headers, exc_info=None):
     if exc_info:
         try:
             if headers_sent:
                 reraise(*exc_info)
         finally:
             exc_info = None
     elif headers_set:
         raise AssertionError('Headers already set')
     headers_set[:] = [status, response_headers]
     return write
Example #4
0
 def start_response(status, response_headers, exc_info=None):
     if exc_info:
         try:
             if headers_sent:
                 reraise(*exc_info)
         finally:
             exc_info = None
     elif headers_set:
         raise AssertionError('Headers already set')
     headers_set[:] = [status, response_headers]
     return write
    def _handle_syntaxerror(self, exception, pkg, import_str):
        """
        Properly handle an syntax error.

        Pass through the error unless silent is set to True.
        """
        if not self.silent:
            reraise(
                SyntaxError,
                SyntaxError(*exception.args),
                sys.exc_info()[2]
            )
    def _handle_importerror(self, exception, pkg, import_str):
        """
        Handle properly an import error.

        If a module does not exists, it's not an error, however an
        :py:exc:`ImportError` generated from importing an existing module is an
        error.
        """
        try:
            for found_module_name in find_modules(pkg):
                if found_module_name == import_str:
                    reraise(ImportError, ImportError(*exception.args),
                            sys.exc_info()[2])
        except ValueError:
            # pkg doesn't exist or is not a package
            pass
Example #7
0
def import_string(import_name, silent=False):
    """Imports an object based on a string.  This is useful if you want to
    use import paths as endpoints or something similar.  An import path can
    be specified either in dotted notation (``xml.sax.saxutils.escape``)
    or with a colon as object delimiter (``xml.sax.saxutils:escape``).

    If `silent` is True the return value will be `None` if the import fails.

    :param import_name: the dotted name for the object to import.
    :param silent: if set to `True` import errors are ignored and
                   `None` is returned instead.
    :return: imported object
    """
    # force the import name to automatically convert to strings
    # __import__ is not able to handle unicode strings in the fromlist
    # if the module is a package
    import_name = str(import_name).replace(':', '.')
    try:
        try:
            __import__(import_name)
        except ImportError:
            if '.' not in import_name:
                raise
        else:
            return sys.modules[import_name]

        module_name, obj_name = import_name.rsplit('.', 1)
        try:
            module = __import__(module_name, None, None, [obj_name])
        except ImportError:
            # support importing modules not yet set up by the parent module
            # (or package for that matter)
            module = import_string(module_name)

        try:
            return getattr(module, obj_name)
        except AttributeError as e:
            raise ImportError(e)

    except ImportError as e:
        if not silent:
            reraise(
                ImportStringError,
                ImportStringError(import_name, e),
                sys.exc_info()[2])
Example #8
0
def import_string(import_name, silent=False):
    """Imports an object based on a string.  This is useful if you want to
    use import paths as endpoints or something similar.  An import path can
    be specified either in dotted notation (``xml.sax.saxutils.escape``)
    or with a colon as object delimiter (``xml.sax.saxutils:escape``).

    If `silent` is True the return value will be `None` if the import fails.

    :param import_name: the dotted name for the object to import.
    :param silent: if set to `True` import errors are ignored and
                   `None` is returned instead.
    :return: imported object
    """
    # force the import name to automatically convert to strings
    # __import__ is not able to handle unicode strings in the fromlist
    # if the module is a package
    import_name = str(import_name).replace(':', '.')
    try:
        try:
            __import__(import_name)
        except ImportError:
            if '.' not in import_name:
                raise
        else:
            return sys.modules[import_name]

        module_name, obj_name = import_name.rsplit('.', 1)
        try:
            module = __import__(module_name, None, None, [obj_name])
        except ImportError:
            # support importing modules not yet set up by the parent module
            # (or package for that matter)
            module = import_string(module_name)

        try:
            return getattr(module, obj_name)
        except AttributeError as e:
            raise ImportError(e)

    except ImportError as e:
        if not silent:
            reraise(
                ImportStringError,
                ImportStringError(import_name, e),
                sys.exc_info()[2])
Example #9
0
def get_current_traceback(ignore_system_exceptions=False,
                          show_hidden_frames=False, skip=0):
    """Get the current exception info as `Traceback` object.  Per default
    calling this method will reraise system exceptions such as generator exit,
    system exit or others.  This behavior can be disabled by passing `False`
    to the function as first parameter.
    """
    exc_type, exc_value, tb = sys.exc_info()
    if ignore_system_exceptions and exc_type in system_exceptions:
        reraise(exc_type, exc_value, tb)
    for _ in range_type(skip):
        if tb.tb_next is None:
            break
        tb = tb.tb_next
    tb = Traceback(exc_type, exc_value, tb)
    if not show_hidden_frames:
        tb.filter_hidden_frames()
    return tb
Example #10
0
def get_current_traceback(ignore_system_exceptions=False,
                          show_hidden_frames=False, skip=0):
    """Get the current exception info as `Traceback` object.  Per default
    calling this method will reraise system exceptions such as generator exit,
    system exit or others.  This behavior can be disabled by passing `False`
    to the function as first parameter.
    """
    exc_type, exc_value, tb = sys.exc_info()
    if ignore_system_exceptions and exc_type in system_exceptions:
        reraise(exc_type, exc_value, tb)
    for _ in range_type(skip):
        if tb.tb_next is None:
            break
        tb = tb.tb_next
    tb = Traceback(exc_type, exc_value, tb)
    if not show_hidden_frames:
        tb.filter_hidden_frames()
    return tb
    def _handle_importerror(self, exception, pkg, import_str):
        """
        Properly handle an import error

        If a module does not exists, it's not an error, however an
        ImportError generated from importing an existing module is an
        error.
        """
        try:
            for found_module_name in find_modules(pkg):
                if found_module_name == import_str:
                    reraise(
                        ImportError,
                        ImportError(*exception.args),
                        sys.exc_info()[2]
                    )
        except ValueError:
            # pkg doesn't exist or is not a package
            pass
Example #12
0
def import_string(import_name, silent=False):
    """Imports an object based on a string.  This is useful if you want to
    use import paths as endpoints or something similar.  An import path can
    be specified either in dotted notation (``xml.sax.saxutils.escape``)
    or with a colon as object delimiter (``xml.sax.saxutils:escape``).

    If `silent` is True the return value will be `None` if the import fails.

    :param import_name: the dotted name for the object to import.
    :param silent: if set to `True` import errors are ignored and
                   `None` is returned instead.
    :return: imported object
    """
    #XXX: py3 review needed
    assert isinstance(import_name, string_types)
    # force the import name to automatically convert to strings
    import_name = str(import_name)
    try:
        if ':' in import_name:
            module, obj = import_name.split(':', 1)
        elif '.' in import_name:
            module, obj = import_name.rsplit('.', 1)
        else:
            return __import__(import_name)
        # __import__ is not able to handle unicode strings in the fromlist
        # if the module is a package
        if PY2 and isinstance(obj, unicode):
            obj = obj.encode('utf-8')
        try:
            return getattr(__import__(module, None, None, [obj]), obj)
        except (ImportError, AttributeError):
            # support importing modules not yet set up by the parent module
            # (or package for that matter)
            modname = module + '.' + obj
            __import__(modname)
            return sys.modules[modname]
    except ImportError as e:
        if not silent:
            reraise(
                ImportStringError,
                ImportStringError(import_name, e),
                sys.exc_info()[2])
Example #13
0
def import_string(import_name, silent=False):
    """Imports an object based on a string.  This is useful if you want to
    use import paths as endpoints or something similar.  An import path can
    be specified either in dotted notation (``xml.sax.saxutils.escape``)
    or with a colon as object delimiter (``xml.sax.saxutils:escape``).

    If `silent` is True the return value will be `None` if the import fails.

    :param import_name: the dotted name for the object to import.
    :param silent: if set to `True` import errors are ignored and
                   `None` is returned instead.
    :return: imported object
    """
    #XXX: py3 review needed
    assert isinstance(import_name, string_types)
    # force the import name to automatically convert to strings
    import_name = str(import_name)
    try:
        if ':' in import_name:
            module, obj = import_name.split(':', 1)
        elif '.' in import_name:
            module, obj = import_name.rsplit('.', 1)
        else:
            return __import__(import_name)
        # __import__ is not able to handle unicode strings in the fromlist
        # if the module is a package
        if PY2 and isinstance(obj, unicode):
            obj = obj.encode('utf-8')
        try:
            return getattr(__import__(module, None, None, [obj]), obj)
        except (ImportError, AttributeError):
            # support importing modules not yet set up by the parent module
            # (or package for that matter)
            modname = module + '.' + obj
            __import__(modname)
            return sys.modules[modname]
    except ImportError as e:
        if not silent:
            reraise(
                ImportStringError,
                ImportStringError(import_name, e),
                sys.exc_info()[2])
    def _discover_module(self, pkg):
        """
        Method to discover a single module. May be overwritten by subclasses.
        """
        import_str = pkg + '.' + self.module_name

        try:
            module = import_string(import_str, self.silent)
            self.register(module)
        except ImportError as e:  # pylint: disable=C0103
            # If a module does not exists, it's not an error, however an
            # ImportError generated from importing an existing module is an
            # error.
            try:
                for found_module_name in find_modules(pkg):
                    if found_module_name == import_str:
                        reraise(
                            ImportError,
                            ImportError(*e.args),
                            sys.exc_info()[2]
                        )
            except ValueError:
                # pkg doesn't exist or is not a package
                pass
Example #15
0
 def start_response(status, headers, exc_info=None):
     if exc_info is not None:
         reraise(*exc_info)
     response[:] = [status, headers]
     return buffer.append
Example #16
0
 def start_response(status, headers, exc_info=None):
     if exc_info is not None:
         reraise(*exc_info)
     response[:] = [status, headers]
     return buffer.append