File: test_pydev_monkey.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 (490 lines) | stat: -rw-r--r-- 20,024 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
# coding: utf-8
import os
import sys

import pytest

from _pydev_bundle.pydev_monkey import pydev_src_dir
from _pydevd_bundle.pydevd_constants import sorted_dict_repr
from pydevd import SetupHolder

try:
    from _pydev_bundle import pydev_monkey
except:
    sys.path.append(os.path.dirname(os.path.dirname(__file__)))
    from _pydev_bundle import pydev_monkey


@pytest.fixture(autouse=True)
def save_setup_holder():
    original = SetupHolder.setup
    yield
    SetupHolder.setup = original


def test_monkey():
    SetupHolder.setup = {"client": "127.0.0.1", "port": "0", "ppid": os.getpid(), "protocol-quoted-line": True}
    check = """C:\\bin\\python.exe -u -c connect(\\"127.0.0.1\\")"""
    debug_command = (
        "import sys; "
        "sys.path.insert(0, r'%s'); "
        "import pydevd; pydevd.config('quoted-line', ''); "
        "pydevd.settrace(host='127.0.0.1', port=0, suspend=False, "
        "trace_only_current_thread=False, patch_multiprocessing=True, access_token=None, client_access_token=None, __setup_holder__=%s); "
        ""
        'connect("127.0.0.1")'
    ) % (pydev_src_dir, sorted_dict_repr(SetupHolder.setup))
    if sys.platform == "win32":
        debug_command = debug_command.replace('"', '\\"')
        debug_command = '"%s"' % debug_command

    assert "C:\\bin\\python.exe -u -c %s" % debug_command == pydev_monkey.patch_arg_str_win(check)


def test_str_to_args_windows():
    assert ["a", "b"] == pydev_monkey.str_to_args_windows('a "b"')


def test_monkey_patch_return_original_args():
    check = ["echo", '"my"', '"args"']
    res = pydev_monkey.patch_args(check[:])
    assert res == check


def test_monkey_patch_pathlib_args():
    try:
        import pathlib
    except ImportError:
        pytest.skip("pathlib not available.")

    check = [pathlib.Path("echo"), '"my"', '"args"']
    res = pydev_monkey.patch_args(check[:])
    assert res == check


def test_monkey_patch_wrong_object_type():
    check = [1, 22, '"my"', '"args"']
    res = pydev_monkey.patch_args(check[:])
    assert res == check


def test_monkey_patch_wrong_object_type_2():
    check = ["C:\\bin\\python.exe", "-u", 1, '-qcconnect("127.0.0.1")']
    res = pydev_monkey.patch_args(check[:])
    assert res == check


def test_monkey_patch_args_module_subprocess_pathlib():
    try:
        import pathlib
    except ImportError:
        pytest.skip("pathlib not available.")

    SetupHolder.setup = {"client": "127.0.0.1", "port": "0", "multiprocess": True}
    if sys.platform == "win32":
        python_path = "C:\\bin\\python.exe"
    else:
        python_path = "/bin/python"
    check = [pathlib.Path(python_path), "-mtest", pathlib.Path("bar")]
    from _pydevd_bundle.pydevd_command_line_handling import get_pydevd_file

    assert pydev_monkey.patch_args(check) == [
        python_path,
        get_pydevd_file(),
        "--module",
        "--port",
        "0",
        "--ppid",
        str(os.getpid()),
        "--client",
        "127.0.0.1",
        "--multiprocess",
        "--protocol-quoted-line",
        "--file",
        "test",
        "bar",
    ]


def test_monkey_patch_args_indc():
    SetupHolder.setup = {"client": "127.0.0.1", "port": "0", "ppid": os.getpid(), "protocol-quoted-line": True, "skip-notify-stdin": True}
    check = ["C:\\bin\\python.exe", "-u", "-c", 'connect("127.0.0.1")']
    debug_command = (
        "import sys; sys.path.insert(0, r'%s'); import pydevd; pydevd.config('quoted-line', ''); "
        "pydevd.settrace(host='127.0.0.1', port=0, suspend=False, trace_only_current_thread=False, patch_multiprocessing=True, access_token=None, client_access_token=None, __setup_holder__=%s); "
        ""
        'connect("127.0.0.1")'
    ) % (pydev_src_dir, sorted_dict_repr(SetupHolder.setup))
    if sys.platform == "win32":
        debug_command = debug_command.replace('"', '\\"')
        debug_command = '"%s"' % debug_command
    res = pydev_monkey.patch_args(check)
    assert res == ["C:\\bin\\python.exe", "-u", "-c", debug_command]


def test_separate_future_imports():
    found = pydev_monkey._separate_future_imports("""from __future__ import print_function\nprint(1)""")
    assert found == ("from __future__ import print_function;", "\nprint(1)")

    found = pydev_monkey._separate_future_imports("""from __future__ import print_function;print(1)""")
    assert found == ("from __future__ import print_function;", "print(1)")

    found = pydev_monkey._separate_future_imports("""from __future__ import (\nprint_function);print(1)""")
    assert found == ("from __future__ import (\nprint_function);", "print(1)")

    found = pydev_monkey._separate_future_imports(""""line";from __future__ import (\n\nprint_function, absolute_imports\n);print(1)""")
    assert found == ('"line";from __future__ import (\n\nprint_function, absolute_imports\n);', "print(1)")

    found = pydev_monkey._separate_future_imports(
        """from __future__ import bar\nfrom __future__ import (\n\nprint_function, absolute_imports\n);print(1)"""
    )
    assert found == ("from __future__ import bar\nfrom __future__ import (\n\nprint_function, absolute_imports\n);", "print(1)")


def test_monkey_patch_args_indc_future_import():
    SetupHolder.setup = {"client": "127.0.0.1", "port": "0", "ppid": os.getpid(), "protocol-quoted-line": True, "skip-notify-stdin": True}
    check = ["C:\\bin\\python.exe", "-u", "-c", 'from __future__ import print_function;connect("127.0.0.1")']
    debug_command = (
        "from __future__ import print_function;import sys; sys.path.insert(0, r'%s'); import pydevd; pydevd.config('quoted-line', ''); "
        "pydevd.settrace(host='127.0.0.1', port=0, suspend=False, trace_only_current_thread=False, patch_multiprocessing=True, access_token=None, client_access_token=None, __setup_holder__=%s); "
        ""
        'connect("127.0.0.1")'
    ) % (pydev_src_dir, sorted_dict_repr(SetupHolder.setup))
    if sys.platform == "win32":
        debug_command = debug_command.replace('"', '\\"')
        debug_command = '"%s"' % debug_command
    res = pydev_monkey.patch_args(check)
    assert res == ["C:\\bin\\python.exe", "-u", "-c", debug_command]


def test_monkey_patch_args_indc_future_import2():
    SetupHolder.setup = {"client": "127.0.0.1", "port": "0", "ppid": os.getpid(), "protocol-quoted-line": True, "skip-notify-stdin": True}
    check = ["C:\\bin\\python.exe", "-u", "-c", 'from __future__ import print_function\nconnect("127.0.0.1")']
    debug_command = (
        "from __future__ import print_function;import sys; sys.path.insert(0, r'%s'); import pydevd; pydevd.config('quoted-line', ''); "
        "pydevd.settrace(host='127.0.0.1', port=0, suspend=False, trace_only_current_thread=False, patch_multiprocessing=True, access_token=None, client_access_token=None, __setup_holder__=%s); "
        ""
        '\nconnect("127.0.0.1")'
    ) % (pydev_src_dir, sorted_dict_repr(SetupHolder.setup))
    if sys.platform == "win32":
        debug_command = debug_command.replace('"', '\\"')
        debug_command = '"%s"' % debug_command
    res = pydev_monkey.patch_args(check)
    assert res == ["C:\\bin\\python.exe", "-u", "-c", debug_command]


def test_monkey_patch_args_indc2():
    SetupHolder.setup = {"client": "127.0.0.1", "port": "0", "ppid": os.getpid(), "protocol-quoted-line": True, "skip-notify-stdin": True}
    check = ["C:\\bin\\python.exe", "-u", '-qcconnect("127.0.0.1")']
    debug_command = (
        "import sys; sys.path.insert(0, r'%s'); import pydevd; pydevd.config('quoted-line', ''); "
        "pydevd.settrace(host='127.0.0.1', port=0, suspend=False, trace_only_current_thread=False, patch_multiprocessing=True, access_token=None, client_access_token=None, __setup_holder__=%s); "
        ""
        'connect("127.0.0.1")'
    ) % (pydev_src_dir, sorted_dict_repr(SetupHolder.setup))
    if sys.platform == "win32":
        debug_command = debug_command.replace('"', '\\"')
        debug_command = '"%s"' % debug_command
    res = pydev_monkey.patch_args(check)
    assert res == ["C:\\bin\\python.exe", "-u", "-qc", debug_command]


def test_monkey_patch_args_x_flag():
    SetupHolder.setup = {"client": "127.0.0.1", "port": "0", "ppid": os.getpid(), "protocol-quoted-line": True, "skip-notify-stdin": True}
    check = ["C:\\bin\\python.exe", "-X", "faulthandler", "-c", 'connect("127.0.0.1")']
    debug_command = (
        "import sys; sys.path.insert(0, r'%s'); import pydevd; pydevd.config('quoted-line', ''); "
        "pydevd.settrace(host='127.0.0.1', port=0, suspend=False, trace_only_current_thread=False, patch_multiprocessing=True, access_token=None, client_access_token=None, __setup_holder__=%s); "
        ""
        'connect("127.0.0.1")'
    ) % (pydev_src_dir, sorted_dict_repr(SetupHolder.setup))
    if sys.platform == "win32":
        debug_command = debug_command.replace('"', '\\"')
        debug_command = '"%s"' % debug_command
    res = pydev_monkey.patch_args(check)
    assert res == ["C:\\bin\\python.exe", "-X", "faulthandler", "-c", debug_command]


def test_monkey_patch_args_flag_in_single_arg_1():
    SetupHolder.setup = {"client": "127.0.0.1", "port": "0", "ppid": os.getpid(), "protocol-quoted-line": True, "skip-notify-stdin": True}
    check = ["C:\\bin\\python.exe", "-qX", "faulthandler", "-c", 'connect("127.0.0.1")']
    debug_command = (
        "import sys; sys.path.insert(0, r'%s'); import pydevd; pydevd.config('quoted-line', ''); "
        "pydevd.settrace(host='127.0.0.1', port=0, suspend=False, trace_only_current_thread=False, patch_multiprocessing=True, access_token=None, client_access_token=None, __setup_holder__=%s); "
        ""
        'connect("127.0.0.1")'
    ) % (pydev_src_dir, sorted_dict_repr(SetupHolder.setup))
    if sys.platform == "win32":
        debug_command = debug_command.replace('"', '\\"')
        debug_command = '"%s"' % debug_command
    res = pydev_monkey.patch_args(check)
    assert res == ["C:\\bin\\python.exe", "-qX", "faulthandler", "-c", debug_command]


def test_monkey_patch_args_flag_in_single_arg_2():
    SetupHolder.setup = {"client": "127.0.0.1", "port": "0", "ppid": os.getpid(), "protocol-quoted-line": True, "skip-notify-stdin": True}
    check = ["C:\\bin\\python.exe", "-qX", "faulthandler", "-c", 'connect("127.0.0.1")']
    debug_command = (
        "import sys; sys.path.insert(0, r'%s'); import pydevd; pydevd.config('quoted-line', ''); "
        "pydevd.settrace(host='127.0.0.1', port=0, suspend=False, trace_only_current_thread=False, patch_multiprocessing=True, access_token=None, client_access_token=None, __setup_holder__=%s); "
        ""
        'connect("127.0.0.1")'
    ) % (pydev_src_dir, sorted_dict_repr(SetupHolder.setup))
    if sys.platform == "win32":
        debug_command = debug_command.replace('"', '\\"')
        debug_command = '"%s"' % debug_command
    res = pydev_monkey.patch_args(check)
    assert res == ["C:\\bin\\python.exe", "-qX", "faulthandler", "-c", debug_command]


def test_monkey_patch_args_flag_in_single_arg_3():
    SetupHolder.setup = {"client": "127.0.0.1", "port": "0", "ppid": os.getpid(), "protocol-quoted-line": True, "skip-notify-stdin": True}
    check = ["C:\\bin\\python.exe", "-qc", 'connect("127.0.0.1")']
    debug_command = (
        "import sys; sys.path.insert(0, r'%s'); import pydevd; pydevd.config('quoted-line', ''); "
        "pydevd.settrace(host='127.0.0.1', port=0, suspend=False, trace_only_current_thread=False, patch_multiprocessing=True, access_token=None, client_access_token=None, __setup_holder__=%s); "
        ""
        'connect("127.0.0.1")'
    ) % (pydev_src_dir, sorted_dict_repr(SetupHolder.setup))
    if sys.platform == "win32":
        debug_command = debug_command.replace('"', '\\"')
        debug_command = '"%s"' % debug_command
    res = pydev_monkey.patch_args(check)
    assert res == ["C:\\bin\\python.exe", "-qc", debug_command]


def test_monkey_patch_args_x_flag_inline():
    SetupHolder.setup = {"client": "127.0.0.1", "port": "0", "ppid": os.getpid(), "protocol-quoted-line": True, "skip-notify-stdin": True}
    check = ["C:\\bin\\python.exe", "-Xfaulthandler", "-c", 'connect("127.0.0.1")', "arg1"]
    debug_command = (
        "import sys; sys.path.insert(0, r'%s'); import pydevd; pydevd.config('quoted-line', ''); "
        "pydevd.settrace(host='127.0.0.1', port=0, suspend=False, trace_only_current_thread=False, patch_multiprocessing=True, access_token=None, client_access_token=None, __setup_holder__=%s); "
        ""
        'connect("127.0.0.1")'
    ) % (pydev_src_dir, sorted_dict_repr(SetupHolder.setup))
    if sys.platform == "win32":
        debug_command = debug_command.replace('"', '\\"')
        debug_command = '"%s"' % debug_command
    res = pydev_monkey.patch_args(check)
    assert res == ["C:\\bin\\python.exe", "-Xfaulthandler", "-c", debug_command, "arg1"]


def test_monkey_patch_args_c_flag_inline():
    SetupHolder.setup = {"client": "127.0.0.1", "port": "0", "ppid": os.getpid(), "protocol-quoted-line": True, "skip-notify-stdin": True}
    check = ["C:\\bin\\python.exe", "-X", "faulthandler", '-cconnect("127.0.0.1")', "arg1"]
    debug_command = (
        "import sys; sys.path.insert(0, r'%s'); import pydevd; pydevd.config('quoted-line', ''); "
        "pydevd.settrace(host='127.0.0.1', port=0, suspend=False, trace_only_current_thread=False, patch_multiprocessing=True, access_token=None, client_access_token=None, __setup_holder__=%s); "
        ""
        'connect("127.0.0.1")'
    ) % (pydev_src_dir, sorted_dict_repr(SetupHolder.setup))
    if sys.platform == "win32":
        debug_command = debug_command.replace('"', '\\"')
        debug_command = '"%s"' % debug_command
    res = pydev_monkey.patch_args(check)
    assert res == ["C:\\bin\\python.exe", "-X", "faulthandler", "-c", debug_command, "arg1"]


def test_monkey_patch_args_module():
    SetupHolder.setup = {"client": "127.0.0.1", "port": "0", "multiprocess": True, "skip-notify-stdin": True}
    check = ["C:\\bin\\python.exe", "-m", "test"]
    from _pydevd_bundle.pydevd_command_line_handling import get_pydevd_file

    assert pydev_monkey.patch_args(check) == [
        "C:\\bin\\python.exe",
        get_pydevd_file(),
        "--module",
        "--port",
        "0",
        "--ppid",
        str(os.getpid()),
        "--client",
        "127.0.0.1",
        "--multiprocess",
        "--skip-notify-stdin",
        "--protocol-quoted-line",
        "--file",
        "test",
    ]


def test_monkey_patch_args_unbuffered_module():
    SetupHolder.setup = {"client": "127.0.0.1", "port": "0", "multiprocess": True, "skip-notify-stdin": True}
    check = ["C:\\bin\\python.exe", "-u", "-m", "test"]
    from _pydevd_bundle.pydevd_command_line_handling import get_pydevd_file

    assert pydev_monkey.patch_args(check) == [
        "C:\\bin\\python.exe",
        "-u",
        get_pydevd_file(),
        "--module",
        "--port",
        "0",
        "--ppid",
        str(os.getpid()),
        "--client",
        "127.0.0.1",
        "--multiprocess",
        "--skip-notify-stdin",
        "--protocol-quoted-line",
        "--file",
        "test",
    ]


def test_monkey_patch_args_module_inline():
    SetupHolder.setup = {"client": "127.0.0.1", "port": "0", "multiprocess": True, "skip-notify-stdin": True}
    check = ["C:\\bin\\python.exe", "-qOmtest"]
    from _pydevd_bundle.pydevd_command_line_handling import get_pydevd_file

    assert pydev_monkey.patch_args(check) == [
        "C:\\bin\\python.exe",
        "-qO",
        get_pydevd_file(),
        "--module",
        "--port",
        "0",
        "--ppid",
        str(os.getpid()),
        "--client",
        "127.0.0.1",
        "--multiprocess",
        "--skip-notify-stdin",
        "--protocol-quoted-line",
        "--file",
        "test",
    ]


def test_monkey_patch_args_module_inline2():
    SetupHolder.setup = {"client": "127.0.0.1", "port": "0", "multiprocess": True, "skip-notify-stdin": True}
    check = ["C:\\bin\\python.exe", "-qOm", "test"]
    from _pydevd_bundle.pydevd_command_line_handling import get_pydevd_file

    assert pydev_monkey.patch_args(check) == [
        "C:\\bin\\python.exe",
        "-qO",
        get_pydevd_file(),
        "--module",
        "--port",
        "0",
        "--ppid",
        str(os.getpid()),
        "--client",
        "127.0.0.1",
        "--multiprocess",
        "--skip-notify-stdin",
        "--protocol-quoted-line",
        "--file",
        "test",
    ]


def test_monkey_patch_args_no_indc():
    SetupHolder.setup = {"client": "127.0.0.1", "port": "0"}
    check = ["C:\\bin\\python.exe", 'connect(\\"127.0.0.1\\")', "with spaces"]
    from _pydevd_bundle.pydevd_command_line_handling import get_pydevd_file

    assert pydev_monkey.patch_args(check) == [
        "C:\\bin\\python.exe",
        get_pydevd_file(),
        "--port",
        "0",
        "--ppid",
        str(os.getpid()),
        "--client",
        "127.0.0.1",
        "--protocol-quoted-line",
        "--file",
        '"connect(\\\\\\"127.0.0.1\\\\\\")"' if sys.platform == "win32" else 'connect(\\"127.0.0.1\\")',
        '"with spaces"' if sys.platform == "win32" else "with spaces",
    ]


def test_monkey_patch_args_no_indc_with_pydevd():
    SetupHolder.setup = {"client": "127.0.0.1", "port": "0"}
    check = ["C:\\bin\\python.exe", "pydevd.py", 'connect(\\"127.0.0.1\\")', "bar"]

    assert pydev_monkey.patch_args(check) == ["C:\\bin\\python.exe", "pydevd.py", 'connect(\\"127.0.0.1\\")', "bar"]


def test_monkey_patch_args_no_indc_without_pydevd():
    from _pydevd_bundle.pydevd_command_line_handling import get_pydevd_file

    SetupHolder.setup = {"client": "127.0.0.1", "port": "0"}
    check = ["C:\\bin\\python.exe", "target.py", 'connect(\\"127.0.0.1\\")', "bar"]
    assert pydev_monkey.patch_args(check) == [
        "C:\\bin\\python.exe",
        get_pydevd_file(),
        "--port",
        "0",
        "--ppid",
        str(os.getpid()),
        "--client",
        "127.0.0.1",
        "--protocol-quoted-line",
        "--file",
        "target.py",
        '"connect(\\\\\\"127.0.0.1\\\\\\")"' if sys.platform == "win32" else 'connect(\\"127.0.0.1\\")',
        "bar",
    ]


@pytest.mark.parametrize("use_bytes", [True, False])
def test_monkey_patch_c_program_arg(use_bytes):
    from _pydevd_bundle.pydevd_command_line_handling import get_pydevd_file

    SetupHolder.setup = {"client": "127.0.0.1", "port": "0", "module": "ignore.this"}
    check = ["C:\\bin\\python.exe", "-u", "target.py", "-c", "-áéíóú"]

    encode = lambda s: s
    if use_bytes:
        check = [c.encode("utf-8") for c in check]
        encode = lambda s: s.encode("utf-8")

    assert pydev_monkey.patch_args(check) == [
        encode("C:\\bin\\python.exe"),
        encode("-u"),
        get_pydevd_file(),
        "--port",
        "0",
        "--ppid",
        str(os.getpid()),
        "--client",
        "127.0.0.1",
        "--protocol-quoted-line",
        "--file",
        encode("target.py"),
        encode("-c"),
        encode("-áéíóú"),
    ]


def test_monkey_patch_args_module_single_arg():
    SetupHolder.setup = {"client": "127.0.0.1", "port": "0", "multiprocess": True, "module": "ignore.this"}
    check = ["C:\\bin\\python.exe", "-mtest", "bar"]
    from _pydevd_bundle.pydevd_command_line_handling import get_pydevd_file

    assert pydev_monkey.patch_args(check) == [
        "C:\\bin\\python.exe",
        get_pydevd_file(),
        "--module",
        "--port",
        "0",
        "--ppid",
        str(os.getpid()),
        "--client",
        "127.0.0.1",
        "--multiprocess",
        "--protocol-quoted-line",
        "--file",
        "test",
        "bar",
    ]


def test_monkey_patch_args_stdin():
    SetupHolder.setup = {"client": "127.0.0.1", "port": "0", "multiprocess": True, "module": "ignore.this"}
    check = ["C:\\bin\\python.exe", "-Xfaulthandler", "-"]
    # i.e.: we don't deal with the stdin.
    assert pydev_monkey.patch_args(check) == check