Event taxonomy
Pyccolo emits over a hundred distinct events. Every one is a member of
pyccolo.trace_events.TraceEvent and is also re-exported at the top level,
so pyc.before_stmt and TraceEvent.before_stmt are the same object. You
register a handler for an event with the decorator or registration API, and the handler is invoked with the standard handler
signature.
Note
The table below is generated at build time directly from
pyccolo/trace_events.py — the list of events, and the thunk / sys /
ast flags, come straight from the source, so this page cannot drift from
the library. If you are reading a released version, it describes exactly the
events that version emits.
How to read the table
Fires on — the source construct (and moment) that triggers the event.
Handler return — what the value your handler returns does. The common contracts are:
replacement value — the returned value replaces the value flowing out of the instrumented expression (return
Noneto leave it unchanged; returnpyccolo.Nullto force an actualNone). This is what powers behavior-changing tracers.callable (thunk) — the event fires before an expression is evaluated, so the value in flight is a zero-argument callable that produces it. Return a (possibly different) callable to control whether and how it runs. These are exactly the events flagged thunk (they live in
BEFORE_EXPR_EVENTS).observe only — the event is a notification; the return value is ignored (aside from the universal
pyccolo.Skip/pyccolo.SkipAllcontrols).
Flags:
thunk — a “before-expression” event; the handler receives/returns a thunk. See Handlers for the calling convention.
sys — a native sys.settrace event; no AST rewriting is involved, so it fires without
pyc.exec(see Tracing real programs).ast: NodeType — you may register a handler by passing this
astnode type instead of the event, viaAST_TO_EVENT_MAPPING(e.g.@pyc.register_handler(ast.Assign)isafter_assign_rhs).
The events
Module & import lifecycle
Event |
Fires on |
Handler return |
Flags |
|---|---|---|---|
|
an |
observe only |
— |
|
a module body begins executing |
observe only |
ast: |
|
a module body finishes executing |
observe only |
— |
|
an |
observe only |
— |
Statements
Event |
Fires on |
Handler return |
Flags |
|---|---|---|---|
|
each statement, before it runs |
a thunk to run instead, or |
— |
|
each statement, after it runs |
observe only |
ast: |
|
a top-level module statement finishes |
replacement value (the displayed result) |
— |
|
a bare expression statement finishes |
replacement value |
— |
|
an |
observe only |
— |
|
an |
observe only |
— |
Names & literals
Event |
Fires on |
Handler return |
Flags |
|---|---|---|---|
|
a bare name is loaded (e.g. |
replacement value |
ast: |
|
a |
replacement value |
— |
|
an |
replacement value |
— |
|
a |
replacement value |
— |
|
a |
replacement value |
— |
|
a |
replacement value |
— |
|
a |
replacement value |
— |
|
a |
replacement value |
— |
|
an |
replacement value |
ast: |
|
an f-string, before it is built |
callable (thunk) |
thunk |
|
an f-string, after it is built |
replacement value |
— |
Assignment
Event |
Fires on |
Handler return |
Flags |
|---|---|---|---|
|
the right-hand side of an assignment, before it runs |
callable (thunk) |
thunk |
|
the right-hand side of an assignment, after it runs |
replacement value |
ast: |
|
the RHS of an augmented assignment ( |
callable (thunk) |
thunk |
|
the RHS of an augmented assignment, after it runs |
replacement value |
— |
Attributes & subscripts
Event |
Fires on |
Handler return |
Flags |
|---|---|---|---|
|
an attribute load |
the receiver object to dereference |
— |
|
an attribute load, after dereference |
replacement value |
ast: |
|
an attribute store |
observe only |
— |
|
an attribute delete |
observe only |
— |
|
a subscript load |
the receiver object to dereference |
— |
|
a subscript load, after dereference |
replacement value |
ast: |
|
a subscript store |
observe only |
— |
|
a subscript delete |
observe only |
— |
|
the slice/key of a subscript, before it is evaluated |
callable (thunk) |
thunk |
|
the slice/key of a subscript, after it is evaluated |
replacement value |
— |
|
a compound load (chained attribute/subscript/call), before it runs |
callable (thunk) |
thunk |
|
a compound load, after it runs |
replacement value |
— |
Calls & arguments
Event |
Fires on |
Handler return |
Flags |
|---|---|---|---|
|
a decorator is applied |
replacement value (the decorator) |
— |
|
a call |
the callable to invoke |
— |
|
a call |
replacement value |
ast: |
|
a call argument, before it is evaluated |
callable (thunk) |
thunk |
|
a call argument, after it is evaluated |
replacement value |
ast: |
|
a |
callable (thunk) |
thunk |
|
a |
replacement value |
ast: |
Operators
Event |
Fires on |
Handler return |
Flags |
|---|---|---|---|
|
a binary op |
callable (thunk) |
thunk |
|
a binary op, after it runs |
replacement value (result) |
ast: |
|
the left operand of a binop, before it runs |
callable (thunk) |
thunk |
|
the left operand of a binop, after it runs |
replacement value |
— |
|
the right operand of a binop, before it runs |
callable (thunk) |
thunk |
|
the right operand of a binop, after it runs |
replacement value |
— |
|
a unary op |
callable (thunk) |
thunk |
|
a unary op, after it runs |
replacement value (result) |
ast: |
|
the operand of a unary op, before it runs |
callable (thunk) |
thunk |
|
the operand of a unary op, after it runs |
replacement value |
— |
|
a boolean op |
callable (thunk) |
thunk |
|
a boolean op, after it runs |
replacement value (result) |
— |
|
an operand of a boolean op, before it runs |
callable (thunk) |
thunk |
|
an operand of a boolean op, after it runs |
replacement value |
— |
|
a comparison |
callable (thunk) |
thunk |
|
a comparison, after it runs |
replacement value (result) |
ast: |
|
the left operand of a comparison |
replacement value |
— |
|
a right-hand operand of a comparison |
replacement value |
— |
Collection literals
Event |
Fires on |
Handler return |
Flags |
|---|---|---|---|
|
a list literal |
callable (thunk) |
thunk |
|
a list literal, after it is built |
replacement value |
ast: |
|
a tuple literal |
callable (thunk) |
thunk |
|
a tuple literal, after it is built |
replacement value |
ast: |
|
a set literal |
callable (thunk) |
thunk |
|
a set literal, after it is built |
replacement value |
ast: |
|
a dict literal |
callable (thunk) |
thunk |
|
a dict literal, after it is built |
replacement value |
ast: |
|
an element of a list literal |
replacement value |
— |
|
an element of a tuple literal |
replacement value |
— |
|
an element of a set literal |
replacement value |
— |
|
a key of a dict literal |
replacement value |
— |
|
a value of a dict literal |
replacement value |
— |
Comprehensions
Event |
Fires on |
Handler return |
Flags |
|---|---|---|---|
|
a comprehension |
replacement value |
— |
|
a comprehension element is evaluated |
replacement value |
— |
|
a dict-comprehension key is evaluated |
replacement value |
— |
|
a dict-comprehension value is evaluated |
replacement value |
— |
Control flow
Event |
Fires on |
Handler return |
Flags |
|---|---|---|---|
|
the test of an |
replacement value (test result) |
— |
|
the test of a |
replacement value (test result) |
— |
|
a |
observe only |
— |
|
a |
observe only (guarded) |
— |
|
a |
observe only |
— |
|
a |
observe only (guarded) |
— |
|
the iterable of a |
callable (thunk) |
thunk |
|
the iterable of a |
replacement value |
— |
Functions & lambdas
Event |
Fires on |
Handler return |
Flags |
|---|---|---|---|
|
a function body, before it executes |
observe only |
— |
|
a function body, after it executes |
observe only (guarded) |
— |
|
a |
callable (thunk) |
thunk |
|
a |
replacement value |
— |
|
a |
observe only |
— |
|
a |
replacement value |
— |
Exceptions
Event |
Fires on |
Handler return |
Flags |
|---|---|---|---|
|
the type in an |
replacement value (the caught type) |
— |
sys.settrace events
Event |
Fires on |
Handler return |
Flags |
|---|---|---|---|
|
|
observe only |
sys |
|
a stack frame is pushed ( |
observe only |
sys |
|
a stack frame is popped ( |
observe only |
sys |
|
an exception is raised ( |
observe only |
sys |
|
a bytecode opcode executes (opt-in, |
observe only |
sys |
|
a C function is called ( |
observe only (rarely used) |
— |
|
a C function returns ( |
observe only (rarely used) |
— |
|
a C function raises ( |
observe only (rarely used) |
— |