• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.github/workflows/H02-Oct-2021-4540

executing/H02-Oct-2021-1,104860

executing.egg-info/H03-May-2022-165112

tests/H03-May-2022-325,578324,259

.gitignoreH A D14-Aug-20201.3 KiB11087

MANIFEST.inH A D14-Aug-202020 21

PKG-INFOH A D02-Oct-20218.2 KiB165112

README.mdH A D12-Sep-20217.3 KiB14090

make_release.shH A D18-Apr-2021615 3123

pyproject.tomlH A D14-Aug-2020219 86

setup.cfgH A D02-Oct-2021977 3933

setup.pyH A D12-Sep-202154 42

tox.iniH A D31-Jul-2021253 1412

README.md

1# executing
2
3[![Build Status](https://github.com/alexmojaki/executing/workflows/Tests/badge.svg?branch=master)](https://github.com/alexmojaki/executing/actions) [![Coverage Status](https://coveralls.io/repos/github/alexmojaki/executing/badge.svg?branch=master)](https://coveralls.io/github/alexmojaki/executing?branch=master) [![Supports Python versions 2.7 and 3.4+, including PyPy](https://img.shields.io/pypi/pyversions/executing.svg)](https://pypi.python.org/pypi/executing)
4
5This mini-package lets you get information about what a frame is currently doing, particularly the AST node being executed.
6
7* [Usage](#usage)
8    * [Getting the AST node](#getting-the-ast-node)
9    * [Getting the source code of the node](#getting-the-source-code-of-the-node)
10    * [Getting the `__qualname__` of the current function](#getting-the-__qualname__-of-the-current-function)
11    * [The Source class](#the-source-class)
12* [Installation](#installation)
13* [How does it work?](#how-does-it-work)
14* [Is it reliable?](#is-it-reliable)
15* [Which nodes can it identify?](#which-nodes-can-it-identify)
16* [Libraries that use this](#libraries-that-use-this)
17
18## Usage
19
20### Getting the AST node
21
22```python
23import executing
24
25node = executing.Source.executing(frame).node
26```
27
28Then `node` will be an AST node (from the `ast` standard library module) or None if the node couldn't be identified (which may happen often and should always be checked).
29
30`node` will always be the same instance for multiple calls with frames at the same point of execution.
31
32If you have a traceback object, pass it directly to `Source.executing()` rather than the `tb_frame` attribute to get the correct node.
33
34### Getting the source code of the node
35
36For this you will need to separately install the [`asttokens`](https://github.com/gristlabs/asttokens) library, then obtain an `ASTTokens` object:
37
38```python
39executing.Source.executing(frame).source.asttokens()
40```
41
42or:
43
44```python
45executing.Source.for_frame(frame).asttokens()
46```
47
48or use one of the convenience methods:
49
50```python
51executing.Source.executing(frame).text()
52executing.Source.executing(frame).text_range()
53```
54
55### Getting the `__qualname__` of the current function
56
57```python
58executing.Source.executing(frame).code_qualname()
59```
60
61or:
62
63```python
64executing.Source.for_frame(frame).code_qualname(frame.f_code)
65```
66
67### The `Source` class
68
69Everything goes through the `Source` class. Only one instance of the class is created for each filename. Subclassing it to add more attributes on creation or methods is recommended. The classmethods such as `executing` will respect this. See the source code and docstrings for more detail.
70
71## Installation
72
73    pip install executing
74
75If you don't like that you can just copy the file `executing.py`, there are no dependencies (but of course you won't get updates).
76
77## How does it work?
78
79Suppose the frame is executing this line:
80
81```python
82self.foo(bar.x)
83```
84
85and in particular it's currently obtaining the attribute `self.foo`. Looking at the bytecode, specifically `frame.f_code.co_code[frame.f_lasti]`, we can tell that it's loading an attribute, but it's not obvious which one. We can narrow down the statement being executed using `frame.f_lineno` and find the two `ast.Attribute` nodes representing `self.foo` and `bar.x`. How do we find out which one it is, without recreating the entire compiler in Python?
86
87The trick is to modify the AST slightly for each candidate expression and observe the changes in the bytecode instructions. We change the AST to this:
88
89```python
90(self.foo ** 'longuniqueconstant')(bar.x)
91```
92
93and compile it, and the bytecode will be almost the same but there will be two new instructions:
94
95    LOAD_CONST 'longuniqueconstant'
96    BINARY_POWER
97
98and just before that will be a `LOAD_ATTR` instruction corresponding to `self.foo`. Seeing that it's in the same position as the original instruction lets us know we've found our match.
99
100## Is it reliable?
101
102Yes - if it identifies a node, you can trust that it's identified the correct one. The tests are very thorough - in addition to unit tests which check various situations directly, there are property tests against a large number of files (see the filenames printed in [this build](https://travis-ci.org/alexmojaki/executing/jobs/557970457)) with real code. Specifically, for each file, the tests:
103
104 1. Identify as many nodes as possible from all the bytecode instructions in the file, and assert that they are all distinct
105 2. Find all the nodes that should be identifiable, and assert that they were indeed identified somewhere
106
107In other words, it shows that there is a one-to-one mapping between the nodes and the instructions that can be handled. This leaves very little room for a bug to creep in.
108
109Furthermore, `executing` checks that the instructions compiled from the modified AST exactly match the original code save for a few small known exceptions. This accounts for all the quirks and optimisations in the interpreter.
110
111## Which nodes can it identify?
112
113Currently it works in almost all cases for the following `ast` nodes:
114
115 - `Call`, e.g. `self.foo(bar)`
116 - `Attribute`, e.g. `point.x`
117 - `Subscript`, e.g. `lst[1]`
118 - `BinOp`, e.g. `x + y` (doesn't include `and` and `or`)
119 - `UnaryOp`, e.g. `-n` (includes `not` but only works sometimes)
120 - `Compare` e.g. `a < b` (not for chains such as `0 < p < 1`)
121
122The plan is to extend to more operations in the future.
123
124## Libraries that use this
125
126### My libraries
127
128- **[`stack_data`](https://github.com/alexmojaki/stack_data)**: Extracts data from stack frames and tracebacks, particularly to display more useful tracebacks than the default. Also uses another related library of mine: **[`pure_eval`](https://github.com/alexmojaki/pure_eval)**.
129- **[`snoop`](https://github.com/alexmojaki/snoop)**: A feature-rich and convenient debugging library. Uses `executing` to show the operation which caused an exception and to allow the `pp` function to display the source of its arguments.
130- **[`heartrate`](https://github.com/alexmojaki/heartrate)**: A simple real time visualisation of the execution of a Python program. Uses `executing` to highlight currently executing operations, particularly in each frame of the stack trace.
131- **[`sorcery`](https://github.com/alexmojaki/sorcery)**: Dark magic delights in Python. Uses `executing` to let special callables called spells know where they're being called from.
132
133### Libraries I've contributed to
134
135- **[`IPython`](https://github.com/ipython/ipython/pull/12150)**: Highlights the executing node in tracebacks using `executing` via [`stack_data`](https://github.com/alexmojaki/stack_data).
136- **[`icecream`](https://github.com/gruns/icecream)**: �� Sweet and creamy print debugging. Uses `executing` to identify where `ic` is called and print its arguments.
137- **[`python-devtools`](https://github.com/samuelcolvin/python-devtools)**: Uses `executing` for print debugging similar to `icecream`.
138- **[`sentry_sdk`](https://github.com/getsentry/sentry-python)**: Add the integration `sentry_sdk.integrations.executingExecutingIntegration()` to show the function `__qualname__` in each frame in sentry events. Highlighting the executing node is hopefully [coming soon](https://github.com/getsentry/sentry/pull/19924).
139- **[`varname`](https://github.com/pwwang/python-varname)**: Dark magics about variable names in python. Uses `executing` to find where its various magical functions like `varname` and `nameof` are called from.
140