|
30 | 30 |
|
31 | 31 | The variables carry a path and nothing else: the launch takes no other argument |
32 | 32 | from the host. |
| 33 | +
|
| 34 | +Under every one of those readings sits Playwright's driver, a Node process that |
| 35 | +leaf neither names nor sees. Where that process ends at startup, or never runs |
| 36 | +at all, no launch is reached, so `playwright_driver()` reports it beside the |
| 37 | +failures above rather than through them. |
33 | 38 | """ |
34 | 39 |
|
35 | 40 | import os |
36 | 41 | import re |
37 | 42 | import shutil |
| 43 | +from contextlib import contextmanager |
| 44 | +from pathlib import Path |
38 | 45 |
|
39 | 46 | VARIABLE = "LEAF_BROWSER_EXECUTABLE" |
40 | 47 | VARIABLES = (VARIABLE, "CHROME_PATH", "CHROME_BIN") |
41 | 48 |
|
| 49 | +# Playwright runs its driver under Node, and reads this for the executable. It is |
| 50 | +# not leaf's namespace, but it is the only knob over the process whose failure |
| 51 | +# `playwright_driver()` reports, so the message that reports one names it. |
| 52 | +DRIVER_VARIABLE = "PLAYWRIGHT_NODEJS_PATH" |
| 53 | + |
42 | 54 | # Chrome first, so discovery reaching a host that also has a Chrome would land |
43 | 55 | # where the channel already does. |
44 | 56 | COMMANDS = ( |
@@ -75,6 +87,108 @@ def discovered_executable() -> str | None: |
75 | 87 | return None |
76 | 88 |
|
77 | 89 |
|
| 90 | +class DriverNotStarted(Exception): |
| 91 | + """Playwright's driver ended before it answered the connection, so no browser |
| 92 | + was ever asked for. Carries that connection's own first line, which each gate |
| 93 | + reports in its own words as it does a failed launch.""" |
| 94 | + |
| 95 | + |
| 96 | +def driver_node() -> str: |
| 97 | + """The Node that Playwright runs its driver under: whichever DRIVER_VARIABLE |
| 98 | + names, else the one bundled in the installed wheel. |
| 99 | +
|
| 100 | + Both readings are Playwright's, made here from the package's own location |
| 101 | + rather than through `playwright._impl._driver`: this runs only on the path |
| 102 | + where the driver already failed, and a private that moved would put the |
| 103 | + traceback back.""" |
| 104 | + if named := os.environ.get(DRIVER_VARIABLE): |
| 105 | + return named |
| 106 | + import playwright |
| 107 | + |
| 108 | + return str(Path(playwright.__file__).parent / "driver" / "node") |
| 109 | + |
| 110 | + |
| 111 | +def driver_hint() -> str: |
| 112 | + """The line each gate appends when the driver never started. |
| 113 | +
|
| 114 | + The two hosts this reaches are a glibc older than the bundled Node needs and a |
| 115 | + DRIVER_VARIABLE naming a Node older than the driver bundle needs. The first |
| 116 | + fails in the loader naming the binary, the second inside the bundle naming |
| 117 | + nothing, and neither says which Node ran or what chooses it — so the hint says |
| 118 | + both.""" |
| 119 | + node = driver_node() |
| 120 | + if os.environ.get(DRIVER_VARIABLE): |
| 121 | + return f"{DRIVER_VARIABLE} named {node}." |
| 122 | + return f"Playwright ran its bundled {node}; {DRIVER_VARIABLE} names another." |
| 123 | + |
| 124 | + |
| 125 | +@contextmanager |
| 126 | +def playwright_driver(): |
| 127 | + """Playwright, with a driver that never started raised as DriverNotStarted |
| 128 | + rather than as a Playwright private. |
| 129 | +
|
| 130 | + `sync_playwright().__enter__` returns `self._playwright`, which the connection |
| 131 | + callback assigns. A driver that exits at startup never runs that callback, so |
| 132 | + the context entry dies on an AttributeError naming that attribute while the |
| 133 | + real cause sits on the connection's init task — printed afterwards, out of |
| 134 | + order, as asyncio's `Task exception was never retrieved`. Reading that task is |
| 135 | + what both names the cause and keeps the second traceback from being printed at |
| 136 | + all. |
| 137 | +
|
| 138 | + A driver the host named but does not have fails one step earlier, in the spawn, |
| 139 | + and that OSError names the file. It reads as the same DriverNotStarted, so both |
| 140 | + hosts get one line — but its manager is left unentered and unexited, which is |
| 141 | + what `with` did too: Playwright's stop asserts a transport handle the failed |
| 142 | + spawn never assigned, so exiting a failed entry replaces the reason with a |
| 143 | + private of its own.""" |
| 144 | + from playwright.sync_api import sync_playwright |
| 145 | + |
| 146 | + manager = sync_playwright() |
| 147 | + try: |
| 148 | + playwright = manager.start() |
| 149 | + except AttributeError as error: |
| 150 | + # The driver ran and stayed silent, so the connection stands: its reason is |
| 151 | + # readable, and stopping it is what reaps the process and the loop. |
| 152 | + reason = _driver_failure(manager) |
| 153 | + manager.__exit__() |
| 154 | + raise DriverNotStarted(reason) from error |
| 155 | + except OSError as error: |
| 156 | + raise DriverNotStarted(_spawn_failure(manager, error)) from error |
| 157 | + try: |
| 158 | + yield playwright |
| 159 | + finally: |
| 160 | + manager.__exit__() |
| 161 | + |
| 162 | + |
| 163 | +def _spawn_failure(manager, error: OSError) -> str: |
| 164 | + """Why the driver never ran, in one line. |
| 165 | +
|
| 166 | + The spawn's own error says it. Reading the transport's error future as well is |
| 167 | + what keeps asyncio from printing that same error a second time as an |
| 168 | + unretrieved future, the way reading the init task does for a driver that ran.""" |
| 169 | + transport = getattr(getattr(manager, "_connection", None), "_transport", None) |
| 170 | + future = getattr(transport, "on_error_future", None) |
| 171 | + if future is not None and future.done() and not future.cancelled(): |
| 172 | + future.exception() |
| 173 | + return str(error).strip().splitlines()[0] |
| 174 | + |
| 175 | + |
| 176 | +def _driver_failure(manager) -> str: |
| 177 | + """Why the connection ended, in one line. |
| 178 | +
|
| 179 | + The reason is an exception on the connection's init task, reachable only |
| 180 | + through Playwright privates. Every reading is optional here and a plain |
| 181 | + sentence stands in for a missing one, because this runs on the path whose |
| 182 | + whole point is that an attribute error stops being what the reader sees.""" |
| 183 | + task = getattr(getattr(manager, "_connection", None), "_init_task", None) |
| 184 | + if task is None or not task.done() or task.cancelled(): |
| 185 | + return "the driver ended before answering" |
| 186 | + error = task.exception() |
| 187 | + if error is None: |
| 188 | + return "the driver ended before answering" |
| 189 | + return str(error).strip().splitlines()[0] |
| 190 | + |
| 191 | + |
78 | 192 | def launch_browser(p): |
79 | 193 | """The host's browser, and what to call it in a message that reports |
80 | 194 | success: whichever executable a browser variable names, else the installed |
|
0 commit comments