Ejemplo n.º 1
0
def test_schema_translation():
    pydevd_base_schema.BaseSchema.initialize_ids_translation()
    stack_trace_arguments = pydevd_schema.StackTraceArguments(threadId=1)
    stack_trace_request = pydevd_schema.StackTraceRequest(stack_trace_arguments)

    stackFrames = [
        pydevd_schema.StackFrame(id=2 ** 45, name='foo', line=1, column=1).to_dict(),
        pydevd_schema.StackFrame(id=2 ** 46, name='bar', line=1, column=1).to_dict(),
    ]
    body = pydevd_schema.StackTraceResponseBody(stackFrames)
    stack_trace_response = pydevd_base_schema.build_response(stack_trace_request, kwargs=dict(body=body))
    as_dict = stack_trace_response.to_dict(update_ids_to_dap=True)
    assert as_dict == {
        'type': 'response',
        'request_seq':-1,
        'success': True,
        'command': 'stackTrace',
        'body': {'stackFrames': [
            {'id': 1, 'name': 'foo', 'line': 1, 'column': 1, 'source': {}},
            {'id': 2, 'name': 'bar', 'line': 1, 'column': 1, 'source': {}},
        ]},
        'seq':-1}

    reconstructed = pydevd_base_schema.from_dict(as_dict, update_ids_from_dap=True)
    assert reconstructed.to_dict() == {
        'type': 'response',
        'request_seq':-1,
        'success': True,
        'command': 'stackTrace',
        'body': {'stackFrames': [
            {'id': 2 ** 45, 'name': 'foo', 'line': 1, 'column': 1, 'source': {}},
            {'id': 2 ** 46, 'name': 'bar', 'line': 1, 'column': 1, 'source': {}}
        ]},
        'seq':-1
    }
    def make_get_thread_stack_message(self, py_db, seq, thread_id, topmost_frame, fmt, must_be_suspended=False, start_frame=0, levels=0):
        frames = []
        module_events = []
        if topmost_frame is not None:
            try:
                # : :type suspended_frames_manager: SuspendedFramesManager
                suspended_frames_manager = py_db.suspended_frames_manager
                frames_list = suspended_frames_manager.get_frames_list(thread_id)
                if frames_list is None:
                    # Could not find stack of suspended frame...
                    if must_be_suspended:
                        return None
                    else:
                        frames_list = pydevd_frame_utils.create_frames_list_from_frame(topmost_frame)

                for frame_id, frame, method_name, original_filename, filename_in_utf8, lineno in self._iter_visible_frames_info(
                        py_db, frames_list
                    ):

                    try:
                        module_name = str(frame.f_globals.get('__name__', ''))
                    except:
                        module_name = '<unknown>'

                    module_events.extend(self.modules_manager.track_module(filename_in_utf8, module_name, frame))

                    presentation_hint = None
                    if not getattr(frame, 'IS_PLUGIN_FRAME', False):  # Never filter out plugin frames!
                        if py_db.is_files_filter_enabled and py_db.apply_files_filter(frame, original_filename, False):
                            continue

                        if not py_db.in_project_scope(frame):
                            presentation_hint = 'subtle'

                    formatted_name = self._format_frame_name(fmt, method_name, module_name, lineno, filename_in_utf8)
                    frames.append(pydevd_schema.StackFrame(
                        frame_id, formatted_name, lineno, column=1, source={
                            'path': filename_in_utf8,
                            'sourceReference': pydevd_file_utils.get_client_filename_source_reference(filename_in_utf8),
                        },
                        presentationHint=presentation_hint).to_dict())
            finally:
                topmost_frame = None

        for module_event in module_events:
            py_db.writer.add_command(module_event)

        total_frames = len(frames)
        stack_frames = frames
        if bool(levels):
            start = start_frame
            end = min(start + levels, total_frames)
            stack_frames = frames[start:end]

        response = pydevd_schema.StackTraceResponse(
            request_seq=seq,
            success=True,
            command='stackTrace',
            body=pydevd_schema.StackTraceResponseBody(stackFrames=stack_frames, totalFrames=total_frames))
        return NetCommand(CMD_RETURN, 0, response, is_json=True)
    def make_get_thread_stack_message(self,
                                      py_db,
                                      seq,
                                      thread_id,
                                      topmost_frame,
                                      fmt,
                                      must_be_suspended=False,
                                      start_frame=0,
                                      levels=0):
        frames = []
        module_events = []

        try:
            # : :type suspended_frames_manager: SuspendedFramesManager
            suspended_frames_manager = py_db.suspended_frames_manager
            frames_list = suspended_frames_manager.get_frames_list(thread_id)
            if frames_list is None:
                # Could not find stack of suspended frame...
                if must_be_suspended:
                    return None
                else:
                    frames_list = pydevd_frame_utils.create_frames_list_from_frame(
                        topmost_frame)

            for frame_id, frame, method_name, original_filename, filename_in_utf8, lineno, applied_mapping, show_as_current_frame in self._iter_visible_frames_info(
                    py_db, frames_list):

                try:
                    module_name = str(frame.f_globals.get('__name__', ''))
                except:
                    module_name = '<unknown>'

                module_events.extend(
                    self.modules_manager.track_module(filename_in_utf8,
                                                      module_name, frame))

                presentation_hint = None
                if not getattr(frame, 'IS_PLUGIN_FRAME',
                               False):  # Never filter out plugin frames!
                    if py_db.is_files_filter_enabled and py_db.apply_files_filter(
                            frame, original_filename, False):
                        continue

                    if not py_db.in_project_scope(frame):
                        presentation_hint = 'subtle'

                formatted_name = self._format_frame_name(
                    fmt, method_name, module_name, lineno, filename_in_utf8)
                if show_as_current_frame:
                    formatted_name += ' (Current frame)'
                source_reference = pydevd_file_utils.get_client_filename_source_reference(
                    filename_in_utf8)

                if not source_reference and not applied_mapping and not os.path.exists(
                        original_filename):
                    if getattr(frame.f_code, 'co_lnotab', None):
                        # Create a source-reference to be used where we provide the source by decompiling the code.
                        # Note: When the time comes to retrieve the source reference in this case, we'll
                        # check the linecache first (see: get_decompiled_source_from_frame_id).
                        source_reference = pydevd_file_utils.create_source_reference_for_frame_id(
                            frame_id, original_filename)
                    else:
                        # Check if someone added a source reference to the linecache (Python attrs does this).
                        if linecache.getline(original_filename, 1):
                            source_reference = pydevd_file_utils.create_source_reference_for_linecache(
                                original_filename)

                frames.append(
                    pydevd_schema.StackFrame(
                        frame_id,
                        formatted_name,
                        lineno,
                        column=1,
                        source={
                            'path': filename_in_utf8,
                            'sourceReference': source_reference,
                        },
                        presentationHint=presentation_hint).to_dict())
        finally:
            topmost_frame = None

        for module_event in module_events:
            py_db.writer.add_command(module_event)

        total_frames = len(frames)
        stack_frames = frames
        if bool(levels):
            start = start_frame
            end = min(start + levels, total_frames)
            stack_frames = frames[start:end]

        response = pydevd_schema.StackTraceResponse(
            request_seq=seq,
            success=True,
            command='stackTrace',
            body=pydevd_schema.StackTraceResponseBody(
                stackFrames=stack_frames, totalFrames=total_frames))
        return NetCommand(CMD_RETURN, 0, response, is_json=True)
Ejemplo n.º 4
0
    def make_get_thread_stack_message(self,
                                      py_db,
                                      seq,
                                      thread_id,
                                      topmost_frame,
                                      fmt,
                                      must_be_suspended=False):
        frames = []
        module_events = []
        if topmost_frame is not None:
            frame_id_to_lineno = {}
            try:
                # : :type suspended_frames_manager: SuspendedFramesManager
                suspended_frames_manager = py_db.suspended_frames_manager
                info = suspended_frames_manager.get_topmost_frame_and_frame_id_to_line(
                    thread_id)
                if info is None:
                    # Could not find stack of suspended frame...
                    if must_be_suspended:
                        return None
                else:
                    # Note: we have to use the topmost frame where it was suspended (it may
                    # be different if it was an exception).
                    topmost_frame, frame_id_to_lineno = info

                for frame_id, frame, method_name, filename_in_utf8, lineno in self._iter_visible_frames_info(
                        py_db, topmost_frame, frame_id_to_lineno):

                    module_name = frame.f_globals.get('__name__', '')

                    module_events.extend(
                        self.modules_manager.track_module(
                            filename_in_utf8, module_name, frame))

                    presentation_hint = None
                    if not getattr(frame, 'IS_PLUGIN_FRAME',
                                   False):  # Never filter out plugin frames!
                        if not py_db.in_project_scope(filename_in_utf8):
                            if py_db.get_use_libraries_filter():
                                continue
                            presentation_hint = 'subtle'

                    formatted_name = self._format_frame_name(
                        fmt, method_name, module_name, lineno,
                        filename_in_utf8)
                    frames.append(
                        pydevd_schema.StackFrame(
                            frame_id,
                            formatted_name,
                            lineno,
                            column=1,
                            source={
                                'path':
                                filename_in_utf8,
                                'sourceReference':
                                pydevd_file_utils.
                                get_client_filename_source_reference(
                                    filename_in_utf8),
                            },
                            presentationHint=presentation_hint).to_dict())
            finally:
                topmost_frame = None

        for module_event in module_events:
            py_db.writer.add_command(module_event)

        response = pydevd_schema.StackTraceResponse(
            request_seq=seq,
            success=True,
            command='stackTrace',
            body=pydevd_schema.StackTraceResponseBody(stackFrames=frames,
                                                      totalFrames=len(frames)))
        return NetCommand(CMD_RETURN, 0, response, is_json=True)