This page is primarily intended for Mercurial's developers.
This is a status page for keeping track of what needs to be done to make progress on Mercurial on Python 3. Our current aim is to support Python 3.5.
hg version, hg debuginstall, hg init, hg files, hg manifest, hg log, hg diff, hg export, hg status, hg summary, hg config, hg identify, hg update, hg commit, hg branches, hg bookmarks works on Python 3 without using any out of core extensions. These won't work for you if you have out of core extensions enabled. There are certain things which don't work yet with these commands like revset, templates.
We have many tests passing on Python 3. You can have a look at them in python3-whitelist
We will be happy to review patches and speed up the work related to Python 3. Before you start there are few things related to current porting and how things work currently. Most of our efforts are to make sure have Python 2 compatibility intact while making Python 3 run.
- We have a source transformer which does following things on Python 3.
It adds b'' in front of string starting with ' or " and not having any u'', r'' or b'' in front.
Adds this line from mercurial.pycompat import delattr, getattr, hasattr, setattr, xrange, open on every python file.
Converts every occurrence of iteritems to items on Python 3.
Converts argument of *attr and encode, decode to unicodes by adding u''.
The transformer currently works on mercurial/, hgext/ and hgext3rd/.
The transformer code lies here and you can also use transformer on your .py files by adding them in the transformer.
Due to everything is unicodes by default in Python 3, and we need to rely on bytes, we have pycompat.py which contains hacks related to various functions of os module on Python.
We also have encoding.environ which helps us using a bytes version of os.environ on both Python 2 and 3.
We are also adding r'' at some places to make it a raw string.
There are currently two tests which are based on Python 3 compatibility and few checks in our linter test-check-code.t to make sure new patches include things from pycompat.py.
- Encoding issues are generally uncovered by our tests (as everything was byte string on Python 2.)
How to start
- You can always set up a virtual environment and run Mercurial in it, but we have a easier way around.
Have Python 3.5 installed and say you have it in variable python3.5.
Run hgrepo$ python3.5 hg version. That must work, if not check that you should not have out of tree extensions enabled.
- Now you can run any hg command this way and test if it's working or not.
Pure-python tests are sometimes easier to port, but often need to be ported to use unittest first instead of our legacy testing system. The first step in migrating such tests to Python 3 involves porting to unittest, followed by any necessary followups to fix issues on Python 3. A list of tests that probably still need this work done can be obtained by running comm -23 <(hg files 'set:tests/test*py - grep(unittest)' | sed 's$tests/$$') contrib/python3-whitelist.
The practice we follow now is run commands which are not yet fixed and try to fix the exceptions raised. So our current approach is exception based.