Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

JupyterLab extension to explore CPython Bytecode

License

NotificationsYou must be signed in to change notification settings

jtpio/jupyterlab-python-bytecode

Repository files navigation

Github Actions StatusBindernpmcode 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 orxeus-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 theAvanced Settings Editor to tweak some of the settings

Contributing

SeeCONTRIBUTING.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:

importdisdis.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:

importmathprint(math.pi)

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

importdisdis.dis("""import mathprint(math.pi)""")

Which will return (example for CPython 3.6.6):

10LOAD_CONST0 (0)2LOAD_CONST1 (None)4IMPORT_NAME0 (math)6STORE_NAME0 (math)38LOAD_NAME1 (print)10LOAD_NAME0 (math)12LOAD_ATTR2 (pi)14CALL_FUNCTION116POP_TOP18LOAD_CONST1 (None)20RETURN_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 newCALL_METHOD opcode introduced in CPython 3.7:

python_comparison

Comparing for and while loops

Original example fromDisassembling Python Bytecode, by Peter Goldsborough

for_while

About

JupyterLab extension to explore CPython Bytecode

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp