Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/1334.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**A Python logpoint that cannot evaluate is refused at `set_breakpoint` time, with the form that works** — debugpy evaluates the text inside each `{...}` group of a `logMessage` as a Python expression at every hit, not as an f-string field, so `{entry!r}`, `{total:.2f}` and `{x=}` were accepted and then logged `invalid syntax (<string>, line 1)` (or `invalid decimal literal (<string>, line 1)`) at each hit. `set_breakpoint` now refuses a group whose top level holds an f-string conversion (`!r`, `!s`, `!a`), a format spec or the `=` debug form, and names the replacement: `{repr(entry)}`, `{format(total, '.2f')}`, `x={repr(x)}`. A group that reads as a dict with a quoted key or several pairs (`{"k": total}`) is told that a dict needs its own braces, `{ {"k": total} }`; a spec written straight after the colon (`{x:10}`) gets the format form only; an unquoted key with a space after the colon (`{k: v}`) gets both, format first. One refusal names up to three groups and counts the rest. Not caught, so debugpy still logs the syntax error at the hit: a brace inside a string literal, which splits the group; a comment; a conversion or spec inside brackets (`{str(x!r)}`); `{x:=^10}`, which reads as an assignment; and any group after one that does not parse on its own, since debugpy joins the groups into one expression (#1334)
2 changes: 2 additions & 0 deletions docs/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ set_breakpoint { "sessionId": "...", "file": "/abs/path/script.py", "line": 15,

A logpoint never pauses: it interpolates the `{expressions}` and writes the line to `get_output` while the program keeps running. Each hit does cost time, however — measured at 24.7 µs per hit on Python 3.13 + debugpy (`sys.monitoring`, 2.5% of a core at 1,000 hits/s), and placing a logpoint anywhere in a function slows that whole function by 58× — so keep logpoints off tight loops. When hits outrun debugpy's delivery rate (measured at 3,200–4,400 lines/s during a run and about 6,000 lines/s draining afterward), lines still queued when the program exits are lost and the session's end answer does not report the loss (issue #1265). See [Logpoints](../tool-reference.md#logpoints) for measurement conditions and what was not measured.

The braces take Python expressions, not f-string fields: debugpy evaluates each group at every hit. `set_breakpoint` therefore refuses a group whose top level holds an f-string conversion, a format spec or the `=` form, and names the form that works: `{x!r}` → `{repr(x)}`, `{total:.2f}` → `{format(total, '.2f')}`, `{x=}` → `x={repr(x)}`. A dict needs its own braces: `{ {"k": total} }`. Not caught, so debugpy logs a syntax error at each hit instead: a brace inside a string literal (debugpy splits the group there), a comment, a conversion or spec inside brackets (`{str(x!r)}`), `{x:=^10}` (read as an assignment), and any group after one that does not parse on its own.

### 3. Start Debugging

Start debugging your Python script:
Expand Down
5 changes: 5 additions & 0 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,11 @@ export {
type LogMessagePart,
type CompiledJsLogpoint
} from './utils/js-logpoint.js';
export {
extractPythonLogMessageGroups,
validatePythonLogMessage,
type PythonLogMessageGroup
} from './utils/python-logpoint.js';
// Argv marker constants shared by spawn-time tagging and the startup orphan
// reapers (issues #343, #431).
export {
Expand Down
5 changes: 5 additions & 0 deletions packages/shared/src/interfaces/adapter-policy-python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { emptyLocalVariableExtraction, extractionFromScope } from './adapter-pol
import type { StackFrame, Variable } from '../models/index.js';
import type { DapClientBehavior, DapClientContext, ReverseRequestResult } from './dap-client-behavior.js';
import type { ChildAdoptionContract, ChildReport } from './child-adoption.js';
import { validatePythonLogMessage } from '../utils/python-logpoint.js';

/**
* A debugpy child report (issue #1081): a `startDebugging` whose configuration
Expand Down Expand Up @@ -111,6 +112,10 @@ export const PythonAdapterPolicy = {
// correctly, sends no `initialized`: the worker does not wait for one (#746).
honoursNoDebug: true,
supportsLogPoints: true,
// debugpy joins every `{...}` group into one `'...' % (g1, g2, ...,)` and
// eval()s it at each hit, so an f-string conversion (`{entry!r}`), format spec
// (`{total:.2f}`) or `=` (`{x=}`) logs `invalid syntax` only when hit (issue #1334).
validateLogMessage: (logMessage: string, _condition?: string): string | undefined => validatePythonLogMessage(logMessage),
supportsFunctionBreakpoints: true,
supportsReverseStartDebugging: false,
// debugpy runs exactly one of these (`debugpy/adapter/clients.py`: '"program",
Expand Down
4 changes: 3 additions & 1 deletion packages/shared/src/interfaces/adapter-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,9 @@ export interface AdapterPolicy {

/**
* Up-front check of a logMessage (and its optional condition) for a policy
* that compiles logpoints itself (issue #853): the returned text refuses
* that compiles logpoints itself (issue #853) or rejects forms its engine
* evaluates as syntax errors only when the line is hit (Python f-string
* conversions, format specs and the `=` form, issue #1334): the returned text refuses
* set_breakpoint the way an expectedContent mismatch does, instead of the
* adapter reporting the syntax error only in its output stream. Undefined
* means the message is acceptable (or the policy does not pre-validate).
Expand Down
Loading
Loading