For those who might not know, futurize is a Python tool that helps convert Python 2 code to Python 3. It's part of the python-future project, which aims to facilitate the transition between the two major versions of the language.
Now, regarding past.builtins:
Purpose:
The primary purpose of past.builtins in the context of futurize is to provide a way to use Python 3's built-in functions and behaviors in Python 2 code, while still allowing the code to run on Python 2. This is achieved through a form of "backporting" or "polyfilling" - making Python 3's built-ins available in a Python 2 environment.
How it works:
Importing: When you run futurize on your Python 2 code, it may insert imports from past.builtins for specific functions or modules that have changed significantly between Python 2 and Python 3.
Backported Implementations: past.builtins contains Python 2-compatible implementations of Python 3's built-in functions and types. These implementations mimic the Python 3 behavior as closely as possible, given the constraints of the Python 2 environment.
Usage in Converted Code: Your converted code (now intended to be more Python 3-compatible) uses these past.builtins imports. When run on Python 2, the past.builtins implementations are used, providing Python 3-like behavior. If the same code is run on Python 3, the native Python 3 built-ins are used instead, as the past imports would not be necessary (and are ignored or removed in a final Python 3-only version).
Examples of What Might Be Included in past.builtins:
Built-in Functions: range (to behave like Python 3's range which returns an iterator), zip, map, etc., which in Python 3 return iterators, unlike their Python 2 counterparts which return lists.
Types and Classes: Implementations or wrappers around types like memoryview, or changes to how certain types behave (e.g., integer division, str vs. bytes handling).
Here's an example to illustrate this:
Before (Python 2):
python
# In Python 2, zip returns a list
zipped = zip([1, 2], [3, 4])
print(type(zipped)) # Output: <type 'list'>
After (Using futurize and past.builtins):
python
from past.builtins import zip
# Now, zip behaves like Python 3's zip, returning an iterator
zipped = zip([1, 2], [3, 4])
print(type(zipped)) # Output: <class 'zip'>
Implementation Details:
The past package uses a technique called "backporting," where it reimplements Python 3 features in a way that works in Python 2. This includes using compatibility layers to ensure that Python 3 semantics (like iterator-based functions) work correctly in Python 2.
For types like memoryview, past.builtins provides a wrapper that mimics the Python 3 memoryview object, allowing Python 2 code to work with it similarly to how it would in Python 3.
Key Benefit:
The use of past.builtins by futurize enables a more gradual migration process from Python 2 to Python 3. It allows developers to start adapting their codebase to Python 3 idioms and best practices while still supporting Python 2, making the eventual full transition to Python 3 smoother.