<commit_before>from io import StringIO
import os
import sys
import traceback

import pytest

from tests_python.debug_constants import IS_PY36_OR_GREATER, IS_CPYTHON, TEST_CYTHON, TODO_PY311

pytestmark = pytest.mark.skipif(
    not IS_PY36_OR_GREATER or
    TODO_PY311 or
    not IS_CPYTHON or
    not TEST_CYTHON, reason='Requires CPython >= 3.6 < 3.11')


class _Tracer(object):

    def __init__(self):
        self.stream = StringIO()
        self._in_print = False
        self.accept_frame = None  # Can be set to a callable
        self.lines_executed = set()

    def tracer_printer(self, frame, event, arg):
        if self._in_print:
            return None
        self._in_print = True
        try:
            if self.accept_frame is None or self.accept_frame(frame):
                if arg is not None:
                    if event == 'exception':
                        arg = arg[0].__name__
                    elif arg is not None:
                        arg = str(arg)
                if arg is None:
                    arg = ''

                self.lines_executed.add(frame.f_lineno)

                s = ' '.join((
                    str(frame.f_lineno),
                    frame.f_code.co_name,
                    os.path.basename(frame.f_code.co_filename),
                    event.upper() if event != 'line' else event,
                    arg,
                ))
                self.writeln(s)
        except:
            traceback.print_exc()
        self._in_print = False
        return self.tracer_printer

    def writeln(self, s):
        self.write(s)
        self.write('\n')

    def write(self, s):
        if isinstance(s, bytes):
            s = s.decode('utf-8')
        self.stream.write(s)

    def call(self, c):
        sys.settrace(self.tracer_printer)
        c()
        sys.settrace(None)
        return self.stream.getvalue()


def check(filename, method, method_kwargs=None, skip_breaks_at_lines=None, method_to_change=None, stop_at_all_lines=False):
    '''
    :param filename:
    :param method:
    :param method_kwargs:
    '''
    from _pydevd_frame_eval.pydevd_modify_bytecode import _get_code_line_info
    from _pydevd_frame_eval import pydevd_modify_bytecode

    if method_to_change is None:
        method_to_change = method

    if method_kwargs is None:
        method_kwargs = {}
    if skip_breaks_at_lines is None:
        skip_breaks_at_lines = set()

    pydev_break_stops = []

    def _pydev_needs_stop_at_break(line):
        pydev_break_stops.append(line)
        return False

    tracer = _Tracer()

    def accept_frame(f):
        return filename in f.f_code.co_filename

    code = method_to_change.__code__
    code_line_info = _get_code_line_info(code)

    try:
        tracer.accept_frame = accept_frame

        def call():
            method(**method_kwargs)

        tracer.call(call)
        breakpoint_hit_at_least_once = False

        # Ok, we just ran the tracer once without any breakpoints.
        #
        # Gather its tracing profile: this will be our baseline for further tests (it should contain
        # the events and the order in which the were executed).
        #
        # Note: methods cannot have random elements when executing (otherwise
        # the order would be different and the test would be expected to fail).
        baseline = tracer.stream.getvalue()

        for line in sorted(code_line_info.line_to_offset):
            if line in skip_breaks_at_lines:
                continue
            # Now, for each valid line, add a breakpoint and check if the tracing profile is exactly
            # the same (and if the line where we added the breakpoint was executed, see if our
            # callback got called).
            success, new_code = pydevd_modify_bytecode.insert_pydevd_breaks(code, set([line]), _pydev_needs_stop_at_break=_pydev_needs_stop_at_break)
            assert success
            method_to_change.__code__ = new_code

            tracer = _Tracer()
            tracer.accept_frame = accept_frame
            tracer.call(call)
            contents = tracer.stream.getvalue()

            assert tracer.lines_executed

            if line in tracer.lines_executed:
                assert set([line]) == set(pydev_break_stops)
                breakpoint_hit_at_least_once = True
            else:
                if stop_at_all_lines:
                    raise AssertionError('Expected the debugger to stop at all lines. Did not stop at line: %s' % (line,))
            del pydev_break_stops[:]

            if baseline != contents:
                print('------- replacement at line: %s ---------' % (line,))
                print('------- baseline ---------')
                print(baseline)
                print('------- contents ---------')
                print(contents)
                print('-------- error -----------')
                assert baseline == contents

        # We must have found a break at least once!
        assert breakpoint_hit_at_least_once
    finally:
        method_to_change.__code__ = code


def test_set_pydevd_break_01():
    from tests_python.resources import _bytecode_overflow_example

    check('_bytecode_overflow_example.py', _bytecode_overflow_example.Dummy.fun, method_kwargs={'text': 'ing'})


def test_set_pydevd_break_01a():
    from tests_python.resources import _bytecode_overflow_example

    check('_bytecode_overflow_example.py', _bytecode_overflow_example.check_backtrack, method_kwargs={'x': 'f'})


def test_set_pydevd_break_02():
    from tests_python.resources import _bytecode_many_names_example
<commit_msg>Fix test_bytecode_manipulation in Python 3.10.5. debugpy:973<commit_after>from io import StringIO
import os
import sys
import traceback

import pytest

from tests_python.debug_constants import IS_PY36_OR_GREATER, IS_CPYTHON, TEST_CYTHON, TODO_PY311
import dis

pytestmark = pytest.mark.skipif(
    not IS_PY36_OR_GREATER or
    TODO_PY311 or
    not IS_CPYTHON or
    not TEST_CYTHON, reason='Requires CPython >= 3.6 < 3.11')


class _Tracer(object):

    def __init__(self):
        self.stream = StringIO()
        self._in_print = False
        self.accept_frame = None  # Can be set to a callable
        self.lines_executed = set()

    def tracer_printer(self, frame, event, arg):
        if self._in_print:
            return None
        self._in_print = True
        try:
            if self.accept_frame is None or self.accept_frame(frame):
                if arg is not None:
                    if event == 'exception':
                        arg = arg[0].__name__
                    elif arg is not None:
                        arg = str(arg)
                if arg is None:
                    arg = ''

                self.lines_executed.add(frame.f_lineno)

                s = ' '.join((
                    str(frame.f_lineno),
                    frame.f_code.co_name,
                    os.path.basename(frame.f_code.co_filename),
                    event.upper() if event != 'line' else event,
                    arg,
                ))
                self.writeln(s)
        except:
            traceback.print_exc()
        self._in_print = False
        return self.tracer_printer

    def writeln(self, s):
        self.write(s)
        self.write('\n')

    def write(self, s):
        if isinstance(s, bytes):
            s = s.decode('utf-8')
        self.stream.write(s)

    def call(self, c):
        sys.settrace(self.tracer_printer)
        c()
        sys.settrace(None)
        return self.stream.getvalue()


def check(
    filename,
    method,
    method_kwargs=None,
    skip_breaks_at_lines=None,
    method_to_change=None,
    stop_at_all_lines=False,
    has_line_event_optimized_in_original_case=False,
    ):
    '''
    :param has_line_event_optimized_in_original_case:
        If True, we're handling a case where we have a double jump, i.e.: some case
        where there's a JUMP_FORWARD which points to a JUMP_ABSOLUTE and this is
        optimized so that the JUMP_FORWARD is changed directly to a JUMP_ABSOLUTE and
        we end up skipping one line event which is supposed to be there but isn't in
        the initial case but appears when we run after modifying the bytecode in memory.

        See: https://github.com/microsoft/debugpy/issues/973#issuecomment-1178090731
    '''
    from _pydevd_frame_eval.pydevd_modify_bytecode import _get_code_line_info
    from _pydevd_frame_eval import pydevd_modify_bytecode

    if method_to_change is None:
        method_to_change = method

    if method_kwargs is None:
        method_kwargs = {}
    if skip_breaks_at_lines is None:
        skip_breaks_at_lines = set()

    pydev_break_stops = []

    def _pydev_needs_stop_at_break(line):
        pydev_break_stops.append(line)
        return False

    tracer = _Tracer()

    def accept_frame(f):
        return filename in f.f_code.co_filename

    code = method_to_change.__code__
    code_line_info = _get_code_line_info(code)

    try:
        tracer.accept_frame = accept_frame

        def call():
            method(**method_kwargs)

        tracer.call(call)
        breakpoint_hit_at_least_once = False

        # Ok, we just ran the tracer once without any breakpoints.
        #
        # Gather its tracing profile: this will be our baseline for further tests (it should contain
        # the events and the order in which the were executed).
        #
        # Note: methods cannot have random elements when executing (otherwise
        # the order would be different and the test would be expected to fail).
        baseline = tracer.stream.getvalue()

        for line in sorted(code_line_info.line_to_offset):
            if line in skip_breaks_at_lines:
                continue
            # Now, for each valid line, add a breakpoint and check if the tracing profile is exactly
            # the same (and if the line where we added the breakpoint was executed, see if our
            # callback got called).
            success, new_code = pydevd_modify_bytecode.insert_pydevd_breaks(code, set([line]), _pydev_needs_stop_at_break=_pydev_needs_stop_at_break)

            assert success
            method_to_change.__code__ = new_code

            tracer = _Tracer()
            tracer.accept_frame = accept_frame
            tracer.call(call)
            contents = tracer.stream.getvalue()

            assert tracer.lines_executed
            if has_line_event_optimized_in_original_case:
                lines = sorted(set(x[1] for x in dis.findlinestarts(new_code)))
                new_line_contents = []
                last_line = str(max(lines)) + ' '
                for l in contents.splitlines(keepends=True):
                    if not l.strip().startswith(last_line):
                        new_line_contents.append(l)
                contents = ''.join(new_line_contents)

            if line in tracer.lines_executed:
                assert set([line]) == set(pydev_break_stops)
                breakpoint_hit_at_least_once = True
            else:
                if stop_at_all_lines:
                    raise AssertionError('Expected the debugger to stop at all lines. Did not stop at line: %s' % (line,))
            del pydev_break_stops[:]

            if baseline != contents:
                print('------- replacement at line: %s ---------' % (line,))
                print('------- baseline ---------')
                print(baseline)
                print('------- contents ---------')
                print(contents)
                print('-------- error -----------')
                assert baseline == contents

        # We must have found a break at least once!
        assert breakpoint_hit_at_least_once
    finally:
        method_to_change.__code__ = code


def test_set_pydevd_break_01():
    from tests_python.resources import _bytecode_overflow_example

    check('_bytecode_overflow_example.py', _bytecode_overflow_example.Dummy.fun, method_kwargs={'text': 'ing'}, has_line_event_optimized_in_original_case=True)


def test_set_pydevd_break_01a():
    from tests_python.resources import _bytecode_overflow_example

    check('_bytecode_overflow_example.py', _bytecode_overflow_example.check_backtrack, method_kwargs={'x': 'f'})


def test_set_pydevd_break_02():
    from tests_python.resources import _bytecode_many_names_example

{'fraction_non_alphanumeric': 0.08296943231441048, 'fraction_numerical': 0.005418081837295811, 'mean_word_length': 2.05811078140455, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6877329', 'n_tokens_mistral': 3747, 'n_tokens_neox': 3545, 'n_words': 922}