Awesome Open Source
Awesome Open Source

jupyterlab-python-bytecode

Github Actions Status Binder npm code style: prettier

JupyterLab extension to inspect Python Bytecode.

screencast

Try it online

Try the extension in your browser with Binder:

Binder

Prerequisites

  • JupyterLab 1.0+
  • ipykernel or xeus-python

To install JupyterLab:

conda install -c conda-forge jupyterlab

Installation

jupyter labextension install jupyterlab-python-bytecode

Features

  • Live Bytecode preview
  • Choose the kernel for a file (if not already started). This allows comparing the bytecode output for different versions of Python.
  • Check the Avanced Settings Editor to tweak some of the settings

Contributing

See CONTRIBUTING.md to know how to contribute and setup a development environment.

How it works

Disassembling the Python code is done by connecting to a kernel, and sending the following code for evaluation from the lab extension:

import dis
dis.dis(code_to_evaluate)

As mentioned in the documentation, there is not guarantee on the stability of the bytecode across Python versions:

Bytecode is an implementation detail of the CPython interpreter. No guarantees are made that bytecode will not be added, removed, or changed between versions of Python. Use of this module should not be considered to work across Python VMs or Python releases.

Example

For example, if the Python file contains the following lines:

import math

print(math.pi)

The following code will be sent to the kernel for evaluation:

import dis
dis.dis("""
import math

print(math.pi)
""")

Which will return (example for CPython 3.6.6):

  1           0 LOAD_CONST               0 (0)
              2 LOAD_CONST               1 (None)
              4 IMPORT_NAME              0 (math)
              6 STORE_NAME               0 (math)

  3           8 LOAD_NAME                1 (print)
             10 LOAD_NAME                0 (math)
             12 LOAD_ATTR                2 (pi)
             14 CALL_FUNCTION            1
             16 POP_TOP
             18 LOAD_CONST               1 (None)
             20 RETURN_VALUE

Comparing versions of CPython

If you have several versions of Python installed on your machine (let's say in different conda environments), you can use the extension to check how the bytecode might differ.

The following example illustrates the introduction of the new CALL_METHOD opcode introduced in CPython 3.7:

python_comparison

Comparing for and while loops

Original example from Disassembling Python Bytecode, by Peter Goldsborough

for_while


Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Typescript (252,254
Jupyter (1,745
Jupyterlab (481
Jupyterlab Extension (218
Related Projects