Development
Pragmatic Python: 2-to-3 porting help
There is an enormous body of Python 2.x code out there; much of which is likely never to be ported to Python 3 at all. But for those who wish to convert their code to Python 3—or to support both 2.x and 3.x—there are often significant barriers to doing so. Neil Schemenauer has some thoughts on smoothing that path with an altered version of the Python 3 interpreter.
He first presented those thoughts in a post
to the python-ideas mailing list in early June. He noted that he had
nearly completed porting an application from Python 2 to Python 3
and that it had "been a lot of work
". There is still a
lot of Python 2 code out there that has not been converted,
which concerns Schemenauer:
Here is a thought that [occurred] to me. Create a patched version of Python 3.x, making a stepping stone version for people porting from Python 2. I know this would have been useful for me.
He suggested that several changes that came with Python 3 should revert to their earlier behavior, but emit warnings. That way the code could be run, with the warnings collected and addressed afterward. For example, Python 2 allows comparing None to any other value, with None always comparing as less than other values. Python 3 raises a TypeError instead, so a stepping-stone interpreter would reinstate the old behavior but generate a warning. Also, mixing Unicode and byte strings would not lead to an error; the result would instead be encoded or decoded using some default encoding (he originally suggested Latin-1, but mentioned UTF-8 as a possibility later in the thread) and a warning issued. There were a few others, as well as a list of things that Schemenauer did not think were needed (e.g. print syntax, try/except syntax) since they are easy to handle when porting.
The reaction to the idea was somewhat negative overall. Some thought Schemenauer was proposing to change Python 3 to permanently to support these kinds of features, which was seen as a step backward. Nick Coghlan argued that type checkers should eventually be able to find the problems that Schemenauer is trying to address. He also thought that converting byte arrays using Latin-1 would be worse than the implicit ASCII conversion that Python 2 does.
But Schemenauer wondered when these tools would be
available. Python 3 was released in 2008, but the tools are still
lacking, he said. Coghlan pointed to a
few different things that can help with the porting process: the
-3 command-line option for warnings about Python 2 code that
will be troublesome in Python 3, the python-future compatibility layer, and
a "minimalistic porting
guide
" that is being developed by Red Hat's Python team. There
is the official porting
guide as well.
As is often the case on python-ideas, there were a few speculative sub-threads on
specific portions of the kind of code that Schemenauer and others wanted to catch.
In particular, the behavior of indexing byte strings was discussed. The current Python 3 behavior
that returns an integer (rather than a one-byte string) is "widely
considered a mistake
", as Guido van Rossum put it, but is not something he is willing to
see deprecated anytime soon. Various ways to remove that behavior were
considered,
though there is already PEP 467, which
would address some of the difficulties.
Paul Moore was not sure about the value proposition of a transitional interpreter as Schemenauer described. He suggested that the costs of porting at all may still outweigh the benefits. In addition, he wondered who would develop it:
Schemenauer agreed that most would probably put
their effort into simply porting their code, but he also pointed to some
work he had done on what he calls "Pragmatic Python". Since he
had "some basic working code
", it was more than simply an
idea, so he started a thread on the
python-dev mailing list about it:
- code coming out of 2to3 runs correctly on this modified Python
- code that runs without warnings on this modified Python will run correctly on Python 3.x.
His suggestion that other names, such as "Perverted,
Debauched or Impure Python
", might suit it better than "Pragmatic"
led to the inevitable bikeshed sub-thread.
Others, though, took exception to recommending the 2to3 tool
for porting. Victor Stinner suggested using 2to6, modernize, or his own sixer instead. 2to3 is mainly aimed
at code that targets Python 3 only, whereas the other tools help
maintain compatibility with Python 2.x (where x is usually 6 and/or 7).
Coghlan gave a list of the steps to take when porting code that still needs to run under both versions of Python:
- update your code with modernize or futurize
- check it still runs on Python 2.7
- check it doesn't generate warnings under 2.7's "-3" switch
- check it passes "pylint --py3k"
- check if it runs on Python 3.5
To that, Brett Cannon, who wrote the official porting guide, added:
"'python3.5 -bb' is best to help keep Python 2.7
compatibility
". The -bb switch will cause Python to emit
errors when it finds comparisons between bytes and strings or bytes and
integers, rather than simply returning False. But Schemenauer was not convinced; he posted a short program that
would issue a traceback under Python 3, but would instead emit
warnings under Pragmatic Python.
There was a bit of miscommunication along the way, but Cannon and Coghlan were really not trying to deter Schemenauer at all. Both were simply trying to have him look at the other options out there and to possibly improve those, rather than focus on 2to3. Schemenauer agreed that 2to6, modernize, or sixer might be a better way forward, rather than starting from code generated by 2to3, but he still sees value in the transitional interpreter:
More warnings for the -3 command-line option in Python 2.7 would be welcome, Cannon and Coghlan said. Schemenauer thought that made sense, though nothing he had built so far would really qualify, but there is a bigger problem than that:
Likewise, a warning about comparison behavior (None, default ordering of types) would also not be useful because there is so much standard library code that would spew warnings.
That, along with the idea that Pragmatic Python would just be a tool to use as part of the porting process, rather than a crutch to allow imperfectly ported code to be run in production, seems to have changed Coghlan's thinking. He has expressed support for the idea, while Cannon mentioned possibly adding Pragmatic Python as an option in the porting guide.
It is a clever idea to use the existing Python 3 interpreter as an aid toward making more code run with that version. Adding Schemenauer's changes to the mainline interpreter, though, would open up a whole raft of support issues, not to mention they would have a rough time ever passing muster with the core developers. "Forking" Python 3 means that these kinds of porting aids can be made available—hopefully without giving anyone the impression they can run their production code on it. It is yet another demonstration of the power of open source.
Brief items
Quotes of the week
Libinput is done
At his blog, libinput maintainer Peter Hutterer announced
that with the recent version 1.4 release, the libinput team has
"now finished the TODO list we had when libinput was first
conceived.
" In addition to marking that occasion with a
clickbait headline, Hutterer goes on to review the full libinput
feature set and its impact on other system components like display
managers. "The hard work doesn't stop of course, there are
still plenty of areas where we need to be better. And of course, new
features come as HW manufacturers bring out new hardware. I already
have touch arbitration on my todo list. But it's nice to wave at this
big milestone as we pass it into the way to the glorious future of
perfect, bug-free input.
" Hutterer also takes time to thank a
long list of libinput contributors for their work.
GammaRay 2.5 is available
Version 2.5 of GammaRay, the inspection tool for Qt applications,
has been released. This
update adds support for Qt 5.7, in particular for the recently added
Qt 3D module. Other changes include access to "ML context
property chains and type information, object instance statistics,
support for inspecting networking and SSL classes, and runtime
switchable logging categories.
" Several existing features were
also improved, such as object and source-code navigation.
OpenVZ 7.0 released
OpenVZ 7.0 has been released. The new release focuses on merging OpenVZ and Virtuozzo source codebase and replacing its hypervisor with KVM. There are many other improvements and new features in container management and more.systemd 231 is available
Version 231 of systemd has been released. Changes include the ability for services to alter the service watchdog timeout at runtime, support for percentage units in MemoryLimit and several other settings, and several new services. In addition, systemd will now log all service processes that it kills forcibly when they fail to shut down cleanly.
Newsletters and articles
Development newsletters from the past week
- What's cooking in git.git (July 25)
- This Week in GTK+ (July 25)
- OpenStack Developer Digest (July 25)
- Perl Weekly (July 25)
- PostgreSQL Weekly News (July 24)
- Python Weekly (July 21)
- Ruby Weekly (July 21)
- This Week in Rust (July 26)
- Tahoe-LAFS Weekly News (July 21)
- Wikimedia Tech News (July 25)
Sitter: Snappy sprint reporty musing
Harald Sitter reports on a discussion at recent sprint focused on making Snap packaging useful for KDE. "Shipping things users can use on Linux has been a pain in the rear since forever and these bundles are meant to change that. As such we as KDE should have a strong interest and presence in this field in the hopes of shaping a future that is useful to us. After all, we are one of the biggest source distributors, and the primary reason we don't also offer generic binary packages of our applications is because this never scaled and was altogether terrible to pull off from a KDE point of view." He and Scarlett Clark are working on some high level mass automation of snap building on top of KDE Neon's existing deb binaries. (Thanks to Jos van den Oever)
Clasen: Using modern gettext
At his blog, Matthias Clasen explores
the recent enhancements to the the classic GNU gettext utility.
Thanks in large part to new maintainer Daiki Ueno, gettext now
understands many more file formats—thus enabling developers to easily
extract strings from a wide variety of source files for translation.
In addition to programming languages, Clasen notes, gettext
understands .desktop files, GSettings schemas, GtkBuilder ui files,
and Appdata files. "If you don’t want to wait for your favorite format to come with built-in its support, you can also include its files with your application; gettext will look for such files in $XDG_DATA_DIRS/gettext/its/.
"
Page editor: Nathan Willis
Next page:
Announcements>>
