def _scan_from_end(self, grpos_range, frames=None, flag_mask=0):
        if not self._s.data:
            return None

        pos = len(self._s.data) - 1

        if grpos_range > 0:
            grpos = self._s.data[pos][0]
            target_grpos = grpos - grpos_range
            while pos > 0:
                pos -= 1
                f = self._s.data[pos]
                if f[0] < target_grpos:
                    pos += 1
                    break
        elif frames > 0:
            pos = max(0, len(self._s.data) - frames)

        if flag_mask < 0:
            mask = - flag_mask
            fpos = pos
            while fpos >= 0:
                f = self._s.data[fpos]
                if f[1] & mask:
                    break
                fpos -= 1
            if fpos >= 0:
                pos = fpos
        elif flag_mask > 0:
            mask = flag_mask
            fpos = pos
            end_pos = len(self._s.data) - 1
            while fpos <= end_pos:
                f = self._s.data[fpos]
                if f[1] & mask:
                    break
                fpos += 1
            if fpos <= end_pos:
                pos = fpos

        return self._s.data_offset + pos
dataset/ETHPy150Open arkadini/twimp/twimp/server/inmemory.py/IMStream._scan_from_end/original