robotcode-repl 2.9.0.dev197__py3-none-any.whl → 2.9.0.dev230__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- robotcode/repl/__version__.py +1 -1
- robotcode/repl/_debug/__init__.py +0 -2
- robotcode/repl/_debug/controller.py +0 -13
- robotcode/repl/_debug/types.py +0 -9
- robotcode/repl/_session.py +211 -311
- robotcode/repl/_windows_job.py +166 -0
- robotcode/repl/cli.py +16 -22
- robotcode/repl/doc_cli.py +3 -0
- robotcode/repl/session_cli.py +77 -97
- {robotcode_repl-2.9.0.dev197.dist-info → robotcode_repl-2.9.0.dev230.dist-info}/METADATA +2 -2
- {robotcode_repl-2.9.0.dev197.dist-info → robotcode_repl-2.9.0.dev230.dist-info}/RECORD +13 -12
- {robotcode_repl-2.9.0.dev197.dist-info → robotcode_repl-2.9.0.dev230.dist-info}/WHEEL +0 -0
- {robotcode_repl-2.9.0.dev197.dist-info → robotcode_repl-2.9.0.dev230.dist-info}/entry_points.txt +0 -0
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"""Windows job objects, by which `session start` ends a session process together with the processes it has started, as
|
|
2
|
+
the process group does on Linux and macOS."""
|
|
3
|
+
|
|
4
|
+
import _winapi
|
|
5
|
+
import ctypes
|
|
6
|
+
import sys
|
|
7
|
+
from ctypes import wintypes
|
|
8
|
+
|
|
9
|
+
assert sys.platform == "win32"
|
|
10
|
+
|
|
11
|
+
CREATE_SUSPENDED = 0x00000004
|
|
12
|
+
|
|
13
|
+
_JOB_OBJECT_EXTENDED_LIMIT_INFORMATION = 9
|
|
14
|
+
_JOB_OBJECT_LIMIT_BREAKAWAY_OK = 0x00000800
|
|
15
|
+
_JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE = 0x00002000
|
|
16
|
+
_PROCESS_SET_QUOTA = 0x0100
|
|
17
|
+
_PROCESS_TERMINATE = 0x0001
|
|
18
|
+
_TH32CS_SNAPTHREAD = 0x00000004
|
|
19
|
+
_THREAD_SUSPEND_RESUME = 0x0002
|
|
20
|
+
_INVALID_HANDLE_VALUE = wintypes.HANDLE(-1).value
|
|
21
|
+
_RESUME_FAILED = 0xFFFFFFFF
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class _BasicLimitInformation(ctypes.Structure):
|
|
25
|
+
_fields_ = [
|
|
26
|
+
("PerProcessUserTimeLimit", wintypes.LARGE_INTEGER),
|
|
27
|
+
("PerJobUserTimeLimit", wintypes.LARGE_INTEGER),
|
|
28
|
+
("LimitFlags", wintypes.DWORD),
|
|
29
|
+
("MinimumWorkingSetSize", ctypes.c_size_t),
|
|
30
|
+
("MaximumWorkingSetSize", ctypes.c_size_t),
|
|
31
|
+
("ActiveProcessLimit", wintypes.DWORD),
|
|
32
|
+
("Affinity", ctypes.c_size_t),
|
|
33
|
+
("PriorityClass", wintypes.DWORD),
|
|
34
|
+
("SchedulingClass", wintypes.DWORD),
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class _IoCounters(ctypes.Structure):
|
|
39
|
+
_fields_ = [
|
|
40
|
+
("ReadOperationCount", ctypes.c_ulonglong),
|
|
41
|
+
("WriteOperationCount", ctypes.c_ulonglong),
|
|
42
|
+
("OtherOperationCount", ctypes.c_ulonglong),
|
|
43
|
+
("ReadTransferCount", ctypes.c_ulonglong),
|
|
44
|
+
("WriteTransferCount", ctypes.c_ulonglong),
|
|
45
|
+
("OtherTransferCount", ctypes.c_ulonglong),
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class _ExtendedLimitInformation(ctypes.Structure):
|
|
50
|
+
_fields_ = [
|
|
51
|
+
("BasicLimitInformation", _BasicLimitInformation),
|
|
52
|
+
("IoInfo", _IoCounters),
|
|
53
|
+
("ProcessMemoryLimit", ctypes.c_size_t),
|
|
54
|
+
("JobMemoryLimit", ctypes.c_size_t),
|
|
55
|
+
("PeakProcessMemoryUsed", ctypes.c_size_t),
|
|
56
|
+
("PeakJobMemoryUsed", ctypes.c_size_t),
|
|
57
|
+
]
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class _ThreadEntry(ctypes.Structure):
|
|
61
|
+
_fields_ = [
|
|
62
|
+
("dwSize", wintypes.DWORD),
|
|
63
|
+
("cntUsage", wintypes.DWORD),
|
|
64
|
+
("th32ThreadID", wintypes.DWORD),
|
|
65
|
+
("th32OwnerProcessID", wintypes.DWORD),
|
|
66
|
+
("tpBasePri", wintypes.LONG),
|
|
67
|
+
("tpDeltaPri", wintypes.LONG),
|
|
68
|
+
("dwFlags", wintypes.DWORD),
|
|
69
|
+
]
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
_kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
|
|
73
|
+
_kernel32.CreateJobObjectW.argtypes = (wintypes.LPVOID, wintypes.LPCWSTR)
|
|
74
|
+
_kernel32.CreateJobObjectW.restype = wintypes.HANDLE
|
|
75
|
+
_kernel32.SetInformationJobObject.argtypes = (wintypes.HANDLE, ctypes.c_int, wintypes.LPVOID, wintypes.DWORD)
|
|
76
|
+
_kernel32.SetInformationJobObject.restype = wintypes.BOOL
|
|
77
|
+
_kernel32.AssignProcessToJobObject.argtypes = (wintypes.HANDLE, wintypes.HANDLE)
|
|
78
|
+
_kernel32.AssignProcessToJobObject.restype = wintypes.BOOL
|
|
79
|
+
_kernel32.TerminateJobObject.argtypes = (wintypes.HANDLE, wintypes.UINT)
|
|
80
|
+
_kernel32.TerminateJobObject.restype = wintypes.BOOL
|
|
81
|
+
_kernel32.CreateToolhelp32Snapshot.argtypes = (wintypes.DWORD, wintypes.DWORD)
|
|
82
|
+
_kernel32.CreateToolhelp32Snapshot.restype = wintypes.HANDLE
|
|
83
|
+
_kernel32.Thread32First.argtypes = (wintypes.HANDLE, ctypes.POINTER(_ThreadEntry))
|
|
84
|
+
_kernel32.Thread32First.restype = wintypes.BOOL
|
|
85
|
+
_kernel32.Thread32Next.argtypes = (wintypes.HANDLE, ctypes.POINTER(_ThreadEntry))
|
|
86
|
+
_kernel32.Thread32Next.restype = wintypes.BOOL
|
|
87
|
+
_kernel32.OpenThread.argtypes = (wintypes.DWORD, wintypes.BOOL, wintypes.DWORD)
|
|
88
|
+
_kernel32.OpenThread.restype = wintypes.HANDLE
|
|
89
|
+
_kernel32.ResumeThread.argtypes = (wintypes.HANDLE,)
|
|
90
|
+
_kernel32.ResumeThread.restype = wintypes.DWORD
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def _error() -> OSError:
|
|
94
|
+
return ctypes.WinError(ctypes.get_last_error())
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _set_limits(job: int, flags: int) -> None:
|
|
98
|
+
info = _ExtendedLimitInformation()
|
|
99
|
+
info.BasicLimitInformation.LimitFlags = flags
|
|
100
|
+
if not _kernel32.SetInformationJobObject(
|
|
101
|
+
job, _JOB_OBJECT_EXTENDED_LIMIT_INFORMATION, ctypes.byref(info), ctypes.sizeof(info)
|
|
102
|
+
):
|
|
103
|
+
raise _error()
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def job_with(pid: int) -> int:
|
|
107
|
+
"""A new job with the process `pid` in it, and every process it starts from then on. The processes of the job may
|
|
108
|
+
start processes outside of it, as some programs ask for; a job that forbids it makes their `CreateProcess` fail.
|
|
109
|
+
|
|
110
|
+
Until `release`, Windows ends the processes of the job once its handle is closed, also when this process ends in
|
|
111
|
+
whatever way."""
|
|
112
|
+
job = _kernel32.CreateJobObjectW(None, None)
|
|
113
|
+
if not job:
|
|
114
|
+
raise _error()
|
|
115
|
+
try:
|
|
116
|
+
_set_limits(job, _JOB_OBJECT_LIMIT_BREAKAWAY_OK | _JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE)
|
|
117
|
+
process = _winapi.OpenProcess(_PROCESS_SET_QUOTA | _PROCESS_TERMINATE, False, pid)
|
|
118
|
+
try:
|
|
119
|
+
if not _kernel32.AssignProcessToJobObject(job, process):
|
|
120
|
+
raise _error()
|
|
121
|
+
finally:
|
|
122
|
+
_winapi.CloseHandle(process)
|
|
123
|
+
except BaseException:
|
|
124
|
+
_winapi.CloseHandle(job)
|
|
125
|
+
raise
|
|
126
|
+
return int(job)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def resume(pid: int) -> None:
|
|
130
|
+
"""Let the process `pid`, started with `CREATE_SUSPENDED`, run: `subprocess` closes the handle of its thread, so
|
|
131
|
+
the thread is found by the process ID."""
|
|
132
|
+
snapshot = _kernel32.CreateToolhelp32Snapshot(_TH32CS_SNAPTHREAD, 0)
|
|
133
|
+
if snapshot == _INVALID_HANDLE_VALUE:
|
|
134
|
+
raise _error()
|
|
135
|
+
try:
|
|
136
|
+
entry = _ThreadEntry()
|
|
137
|
+
entry.dwSize = ctypes.sizeof(entry)
|
|
138
|
+
resumed = 0
|
|
139
|
+
found = _kernel32.Thread32First(snapshot, ctypes.byref(entry))
|
|
140
|
+
while found:
|
|
141
|
+
if entry.th32OwnerProcessID == pid:
|
|
142
|
+
thread = _kernel32.OpenThread(_THREAD_SUSPEND_RESUME, False, entry.th32ThreadID)
|
|
143
|
+
if not thread:
|
|
144
|
+
raise _error()
|
|
145
|
+
try:
|
|
146
|
+
if _kernel32.ResumeThread(thread) == _RESUME_FAILED:
|
|
147
|
+
raise _error()
|
|
148
|
+
finally:
|
|
149
|
+
_winapi.CloseHandle(thread)
|
|
150
|
+
resumed += 1
|
|
151
|
+
found = _kernel32.Thread32Next(snapshot, ctypes.byref(entry))
|
|
152
|
+
if not resumed:
|
|
153
|
+
raise OSError(f"Found no thread of process {pid} to resume.")
|
|
154
|
+
finally:
|
|
155
|
+
_winapi.CloseHandle(snapshot)
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def release(job: int) -> None:
|
|
159
|
+
"""Let the processes of `job` run on once its handle is closed."""
|
|
160
|
+
_set_limits(job, _JOB_OBJECT_LIMIT_BREAKAWAY_OK)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def terminate(job: int) -> None:
|
|
164
|
+
"""End every process in `job`."""
|
|
165
|
+
if not _kernel32.TerminateJobObject(job, 1):
|
|
166
|
+
raise _error()
|
robotcode/repl/cli.py
CHANGED
|
@@ -17,7 +17,7 @@ from robotcode.runner.cli.robot import ROBOT_OPTIONS, ROBOT_VERSION_OPTIONS, loa
|
|
|
17
17
|
from .__version__ import __version__
|
|
18
18
|
from ._backends import BACKEND_CHOICES
|
|
19
19
|
from ._debug import DebugController, DebugTerminated
|
|
20
|
-
from ._session import SessionError, SessionInterpreter, SessionKind, SessionServer, project_folder
|
|
20
|
+
from ._session import PipeHolder, SessionError, SessionInterpreter, SessionKind, SessionServer, project_folder
|
|
21
21
|
from .console_interpreter import _LINE_BREAK_RE, ConsoleInterpreter
|
|
22
22
|
from .prompt_toolkit_interpreter import PromptToolkitConsoleInterpreter
|
|
23
23
|
from .run import ReplResult, run_repl
|
|
@@ -402,14 +402,6 @@ SESSION_OPTIONS = [
|
|
|
402
402
|
hidden=show_hidden_arguments(),
|
|
403
403
|
help="Serve the session ID that `robotcode session start` started. This is an internal option.",
|
|
404
404
|
),
|
|
405
|
-
click.option(
|
|
406
|
-
"--serve-output",
|
|
407
|
-
type=int,
|
|
408
|
-
metavar="FD",
|
|
409
|
-
hidden=show_hidden_arguments(),
|
|
410
|
-
help="Read the output of the session from the pipe FD, a handle on Windows, into memory. "
|
|
411
|
-
"This is an internal option.",
|
|
412
|
-
),
|
|
413
405
|
click.option(
|
|
414
406
|
"--serve-idle-timeout",
|
|
415
407
|
type=float,
|
|
@@ -419,23 +411,29 @@ SESSION_OPTIONS = [
|
|
|
419
411
|
help="End the session after it has waited at its prompt for MINUTES without a request. "
|
|
420
412
|
"This is an internal option.",
|
|
421
413
|
),
|
|
414
|
+
click.option(
|
|
415
|
+
"--serve-output-holder",
|
|
416
|
+
metavar="PID:HANDLE",
|
|
417
|
+
hidden=show_hidden_arguments(),
|
|
418
|
+
help="Take the output of the session from the handle HANDLE of the process PID, on Windows. "
|
|
419
|
+
"This is an internal option.",
|
|
420
|
+
),
|
|
422
421
|
]
|
|
423
422
|
|
|
424
423
|
|
|
425
424
|
def _session_server(
|
|
426
425
|
app: Application,
|
|
427
426
|
serve_session: Optional[str],
|
|
428
|
-
serve_output: Optional[int],
|
|
429
427
|
kind: SessionKind,
|
|
430
428
|
idle_timeout: float,
|
|
429
|
+
output_holder: Optional[str],
|
|
431
430
|
) -> Optional[SessionServer]:
|
|
432
431
|
if serve_session is None:
|
|
433
432
|
return None
|
|
434
|
-
if serve_output is None:
|
|
435
|
-
raise click.UsageError("--serve-session needs --serve-output.")
|
|
436
433
|
try:
|
|
434
|
+
holder = PipeHolder.parse(output_holder) if output_holder is not None else None
|
|
437
435
|
# The folder `session start` ran in decides the project, also for a debug run of a suite elsewhere.
|
|
438
|
-
return SessionServer(serve_session, kind, project_folder(app), idle_timeout * 60,
|
|
436
|
+
return SessionServer(serve_session, kind, project_folder(app), idle_timeout * 60, holder)
|
|
439
437
|
except SessionError as e:
|
|
440
438
|
raise click.ClickException(str(e)) from e
|
|
441
439
|
|
|
@@ -525,8 +523,8 @@ def repl(
|
|
|
525
523
|
break_on_failed_test: bool,
|
|
526
524
|
break_on_failed_suite: bool,
|
|
527
525
|
serve_session: Optional[str],
|
|
528
|
-
serve_output: Optional[int],
|
|
529
526
|
serve_idle_timeout: float,
|
|
527
|
+
serve_output_holder: Optional[str],
|
|
530
528
|
) -> None:
|
|
531
529
|
"""Run Robot Framework interactively (alias `shell`).
|
|
532
530
|
|
|
@@ -553,7 +551,7 @@ def repl(
|
|
|
553
551
|
|
|
554
552
|
backend = _resolve_backend(plain, backend)
|
|
555
553
|
|
|
556
|
-
server = _session_server(app, serve_session,
|
|
554
|
+
server = _session_server(app, serve_session, SessionKind.REPL, serve_idle_timeout, serve_output_holder)
|
|
557
555
|
interpreter = (
|
|
558
556
|
SessionInterpreter(server, app=app, files=list(files), show_keywords=show_keywords, inspect=inspect)
|
|
559
557
|
if server is not None
|
|
@@ -573,7 +571,7 @@ def repl(
|
|
|
573
571
|
# The debugger stays wired for the whole interactive session and is torn down
|
|
574
572
|
# with the interpreter when the session ends — so, unlike `robot`, there is no
|
|
575
573
|
# per-run unregister here. `attached` decides whether it actually pauses.
|
|
576
|
-
|
|
574
|
+
_attach_debugger(
|
|
577
575
|
interpreter,
|
|
578
576
|
break_at=break_at,
|
|
579
577
|
attached=attached,
|
|
@@ -582,8 +580,6 @@ def repl(
|
|
|
582
580
|
break_on_failed_test=break_on_failed_test,
|
|
583
581
|
break_on_failed_suite=break_on_failed_suite,
|
|
584
582
|
)
|
|
585
|
-
if server is not None:
|
|
586
|
-
server.keep_breakpoints(controller)
|
|
587
583
|
|
|
588
584
|
# A session records the exit code of the command when the REPL exits, and stays open.
|
|
589
585
|
session: ContextManager[object] = server if server is not None else contextlib.nullcontext()
|
|
@@ -649,8 +645,8 @@ def robot_debug(
|
|
|
649
645
|
break_on_failed_test: bool,
|
|
650
646
|
break_on_failed_suite: bool,
|
|
651
647
|
serve_session: Optional[str],
|
|
652
|
-
serve_output: Optional[int],
|
|
653
648
|
serve_idle_timeout: float,
|
|
649
|
+
serve_output_holder: Optional[str],
|
|
654
650
|
) -> None:
|
|
655
651
|
"""Run a real Robot Framework suite with the debugger attached (alias `run-debug`).
|
|
656
652
|
|
|
@@ -667,7 +663,7 @@ def robot_debug(
|
|
|
667
663
|
|
|
668
664
|
backend = _resolve_backend(plain, backend)
|
|
669
665
|
|
|
670
|
-
server = _session_server(app, serve_session,
|
|
666
|
+
server = _session_server(app, serve_session, SessionKind.DEBUG, serve_idle_timeout, serve_output_holder)
|
|
671
667
|
# A run stops at `(rdb) `, which always shows; `robot-debug` has no option for `>>> `.
|
|
672
668
|
interpreter = (
|
|
673
669
|
SessionInterpreter(server, app=app, files=[], show_keywords=False, inspect=False)
|
|
@@ -692,8 +688,6 @@ def robot_debug(
|
|
|
692
688
|
break_on_failed_test=break_on_failed_test,
|
|
693
689
|
break_on_failed_suite=break_on_failed_suite,
|
|
694
690
|
)
|
|
695
|
-
if server is not None:
|
|
696
|
-
server.keep_breakpoints(controller)
|
|
697
691
|
controller.warn_about_unreached_breakpoints()
|
|
698
692
|
|
|
699
693
|
# `robot-debug` produces the same console output as `robotcode robot` — the run
|
robotcode/repl/doc_cli.py
CHANGED
|
@@ -90,6 +90,8 @@ class KeywordEntry:
|
|
|
90
90
|
doc: str
|
|
91
91
|
source: str
|
|
92
92
|
lineno: int
|
|
93
|
+
# only `True` or `None`: the JSON and TOML output leave the field out when it is `None`
|
|
94
|
+
deprecated: Optional[bool] = None
|
|
93
95
|
|
|
94
96
|
|
|
95
97
|
@dataclass
|
|
@@ -398,6 +400,7 @@ def _keyword_entries(library: LibraryDoc, page: str, keywords: List[KeywordDoc])
|
|
|
398
400
|
doc=kw.format_text(kw.doc),
|
|
399
401
|
source=kw.source or "",
|
|
400
402
|
lineno=kw.line_no,
|
|
403
|
+
deprecated=True if kw.is_deprecated else None,
|
|
401
404
|
)
|
|
402
405
|
for kw in keywords
|
|
403
406
|
]
|
robotcode/repl/session_cli.py
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import
|
|
1
|
+
import contextlib
|
|
2
2
|
import multiprocessing
|
|
3
3
|
import os
|
|
4
|
+
import signal
|
|
4
5
|
import sys
|
|
6
|
+
import threading
|
|
5
7
|
import time
|
|
6
|
-
from
|
|
8
|
+
from types import FrameType
|
|
9
|
+
from typing import Any, Callable, Dict, Iterator, List, Optional, Tuple, Type, TypeVar, Union
|
|
7
10
|
|
|
8
11
|
import click
|
|
9
12
|
from click.core import ParameterSource
|
|
@@ -19,12 +22,9 @@ from ._session import (
|
|
|
19
22
|
STARTUP_LIMIT,
|
|
20
23
|
AnswerStatus,
|
|
21
24
|
ChannelAnswer,
|
|
22
|
-
MovedAnswer,
|
|
23
25
|
OutputAnswer,
|
|
24
26
|
RefusedAnswer,
|
|
25
27
|
Request,
|
|
26
|
-
RestartedAnswer,
|
|
27
|
-
RestartFailedAnswer,
|
|
28
28
|
ResultAnswer,
|
|
29
29
|
SessionAnswer,
|
|
30
30
|
SessionEntry,
|
|
@@ -37,17 +37,15 @@ from ._session import (
|
|
|
37
37
|
create_sessions_dir,
|
|
38
38
|
new_session_id,
|
|
39
39
|
open_sessions,
|
|
40
|
+
output_holder,
|
|
40
41
|
project_folder,
|
|
42
|
+
read_available,
|
|
41
43
|
read_entry,
|
|
42
|
-
read_to_end,
|
|
43
44
|
remove_session_files,
|
|
44
45
|
sessions_dir,
|
|
45
46
|
start_session_process,
|
|
46
47
|
)
|
|
47
48
|
|
|
48
|
-
if sys.platform == "win32":
|
|
49
|
-
import msvcrt
|
|
50
|
-
|
|
51
49
|
F = TypeVar("F", bound=Callable[..., Any])
|
|
52
50
|
A = TypeVar("A", bound=SessionAnswer)
|
|
53
51
|
|
|
@@ -79,8 +77,7 @@ def session() -> None:
|
|
|
79
77
|
take. The session keeps running when the command has returned, until it is
|
|
80
78
|
stopped or has waited for the idle timeout without a request. When the REPL
|
|
81
79
|
exits or the debug run ends, the session stays open: `session status` tells
|
|
82
|
-
it, `session result` prints the exit code
|
|
83
|
-
again.
|
|
80
|
+
it, and `session result` prints the exit code.
|
|
84
81
|
|
|
85
82
|
\b
|
|
86
83
|
Examples:
|
|
@@ -186,17 +183,16 @@ def _start(
|
|
|
186
183
|
session_id = new_session_id(kind)
|
|
187
184
|
try:
|
|
188
185
|
create_sessions_dir()
|
|
186
|
+
# The process writes its standard output and standard error to the pipe and reads it at the other end, which
|
|
187
|
+
# it takes from this process.
|
|
188
|
+
read_end, write_end = create_output_pipe(session_id)
|
|
189
189
|
except OSError as e:
|
|
190
190
|
# A sandbox, for example, can keep a command from writing there.
|
|
191
|
+
remove_session_files(session_id)
|
|
191
192
|
raise click.ClickException(
|
|
192
193
|
f"Can't create the files of the session in {sessions_dir()}: {e.strerror or e}."
|
|
193
194
|
) from e
|
|
194
|
-
|
|
195
|
-
read_end, write_end = create_output_pipe()
|
|
196
|
-
if sys.platform == "win32":
|
|
197
|
-
output = msvcrt.get_osfhandle(read_end)
|
|
198
|
-
else:
|
|
199
|
-
output = read_end
|
|
195
|
+
holder = output_holder(read_end)
|
|
200
196
|
command = [
|
|
201
197
|
sys.executable,
|
|
202
198
|
"-m",
|
|
@@ -207,47 +203,58 @@ def _start(
|
|
|
207
203
|
kind.command,
|
|
208
204
|
"--serve-session",
|
|
209
205
|
session_id,
|
|
210
|
-
"--serve-output",
|
|
211
|
-
str(output),
|
|
212
206
|
"--serve-idle-timeout",
|
|
213
207
|
str(idle_timeout),
|
|
208
|
+
*(["--serve-output-holder", str(holder)] if holder is not None else []),
|
|
214
209
|
*args,
|
|
215
210
|
]
|
|
216
211
|
# Unbuffered, so that an answer has all the output written before it, and in UTF-8, which the session decodes.
|
|
217
212
|
env = {**os.environ, "PYTHONUNBUFFERED": "1", "PYTHONIOENCODING": "utf-8"}
|
|
218
213
|
try:
|
|
219
|
-
process = start_session_process(command,
|
|
214
|
+
process = start_session_process(command, write_end, env)
|
|
215
|
+
except BaseException:
|
|
216
|
+
os.close(read_end)
|
|
217
|
+
remove_session_files(session_id)
|
|
218
|
+
raise
|
|
220
219
|
finally:
|
|
221
220
|
os.close(write_end)
|
|
222
221
|
|
|
223
222
|
try:
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
while entry is None:
|
|
231
|
-
if process.poll() is not None:
|
|
232
|
-
_ended_before_ready(
|
|
233
|
-
app,
|
|
234
|
-
session_id,
|
|
235
|
-
read_to_end(read_end),
|
|
236
|
-
f"The session ended before it was ready, with exit code {process.returncode}.",
|
|
237
|
-
process.returncode,
|
|
238
|
-
)
|
|
239
|
-
if time.monotonic() >= ready_by:
|
|
240
|
-
process.kill()
|
|
241
|
-
process.wait()
|
|
242
|
-
_ended_before_ready(
|
|
243
|
-
app,
|
|
244
|
-
session_id,
|
|
245
|
-
read_to_end(read_end),
|
|
246
|
-
f"The session wasn't ready after {limit:g} seconds and was ended.",
|
|
247
|
-
DATA_ERROR,
|
|
248
|
-
)
|
|
249
|
-
time.sleep(0.05)
|
|
223
|
+
with _ending_like_ctrl_c():
|
|
224
|
+
# `--timeout` is for the first prompt. Until the session takes commands at all, `start` waits longer, so
|
|
225
|
+
# that the other commands always reach a session whose ID it has printed.
|
|
226
|
+
deadline = time.monotonic() + timeout
|
|
227
|
+
limit = max(timeout, _STARTUP_LIMIT)
|
|
228
|
+
ready_by = time.monotonic() + limit
|
|
250
229
|
entry = read_entry(session_id)
|
|
230
|
+
while entry is None:
|
|
231
|
+
if process.popen.poll() is not None:
|
|
232
|
+
# What the process has started, such as the session under a `wrapper`, may still run.
|
|
233
|
+
process.end()
|
|
234
|
+
_ended_before_ready(
|
|
235
|
+
app,
|
|
236
|
+
session_id,
|
|
237
|
+
read_available(read_end),
|
|
238
|
+
f"The session ended before it was ready, with exit code {process.popen.returncode}.",
|
|
239
|
+
process.popen.returncode,
|
|
240
|
+
)
|
|
241
|
+
if time.monotonic() >= ready_by:
|
|
242
|
+
process.end()
|
|
243
|
+
_ended_before_ready(
|
|
244
|
+
app,
|
|
245
|
+
session_id,
|
|
246
|
+
read_available(read_end),
|
|
247
|
+
f"The session wasn't ready after {limit:g} seconds and was ended.",
|
|
248
|
+
DATA_ERROR,
|
|
249
|
+
)
|
|
250
|
+
time.sleep(0.05)
|
|
251
|
+
entry = read_entry(session_id)
|
|
252
|
+
process.release()
|
|
253
|
+
except BaseException:
|
|
254
|
+
# Such as a Ctrl-C: a session whose ID this process hasn't printed ends with it.
|
|
255
|
+
process.end()
|
|
256
|
+
remove_session_files(session_id)
|
|
257
|
+
raise
|
|
251
258
|
finally:
|
|
252
259
|
# Once the session is ready, it reads the pipe itself.
|
|
253
260
|
os.close(read_end)
|
|
@@ -261,6 +268,26 @@ def _start(
|
|
|
261
268
|
app.exit(_exit_code(answer))
|
|
262
269
|
|
|
263
270
|
|
|
271
|
+
def _exit_on_signal(signum: int, frame: Optional[FrameType]) -> None:
|
|
272
|
+
raise SystemExit(128 + signum)
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
@contextlib.contextmanager
|
|
276
|
+
def _ending_like_ctrl_c() -> Iterator[None]:
|
|
277
|
+
"""Let a SIGTERM or a SIGHUP, as when the terminal closes, end this process the way a Ctrl-C does, so that it ends
|
|
278
|
+
the session that isn't ready yet too. On Windows the job of the session does that, however this process ends."""
|
|
279
|
+
previous: Dict[signal.Signals, Union[Callable[[int, Optional[FrameType]], Any], int, None]] = {}
|
|
280
|
+
# Python takes signal handlers only in its main thread.
|
|
281
|
+
if sys.platform != "win32" and threading.current_thread() is threading.main_thread():
|
|
282
|
+
for signum in (signal.SIGTERM, signal.SIGHUP):
|
|
283
|
+
previous[signum] = signal.signal(signum, _exit_on_signal)
|
|
284
|
+
try:
|
|
285
|
+
yield
|
|
286
|
+
finally:
|
|
287
|
+
for signum, handler in previous.items():
|
|
288
|
+
signal.signal(signum, signal.SIG_DFL if handler is None else handler)
|
|
289
|
+
|
|
290
|
+
|
|
264
291
|
def _ended_before_ready(app: Application, session_id: str, output: str, message: str, exit_code: int) -> None:
|
|
265
292
|
remove_session_files(session_id)
|
|
266
293
|
if output:
|
|
@@ -283,8 +310,7 @@ def send(app: Application, timeout: float, session_id: str, text: str) -> None:
|
|
|
283
310
|
prompt, each in a send of its own. A block that INPUT leaves unfinished
|
|
284
311
|
fails. The exit code is 0 when the statement passed, 1 when it failed, and
|
|
285
312
|
124 when it still runs after `--timeout`. When the REPL or the run ends,
|
|
286
|
-
`send` says so with its exit code; a `send` after that fails
|
|
287
|
-
`session restart` starts it again.
|
|
313
|
+
`send` says so with its exit code; a `send` after that fails.
|
|
288
314
|
"""
|
|
289
315
|
entry = _entry(session_id)
|
|
290
316
|
answer = _expect(OutputAnswer, entry, SessionRequest(Request.SEND, text), timeout)
|
|
@@ -343,45 +369,6 @@ def session_result(app: Application, session_id: str) -> None:
|
|
|
343
369
|
app.echo(str(answer.exit_code))
|
|
344
370
|
|
|
345
371
|
|
|
346
|
-
@session.command()
|
|
347
|
-
@_timeout_option("the first prompt")
|
|
348
|
-
@click.argument("session_id", metavar="ID")
|
|
349
|
-
@pass_application
|
|
350
|
-
def restart(app: Application, timeout: float, session_id: str) -> None:
|
|
351
|
-
"""\
|
|
352
|
-
Start the REPL or the run of the session ID again in a new process, and print its output up to its first prompt
|
|
353
|
-
or its end.
|
|
354
|
-
|
|
355
|
-
A REPL or a run that hasn't ended is ended as with `session stop`, and its
|
|
356
|
-
output since the last command comes first. The session keeps its ID, and
|
|
357
|
-
the new process starts with the options of `session start` and the
|
|
358
|
-
breakpoints as they are.
|
|
359
|
-
"""
|
|
360
|
-
entry = _entry(session_id)
|
|
361
|
-
# The old process ends the REPL or the run as `stop` does, then waits for the new process to take commands.
|
|
362
|
-
restarted = _request(entry, SessionRequest(Request.RESTART), 2 * _STARTUP_LIMIT)
|
|
363
|
-
if not isinstance(restarted, RestartedAnswer):
|
|
364
|
-
# After its limit, `restart` gets the output so far, while the restart goes on.
|
|
365
|
-
if isinstance(restarted, (OutputAnswer, RestartFailedAnswer)) and restarted.output:
|
|
366
|
-
app.echo(restarted.output, nl=False)
|
|
367
|
-
if isinstance(restarted, RestartFailedAnswer):
|
|
368
|
-
raise click.ClickException(restarted.reason)
|
|
369
|
-
raise click.ClickException(f"Session '{session_id}' hasn't restarted yet; `session status` tells when it has.")
|
|
370
|
-
before = restarted.output
|
|
371
|
-
# The file names the new process now.
|
|
372
|
-
entry = _entry(session_id)
|
|
373
|
-
answer = _expect(OutputAnswer, entry, SessionRequest(Request.WAIT), timeout)
|
|
374
|
-
if _structured(app):
|
|
375
|
-
whole = dataclasses.replace(answer, output=before + answer.output)
|
|
376
|
-
app.print_data({"id": session_id, **as_dict(whole, remove_defaults=True)})
|
|
377
|
-
else:
|
|
378
|
-
if before:
|
|
379
|
-
app.echo(before, nl=not before.endswith("\n"))
|
|
380
|
-
app.echo(f"Restarted session {session_id}.")
|
|
381
|
-
_echo_answer(app, entry, answer)
|
|
382
|
-
app.exit(_exit_code(answer))
|
|
383
|
-
|
|
384
|
-
|
|
385
372
|
@session.command()
|
|
386
373
|
@click.argument("session_id", metavar="ID")
|
|
387
374
|
def interrupt(session_id: str) -> None:
|
|
@@ -483,13 +470,6 @@ def _entry(session_id: str) -> SessionEntry:
|
|
|
483
470
|
def _request(entry: SessionEntry, request: SessionRequest, timeout: Optional[float] = None) -> SessionAnswer:
|
|
484
471
|
"""The answer of the session `entry` to `request`; after `timeout` seconds, the output so far."""
|
|
485
472
|
answer = _ask(entry, request, timeout)
|
|
486
|
-
while isinstance(answer, MovedAnswer):
|
|
487
|
-
# The session has just restarted in a new process, which its file names now.
|
|
488
|
-
moved_to = read_entry(entry.id)
|
|
489
|
-
if moved_to is None or moved_to.address == entry.address:
|
|
490
|
-
raise click.ClickException(f"No session '{entry.id}' is open.")
|
|
491
|
-
entry = moved_to
|
|
492
|
-
answer = _ask(entry, request, timeout)
|
|
493
473
|
if isinstance(answer, RefusedAnswer):
|
|
494
474
|
raise click.ClickException(answer.message)
|
|
495
475
|
return answer
|
|
@@ -516,9 +496,9 @@ def _ask(entry: SessionEntry, request: SessionRequest, timeout: Optional[float])
|
|
|
516
496
|
# The answer came just now, and the session has closed the connection.
|
|
517
497
|
pass
|
|
518
498
|
answer: ChannelAnswer = connection.recv()
|
|
519
|
-
except (EOFError, ConnectionError, FileNotFoundError):
|
|
520
|
-
# The
|
|
521
|
-
|
|
499
|
+
except (EOFError, ConnectionError, FileNotFoundError) as e:
|
|
500
|
+
# The session has ended since the command read its file.
|
|
501
|
+
raise click.ClickException(f"No session '{entry.id}' is open.") from e
|
|
522
502
|
except (OSError, multiprocessing.AuthenticationError) as e:
|
|
523
503
|
raise click.ClickException(f"Session '{entry.id}' doesn't answer: {e}") from e
|
|
524
504
|
return answer
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: robotcode-repl
|
|
3
|
-
Version: 2.9.0-dev.
|
|
3
|
+
Version: 2.9.0-dev.230
|
|
4
4
|
Summary: RobotCode REPL for Robot Framework
|
|
5
5
|
Project-URL: Homepage, https://robotcode.io
|
|
6
6
|
Project-URL: Donate, https://opencollective.com/robotcode
|
|
@@ -27,7 +27,7 @@ Classifier: Topic :: Utilities
|
|
|
27
27
|
Classifier: Typing :: Typed
|
|
28
28
|
Requires-Python: >=3.10
|
|
29
29
|
Requires-Dist: prompt-toolkit!=3.0.52
|
|
30
|
-
Requires-Dist: robotcode-runner==2.9.0-dev.
|
|
30
|
+
Requires-Dist: robotcode-runner==2.9.0-dev.230
|
|
31
31
|
Description-Content-Type: text/markdown
|
|
32
32
|
|
|
33
33
|
# robotcode-repl
|
|
@@ -1,32 +1,33 @@
|
|
|
1
1
|
robotcode/repl/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
-
robotcode/repl/__version__.py,sha256=
|
|
2
|
+
robotcode/repl/__version__.py,sha256=ywQBe3tPie4G6fmOEZtLhXB6uR6pzyY-tUvQqJnZ3Rs,30
|
|
3
3
|
robotcode/repl/_backends.py,sha256=s7PV-HBhnAvwjn2le0cbOJ5hwLb4MjahUo6KWtRC3lc,110
|
|
4
4
|
robotcode/repl/_indent.py,sha256=jwFKaHNvTWYpUwIWHu6JHoaCz7ljOxneR5sBrKcAjpI,3303
|
|
5
5
|
robotcode/repl/_keyword_lookup.py,sha256=c8InMUDGKsw4wYmiqJzBr9GGxPC-59sRZHI0C-GWjFA,7748
|
|
6
6
|
robotcode/repl/_progress.py,sha256=BaSbDGOy5UOXvD855Kd-lDrYbqPrYIWRVs507vJznK4,2586
|
|
7
|
-
robotcode/repl/_session.py,sha256=
|
|
7
|
+
robotcode/repl/_session.py,sha256=MI1orTClEhtU-Bpv7XmQ2tfTTd7MJe_ONer1SrrnEoY,45762
|
|
8
8
|
robotcode/repl/_session_export.py,sha256=sTSkLEMoSxNQOsII3xfDY8h-nV-mrem-4h6Iug7VQv4,2841
|
|
9
|
+
robotcode/repl/_windows_job.py,sha256=x6Bqp-ekl4rFlkLLdqqDVokkLJxdGleGQt5EyzR2Beg,6310
|
|
9
10
|
robotcode/repl/base_interpreter.py,sha256=Hb2Q2QaDAyai-Aom6dHMLRswzE2SRU_krN5HHd2Pd1I,30308
|
|
10
|
-
robotcode/repl/cli.py,sha256=
|
|
11
|
+
robotcode/repl/cli.py,sha256=bknhMS7Gtf-o0jCsU72HUtw0phXzKVVilYMbf_ln1Vk,27120
|
|
11
12
|
robotcode/repl/console_interpreter.py,sha256=nhuZLT3V2jpqdPfPiNPtWKMzgf-hqkQ4knmVywocGrM,91764
|
|
12
|
-
robotcode/repl/doc_cli.py,sha256=
|
|
13
|
+
robotcode/repl/doc_cli.py,sha256=ML16EHHJPdfMYVGZ526Jll19LRoNUV3ig30K5xYpbS4,23426
|
|
13
14
|
robotcode/repl/hooks.py,sha256=JD0A6vqQ0pcJx45l5w0wq-bWqnNkBV_6tPugtMZN4yo,280
|
|
14
15
|
robotcode/repl/prompt_toolkit_interpreter.py,sha256=fq0uRsWucGhjKwGekUShz4r4Vt0nvbeAa-phLvI6WNw,13416
|
|
15
16
|
robotcode/repl/py.typed,sha256=bWew9mHgMy8LqMu7RuqQXFXLBxh2CRx0dUbSx-3wE48,27
|
|
16
17
|
robotcode/repl/run.py,sha256=IWeiGZm56vkLn-hykP-_mWLrdpDciNyMk33_dT-6EJA,5327
|
|
17
|
-
robotcode/repl/session_cli.py,sha256=
|
|
18
|
+
robotcode/repl/session_cli.py,sha256=eCsbj0Oty23tSDJeoRv43c1O8zu-QQpVTaS1-nU_H-E,19909
|
|
18
19
|
robotcode/repl/Repl/__init__.py,sha256=-j2WxY3MzdLfsMvpa2wMs6WVF_eeJygdshtMN50r67U,43
|
|
19
20
|
robotcode/repl/Repl/repl.py,sha256=k3vCeg9z_4cb2dgZ4YKHl4TD599gumVkQMImAzGl20Y,1628
|
|
20
|
-
robotcode/repl/_debug/__init__.py,sha256=
|
|
21
|
-
robotcode/repl/_debug/controller.py,sha256=
|
|
22
|
-
robotcode/repl/_debug/types.py,sha256=
|
|
21
|
+
robotcode/repl/_debug/__init__.py,sha256=uQNyC3pdlpJZI3FRKzcp9BYEz1QtfBR--gsgKNomxrQ,908
|
|
22
|
+
robotcode/repl/_debug/controller.py,sha256=6UYk1hYWuuvkLzAfz9kmbUo2OguAfZk813NZDL45PRw,34886
|
|
23
|
+
robotcode/repl/_debug/types.py,sha256=_1Xk9Vj6sAJ4u35C5OI2S0Uwsk9EgLpFH8PDixbAKIs,4891
|
|
23
24
|
robotcode/repl/_pt/__init__.py,sha256=k1bJgYbKwp5ScYO8fSx0BVy8XW9Aw_8o0jTOWQTQdX8,411
|
|
24
25
|
robotcode/repl/_pt/completion.py,sha256=BJD6sASKu1EfW0QPNKIDRc9O-8ttErXvxvnFb10lAxk,23318
|
|
25
26
|
robotcode/repl/_pt/components.py,sha256=kuYVXJc4bFJq4b3-JtAtBYymvS7ssSwlKDldWYFGMuA,20152
|
|
26
27
|
robotcode/repl/_pt/doc_viewer.py,sha256=L7ik-U9yLKv2lTLpu_85BRTsHJRAneArEkW4wik7IJo,72000
|
|
27
28
|
robotcode/repl/_pt/history.py,sha256=Rvbdu4LtbYk9dPgYJY7D1JpfyT1SETUdNmtevxo21H4,1591
|
|
28
29
|
robotcode/repl/_pt/lexer.py,sha256=YXtDMWQXPIocwceMXFlvULWyGXCDmqeAtTer_Ecuvwk,13925
|
|
29
|
-
robotcode_repl-2.9.0.
|
|
30
|
-
robotcode_repl-2.9.0.
|
|
31
|
-
robotcode_repl-2.9.0.
|
|
32
|
-
robotcode_repl-2.9.0.
|
|
30
|
+
robotcode_repl-2.9.0.dev230.dist-info/METADATA,sha256=m9vBmp44GIccIdgEFg5FnYZ24ou6ny6UfanE67X88_0,2835
|
|
31
|
+
robotcode_repl-2.9.0.dev230.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
32
|
+
robotcode_repl-2.9.0.dev230.dist-info/entry_points.txt,sha256=lI4idVOEmSWxmmioejg-_QvHXTLySK6ZxJ13R_v8ykU,40
|
|
33
|
+
robotcode_repl-2.9.0.dev230.dist-info/RECORD,,
|
|
File without changes
|
{robotcode_repl-2.9.0.dev197.dist-info → robotcode_repl-2.9.0.dev230.dist-info}/entry_points.txt
RENAMED
|
File without changes
|