Differences between revisions 6 and 23 (spanning 17 versions)
Revision 6 as of 2015-10-12 17:16:21
Size: 1186
Editor: AugieFackler
Comment:
Revision 23 as of 2017-05-29 10:24:51
Size: 3636
Editor: PulkitGoyal
Comment:
Deletions are marked like this. Additions are marked like this.
Line 5: Line 5:

To see a summary of our current position on Python 3 support, see [[SupportedPythonVersions#Python_3.x_support]].

This is a status page for keeping track of what needs to be done to make progress on Mercurial on Python 3. Nobody is actively working on this - AugieFackler works on it sporadically, and would be happy to see patches on this topic flagged with Py3 on the mailing list. The work in progress is visible at http://hg.durin42.com/hg-py3k/ - note that ChangesetEvolution is in use on that repository, and so hashes will change over time as the patchset is rebased.

The most significant problem at the moment is some lingering cyclic imports in the codebase:

==== mercurial.repoview -> mercurial.revset -> mercurial.repoview ====
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.
Line 15: Line 8:
`hg version`, `hg debuginstall`, `hg init`, `hg files`, `hg manifest`, `hg log`, `hg diff`, `hg export`, `hg init`, `hg status`, `hg summary`, `hg config` 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.
Line 16: Line 10:
run-tests.py can pass its own tests (test-run-tests.py and test-run-tests.t) under 2.6, 2.7 and 3.5 as of [[https://hg.python.org/cpython/rev/1764d42b340d|this change in cpython]]. The series will be submitted for review to hg once we're ready to drop Python 2.4 and 2.5. == Contributing ==
Line 18: Line 12:
== Next Steps == 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.
Line 20: Line 14:
Get the entire codebase working with 'from __future__ import absolute_import'.  * We have a source transformer which does following things on Python 3.
    1. It adds b'' in front of string starting with "'" or '"' and not having any u'', r'' or b'' in front.
    2. Adds this line `from mercurial.pycompat import delattr, getattr, hasattr, setattr, xrange, open` on every python file.
    3. Converts every occurrence of `iteritems` to `items` on Python 3.
    4. Converts argument of *attr and encode, decode to unicodes by adding u''.
    5. The transformer currently works on `mercurial/, hgext/ and hgext3rd/`.
    6. The transformer code lies [[https://www.mercurial-scm.org/repo/hg/file/295625f1296b/mercurial/__init__.py#l124|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 [[https://www.mercurial-scm.org/repo/hg/file/tip/mercurial/pycompat.py|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 [[https://www.mercurial-scm.org/repo/hg/file/tip/tests/test-check-code.t|test-check-code.t]] to make sure new patches include things from `pycompat.py`.

    1. [[https://www.mercurial-scm.org/repo/hg/file/tip/tests/test-check-py3-compat.t|test-check-py3-compat.t]] : This test was used initially to fix a lot of things, not very much helpful now.
    2. [[https://www.mercurial-scm.org/repo/hg/file/tip/tests/test-py3-commands.t|test-py3-commands.t]]: This test lists commands which actually works on Python 3. If you want an updated list anytime, the test is the best place to look for.

 * 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.
    1. Clone the [[https://www.mercurial-scm.org/repo/hg|main repo]] or [[https://www.mercurial-scm.org/repo/hg-committed/|committed-repo]] and say it's in folder name `hgrepo`.
    2. Have Python 3.5 installed and say you have it in variable `python3.5`.
    3. Run `hgrepo$ python3.5 hg version`. That must work, if not check that you should not have out of tree extensions enabled.
    4. Now you can run any hg command this way and test if it's working or not.

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.

Note:

This page is primarily intended for developers of Mercurial.

Python 3

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.

1. What Works

hg version, hg debuginstall, hg init, hg files, hg manifest, hg log, hg diff, hg export, hg init, hg status, hg summary, hg config 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.

2. Contributing

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.
    1. It adds b in front of string starting with "'" or '"' and not having any u, r or b in front.

    2. Adds this line from mercurial.pycompat import delattr, getattr, hasattr, setattr, xrange, open on every python file.

    3. Converts every occurrence of iteritems to items on Python 3.

    4. Converts argument of *attr and encode, decode to unicodes by adding u.

    5. The transformer currently works on mercurial/, hgext/ and hgext3rd/.

    6. 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.

    1. test-check-py3-compat.t : This test was used initially to fix a lot of things, not very much helpful now.

    2. test-py3-commands.t: This test lists commands which actually works on Python 3. If you want an updated list anytime, the test is the best place to look for.

  • Encoding issues are generally uncovered by our tests (as everything was byte string on Python 2.)

3. How to start

  • You can always set up a virtual environment and run Mercurial in it, but we have a easier way around.
    1. Clone the main repo or committed-repo and say it's in folder name hgrepo.

    2. Have Python 3.5 installed and say you have it in variable python3.5.

    3. Run hgrepo$ python3.5 hg version. That must work, if not check that you should not have out of tree extensions enabled.

    4. Now you can run any hg command this way and test if it's working or not.

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.

Python3 (last edited 2023-02-19 16:08:38 by AntonShestakov)