# Path: pypi_redirect/pypi_redirect/server_app/handler/_exception.py
# class HandlerException(Exception):
#     def __init__(self, wrapped_exception):
#         self.wrapped_exception = wrapped_exception
# 
#     def raise_wrapped(self):
#         """
#         Raises the 'wrapped' exception, preserving the original
#         traceback. This must be called from within an `except` block.
#         """
#         # The first item is a class instance, so the second item must
#         # be None. The third item is the traceback object, which is why
#         # this method must be called from within an `except` block.
#         raise (
#             self.wrapped_exception(),
#             None,
#             sys.exc_info()[2])
# 
# Path: pypi_redirect/pypi_redirect/server_app/http/path_length_dispatcher.py
# class PathLengthDispatcher(object):
#     def __init__(
#             self,
#             handlers_indexed_by_path_length,
#             invalid_path_handler):
#         self.handlers_indexed_by_path_length = handlers_indexed_by_path_length
#         self.invalid_path_handler = invalid_path_handler
# 
#     @cherrypy.expose
#     def default(self, *path):
#         try:
#             return self.handlers_indexed_by_path_length[len(path)](
#                 path=path,
#                 request=cherrypy.request,
#                 response=cherrypy.response)
# 
#         except IndexError:
#             return self.invalid_path_handler(
#                 path=path,
#                 request=cherrypy.request,
#                 response=cherrypy.response)
# 
#         except HandlerException as e:
#             e.raise_wrapped()


from nose.tools import eq_
from ...server_app.handler._exception import HandlerException
from ...server_app.http.path_length_dispatcher import PathLengthDispatcher
from _utils import FunctionStub
        for n in xrange(self.number_of_handlers):
            try:
                path = ['item'] * n
                actual_result = self.dispatcher.default(*path)
                eq_(actual_result, self.normal_handlers[n].dummy_result)

            except _UniqueException:
                assert self.normal_handlers_do_throw_exceptions, \
                    'Caught unexpected exception from normal handler'

    def _assert_exception_handler_behavior(self):
        try:
            path = ['item'] * self.number_of_handlers
            actual_result = self.dispatcher.default(*path)
            eq_(actual_result, self.exception_handler.dummy_result)

        except _UniqueException:
            assert self.exception_handler_does_throw_exception, \
                'Caught unexpected exception from invalid path handler'

    def perform_assertions(self):
        self._assert_normal_handler_behavior()
        self._assert_exception_handler_behavior()


def _create_normal_sad_handlers(number_of_handlers):
    handlers = []
    for n in xrange(number_of_handlers):
        handlers.append(FunctionStub(
            name='Path length {} handler'.format(n),

# Path: pypi_redirect/pypi_redirect/server_app/handler/_exception.py
# class HandlerException(Exception):
#     def __init__(self, wrapped_exception):
#         self.wrapped_exception = wrapped_exception
# 
#     def raise_wrapped(self):
#         """
#         Raises the 'wrapped' exception, preserving the original
#         traceback. This must be called from within an `except` block.
#         """
#         # The first item is a class instance, so the second item must
#         # be None. The third item is the traceback object, which is why
#         # this method must be called from within an `except` block.
#         raise (
#             self.wrapped_exception(),
#             None,
#             sys.exc_info()[2])
# 
# Path: pypi_redirect/pypi_redirect/server_app/http/path_length_dispatcher.py
# class PathLengthDispatcher(object):
#     def __init__(
#             self,
#             handlers_indexed_by_path_length,
#             invalid_path_handler):
#         self.handlers_indexed_by_path_length = handlers_indexed_by_path_length
#         self.invalid_path_handler = invalid_path_handler
# 
#     @cherrypy.expose
#     def default(self, *path):
#         try:
#             return self.handlers_indexed_by_path_length[len(path)](
#                 path=path,
#                 request=cherrypy.request,
#                 response=cherrypy.response)
# 
#         except IndexError:
#             return self.invalid_path_handler(
#                 path=path,
#                 request=cherrypy.request,
#                 response=cherrypy.response)
# 
#         except HandlerException as e:
#             e.raise_wrapped()


# Path: pypi_redirect/pypi_redirect/test/unit_tests/path_length_dispatcher_test.py
from nose.tools import eq_
from ...server_app.handler._exception import HandlerException
from ...server_app.http.path_length_dispatcher import PathLengthDispatcher
from _utils import FunctionStub

        for n in xrange(self.number_of_handlers):
            try:
                path = ['item'] * n
                actual_result = self.dispatcher.default(*path)
                eq_(actual_result, self.normal_handlers[n].dummy_result)

            except _UniqueException:
                assert self.normal_handlers_do_throw_exceptions, \
                    'Caught unexpected exception from normal handler'

    def _assert_exception_handler_behavior(self):
        try:
            path = ['item'] * self.number_of_handlers
            actual_result = self.dispatcher.default(*path)
            eq_(actual_result, self.exception_handler.dummy_result)

        except _UniqueException:
            assert self.exception_handler_does_throw_exception, \
                'Caught unexpected exception from invalid path handler'

    def perform_assertions(self):
        self._assert_normal_handler_behavior()
        self._assert_exception_handler_behavior()


def _create_normal_sad_handlers(number_of_handlers):
    handlers = []
    for n in xrange(number_of_handlers):
        handlers.append(FunctionStub(
            name='Path length {} handler'.format(n),

            dummy_exception=HandlerException(