# Buggy code
def __new__(subtype, filename, dtype=uint8, mode="r+", offset=0, shape=None, order="C"):
    # Import here to minimize 'import numpy' overhead
    import mmap
    import os.path

    try:
        mode = mode_equivalents[mode]
    except KeyError:
        if mode not in valid_filemodes:
            raise ValueError(
                "mode must be one of %s"
                % (valid_filemodes + list(mode_equivalents.keys()))
            )

    if mode == "w+" and shape is None:
        raise ValueError("shape must be given")

    if hasattr(filename, "read"):
        f_ctx = contextlib_nullcontext(filename)
    else:
        f_ctx = open(os_fspath(filename), ("r" if mode == "c" else mode) + "b")

    with f_ctx as fid:
        fid.seek(0, 2)
        flen = fid.tell()
        descr = dtypedescr(dtype)
        _dbytes = descr.itemsize

        if shape is None:
            bytes = flen - offset
            if bytes % _dbytes:
                raise ValueError(
                    "Size of available data is not a multiple of the data-type size."
                )
            size = bytes // _dbytes
            shape = (size,)
        else:
            if not isinstance(shape, tuple):
                shape = (shape,)
            size = np.intp(1)  # avoid default choice of np.int_, which might overflow
            for k in shape:
                size *= k

        bytes = long(offset + size * _dbytes)

        if mode == "w+" or (mode == "r+" and flen < bytes):
            fid.seek(bytes - 1, 0)
            fid.write(b"\0")
            fid.flush()

        if mode == "c":
            acc = mmap.ACCESS_COPY
        elif mode == "r":
            acc = mmap.ACCESS_READ
        else:
            acc = mmap.ACCESS_WRITE

        start = offset - offset % mmap.ALLOCATIONGRANULARITY
        bytes -= start
        array_offset = offset - start
        mm = mmap.mmap(fid.fileno(), bytes, access=acc, offset=start)

        self = ndarray.__new__(
            subtype, shape, dtype=descr, buffer=mm, offset=array_offset, order=order
        )
        self._mmap = mm
        self.offset = offset
        self.mode = mode

        if is_pathlib_path(filename):
            # special case - if we were constructed with a pathlib.path,
            # then filename is a path object, not a string
            self.filename = filename.resolve()
        elif hasattr(fid, "name") and isinstance(fid.name, basestring):
            # py3 returns int for TemporaryFile().name
            self.filename = os.path.abspath(fid.name)
        # same as memmap copies (e.g. memmap + 1)
        else:
            self.filename = None

    return self


# Traceback (OSError)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/numpy/core/memmap.py", line 250, in __new__
fid.seek(bytes - 1, 0)
OSError: [Errno 22] Invalid argument

# Fixed code
def __new__(subtype, filename, dtype=uint8, mode="r+", offset=0, shape=None, order="C"):
    # Import here to minimize 'import numpy' overhead
    import mmap
    import os.path

    try:
        mode = mode_equivalents[mode]
    except KeyError:
        if mode not in valid_filemodes:
            raise ValueError(
                "mode must be one of %s"
                % (valid_filemodes + list(mode_equivalents.keys()))
            )

    if mode == "w+" and shape is None:
        raise ValueError("shape must be given")

    if hasattr(filename, "read"):
        f_ctx = contextlib_nullcontext(filename)
    else:
        f_ctx = open(os_fspath(filename), ("r" if mode == "c" else mode) + "b")

    with f_ctx as fid:
        fid.seek(0, 2)
        flen = fid.tell()
        descr = dtypedescr(dtype)
        _dbytes = descr.itemsize

        if shape is None:
            bytes = flen - offset
            if bytes % _dbytes:
                raise ValueError(
                    "Size of available data is not a multiple of the data-type size."
                )
            size = bytes // _dbytes
            shape = (size,)
        else:
            if not isinstance(shape, tuple):
                shape = (shape,)
            size = np.intp(1)  # avoid default choice of np.int_, which might overflow
            for k in shape:
                size *= k

        bytes = long(offset + size * _dbytes)

        if mode in ("w+", "r+") and flen < bytes:
            fid.seek(bytes - 1, 0)
            fid.write(b"\0")
            fid.flush()

        if mode == "c":
            acc = mmap.ACCESS_COPY
        elif mode == "r":
            acc = mmap.ACCESS_READ
        else:
            acc = mmap.ACCESS_WRITE

        start = offset - offset % mmap.ALLOCATIONGRANULARITY
        bytes -= start
        array_offset = offset - start
        mm = mmap.mmap(fid.fileno(), bytes, access=acc, offset=start)

        self = ndarray.__new__(
            subtype, shape, dtype=descr, buffer=mm, offset=array_offset, order=order
        )
        self._mmap = mm
        self.offset = offset
        self.mode = mode

        if is_pathlib_path(filename):
            # special case - if we were constructed with a pathlib.path,
            # then filename is a path object, not a string
            self.filename = filename.resolve()
        elif hasattr(fid, "name") and isinstance(fid.name, basestring):
            # py3 returns int for TemporaryFile().name
            self.filename = os.path.abspath(fid.name)
        # same as memmap copies (e.g. memmap + 1)
        else:
            self.filename = None

    return self