# Buggy code
def _rank_filter(
    input,
    rank,
    size=None,
    footprint=None,
    output=None,
    mode="reflect",
    cval=0.0,
    origin=0,
    operation="rank",
):
    if (size is not None) and (footprint is not None):
        warnings.warn(
            "ignoring size because footprint is set", UserWarning, stacklevel=3
        )
    input = numpy.asarray(input)
    if numpy.iscomplexobj(input):
        raise TypeError("Complex type not supported")
    origins = _ni_support._normalize_sequence(origin, input.ndim)
    if footprint is None:
        if size is None:
            raise RuntimeError("no footprint or filter size provided")
        sizes = _ni_support._normalize_sequence(size, input.ndim)
        footprint = numpy.ones(sizes, dtype=bool)
    else:
        footprint = numpy.asarray(footprint, dtype=bool)
    fshape = [ii for ii in footprint.shape if ii > 0]
    if len(fshape) != input.ndim:
        raise RuntimeError("filter footprint array has incorrect shape.")
    for origin, lenf in zip(origins, fshape):
        if (lenf // 2 + origin < 0) or (lenf // 2 + origin >= lenf):
            raise ValueError("invalid origin")
    if not footprint.flags.contiguous:
        footprint = footprint.copy()
    filter_size = numpy.where(footprint, 1, 0).sum()
    if operation == "median":
        rank = filter_size // 2
    elif operation == "percentile":
        percentile = rank
        if percentile < 0.0:
            percentile += 100.0
        if percentile < 0 or percentile > 100:
            raise RuntimeError("invalid percentile")
        if percentile == 100.0:
            rank = filter_size - 1
        else:
            rank = int(float(filter_size) * percentile / 100.0)
    if rank < 0:
        rank += filter_size
    if rank < 0 or rank >= filter_size:
        raise RuntimeError("rank not within filter footprint size")
    if rank == 0:
        return minimum_filter(input, None, footprint, output, mode, cval, origins)
    elif rank == filter_size - 1:
        return maximum_filter(input, None, footprint, output, mode, cval, origins)
    else:
        output = _ni_support._get_output(output, input)
        mode = _ni_support._extend_mode_to_code(mode)
        _nd_image.rank_filter(input, rank, footprint, output, mode, cval, origins)
        return output


# Traceback (RuntimeError)
Traceback (most recent call last):
File "C:\Users\Sonicrs\Desktop\scipy_bug_tester.py", line 31, in <module>
mode=('constant', 'constant'))
File "C:\Users\Sonicrs\AppData\Local\Programs\Python\Python37\lib\site-packages\scipy\ndimage\filters.py", line 1097, in maximum_filter
cval, origin, 0)
File "C:\Users\Sonicrs\AppData\Local\Programs\Python\Python37\lib\site-packages\scipy\ndimage\filters.py", line 1020, in _min_or_max_filter
mode = _ni_support._extend_mode_to_code(mode)
File "C:\Users\Sonicrs\AppData\Local\Programs\Python\Python37\lib\site-packages\scipy\ndimage\_ni_support.py", line 52, in _extend_mode_to_code
raise RuntimeError('boundary mode not supported')
RuntimeError: boundary mode not supported

# Fixed code
def _rank_filter(
    input,
    rank,
    size=None,
    footprint=None,
    output=None,
    mode="reflect",
    cval=0.0,
    origin=0,
    operation="rank",
):
    if (size is not None) and (footprint is not None):
        warnings.warn(
            "ignoring size because footprint is set", UserWarning, stacklevel=3
        )
    input = numpy.asarray(input)
    if numpy.iscomplexobj(input):
        raise TypeError("Complex type not supported")
    origins = _ni_support._normalize_sequence(origin, input.ndim)
    if footprint is None:
        if size is None:
            raise RuntimeError("no footprint or filter size provided")
        sizes = _ni_support._normalize_sequence(size, input.ndim)
        footprint = numpy.ones(sizes, dtype=bool)
    else:
        footprint = numpy.asarray(footprint, dtype=bool)
    fshape = [ii for ii in footprint.shape if ii > 0]
    if len(fshape) != input.ndim:
        raise RuntimeError("filter footprint array has incorrect shape.")
    for origin, lenf in zip(origins, fshape):
        if (lenf // 2 + origin < 0) or (lenf // 2 + origin >= lenf):
            raise ValueError("invalid origin")
    if not footprint.flags.contiguous:
        footprint = footprint.copy()
    filter_size = numpy.where(footprint, 1, 0).sum()
    if operation == "median":
        rank = filter_size // 2
    elif operation == "percentile":
        percentile = rank
        if percentile < 0.0:
            percentile += 100.0
        if percentile < 0 or percentile > 100:
            raise RuntimeError("invalid percentile")
        if percentile == 100.0:
            rank = filter_size - 1
        else:
            rank = int(float(filter_size) * percentile / 100.0)
    if rank < 0:
        rank += filter_size
    if rank < 0 or rank >= filter_size:
        raise RuntimeError("rank not within filter footprint size")
    if rank == 0:
        return minimum_filter(input, None, footprint, output, mode, cval, origins)
    elif rank == filter_size - 1:
        return maximum_filter(input, None, footprint, output, mode, cval, origins)
    else:
        output = _ni_support._get_output(output, input)
        if not isinstance(mode, str) and isinstance(mode, Iterable):
            raise RuntimeError(
                "A sequence of modes is not supported by non-separable rank filters"
            )
        mode = _ni_support._extend_mode_to_code(mode)
        _nd_image.rank_filter(input, rank, footprint, output, mode, cval, origins)
        return output