def __new__(mcls, name, bases, namespace):
    """
        Create either `ContextManager` itself or a subclass of it.
        
        For subclasses of `ContextManager`, if a `manage_context` method is
        available, we will use `__enter__` and `__exit__` that will use the
        generator returned by `manage_context`.
        """
    if 'manage_context' in namespace:
        from .context_manager import ContextManager
        manage_context = namespace['manage_context']
        if '__enter__' in namespace:
            raise Exception(
                'You defined both an `__enter__` method and a `manage_context` method-- That is unallowed. You need to *either* define a `manage_context` method *or* an `__enter__` and `__exit__` pair.'
                )
        if '__exit__' in namespace:
            raise Exception(
                'You defined both an `__exit__` method and a `manage_context` method-- That is unallowed. You need to *either* define a `manage_context` method *or* an `__enter__` and `__exit__` pair.'
                )
        namespace['__enter__'
            ] = ContextManager._ContextManager__enter_using_manage_context
        namespace['__exit__'
            ] = ContextManager._ContextManager__exit_using_manage_context
    result_class = super().__new__(mcls, name, bases, namespace)
    if not result_class.__is_the_base_context_manager_class(
        ) and 'manage_context' not in namespace and hasattr(result_class,
        'manage_context'):
        from .context_manager import ContextManager
        our_enter_uses_manage_context = (result_class.__enter__ ==
            ContextManager._ContextManager__enter_using_manage_context)
        our_exit_uses_manage_context = (result_class.__exit__ ==
            ContextManager._ContextManager__exit_using_manage_context)
        if our_exit_uses_manage_context and not our_enter_uses_manage_context:
            assert '__enter__' in namespace
            raise Exception(
                'The %s class defines an `__enter__` method, but not an `__exit__` method; we cannot use the `__exit__` method of its base context manager class because it uses the `manage_context` generator function.'
                 % result_class)
        if our_enter_uses_manage_context and not our_exit_uses_manage_context:
            assert '__exit__' in namespace
            raise Exception(
                'The %s class defines an `__exit__` method, but not an `__enter__` method; we cannot use the `__enter__` method of its base context manager class because it uses the `manage_context` generator function.'
                 % result_class)
    return result_class
