pyodide/python/code_transformation.py - Code Transformation
Location: src/pyodide/python/code_transformation.py
This file is a key part of Nagini's non-blocking input system. It contains the logic for transforming user-provided Python code to support async input() calls.
Core Functions
transform_code_for_execution(code)
- Description: This is the main function called by the worker. It first checks if the user's code contains any
input()calls. If it does, it transforms the code by callingprepare_code_for_async_inputand then wrapping the entire script in anasync deffunction. This allows the code to be run asynchronously, preventing it from blocking the main browser thread while waiting for user input. If noinput()calls are found, the code is returned unmodified. - Parameters:
code(string): The user's Python code.
- Returns: The transformed (or original) Python code as a string.
prepare_code_for_async_input(code)
- Description: This function iterates through the user's code line by line and replaces every call to
input()withawait input(). This is necessary because the built-ininputfunction is replaced with anasyncversion inworker-input.js. - Parameters:
code(string): The user's Python code.
- Returns: The Python code with
awaitadded to allinput()calls.