Differences between revisions 16 and 55 (spanning 39 versions)
Revision 16 as of 2017-03-11 21:01:38
Size: 2564
Editor: PulkitGoyal
Comment:
Revision 55 as of 2023-02-19 16:08:38
Size: 4267
Comment: python 3.5
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
#pragma section-numbers 2
Line 5: Line 7:
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. This is a status page for keeping track of what needs to be done to make progress on Mercurial on Python 3.
Line 7: Line 9:
== What Works ==
`hg version` works on Python 3 without using any out of core extensions. `hg debuginstall` is our next goal and it's nearly complete. These won't work for you if you have out of core extensions enabled.
<<TableOfContents>>
Line 10: Line 11:
== Contributing == == Status ==
Line 12: Line 13:
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. '''Support for Python 2 has been dropped for the Mercurial 6.2 release. 6.1.x is the last series to support Python 2.'''
Line 14: Line 15:
 * 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/tip/mercurial/__init__.py#l124|here]] and you can also use transformer on your .py files by adding them in the transformer.
'''Support for Python 3.5 has been dropped for the Mercurial 6.2 release. 6.1.x is the last series to support Python 3.5.'''
Line 22: Line 17:
 * 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. Mercurial 5.2 was the first release that officially had support for Python 3. Currently supported Python 3 versions are 3.6 and later. Mercurial with Python 3 on Windows is tested regularly, but there remains some known issues outside of the core.
Line 24: Line 19:
 * We also have `encoding.environ` which helps us using a bytes version of `os.environ` on both Python 2 and 3. It is the project policy for Mercurial and its core extensions to be compatible with Python 3. Over 99% of tests pass with Python 3 and test regressions are treated seriously.
Line 26: Line 21:
 * We are also adding r'' at some places to make it a raw string. Most used 3rd party extensions like evolve and topic have been ported to Python 3. There are some which have not yet been ported.
Line 28: Line 23:
 * 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`. == Using ==
Line 30: Line 25:
    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-check-py3-commands.t|test-check-py3-commands.t]]: This test which things actually works on Python 3. You can see in the test that `hg version` runs on vanilla hg.
Since Mercurial 5.2, `setup.py` no longer refuses to run with Python 3.
`pip install Mercurial` or `python setup.py install` should work out of the box.
But be aware that Windows support is still considered a beta level.

No run-time environment variable or config option is required to use Python 3 with Mercurial: only the installation step / `setup.py` requires special action to override the Python version check.

== Things need to be investigated ==

 * Windows encoding changes (fixed in 5.3)
 https://docs.python.org/3/whatsnew/3.6.html#pep-529-change-windows-filesystem-encoding-to-utf-8

 Probably need to call {{{sys._enablelegacywindowsfsencoding()}}} at startup.
 * Lazy importer performance overhead. Our custom importer on Python 2 always returns a stub module during ``import``. Python 3's does I/O to verify the module exists then returns a lazy module that is loaded when first accessed. In addition to behavior differences, the I/O may contribute sufficient performance overhead to constitute a problem.
 * A mechanism for extensions to advertise that they are Python 3 compatible. Nearly every extension will break in Python 3. We may want a mechanism that requires extensions to self-declare that they are Python 3 compatible - possibly via special syntax in their source code or the presence of a well-named variable. It might have to be at the source level because Python 3 would need to evaluate code in order to obtain the value of a module-level variable.

== Porting Extensions to Python 3 ==

'''Note Feb 2022:''' most major extensions have been ported to Python 3 a while back.

Nearly every extension will need to be ported to be compatible with Python 3. This is because of fundamental differences between Python 2 and Python 3.

The source code for Mercurial extensions will need to be Python 3 native and will need to be compatible with Mercurial's APIs. In many cases, existing source code will compile on Python 3 but will fail at run-time. Sources of run-time errors include:

 * Use of `str` instead of `bytes`. Mercurial uses `bytes` (`b''` strings) in almost all of its APIs and data structures. This is in contrast to much Python code, which uses `str` and `''` strings. It is common for extensions to `b''` prefix most strings in order to remain compatible with Mercurial.
 * Use of `iteritems()`, `iterkeys()`, etc. These methods from core data structures do not exist in Python 3.
 * Import of renamed modules. Python 3 refactored the locations of various modules in the Python standard library. Extensions may need to take this into account.

Do an Internet search for ''Python 3 porting'' to find well-written and comprehensive guides on generically porting code to Python 3.

Extension authors may find the [[https://www.mercurial-scm.org/repo/hg-committed/file/tip/mercurial/pycompat.py|mercurial.pycompat]] module useful. This modules contains abstractions and utilities for bridging the differences between Python 2 and 3. It is conceptually similar to the `six` Python module.

If you need help or guidance on porting extensions, you can message on IRC or the development MailingList. We will be happy to help you.

----
CategoryAudit

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.

1. Status

Support for Python 2 has been dropped for the Mercurial 6.2 release. 6.1.x is the last series to support Python 2.

Support for Python 3.5 has been dropped for the Mercurial 6.2 release. 6.1.x is the last series to support Python 3.5.

Mercurial 5.2 was the first release that officially had support for Python 3. Currently supported Python 3 versions are 3.6 and later. Mercurial with Python 3 on Windows is tested regularly, but there remains some known issues outside of the core.

It is the project policy for Mercurial and its core extensions to be compatible with Python 3. Over 99% of tests pass with Python 3 and test regressions are treated seriously.

Most used 3rd party extensions like evolve and topic have been ported to Python 3. There are some which have not yet been ported.

2. Using

Since Mercurial 5.2, setup.py no longer refuses to run with Python 3. pip install Mercurial or python setup.py install should work out of the box. But be aware that Windows support is still considered a beta level.

No run-time environment variable or config option is required to use Python 3 with Mercurial: only the installation step / setup.py requires special action to override the Python version check.

3. Things need to be investigated

  • Windows encoding changes (fixed in 5.3)

    https://docs.python.org/3/whatsnew/3.6.html#pep-529-change-windows-filesystem-encoding-to-utf-8

    Probably need to call sys._enablelegacywindowsfsencoding() at startup.

  • Lazy importer performance overhead. Our custom importer on Python 2 always returns a stub module during import. Python 3's does I/O to verify the module exists then returns a lazy module that is loaded when first accessed. In addition to behavior differences, the I/O may contribute sufficient performance overhead to constitute a problem.

  • A mechanism for extensions to advertise that they are Python 3 compatible. Nearly every extension will break in Python 3. We may want a mechanism that requires extensions to self-declare that they are Python 3 compatible - possibly via special syntax in their source code or the presence of a well-named variable. It might have to be at the source level because Python 3 would need to evaluate code in order to obtain the value of a module-level variable.

4. Porting Extensions to Python 3

Note Feb 2022: most major extensions have been ported to Python 3 a while back.

Nearly every extension will need to be ported to be compatible with Python 3. This is because of fundamental differences between Python 2 and Python 3.

The source code for Mercurial extensions will need to be Python 3 native and will need to be compatible with Mercurial's APIs. In many cases, existing source code will compile on Python 3 but will fail at run-time. Sources of run-time errors include:

  • Use of str instead of bytes. Mercurial uses bytes (b'' strings) in almost all of its APIs and data structures. This is in contrast to much Python code, which uses str and '' strings. It is common for extensions to b'' prefix most strings in order to remain compatible with Mercurial.

  • Use of iteritems(), iterkeys(), etc. These methods from core data structures do not exist in Python 3.

  • Import of renamed modules. Python 3 refactored the locations of various modules in the Python standard library. Extensions may need to take this into account.

Do an Internet search for Python 3 porting to find well-written and comprehensive guides on generically porting code to Python 3.

Extension authors may find the mercurial.pycompat module useful. This modules contains abstractions and utilities for bridging the differences between Python 2 and 3. It is conceptually similar to the six Python module.

If you need help or guidance on porting extensions, you can message on IRC or the development MailingList. We will be happy to help you.


CategoryAudit

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