Fundamentals 8 min read

What Defines a True Python Implementation? REPL, Compatibility, and WebAssembly

The article examines the challenges of creating a Python implementation for WebAssembly, questioning what core features—such as a REPL, dynamic compilation, sys.settrace, and frame inspection—are essential for a language to be recognized as Python, and how much compatibility can be sacrificed for performance.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
What Defines a True Python Implementation? REPL, Compatibility, and WebAssembly

Why ask this question?

We need a Python implementation for WebAssembly, which is no longer a secret. It brings Python to browsers and, because iOS and Android support JavaScript as part of apps, also to mobile devices, which is exciting.

However, when considering the daunting task of creating a new Python implementation, I ask: what exactly is Python? Having lived with CPython for so long, many of us simply equate “Python” with CPython. Projects like PyPy aim for compatibility by reproducing CPython’s implementation details, and most implementations strive to pass CPython’s test suite and be as compatible as possible.

This is daunting. CPython’s Python is highly dynamic and exposes many features that only make sense in an interpreter implementation. For example, PyPy has a baseline interpreter for JIT, but Python code can force PyPy to disable JIT and stick to bytecode. The REPL makes things especially dynamic, as the interpreter instantly parses, compiles, and executes everything entered.

This makes me wonder what Python truly is, what the core of the language is, and what baseline features every Python implementation must cover to still be recognizable as Python. From my perspective, how much must be implemented to compile Python directly to WebAssembly and still be considered a Python implementation?

Does Python need a REPL?

What really sparked this question was thinking about compiling Python to WebAssembly without building another interpreter, but instead emitting static WebAssembly from Python source while still reasonably calling it “Python”.

I know that dynamic compilation via eval() or compile() is hard because WebAssembly’s security model validates modules at load time, meaning there’s no structure to run arbitrary code in another module’s memory space, making REPL implementation tricky.

But this leads me to ask: does Python truly need a REPL? Not to misunderstand—REPL is convenient, but if an implementation lacks it, is it still Python? I think a non‑interactive Python is still Python; it just lacks a potentially key feature.

Which parts of Python must be present to be considered “Python”?

Can you live without locals()? It’s a very dynamic feature that can collect all defined local variables and their values into a dictionary. In an interpreter like CPython you can fetch locals from the current frame, but in a compiled language you must know when to collect this information because calling locals() doesn’t guarantee the data is readily available.

Or what about overriding locals() itself? In CPython this isn’t hard because the builtins module provides a dict that you can replace, and it propagates to future calls. In a compiled language, detecting and handling this requires extra effort and can hurt performance.

What about sys.settrace()? It triggers a callback for every bytecode, which can’t work if the code is compiled. You could fake it by checking for a trace function after each line, but that seems excessive unless the compiler supports it via a flag.

And sys._getframe()? Compiled languages don’t necessarily have direct access to execution frames, so you’d need to simulate it if you want to provide frames on demand.

As you can see, many Python features make compilation difficult (Nuitka is thus well‑suited for the challenge). Yet I bet most of the mentioned features are rarely used, so if an implementation omits them, can it still be called “Python”?

How much compatibility is useful?

I have no definitive answer, but it illustrates the difficulty of implementing Python and its compatibility with existing software. I think WebAssembly doesn’t need to support the entire Python ecosystem to be useful; it can interoperate with other ecosystems like Rust and JavaScript, making it feasible to implement only the necessary parts.

I have no answer

Developing a compiler that translates Python code directly to WebAssembly, sacrificing some compatibility for performance, may make sense. Likewise, an interpreter designed for WebAssembly that retains compatibility with existing code could be valuable. Supporting RustPython in their WebAssembly efforts, or leveraging Pyodide, might also be viable paths. I don’t think any of these options are inherently wrong; the right one will emerge based on interest and usefulness.

Original link: https://developer.51cto.com/art/202008/622721.htm Author: Machine Learning and Data Analysis
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonWebAssemblycompatibilityreplLanguage Implementation
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.