See https://github.com/python/cpython/blob/b02774f42108aaf18eb19865472c8d5cd95b5f11/Lib/bdb.py#L319-L332

def set_trace(self, frame=None):
        """See https://github.com/python/cpython/blob/b02774f42108aaf18eb19865472c8d5cd95b5f11/Lib/bdb.py#L319-L332"""
        self.reset()

        if frame is None:
            frame = sys._getframe().f_back

        while frame:
            frame.f_trace = self.trace_dispatch
            self.botframe = frame
            frame = frame.f_back

        # Automatically proceed to next break point
        self.set_continue()

        sys.settrace(self.trace_dispatch)