File: _debugger_case_logging.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 (63 lines) | stat: -rw-r--r-- 2,347 bytes parent folder | download | duplicates (3)
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
import subprocess
import sys
import json
from _pydev_bundle import pydev_log
import os
import io


def gen_debug_info():
    from _pydevd_bundle.pydevd_constants import DebugInfoHolder
    dct = {}
    for name in (
        'PYDEVD_DEBUG_FILE',
        'DEBUG_TRACE_LEVEL',
        ):
        dct[name] = getattr(DebugInfoHolder, name)

    return dct


if __name__ == "__main__":
    if '-print-debug' in sys.argv:
        info = gen_debug_info()  # break on 2nd process
        pydev_log.info('Something in print-debug')

        print('>>> print-debug pid: %s' % os.getpid())
        print(json.dumps(info))

    else:
        # Note: when running tests we usually have logging setup,
        # so, we create a context so that our changes are restored
        # when it finishes (as the `log_to` function will just reset
        # whatever is there).
        s = io.StringIO()
        with pydev_log.log_context(trace_level=3, stream=s):
            target_log_file = os.getenv('TARGET_LOG_FILE')

            pydev_log.log_to(target_log_file, 1)
            new_debug_info = gen_debug_info()
            subprocess_pid = None
            with subprocess.Popen(
                    [sys.executable, __file__, '-print-debug'],
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE,
                ) as process:
                subprocess_pid = process.pid
                stdout, stderr = process.communicate(input)

            output = stdout.decode('utf-8')
            pydev_log.info('Something in initial')

        log_contents = open(pydev_log._compute_filename_with_pid(target_log_file)).read()
        assert 'Something in initial' in log_contents, 'Did not find "Something in initial" in %s' % (log_contents,)

        log_contents = open(pydev_log._compute_filename_with_pid(target_log_file, pid=subprocess_pid)).read()
        assert 'Something in print-debug' in log_contents, 'Did not find "Something in print-debug" in %s' % (log_contents,)

        output = ''.join(output.splitlines(keepends=True)[1:])  # Remove the first line
        loaded_debug_info = json.loads(output)
        assert loaded_debug_info == new_debug_info, 'Expected %s. Found: %s' % (new_debug_info, loaded_debug_info)
        print('>>> Initial pid: %s' % os.getpid())
        print(output)
        print('TEST SUCEEDED')