File: test_collect_bytecode_info.py

package info (click to toggle)
pydevd 3.3.0%2Bds-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 13,892 kB
  • sloc: python: 77,508; cpp: 1,869; sh: 368; makefile: 50; ansic: 4
file content (700 lines) | stat: -rw-r--r-- 21,088 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
# coding: utf-8
from io import StringIO
import os.path
import sys
import traceback

from _pydevd_bundle.pydevd_collect_bytecode_info import collect_try_except_info, \
    collect_return_info, code_to_bytecode_representation
from tests_python.debugger_unittest import IS_CPYTHON, IS_PYPY
from _pydevd_bundle.pydevd_constants import IS_PY38_OR_GREATER, IS_JYTHON
from tests_python.debug_constants import IS_PY311_OR_GREATER, TODO_PYPY

def _method_call_with_error():
    try:
        _method_reraise()
    except:
        raise


def _method_except_local():
    Foo = AssertionError
    try:  # SETUP_EXCEPT (to except line)
        raise AssertionError()
    except Foo as exc:
        # DUP_TOP, LOAD_FAST (x), COMPARE_OP (exception match), POP_JUMP_IF_FALSE
        pass


def _method_reraise():
    try:  # SETUP_EXCEPT (to except line)
        raise AssertionError()
    except AssertionError as e:  # POP_TOP
        raise e


def _method_return_with_error():
    _method_call_with_error()


def _method_return_with_error2():
    try:
        _method_call_with_error()
    except:
        return


def _method_simple_raise_any_except():
    try:  # SETUP_EXCEPT (to except line)
        raise AssertionError()
    except:  # POP_TOP
        pass


def _method_simple_raise_any_except_return_on_raise():
    # Note how the tracing the debugger has is equal to the tracing from _method_simple_raise_any_except
    # but this one resulted in an unhandled exception while the other one didn't.
    try:  # SETUP_EXCEPT (to except line)
        raise AssertionError()
    except:  # POP_TOP
        raise  # RAISE_VARARGS


def _method_simple_raise_local_load():
    x = AssertionError
    try:  # SETUP_EXCEPT (to except line)
        raise AssertionError()
    except x as exc:
        # DUP_TOP, LOAD_GLOBAL (NameError), LOAD_GLOBAL(AssertionError), BUILD_TUPLE,
        # COMPARE_OP (exception match), POP_JUMP_IF_FALSE
        pass


def _method_simple_raise_multi_except():
    try:  # SETUP_EXCEPT (to except line)
        raise AssertionError()
    except (NameError, AssertionError) as exc:
        # DUP_TOP, LOAD_FAST (x), COMPARE_OP (exception match), POP_JUMP_IF_FALSE
        pass


def _method_simple_raise_unmatched_except():
    try:  # SETUP_EXCEPT (to except line)
        raise AssertionError()
    except NameError:  # DUP_TOP, LOAD_GLOBAL (NameError), COMPARE_OP (exception match), POP_JUMP_IF_FALSE
        pass


class _Tracer(object):

    def __init__(self, partial_info=False):
        self.partial_info = partial_info
        self.stream = StringIO()
        self._in_print = False

    def tracer_printer(self, frame, event, arg):
        if self._in_print:
            return None
        self._in_print = True
        try:
            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 = ''

            if self.partial_info:
                s = ' '.join((
                    os.path.basename(frame.f_code.co_filename),
                    event.upper() if event != 'line' else event,
                    arg,
                ))
            else:
                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)
        try:
            c()
        except:
            pass
        sys.settrace(None)
        return self.stream.getvalue()


import pytest


class _ExcVerifier(object):

    def __init__(self, pyfile):
        self.pyfile = pyfile

    def check(self, method, expected_as_str, expected_as_str_source_version=None, update_try_except_infos=None):
        code = method.__code__

        try_except_infos = sorted(collect_try_except_info(code, use_func_first_line=True), key=lambda t:t.try_line)
        if IS_CPYTHON or IS_PYPY:
            if update_try_except_infos is not None:
                update_try_except_infos(try_except_infos)

            if sys.version_info[:2] not in ((3, 10), (3, 11), (3, 12), (3, 13)):
                assert str(try_except_infos) == expected_as_str
            from _pydevd_bundle.pydevd_collect_bytecode_info import collect_try_except_info_from_source

            expected_as_str_source_version = expected_as_str_source_version or expected_as_str
            try_except_infos = collect_try_except_info_from_source(self.pyfile(method))
            if update_try_except_infos is not None:
                update_try_except_infos(try_except_infos)
            assert str(try_except_infos) == expected_as_str_source_version
        else:
            assert try_except_infos == []


@pytest.fixture
def exc_verifier(pyfile):
    return _ExcVerifier(pyfile)


@pytest.mark.skipif(not IS_CPYTHON, reason='CPython only test.')
def test_collect_try_except_info(data_regression, pyfile):
    from _pydevd_bundle.pydevd_collect_bytecode_info import collect_try_except_info_from_source
    method_to_info = {}
    method_to_info_from_source = {}
    for key, method in sorted(dict(globals()).items()):
        if key.startswith('_method'):

            info = collect_try_except_info_from_source(pyfile(method))
            method_to_info_from_source[key] = sorted(str(x) for x in info)

            info = collect_try_except_info(method.__code__, use_func_first_line=True)
            method_to_info[key] = sorted(str(x) for x in info)

    if sys.version_info[:2] not in ((3, 10), (3, 11), (3, 12), (3, 13)):
        data_regression.check(method_to_info)

    data_regression.check(method_to_info_from_source)


def test_collect_try_except_info2(exc_verifier):

    def method():
        try:
            raise AssertionError
        except:
            _a = 10
            raise
        finally:
            _b = 20
        _c = 20

    exc_verifier.check(method, '[{try:1 except 3 end block 5 raises: 5}]')


def test_collect_try_except_info3(exc_verifier):

    def method():
        get_exc_class = lambda:AssertionError
        try:  # SETUP_EXCEPT (to except line)
            raise AssertionError()
        except get_exc_class() \
                as e:  # POP_TOP
            raise e

    exc_verifier.check(method, '[{try:2 except 4 end block 6}]')


def test_collect_try_except_info4(exc_verifier):

    def method():
        for i in range(2):
            try:
                raise AssertionError()
            except AssertionError:
                if i == 1:
                    try:
                        raise
                    except:
                        pass

        _foo = 10

    exc_verifier.check(
        method,
        '[{try:2 except 4 end block 9 raises: 7}, {try:6 except 8 end block 9 raises: 7}]',
        '[{try:2 except 4 end block 9 raises: 7}, {try:6 except 8 end block 9}]',
    )


def test_collect_try_except_info4a(exc_verifier):

    def method():
        for i in range(2):
            try:
                raise AssertionError()
            except:
                if i == 1:
                    try:
                        raise
                    except:
                        pass

        _foo = 10

    exc_verifier.check(method,
        '[{try:2 except 4 end block 9 raises: 7}, {try:6 except 8 end block 9 raises: 7}]',
        '[{try:2 except 4 end block 9 raises: 7}, {try:6 except 8 end block 9}]',
    )


def test_collect_try_except_info_raise_unhandled7(exc_verifier):

    def raise_unhandled7():
        try:
            raise AssertionError()
        except AssertionError:
            try:
                raise AssertionError()
            except RuntimeError:
                pass

    exc_verifier.check(raise_unhandled7, '[{try:1 except 3 end block 7}, {try:4 except 6 end block 7}]')


def test_collect_try_except_info_raise_unhandled10(exc_verifier):

    def raise_unhandled10():
        for i in range(2):
            try:
                raise AssertionError()
            except AssertionError:
                if i == 1:
                    try:
                        raise
                    except RuntimeError:
                        pass

    exc_verifier.check(
        raise_unhandled10,
        '[{try:2 except 4 end block 9 raises: 7}, {try:6 except 8 end block 9 raises: 7}]',
        '[{try:2 except 4 end block 9 raises: 7}, {try:6 except 8 end block 9}]',
    )


def test_collect_try_except_info_return_on_except(exc_verifier):

    def method():
        try:  # SETUP_EXCEPT (to except line)
            try:  # SETUP_EXCEPT (to except line)
                raise AssertionError()
            except:  # POP_TOP
                raise
        except:  # POP_TOP
            return (
                1,
                2
            )

    def update_try_except_infos(try_except_infos):
        for try_except_info in try_except_infos:
            # On 3.7/3.8 the last bytecode actually has a different start line.
            if try_except_info.except_end_line in (7, 8):
                try_except_info.except_end_line = 9

    try_except_info_for_source = '[{try:1 except 6 end block 10}, {try:2 except 4 end block 5 raises: 5}]'
    if sys.version_info[:2] <= (3, 7):
        # The ast doesn't have end_lineno, so, the end block must be calculated based on children lineno (and thus is a bit different).
        try_except_info_for_source = '[{try:1 except 6 end block 9}, {try:2 except 4 end block 5 raises: 5}]'

    exc_verifier.check(
        method,
        '[{try:1 except 6 end block 9 raises: 5}, {try:2 except 4 end block 5 raises: 5}]',
        try_except_info_for_source,
        update_try_except_infos=update_try_except_infos
    )


def test_collect_try_except_info_with(exc_verifier):

    def try_except_with():
        try:
            with object():
                pass
        except AssertionError:
            pass

    exc_verifier.check(try_except_with, '[{try:1 except 4 end block 5}]')


def test_collect_try_except_info_in_single_line_1(exc_verifier):

    def try_except_single_line():
        try:range()
        except:
            return False
        return True

    exc_verifier.check(try_except_single_line, '[{try:1 except 2 end block 3}]')


def test_collect_try_except_info_in_single_line_2(exc_verifier):

    def try_except_single_line():
        try:range()
        except: return False
        return True

    exc_verifier.check(try_except_single_line, '[{try:1 except 2 end block 2}]')


def test_collect_try_except_info_multiple_except(exc_verifier):

    def try_except_with():
        try:
            pass
        except AssertionError:
            a = 1
        except RuntimeError:
            a = 2
        except:
            a = 3

    exc_verifier.check(try_except_with, '[{try:1 except 3 end block 8}]')


def test_collect_try_except_info_async_for():
    if IS_PY311_OR_GREATER:
        pytest.skip('On Python 3.11 we just support collecting info from the AST.')

    if TODO_PYPY:
        pytest.skip('Not ok for pypy')

    # Not valid on Python 2.
    code_str = '''
async def try_except_with():
    try:
        async for a in object():
            b = 10
        else:
            b = 20
    except AssertionError:
        pass
'''

    namespace = {}
    exec(code_str, namespace, namespace)
    code = namespace['try_except_with'].__code__

    lst = sorted(collect_try_except_info(code, use_func_first_line=True), key=lambda t:t.try_line)
    if IS_CPYTHON or IS_PYPY:
        if IS_PY38_OR_GREATER:
            assert str(lst) == '[{try:1 except 6 end block 7}]'
        else:
            # Before Python 3.8 the async for does a try..except StopAsyncIteration internally.
            assert str(lst) in (
                '[{try:1 except 6 end block 7}, {try:2 except 2 end block 7}]',
                '[{try:1 except 6 end block 7}, {try:2 except 2 end block 2}]'
            )

        # The version from the contents should always be correct.
        from _pydevd_bundle.pydevd_collect_bytecode_info import collect_try_except_info_from_contents
        assert str(collect_try_except_info_from_contents(code_str)) == '[{try:3 except 8 end block 9}]'

    else:
        assert lst == []


@pytest.mark.skipif(IS_JYTHON, reason='Jython does not have bytecode support.')
def test_collect_return_info():

    def method():
        return 1

    assert str(collect_return_info(method.__code__, use_func_first_line=True)) == '[{return: 1}]'

    def method2():
        pass

    assert str(collect_return_info(method2.__code__, use_func_first_line=True)) == '[{return: 1}]'

    def method3():
        yield 1
        yield 2

    assert str(collect_return_info(method3.__code__, use_func_first_line=True)) == '[{return: 2}]'

    def method4():
        return (1,
                2,
                3,
                4)

    assert str(collect_return_info(method4.__code__, use_func_first_line=True)) == \
        '[{return: 1}]' if IS_PY38_OR_GREATER else '[{return: 4}]'

    def method5():
        return \
            \
            1

    assert str(collect_return_info(method5.__code__, use_func_first_line=True)) == \
        '[{return: 1}]' if IS_PY38_OR_GREATER else '[{return: 3}]'

    code = '''
def method():
    if a:
        yield 1
        yield 2
        return 1
    else:
        pass
'''

    scope = {}
    exec(code, scope)
    assert str(collect_return_info(scope['method'].__code__, use_func_first_line=True)) == \
        '[{return: 4}, {return: 6}]'


@pytest.mark.skipif(IS_JYTHON, reason='Jython does not have bytecode support.')
def test_simple_code_to_bytecode_repr():

    def method4():
        return (1,
                2,
                3,
                call('tnh %s' % 1))

    contents = code_to_bytecode_representation(method4.__code__, use_func_first_line=True)
    assert contents.count('\n') == 4, 'Found:%s' % (contents,)


@pytest.mark.skipif(IS_JYTHON, reason='Jython does not have bytecode support.')
def test_simple_code_to_bytecode_repr_many():

    def method4():
        a = call()
        if a == 20:
            [x for x in call()]

        def method2():
            for x in y:
                yield x
            raise AssertionError

        return (1,
                2,
                3,
                call('tnh 1' % 1))

    new_repr = code_to_bytecode_representation(method4.__code__, use_func_first_line=True)
    # print(new_repr)


@pytest.mark.skipif(IS_JYTHON, reason='Jython does not have bytecode support.')
def test_simple_code_to_bytecode_repr2():

    def method():
        print(10)

        def method4(a, b):
            return (1,
                    2,
                    3,
                    call('somestr %s' % 1))

        print(20)

    s = code_to_bytecode_representation(method.__code__, use_func_first_line=True)
    assert s.count('\n') == 9, 'Expected 9 lines. Found: %s in:>>\n%s\n<<' % (s.count('\n'), s)
    assert 'somestr' in s  # i.e.: the contents of the inner code have been added too


@pytest.mark.skipif(IS_JYTHON, reason='Jython does not have bytecode support.')
def test_simple_code_to_bytecode_repr_simple_method_calls():

    def method4():
        call()
        a = 10
        call(1, 2, 3, a, b, "x")

    new_repr = code_to_bytecode_representation(method4.__code__, use_func_first_line=True)
    assert 'call()' in new_repr
    assert 'call(1, 2, 3, a, b, \'x\')' in new_repr
    assert 'NULL' not in new_repr


@pytest.mark.skipif(IS_JYTHON, reason='Jython does not have bytecode support.')
def test_simple_code_to_bytecode_repr_assign():

    def method4():
        a = call()
        return call()

    new_repr = code_to_bytecode_representation(method4.__code__, use_func_first_line=True)
    assert 'a = call()' in new_repr
    assert 'return call()' in new_repr


@pytest.mark.skipif(IS_JYTHON, reason='Jython does not have bytecode support.')
def test_simple_code_to_bytecode_repr_tuple():

    def method4():
        return (1, 2, call(3, 4))

    new_repr = code_to_bytecode_representation(method4.__code__, use_func_first_line=True)
    assert 'return (1, 2, call(3, 4))' in new_repr


@pytest.mark.skipif(IS_JYTHON, reason='Jython does not have bytecode support.')
def test_build_tuple():

    def method4():
        return call(1, (call2(), 2))

    new_repr = code_to_bytecode_representation(method4.__code__, use_func_first_line=True)
    assert 'return call(1, (call2(), 2))' in new_repr


@pytest.mark.skipif(IS_JYTHON, reason='Jython does not have bytecode support.')
def test_simple_code_to_bytecode_repr_return_tuple():

    def method4():
        return (1, 2, 3, a)

    new_repr = code_to_bytecode_representation(method4.__code__, use_func_first_line=True)
    assert 'return (1, 2, 3, a)' in new_repr


@pytest.mark.skipif(IS_JYTHON, reason='Jython does not have bytecode support.')
def test_simple_code_to_bytecode_repr_return_tuple_with_call():

    def method4():
        return (1, 2, 3, a())

    new_repr = code_to_bytecode_representation(method4.__code__, use_func_first_line=True)
    assert 'return (1, 2, 3, a())' in new_repr


@pytest.mark.skipif(IS_JYTHON, reason='Jython does not have bytecode support.')
def test_simple_code_to_bytecode_repr_attr():

    def method4():
        call(a.b.c)

    new_repr = code_to_bytecode_representation(method4.__code__, use_func_first_line=True)
    assert 'call(a.b.c)' in new_repr


@pytest.mark.skipif(IS_JYTHON, reason='Jython does not have bytecode support.')
def test_simple_code_to_bytecode_cls_method():

    def method4():

        class B:

            def method(self):
                self.a.b.c

    new_repr = code_to_bytecode_representation(method4.__code__, use_func_first_line=True)
    assert 'self.a.b.c' in new_repr


@pytest.mark.skipif(IS_JYTHON, reason='Jython does not have bytecode support.')
def test_simple_code_to_bytecode_repr_unicode():

    def method4():
        return 'áéíóú'

    new_repr = code_to_bytecode_representation(method4.__code__, use_func_first_line=True)
    assert repr('áéíóú') in new_repr


def _create_entry(instruction):
    argval = instruction.argval
    return dict(
        opname=instruction.opname,
        argval=argval,
        starts_line=instruction.starts_line,
        is_jump_target=instruction.is_jump_target,
    )


def debug_test_iter_bytecode(data_regression):
    # Note: not run by default, only to help visualizing bytecode and comparing differences among versions.
    import dis

    basename = 'test_iter_bytecode.py%s%s' % (sys.version_info[:2])
    method_to_info = {}
    for key, method in sorted(dict(globals()).items()):
        if key.startswith('_method'):
            info = []

            if sys.version_info[0] < 3:
                from _pydevd_bundle.pydevd_collect_bytecode_info import _iter_as_bytecode_as_instructions_py2
                iter_in = _iter_as_bytecode_as_instructions_py2(method.__code__)
            else:
                iter_in = dis.Bytecode(method)

            for instruction in iter_in:
                info.append(_create_entry(instruction))

            msg = []
            for d in info:
                line = []
                for k, v in sorted(d.items()):
                    line.append(u'%s=%s' % (k, v))
                msg.append(u'   '.join(line))
            if isinstance(key, bytes):
                key = key.decode('utf-8')
            method_to_info[key] = msg

    data_regression.check(method_to_info, basename=basename)


def debug_test_tracing_output():  # Note: not run by default, only to debug tracing.
    from collections import defaultdict
    output_to_method_names = defaultdict(list)
    for key, val in sorted(dict(globals()).items()):
        if key.startswith('_method'):
            tracer = _Tracer(partial_info=False)
            output_to_method_names[tracer.call(val)].append(val.__name__)

    # Notes:
    #
    # Seen as the same by the tracing (so, we inspect the bytecode to disambiguate).
    #     _method_simple_raise_any_except
    #     _method_simple_raise_any_except_return_on_raise
    #     _method_simple_raise_multi_except
    #
    # The return with an exception is always None
    #
    # It's not possible to disambiguate from a return None, pass or raise just with the tracing
    # (only a raise with an exception is gotten by the debugger).
    for output, method_names in sorted(output_to_method_names.items(), key=lambda x:(-len(x[1]), ''.join(x[1]))):
        print('=' * 80)
        print('    %s ' % (', '.join(method_names),))
        print('=' * 80)
        print(output)