GitHub - nascheme/ppython: Pragmatic Python

Overview

This is a version of Python 3.6 with extra backwards compatibility features. The objective is to reduce the number of manual changes necessary after running 2to3 on a Python 2 program. Extra backwards compatible behavior will generate warnings when triggered. Once your program runs without warnings, you can safely switch to running with stock Python 3.

Some of the backwards compatible changes are as follows:

  • None compares smaller than all other types

  • comparision between disjoin types no longer raises a TypeError by default. Instead, a result based on comparing the type names is used.

  • mixing byte and str objects will not generally result in TypeError. Instead latin-1 encoding/decoding will be used to convert to the needed type.

  • added str.decode() method

  • getattr(), setattr(), delattr) accept bytes

  • str implements buffer interface

  • str/bytes decode()/encode() accept non-text encodings (e.g. base64)

  • generator objects have a next() method

  • dicts have 'iterkeys', 'itervalues', and 'iteritems' methods

All of this backwards compatible behavior will result in warnings being generated.

The recommended procedure for porting Python 2 code is as follows. Run 2to3 on the code. Run it using Pragmatic Python and fix any warnings that are generated. When all the warnings are fixed, the code should run correctly on regular "pure" versions of Python 3.

Goals

  • Python 2 code that comes out of our version of 2to3 should run correctly on Pragmatic Python without manual changes.

  • Code that runs on Pragmatic Python without warnings should run correctly on standard "pure" Python 3.

Practical issues will result in us having to compromise on these goals. When we compromise, try to minimize the amount of effort that it takes to port Python 2 code.

Plan

Rather than try to guess at the backwards compatibility that is needed, implement it as needed for porting real programs. Run 2to3 on an existing Python 2 library or program. Try to run the result. If something fails, try the following in order:

  • fix 2to3 to handle it

  • implement backwards compatibility code that will generate a warning

  • add a section to a porting guide explaining the issue and how to solve it