changeset 43437:93f74a7d3f07

merge with stable
author Martin von Zweigbergk <martinvonz@google.com>
date Tue, 05 Nov 2019 13:19:24 -0800
parents bfc68404cccd (current diff) 93aeebc90ff9 (diff)
children a77d4fe347a4
files mercurial/commands.py mercurial/pycompat.py
diffstat 89 files changed, 1699 insertions(+), 5050 deletions(-) [+]
line wrap: on
line diff
--- a/.hgsigs	Mon Nov 04 00:16:44 2019 +0100
+++ b/.hgsigs	Tue Nov 05 13:19:24 2019 -0800
@@ -185,3 +185,5 @@
 e91930d712e8507d1bc1b2dffd96c83edc4cbed3 0 iQJEBAABCAAuFiEEK8zhT1xnJaouqK63ucncgkqlvdUFAl1DD/sQHHJhZkBkdXJpbjQyLmNvbQAKCRC5ydyCSqW91bvmD/4/QDZZGVe+WiMUxbT+grfFjwjX4nkg7Vt+6vQbjN68NC5XpSiCzW8uu0LRemX0KJKoOfQxqHk3YKkZZHIk10Fe6RSLWt8dqlfa2J9B2U8DwMEBykCOuxcLlDe7DGaaMXlXXRhNXebRheNPLeNe+r7beMAAjwchTIIJD5xcFnPRFR0nN7Vj7eRUdWIQ9H/s7TolPz1Mf7IWqapLjPtofiwSgtRoXfIAkuuabnE4eMVJ8rsLwcuMhxWP2zjEfEg68YkiGBAFmlnRk+3lJpiB9kVapB3cWcsWv2OBhz0D3NgGp82eWkjJCZZhZ+zHHrQ6L9zbiArzW9NVvPEAKLbl3XUhFUzFTUD+S38wsYLYL5RkzhlCI2/K1LJLOtj7r0Seen0v8X842p0cXmxTg/o1Vg3JOm04l9AwzCsnqwIqV7Ru//KPqH91MFFH6T6tbfjtLHRmjxRjMZmVt7ZQjS84opVCZwgUTZZJB2kd1goROjdowQVK6qsEonlzGjWb9zc3el5L9uzDeim3e5t2GNRVt8veQaLc+U2hHWniVsDJMvqp2Hr9IWUKp+bu/35B1nElvooS40gj2WhkfkCbbXSg9qnVLwGxxcGdF28Z0nhQcfKiJAc+8l9l19GNhdKxOi4zUXlp90opPWfT7wGQmysvTjQeFL2zX9ziuHUZZwlW1YbeMQ==
 a4e32fd539ab41489a51b2aa88bda9a73b839562 0 iQJEBAABCAAuFiEEK8zhT1xnJaouqK63ucncgkqlvdUFAl1xTxUQHHJhZkBkdXJpbjQyLmNvbQAKCRC5ydyCSqW91ZQgD/96mViQ6fEh84l4XyAlY6Dq3SgMqEXttsUpk/GPoW4ykDFKN6VoiOaPoyNODO/46V3yeAjYjy3vX7Ua4/MY1NlnNoliQcTYtRV3SlDdoueTPOLfO6YSV27LG+dX/HYvPc/htCVmIVItU1JL+KEpXnv+bT50Bk+m6OgzfJMDzdHQ5ICImT8gW7UXlH/mlNtWMOrJDk3cArGhGs/pTFVrfgRTfDfDGSA9xW0/QvsNI5iwZHgMYaqoPFDnw6d/NXWRlk77KNiXkBEOKHf6UEWecMKmiSCm8RePSiX9ezqdcBAHygOg4KUeiR2kPNl4QJtskyG4CwWxlmGlfgKx07s7rGafE+DWLEYC9Wa8qK6/LPiowm17m/UlAYxdFXaBCiN0wgEw7oNmjcx/791ez+CL1+h6pd0+iSVI4bO9/YZ8LPROYef18MFm+IFIDIOgZU4eUbpBrzBb3IM1a519xgnmWXAjtRtGWEZMuHaSoLJf2pDXvaUPX6YpJeqCBFO3q/swbiJsQsy6xRW0Dwtn7umU1PGdmMoTnskTRKy9Kgzv7lf/nsUuRbzzM4ut9m1TOo27AulObMrmQB4YvLi/LEnYaRNx18yaqOceMxb/mS0tHLgcZToy9rTV+vtC21vgwfzGia2neLLe50tnIsBPP/AdTOw9ZDMRfXMCajWM22hPxvnGcw==
 181e52f2b62f4768aa0d988936c929dc7c4a41a0 0 iQJEBAABCAAuFiEEK8zhT1xnJaouqK63ucncgkqlvdUFAl2UzlMQHHJhZkBkdXJpbjQyLmNvbQAKCRC5ydyCSqW91SDzD/0YZqtN+LK5AusJjWaTa61DRIPhJQoZD+HKg4kAzjL8zw8SxBGLxMZkGmve9QFMNzqIr5kkPk6yEKrEWYqyPtpwrv5Xh5D4d8AKfphdzwSr+BvMk4fBEvwnBhrUJtKDEiuYQdbh4+OQfQs1c3xhtinjXn30160uzFvLQY6/h4hxai2XWj4trgoNXqPHDHlQKc6kRfPpmNO2UZhG+2Xfsava2JpcP4xA2R0XkI10be5MDoGU4AFCMUcXZzIto0DYT+HOezowoNpdC1EWVHfa+bdrlzHHO7WPaTLzEPy44/IhXmNhbwFKOk5RZ/qBADQvs9BDfmIDczOoZKTC5+ESZM0PR2np5t7+JFMUeeRcINqBdSc4Aszw3iHjgNbJJ3viU72JZvGGGd9MglP590tA0proVGxQgvXDq3mtq3Se5yOLAjmRnktW5Tnt8/Z3ycuZz+QsTEMXR5uIZvgz63ibfsCGTXFYUz9h7McGgmhfKWvQw9+MH6kRbE9U8qaUumgf4zi4HNzmf8AyaMJo07DIMwWVgjlVUdWUlN/Eg61fU3wC79mV8mLVsi5/TZ986obz4csoYSYXyyez5ScRji+znSw8vUx0YhoiOQbDms/y2QZR/toyon554tHkDZsya2lhpwXs8T0IFZhERXsmz/XmT3fWnhSzyrUe6VjBMep1zn6lvQ==
+59338f9561099de77c684c00f76507f11e46ebe8 0 iQJEBAABCAAuFiEEK8zhT1xnJaouqK63ucncgkqlvdUFAl2ty1MQHHJhZkBkdXJpbjQyLmNvbQAKCRC5ydyCSqW91XBUD/wJqwW0cuMCUvuUODLIfWa7ZxNl1mV9eW3tFQEuLGry97s12KDwBe0Erdjj7DASl4/6Xpc4PYxelZwSw4xT1UQg7wd/C3daCq/cDXrAkl7ZNTAHu6iAnHh25mOpIBfhMbh4j3YD0A2OoI17QGScU6S7Uv0Gz1CY20lJmEqsMzuuDPm2zrdPnTWffRUuPgskAg3czaw45Na7nUBeaxN1On0O5WqMYZsCGyi14g5S0Z0LHMKRJzc/s48JUTDjTbbzJ6HBxrxWTW2v8gN2J6QDYykcLBB9kV6laal9jhWs9n/w0yWwHfBfJ+E4EiMXeRdZgGA55OCOuDxnmmONs1/Z0WwPo+vQlowEnjDMT0jPrPePZ5P4BDXZD3tGsmdXDHM7j+VfDyPh1FBFpcaej44t84X1OWtAnLZ3VMPLwobz9MOzz4wr9UuHq23hus0Fen+FJYOAlTx9qPAqBrCTpGl+h1DMKD62D7lF8Z1CxTlqg9PPBB7IZNCXoN7FZ4Wfhv1AarMVNNUgBx6m0r6OScCXrluuFklYDSIZrfgiwosXxsHW27RjxktrV4O+J1GT/chLBJFViTZg/gX/9UC3eLkzp1t6gC6T9SQ+lq0/I+1/rHQkxNaywLycBPOG1yb/59mibEwB9+Mu9anRYKFNHEktNoEmyw5G9UoZhD+1tHt4tkJCwA==
+ca3dca416f8d5863ca6f5a4a6a6bb835dcd5feeb 0 iQJEBAABCAAuFiEEK8zhT1xnJaouqK63ucncgkqlvdUFAl3BrQ4QHHJhZkBkdXJpbjQyLmNvbQAKCRC5ydyCSqW91ZXjEACfBdZczf0a4bmeaaxRwxXAniSS4rVkF790g22fsvSZFvQEpmwqNtsvbTt3N1V2QSDSZyhBa+/qfpuZ689VXMlR3rcJOVjo/7193QLXHOPfRn7sDeeCxjsbtXXLbLa8UT56gtT5gUa4i0LC2kHBEi+UhV9EGgSaDTBxWUFJ9RY2sosy1XFiOUlkUoHUbqUF28J3/CxEXzULWkqTOPwh94JYsgXSSS69WNZEfsuEBSPCzn8Gd7z7lWudZ/VTZBTpTji7HQxpFtSZxNzpwmcmVOH9HlEKoA1K4JoR+1TMHqSytQXlz3FMF6c6Z1G+OPpwTGCjGTkB9ZAusP3gU8KIZTTEXthiEluRtnRq1yu4K2LTyY172JPJvANAWpVEvBvn4k5c9tDOEt9RCAPqCrgNGzDTrw02+gZyyNkjcS6hPn+cDJ6OQ1j2eCQtHlqfHLSc7FsRjUSTiKSEUTdWvHbNfOYe6Yth/tnQ7TnpnS9S0eiugFzZs2f8P85Gfa3uTFQIDm67Ud+8Yu1uOxa6bhECLaXEACnLofzz8sioLsJMiOoG2HmwhyPyfZUHXlb2zdsSP3LC+gKN39VvzSxhhjrIUJoM4ulP0GP1/lkMVzOady66iLaEwDvEn4FLmu395SubHwbre1Jx83hiCQpZfPkI0PhKnh4yVm+BRGUpX97rMTGjzw==
--- a/.hgtags	Mon Nov 04 00:16:44 2019 +0100
+++ b/.hgtags	Tue Nov 05 13:19:24 2019 -0800
@@ -198,3 +198,5 @@
 e91930d712e8507d1bc1b2dffd96c83edc4cbed3 5.1
 a4e32fd539ab41489a51b2aa88bda9a73b839562 5.1.1
 181e52f2b62f4768aa0d988936c929dc7c4a41a0 5.1.2
+59338f9561099de77c684c00f76507f11e46ebe8 5.2rc0
+ca3dca416f8d5863ca6f5a4a6a6bb835dcd5feeb 5.2
--- a/Makefile	Mon Nov 04 00:16:44 2019 +0100
+++ b/Makefile	Tue Nov 05 13:19:24 2019 -0800
@@ -183,16 +183,15 @@
   centos5 \
   centos6 \
   centos7 \
+  centos8 \
   deb \
   docker-centos5 \
   docker-centos6 \
   docker-centos7 \
+  docker-centos8 \
   docker-debian-jessie \
   docker-debian-stretch \
-  docker-fedora20 \
-  docker-fedora21 \
-  docker-fedora28 \
-  docker-fedora29 \
+  docker-fedora \
   docker-ubuntu-trusty \
   docker-ubuntu-trusty-ppa \
   docker-ubuntu-xenial \
@@ -201,10 +200,7 @@
   docker-ubuntu-artful-ppa \
   docker-ubuntu-bionic \
   docker-ubuntu-bionic-ppa \
-  fedora20 \
-  fedora21 \
-  fedora28 \
-  fedora29 \
+  fedora \
   linux-wheels \
   linux-wheels-x86_64 \
   linux-wheels-i686 \
--- a/black.toml	Mon Nov 04 00:16:44 2019 +0100
+++ b/black.toml	Tue Nov 05 13:19:24 2019 -0800
@@ -9,9 +9,7 @@
 | \.mypy_cache/
 | \.venv/
 | mercurial/thirdparty/
-| hgext/fsmonitor/pywatchman/
 | contrib/python-zstandard/
-| contrib/grey.py
 '''
 skip-string-normalization = true
 quiet = true
--- a/contrib/automation/hgautomation/aws.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/contrib/automation/hgautomation/aws.py	Tue Nov 05 13:19:24 2019 -0800
@@ -191,6 +191,10 @@
 $Setting = 'LocalAccountTokenFilterPolicy'
 Set-ItemProperty -Path $Key -Name $Setting -Value 1 -Force
 
+# Avoid long usernames in the temp directory path because the '~' causes extra quoting in ssh output
+[System.Environment]::SetEnvironmentVariable('TMP', 'C:\Temp', [System.EnvironmentVariableTarget]::User)
+[System.Environment]::SetEnvironmentVariable('TEMP', 'C:\Temp', [System.EnvironmentVariableTarget]::User)
+
 # Configure and restart the WinRM Service; Enable the required firewall exception
 Stop-Service -Name WinRM
 Set-Service -Name WinRM -StartupType Automatic
--- a/contrib/automation/hgautomation/linux.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/contrib/automation/hgautomation/linux.py	Tue Nov 05 13:19:24 2019 -0800
@@ -25,12 +25,12 @@
 }
 
 INSTALL_PYTHONS = r'''
-PYENV2_VERSIONS="2.7.16 pypy2.7-7.1.1"
-PYENV3_VERSIONS="3.5.7 3.6.9 3.7.4 3.8.0 pypy3.5-7.0.0 pypy3.6-7.1.1"
+PYENV2_VERSIONS="2.7.17 pypy2.7-7.2.0"
+PYENV3_VERSIONS="3.5.7 3.6.9 3.7.5 3.8.0 pypy3.5-7.0.0 pypy3.6-7.2.0"
 
 git clone https://github.com/pyenv/pyenv.git /hgdev/pyenv
 pushd /hgdev/pyenv
-git checkout d6d6bc8bb08bcdcbf4eb79509aa7061011ade1c4
+git checkout 0e7cfc3b3d4eca46ad83d632e1505f5932cd179b
 popd
 
 export PYENV_ROOT="/hgdev/pyenv"
@@ -266,6 +266,7 @@
     python3-fuzzywuzzy \
     python3-pygments \
     python3-vcr \
+    python3-venv \
     rsync \
     sqlite3 \
     subversion \
--- a/contrib/automation/linux-requirements-py3.txt	Mon Nov 04 00:16:44 2019 +0100
+++ b/contrib/automation/linux-requirements-py3.txt	Tue Nov 05 13:19:24 2019 -0800
@@ -4,10 +4,25 @@
 #
 #    pip-compile --generate-hashes --output-file=contrib/automation/linux-requirements-py3.txt contrib/automation/linux-requirements.txt.in
 #
+appdirs==1.4.3 \
+    --hash=sha256:9e5896d1372858f8dd3344faf4e5014d21849c756c8d5701f78f8a103b372d92 \
+    --hash=sha256:d8b24664561d0d34ddfaec54636d502d7cea6e29c3eaf68f3df6180863e2166e \
+    # via black
 astroid==2.2.5 \
     --hash=sha256:6560e1e1749f68c64a4b5dee4e091fce798d2f0d84ebe638cf0e0585a343acf4 \
     --hash=sha256:b65db1bbaac9f9f4d190199bb8680af6f6f84fd3769a5ea883df8a91fe68b4c4 \
     # via pylint
+attrs==19.3.0 \
+    --hash=sha256:08a96c641c3a74e44eb59afb61a24f2cb9f4d7188748e76ba4bb5edfa3cb7d1c \
+    --hash=sha256:f7b7ce16570fe9965acd6d30101a28f62fb4a7f9e926b3bbc9b61f8b04247e72 \
+    # via black
+black==19.10b0 ; python_version >= "3.6" and platform_python_implementation != "PyPy" \
+    --hash=sha256:1b30e59be925fafc1ee4565e5e08abef6b03fe455102883820fe5ee2e4734e0b \
+    --hash=sha256:c2edb73a08e9e0e6f65a0e6af18b059b8b1cdd5bef997d7a0b181df93dc81539
+click==7.0 \
+    --hash=sha256:2335065e6395b9e67ca716de5f7526736bfa6ceead690adf616d925bdc622b13 \
+    --hash=sha256:5b94b49521f6456670fdb30cd82a4eca9412788a93fa6dd6df72c94d5a8ff2d7 \
+    # via black
 docutils==0.15.2 \
     --hash=sha256:6c4f696463b79f1fb8ba0c594b63840ebd41f059e92b31957c46b74a4599b6d0 \
     --hash=sha256:9e4d7ecfc600058e07ba661411a2b7de2fd0fafa17d1a7f7361cd47b1175c827 \
@@ -78,6 +93,9 @@
     --hash=sha256:d3be11ac43ab1a3e979dac80843b42226d5d3cccd3986f2e03152720a4297cd7 \
     --hash=sha256:db603a1c235d110c860d5f39988ebc8218ee028f07a7cbc056ba6424372ca31b \
     # via yarl
+pathspec==0.6.0 \
+    --hash=sha256:e285ccc8b0785beadd4c18e5708b12bb8fcf529a1e61215b3feff1d1e559ea5c \
+    # via black
 pyflakes==2.1.1 \
     --hash=sha256:17dbeb2e3f4d772725c777fabc446d5634d1038f234e77343108ce445ea69ce0 \
     --hash=sha256:d976835886f8c5b31d47970ed689944a0262b5f3afa00a5a7b4dc81e5449f8a2
@@ -104,10 +122,27 @@
     --hash=sha256:bf47c0607522fdbca6c9e817a6e81b08491de50f3766a7a0e6a5be7905961b41 \
     --hash=sha256:f81025eddd0327c7d4cfe9b62cf33190e1e736cc6e97502b3ec425f574b3e7a8 \
     # via vcrpy
+regex==2019.11.1 \
+    --hash=sha256:15454b37c5a278f46f7aa2d9339bda450c300617ca2fca6558d05d870245edc7 \
+    --hash=sha256:1ad40708c255943a227e778b022c6497c129ad614bb7a2a2f916e12e8a359ee7 \
+    --hash=sha256:5e00f65cc507d13ab4dfa92c1232d004fa202c1d43a32a13940ab8a5afe2fb96 \
+    --hash=sha256:604dc563a02a74d70ae1f55208ddc9bfb6d9f470f6d1a5054c4bd5ae58744ab1 \
+    --hash=sha256:720e34a539a76a1fedcebe4397290604cc2bdf6f81eca44adb9fb2ea071c0c69 \
+    --hash=sha256:7caf47e4a9ac6ef08cabd3442cc4ca3386db141fb3c8b2a7e202d0470028e910 \
+    --hash=sha256:c31eaf28c6fe75ea329add0022efeed249e37861c19681960f99bbc7db981fb2 \
+    --hash=sha256:c7393597191fc2043c744db021643549061e12abe0b3ff5c429d806de7b93b66 \
+    --hash=sha256:d2b302f8cdd82c8f48e9de749d1d17f85ce9a0f082880b9a4859f66b07037dc6 \
+    --hash=sha256:e3d8dd0ec0ea280cf89026b0898971f5750a7bd92cb62c51af5a52abd020054a \
+    --hash=sha256:ec032cbfed59bd5a4b8eab943c310acfaaa81394e14f44454ad5c9eba4f24a74 \
+    # via black
 six==1.12.0 \
     --hash=sha256:3350809f0555b11f552448330d0b52d5f24c91a322ea4a15ef22629740f3761c \
     --hash=sha256:d16a0141ec1a18405cd4ce8b4613101da75da0e9a7aec5bdd4fa804d0e0eba73 \
     # via astroid, vcrpy
+toml==0.10.0 \
+    --hash=sha256:229f81c57791a41d65e399fc06bf0848bab550a9dfd5ed66df18ce5f05e73d5c \
+    --hash=sha256:235682dd292d5899d361a811df37e04a8828a5b1da3115886b73cf81ebc9100e \
+    # via black
 typed-ast==1.4.0 ; python_version >= "3.0" and platform_python_implementation != "PyPy" \
     --hash=sha256:18511a0b3e7922276346bcb47e2ef9f38fb90fd31cb9223eed42c85d1312344e \
     --hash=sha256:262c247a82d005e43b5b7f69aff746370538e176131c32dda9cb0f324d27141e \
@@ -146,4 +181,4 @@
 
 # WARNING: The following packages were not pinned, but pip requires them to be
 # pinned when the requirements file includes hashes. Consider using the --allow-unsafe flag.
-# setuptools==41.0.1        # via python-levenshtein
+# setuptools==41.6.0        # via python-levenshtein
--- a/contrib/automation/linux-requirements.txt.in	Mon Nov 04 00:16:44 2019 +0100
+++ b/contrib/automation/linux-requirements.txt.in	Tue Nov 05 13:19:24 2019 -0800
@@ -1,3 +1,5 @@
+# black pulls in typed-ast, which doesn't install on PyPy.
+black ; python_version >= '3.6' and platform_python_implementation != 'PyPy'
 # Bazaar doesn't work with Python 3 nor PyPy.
 bzr ; python_version <= '2.7' and platform_python_implementation == 'CPython'
 docutils
--- a/contrib/byteify-strings.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/contrib/byteify-strings.py	Tue Nov 05 13:19:24 2019 -0800
@@ -339,7 +339,7 @@
 
 
 if __name__ == '__main__':
-    if sys.version_info.major < 3:
-        print('This script must be run under Python 3.')
+    if sys.version_info[0:2] < (3, 7):
+        print('This script must be run under Python 3.7+')
         sys.exit(3)
     main()
--- a/contrib/examples/fix.hgrc	Mon Nov 04 00:16:44 2019 +0100
+++ b/contrib/examples/fix.hgrc	Tue Nov 05 13:19:24 2019 -0800
@@ -5,11 +5,5 @@
 rustfmt:command = rustfmt {rootpath}
 rustfmt:pattern = set:**.rs
 
-# We use black, but currently with
-# https://github.com/psf/black/pull/826 applied. For now
-# contrib/grey.py is our fork of black. You need to pip install
-# git+https://github.com/python/black/@d9e71a75ccfefa3d9156a64c03313a0d4ad981e5
-# to have the dependencies for grey.
-#
-# black:command = python3.7 contrib/grey.py --config=black.toml -
-# black:pattern = set:**.py - hgext/fsmonitor/pywatchman/** - mercurial/thirdparty/** - "contrib/python-zstandard/** - contrib/grey.py"
+black:command = black --config=black.toml -
+black:pattern = set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"
--- a/contrib/grey.py	Mon Nov 04 00:16:44 2019 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,4094 +0,0 @@
-# no-check-code because 3rd party
-import ast
-import asyncio
-from concurrent.futures import Executor, ProcessPoolExecutor
-from contextlib import contextmanager
-from datetime import datetime
-from enum import Enum
-from functools import lru_cache, partial, wraps
-import io
-import itertools
-import logging
-from multiprocessing import Manager, freeze_support
-import os
-from pathlib import Path
-import pickle
-import re
-import signal
-import sys
-import tempfile
-import tokenize
-import traceback
-from typing import (
-    Any,
-    Callable,
-    Collection,
-    Dict,
-    Generator,
-    Generic,
-    Iterable,
-    Iterator,
-    List,
-    Optional,
-    Pattern,
-    Sequence,
-    Set,
-    Tuple,
-    TypeVar,
-    Union,
-    cast,
-)
-
-from appdirs import user_cache_dir
-from attr import dataclass, evolve, Factory
-import click
-import toml
-from typed_ast import ast3, ast27
-
-# lib2to3 fork
-from blib2to3.pytree import Node, Leaf, type_repr
-from blib2to3 import pygram, pytree
-from blib2to3.pgen2 import driver, token
-from blib2to3.pgen2.grammar import Grammar
-from blib2to3.pgen2.parse import ParseError
-
-__version__ = '19.3b1.dev95+gdc1add6.d20191005'
-
-DEFAULT_LINE_LENGTH = 88
-DEFAULT_EXCLUDES = (
-    r"/(\.eggs|\.git|\.hg|\.mypy_cache|\.nox|\.tox|\.venv|_build|buck-out|build|dist)/"
-)
-DEFAULT_INCLUDES = r"\.pyi?$"
-CACHE_DIR = Path(user_cache_dir("black", version=__version__))
-
-
-# types
-FileContent = str
-Encoding = str
-NewLine = str
-Depth = int
-NodeType = int
-LeafID = int
-Priority = int
-Index = int
-LN = Union[Leaf, Node]
-SplitFunc = Callable[["Line", Collection["Feature"]], Iterator["Line"]]
-Timestamp = float
-FileSize = int
-CacheInfo = Tuple[Timestamp, FileSize]
-Cache = Dict[Path, CacheInfo]
-out = partial(click.secho, bold=True, err=True)
-err = partial(click.secho, fg="red", err=True)
-
-pygram.initialize(CACHE_DIR)
-syms = pygram.python_symbols
-
-
-class NothingChanged(UserWarning):
-    """Raised when reformatted code is the same as source."""
-
-
-class CannotSplit(Exception):
-    """A readable split that fits the allotted line length is impossible."""
-
-
-class InvalidInput(ValueError):
-    """Raised when input source code fails all parse attempts."""
-
-
-class WriteBack(Enum):
-    NO = 0
-    YES = 1
-    DIFF = 2
-    CHECK = 3
-
-    @classmethod
-    def from_configuration(cls, *, check: bool, diff: bool) -> "WriteBack":
-        if check and not diff:
-            return cls.CHECK
-
-        return cls.DIFF if diff else cls.YES
-
-
-class Changed(Enum):
-    NO = 0
-    CACHED = 1
-    YES = 2
-
-
-class TargetVersion(Enum):
-    PY27 = 2
-    PY33 = 3
-    PY34 = 4
-    PY35 = 5
-    PY36 = 6
-    PY37 = 7
-    PY38 = 8
-
-    def is_python2(self) -> bool:
-        return self is TargetVersion.PY27
-
-
-PY36_VERSIONS = {TargetVersion.PY36, TargetVersion.PY37, TargetVersion.PY38}
-
-
-class Feature(Enum):
-    # All string literals are unicode
-    UNICODE_LITERALS = 1
-    F_STRINGS = 2
-    NUMERIC_UNDERSCORES = 3
-    TRAILING_COMMA_IN_CALL = 4
-    TRAILING_COMMA_IN_DEF = 5
-    # The following two feature-flags are mutually exclusive, and exactly one should be
-    # set for every version of python.
-    ASYNC_IDENTIFIERS = 6
-    ASYNC_KEYWORDS = 7
-    ASSIGNMENT_EXPRESSIONS = 8
-    POS_ONLY_ARGUMENTS = 9
-
-
-VERSION_TO_FEATURES: Dict[TargetVersion, Set[Feature]] = {
-    TargetVersion.PY27: {Feature.ASYNC_IDENTIFIERS},
-    TargetVersion.PY33: {Feature.UNICODE_LITERALS, Feature.ASYNC_IDENTIFIERS},
-    TargetVersion.PY34: {Feature.UNICODE_LITERALS, Feature.ASYNC_IDENTIFIERS},
-    TargetVersion.PY35: {
-        Feature.UNICODE_LITERALS,
-        Feature.TRAILING_COMMA_IN_CALL,
-        Feature.ASYNC_IDENTIFIERS,
-    },
-    TargetVersion.PY36: {
-        Feature.UNICODE_LITERALS,
-        Feature.F_STRINGS,
-        Feature.NUMERIC_UNDERSCORES,
-        Feature.TRAILING_COMMA_IN_CALL,
-        Feature.TRAILING_COMMA_IN_DEF,
-        Feature.ASYNC_IDENTIFIERS,
-    },
-    TargetVersion.PY37: {
-        Feature.UNICODE_LITERALS,
-        Feature.F_STRINGS,
-        Feature.NUMERIC_UNDERSCORES,
-        Feature.TRAILING_COMMA_IN_CALL,
-        Feature.TRAILING_COMMA_IN_DEF,
-        Feature.ASYNC_KEYWORDS,
-    },
-    TargetVersion.PY38: {
-        Feature.UNICODE_LITERALS,
-        Feature.F_STRINGS,
-        Feature.NUMERIC_UNDERSCORES,
-        Feature.TRAILING_COMMA_IN_CALL,
-        Feature.TRAILING_COMMA_IN_DEF,
-        Feature.ASYNC_KEYWORDS,
-        Feature.ASSIGNMENT_EXPRESSIONS,
-        Feature.POS_ONLY_ARGUMENTS,
-    },
-}
-
-
-@dataclass
-class FileMode:
-    target_versions: Set[TargetVersion] = Factory(set)
-    line_length: int = DEFAULT_LINE_LENGTH
-    string_normalization: bool = True
-    is_pyi: bool = False
-
-    def get_cache_key(self) -> str:
-        if self.target_versions:
-            version_str = ",".join(
-                str(version.value)
-                for version in sorted(self.target_versions, key=lambda v: v.value)
-            )
-        else:
-            version_str = "-"
-        parts = [
-            version_str,
-            str(self.line_length),
-            str(int(self.string_normalization)),
-            str(int(self.is_pyi)),
-        ]
-        return ".".join(parts)
-
-
-def supports_feature(target_versions: Set[TargetVersion], feature: Feature) -> bool:
-    return all(feature in VERSION_TO_FEATURES[version] for version in target_versions)
-
-
-def read_pyproject_toml(
-    ctx: click.Context, param: click.Parameter, value: Union[str, int, bool, None]
-) -> Optional[str]:
-    """Inject Black configuration from "pyproject.toml" into defaults in `ctx`.
-
-    Returns the path to a successfully found and read configuration file, None
-    otherwise.
-    """
-    assert not isinstance(value, (int, bool)), "Invalid parameter type passed"
-    if not value:
-        root = find_project_root(ctx.params.get("src", ()))
-        path = root / "pyproject.toml"
-        if path.is_file():
-            value = str(path)
-        else:
-            return None
-
-    try:
-        pyproject_toml = toml.load(value)
-        config = pyproject_toml.get("tool", {}).get("black", {})
-    except (toml.TomlDecodeError, OSError) as e:
-        raise click.FileError(
-            filename=value, hint=f"Error reading configuration file: {e}"
-        )
-
-    if not config:
-        return None
-
-    if ctx.default_map is None:
-        ctx.default_map = {}
-    ctx.default_map.update(  # type: ignore  # bad types in .pyi
-        {k.replace("--", "").replace("-", "_"): v for k, v in config.items()}
-    )
-    return value
-
-
-@click.command(context_settings=dict(help_option_names=["-h", "--help"]))
-@click.option("-c", "--code", type=str, help="Format the code passed in as a string.")
-@click.option(
-    "-l",
-    "--line-length",
-    type=int,
-    default=DEFAULT_LINE_LENGTH,
-    help="How many characters per line to allow.",
-    show_default=True,
-)
-@click.option(
-    "-t",
-    "--target-version",
-    type=click.Choice([v.name.lower() for v in TargetVersion]),
-    callback=lambda c, p, v: [TargetVersion[val.upper()] for val in v],
-    multiple=True,
-    help=(
-        "Python versions that should be supported by Black's output. [default: "
-        "per-file auto-detection]"
-    ),
-)
-@click.option(
-    "--py36",
-    is_flag=True,
-    help=(
-        "Allow using Python 3.6-only syntax on all input files.  This will put "
-        "trailing commas in function signatures and calls also after *args and "
-        "**kwargs. Deprecated; use --target-version instead. "
-        "[default: per-file auto-detection]"
-    ),
-)
-@click.option(
-    "--pyi",
-    is_flag=True,
-    help=(
-        "Format all input files like typing stubs regardless of file extension "
-        "(useful when piping source on standard input)."
-    ),
-)
-@click.option(
-    "-S",
-    "--skip-string-normalization",
-    is_flag=True,
-    help="Don't normalize string quotes or prefixes.",
-)
-@click.option(
-    "--check",
-    is_flag=True,
-    help=(
-        "Don't write the files back, just return the status.  Return code 0 "
-        "means nothing would change.  Return code 1 means some files would be "
-        "reformatted.  Return code 123 means there was an internal error."
-    ),
-)
-@click.option(
-    "--diff",
-    is_flag=True,
-    help="Don't write the files back, just output a diff for each file on stdout.",
-)
-@click.option(
-    "--fast/--safe",
-    is_flag=True,
-    help="If --fast given, skip temporary sanity checks. [default: --safe]",
-)
-@click.option(
-    "--include",
-    type=str,
-    default=DEFAULT_INCLUDES,
-    help=(
-        "A regular expression that matches files and directories that should be "
-        "included on recursive searches.  An empty value means all files are "
-        "included regardless of the name.  Use forward slashes for directories on "
-        "all platforms (Windows, too).  Exclusions are calculated first, inclusions "
-        "later."
-    ),
-    show_default=True,
-)
-@click.option(
-    "--exclude",
-    type=str,
-    default=DEFAULT_EXCLUDES,
-    help=(
-        "A regular expression that matches files and directories that should be "
-        "excluded on recursive searches.  An empty value means no paths are excluded. "
-        "Use forward slashes for directories on all platforms (Windows, too).  "
-        "Exclusions are calculated first, inclusions later."
-    ),
-    show_default=True,
-)
-@click.option(
-    "-q",
-    "--quiet",
-    is_flag=True,
-    help=(
-        "Don't emit non-error messages to stderr. Errors are still emitted; "
-        "silence those with 2>/dev/null."
-    ),
-)
-@click.option(
-    "-v",
-    "--verbose",
-    is_flag=True,
-    help=(
-        "Also emit messages to stderr about files that were not changed or were "
-        "ignored due to --exclude=."
-    ),
-)
-@click.version_option(version=__version__)
-@click.argument(
-    "src",
-    nargs=-1,
-    type=click.Path(
-        exists=True, file_okay=True, dir_okay=True, readable=True, allow_dash=True
-    ),
-    is_eager=True,
-)
-@click.option(
-    "--config",
-    type=click.Path(
-        exists=False, file_okay=True, dir_okay=False, readable=True, allow_dash=False
-    ),
-    is_eager=True,
-    callback=read_pyproject_toml,
-    help="Read configuration from PATH.",
-)
-@click.pass_context
-def main(
-    ctx: click.Context,
-    code: Optional[str],
-    line_length: int,
-    target_version: List[TargetVersion],
-    check: bool,
-    diff: bool,
-    fast: bool,
-    pyi: bool,
-    py36: bool,
-    skip_string_normalization: bool,
-    quiet: bool,
-    verbose: bool,
-    include: str,
-    exclude: str,
-    src: Tuple[str],
-    config: Optional[str],
-) -> None:
-    """The uncompromising code formatter."""
-    write_back = WriteBack.from_configuration(check=check, diff=diff)
-    if target_version:
-        if py36:
-            err(f"Cannot use both --target-version and --py36")
-            ctx.exit(2)
-        else:
-            versions = set(target_version)
-    elif py36:
-        err(
-            "--py36 is deprecated and will be removed in a future version. "
-            "Use --target-version py36 instead."
-        )
-        versions = PY36_VERSIONS
-    else:
-        # We'll autodetect later.
-        versions = set()
-    mode = FileMode(
-        target_versions=versions,
-        line_length=line_length,
-        is_pyi=pyi,
-        string_normalization=not skip_string_normalization,
-    )
-    if config and verbose:
-        out(f"Using configuration from {config}.", bold=False, fg="blue")
-    if code is not None:
-        print(format_str(code, mode=mode))
-        ctx.exit(0)
-    try:
-        include_regex = re_compile_maybe_verbose(include)
-    except re.error:
-        err(f"Invalid regular expression for include given: {include!r}")
-        ctx.exit(2)
-    try:
-        exclude_regex = re_compile_maybe_verbose(exclude)
-    except re.error:
-        err(f"Invalid regular expression for exclude given: {exclude!r}")
-        ctx.exit(2)
-    report = Report(check=check, quiet=quiet, verbose=verbose)
-    root = find_project_root(src)
-    sources: Set[Path] = set()
-    path_empty(src, quiet, verbose, ctx)
-    for s in src:
-        p = Path(s)
-        if p.is_dir():
-            sources.update(
-                gen_python_files_in_dir(p, root, include_regex, exclude_regex, report)
-            )
-        elif p.is_file() or s == "-":
-            # if a file was explicitly given, we don't care about its extension
-            sources.add(p)
-        else:
-            err(f"invalid path: {s}")
-    if len(sources) == 0:
-        if verbose or not quiet:
-            out("No Python files are present to be formatted. Nothing to do 😴")
-        ctx.exit(0)
-
-    if len(sources) == 1:
-        reformat_one(
-            src=sources.pop(),
-            fast=fast,
-            write_back=write_back,
-            mode=mode,
-            report=report,
-        )
-    else:
-        reformat_many(
-            sources=sources, fast=fast, write_back=write_back, mode=mode, report=report
-        )
-
-    if verbose or not quiet:
-        out("Oh no! 💥 💔 💥" if report.return_code else "All done! ✨ 🍰 ✨")
-        click.secho(str(report), err=True)
-    ctx.exit(report.return_code)
-
-
-def path_empty(src: Tuple[str], quiet: bool, verbose: bool, ctx: click.Context) -> None:
-    """
-    Exit if there is no `src` provided for formatting
-    """
-    if not src:
-        if verbose or not quiet:
-            out("No Path provided. Nothing to do 😴")
-            ctx.exit(0)
-
-
-def reformat_one(
-    src: Path, fast: bool, write_back: WriteBack, mode: FileMode, report: "Report"
-) -> None:
-    """Reformat a single file under `src` without spawning child processes.
-
-    `fast`, `write_back`, and `mode` options are passed to
-    :func:`format_file_in_place` or :func:`format_stdin_to_stdout`.
-    """
-    try:
-        changed = Changed.NO
-        if not src.is_file() and str(src) == "-":
-            if format_stdin_to_stdout(fast=fast, write_back=write_back, mode=mode):
-                changed = Changed.YES
-        else:
-            cache: Cache = {}
-            if write_back != WriteBack.DIFF:
-                cache = read_cache(mode)
-                res_src = src.resolve()
-                if res_src in cache and cache[res_src] == get_cache_info(res_src):
-                    changed = Changed.CACHED
-            if changed is not Changed.CACHED and format_file_in_place(
-                src, fast=fast, write_back=write_back, mode=mode
-            ):
-                changed = Changed.YES
-            if (write_back is WriteBack.YES and changed is not Changed.CACHED) or (
-                write_back is WriteBack.CHECK and changed is Changed.NO
-            ):
-                write_cache(cache, [src], mode)
-        report.done(src, changed)
-    except Exception as exc:
-        report.failed(src, str(exc))
-
-
-def reformat_many(
-    sources: Set[Path],
-    fast: bool,
-    write_back: WriteBack,
-    mode: FileMode,
-    report: "Report",
-) -> None:
-    """Reformat multiple files using a ProcessPoolExecutor."""
-    loop = asyncio.get_event_loop()
-    worker_count = os.cpu_count()
-    if sys.platform == "win32":
-        # Work around https://bugs.python.org/issue26903
-        worker_count = min(worker_count, 61)
-    executor = ProcessPoolExecutor(max_workers=worker_count)
-    try:
-        loop.run_until_complete(
-            schedule_formatting(
-                sources=sources,
-                fast=fast,
-                write_back=write_back,
-                mode=mode,
-                report=report,
-                loop=loop,
-                executor=executor,
-            )
-        )
-    finally:
-        shutdown(loop)
-        executor.shutdown()
-
-
-async def schedule_formatting(
-    sources: Set[Path],
-    fast: bool,
-    write_back: WriteBack,
-    mode: FileMode,
-    report: "Report",
-    loop: asyncio.AbstractEventLoop,
-    executor: Executor,
-) -> None:
-    """Run formatting of `sources` in parallel using the provided `executor`.
-
-    (Use ProcessPoolExecutors for actual parallelism.)
-
-    `write_back`, `fast`, and `mode` options are passed to
-    :func:`format_file_in_place`.
-    """
-    cache: Cache = {}
-    if write_back != WriteBack.DIFF:
-        cache = read_cache(mode)
-        sources, cached = filter_cached(cache, sources)
-        for src in sorted(cached):
-            report.done(src, Changed.CACHED)
-    if not sources:
-        return
-
-    cancelled = []
-    sources_to_cache = []
-    lock = None
-    if write_back == WriteBack.DIFF:
-        # For diff output, we need locks to ensure we don't interleave output
-        # from different processes.
-        manager = Manager()
-        lock = manager.Lock()
-    tasks = {
-        asyncio.ensure_future(
-            loop.run_in_executor(
-                executor, format_file_in_place, src, fast, mode, write_back, lock
-            )
-        ): src
-        for src in sorted(sources)
-    }
-    pending: Iterable[asyncio.Future] = tasks.keys()
-    try:
-        loop.add_signal_handler(signal.SIGINT, cancel, pending)
-        loop.add_signal_handler(signal.SIGTERM, cancel, pending)
-    except NotImplementedError:
-        # There are no good alternatives for these on Windows.
-        pass
-    while pending:
-        done, _ = await asyncio.wait(pending, return_when=asyncio.FIRST_COMPLETED)
-        for task in done:
-            src = tasks.pop(task)
-            if task.cancelled():
-                cancelled.append(task)
-            elif task.exception():
-                report.failed(src, str(task.exception()))
-            else:
-                changed = Changed.YES if task.result() else Changed.NO
-                # If the file was written back or was successfully checked as
-                # well-formatted, store this information in the cache.
-                if write_back is WriteBack.YES or (
-                    write_back is WriteBack.CHECK and changed is Changed.NO
-                ):
-                    sources_to_cache.append(src)
-                report.done(src, changed)
-    if cancelled:
-        await asyncio.gather(*cancelled, loop=loop, return_exceptions=True)
-    if sources_to_cache:
-        write_cache(cache, sources_to_cache, mode)
-
-
-def format_file_in_place(
-    src: Path,
-    fast: bool,
-    mode: FileMode,
-    write_back: WriteBack = WriteBack.NO,
-    lock: Any = None,  # multiprocessing.Manager().Lock() is some crazy proxy
-) -> bool:
-    """Format file under `src` path. Return True if changed.
-
-    If `write_back` is DIFF, write a diff to stdout. If it is YES, write reformatted
-    code to the file.
-    `mode` and `fast` options are passed to :func:`format_file_contents`.
-    """
-    if src.suffix == ".pyi":
-        mode = evolve(mode, is_pyi=True)
-
-    then = datetime.utcfromtimestamp(src.stat().st_mtime)
-    with open(src, "rb") as buf:
-        src_contents, encoding, newline = decode_bytes(buf.read())
-    try:
-        dst_contents = format_file_contents(src_contents, fast=fast, mode=mode)
-    except NothingChanged:
-        return False
-
-    if write_back == write_back.YES:
-        with open(src, "w", encoding=encoding, newline=newline) as f:
-            f.write(dst_contents)
-    elif write_back == write_back.DIFF:
-        now = datetime.utcnow()
-        src_name = f"{src}\t{then} +0000"
-        dst_name = f"{src}\t{now} +0000"
-        diff_contents = diff(src_contents, dst_contents, src_name, dst_name)
-
-        with lock or nullcontext():
-            f = io.TextIOWrapper(
-                sys.stdout.buffer,
-                encoding=encoding,
-                newline=newline,
-                write_through=True,
-            )
-            f.write(diff_contents)
-            f.detach()
-
-    return True
-
-
-def format_stdin_to_stdout(
-    fast: bool, *, write_back: WriteBack = WriteBack.NO, mode: FileMode
-) -> bool:
-    """Format file on stdin. Return True if changed.
-
-    If `write_back` is YES, write reformatted code back to stdout. If it is DIFF,
-    write a diff to stdout. The `mode` argument is passed to
-    :func:`format_file_contents`.
-    """
-    then = datetime.utcnow()
-    src, encoding, newline = decode_bytes(sys.stdin.buffer.read())
-    dst = src
-    try:
-        dst = format_file_contents(src, fast=fast, mode=mode)
-        return True
-
-    except NothingChanged:
-        return False
-
-    finally:
-        f = io.TextIOWrapper(
-            sys.stdout.buffer, encoding=encoding, newline=newline, write_through=True
-        )
-        if write_back == WriteBack.YES:
-            f.write(dst)
-        elif write_back == WriteBack.DIFF:
-            now = datetime.utcnow()
-            src_name = f"STDIN\t{then} +0000"
-            dst_name = f"STDOUT\t{now} +0000"
-            f.write(diff(src, dst, src_name, dst_name))
-        f.detach()
-
-
-def format_file_contents(
-    src_contents: str, *, fast: bool, mode: FileMode
-) -> FileContent:
-    """Reformat contents a file and return new contents.
-
-    If `fast` is False, additionally confirm that the reformatted code is
-    valid by calling :func:`assert_equivalent` and :func:`assert_stable` on it.
-    `mode` is passed to :func:`format_str`.
-    """
-    if src_contents.strip() == "":
-        raise NothingChanged
-
-    dst_contents = format_str(src_contents, mode=mode)
-    if src_contents == dst_contents:
-        raise NothingChanged
-
-    if not fast:
-        assert_equivalent(src_contents, dst_contents)
-        assert_stable(src_contents, dst_contents, mode=mode)
-    return dst_contents
-
-
-def format_str(src_contents: str, *, mode: FileMode) -> FileContent:
-    """Reformat a string and return new contents.
-
-    `mode` determines formatting options, such as how many characters per line are
-    allowed.
-    """
-    src_node = lib2to3_parse(src_contents.lstrip(), mode.target_versions)
-    dst_contents = []
-    future_imports = get_future_imports(src_node)
-    if mode.target_versions:
-        versions = mode.target_versions
-    else:
-        versions = detect_target_versions(src_node)
-    normalize_fmt_off(src_node)
-    lines = LineGenerator(
-        remove_u_prefix="unicode_literals" in future_imports
-        or supports_feature(versions, Feature.UNICODE_LITERALS),
-        is_pyi=mode.is_pyi,
-        normalize_strings=mode.string_normalization,
-    )
-    elt = EmptyLineTracker(is_pyi=mode.is_pyi)
-    empty_line = Line()
-    after = 0
-    split_line_features = {
-        feature
-        for feature in {Feature.TRAILING_COMMA_IN_CALL, Feature.TRAILING_COMMA_IN_DEF}
-        if supports_feature(versions, feature)
-    }
-    for current_line in lines.visit(src_node):
-        for _ in range(after):
-            dst_contents.append(str(empty_line))
-        before, after = elt.maybe_empty_lines(current_line)
-        for _ in range(before):
-            dst_contents.append(str(empty_line))
-        for line in split_line(
-            current_line, line_length=mode.line_length, features=split_line_features
-        ):
-            dst_contents.append(str(line))
-    return "".join(dst_contents)
-
-
-def decode_bytes(src: bytes) -> Tuple[FileContent, Encoding, NewLine]:
-    """Return a tuple of (decoded_contents, encoding, newline).
-
-    `newline` is either CRLF or LF but `decoded_contents` is decoded with
-    universal newlines (i.e. only contains LF).
-    """
-    srcbuf = io.BytesIO(src)
-    encoding, lines = tokenize.detect_encoding(srcbuf.readline)
-    if not lines:
-        return "", encoding, "\n"
-
-    newline = "\r\n" if b"\r\n" == lines[0][-2:] else "\n"
-    srcbuf.seek(0)
-    with io.TextIOWrapper(srcbuf, encoding) as tiow:
-        return tiow.read(), encoding, newline
-
-
-def get_grammars(target_versions: Set[TargetVersion]) -> List[Grammar]:
-    if not target_versions:
-        # No target_version specified, so try all grammars.
-        return [
-            # Python 3.7+
-            pygram.python_grammar_no_print_statement_no_exec_statement_async_keywords,
-            # Python 3.0-3.6
-            pygram.python_grammar_no_print_statement_no_exec_statement,
-            # Python 2.7 with future print_function import
-            pygram.python_grammar_no_print_statement,
-            # Python 2.7
-            pygram.python_grammar,
-        ]
-    elif all(version.is_python2() for version in target_versions):
-        # Python 2-only code, so try Python 2 grammars.
-        return [
-            # Python 2.7 with future print_function import
-            pygram.python_grammar_no_print_statement,
-            # Python 2.7
-            pygram.python_grammar,
-        ]
-    else:
-        # Python 3-compatible code, so only try Python 3 grammar.
-        grammars = []
-        # If we have to parse both, try to parse async as a keyword first
-        if not supports_feature(target_versions, Feature.ASYNC_IDENTIFIERS):
-            # Python 3.7+
-            grammars.append(
-                pygram.python_grammar_no_print_statement_no_exec_statement_async_keywords  # noqa: B950
-            )
-        if not supports_feature(target_versions, Feature.ASYNC_KEYWORDS):
-            # Python 3.0-3.6
-            grammars.append(pygram.python_grammar_no_print_statement_no_exec_statement)
-        # At least one of the above branches must have been taken, because every Python
-        # version has exactly one of the two 'ASYNC_*' flags
-        return grammars
-
-
-def lib2to3_parse(src_txt: str, target_versions: Iterable[TargetVersion] = ()) -> Node:
-    """Given a string with source, return the lib2to3 Node."""
-    if src_txt[-1:] != "\n":
-        src_txt += "\n"
-
-    for grammar in get_grammars(set(target_versions)):
-        drv = driver.Driver(grammar, pytree.convert)
-        try:
-            result = drv.parse_string(src_txt, True)
-            break
-
-        except ParseError as pe:
-            lineno, column = pe.context[1]
-            lines = src_txt.splitlines()
-            try:
-                faulty_line = lines[lineno - 1]
-            except IndexError:
-                faulty_line = "<line number missing in source>"
-            exc = InvalidInput(f"Cannot parse: {lineno}:{column}: {faulty_line}")
-    else:
-        raise exc from None
-
-    if isinstance(result, Leaf):
-        result = Node(syms.file_input, [result])
-    return result
-
-
-def lib2to3_unparse(node: Node) -> str:
-    """Given a lib2to3 node, return its string representation."""
-    code = str(node)
-    return code
-
-
-T = TypeVar("T")
-
-
-class Visitor(Generic[T]):
-    """Basic lib2to3 visitor that yields things of type `T` on `visit()`."""
-
-    def visit(self, node: LN) -> Iterator[T]:
-        """Main method to visit `node` and its children.
-
-        It tries to find a `visit_*()` method for the given `node.type`, like
-        `visit_simple_stmt` for Node objects or `visit_INDENT` for Leaf objects.
-        If no dedicated `visit_*()` method is found, chooses `visit_default()`
-        instead.
-
-        Then yields objects of type `T` from the selected visitor.
-        """
-        if node.type < 256:
-            name = token.tok_name[node.type]
-        else:
-            name = type_repr(node.type)
-        yield from getattr(self, f"visit_{name}", self.visit_default)(node)
-
-    def visit_default(self, node: LN) -> Iterator[T]:
-        """Default `visit_*()` implementation. Recurses to children of `node`."""
-        if isinstance(node, Node):
-            for child in node.children:
-                yield from self.visit(child)
-
-
-@dataclass
-class DebugVisitor(Visitor[T]):
-    tree_depth: int = 0
-
-    def visit_default(self, node: LN) -> Iterator[T]:
-        indent = " " * (2 * self.tree_depth)
-        if isinstance(node, Node):
-            _type = type_repr(node.type)
-            out(f"{indent}{_type}", fg="yellow")
-            self.tree_depth += 1
-            for child in node.children:
-                yield from self.visit(child)
-
-            self.tree_depth -= 1
-            out(f"{indent}/{_type}", fg="yellow", bold=False)
-        else:
-            _type = token.tok_name.get(node.type, str(node.type))
-            out(f"{indent}{_type}", fg="blue", nl=False)
-            if node.prefix:
-                # We don't have to handle prefixes for `Node` objects since
-                # that delegates to the first child anyway.
-                out(f" {node.prefix!r}", fg="green", bold=False, nl=False)
-            out(f" {node.value!r}", fg="blue", bold=False)
-
-    @classmethod
-    def show(cls, code: Union[str, Leaf, Node]) -> None:
-        """Pretty-print the lib2to3 AST of a given string of `code`.
-
-        Convenience method for debugging.
-        """
-        v: DebugVisitor[None] = DebugVisitor()
-        if isinstance(code, str):
-            code = lib2to3_parse(code)
-        list(v.visit(code))
-
-
-WHITESPACE = {token.DEDENT, token.INDENT, token.NEWLINE}
-STATEMENT = {
-    syms.if_stmt,
-    syms.while_stmt,
-    syms.for_stmt,
-    syms.try_stmt,
-    syms.except_clause,
-    syms.with_stmt,
-    syms.funcdef,
-    syms.classdef,
-}
-STANDALONE_COMMENT = 153
-token.tok_name[STANDALONE_COMMENT] = "STANDALONE_COMMENT"
-LOGIC_OPERATORS = {"and", "or"}
-COMPARATORS = {
-    token.LESS,
-    token.GREATER,
-    token.EQEQUAL,
-    token.NOTEQUAL,
-    token.LESSEQUAL,
-    token.GREATEREQUAL,
-}
-MATH_OPERATORS = {
-    token.VBAR,
-    token.CIRCUMFLEX,
-    token.AMPER,
-    token.LEFTSHIFT,
-    token.RIGHTSHIFT,
-    token.PLUS,
-    token.MINUS,
-    token.STAR,
-    token.SLASH,
-    token.DOUBLESLASH,
-    token.PERCENT,
-    token.AT,
-    token.TILDE,
-    token.DOUBLESTAR,
-}
-STARS = {token.STAR, token.DOUBLESTAR}
-VARARGS_SPECIALS = STARS | {token.SLASH}
-VARARGS_PARENTS = {
-    syms.arglist,
-    syms.argument,  # double star in arglist
-    syms.trailer,  # single argument to call
-    syms.typedargslist,
-    syms.varargslist,  # lambdas
-}
-UNPACKING_PARENTS = {
-    syms.atom,  # single element of a list or set literal
-    syms.dictsetmaker,
-    syms.listmaker,
-    syms.testlist_gexp,
-    syms.testlist_star_expr,
-}
-TEST_DESCENDANTS = {
-    syms.test,
-    syms.lambdef,
-    syms.or_test,
-    syms.and_test,
-    syms.not_test,
-    syms.comparison,
-    syms.star_expr,
-    syms.expr,
-    syms.xor_expr,
-    syms.and_expr,
-    syms.shift_expr,
-    syms.arith_expr,
-    syms.trailer,
-    syms.term,
-    syms.power,
-}
-ASSIGNMENTS = {
-    "=",
-    "+=",
-    "-=",
-    "*=",
-    "@=",
-    "/=",
-    "%=",
-    "&=",
-    "|=",
-    "^=",
-    "<<=",
-    ">>=",
-    "**=",
-    "//=",
-}
-COMPREHENSION_PRIORITY = 20
-COMMA_PRIORITY = 18
-TERNARY_PRIORITY = 16
-LOGIC_PRIORITY = 14
-STRING_PRIORITY = 12
-COMPARATOR_PRIORITY = 10
-MATH_PRIORITIES = {
-    token.VBAR: 9,
-    token.CIRCUMFLEX: 8,
-    token.AMPER: 7,
-    token.LEFTSHIFT: 6,
-    token.RIGHTSHIFT: 6,
-    token.PLUS: 5,
-    token.MINUS: 5,
-    token.STAR: 4,
-    token.SLASH: 4,
-    token.DOUBLESLASH: 4,
-    token.PERCENT: 4,
-    token.AT: 4,
-    token.TILDE: 3,
-    token.DOUBLESTAR: 2,
-}
-DOT_PRIORITY = 1
-
-
-@dataclass
-class BracketTracker:
-    """Keeps track of brackets on a line."""
-
-    depth: int = 0
-    bracket_match: Dict[Tuple[Depth, NodeType], Leaf] = Factory(dict)
-    delimiters: Dict[LeafID, Priority] = Factory(dict)
-    previous: Optional[Leaf] = None
-    _for_loop_depths: List[int] = Factory(list)
-    _lambda_argument_depths: List[int] = Factory(list)
-
-    def mark(self, leaf: Leaf) -> None:
-        """Mark `leaf` with bracket-related metadata. Keep track of delimiters.
-
-        All leaves receive an int `bracket_depth` field that stores how deep
-        within brackets a given leaf is. 0 means there are no enclosing brackets
-        that started on this line.
-
-        If a leaf is itself a closing bracket, it receives an `opening_bracket`
-        field that it forms a pair with. This is a one-directional link to
-        avoid reference cycles.
-
-        If a leaf is a delimiter (a token on which Black can split the line if
-        needed) and it's on depth 0, its `id()` is stored in the tracker's
-        `delimiters` field.
-        """
-        if leaf.type == token.COMMENT:
-            return
-
-        self.maybe_decrement_after_for_loop_variable(leaf)
-        self.maybe_decrement_after_lambda_arguments(leaf)
-        if leaf.type in CLOSING_BRACKETS:
-            self.depth -= 1
-            opening_bracket = self.bracket_match.pop((self.depth, leaf.type))
-            leaf.opening_bracket = opening_bracket
-        leaf.bracket_depth = self.depth
-        if self.depth == 0:
-            delim = is_split_before_delimiter(leaf, self.previous)
-            if delim and self.previous is not None:
-                self.delimiters[id(self.previous)] = delim
-            else:
-                delim = is_split_after_delimiter(leaf, self.previous)
-                if delim:
-                    self.delimiters[id(leaf)] = delim
-        if leaf.type in OPENING_BRACKETS:
-            self.bracket_match[self.depth, BRACKET[leaf.type]] = leaf
-            self.depth += 1
-        self.previous = leaf
-        self.maybe_increment_lambda_arguments(leaf)
-        self.maybe_increment_for_loop_variable(leaf)
-
-    def any_open_brackets(self) -> bool:
-        """Return True if there is an yet unmatched open bracket on the line."""
-        return bool(self.bracket_match)
-
-    def max_delimiter_priority(self, exclude: Iterable[LeafID] = ()) -> Priority:
-        """Return the highest priority of a delimiter found on the line.
-
-        Values are consistent with what `is_split_*_delimiter()` return.
-        Raises ValueError on no delimiters.
-        """
-        return max(v for k, v in self.delimiters.items() if k not in exclude)
-
-    def delimiter_count_with_priority(self, priority: Priority = 0) -> int:
-        """Return the number of delimiters with the given `priority`.
-
-        If no `priority` is passed, defaults to max priority on the line.
-        """
-        if not self.delimiters:
-            return 0
-
-        priority = priority or self.max_delimiter_priority()
-        return sum(1 for p in self.delimiters.values() if p == priority)
-
-    def maybe_increment_for_loop_variable(self, leaf: Leaf) -> bool:
-        """In a for loop, or comprehension, the variables are often unpacks.
-
-        To avoid splitting on the comma in this situation, increase the depth of
-        tokens between `for` and `in`.
-        """
-        if leaf.type == token.NAME and leaf.value == "for":
-            self.depth += 1
-            self._for_loop_depths.append(self.depth)
-            return True
-
-        return False
-
-    def maybe_decrement_after_for_loop_variable(self, leaf: Leaf) -> bool:
-        """See `maybe_increment_for_loop_variable` above for explanation."""
-        if (
-            self._for_loop_depths
-            and self._for_loop_depths[-1] == self.depth
-            and leaf.type == token.NAME
-            and leaf.value == "in"
-        ):
-            self.depth -= 1
-            self._for_loop_depths.pop()
-            return True
-
-        return False
-
-    def maybe_increment_lambda_arguments(self, leaf: Leaf) -> bool:
-        """In a lambda expression, there might be more than one argument.
-
-        To avoid splitting on the comma in this situation, increase the depth of
-        tokens between `lambda` and `:`.
-        """
-        if leaf.type == token.NAME and leaf.value == "lambda":
-            self.depth += 1
-            self._lambda_argument_depths.append(self.depth)
-            return True
-
-        return False
-
-    def maybe_decrement_after_lambda_arguments(self, leaf: Leaf) -> bool:
-        """See `maybe_increment_lambda_arguments` above for explanation."""
-        if (
-            self._lambda_argument_depths
-            and self._lambda_argument_depths[-1] == self.depth
-            and leaf.type == token.COLON
-        ):
-            self.depth -= 1
-            self._lambda_argument_depths.pop()
-            return True
-
-        return False
-
-    def get_open_lsqb(self) -> Optional[Leaf]:
-        """Return the most recent opening square bracket (if any)."""
-        return self.bracket_match.get((self.depth - 1, token.RSQB))
-
-
-@dataclass
-class Line:
-    """Holds leaves and comments. Can be printed with `str(line)`."""
-
-    depth: int = 0
-    leaves: List[Leaf] = Factory(list)
-    comments: Dict[LeafID, List[Leaf]] = Factory(dict)  # keys ordered like `leaves`
-    bracket_tracker: BracketTracker = Factory(BracketTracker)
-    inside_brackets: bool = False
-    should_explode: bool = False
-
-    def append(self, leaf: Leaf, preformatted: bool = False) -> None:
-        """Add a new `leaf` to the end of the line.
-
-        Unless `preformatted` is True, the `leaf` will receive a new consistent
-        whitespace prefix and metadata applied by :class:`BracketTracker`.
-        Trailing commas are maybe removed, unpacked for loop variables are
-        demoted from being delimiters.
-
-        Inline comments are put aside.
-        """
-        has_value = leaf.type in BRACKETS or bool(leaf.value.strip())
-        if not has_value:
-            return
-
-        if token.COLON == leaf.type and self.is_class_paren_empty:
-            del self.leaves[-2:]
-        if self.leaves and not preformatted:
-            # Note: at this point leaf.prefix should be empty except for
-            # imports, for which we only preserve newlines.
-            leaf.prefix += whitespace(
-                leaf, complex_subscript=self.is_complex_subscript(leaf)
-            )
-        if self.inside_brackets or not preformatted:
-            self.bracket_tracker.mark(leaf)
-            self.maybe_remove_trailing_comma(leaf)
-        if not self.append_comment(leaf):
-            self.leaves.append(leaf)
-
-    def append_safe(self, leaf: Leaf, preformatted: bool = False) -> None:
-        """Like :func:`append()` but disallow invalid standalone comment structure.
-
-        Raises ValueError when any `leaf` is appended after a standalone comment
-        or when a standalone comment is not the first leaf on the line.
-        """
-        if self.bracket_tracker.depth == 0:
-            if self.is_comment:
-                raise ValueError("cannot append to standalone comments")
-
-            if self.leaves and leaf.type == STANDALONE_COMMENT:
-                raise ValueError(
-                    "cannot append standalone comments to a populated line"
-                )
-
-        self.append(leaf, preformatted=preformatted)
-
-    @property
-    def is_comment(self) -> bool:
-        """Is this line a standalone comment?"""
-        return len(self.leaves) == 1 and self.leaves[0].type == STANDALONE_COMMENT
-
-    @property
-    def is_decorator(self) -> bool:
-        """Is this line a decorator?"""
-        return bool(self) and self.leaves[0].type == token.AT
-
-    @property
-    def is_import(self) -> bool:
-        """Is this an import line?"""
-        return bool(self) and is_import(self.leaves[0])
-
-    @property
-    def is_class(self) -> bool:
-        """Is this line a class definition?"""
-        return (
-            bool(self)
-            and self.leaves[0].type == token.NAME
-            and self.leaves[0].value == "class"
-        )
-
-    @property
-    def is_stub_class(self) -> bool:
-        """Is this line a class definition with a body consisting only of "..."?"""
-        return self.is_class and self.leaves[-3:] == [
-            Leaf(token.DOT, ".") for _ in range(3)
-        ]
-
-    @property
-    def is_collection_with_optional_trailing_comma(self) -> bool:
-        """Is this line a collection literal with a trailing comma that's optional?
-
-        Note that the trailing comma in a 1-tuple is not optional.
-        """
-        if not self.leaves or len(self.leaves) < 4:
-            return False
-        # Look for and address a trailing colon.
-        if self.leaves[-1].type == token.COLON:
-            closer = self.leaves[-2]
-            close_index = -2
-        else:
-            closer = self.leaves[-1]
-            close_index = -1
-        if closer.type not in CLOSING_BRACKETS or self.inside_brackets:
-            return False
-        if closer.type == token.RPAR:
-            # Tuples require an extra check, because if there's only
-            # one element in the tuple removing the comma unmakes the
-            # tuple.
-            #
-            # We also check for parens before looking for the trailing
-            # comma because in some cases (eg assigning a dict
-            # literal) the literal gets wrapped in temporary parens
-            # during parsing. This case is covered by the
-            # collections.py test data.
-            opener = closer.opening_bracket
-            for _open_index, leaf in enumerate(self.leaves):
-                if leaf is opener:
-                    break
-            else:
-                # Couldn't find the matching opening paren, play it safe.
-                return False
-            commas = 0
-            comma_depth = self.leaves[close_index - 1].bracket_depth
-            for leaf in self.leaves[_open_index + 1 : close_index]:
-                if leaf.bracket_depth == comma_depth and leaf.type == token.COMMA:
-                    commas += 1
-            if commas > 1:
-                # We haven't looked yet for the trailing comma because
-                # we might also have caught noop parens.
-                return self.leaves[close_index - 1].type == token.COMMA
-            elif commas == 1:
-                return False  # it's either a one-tuple or didn't have a trailing comma
-            if self.leaves[close_index - 1].type in CLOSING_BRACKETS:
-                close_index -= 1
-                closer = self.leaves[close_index]
-                if closer.type == token.RPAR:
-                    # TODO: this is a gut feeling. Will we ever see this?
-                    return False
-        if self.leaves[close_index - 1].type != token.COMMA:
-            return False
-        return True
-
-    @property
-    def is_def(self) -> bool:
-        """Is this a function definition? (Also returns True for async defs.)"""
-        try:
-            first_leaf = self.leaves[0]
-        except IndexError:
-            return False
-
-        try:
-            second_leaf: Optional[Leaf] = self.leaves[1]
-        except IndexError:
-            second_leaf = None
-        return (first_leaf.type == token.NAME and first_leaf.value == "def") or (
-            first_leaf.type == token.ASYNC
-            and second_leaf is not None
-            and second_leaf.type == token.NAME
-            and second_leaf.value == "def"
-        )
-
-    @property
-    def is_class_paren_empty(self) -> bool:
-        """Is this a class with no base classes but using parentheses?
-
-        Those are unnecessary and should be removed.
-        """
-        return (
-            bool(self)
-            and len(self.leaves) == 4
-            and self.is_class
-            and self.leaves[2].type == token.LPAR
-            and self.leaves[2].value == "("
-            and self.leaves[3].type == token.RPAR
-            and self.leaves[3].value == ")"
-        )
-
-    @property
-    def is_triple_quoted_string(self) -> bool:
-        """Is the line a triple quoted string?"""
-        return (
-            bool(self)
-            and self.leaves[0].type == token.STRING
-            and self.leaves[0].value.startswith(('"""', "'''"))
-        )
-
-    def contains_standalone_comments(self, depth_limit: int = sys.maxsize) -> bool:
-        """If so, needs to be split before emitting."""
-        for leaf in self.leaves:
-            if leaf.type == STANDALONE_COMMENT:
-                if leaf.bracket_depth <= depth_limit:
-                    return True
-        return False
-
-    def contains_uncollapsable_type_comments(self) -> bool:
-        ignored_ids = set()
-        try:
-            last_leaf = self.leaves[-1]
-            ignored_ids.add(id(last_leaf))
-            if last_leaf.type == token.COMMA or (
-                last_leaf.type == token.RPAR and not last_leaf.value
-            ):
-                # When trailing commas or optional parens are inserted by Black for
-                # consistency, comments after the previous last element are not moved
-                # (they don't have to, rendering will still be correct).  So we ignore
-                # trailing commas and invisible.
-                last_leaf = self.leaves[-2]
-                ignored_ids.add(id(last_leaf))
-        except IndexError:
-            return False
-
-        # A type comment is uncollapsable if it is attached to a leaf
-        # that isn't at the end of the line (since that could cause it
-        # to get associated to a different argument) or if there are
-        # comments before it (since that could cause it to get hidden
-        # behind a comment.
-        comment_seen = False
-        for leaf_id, comments in self.comments.items():
-            for comment in comments:
-                if is_type_comment(comment):
-                    if leaf_id not in ignored_ids or comment_seen:
-                        return True
-
-            comment_seen = True
-
-        return False
-
-    def contains_unsplittable_type_ignore(self) -> bool:
-        if not self.leaves:
-            return False
-
-        # If a 'type: ignore' is attached to the end of a line, we
-        # can't split the line, because we can't know which of the
-        # subexpressions the ignore was meant to apply to.
-        #
-        # We only want this to apply to actual physical lines from the
-        # original source, though: we don't want the presence of a
-        # 'type: ignore' at the end of a multiline expression to
-        # justify pushing it all onto one line. Thus we
-        # (unfortunately) need to check the actual source lines and
-        # only report an unsplittable 'type: ignore' if this line was
-        # one line in the original code.
-        if self.leaves[0].lineno == self.leaves[-1].lineno:
-            for comment in self.comments.get(id(self.leaves[-1]), []):
-                if is_type_comment(comment, " ignore"):
-                    return True
-
-        return False
-
-    def contains_multiline_strings(self) -> bool:
-        for leaf in self.leaves:
-            if is_multiline_string(leaf):
-                return True
-
-        return False
-
-    def maybe_remove_trailing_comma(self, closing: Leaf) -> bool:
-        """Remove trailing comma if there is one and it's safe."""
-        if not (self.leaves and self.leaves[-1].type == token.COMMA):
-            return False
-        # We remove trailing commas only in the case of importing a
-        # single name from a module.
-        if not (
-            self.leaves
-            and self.is_import
-            and len(self.leaves) > 4
-            and self.leaves[-1].type == token.COMMA
-            and closing.type in CLOSING_BRACKETS
-            and self.leaves[-4].type == token.NAME
-            and (
-                # regular `from foo import bar,`
-                self.leaves[-4].value == "import"
-                # `from foo import (bar as baz,)
-                or (
-                    len(self.leaves) > 6
-                    and self.leaves[-6].value == "import"
-                    and self.leaves[-3].value == "as"
-                )
-                # `from foo import bar as baz,`
-                or (
-                    len(self.leaves) > 5
-                    and self.leaves[-5].value == "import"
-                    and self.leaves[-3].value == "as"
-                )
-            )
-            and closing.type == token.RPAR
-        ):
-            return False
-
-        self.remove_trailing_comma()
-        return True
-
-    def append_comment(self, comment: Leaf) -> bool:
-        """Add an inline or standalone comment to the line."""
-        if (
-            comment.type == STANDALONE_COMMENT
-            and self.bracket_tracker.any_open_brackets()
-        ):
-            comment.prefix = ""
-            return False
-
-        if comment.type != token.COMMENT:
-            return False
-
-        if not self.leaves:
-            comment.type = STANDALONE_COMMENT
-            comment.prefix = ""
-            return False
-
-        last_leaf = self.leaves[-1]
-        if (
-            last_leaf.type == token.RPAR
-            and not last_leaf.value
-            and last_leaf.parent
-            and len(list(last_leaf.parent.leaves())) <= 3
-            and not is_type_comment(comment)
-        ):
-            # Comments on an optional parens wrapping a single leaf should belong to
-            # the wrapped node except if it's a type comment. Pinning the comment like
-            # this avoids unstable formatting caused by comment migration.
-            if len(self.leaves) < 2:
-                comment.type = STANDALONE_COMMENT
-                comment.prefix = ""
-                return False
-            last_leaf = self.leaves[-2]
-        self.comments.setdefault(id(last_leaf), []).append(comment)
-        return True
-
-    def comments_after(self, leaf: Leaf) -> List[Leaf]:
-        """Generate comments that should appear directly after `leaf`."""
-        return self.comments.get(id(leaf), [])
-
-    def remove_trailing_comma(self) -> None:
-        """Remove the trailing comma and moves the comments attached to it."""
-        trailing_comma = self.leaves.pop()
-        trailing_comma_comments = self.comments.pop(id(trailing_comma), [])
-        self.comments.setdefault(id(self.leaves[-1]), []).extend(
-            trailing_comma_comments
-        )
-
-    def is_complex_subscript(self, leaf: Leaf) -> bool:
-        """Return True iff `leaf` is part of a slice with non-trivial exprs."""
-        open_lsqb = self.bracket_tracker.get_open_lsqb()
-        if open_lsqb is None:
-            return False
-
-        subscript_start = open_lsqb.next_sibling
-
-        if isinstance(subscript_start, Node):
-            if subscript_start.type == syms.listmaker:
-                return False
-
-            if subscript_start.type == syms.subscriptlist:
-                subscript_start = child_towards(subscript_start, leaf)
-        return subscript_start is not None and any(
-            n.type in TEST_DESCENDANTS for n in subscript_start.pre_order()
-        )
-
-    def __str__(self) -> str:
-        """Render the line."""
-        if not self:
-            return "\n"
-
-        indent = "    " * self.depth
-        leaves = iter(self.leaves)
-        first = next(leaves)
-        res = f"{first.prefix}{indent}{first.value}"
-        for leaf in leaves:
-            res += str(leaf)
-        for comment in itertools.chain.from_iterable(self.comments.values()):
-            res += str(comment)
-        return res + "\n"
-
-    def __bool__(self) -> bool:
-        """Return True if the line has leaves or comments."""
-        return bool(self.leaves or self.comments)
-
-
-@dataclass
-class EmptyLineTracker:
-    """Provides a stateful method that returns the number of potential extra
-    empty lines needed before and after the currently processed line.
-
-    Note: this tracker works on lines that haven't been split yet.  It assumes
-    the prefix of the first leaf consists of optional newlines.  Those newlines
-    are consumed by `maybe_empty_lines()` and included in the computation.
-    """
-
-    is_pyi: bool = False
-    previous_line: Optional[Line] = None
-    previous_after: int = 0
-    previous_defs: List[int] = Factory(list)
-
-    def maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
-        """Return the number of extra empty lines before and after the `current_line`.
-
-        This is for separating `def`, `async def` and `class` with extra empty
-        lines (two on module-level).
-        """
-        before, after = self._maybe_empty_lines(current_line)
-        before = (
-            # Black should not insert empty lines at the beginning
-            # of the file
-            0
-            if self.previous_line is None
-            else before - self.previous_after
-        )
-        self.previous_after = after
-        self.previous_line = current_line
-        return before, after
-
-    def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
-        max_allowed = 1
-        if current_line.depth == 0:
-            max_allowed = 1 if self.is_pyi else 2
-        if current_line.leaves:
-            # Consume the first leaf's extra newlines.
-            first_leaf = current_line.leaves[0]
-            before = first_leaf.prefix.count("\n")
-            before = min(before, max_allowed)
-            first_leaf.prefix = ""
-        else:
-            before = 0
-        depth = current_line.depth
-        while self.previous_defs and self.previous_defs[-1] >= depth:
-            self.previous_defs.pop()
-            if self.is_pyi:
-                before = 0 if depth else 1
-            else:
-                before = 1 if depth else 2
-        if current_line.is_decorator or current_line.is_def or current_line.is_class:
-            return self._maybe_empty_lines_for_class_or_def(current_line, before)
-
-        if (
-            self.previous_line
-            and self.previous_line.is_import
-            and not current_line.is_import
-            and depth == self.previous_line.depth
-        ):
-            return (before or 1), 0
-
-        if (
-            self.previous_line
-            and self.previous_line.is_class
-            and current_line.is_triple_quoted_string
-        ):
-            return before, 1
-
-        return before, 0
-
-    def _maybe_empty_lines_for_class_or_def(
-        self, current_line: Line, before: int
-    ) -> Tuple[int, int]:
-        if not current_line.is_decorator:
-            self.previous_defs.append(current_line.depth)
-        if self.previous_line is None:
-            # Don't insert empty lines before the first line in the file.
-            return 0, 0
-
-        if self.previous_line.is_decorator:
-            return 0, 0
-
-        if self.previous_line.depth < current_line.depth and (
-            self.previous_line.is_class or self.previous_line.is_def
-        ):
-            return 0, 0
-
-        if (
-            self.previous_line.is_comment
-            and self.previous_line.depth == current_line.depth
-            and before == 0
-        ):
-            return 0, 0
-
-        if self.is_pyi:
-            if self.previous_line.depth > current_line.depth:
-                newlines = 1
-            elif current_line.is_class or self.previous_line.is_class:
-                if current_line.is_stub_class and self.previous_line.is_stub_class:
-                    # No blank line between classes with an empty body
-                    newlines = 0
-                else:
-                    newlines = 1
-            elif current_line.is_def and not self.previous_line.is_def:
-                # Blank line between a block of functions and a block of non-functions
-                newlines = 1
-            else:
-                newlines = 0
-        else:
-            newlines = 2
-        if current_line.depth and newlines:
-            newlines -= 1
-        return newlines, 0
-
-
-@dataclass
-class LineGenerator(Visitor[Line]):
-    """Generates reformatted Line objects.  Empty lines are not emitted.
-
-    Note: destroys the tree it's visiting by mutating prefixes of its leaves
-    in ways that will no longer stringify to valid Python code on the tree.
-    """
-
-    is_pyi: bool = False
-    normalize_strings: bool = True
-    current_line: Line = Factory(Line)
-    remove_u_prefix: bool = False
-
-    def line(self, indent: int = 0) -> Iterator[Line]:
-        """Generate a line.
-
-        If the line is empty, only emit if it makes sense.
-        If the line is too long, split it first and then generate.
-
-        If any lines were generated, set up a new current_line.
-        """
-        if not self.current_line:
-            self.current_line.depth += indent
-            return  # Line is empty, don't emit. Creating a new one unnecessary.
-
-        complete_line = self.current_line
-        self.current_line = Line(depth=complete_line.depth + indent)
-        yield complete_line
-
-    def visit_default(self, node: LN) -> Iterator[Line]:
-        """Default `visit_*()` implementation. Recurses to children of `node`."""
-        if isinstance(node, Leaf):
-            any_open_brackets = self.current_line.bracket_tracker.any_open_brackets()
-            for comment in generate_comments(node):
-                if any_open_brackets:
-                    # any comment within brackets is subject to splitting
-                    self.current_line.append(comment)
-                elif comment.type == token.COMMENT:
-                    # regular trailing comment
-                    self.current_line.append(comment)
-                    yield from self.line()
-
-                else:
-                    # regular standalone comment
-                    yield from self.line()
-
-                    self.current_line.append(comment)
-                    yield from self.line()
-
-            normalize_prefix(node, inside_brackets=any_open_brackets)
-            if self.normalize_strings and node.type == token.STRING:
-                normalize_string_prefix(node, remove_u_prefix=self.remove_u_prefix)
-                normalize_string_quotes(node)
-            if node.type == token.NUMBER:
-                normalize_numeric_literal(node)
-            if node.type not in WHITESPACE:
-                self.current_line.append(node)
-        yield from super().visit_default(node)
-
-    def visit_atom(self, node: Node) -> Iterator[Line]:
-        # Always make parentheses invisible around a single node, because it should
-        # not be needed (except in the case of yield, where removing the parentheses
-        # produces a SyntaxError).
-        if (
-            len(node.children) == 3
-            and isinstance(node.children[0], Leaf)
-            and node.children[0].type == token.LPAR
-            and isinstance(node.children[2], Leaf)
-            and node.children[2].type == token.RPAR
-            and isinstance(node.children[1], Leaf)
-            and not (
-                node.children[1].type == token.NAME
-                and node.children[1].value == "yield"
-            )
-        ):
-            node.children[0].value = ""
-            node.children[2].value = ""
-        yield from super().visit_default(node)
-
-    def visit_factor(self, node: Node) -> Iterator[Line]:
-        """Force parentheses between a unary op and a binary power:
-
-        -2 ** 8 -> -(2 ** 8)
-        """
-        child = node.children[1]
-        if child.type == syms.power and len(child.children) == 3:
-            lpar = Leaf(token.LPAR, "(")
-            rpar = Leaf(token.RPAR, ")")
-            index = child.remove() or 0
-            node.insert_child(index, Node(syms.atom, [lpar, child, rpar]))
-        yield from self.visit_default(node)
-
-    def visit_INDENT(self, node: Node) -> Iterator[Line]:
-        """Increase indentation level, maybe yield a line."""
-        # In blib2to3 INDENT never holds comments.
-        yield from self.line(+1)
-        yield from self.visit_default(node)
-
-    def visit_DEDENT(self, node: Node) -> Iterator[Line]:
-        """Decrease indentation level, maybe yield a line."""
-        # The current line might still wait for trailing comments.  At DEDENT time
-        # there won't be any (they would be prefixes on the preceding NEWLINE).
-        # Emit the line then.
-        yield from self.line()
-
-        # While DEDENT has no value, its prefix may contain standalone comments
-        # that belong to the current indentation level.  Get 'em.
-        yield from self.visit_default(node)
-
-        # Finally, emit the dedent.
-        yield from self.line(-1)
-
-    def visit_stmt(
-        self, node: Node, keywords: Set[str], parens: Set[str]
-    ) -> Iterator[Line]:
-        """Visit a statement.
-
-        This implementation is shared for `if`, `while`, `for`, `try`, `except`,
-        `def`, `with`, `class`, `assert` and assignments.
-
-        The relevant Python language `keywords` for a given statement will be
-        NAME leaves within it. This methods puts those on a separate line.
-
-        `parens` holds a set of string leaf values immediately after which
-        invisible parens should be put.
-        """
-        normalize_invisible_parens(node, parens_after=parens)
-        for child in node.children:
-            if child.type == token.NAME and child.value in keywords:  # type: ignore
-                yield from self.line()
-
-            yield from self.visit(child)
-
-    def visit_suite(self, node: Node) -> Iterator[Line]:
-        """Visit a suite."""
-        if self.is_pyi and is_stub_suite(node):
-            yield from self.visit(node.children[2])
-        else:
-            yield from self.visit_default(node)
-
-    def visit_simple_stmt(self, node: Node) -> Iterator[Line]:
-        """Visit a statement without nested statements."""
-        is_suite_like = node.parent and node.parent.type in STATEMENT
-        if is_suite_like:
-            if self.is_pyi and is_stub_body(node):
-                yield from self.visit_default(node)
-            else:
-                yield from self.line(+1)
-                yield from self.visit_default(node)
-                yield from self.line(-1)
-
-        else:
-            if not self.is_pyi or not node.parent or not is_stub_suite(node.parent):
-                yield from self.line()
-            yield from self.visit_default(node)
-
-    def visit_async_stmt(self, node: Node) -> Iterator[Line]:
-        """Visit `async def`, `async for`, `async with`."""
-        yield from self.line()
-
-        children = iter(node.children)
-        for child in children:
-            yield from self.visit(child)
-
-            if child.type == token.ASYNC:
-                break
-
-        internal_stmt = next(children)
-        for child in internal_stmt.children:
-            yield from self.visit(child)
-
-    def visit_decorators(self, node: Node) -> Iterator[Line]:
-        """Visit decorators."""
-        for child in node.children:
-            yield from self.line()
-            yield from self.visit(child)
-
-    def visit_SEMI(self, leaf: Leaf) -> Iterator[Line]:
-        """Remove a semicolon and put the other statement on a separate line."""
-        yield from self.line()
-
-    def visit_ENDMARKER(self, leaf: Leaf) -> Iterator[Line]:
-        """End of file. Process outstanding comments and end with a newline."""
-        yield from self.visit_default(leaf)
-        yield from self.line()
-
-    def visit_STANDALONE_COMMENT(self, leaf: Leaf) -> Iterator[Line]:
-        if not self.current_line.bracket_tracker.any_open_brackets():
-            yield from self.line()
-        yield from self.visit_default(leaf)
-
-    def __attrs_post_init__(self) -> None:
-        """You are in a twisty little maze of passages."""
-        v = self.visit_stmt
-        Ø: Set[str] = set()
-        self.visit_assert_stmt = partial(v, keywords={"assert"}, parens={"assert", ","})
-        self.visit_if_stmt = partial(
-            v, keywords={"if", "else", "elif"}, parens={"if", "elif"}
-        )
-        self.visit_while_stmt = partial(v, keywords={"while", "else"}, parens={"while"})
-        self.visit_for_stmt = partial(v, keywords={"for", "else"}, parens={"for", "in"})
-        self.visit_try_stmt = partial(
-            v, keywords={"try", "except", "else", "finally"}, parens=Ø
-        )
-        self.visit_except_clause = partial(v, keywords={"except"}, parens=Ø)
-        self.visit_with_stmt = partial(v, keywords={"with"}, parens=Ø)
-        self.visit_funcdef = partial(v, keywords={"def"}, parens=Ø)
-        self.visit_classdef = partial(v, keywords={"class"}, parens=Ø)
-        self.visit_expr_stmt = partial(v, keywords=Ø, parens=ASSIGNMENTS)
-        self.visit_return_stmt = partial(v, keywords={"return"}, parens={"return"})
-        self.visit_import_from = partial(v, keywords=Ø, parens={"import"})
-        self.visit_del_stmt = partial(v, keywords=Ø, parens={"del"})
-        self.visit_async_funcdef = self.visit_async_stmt
-        self.visit_decorated = self.visit_decorators
-
-
-IMPLICIT_TUPLE = {syms.testlist, syms.testlist_star_expr, syms.exprlist}
-BRACKET = {token.LPAR: token.RPAR, token.LSQB: token.RSQB, token.LBRACE: token.RBRACE}
-OPENING_BRACKETS = set(BRACKET.keys())
-CLOSING_BRACKETS = set(BRACKET.values())
-BRACKETS = OPENING_BRACKETS | CLOSING_BRACKETS
-ALWAYS_NO_SPACE = CLOSING_BRACKETS | {token.COMMA, STANDALONE_COMMENT}
-
-
-def whitespace(leaf: Leaf, *, complex_subscript: bool) -> str:  # noqa: C901
-    """Return whitespace prefix if needed for the given `leaf`.
-
-    `complex_subscript` signals whether the given leaf is part of a subscription
-    which has non-trivial arguments, like arithmetic expressions or function calls.
-    """
-    NO = ""
-    SPACE = " "
-    DOUBLESPACE = "  "
-    t = leaf.type
-    p = leaf.parent
-    v = leaf.value
-    if t in ALWAYS_NO_SPACE:
-        return NO
-
-    if t == token.COMMENT:
-        return DOUBLESPACE
-
-    assert p is not None, f"INTERNAL ERROR: hand-made leaf without parent: {leaf!r}"
-    if t == token.COLON and p.type not in {
-        syms.subscript,
-        syms.subscriptlist,
-        syms.sliceop,
-    }:
-        return NO
-
-    prev = leaf.prev_sibling
-    if not prev:
-        prevp = preceding_leaf(p)
-        if not prevp or prevp.type in OPENING_BRACKETS:
-            return NO
-
-        if t == token.COLON:
-            if prevp.type == token.COLON:
-                return NO
-
-            elif prevp.type != token.COMMA and not complex_subscript:
-                return NO
-
-            return SPACE
-
-        if prevp.type == token.EQUAL:
-            if prevp.parent:
-                if prevp.parent.type in {
-                    syms.arglist,
-                    syms.argument,
-                    syms.parameters,
-                    syms.varargslist,
-                }:
-                    return NO
-
-                elif prevp.parent.type == syms.typedargslist:
-                    # A bit hacky: if the equal sign has whitespace, it means we
-                    # previously found it's a typed argument.  So, we're using
-                    # that, too.
-                    return prevp.prefix
-
-        elif prevp.type in VARARGS_SPECIALS:
-            if is_vararg(prevp, within=VARARGS_PARENTS | UNPACKING_PARENTS):
-                return NO
-
-        elif prevp.type == token.COLON:
-            if prevp.parent and prevp.parent.type in {syms.subscript, syms.sliceop}:
-                return SPACE if complex_subscript else NO
-
-        elif (
-            prevp.parent
-            and prevp.parent.type == syms.factor
-            and prevp.type in MATH_OPERATORS
-        ):
-            return NO
-
-        elif (
-            prevp.type == token.RIGHTSHIFT
-            and prevp.parent
-            and prevp.parent.type == syms.shift_expr
-            and prevp.prev_sibling
-            and prevp.prev_sibling.type == token.NAME
-            and prevp.prev_sibling.value == "print"  # type: ignore
-        ):
-            # Python 2 print chevron
-            return NO
-
-    elif prev.type in OPENING_BRACKETS:
-        return NO
-
-    if p.type in {syms.parameters, syms.arglist}:
-        # untyped function signatures or calls
-        if not prev or prev.type != token.COMMA:
-            return NO
-
-    elif p.type == syms.varargslist:
-        # lambdas
-        if prev and prev.type != token.COMMA:
-            return NO
-
-    elif p.type == syms.typedargslist:
-        # typed function signatures
-        if not prev:
-            return NO
-
-        if t == token.EQUAL:
-            if prev.type != syms.tname:
-                return NO
-
-        elif prev.type == token.EQUAL:
-            # A bit hacky: if the equal sign has whitespace, it means we
-            # previously found it's a typed argument.  So, we're using that, too.
-            return prev.prefix
-
-        elif prev.type != token.COMMA:
-            return NO
-
-    elif p.type == syms.tname:
-        # type names
-        if not prev:
-            prevp = preceding_leaf(p)
-            if not prevp or prevp.type != token.COMMA:
-                return NO
-
-    elif p.type == syms.trailer:
-        # attributes and calls
-        if t == token.LPAR or t == token.RPAR:
-            return NO
-
-        if not prev:
-            if t == token.DOT:
-                prevp = preceding_leaf(p)
-                if not prevp or prevp.type != token.NUMBER:
-                    return NO
-
-            elif t == token.LSQB:
-                return NO
-
-        elif prev.type != token.COMMA:
-            return NO
-
-    elif p.type == syms.argument:
-        # single argument
-        if t == token.EQUAL:
-            return NO
-
-        if not prev:
-            prevp = preceding_leaf(p)
-            if not prevp or prevp.type == token.LPAR:
-                return NO
-
-        elif prev.type in {token.EQUAL} | VARARGS_SPECIALS:
-            return NO
-
-    elif p.type == syms.decorator:
-        # decorators
-        return NO
-
-    elif p.type == syms.dotted_name:
-        if prev:
-            return NO
-
-        prevp = preceding_leaf(p)
-        if not prevp or prevp.type == token.AT or prevp.type == token.DOT:
-            return NO
-
-    elif p.type == syms.classdef:
-        if t == token.LPAR:
-            return NO
-
-        if prev and prev.type == token.LPAR:
-            return NO
-
-    elif p.type in {syms.subscript, syms.sliceop}:
-        # indexing
-        if not prev:
-            assert p.parent is not None, "subscripts are always parented"
-            if p.parent.type == syms.subscriptlist:
-                return SPACE
-
-            return NO
-
-        elif not complex_subscript:
-            return NO
-
-    elif p.type == syms.atom:
-        if prev and t == token.DOT:
-            # dots, but not the first one.
-            return NO
-
-    elif p.type == syms.dictsetmaker:
-        # dict unpacking
-        if prev and prev.type == token.DOUBLESTAR:
-            return NO
-
-    elif p.type in {syms.factor, syms.star_expr}:
-        # unary ops
-        if not prev:
-            prevp = preceding_leaf(p)
-            if not prevp or prevp.type in OPENING_BRACKETS:
-                return NO
-
-            prevp_parent = prevp.parent
-            assert prevp_parent is not None
-            if prevp.type == token.COLON and prevp_parent.type in {
-                syms.subscript,
-                syms.sliceop,
-            }:
-                return NO
-
-            elif prevp.type == token.EQUAL and prevp_parent.type == syms.argument:
-                return NO
-
-        elif t in {token.NAME, token.NUMBER, token.STRING}:
-            return NO
-
-    elif p.type == syms.import_from:
-        if t == token.DOT:
-            if prev and prev.type == token.DOT:
-                return NO
-
-        elif t == token.NAME:
-            if v == "import":
-                return SPACE
-
-            if prev and prev.type == token.DOT:
-                return NO
-
-    elif p.type == syms.sliceop:
-        return NO
-
-    return SPACE
-
-
-def preceding_leaf(node: Optional[LN]) -> Optional[Leaf]:
-    """Return the first leaf that precedes `node`, if any."""
-    while node:
-        res = node.prev_sibling
-        if res:
-            if isinstance(res, Leaf):
-                return res
-
-            try:
-                return list(res.leaves())[-1]
-
-            except IndexError:
-                return None
-
-        node = node.parent
-    return None
-
-
-def child_towards(ancestor: Node, descendant: LN) -> Optional[LN]:
-    """Return the child of `ancestor` that contains `descendant`."""
-    node: Optional[LN] = descendant
-    while node and node.parent != ancestor:
-        node = node.parent
-    return node
-
-
-def container_of(leaf: Leaf) -> LN:
-    """Return `leaf` or one of its ancestors that is the topmost container of it.
-
-    By "container" we mean a node where `leaf` is the very first child.
-    """
-    same_prefix = leaf.prefix
-    container: LN = leaf
-    while container:
-        parent = container.parent
-        if parent is None:
-            break
-
-        if parent.children[0].prefix != same_prefix:
-            break
-
-        if parent.type == syms.file_input:
-            break
-
-        if parent.prev_sibling is not None and parent.prev_sibling.type in BRACKETS:
-            break
-
-        container = parent
-    return container
-
-
-def is_split_after_delimiter(leaf: Leaf, previous: Optional[Leaf] = None) -> Priority:
-    """Return the priority of the `leaf` delimiter, given a line break after it.
-
-    The delimiter priorities returned here are from those delimiters that would
-    cause a line break after themselves.
-
-    Higher numbers are higher priority.
-    """
-    if leaf.type == token.COMMA:
-        return COMMA_PRIORITY
-
-    return 0
-
-
-def is_split_before_delimiter(leaf: Leaf, previous: Optional[Leaf] = None) -> Priority:
-    """Return the priority of the `leaf` delimiter, given a line break before it.
-
-    The delimiter priorities returned here are from those delimiters that would
-    cause a line break before themselves.
-
-    Higher numbers are higher priority.
-    """
-    if is_vararg(leaf, within=VARARGS_PARENTS | UNPACKING_PARENTS):
-        # * and ** might also be MATH_OPERATORS but in this case they are not.
-        # Don't treat them as a delimiter.
-        return 0
-
-    if (
-        leaf.type == token.DOT
-        and leaf.parent
-        and leaf.parent.type not in {syms.import_from, syms.dotted_name}
-        and (previous is None or previous.type in CLOSING_BRACKETS)
-    ):
-        return DOT_PRIORITY
-
-    if (
-        leaf.type in MATH_OPERATORS
-        and leaf.parent
-        and leaf.parent.type not in {syms.factor, syms.star_expr}
-    ):
-        return MATH_PRIORITIES[leaf.type]
-
-    if leaf.type in COMPARATORS:
-        return COMPARATOR_PRIORITY
-
-    if (
-        leaf.type == token.STRING
-        and previous is not None
-        and previous.type == token.STRING
-    ):
-        return STRING_PRIORITY
-
-    if leaf.type not in {token.NAME, token.ASYNC}:
-        return 0
-
-    if (
-        leaf.value == "for"
-        and leaf.parent
-        and leaf.parent.type in {syms.comp_for, syms.old_comp_for}
-        or leaf.type == token.ASYNC
-    ):
-        if (
-            not isinstance(leaf.prev_sibling, Leaf)
-            or leaf.prev_sibling.value != "async"
-        ):
-            return COMPREHENSION_PRIORITY
-
-    if (
-        leaf.value == "if"
-        and leaf.parent
-        and leaf.parent.type in {syms.comp_if, syms.old_comp_if}
-    ):
-        return COMPREHENSION_PRIORITY
-
-    if leaf.value in {"if", "else"} and leaf.parent and leaf.parent.type == syms.test:
-        return TERNARY_PRIORITY
-
-    if leaf.value == "is":
-        return COMPARATOR_PRIORITY
-
-    if (
-        leaf.value == "in"
-        and leaf.parent
-        and leaf.parent.type in {syms.comp_op, syms.comparison}
-        and not (
-            previous is not None
-            and previous.type == token.NAME
-            and previous.value == "not"
-        )
-    ):
-        return COMPARATOR_PRIORITY
-
-    if (
-        leaf.value == "not"
-        and leaf.parent
-        and leaf.parent.type == syms.comp_op
-        and not (
-            previous is not None
-            and previous.type == token.NAME
-            and previous.value == "is"
-        )
-    ):
-        return COMPARATOR_PRIORITY
-
-    if leaf.value in LOGIC_OPERATORS and leaf.parent:
-        return LOGIC_PRIORITY
-
-    return 0
-
-
-FMT_OFF = {"# fmt: off", "# fmt:off", "# yapf: disable"}
-FMT_ON = {"# fmt: on", "# fmt:on", "# yapf: enable"}
-
-
-def generate_comments(leaf: LN) -> Iterator[Leaf]:
-    """Clean the prefix of the `leaf` and generate comments from it, if any.
-
-    Comments in lib2to3 are shoved into the whitespace prefix.  This happens
-    in `pgen2/driver.py:Driver.parse_tokens()`.  This was a brilliant implementation
-    move because it does away with modifying the grammar to include all the
-    possible places in which comments can be placed.
-
-    The sad consequence for us though is that comments don't "belong" anywhere.
-    This is why this function generates simple parentless Leaf objects for
-    comments.  We simply don't know what the correct parent should be.
-
-    No matter though, we can live without this.  We really only need to
-    differentiate between inline and standalone comments.  The latter don't
-    share the line with any code.
-
-    Inline comments are emitted as regular token.COMMENT leaves.  Standalone
-    are emitted with a fake STANDALONE_COMMENT token identifier.
-    """
-    for pc in list_comments(leaf.prefix, is_endmarker=leaf.type == token.ENDMARKER):
-        yield Leaf(pc.type, pc.value, prefix="\n" * pc.newlines)
-
-
-@dataclass
-class ProtoComment:
-    """Describes a piece of syntax that is a comment.
-
-    It's not a :class:`blib2to3.pytree.Leaf` so that:
-
-    * it can be cached (`Leaf` objects should not be reused more than once as
-      they store their lineno, column, prefix, and parent information);
-    * `newlines` and `consumed` fields are kept separate from the `value`. This
-      simplifies handling of special marker comments like ``# fmt: off/on``.
-    """
-
-    type: int  # token.COMMENT or STANDALONE_COMMENT
-    value: str  # content of the comment
-    newlines: int  # how many newlines before the comment
-    consumed: int  # how many characters of the original leaf's prefix did we consume
-
-
-@lru_cache(maxsize=4096)
-def list_comments(prefix: str, *, is_endmarker: bool) -> List[ProtoComment]:
-    """Return a list of :class:`ProtoComment` objects parsed from the given `prefix`."""
-    result: List[ProtoComment] = []
-    if not prefix or "#" not in prefix:
-        return result
-
-    consumed = 0
-    nlines = 0
-    ignored_lines = 0
-    for index, line in enumerate(prefix.split("\n")):
-        consumed += len(line) + 1  # adding the length of the split '\n'
-        line = line.lstrip()
-        if not line:
-            nlines += 1
-        if not line.startswith("#"):
-            # Escaped newlines outside of a comment are not really newlines at
-            # all. We treat a single-line comment following an escaped newline
-            # as a simple trailing comment.
-            if line.endswith("\\"):
-                ignored_lines += 1
-            continue
-
-        if index == ignored_lines and not is_endmarker:
-            comment_type = token.COMMENT  # simple trailing comment
-        else:
-            comment_type = STANDALONE_COMMENT
-        comment = make_comment(line)
-        result.append(
-            ProtoComment(
-                type=comment_type, value=comment, newlines=nlines, consumed=consumed
-            )
-        )
-        nlines = 0
-    return result
-
-
-def make_comment(content: str) -> str:
-    """Return a consistently formatted comment from the given `content` string.
-
-    All comments (except for "##", "#!", "#:", '#'", "#%%") should have a single
-    space between the hash sign and the content.
-
-    If `content` didn't start with a hash sign, one is provided.
-    """
-    content = content.rstrip()
-    if not content:
-        return "#"
-
-    if content[0] == "#":
-        content = content[1:]
-    if content and content[0] not in " !:#'%":
-        content = " " + content
-    return "#" + content
-
-
-def split_line(
-    line: Line,
-    line_length: int,
-    inner: bool = False,
-    features: Collection[Feature] = (),
-) -> Iterator[Line]:
-    """Split a `line` into potentially many lines.
-
-    They should fit in the allotted `line_length` but might not be able to.
-    `inner` signifies that there were a pair of brackets somewhere around the
-    current `line`, possibly transitively. This means we can fallback to splitting
-    by delimiters if the LHS/RHS don't yield any results.
-
-    `features` are syntactical features that may be used in the output.
-    """
-    if line.is_comment:
-        yield line
-        return
-
-    line_str = str(line).strip("\n")
-
-    if (
-        not line.contains_uncollapsable_type_comments()
-        and not line.should_explode
-        and not line.is_collection_with_optional_trailing_comma
-        and (
-            is_line_short_enough(line, line_length=line_length, line_str=line_str)
-            or line.contains_unsplittable_type_ignore()
-        )
-    ):
-        yield line
-        return
-
-    split_funcs: List[SplitFunc]
-    if line.is_def:
-        split_funcs = [left_hand_split]
-    else:
-
-        def rhs(line: Line, features: Collection[Feature]) -> Iterator[Line]:
-            for omit in generate_trailers_to_omit(line, line_length):
-                lines = list(right_hand_split(line, line_length, features, omit=omit))
-                if is_line_short_enough(lines[0], line_length=line_length):
-                    yield from lines
-                    return
-
-            # All splits failed, best effort split with no omits.
-            # This mostly happens to multiline strings that are by definition
-            # reported as not fitting a single line.
-            yield from right_hand_split(line, line_length, features=features)
-
-        if line.inside_brackets:
-            split_funcs = [delimiter_split, standalone_comment_split, rhs]
-        else:
-            split_funcs = [rhs]
-    for split_func in split_funcs:
-        # We are accumulating lines in `result` because we might want to abort
-        # mission and return the original line in the end, or attempt a different
-        # split altogether.
-        result: List[Line] = []
-        try:
-            for l in split_func(line, features):
-                if str(l).strip("\n") == line_str:
-                    raise CannotSplit("Split function returned an unchanged result")
-
-                result.extend(
-                    split_line(
-                        l, line_length=line_length, inner=True, features=features
-                    )
-                )
-        except CannotSplit:
-            continue
-
-        else:
-            yield from result
-            break
-
-    else:
-        yield line
-
-
-def left_hand_split(line: Line, features: Collection[Feature] = ()) -> Iterator[Line]:
-    """Split line into many lines, starting with the first matching bracket pair.
-
-    Note: this usually looks weird, only use this for function definitions.
-    Prefer RHS otherwise.  This is why this function is not symmetrical with
-    :func:`right_hand_split` which also handles optional parentheses.
-    """
-    tail_leaves: List[Leaf] = []
-    body_leaves: List[Leaf] = []
-    head_leaves: List[Leaf] = []
-    current_leaves = head_leaves
-    matching_bracket = None
-    for leaf in line.leaves:
-        if (
-            current_leaves is body_leaves
-            and leaf.type in CLOSING_BRACKETS
-            and leaf.opening_bracket is matching_bracket
-        ):
-            current_leaves = tail_leaves if body_leaves else head_leaves
-        current_leaves.append(leaf)
-        if current_leaves is head_leaves:
-            if leaf.type in OPENING_BRACKETS:
-                matching_bracket = leaf
-                current_leaves = body_leaves
-    if not matching_bracket:
-        raise CannotSplit("No brackets found")
-
-    head = bracket_split_build_line(head_leaves, line, matching_bracket)
-    body = bracket_split_build_line(body_leaves, line, matching_bracket, is_body=True)
-    tail = bracket_split_build_line(tail_leaves, line, matching_bracket)
-    bracket_split_succeeded_or_raise(head, body, tail)
-    for result in (head, body, tail):
-        if result:
-            yield result
-
-
-def right_hand_split(
-    line: Line,
-    line_length: int,
-    features: Collection[Feature] = (),
-    omit: Collection[LeafID] = (),
-) -> Iterator[Line]:
-    """Split line into many lines, starting with the last matching bracket pair.
-
-    If the split was by optional parentheses, attempt splitting without them, too.
-    `omit` is a collection of closing bracket IDs that shouldn't be considered for
-    this split.
-
-    Note: running this function modifies `bracket_depth` on the leaves of `line`.
-    """
-    tail_leaves: List[Leaf] = []
-    body_leaves: List[Leaf] = []
-    head_leaves: List[Leaf] = []
-    current_leaves = tail_leaves
-    opening_bracket = None
-    closing_bracket = None
-    for leaf in reversed(line.leaves):
-        if current_leaves is body_leaves:
-            if leaf is opening_bracket:
-                current_leaves = head_leaves if body_leaves else tail_leaves
-        current_leaves.append(leaf)
-        if current_leaves is tail_leaves:
-            if leaf.type in CLOSING_BRACKETS and id(leaf) not in omit:
-                opening_bracket = leaf.opening_bracket
-                closing_bracket = leaf
-                current_leaves = body_leaves
-    if not (opening_bracket and closing_bracket and head_leaves):
-        # If there is no opening or closing_bracket that means the split failed and
-        # all content is in the tail.  Otherwise, if `head_leaves` are empty, it means
-        # the matching `opening_bracket` wasn't available on `line` anymore.
-        raise CannotSplit("No brackets found")
-
-    tail_leaves.reverse()
-    body_leaves.reverse()
-    head_leaves.reverse()
-    head = bracket_split_build_line(head_leaves, line, opening_bracket)
-    body = bracket_split_build_line(body_leaves, line, opening_bracket, is_body=True)
-    tail = bracket_split_build_line(tail_leaves, line, opening_bracket)
-    bracket_split_succeeded_or_raise(head, body, tail)
-    if (
-        # the body shouldn't be exploded
-        not body.should_explode
-        # the opening bracket is an optional paren
-        and opening_bracket.type == token.LPAR
-        and not opening_bracket.value
-        # the closing bracket is an optional paren
-        and closing_bracket.type == token.RPAR
-        and not closing_bracket.value
-        # it's not an import (optional parens are the only thing we can split on
-        # in this case; attempting a split without them is a waste of time)
-        and not line.is_import
-        # there are no standalone comments in the body
-        and not body.contains_standalone_comments(0)
-        # and we can actually remove the parens
-        and can_omit_invisible_parens(body, line_length)
-    ):
-        omit = {id(closing_bracket), *omit}
-        try:
-            yield from right_hand_split(line, line_length, features=features, omit=omit)
-            return
-
-        except CannotSplit:
-            if not (
-                can_be_split(body)
-                or is_line_short_enough(body, line_length=line_length)
-            ):
-                raise CannotSplit(
-                    "Splitting failed, body is still too long and can't be split."
-                )
-
-            elif head.contains_multiline_strings() or tail.contains_multiline_strings():
-                raise CannotSplit(
-                    "The current optional pair of parentheses is bound to fail to "
-                    "satisfy the splitting algorithm because the head or the tail "
-                    "contains multiline strings which by definition never fit one "
-                    "line."
-                )
-
-    ensure_visible(opening_bracket)
-    ensure_visible(closing_bracket)
-    for result in (head, body, tail):
-        if result:
-            yield result
-
-
-def bracket_split_succeeded_or_raise(head: Line, body: Line, tail: Line) -> None:
-    """Raise :exc:`CannotSplit` if the last left- or right-hand split failed.
-
-    Do nothing otherwise.
-
-    A left- or right-hand split is based on a pair of brackets. Content before
-    (and including) the opening bracket is left on one line, content inside the
-    brackets is put on a separate line, and finally content starting with and
-    following the closing bracket is put on a separate line.
-
-    Those are called `head`, `body`, and `tail`, respectively. If the split
-    produced the same line (all content in `head`) or ended up with an empty `body`
-    and the `tail` is just the closing bracket, then it's considered failed.
-    """
-    tail_len = len(str(tail).strip())
-    if not body:
-        if tail_len == 0:
-            raise CannotSplit("Splitting brackets produced the same line")
-
-        elif tail_len < 3:
-            raise CannotSplit(
-                f"Splitting brackets on an empty body to save "
-                f"{tail_len} characters is not worth it"
-            )
-
-
-def bracket_split_build_line(
-    leaves: List[Leaf], original: Line, opening_bracket: Leaf, *, is_body: bool = False
-) -> Line:
-    """Return a new line with given `leaves` and respective comments from `original`.
-
-    If `is_body` is True, the result line is one-indented inside brackets and as such
-    has its first leaf's prefix normalized and a trailing comma added when expected.
-    """
-    result = Line(depth=original.depth)
-    if is_body:
-        result.inside_brackets = True
-        result.depth += 1
-        if leaves:
-            # Since body is a new indent level, remove spurious leading whitespace.
-            normalize_prefix(leaves[0], inside_brackets=True)
-            # Ensure a trailing comma for imports and standalone function arguments, but
-            # be careful not to add one after any comments.
-            no_commas = original.is_def and not any(
-                l.type == token.COMMA for l in leaves
-            )
-
-            if original.is_import or no_commas:
-                for i in range(len(leaves) - 1, -1, -1):
-                    if leaves[i].type == STANDALONE_COMMENT:
-                        continue
-                    elif leaves[i].type == token.COMMA:
-                        break
-                    else:
-                        leaves.insert(i + 1, Leaf(token.COMMA, ","))
-                        break
-    # Populate the line
-    for leaf in leaves:
-        result.append(leaf, preformatted=True)
-        for comment_after in original.comments_after(leaf):
-            result.append(comment_after, preformatted=True)
-    if is_body:
-        result.should_explode = should_explode(result, opening_bracket)
-    return result
-
-
-def dont_increase_indentation(split_func: SplitFunc) -> SplitFunc:
-    """Normalize prefix of the first leaf in every line returned by `split_func`.
-
-    This is a decorator over relevant split functions.
-    """
-
-    @wraps(split_func)
-    def split_wrapper(line: Line, features: Collection[Feature] = ()) -> Iterator[Line]:
-        for l in split_func(line, features):
-            normalize_prefix(l.leaves[0], inside_brackets=True)
-            yield l
-
-    return split_wrapper
-
-
-@dont_increase_indentation
-def delimiter_split(line: Line, features: Collection[Feature] = ()) -> Iterator[Line]:
-    """Split according to delimiters of the highest priority.
-
-    If the appropriate Features are given, the split will add trailing commas
-    also in function signatures and calls that contain `*` and `**`.
-    """
-    try:
-        last_leaf = line.leaves[-1]
-    except IndexError:
-        raise CannotSplit("Line empty")
-
-    bt = line.bracket_tracker
-    try:
-        delimiter_priority = bt.max_delimiter_priority(exclude={id(last_leaf)})
-    except ValueError:
-        raise CannotSplit("No delimiters found")
-
-    if delimiter_priority == DOT_PRIORITY:
-        if bt.delimiter_count_with_priority(delimiter_priority) == 1:
-            raise CannotSplit("Splitting a single attribute from its owner looks wrong")
-
-    current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
-    lowest_depth = sys.maxsize
-    trailing_comma_safe = True
-
-    def append_to_line(leaf: Leaf) -> Iterator[Line]:
-        """Append `leaf` to current line or to new line if appending impossible."""
-        nonlocal current_line
-        try:
-            current_line.append_safe(leaf, preformatted=True)
-        except ValueError:
-            yield current_line
-
-            current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
-            current_line.append(leaf)
-
-    for leaf in line.leaves:
-        yield from append_to_line(leaf)
-
-        for comment_after in line.comments_after(leaf):
-            yield from append_to_line(comment_after)
-
-        lowest_depth = min(lowest_depth, leaf.bracket_depth)
-        if leaf.bracket_depth == lowest_depth:
-            if is_vararg(leaf, within={syms.typedargslist}):
-                trailing_comma_safe = (
-                    trailing_comma_safe and Feature.TRAILING_COMMA_IN_DEF in features
-                )
-            elif is_vararg(leaf, within={syms.arglist, syms.argument}):
-                trailing_comma_safe = (
-                    trailing_comma_safe and Feature.TRAILING_COMMA_IN_CALL in features
-                )
-
-        leaf_priority = bt.delimiters.get(id(leaf))
-        if leaf_priority == delimiter_priority:
-            yield current_line
-
-            current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
-    if current_line:
-        if (
-            trailing_comma_safe
-            and delimiter_priority == COMMA_PRIORITY
-            and current_line.leaves[-1].type != token.COMMA
-            and current_line.leaves[-1].type != STANDALONE_COMMENT
-        ):
-            current_line.append(Leaf(token.COMMA, ","))
-        yield current_line
-
-
-@dont_increase_indentation
-def standalone_comment_split(
-    line: Line, features: Collection[Feature] = ()
-) -> Iterator[Line]:
-    """Split standalone comments from the rest of the line."""
-    if not line.contains_standalone_comments(0):
-        raise CannotSplit("Line does not have any standalone comments")
-
-    current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
-
-    def append_to_line(leaf: Leaf) -> Iterator[Line]:
-        """Append `leaf` to current line or to new line if appending impossible."""
-        nonlocal current_line
-        try:
-            current_line.append_safe(leaf, preformatted=True)
-        except ValueError:
-            yield current_line
-
-            current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
-            current_line.append(leaf)
-
-    for leaf in line.leaves:
-        yield from append_to_line(leaf)
-
-        for comment_after in line.comments_after(leaf):
-            yield from append_to_line(comment_after)
-
-    if current_line:
-        yield current_line
-
-
-def is_import(leaf: Leaf) -> bool:
-    """Return True if the given leaf starts an import statement."""
-    p = leaf.parent
-    t = leaf.type
-    v = leaf.value
-    return bool(
-        t == token.NAME
-        and (
-            (v == "import" and p and p.type == syms.import_name)
-            or (v == "from" and p and p.type == syms.import_from)
-        )
-    )
-
-
-def is_type_comment(leaf: Leaf, suffix: str = "") -> bool:
-    """Return True if the given leaf is a special comment.
-    Only returns true for type comments for now."""
-    t = leaf.type
-    v = leaf.value
-    return t in {token.COMMENT, t == STANDALONE_COMMENT} and v.startswith(
-        "# type:" + suffix
-    )
-
-
-def normalize_prefix(leaf: Leaf, *, inside_brackets: bool) -> None:
-    """Leave existing extra newlines if not `inside_brackets`. Remove everything
-    else.
-
-    Note: don't use backslashes for formatting or you'll lose your voting rights.
-    """
-    if not inside_brackets:
-        spl = leaf.prefix.split("#")
-        if "\\" not in spl[0]:
-            nl_count = spl[-1].count("\n")
-            if len(spl) > 1:
-                nl_count -= 1
-            leaf.prefix = "\n" * nl_count
-            return
-
-    leaf.prefix = ""
-
-
-def normalize_string_prefix(leaf: Leaf, remove_u_prefix: bool = False) -> None:
-    """Make all string prefixes lowercase.
-
-    If remove_u_prefix is given, also removes any u prefix from the string.
-
-    Note: Mutates its argument.
-    """
-    match = re.match(r"^([furbFURB]*)(.*)$", leaf.value, re.DOTALL)
-    assert match is not None, f"failed to match string {leaf.value!r}"
-    orig_prefix = match.group(1)
-    new_prefix = orig_prefix.lower()
-    if remove_u_prefix:
-        new_prefix = new_prefix.replace("u", "")
-    leaf.value = f"{new_prefix}{match.group(2)}"
-
-
-def normalize_string_quotes(leaf: Leaf) -> None:
-    """Prefer double quotes but only if it doesn't cause more escaping.
-
-    Adds or removes backslashes as appropriate. Doesn't parse and fix
-    strings nested in f-strings (yet).
-
-    Note: Mutates its argument.
-    """
-    value = leaf.value.lstrip("furbFURB")
-    if value[:3] == '"""':
-        return
-
-    elif value[:3] == "'''":
-        orig_quote = "'''"
-        new_quote = '"""'
-    elif value[0] == '"':
-        orig_quote = '"'
-        new_quote = "'"
-    else:
-        orig_quote = "'"
-        new_quote = '"'
-    first_quote_pos = leaf.value.find(orig_quote)
-    if first_quote_pos == -1:
-        return  # There's an internal error
-
-    prefix = leaf.value[:first_quote_pos]
-    unescaped_new_quote = re.compile(rf"(([^\\]|^)(\\\\)*){new_quote}")
-    escaped_new_quote = re.compile(rf"([^\\]|^)\\((?:\\\\)*){new_quote}")
-    escaped_orig_quote = re.compile(rf"([^\\]|^)\\((?:\\\\)*){orig_quote}")
-    body = leaf.value[first_quote_pos + len(orig_quote) : -len(orig_quote)]
-    if "r" in prefix.casefold():
-        if unescaped_new_quote.search(body):
-            # There's at least one unescaped new_quote in this raw string
-            # so converting is impossible
-            return
-
-        # Do not introduce or remove backslashes in raw strings
-        new_body = body
-    else:
-        # remove unnecessary escapes
-        new_body = sub_twice(escaped_new_quote, rf"\1\2{new_quote}", body)
-        if body != new_body:
-            # Consider the string without unnecessary escapes as the original
-            body = new_body
-            leaf.value = f"{prefix}{orig_quote}{body}{orig_quote}"
-        new_body = sub_twice(escaped_orig_quote, rf"\1\2{orig_quote}", new_body)
-        new_body = sub_twice(unescaped_new_quote, rf"\1\\{new_quote}", new_body)
-    if "f" in prefix.casefold():
-        matches = re.findall(
-            r"""
-            (?:[^{]|^)\{  # start of the string or a non-{ followed by a single {
-                ([^{].*?)  # contents of the brackets except if begins with {{
-            \}(?:[^}]|$)  # A } followed by end of the string or a non-}
-            """,
-            new_body,
-            re.VERBOSE,
-        )
-        for m in matches:
-            if "\\" in str(m):
-                # Do not introduce backslashes in interpolated expressions
-                return
-    if new_quote == '"""' and new_body[-1:] == '"':
-        # edge case:
-        new_body = new_body[:-1] + '\\"'
-    orig_escape_count = body.count("\\")
-    new_escape_count = new_body.count("\\")
-    if new_escape_count > orig_escape_count:
-        return  # Do not introduce more escaping
-
-    if new_escape_count == orig_escape_count and orig_quote == '"':
-        return  # Prefer double quotes
-
-    leaf.value = f"{prefix}{new_quote}{new_body}{new_quote}"
-
-
-def normalize_numeric_literal(leaf: Leaf) -> None:
-    """Normalizes numeric (float, int, and complex) literals.
-
-    All letters used in the representation are normalized to lowercase (except
-    in Python 2 long literals).
-    """
-    text = leaf.value.lower()
-    if text.startswith(("0o", "0b")):
-        # Leave octal and binary literals alone.
-        pass
-    elif text.startswith("0x"):
-        # Change hex literals to upper case.
-        before, after = text[:2], text[2:]
-        text = f"{before}{after.upper()}"
-    elif "e" in text:
-        before, after = text.split("e")
-        sign = ""
-        if after.startswith("-"):
-            after = after[1:]
-            sign = "-"
-        elif after.startswith("+"):
-            after = after[1:]
-        before = format_float_or_int_string(before)
-        text = f"{before}e{sign}{after}"
-    elif text.endswith(("j", "l")):
-        number = text[:-1]
-        suffix = text[-1]
-        # Capitalize in "2L" because "l" looks too similar to "1".
-        if suffix == "l":
-            suffix = "L"
-        text = f"{format_float_or_int_string(number)}{suffix}"
-    else:
-        text = format_float_or_int_string(text)
-    leaf.value = text
-
-
-def format_float_or_int_string(text: str) -> str:
-    """Formats a float string like "1.0"."""
-    if "." not in text:
-        return text
-
-    before, after = text.split(".")
-    return f"{before or 0}.{after or 0}"
-
-
-def normalize_invisible_parens(node: Node, parens_after: Set[str]) -> None:
-    """Make existing optional parentheses invisible or create new ones.
-
-    `parens_after` is a set of string leaf values immediately after which parens
-    should be put.
-
-    Standardizes on visible parentheses for single-element tuples, and keeps
-    existing visible parentheses for other tuples and generator expressions.
-    """
-    for pc in list_comments(node.prefix, is_endmarker=False):
-        if pc.value in FMT_OFF:
-            # This `node` has a prefix with `# fmt: off`, don't mess with parens.
-            return
-
-    check_lpar = False
-    for index, child in enumerate(list(node.children)):
-        # Add parentheses around long tuple unpacking in assignments.
-        if (
-            index == 0
-            and isinstance(child, Node)
-            and child.type == syms.testlist_star_expr
-        ):
-            check_lpar = True
-
-        if check_lpar:
-            if is_walrus_assignment(child):
-                continue
-            if child.type == syms.atom:
-                # Determines if the underlying atom should be surrounded with
-                # invisible params - also makes parens invisible recursively
-                # within the atom and removes repeated invisible parens within
-                # the atom
-                should_surround_with_parens = maybe_make_parens_invisible_in_atom(
-                    child, parent=node
-                )
-
-                if should_surround_with_parens:
-                    lpar = Leaf(token.LPAR, "")
-                    rpar = Leaf(token.RPAR, "")
-                    index = child.remove() or 0
-                    node.insert_child(index, Node(syms.atom, [lpar, child, rpar]))
-            elif is_one_tuple(child):
-                # wrap child in visible parentheses
-                lpar = Leaf(token.LPAR, "(")
-                rpar = Leaf(token.RPAR, ")")
-                child.remove()
-                node.insert_child(index, Node(syms.atom, [lpar, child, rpar]))
-            elif node.type == syms.import_from:
-                # "import from" nodes store parentheses directly as part of
-                # the statement
-                if child.type == token.LPAR:
-                    # make parentheses invisible
-                    child.value = ""  # type: ignore
-                    node.children[-1].value = ""  # type: ignore
-                elif child.type != token.STAR:
-                    # insert invisible parentheses
-                    node.insert_child(index, Leaf(token.LPAR, ""))
-                    node.append_child(Leaf(token.RPAR, ""))
-                break
-
-            elif not (isinstance(child, Leaf) and is_multiline_string(child)):
-                # wrap child in invisible parentheses
-                lpar = Leaf(token.LPAR, "")
-                rpar = Leaf(token.RPAR, "")
-                index = child.remove() or 0
-                prefix = child.prefix
-                child.prefix = ""
-                new_child = Node(syms.atom, [lpar, child, rpar])
-                new_child.prefix = prefix
-                node.insert_child(index, new_child)
-
-        check_lpar = isinstance(child, Leaf) and child.value in parens_after
-
-
-def normalize_fmt_off(node: Node) -> None:
-    """Convert content between `# fmt: off`/`# fmt: on` into standalone comments."""
-    try_again = True
-    while try_again:
-        try_again = convert_one_fmt_off_pair(node)
-
-
-def convert_one_fmt_off_pair(node: Node) -> bool:
-    """Convert content of a single `# fmt: off`/`# fmt: on` into a standalone comment.
-
-    Returns True if a pair was converted.
-    """
-    for leaf in node.leaves():
-        previous_consumed = 0
-        for comment in list_comments(leaf.prefix, is_endmarker=False):
-            if comment.value in FMT_OFF:
-                # We only want standalone comments. If there's no previous leaf or
-                # the previous leaf is indentation, it's a standalone comment in
-                # disguise.
-                if comment.type != STANDALONE_COMMENT:
-                    prev = preceding_leaf(leaf)
-                    if prev and prev.type not in WHITESPACE:
-                        continue
-
-                ignored_nodes = list(generate_ignored_nodes(leaf))
-                if not ignored_nodes:
-                    continue
-
-                first = ignored_nodes[0]  # Can be a container node with the `leaf`.
-                parent = first.parent
-                prefix = first.prefix
-                first.prefix = prefix[comment.consumed :]
-                hidden_value = (
-                    comment.value + "\n" + "".join(str(n) for n in ignored_nodes)
-                )
-                if hidden_value.endswith("\n"):
-                    # That happens when one of the `ignored_nodes` ended with a NEWLINE
-                    # leaf (possibly followed by a DEDENT).
-                    hidden_value = hidden_value[:-1]
-                first_idx = None
-                for ignored in ignored_nodes:
-                    index = ignored.remove()
-                    if first_idx is None:
-                        first_idx = index
-                assert parent is not None, "INTERNAL ERROR: fmt: on/off handling (1)"
-                assert first_idx is not None, "INTERNAL ERROR: fmt: on/off handling (2)"
-                parent.insert_child(
-                    first_idx,
-                    Leaf(
-                        STANDALONE_COMMENT,
-                        hidden_value,
-                        prefix=prefix[:previous_consumed] + "\n" * comment.newlines,
-                    ),
-                )
-                return True
-
-            previous_consumed = comment.consumed
-
-    return False
-
-
-def generate_ignored_nodes(leaf: Leaf) -> Iterator[LN]:
-    """Starting from the container of `leaf`, generate all leaves until `# fmt: on`.
-
-    Stops at the end of the block.
-    """
-    container: Optional[LN] = container_of(leaf)
-    while container is not None and container.type != token.ENDMARKER:
-        for comment in list_comments(container.prefix, is_endmarker=False):
-            if comment.value in FMT_ON:
-                return
-
-        yield container
-
-        container = container.next_sibling
-
-
-def maybe_make_parens_invisible_in_atom(node: LN, parent: LN) -> bool:
-    """If it's safe, make the parens in the atom `node` invisible, recursively.
-    Additionally, remove repeated, adjacent invisible parens from the atom `node`
-    as they are redundant.
-
-    Returns whether the node should itself be wrapped in invisible parentheses.
-
-    """
-    if (
-        node.type != syms.atom
-        or is_empty_tuple(node)
-        or is_one_tuple(node)
-        or (is_yield(node) and parent.type != syms.expr_stmt)
-        or max_delimiter_priority_in_atom(node) >= COMMA_PRIORITY
-    ):
-        return False
-
-    first = node.children[0]
-    last = node.children[-1]
-    if first.type == token.LPAR and last.type == token.RPAR:
-        middle = node.children[1]
-        # make parentheses invisible
-        first.value = ""  # type: ignore
-        last.value = ""  # type: ignore
-        maybe_make_parens_invisible_in_atom(middle, parent=parent)
-
-        if is_atom_with_invisible_parens(middle):
-            # Strip the invisible parens from `middle` by replacing
-            # it with the child in-between the invisible parens
-            middle.replace(middle.children[1])
-
-        return False
-
-    return True
-
-
-def is_atom_with_invisible_parens(node: LN) -> bool:
-    """Given a `LN`, determines whether it's an atom `node` with invisible
-    parens. Useful in dedupe-ing and normalizing parens.
-    """
-    if isinstance(node, Leaf) or node.type != syms.atom:
-        return False
-
-    first, last = node.children[0], node.children[-1]
-    return (
-        isinstance(first, Leaf)
-        and first.type == token.LPAR
-        and first.value == ""
-        and isinstance(last, Leaf)
-        and last.type == token.RPAR
-        and last.value == ""
-    )
-
-
-def is_empty_tuple(node: LN) -> bool:
-    """Return True if `node` holds an empty tuple."""
-    return (
-        node.type == syms.atom
-        and len(node.children) == 2
-        and node.children[0].type == token.LPAR
-        and node.children[1].type == token.RPAR
-    )
-
-
-def unwrap_singleton_parenthesis(node: LN) -> Optional[LN]:
-    """Returns `wrapped` if `node` is of the shape ( wrapped ).
-
-    Parenthesis can be optional. Returns None otherwise"""
-    if len(node.children) != 3:
-        return None
-    lpar, wrapped, rpar = node.children
-    if not (lpar.type == token.LPAR and rpar.type == token.RPAR):
-        return None
-
-    return wrapped
-
-
-def is_one_tuple(node: LN) -> bool:
-    """Return True if `node` holds a tuple with one element, with or without parens."""
-    if node.type == syms.atom:
-        gexp = unwrap_singleton_parenthesis(node)
-        if gexp is None or gexp.type != syms.testlist_gexp:
-            return False
-
-        return len(gexp.children) == 2 and gexp.children[1].type == token.COMMA
-
-    return (
-        node.type in IMPLICIT_TUPLE
-        and len(node.children) == 2
-        and node.children[1].type == token.COMMA
-    )
-
-
-def is_walrus_assignment(node: LN) -> bool:
-    """Return True iff `node` is of the shape ( test := test )"""
-    inner = unwrap_singleton_parenthesis(node)
-    return inner is not None and inner.type == syms.namedexpr_test
-
-
-def is_yield(node: LN) -> bool:
-    """Return True if `node` holds a `yield` or `yield from` expression."""
-    if node.type == syms.yield_expr:
-        return True
-
-    if node.type == token.NAME and node.value == "yield":  # type: ignore
-        return True
-
-    if node.type != syms.atom:
-        return False
-
-    if len(node.children) != 3:
-        return False
-
-    lpar, expr, rpar = node.children
-    if lpar.type == token.LPAR and rpar.type == token.RPAR:
-        return is_yield(expr)
-
-    return False
-
-
-def is_vararg(leaf: Leaf, within: Set[NodeType]) -> bool:
-    """Return True if `leaf` is a star or double star in a vararg or kwarg.
-
-    If `within` includes VARARGS_PARENTS, this applies to function signatures.
-    If `within` includes UNPACKING_PARENTS, it applies to right hand-side
-    extended iterable unpacking (PEP 3132) and additional unpacking
-    generalizations (PEP 448).
-    """
-    if leaf.type not in VARARGS_SPECIALS or not leaf.parent:
-        return False
-
-    p = leaf.parent
-    if p.type == syms.star_expr:
-        # Star expressions are also used as assignment targets in extended
-        # iterable unpacking (PEP 3132).  See what its parent is instead.
-        if not p.parent:
-            return False
-
-        p = p.parent
-
-    return p.type in within
-
-
-def is_multiline_string(leaf: Leaf) -> bool:
-    """Return True if `leaf` is a multiline string that actually spans many lines."""
-    value = leaf.value.lstrip("furbFURB")
-    return value[:3] in {'"""', "'''"} and "\n" in value
-
-
-def is_stub_suite(node: Node) -> bool:
-    """Return True if `node` is a suite with a stub body."""
-    if (
-        len(node.children) != 4
-        or node.children[0].type != token.NEWLINE
-        or node.children[1].type != token.INDENT
-        or node.children[3].type != token.DEDENT
-    ):
-        return False
-
-    return is_stub_body(node.children[2])
-
-
-def is_stub_body(node: LN) -> bool:
-    """Return True if `node` is a simple statement containing an ellipsis."""
-    if not isinstance(node, Node) or node.type != syms.simple_stmt:
-        return False
-
-    if len(node.children) != 2:
-        return False
-
-    child = node.children[0]
-    return (
-        child.type == syms.atom
-        and len(child.children) == 3
-        and all(leaf == Leaf(token.DOT, ".") for leaf in child.children)
-    )
-
-
-def max_delimiter_priority_in_atom(node: LN) -> Priority:
-    """Return maximum delimiter priority inside `node`.
-
-    This is specific to atoms with contents contained in a pair of parentheses.
-    If `node` isn't an atom or there are no enclosing parentheses, returns 0.
-    """
-    if node.type != syms.atom:
-        return 0
-
-    first = node.children[0]
-    last = node.children[-1]
-    if not (first.type == token.LPAR and last.type == token.RPAR):
-        return 0
-
-    bt = BracketTracker()
-    for c in node.children[1:-1]:
-        if isinstance(c, Leaf):
-            bt.mark(c)
-        else:
-            for leaf in c.leaves():
-                bt.mark(leaf)
-    try:
-        return bt.max_delimiter_priority()
-
-    except ValueError:
-        return 0
-
-
-def ensure_visible(leaf: Leaf) -> None:
-    """Make sure parentheses are visible.
-
-    They could be invisible as part of some statements (see
-    :func:`normalize_invisible_parens` and :func:`visit_import_from`).
-    """
-    if leaf.type == token.LPAR:
-        leaf.value = "("
-    elif leaf.type == token.RPAR:
-        leaf.value = ")"
-
-
-def should_explode(line: Line, opening_bracket: Leaf) -> bool:
-    """Should `line` immediately be split with `delimiter_split()` after RHS?"""
-
-    if not (
-        opening_bracket.parent
-        and opening_bracket.parent.type in {syms.atom, syms.import_from}
-        and opening_bracket.value in "[{("
-    ):
-        return False
-
-    try:
-        last_leaf = line.leaves[-1]
-        exclude = {id(last_leaf)} if last_leaf.type == token.COMMA else set()
-        max_priority = line.bracket_tracker.max_delimiter_priority(exclude=exclude)
-    except (IndexError, ValueError):
-        return False
-
-    return max_priority == COMMA_PRIORITY
-
-
-def get_features_used(node: Node) -> Set[Feature]:
-    """Return a set of (relatively) new Python features used in this file.
-
-    Currently looking for:
-    - f-strings;
-    - underscores in numeric literals;
-    - trailing commas after * or ** in function signatures and calls;
-    - positional only arguments in function signatures and lambdas;
-    """
-    features: Set[Feature] = set()
-    for n in node.pre_order():
-        if n.type == token.STRING:
-            value_head = n.value[:2]  # type: ignore
-            if value_head in {'f"', 'F"', "f'", "F'", "rf", "fr", "RF", "FR"}:
-                features.add(Feature.F_STRINGS)
-
-        elif n.type == token.NUMBER:
-            if "_" in n.value:  # type: ignore
-                features.add(Feature.NUMERIC_UNDERSCORES)
-
-        elif n.type == token.SLASH:
-            if n.parent and n.parent.type in {syms.typedargslist, syms.arglist}:
-                features.add(Feature.POS_ONLY_ARGUMENTS)
-
-        elif n.type == token.COLONEQUAL:
-            features.add(Feature.ASSIGNMENT_EXPRESSIONS)
-
-        elif (
-            n.type in {syms.typedargslist, syms.arglist}
-            and n.children
-            and n.children[-1].type == token.COMMA
-        ):
-            if n.type == syms.typedargslist:
-                feature = Feature.TRAILING_COMMA_IN_DEF
-            else:
-                feature = Feature.TRAILING_COMMA_IN_CALL
-
-            for ch in n.children:
-                if ch.type in STARS:
-                    features.add(feature)
-
-                if ch.type == syms.argument:
-                    for argch in ch.children:
-                        if argch.type in STARS:
-                            features.add(feature)
-
-    return features
-
-
-def detect_target_versions(node: Node) -> Set[TargetVersion]:
-    """Detect the version to target based on the nodes used."""
-    features = get_features_used(node)
-    return {
-        version for version in TargetVersion if features <= VERSION_TO_FEATURES[version]
-    }
-
-
-def generate_trailers_to_omit(line: Line, line_length: int) -> Iterator[Set[LeafID]]:
-    """Generate sets of closing bracket IDs that should be omitted in a RHS.
-
-    Brackets can be omitted if the entire trailer up to and including
-    a preceding closing bracket fits in one line.
-
-    Yielded sets are cumulative (contain results of previous yields, too).  First
-    set is empty.
-    """
-
-    omit: Set[LeafID] = set()
-    yield omit
-
-    length = 4 * line.depth
-    opening_bracket = None
-    closing_bracket = None
-    inner_brackets: Set[LeafID] = set()
-    for index, leaf, leaf_length in enumerate_with_length(line, reversed=True):
-        length += leaf_length
-        if length > line_length:
-            break
-
-        has_inline_comment = leaf_length > len(leaf.value) + len(leaf.prefix)
-        if leaf.type == STANDALONE_COMMENT or has_inline_comment:
-            break
-
-        if opening_bracket:
-            if leaf is opening_bracket:
-                opening_bracket = None
-            elif leaf.type in CLOSING_BRACKETS:
-                inner_brackets.add(id(leaf))
-        elif leaf.type in CLOSING_BRACKETS:
-            if index > 0 and line.leaves[index - 1].type in OPENING_BRACKETS:
-                # Empty brackets would fail a split so treat them as "inner"
-                # brackets (e.g. only add them to the `omit` set if another
-                # pair of brackets was good enough.
-                inner_brackets.add(id(leaf))
-                continue
-
-            if closing_bracket:
-                omit.add(id(closing_bracket))
-                omit.update(inner_brackets)
-                inner_brackets.clear()
-                yield omit
-
-            if leaf.value:
-                opening_bracket = leaf.opening_bracket
-                closing_bracket = leaf
-
-
-def get_future_imports(node: Node) -> Set[str]:
-    """Return a set of __future__ imports in the file."""
-    imports: Set[str] = set()
-
-    def get_imports_from_children(children: List[LN]) -> Generator[str, None, None]:
-        for child in children:
-            if isinstance(child, Leaf):
-                if child.type == token.NAME:
-                    yield child.value
-            elif child.type == syms.import_as_name:
-                orig_name = child.children[0]
-                assert isinstance(orig_name, Leaf), "Invalid syntax parsing imports"
-                assert orig_name.type == token.NAME, "Invalid syntax parsing imports"
-                yield orig_name.value
-            elif child.type == syms.import_as_names:
-                yield from get_imports_from_children(child.children)
-            else:
-                raise AssertionError("Invalid syntax parsing imports")
-
-    for child in node.children:
-        if child.type != syms.simple_stmt:
-            break
-        first_child = child.children[0]
-        if isinstance(first_child, Leaf):
-            # Continue looking if we see a docstring; otherwise stop.
-            if (
-                len(child.children) == 2
-                and first_child.type == token.STRING
-                and child.children[1].type == token.NEWLINE
-            ):
-                continue
-            else:
-                break
-        elif first_child.type == syms.import_from:
-            module_name = first_child.children[1]
-            if not isinstance(module_name, Leaf) or module_name.value != "__future__":
-                break
-            imports |= set(get_imports_from_children(first_child.children[3:]))
-        else:
-            break
-    return imports
-
-
-def gen_python_files_in_dir(
-    path: Path,
-    root: Path,
-    include: Pattern[str],
-    exclude: Pattern[str],
-    report: "Report",
-) -> Iterator[Path]:
-    """Generate all files under `path` whose paths are not excluded by the
-    `exclude` regex, but are included by the `include` regex.
-
-    Symbolic links pointing outside of the `root` directory are ignored.
-
-    `report` is where output about exclusions goes.
-    """
-    assert root.is_absolute(), f"INTERNAL ERROR: `root` must be absolute but is {root}"
-    for child in path.iterdir():
-        try:
-            normalized_path = "/" + child.resolve().relative_to(root).as_posix()
-        except ValueError:
-            if child.is_symlink():
-                report.path_ignored(
-                    child, f"is a symbolic link that points outside {root}"
-                )
-                continue
-
-            raise
-
-        if child.is_dir():
-            normalized_path += "/"
-        exclude_match = exclude.search(normalized_path)
-        if exclude_match and exclude_match.group(0):
-            report.path_ignored(child, f"matches the --exclude regular expression")
-            continue
-
-        if child.is_dir():
-            yield from gen_python_files_in_dir(child, root, include, exclude, report)
-
-        elif child.is_file():
-            include_match = include.search(normalized_path)
-            if include_match:
-                yield child
-
-
-@lru_cache()
-def find_project_root(srcs: Iterable[str]) -> Path:
-    """Return a directory containing .git, .hg, or pyproject.toml.
-
-    That directory can be one of the directories passed in `srcs` or their
-    common parent.
-
-    If no directory in the tree contains a marker that would specify it's the
-    project root, the root of the file system is returned.
-    """
-    if not srcs:
-        return Path("/").resolve()
-
-    common_base = min(Path(src).resolve() for src in srcs)
-    if common_base.is_dir():
-        # Append a fake file so `parents` below returns `common_base_dir`, too.
-        common_base /= "fake-file"
-    for directory in common_base.parents:
-        if (directory / ".git").is_dir():
-            return directory
-
-        if (directory / ".hg").is_dir():
-            return directory
-
-        if (directory / "pyproject.toml").is_file():
-            return directory
-
-    return directory
-
-
-@dataclass
-class Report:
-    """Provides a reformatting counter. Can be rendered with `str(report)`."""
-
-    check: bool = False
-    quiet: bool = False
-    verbose: bool = False
-    change_count: int = 0
-    same_count: int = 0
-    failure_count: int = 0
-
-    def done(self, src: Path, changed: Changed) -> None:
-        """Increment the counter for successful reformatting. Write out a message."""
-        if changed is Changed.YES:
-            reformatted = "would reformat" if self.check else "reformatted"
-            if self.verbose or not self.quiet:
-                out(f"{reformatted} {src}")
-            self.change_count += 1
-        else:
-            if self.verbose:
-                if changed is Changed.NO:
-                    msg = f"{src} already well formatted, good job."
-                else:
-                    msg = f"{src} wasn't modified on disk since last run."
-                out(msg, bold=False)
-            self.same_count += 1
-
-    def failed(self, src: Path, message: str) -> None:
-        """Increment the counter for failed reformatting. Write out a message."""
-        err(f"error: cannot format {src}: {message}")
-        self.failure_count += 1
-
-    def path_ignored(self, path: Path, message: str) -> None:
-        if self.verbose:
-            out(f"{path} ignored: {message}", bold=False)
-
-    @property
-    def return_code(self) -> int:
-        """Return the exit code that the app should use.
-
-        This considers the current state of changed files and failures:
-        - if there were any failures, return 123;
-        - if any files were changed and --check is being used, return 1;
-        - otherwise return 0.
-        """
-        # According to http://tldp.org/LDP/abs/html/exitcodes.html starting with
-        # 126 we have special return codes reserved by the shell.
-        if self.failure_count:
-            return 123
-
-        elif self.change_count and self.check:
-            return 1
-
-        return 0
-
-    def __str__(self) -> str:
-        """Render a color report of the current state.
-
-        Use `click.unstyle` to remove colors.
-        """
-        if self.check:
-            reformatted = "would be reformatted"
-            unchanged = "would be left unchanged"
-            failed = "would fail to reformat"
-        else:
-            reformatted = "reformatted"
-            unchanged = "left unchanged"
-            failed = "failed to reformat"
-        report = []
-        if self.change_count:
-            s = "s" if self.change_count > 1 else ""
-            report.append(
-                click.style(f"{self.change_count} file{s} {reformatted}", bold=True)
-            )
-        if self.same_count:
-            s = "s" if self.same_count > 1 else ""
-            report.append(f"{self.same_count} file{s} {unchanged}")
-        if self.failure_count:
-            s = "s" if self.failure_count > 1 else ""
-            report.append(
-                click.style(f"{self.failure_count} file{s} {failed}", fg="red")
-            )
-        return ", ".join(report) + "."
-
-
-def parse_ast(src: str) -> Union[ast.AST, ast3.AST, ast27.AST]:
-    filename = "<unknown>"
-    if sys.version_info >= (3, 8):
-        # TODO: support Python 4+ ;)
-        for minor_version in range(sys.version_info[1], 4, -1):
-            try:
-                return ast.parse(src, filename, feature_version=(3, minor_version))
-            except SyntaxError:
-                continue
-    else:
-        for feature_version in (7, 6):
-            try:
-                return ast3.parse(src, filename, feature_version=feature_version)
-            except SyntaxError:
-                continue
-
-    return ast27.parse(src)
-
-
-def _fixup_ast_constants(
-    node: Union[ast.AST, ast3.AST, ast27.AST]
-) -> Union[ast.AST, ast3.AST, ast27.AST]:
-    """Map ast nodes deprecated in 3.8 to Constant."""
-    # casts are required until this is released:
-    # https://github.com/python/typeshed/pull/3142
-    if isinstance(node, (ast.Str, ast3.Str, ast27.Str, ast.Bytes, ast3.Bytes)):
-        return cast(ast.AST, ast.Constant(value=node.s))
-    elif isinstance(node, (ast.Num, ast3.Num, ast27.Num)):
-        return cast(ast.AST, ast.Constant(value=node.n))
-    elif isinstance(node, (ast.NameConstant, ast3.NameConstant)):
-        return cast(ast.AST, ast.Constant(value=node.value))
-    return node
-
-
-def assert_equivalent(src: str, dst: str) -> None:
-    """Raise AssertionError if `src` and `dst` aren't equivalent."""
-
-    def _v(node: Union[ast.AST, ast3.AST, ast27.AST], depth: int = 0) -> Iterator[str]:
-        """Simple visitor generating strings to compare ASTs by content."""
-
-        node = _fixup_ast_constants(node)
-
-        yield f"{'  ' * depth}{node.__class__.__name__}("
-
-        for field in sorted(node._fields):
-            # TypeIgnore has only one field 'lineno' which breaks this comparison
-            type_ignore_classes = (ast3.TypeIgnore, ast27.TypeIgnore)
-            if sys.version_info >= (3, 8):
-                type_ignore_classes += (ast.TypeIgnore,)
-            if isinstance(node, type_ignore_classes):
-                break
-
-            try:
-                value = getattr(node, field)
-            except AttributeError:
-                continue
-
-            yield f"{'  ' * (depth+1)}{field}="
-
-            if isinstance(value, list):
-                for item in value:
-                    # Ignore nested tuples within del statements, because we may insert
-                    # parentheses and they change the AST.
-                    if (
-                        field == "targets"
-                        and isinstance(node, (ast.Delete, ast3.Delete, ast27.Delete))
-                        and isinstance(item, (ast.Tuple, ast3.Tuple, ast27.Tuple))
-                    ):
-                        for item in item.elts:
-                            yield from _v(item, depth + 2)
-                    elif isinstance(item, (ast.AST, ast3.AST, ast27.AST)):
-                        yield from _v(item, depth + 2)
-
-            elif isinstance(value, (ast.AST, ast3.AST, ast27.AST)):
-                yield from _v(value, depth + 2)
-
-            else:
-                yield f"{'  ' * (depth+2)}{value!r},  # {value.__class__.__name__}"
-
-        yield f"{'  ' * depth})  # /{node.__class__.__name__}"
-
-    try:
-        src_ast = parse_ast(src)
-    except Exception as exc:
-        raise AssertionError(
-            f"cannot use --safe with this file; failed to parse source file.  "
-            f"AST error message: {exc}"
-        )
-
-    try:
-        dst_ast = parse_ast(dst)
-    except Exception as exc:
-        log = dump_to_file("".join(traceback.format_tb(exc.__traceback__)), dst)
-        raise AssertionError(
-            f"INTERNAL ERROR: Black produced invalid code: {exc}. "
-            f"Please report a bug on https://github.com/psf/black/issues.  "
-            f"This invalid output might be helpful: {log}"
-        ) from None
-
-    src_ast_str = "\n".join(_v(src_ast))
-    dst_ast_str = "\n".join(_v(dst_ast))
-    if src_ast_str != dst_ast_str:
-        log = dump_to_file(diff(src_ast_str, dst_ast_str, "src", "dst"))
-        raise AssertionError(
-            f"INTERNAL ERROR: Black produced code that is not equivalent to "
-            f"the source.  "
-            f"Please report a bug on https://github.com/psf/black/issues.  "
-            f"This diff might be helpful: {log}"
-        ) from None
-
-
-def assert_stable(src: str, dst: str, mode: FileMode) -> None:
-    """Raise AssertionError if `dst` reformats differently the second time."""
-    newdst = format_str(dst, mode=mode)
-    if dst != newdst:
-        log = dump_to_file(
-            diff(src, dst, "source", "first pass"),
-            diff(dst, newdst, "first pass", "second pass"),
-        )
-        raise AssertionError(
-            f"INTERNAL ERROR: Black produced different code on the second pass "
-            f"of the formatter.  "
-            f"Please report a bug on https://github.com/psf/black/issues.  "
-            f"This diff might be helpful: {log}"
-        ) from None
-
-
-def dump_to_file(*output: str) -> str:
-    """Dump `output` to a temporary file. Return path to the file."""
-    with tempfile.NamedTemporaryFile(
-        mode="w", prefix="blk_", suffix=".log", delete=False, encoding="utf8"
-    ) as f:
-        for lines in output:
-            f.write(lines)
-            if lines and lines[-1] != "\n":
-                f.write("\n")
-    return f.name
-
-
-@contextmanager
-def nullcontext() -> Iterator[None]:
-    """Return context manager that does nothing.
-    Similar to `nullcontext` from python 3.7"""
-    yield
-
-
-def diff(a: str, b: str, a_name: str, b_name: str) -> str:
-    """Return a unified diff string between strings `a` and `b`."""
-    import difflib
-
-    a_lines = [line + "\n" for line in a.split("\n")]
-    b_lines = [line + "\n" for line in b.split("\n")]
-    return "".join(
-        difflib.unified_diff(a_lines, b_lines, fromfile=a_name, tofile=b_name, n=5)
-    )
-
-
-def cancel(tasks: Iterable[asyncio.Task]) -> None:
-    """asyncio signal handler that cancels all `tasks` and reports to stderr."""
-    err("Aborted!")
-    for task in tasks:
-        task.cancel()
-
-
-def shutdown(loop: asyncio.AbstractEventLoop) -> None:
-    """Cancel all pending tasks on `loop`, wait for them, and close the loop."""
-    try:
-        if sys.version_info[:2] >= (3, 7):
-            all_tasks = asyncio.all_tasks
-        else:
-            all_tasks = asyncio.Task.all_tasks
-        # This part is borrowed from asyncio/runners.py in Python 3.7b2.
-        to_cancel = [task for task in all_tasks(loop) if not task.done()]
-        if not to_cancel:
-            return
-
-        for task in to_cancel:
-            task.cancel()
-        loop.run_until_complete(
-            asyncio.gather(*to_cancel, loop=loop, return_exceptions=True)
-        )
-    finally:
-        # `concurrent.futures.Future` objects cannot be cancelled once they
-        # are already running. There might be some when the `shutdown()` happened.
-        # Silence their logger's spew about the event loop being closed.
-        cf_logger = logging.getLogger("concurrent.futures")
-        cf_logger.setLevel(logging.CRITICAL)
-        loop.close()
-
-
-def sub_twice(regex: Pattern[str], replacement: str, original: str) -> str:
-    """Replace `regex` with `replacement` twice on `original`.
-
-    This is used by string normalization to perform replaces on
-    overlapping matches.
-    """
-    return regex.sub(replacement, regex.sub(replacement, original))
-
-
-def re_compile_maybe_verbose(regex: str) -> Pattern[str]:
-    """Compile a regular expression string in `regex`.
-
-    If it contains newlines, use verbose mode.
-    """
-    if "\n" in regex:
-        regex = "(?x)" + regex
-    return re.compile(regex)
-
-
-def enumerate_reversed(sequence: Sequence[T]) -> Iterator[Tuple[Index, T]]:
-    """Like `reversed(enumerate(sequence))` if that were possible."""
-    index = len(sequence) - 1
-    for element in reversed(sequence):
-        yield (index, element)
-        index -= 1
-
-
-def enumerate_with_length(
-    line: Line, reversed: bool = False
-) -> Iterator[Tuple[Index, Leaf, int]]:
-    """Return an enumeration of leaves with their length.
-
-    Stops prematurely on multiline strings and standalone comments.
-    """
-    op = cast(
-        Callable[[Sequence[Leaf]], Iterator[Tuple[Index, Leaf]]],
-        enumerate_reversed if reversed else enumerate,
-    )
-    for index, leaf in op(line.leaves):
-        length = len(leaf.prefix) + len(leaf.value)
-        if "\n" in leaf.value:
-            return  # Multiline strings, we can't continue.
-
-        for comment in line.comments_after(leaf):
-            length += len(comment.value)
-
-        yield index, leaf, length
-
-
-def is_line_short_enough(line: Line, *, line_length: int, line_str: str = "") -> bool:
-    """Return True if `line` is no longer than `line_length`.
-
-    Uses the provided `line_str` rendering, if any, otherwise computes a new one.
-    """
-    if not line_str:
-        line_str = str(line).strip("\n")
-    return (
-        len(line_str) <= line_length
-        and "\n" not in line_str  # multiline strings
-        and not line.contains_standalone_comments()
-    )
-
-
-def can_be_split(line: Line) -> bool:
-    """Return False if the line cannot be split *for sure*.
-
-    This is not an exhaustive search but a cheap heuristic that we can use to
-    avoid some unfortunate formattings (mostly around wrapping unsplittable code
-    in unnecessary parentheses).
-    """
-    leaves = line.leaves
-    if len(leaves) < 2:
-        return False
-
-    if leaves[0].type == token.STRING and leaves[1].type == token.DOT:
-        call_count = 0
-        dot_count = 0
-        next = leaves[-1]
-        for leaf in leaves[-2::-1]:
-            if leaf.type in OPENING_BRACKETS:
-                if next.type not in CLOSING_BRACKETS:
-                    return False
-
-                call_count += 1
-            elif leaf.type == token.DOT:
-                dot_count += 1
-            elif leaf.type == token.NAME:
-                if not (next.type == token.DOT or next.type in OPENING_BRACKETS):
-                    return False
-
-            elif leaf.type not in CLOSING_BRACKETS:
-                return False
-
-            if dot_count > 1 and call_count > 1:
-                return False
-
-    return True
-
-
-def can_omit_invisible_parens(line: Line, line_length: int) -> bool:
-    """Does `line` have a shape safe to reformat without optional parens around it?
-
-    Returns True for only a subset of potentially nice looking formattings but
-    the point is to not return false positives that end up producing lines that
-    are too long.
-    """
-    bt = line.bracket_tracker
-    if not bt.delimiters:
-        # Without delimiters the optional parentheses are useless.
-        return True
-
-    max_priority = bt.max_delimiter_priority()
-    if bt.delimiter_count_with_priority(max_priority) > 1:
-        # With more than one delimiter of a kind the optional parentheses read better.
-        return False
-
-    if max_priority == DOT_PRIORITY:
-        # A single stranded method call doesn't require optional parentheses.
-        return True
-
-    assert len(line.leaves) >= 2, "Stranded delimiter"
-
-    first = line.leaves[0]
-    second = line.leaves[1]
-    penultimate = line.leaves[-2]
-    last = line.leaves[-1]
-
-    # With a single delimiter, omit if the expression starts or ends with
-    # a bracket.
-    if first.type in OPENING_BRACKETS and second.type not in CLOSING_BRACKETS:
-        remainder = False
-        length = 4 * line.depth
-        for _index, leaf, leaf_length in enumerate_with_length(line):
-            if leaf.type in CLOSING_BRACKETS and leaf.opening_bracket is first:
-                remainder = True
-            if remainder:
-                length += leaf_length
-                if length > line_length:
-                    break
-
-                if leaf.type in OPENING_BRACKETS:
-                    # There are brackets we can further split on.
-                    remainder = False
-
-        else:
-            # checked the entire string and line length wasn't exceeded
-            if len(line.leaves) == _index + 1:
-                return True
-
-        # Note: we are not returning False here because a line might have *both*
-        # a leading opening bracket and a trailing closing bracket.  If the
-        # opening bracket doesn't match our rule, maybe the closing will.
-
-    if (
-        last.type == token.RPAR
-        or last.type == token.RBRACE
-        or (
-            # don't use indexing for omitting optional parentheses;
-            # it looks weird
-            last.type == token.RSQB
-            and last.parent
-            and last.parent.type != syms.trailer
-        )
-    ):
-        if penultimate.type in OPENING_BRACKETS:
-            # Empty brackets don't help.
-            return False
-
-        if is_multiline_string(first):
-            # Additional wrapping of a multiline string in this situation is
-            # unnecessary.
-            return True
-
-        length = 4 * line.depth
-        seen_other_brackets = False
-        for _index, leaf, leaf_length in enumerate_with_length(line):
-            length += leaf_length
-            if leaf is last.opening_bracket:
-                if seen_other_brackets or length <= line_length:
-                    return True
-
-            elif leaf.type in OPENING_BRACKETS:
-                # There are brackets we can further split on.
-                seen_other_brackets = True
-
-    return False
-
-
-def get_cache_file(mode: FileMode) -> Path:
-    return CACHE_DIR / f"cache.{mode.get_cache_key()}.pickle"
-
-
-def read_cache(mode: FileMode) -> Cache:
-    """Read the cache if it exists and is well formed.
-
-    If it is not well formed, the call to write_cache later should resolve the issue.
-    """
-    cache_file = get_cache_file(mode)
-    if not cache_file.exists():
-        return {}
-
-    with cache_file.open("rb") as fobj:
-        try:
-            cache: Cache = pickle.load(fobj)
-        except pickle.UnpicklingError:
-            return {}
-
-    return cache
-
-
-def get_cache_info(path: Path) -> CacheInfo:
-    """Return the information used to check if a file is already formatted or not."""
-    stat = path.stat()
-    return stat.st_mtime, stat.st_size
-
-
-def filter_cached(cache: Cache, sources: Iterable[Path]) -> Tuple[Set[Path], Set[Path]]:
-    """Split an iterable of paths in `sources` into two sets.
-
-    The first contains paths of files that modified on disk or are not in the
-    cache. The other contains paths to non-modified files.
-    """
-    todo, done = set(), set()
-    for src in sources:
-        src = src.resolve()
-        if cache.get(src) != get_cache_info(src):
-            todo.add(src)
-        else:
-            done.add(src)
-    return todo, done
-
-
-def write_cache(cache: Cache, sources: Iterable[Path], mode: FileMode) -> None:
-    """Update the cache file."""
-    cache_file = get_cache_file(mode)
-    try:
-        CACHE_DIR.mkdir(parents=True, exist_ok=True)
-        new_cache = {**cache, **{src.resolve(): get_cache_info(src) for src in sources}}
-        with tempfile.NamedTemporaryFile(dir=str(cache_file.parent), delete=False) as f:
-            pickle.dump(new_cache, f, protocol=pickle.HIGHEST_PROTOCOL)
-        os.replace(f.name, cache_file)
-    except OSError:
-        pass
-
-
-def patch_click() -> None:
-    """Make Click not crash.
-
-    On certain misconfigured environments, Python 3 selects the ASCII encoding as the
-    default which restricts paths that it can access during the lifetime of the
-    application.  Click refuses to work in this scenario by raising a RuntimeError.
-
-    In case of Black the likelihood that non-ASCII characters are going to be used in
-    file paths is minimal since it's Python source code.  Moreover, this crash was
-    spurious on Python 3.7 thanks to PEP 538 and PEP 540.
-    """
-    try:
-        from click import core
-        from click import _unicodefun  # type: ignore
-    except ModuleNotFoundError:
-        return
-
-    for module in (core, _unicodefun):
-        if hasattr(module, "_verify_python3_env"):
-            module._verify_python3_env = lambda: None
-
-
-def patched_main() -> None:
-    freeze_support()
-    patch_click()
-    main()
-
-
-if __name__ == "__main__":
-    patched_main()
--- a/contrib/import-checker.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/contrib/import-checker.py	Tue Nov 05 13:19:24 2019 -0800
@@ -4,6 +4,7 @@
 
 import ast
 import collections
+import io
 import os
 import sys
 
@@ -754,7 +755,11 @@
             yield src.read(), modname, f, 0
             py = True
     if py or f.endswith('.t'):
-        with open(f, 'r') as src:
+        # Strictly speaking we should sniff for the magic header that denotes
+        # Python source file encoding. But in reality we don't use anything
+        # other than ASCII (mainly) and UTF-8 (in a few exceptions), so
+        # simplicity is fine.
+        with io.open(f, 'r', encoding='utf-8') as src:
             for script, modname, t, line in embedded(f, modname, src):
                 yield script, modname.encode('utf8'), t, line
 
--- a/contrib/install-windows-dependencies.ps1	Mon Nov 04 00:16:44 2019 +0100
+++ b/contrib/install-windows-dependencies.ps1	Tue Nov 05 13:19:24 2019 -0800
@@ -22,10 +22,10 @@
 $VC9_PYTHON_URL = "https://download.microsoft.com/download/7/9/6/796EF2E4-801B-4FC4-AB28-B59FBF6D907B/VCForPython27.msi"
 $VC9_PYTHON_SHA256 = "070474db76a2e625513a5835df4595df9324d820f9cc97eab2a596dcbc2f5cbf"
 
-$PYTHON27_x64_URL = "https://www.python.org/ftp/python/2.7.16/python-2.7.16.amd64.msi"
-$PYTHON27_x64_SHA256 = "7c0f45993019152d46041a7db4b947b919558fdb7a8f67bcd0535bc98d42b603"
-$PYTHON27_X86_URL = "https://www.python.org/ftp/python/2.7.16/python-2.7.16.msi"
-$PYTHON27_X86_SHA256 = "d57dc3e1ba490aee856c28b4915d09e3f49442461e46e481bc6b2d18207831d7"
+$PYTHON27_x64_URL = "https://www.python.org/ftp/python/2.7.17/python-2.7.17.amd64.msi"
+$PYTHON27_x64_SHA256 = "3b934447e3620e51d2daf5b2f258c9b617bcc686ca2f777a49aa3b47893abf1b"
+$PYTHON27_X86_URL = "https://www.python.org/ftp/python/2.7.17/python-2.7.17.msi"
+$PYTHON27_X86_SHA256 = "a4e3a321517c6b0c2693d6f712a0d18c82600b3d0c759c299b3d14384a17f863"
 
 $PYTHON35_x86_URL = "https://www.python.org/ftp/python/3.5.4/python-3.5.4.exe"
 $PYTHON35_x86_SHA256 = "F27C2D67FD9688E4970F3BFF799BB9D722A0D6C2C13B04848E1F7D620B524B0E"
@@ -37,10 +37,10 @@
 $PYTHON36_x64_URL = "https://www.python.org/ftp/python/3.6.8/python-3.6.8-amd64.exe"
 $PYTHON36_x64_SHA256 = "96088A58B7C43BC83B84E6B67F15E8706C614023DD64F9A5A14E81FF824ADADC"
 
-$PYTHON37_x86_URL = "https://www.python.org/ftp/python/3.7.4/python-3.7.4.exe"
-$PYTHON37_x86_SHA256 = "9a30ab5568ba37bfbcae5cdee19e9dc30765c42cf066f605221563ff8b20ee34"
-$PYTHON37_X64_URL = "https://www.python.org/ftp/python/3.7.4/python-3.7.4-amd64.exe"
-$PYTHON37_x64_SHA256 = "bab92f987320975c7826171a072bfd64f8f0941aaf2cdeba6924b7025c9968a3"
+$PYTHON37_x86_URL = "https://www.python.org/ftp/python/3.7.5/python-3.7.5.exe"
+$PYTHON37_x86_SHA256 = "3c2ae8f72b48e6e0c2b482206e322bf5d0344ff91abc3b3c200cec9e275c7168"
+$PYTHON37_X64_URL = "https://www.python.org/ftp/python/3.7.5/python-3.7.5-amd64.exe"
+$PYTHON37_x64_SHA256 = "f3d60c127e7a92ed547efa3321bf70cd96b75c53bf4b903147015257c1314981"
 
 $PYTHON38_x86_URL = "https://www.python.org/ftp/python/3.8.0/python-3.8.0.exe"
 $PYTHON38_x86_SHA256 = "b471908de5e10d8fb5c3351a5affb1172da7790c533e0c9ffbaeec9c11611b15"
--- a/contrib/packaging/Makefile	Mon Nov 04 00:16:44 2019 +0100
+++ b/contrib/packaging/Makefile	Tue Nov 05 13:19:24 2019 -0800
@@ -11,19 +11,17 @@
   cosmic \
   disco
 
-FEDORA_RELEASES := \
-  20 \
-  21 \
-  28 \
-  29
+FEDORA_RELEASE := 31
 
 CENTOS_RELEASES := \
   5 \
   6 \
-  7
+  7 \
+  8
 
 # Build a Python for these CentOS releases.
 CENTOS_WITH_PYTHON_RELEASES := 5 6
+CENTOS_WITH_NONVERSIONED_PYTHON := 5 6 7
 
 help:
 	@echo 'Packaging Make Targets'
@@ -34,8 +32,8 @@
 	@echo 'docker-debian-{$(strip $(DEBIAN_CODENAMES))}'
 	@echo '   Build Debian packages specific to a Debian distro using Docker.'
 	@echo ''
-	@echo 'docker-fedora{$(strip $(FEDORA_RELEASES))}'
-	@echo '   Build an RPM for a specific Fedora version using Docker.'
+	@echo 'docker-fedora'
+	@echo '   Build an RPM for a Fedora $(FEDORA_RELEASE) using Docker.'
 	@echo ''
 	@echo 'docker-ubuntu-{$(strip $(UBUNTU_CODENAMES))}'
 	@echo '   Build Debian package specific to an Ubuntu distro using Docker.'
@@ -59,8 +57,8 @@
 	@echo 'centos{$(strip $(CENTOS_RELEASES))}'
 	@echo '   Build an RPM for a specific CentOS version locally'
 	@echo ''
-	@echo 'fedora{$(strip $(FEDORA_RELEASES))}'
-	@echo '   Build an RPM for a specific Fedora version locally'
+	@echo 'fedora'
+	@echo '   Build an RPM for Fedora $(FEDORA_RELEASE) locally'
 
 .PHONY: help
 
@@ -97,37 +95,30 @@
 $(foreach codename,$(UBUNTU_CODENAMES),$(eval $(call ubuntu_targets,$(codename))))
 
 # Fedora targets.
-define fedora_targets
-.PHONY: fedora$(1)
-fedora$(1):
-	mkdir -p $$(HGROOT)/packages/fedora$(1)
+.PHONY: fedora
+fedora:
+	mkdir -p $(HGROOT)/packages/fedora$(FEDORA_RELEASE)
 	./buildrpm
-	cp $$(HGROOT)/contrib/packaging/rpmbuild/RPMS/*/* $$(HGROOT)/packages/fedora$(1)
-	cp $$(HGROOT)/contrib/packaging/rpmbuild/SRPMS/* $$(HGROOT)/packages/fedora$(1)
+	cp $(HGROOT)/contrib/packaging/rpmbuild/RPMS/*/* $(HGROOT)/packages/fedora$(FEDORA_RELEASE)
+	cp $(HGROOT)/contrib/packaging/rpmbuild/SRPMS/* $(HGROOT)/packages/fedora$(FEDORA_RELEASE)
 	rm -rf $(HGROOT)/rpmbuild
 
-.PHONY: docker-fedora$(1)
-docker-fedora$(1):
-	mkdir -p $$(HGROOT)/packages/fedora$(1)
-	./dockerrpm fedora$(1)
-
-endef
-
-$(foreach release,$(FEDORA_RELEASES),$(eval $(call fedora_targets,$(release))))
+.PHONY: docker-fedora
+docker-fedora:
+	./dockerrpm fedora$(FEDORA_RELEASE)
 
 # CentOS targets.
 define centos_targets
 .PHONY: centos$(1)
 centos$(1):
 	mkdir -p $$(HGROOT)/packages/centos$(1)
-	./buildrpm $$(if $$(filter $(1),$$(CENTOS_WITH_PYTHON_RELEASES)),--withpython)
+	./buildrpm $$(if $$(filter $(1),$$(CENTOS_WITH_PYTHON_RELEASES)),--withpython,$$(if $$(filter $(1),$$(CENTOS_WITH_NONVERSIONED_PYTHON)),--python python,))
 	cp $$(HGROOT)/contrib/packaging/rpmbuild/RPMS/*/* $$(HGROOT)/packages/centos$(1)
 	cp $$(HGROOT)/contrib/packaging/rpmbuild/SRPMS/* $$(HGROOT)/packages/centos$(1)
 
 .PHONY: docker-centos$(1)
 docker-centos$(1):
-	mkdir -p $$(HGROOT)/packages/centos$(1)
-	./dockerrpm centos$(1) $$(if $$(filter $(1),$$(CENTOS_WITH_PYTHON_RELEASES)),--withpython)
+	./dockerrpm centos$(1) $$(if $$(filter $(1),$$(CENTOS_WITH_PYTHON_RELEASES)),--withpython,$$(if $$(filter $(1),$$(CENTOS_WITH_NONVERSIONED_PYTHON)),--python python,))
 
 endef
 
--- a/contrib/packaging/builddeb	Mon Nov 04 00:16:44 2019 +0100
+++ b/contrib/packaging/builddeb	Tue Nov 05 13:19:24 2019 -0800
@@ -106,10 +106,10 @@
     echo
     OUTPUTDIR=${OUTPUTDIR:=packages/$DISTID-$CODENAME}
     mkdir -p "$OUTPUTDIR"
-    find ../mercurial*.deb ../mercurial_*.build ../mercurial_*.changes \
+    find ../mercurial*.deb ../mercurial_*.build* ../mercurial_*.changes \
           ../mercurial*.dsc ../mercurial*.gz \
           -type f -newer $control -print0 2>/dev/null | \
       xargs -Inarf -0 mv narf "$OUTPUTDIR"
     echo "Built packages for $debver:"
-    find "$PWD"/"$OUTPUTDIR" -type f -newer $control -name '*.deb'
+    find "$OUTPUTDIR" -type f -newer $control -name '*.deb'
 fi
--- a/contrib/packaging/buildrpm	Mon Nov 04 00:16:44 2019 +0100
+++ b/contrib/packaging/buildrpm	Tue Nov 05 13:19:24 2019 -0800
@@ -1,16 +1,12 @@
 #!/bin/bash -e
 #
-# Build a Mercurial RPM from the current repo
-#
-# Tested on
-# - Fedora 20
-# - CentOS 5
-# - centOS 6
+# Build a Mercurial RPM from the current repo, mainly for Fedora/CentOS/RHEL
 
 . $(dirname $0)/packagelib.sh
 
 BUILD=1
 RPMBUILDDIR="$PWD/rpmbuild"
+PYTHONEXE=python3
 
 while [ "$1" ]; do
     case "$1" in
@@ -18,10 +14,16 @@
         shift
         BUILD=
         ;;
+    --python)
+        shift
+        PYTHONEXE=$1
+        shift
+        ;;
     --withpython | --with-python)
         shift
         PYTHONVER=2.7.16
         PYTHONMD5=f1a2ace631068444831d01485466ece0
+        PYTHONEXE=python
         ;;
     --rpmbuilddir )
         shift
@@ -51,7 +53,7 @@
 gethgversion
 
 if [ -z "$type" ] ; then
-   release=1
+    release=1
 else
     release=0.9_$type
 fi
@@ -96,6 +98,7 @@
 
 sed -e "s,^Version:.*,Version: $version," \
     -e "s,^Release:.*,Release: $release," \
+    -e "s/^%global pythonexe .*/%global pythonexe $PYTHONEXE/" \
     $specfile > $rpmspec
 
 echo >> $rpmspec
@@ -121,8 +124,8 @@
     if prevtitle != title:
         prevtitle = title
         print
-        print title
-    print "- %s" % l[3].strip()
+        print(title)
+    print("- %s" % l[3].strip())
 ' >> $rpmspec
 
 else
@@ -138,7 +141,7 @@
 for l in sys.stdin.readlines():
     tok = l.split("\t")
     hgdate = tuple(int(v) for v in tok[0].split())
-    print "* %s %s\n- %s" % (datestr(hgdate, "%a %b %d %Y"), tok[1], tok[2])
+    print("* %s %s\n- %s" % (datestr(hgdate, "%a %b %d %Y"), tok[1], tok[2]))
 ' >> $rpmspec
 
 fi
--- a/contrib/packaging/debian/control	Mon Nov 04 00:16:44 2019 +0100
+++ b/contrib/packaging/debian/control	Tue Nov 05 13:19:24 2019 -0800
@@ -7,21 +7,24 @@
  dh-python,
  less,
  netbase,
- python-all,
- python-all-dev,
- python-docutils,
+ python3-all,
+ python3-all-dev,
+ python3-docutils,
  unzip,
  zip
 Standards-Version: 3.9.4
-X-Python-Version: >= 2.7
+X-Python3-Version: >= 3.5
 
 Package: mercurial
 Depends:
- python,
+ sensible-utils,
  ${shlibs:Depends},
  ${misc:Depends},
- ${python:Depends},
- mercurial-common (= ${source:Version})
+ ${python3:Depends},
+Recommends: ca-certificates
+Suggests: wish
+Replaces: mercurial-common
+Breaks: mercurial-common
 Architecture: any
 Description: fast, easy to use, distributed revision control tool.
  Mercurial is a fast, lightweight Source Control Management system designed
@@ -36,19 +39,3 @@
   * Easy-to-use command-line interface
   * Integrated stand-alone web interface
   * Small Python codebase
-
-Package: mercurial-common
-Architecture: all
-Depends:
- ${misc:Depends},
- ${python:Depends},
-Recommends: mercurial (= ${source:Version}), ca-certificates
-Suggests: wish
-Breaks: mercurial (<< ${source:Version})
-Replaces: mercurial (<< 2.6.3)
-Description: easy-to-use, scalable distributed version control system (common files)
- Mercurial is a fast, lightweight Source Control Management system designed
- for efficient handling of very large distributed projects.
- .
- This package contains the architecture independent components of Mercurial,
- and is generally useless without the mercurial package.
--- a/contrib/packaging/debian/rules	Mon Nov 04 00:16:44 2019 +0100
+++ b/contrib/packaging/debian/rules	Tue Nov 05 13:19:24 2019 -0800
@@ -4,41 +4,41 @@
 
 CPUS=$(shell cat /proc/cpuinfo | grep -E ^processor | wc -l)
 
+export HGPYTHON3=1
+export PYTHON=python3
+
 %:
-	dh $@ --with python2
+	dh $@ --with python3
 
 override_dh_auto_test:
 	http_proxy='' dh_auto_test -- TESTFLAGS="-j$(CPUS)"
 
-override_dh_python2:
-	dh_python2
-	find debian/mercurial/usr/share -type d -empty -delete
+override_dh_python3:
+	dh_python3 --shebang=/usr/bin/python3
+
+override_dh_auto_clean:
+	$(MAKE) cleanbutpackages
+	$(MAKE) -C contrib/chg clean
 
-override_dh_install:
-	python$(PYVERS) setup.py install --root "$(CURDIR)"/debian/mercurial --install-layout=deb
+override_dh_auto_build:
+	$(MAKE) all
+	$(MAKE) -C contrib/chg all
+
+override_dh_auto_install:
+	python3 setup.py install --root "$(CURDIR)"/debian/mercurial --install-layout=deb
 	# chg
 	make -C contrib/chg \
 		DESTDIR="$(CURDIR)"/debian/mercurial \
 		PREFIX=/usr \
-		clean install
-	# remove arch-independent python stuff
-	find "$(CURDIR)"/debian/mercurial/usr/lib \
-		! -name '*.so' ! -type d -delete , \
-		-type d -empty -delete
-	python$(PYVERS) setup.py install --root "$(CURDIR)/debian/mercurial-common" --install-layout=deb
-	make install-doc PREFIX="$(CURDIR)"/debian/mercurial-common/usr
-	# remove arch-dependent python stuff
-	find "$(CURDIR)"/debian/mercurial-common/usr/lib \
-		-name '*.so' ! -type d -delete , \
-		-type d -empty -delete
-	cp contrib/hg-ssh "$(CURDIR)"/debian/mercurial-common/usr/bin
-	mkdir -p "$(CURDIR)"/debian/mercurial-common/usr/share/mercurial
-	cp contrib/hgk "$(CURDIR)"/debian/mercurial-common/usr/share/mercurial
-	mkdir -p "$(CURDIR)"/debian/mercurial-common/etc/mercurial/hgrc.d/
-	cp contrib/packaging/debian/*.rc "$(CURDIR)"/debian/mercurial-common/etc/mercurial/hgrc.d/
+		install
+	make install-doc PREFIX="$(CURDIR)"/debian/mercurial/usr
+	cp contrib/hg-ssh "$(CURDIR)"/debian/mercurial/usr/bin
+	mkdir -p "$(CURDIR)"/debian/mercurial/usr/share/mercurial
+	cp contrib/hgk "$(CURDIR)"/debian/mercurial/usr/share/mercurial
+	mkdir -p "$(CURDIR)"/debian/mercurial/etc/mercurial/hgrc.d/
+	cp contrib/packaging/debian/*.rc "$(CURDIR)"/debian/mercurial/etc/mercurial/hgrc.d/
 	# completions
-	mkdir -p "$(CURDIR)"/debian/mercurial-common/usr/share/bash-completion/completions
-	cp contrib/bash_completion "$(CURDIR)"/debian/mercurial-common/usr/share/bash-completion/completions/hg
-	mkdir -p "$(CURDIR)"/debian/mercurial-common/usr/share/zsh/vendor-completions
-	cp contrib/zsh_completion "$(CURDIR)"/debian/mercurial-common/usr/share/zsh/vendor-completions/_hg
-	rm "$(CURDIR)"/debian/mercurial-common/usr/bin/hg
+	mkdir -p "$(CURDIR)"/debian/mercurial/usr/share/bash-completion/completions
+	cp contrib/bash_completion "$(CURDIR)"/debian/mercurial/usr/share/bash-completion/completions/hg
+	mkdir -p "$(CURDIR)"/debian/mercurial/usr/share/zsh/vendor-completions
+	cp contrib/zsh_completion "$(CURDIR)"/debian/mercurial/usr/share/zsh/vendor-completions/_hg
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/contrib/packaging/docker/centos8	Tue Nov 05 13:19:24 2019 -0800
@@ -0,0 +1,15 @@
+FROM centos:centos8
+
+RUN groupadd -g %GID% build && \
+    useradd -u %UID% -g %GID% -s /bin/bash -d /build -m build
+
+RUN yum install -y \
+	gcc \
+	gettext \
+	make \
+	python3-devel \
+	python3-docutils \
+	rpm-build
+
+# For creating repo meta data
+RUN yum install -y createrepo
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/contrib/packaging/docker/fedora.template	Tue Nov 05 13:19:24 2019 -0800
@@ -0,0 +1,15 @@
+FROM fedora:%OS_RELEASE%
+
+RUN groupadd -g 1000 build && \
+    useradd -u 1000 -g 1000 -s /bin/bash -d /build -m build
+
+RUN dnf install -y \
+	gcc \
+	gettext \
+	make \
+	python3-devel \
+	python3-docutils \
+	rpm-build
+
+# For creating repo meta data
+RUN dnf install -y createrepo
--- a/contrib/packaging/docker/fedora20	Mon Nov 04 00:16:44 2019 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,15 +0,0 @@
-FROM fedora:20
-
-RUN groupadd -g 1000 build && \
-    useradd -u 1000 -g 1000 -s /bin/bash -d /build -m build
-
-RUN yum install -y \
-	gcc \
-	gettext \
-	make \
-	python-devel \
-	python-docutils \
-	rpm-build
-
-# For creating repo meta data
-RUN yum install -y createrepo
--- a/contrib/packaging/docker/fedora21	Mon Nov 04 00:16:44 2019 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,15 +0,0 @@
-FROM fedora:21
-
-RUN groupadd -g 1000 build && \
-    useradd -u 1000 -g 1000 -s /bin/bash -d /build -m build
-
-RUN yum install -y \
-	gcc \
-	gettext \
-	make \
-	python-devel \
-	python-docutils \
-	rpm-build
-
-# For creating repo meta data
-RUN yum install -y createrepo
--- a/contrib/packaging/docker/fedora28	Mon Nov 04 00:16:44 2019 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,15 +0,0 @@
-FROM fedora:28
-
-RUN groupadd -g 1000 build && \
-    useradd -u 1000 -g 1000 -s /bin/bash -d /build -m build
-
-RUN dnf install -y \
-	gcc \
-	gettext \
-	make \
-	python-devel \
-	python-docutils \
-	rpm-build
-
-# For creating repo meta data
-RUN dnf install -y createrepo
--- a/contrib/packaging/docker/fedora29	Mon Nov 04 00:16:44 2019 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,15 +0,0 @@
-FROM fedora:29
-
-RUN groupadd -g 1000 build && \
-    useradd -u 1000 -g 1000 -s /bin/bash -d /build -m build
-
-RUN dnf install -y \
-	gcc \
-	gettext \
-	make \
-	python-devel \
-	python-docutils \
-	rpm-build
-
-# For creating repo meta data
-RUN dnf install -y createrepo
--- a/contrib/packaging/dockerrpm	Mon Nov 04 00:16:44 2019 +0100
+++ b/contrib/packaging/dockerrpm	Tue Nov 05 13:19:24 2019 -0800
@@ -6,6 +6,14 @@
 PLATFORM="$1"
 shift # extra params are passed to buildrpm
 
+DOCKERFILE="$PLATFORM"
+OS_RELEASE="${PLATFORM//[a-z]/}"
+case "$PLATFORM" in
+fedora*)
+    DOCKERFILE="${PLATFORM//[0-9]/}.template"
+    ;;
+esac
+
 DOCKER=$($BUILDDIR/hg-docker docker-path)
 
 CONTAINER=hg-docker-$PLATFORM
@@ -18,9 +26,14 @@
     DOCKERGID=$(id -g)
 fi
 
-$BUILDDIR/hg-docker build --build-arg UID=$DOCKERUID --build-arg GID=$DOCKERGID $BUILDDIR/docker/$PLATFORM $CONTAINER
+$BUILDDIR/hg-docker build \
+    --build-arg UID=$DOCKERUID \
+    --build-arg GID=$DOCKERGID \
+    --build-arg OS_RELEASE=${OS_RELEASE:-latest} \
+    $BUILDDIR/docker/$DOCKERFILE $CONTAINER
 
 RPMBUILDDIR=$ROOTDIR/packages/$PLATFORM
+mkdir -p $RPMBUILDDIR
 $ROOTDIR/contrib/packaging/buildrpm --rpmbuilddir $RPMBUILDDIR --prepare $*
 
 DSHARED=/mnt/shared
--- a/contrib/packaging/inno/requirements.txt	Mon Nov 04 00:16:44 2019 +0100
+++ b/contrib/packaging/inno/requirements.txt	Tue Nov 05 13:19:24 2019 -0800
@@ -2,37 +2,119 @@
 # This file is autogenerated by pip-compile
 # To update, run:
 #
-#    pip-compile --generate-hashes contrib/packaging/inno/requirements.txt.in -o contrib/packaging/inno/requirements.txt
+#    pip-compile --generate-hashes --output-file=contrib/packaging/inno/requirements.txt contrib/packaging/inno/requirements.txt.in
 #
-certifi==2018.11.29 \
-    --hash=sha256:47f9c83ef4c0c621eaef743f133f09fa8a74a9b75f037e8624f83bd1b6626cb7 \
-    --hash=sha256:993f830721089fef441cdfeb4b2c8c9df86f0c63239f06bd025a76a7daddb033 \
+certifi==2019.9.11 \
+    --hash=sha256:e4f3620cfea4f83eedc95b24abd9cd56f3c4b146dd0177e83a21b4eb49e21e50 \
+    --hash=sha256:fd7c7c74727ddcf00e9acd26bba8da604ffec95bf1c2144e67aff7a8b50e6cef \
     # via dulwich
-configparser==3.7.3 \
-    --hash=sha256:27594cf4fc279f321974061ac69164aaebd2749af962ac8686b20503ac0bcf2d \
-    --hash=sha256:9d51fe0a382f05b6b117c5e601fc219fede4a8c71703324af3f7d883aef476a3 \
+cffi==1.13.1 \
+    --hash=sha256:00d890313797d9fe4420506613384b43099ad7d2b905c0752dbcc3a6f14d80fa \
+    --hash=sha256:0cf9e550ac6c5e57b713437e2f4ac2d7fd0cd10336525a27224f5fc1ec2ee59a \
+    --hash=sha256:0ea23c9c0cdd6778146a50d867d6405693ac3b80a68829966c98dd5e1bbae400 \
+    --hash=sha256:193697c2918ecdb3865acf6557cddf5076bb39f1f654975e087b67efdff83365 \
+    --hash=sha256:1ae14b542bf3b35e5229439c35653d2ef7d8316c1fffb980f9b7647e544baa98 \
+    --hash=sha256:1e389e069450609c6ffa37f21f40cce36f9be7643bbe5051ab1de99d5a779526 \
+    --hash=sha256:263242b6ace7f9cd4ea401428d2d45066b49a700852334fd55311bde36dcda14 \
+    --hash=sha256:33142ae9807665fa6511cfa9857132b2c3ee6ddffb012b3f0933fc11e1e830d5 \
+    --hash=sha256:364f8404034ae1b232335d8c7f7b57deac566f148f7222cef78cf8ae28ef764e \
+    --hash=sha256:47368f69fe6529f8f49a5d146ddee713fc9057e31d61e8b6dc86a6a5e38cecc1 \
+    --hash=sha256:4895640844f17bec32943995dc8c96989226974dfeb9dd121cc45d36e0d0c434 \
+    --hash=sha256:558b3afef987cf4b17abd849e7bedf64ee12b28175d564d05b628a0f9355599b \
+    --hash=sha256:5ba86e1d80d458b338bda676fd9f9d68cb4e7a03819632969cf6d46b01a26730 \
+    --hash=sha256:63424daa6955e6b4c70dc2755897f5be1d719eabe71b2625948b222775ed5c43 \
+    --hash=sha256:6381a7d8b1ebd0bc27c3bc85bc1bfadbb6e6f756b4d4db0aa1425c3719ba26b4 \
+    --hash=sha256:6381ab708158c4e1639da1f2a7679a9bbe3e5a776fc6d1fd808076f0e3145331 \
+    --hash=sha256:6fd58366747debfa5e6163ada468a90788411f10c92597d3b0a912d07e580c36 \
+    --hash=sha256:728ec653964655d65408949b07f9b2219df78badd601d6c49e28d604efe40599 \
+    --hash=sha256:7cfcfda59ef1f95b9f729c56fe8a4041899f96b72685d36ef16a3440a0f85da8 \
+    --hash=sha256:819f8d5197c2684524637f940445c06e003c4a541f9983fd30d6deaa2a5487d8 \
+    --hash=sha256:825ecffd9574557590e3225560a8a9d751f6ffe4a49e3c40918c9969b93395fa \
+    --hash=sha256:9009e917d8f5ef780c2626e29b6bc126f4cb2a4d43ca67aa2b40f2a5d6385e78 \
+    --hash=sha256:9c77564a51d4d914ed5af096cd9843d90c45b784b511723bd46a8a9d09cf16fc \
+    --hash=sha256:a19089fa74ed19c4fe96502a291cfdb89223a9705b1d73b3005df4256976142e \
+    --hash=sha256:a40ed527bffa2b7ebe07acc5a3f782da072e262ca994b4f2085100b5a444bbb2 \
+    --hash=sha256:bb75ba21d5716abc41af16eac1145ab2e471deedde1f22c6f99bd9f995504df0 \
+    --hash=sha256:e22a00c0c81ffcecaf07c2bfb3672fa372c50e2bd1024ffee0da191c1b27fc71 \
+    --hash=sha256:e55b5a746fb77f10c83e8af081979351722f6ea48facea79d470b3731c7b2891 \
+    --hash=sha256:ec2fa3ee81707a5232bf2dfbd6623fdb278e070d596effc7e2d788f2ada71a05 \
+    --hash=sha256:fd82eb4694be712fcae03c717ca2e0fc720657ac226b80bbb597e971fc6928c2 \
+    # via cryptography
+configparser==4.0.2 \
+    --hash=sha256:254c1d9c79f60c45dfde850850883d5aaa7f19a23f13561243a050d5a7c3fe4c \
+    --hash=sha256:c7d282687a5308319bf3d2e7706e575c635b0a470342641c93bea0ea3b5331df \
     # via entrypoints
-docutils==0.14 \
-    --hash=sha256:02aec4bd92ab067f6ff27a38a38a41173bf01bed8f89157768c1573f53e474a6 \
-    --hash=sha256:51e64ef2ebfb29cae1faa133b3710143496eca21c530f3f71424d77687764274 \
-    --hash=sha256:7a4bd47eaf6596e1295ecb11361139febe29b084a87bf005bf899f9a42edc3c6
-dulwich==0.19.11 \
-    --hash=sha256:afbe070f6899357e33f63f3f3696e601731fef66c64a489dea1bc9f539f4a725
+cryptography==2.8 \
+    --hash=sha256:02079a6addc7b5140ba0825f542c0869ff4df9a69c360e339ecead5baefa843c \
+    --hash=sha256:1df22371fbf2004c6f64e927668734070a8953362cd8370ddd336774d6743595 \
+    --hash=sha256:369d2346db5934345787451504853ad9d342d7f721ae82d098083e1f49a582ad \
+    --hash=sha256:3cda1f0ed8747339bbdf71b9f38ca74c7b592f24f65cdb3ab3765e4b02871651 \
+    --hash=sha256:44ff04138935882fef7c686878e1c8fd80a723161ad6a98da31e14b7553170c2 \
+    --hash=sha256:4b1030728872c59687badcca1e225a9103440e467c17d6d1730ab3d2d64bfeff \
+    --hash=sha256:58363dbd966afb4f89b3b11dfb8ff200058fbc3b947507675c19ceb46104b48d \
+    --hash=sha256:6ec280fb24d27e3d97aa731e16207d58bd8ae94ef6eab97249a2afe4ba643d42 \
+    --hash=sha256:7270a6c29199adc1297776937a05b59720e8a782531f1f122f2eb8467f9aab4d \
+    --hash=sha256:73fd30c57fa2d0a1d7a49c561c40c2f79c7d6c374cc7750e9ac7c99176f6428e \
+    --hash=sha256:7f09806ed4fbea8f51585231ba742b58cbcfbfe823ea197d8c89a5e433c7e912 \
+    --hash=sha256:90df0cc93e1f8d2fba8365fb59a858f51a11a394d64dbf3ef844f783844cc793 \
+    --hash=sha256:971221ed40f058f5662a604bd1ae6e4521d84e6cad0b7b170564cc34169c8f13 \
+    --hash=sha256:a518c153a2b5ed6b8cc03f7ae79d5ffad7315ad4569b2d5333a13c38d64bd8d7 \
+    --hash=sha256:b0de590a8b0979649ebeef8bb9f54394d3a41f66c5584fff4220901739b6b2f0 \
+    --hash=sha256:b43f53f29816ba1db8525f006fa6f49292e9b029554b3eb56a189a70f2a40879 \
+    --hash=sha256:d31402aad60ed889c7e57934a03477b572a03af7794fa8fb1780f21ea8f6551f \
+    --hash=sha256:de96157ec73458a7f14e3d26f17f8128c959084931e8997b9e655a39c8fde9f9 \
+    --hash=sha256:df6b4dca2e11865e6cfbfb708e800efb18370f5a46fd601d3755bc7f85b3a8a2 \
+    --hash=sha256:ecadccc7ba52193963c0475ac9f6fa28ac01e01349a2ca48509667ef41ffd2cf \
+    --hash=sha256:fb81c17e0ebe3358486cd8cc3ad78adbae58af12fc2bf2bc0bb84e8090fa5ce8 \
+    # via secretstorage
+docutils==0.15.2 \
+    --hash=sha256:6c4f696463b79f1fb8ba0c594b63840ebd41f059e92b31957c46b74a4599b6d0 \
+    --hash=sha256:9e4d7ecfc600058e07ba661411a2b7de2fd0fafa17d1a7f7361cd47b1175c827 \
+    --hash=sha256:a2aeea129088da402665e92e0b25b04b073c04b2dce4ab65caaa38b7ce2e1a99
+dulwich==0.19.13 \
+    --hash=sha256:0e442f6f96e6d97270a7cca4e75306b6b0228627bdf57dde3759e0e345a6b523 \
+    --hash=sha256:667f49536ccba09d3b90bac80d44048e45566f84b98a5e139cc8c70757a6ae60 \
+    --hash=sha256:82792a9d49b112fa2151fa0fb29b01667855a843ff99325b1c1578a4aec11b57 \
+    --hash=sha256:aa628449c5f594a9a282f4d9e5993fef65481ef5e3b9b6c52ff31200f8f5dc95 \
+    --hash=sha256:ab4668bc4e1996d12eb1910e123a09edcff8e166e7ec46db5aafb5c7e250b99f \
+    --hash=sha256:c35ed2cd5b263ce0d67758ffba590c0466ff13b048457ff060b7d2e6cb55a40e \
+    --hash=sha256:c8b48079a14850cbeb788b38e1061ae6db75061431c1c0f91382460be4c84bbe \
+    --hash=sha256:dfcd9943c69f963dd61a027f480d16f548ea5905c2485be8f4b8f130df2c32de \
+    --hash=sha256:e3693c3238c1a5fc1e4427281c4455d78549f4797f2a7107a5f4443b21efafb4
 entrypoints==0.3 \
     --hash=sha256:589f874b313739ad35be6e0cd7efde2a4e9b6fea91edcc34e58ecbb8dbe56d19 \
     --hash=sha256:c70dd71abe5a8c85e55e12c19bd91ccfeec11a6e99044204511f9ed547d48451 \
     # via keyring
+enum34==1.1.6 \
+    --hash=sha256:2d81cbbe0e73112bdfe6ef8576f2238f2ba27dd0d55752a776c41d38b7da2850 \
+    --hash=sha256:644837f692e5f550741432dd3f223bbb9852018674981b1664e5dc339387588a \
+    --hash=sha256:6bd0f6ad48ec2aa117d3d141940d484deccda84d4fcd884f5c3d93c23ecd8c79 \
+    --hash=sha256:8ad8c4783bf61ded74527bffb48ed9b54166685e4230386a9ed9b1279e2df5b1 \
+    # via cryptography
+ipaddress==1.0.23 \
+    --hash=sha256:6e0f4a39e66cb5bb9a137b00276a2eff74f93b71dcbdad6f10ff7df9d3557fcc \
+    --hash=sha256:b7f8e0369580bb4a24d5ba1d7cc29660a4a6987763faf1d8a8046830e020e7e2 \
+    # via cryptography
 keyring==18.0.1 \
     --hash=sha256:67d6cc0132bd77922725fae9f18366bb314fd8f95ff4d323a4df41890a96a838 \
     --hash=sha256:7b29ebfcf8678c4da531b2478a912eea01e80007e5ddca9ee0c7038cb3489ec6
-pygments==2.3.1 \
-    --hash=sha256:5ffada19f6203563680669ee7f53b64dabbeb100eb51b61996085e99c03b284a \
-    --hash=sha256:e8218dd399a61674745138520d0d4cf2621d7e032439341bc3f647bff125818d
+pycparser==2.19 \
+    --hash=sha256:a988718abfad80b6b157acce7bf130a30876d27603738ac39f140993246b25b3 \
+    # via cffi
+pygments==2.4.2 \
+    --hash=sha256:71e430bc85c88a430f000ac1d9b331d2407f681d6f6aec95e8bcfbc3df5b0127 \
+    --hash=sha256:881c4c157e45f30af185c1ffe8d549d48ac9127433f2c380c24b84572ad66297
 pywin32-ctypes==0.2.0 \
     --hash=sha256:24ffc3b341d457d48e8922352130cf2644024a4ff09762a2261fd34c36ee5942 \
-    --hash=sha256:9dc2d991b3479cc2df15930958b674a48a227d5361d413827a4cfd0b5876fc98 \
+    --hash=sha256:9dc2d991b3479cc2df15930958b674a48a227d5361d413827a4cfd0b5876fc98
+secretstorage==2.3.1 \
+    --hash=sha256:3af65c87765323e6f64c83575b05393f9e003431959c9395d1791d51497f29b6 \
     # via keyring
-urllib3==1.24.1 \
-    --hash=sha256:61bf29cada3fc2fbefad4fdf059ea4bd1b4a86d2b6d15e1c7c0b582b9752fe39 \
-    --hash=sha256:de9529817c93f27c8ccbfead6985011db27bd0ddfcdb2d86f3f663385c6a9c22 \
+six==1.12.0 \
+    --hash=sha256:3350809f0555b11f552448330d0b52d5f24c91a322ea4a15ef22629740f3761c \
+    --hash=sha256:d16a0141ec1a18405cd4ce8b4613101da75da0e9a7aec5bdd4fa804d0e0eba73 \
+    # via cryptography
+urllib3==1.25.6 \
+    --hash=sha256:3de946ffbed6e6746608990594d08faac602528ac7015ac28d33cee6a45b7398 \
+    --hash=sha256:9a107b99a5393caf59c7aa3c1249c16e6879447533d0887f4336dde834c7be86 \
     # via dulwich
--- a/contrib/packaging/inno/requirements.txt.in	Mon Nov 04 00:16:44 2019 +0100
+++ b/contrib/packaging/inno/requirements.txt.in	Tue Nov 05 13:19:24 2019 -0800
@@ -2,3 +2,6 @@
 dulwich
 keyring
 pygments
+# Need to list explicitly so dependency gets pulled in when
+# not running on Windows.
+pywin32-ctypes
--- a/contrib/packaging/mercurial.spec	Mon Nov 04 00:16:44 2019 +0100
+++ b/contrib/packaging/mercurial.spec	Tue Nov 05 13:19:24 2019 -0800
@@ -2,6 +2,8 @@
 
 %define withpython %{nil}
 
+%global pythonexe python3
+
 %if "%{?withpython}"
 
 %global pythonver %{withpython}
@@ -15,7 +17,7 @@
 
 %else
 
-%global pythonver %(python -c 'import sys;print ".".join(map(str, sys.version_info[:2]))')
+%global pythonver %(%{pythonexe} -c 'import sys;print(".".join(map(str, sys.version_info[:2])))')
 
 %endif
 
@@ -37,8 +39,8 @@
 %if "%{?withpython}"
 BuildRequires: readline-devel, openssl-devel, ncurses-devel, zlib-devel, bzip2-devel
 %else
-BuildRequires: python >= 2.7, python-devel, python-docutils >= 0.5
-Requires: python >= 2.7
+BuildRequires: %{pythonexe} >= %{pythonver}, %{pythonexe}-devel, %{pythonexe}-docutils >= 0.5
+Requires: %{pythonexe} >= %{pythonver}
 %endif
 # The hgk extension uses the wish tcl interpreter, but we don't enforce it
 #Requires: tk
@@ -52,13 +54,15 @@
 %if "%{?withpython}"
 %setup -q -n mercurial-%{version}-%{release} -a1 -a2
 # despite the comments in cgi.py, we do this to prevent rpmdeps from picking /usr/local/bin/python up
-sed -i '1c#! /usr/bin/env python' %{pythonname}/Lib/cgi.py
+sed -i '1c#! /usr/bin/env %{pythonexe}' %{pythonname}/Lib/cgi.py
 %else
 %setup -q -n mercurial-%{version}-%{release}
 %endif
 
 %build
 
+export HGPYTHON3=1
+
 %if "%{?withpython}"
 
 PYPATH=$PWD/%{pythonname}
@@ -82,12 +86,16 @@
 
 %endif
 
-make all
+make all PYTHON=%{pythonexe}
 make -C contrib/chg
 
+sed -i -e '1s|#!/usr/bin/env python$|#!/usr/bin/env %{pythonexe}|' contrib/hg-ssh
+
 %install
 rm -rf $RPM_BUILD_ROOT
 
+export HGPYTHON3=1
+
 %if "%{?withpython}"
 
 PYPATH=$PWD/%{pythonname}
@@ -101,14 +109,14 @@
 LD_LIBRARY_PATH=$PYPATH $PYPATH/python setup.py install --root="$RPM_BUILD_ROOT"
 cd -
 
-PATH=$PYPATH:$PATH LD_LIBRARY_PATH=$PYPATH make install DESTDIR=$RPM_BUILD_ROOT PREFIX=%{hgpyprefix} MANDIR=%{_mandir}
+PATH=$PYPATH:$PATH LD_LIBRARY_PATH=$PYPATH make install PYTHON=%{pythonexe} DESTDIR=$RPM_BUILD_ROOT PREFIX=%{hgpyprefix} MANDIR=%{_mandir}
 mkdir -p $RPM_BUILD_ROOT%{_bindir}
 ( cd $RPM_BUILD_ROOT%{_bindir}/ && ln -s ../..%{hgpyprefix}/bin/hg . )
 ( cd $RPM_BUILD_ROOT%{_bindir}/ && ln -s ../..%{hgpyprefix}/bin/python2.? %{pythonhg} )
 
 %else
 
-make install DESTDIR=$RPM_BUILD_ROOT PREFIX=%{_prefix} MANDIR=%{_mandir}
+make install PYTHON=%{pythonexe} DESTDIR=$RPM_BUILD_ROOT PREFIX=%{_prefix} MANDIR=%{_mandir}
 
 %endif
 
@@ -135,7 +143,7 @@
 
 %files
 %defattr(-,root,root,-)
-%doc CONTRIBUTORS COPYING doc/README doc/hg*.txt doc/hg*.html *.cgi contrib/*.fcgi
+%doc CONTRIBUTORS COPYING doc/README doc/hg*.txt doc/hg*.html *.cgi contrib/*.fcgi contrib/*.wsgi
 %doc %attr(644,root,root) %{_mandir}/man?/hg*
 %doc %attr(644,root,root) contrib/*.svg
 %dir %{_datadir}/zsh/
--- a/contrib/packaging/wix/requirements.txt	Mon Nov 04 00:16:44 2019 +0100
+++ b/contrib/packaging/wix/requirements.txt	Tue Nov 05 13:19:24 2019 -0800
@@ -2,12 +2,12 @@
 # This file is autogenerated by pip-compile
 # To update, run:
 #
-#    pip-compile --generate-hashes contrib/packaging/wix/requirements.txt.in -o contrib/packaging/wix/requirements.txt
+#    pip-compile --generate-hashes --output-file=contrib/packaging/wix/requirements.txt contrib/packaging/wix/requirements.txt.in
 #
-docutils==0.14 \
-    --hash=sha256:02aec4bd92ab067f6ff27a38a38a41173bf01bed8f89157768c1573f53e474a6 \
-    --hash=sha256:51e64ef2ebfb29cae1faa133b3710143496eca21c530f3f71424d77687764274 \
-    --hash=sha256:7a4bd47eaf6596e1295ecb11361139febe29b084a87bf005bf899f9a42edc3c6
-pygments==2.3.1 \
-    --hash=sha256:5ffada19f6203563680669ee7f53b64dabbeb100eb51b61996085e99c03b284a \
-    --hash=sha256:e8218dd399a61674745138520d0d4cf2621d7e032439341bc3f647bff125818d
+docutils==0.15.2 \
+    --hash=sha256:6c4f696463b79f1fb8ba0c594b63840ebd41f059e92b31957c46b74a4599b6d0 \
+    --hash=sha256:9e4d7ecfc600058e07ba661411a2b7de2fd0fafa17d1a7f7361cd47b1175c827 \
+    --hash=sha256:a2aeea129088da402665e92e0b25b04b073c04b2dce4ab65caaa38b7ce2e1a99
+pygments==2.4.2 \
+    --hash=sha256:71e430bc85c88a430f000ac1d9b331d2407f681d6f6aec95e8bcfbc3df5b0127 \
+    --hash=sha256:881c4c157e45f30af185c1ffe8d549d48ac9127433f2c380c24b84572ad66297
--- a/contrib/simplemerge	Mon Nov 04 00:16:44 2019 +0100
+++ b/contrib/simplemerge	Tue Nov 05 13:19:24 2019 -0800
@@ -18,6 +18,7 @@
 )
 from mercurial.utils import (
     procutil,
+    stringutil
 )
 
 options = [(b'L', b'label', [], _(b'labels to use on conflict markers')),
@@ -75,8 +76,7 @@
                                      context.arbitraryfilectx(other),
                                      **pycompat.strkwargs(opts)))
 except ParseError as e:
-    if pycompat.ispy3:
-        e = str(e).encode('utf8')
+    e = stringutil.forcebytestr(e)
     pycompat.stdout.write(b"%s: %s\n" % (sys.argv[0].encode('utf8'), e))
     showhelp()
     sys.exit(1)
--- a/hgdemandimport/demandimportpy3.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/hgdemandimport/demandimportpy3.py	Tue Nov 05 13:19:24 2019 -0800
@@ -53,9 +53,13 @@
 
 # This is 3.6+ because with Python 3.5 it isn't possible to lazily load
 # extensions. See the discussion in https://bugs.python.org/issue26186 for more.
-_extensions_loader = _lazyloaderex.factory(
-    importlib.machinery.ExtensionFileLoader
-)
+if sys.version_info[0:2] >= (3, 6):
+    _extensions_loader = _lazyloaderex.factory(
+        importlib.machinery.ExtensionFileLoader
+    )
+else:
+    _extensions_loader = importlib.machinery.ExtensionFileLoader
+
 _bytecode_loader = _lazyloaderex.factory(
     importlib.machinery.SourcelessFileLoader
 )
--- a/hgext/bugzilla.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/hgext/bugzilla.py	Tue Nov 05 13:19:24 2019 -0800
@@ -955,7 +955,7 @@
     def _fetch(self, burl):
         try:
             resp = url.open(self.ui, burl)
-            return json.loads(resp.read())
+            return pycompat.json_loads(resp.read())
         except util.urlerr.httperror as inst:
             if inst.code == 401:
                 raise error.Abort(_(b'authorization failed'))
@@ -978,7 +978,7 @@
         req = request_type(burl, data, {b'Content-Type': b'application/json'})
         try:
             resp = url.opener(self.ui).open(req)
-            return json.loads(resp.read())
+            return pycompat.json_loads(resp.read())
         except util.urlerr.httperror as inst:
             if inst.code == 401:
                 raise error.Abort(_(b'authorization failed'))
--- a/hgext/convert/gnuarch.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/hgext/convert/gnuarch.py	Tue Nov 05 13:19:24 2019 -0800
@@ -7,7 +7,6 @@
 # GNU General Public License version 2 or any later version.
 from __future__ import absolute_import
 
-import email.parser as emailparser
 import os
 import shutil
 import stat
@@ -17,6 +16,7 @@
 from mercurial import (
     encoding,
     error,
+    mail,
     pycompat,
     util,
 )
@@ -69,7 +69,6 @@
         self.changes = {}
         self.parents = {}
         self.tags = {}
-        self.catlogparser = emailparser.Parser()
         self.encoding = encoding.encoding
         self.archives = []
 
@@ -299,26 +298,29 @@
 
     def _parsecatlog(self, data, rev):
         try:
-            catlog = self.catlogparser.parsestr(data)
+            catlog = mail.parsebytes(data)
 
             # Commit date
             self.changes[rev].date = dateutil.datestr(
-                dateutil.strdate(catlog[b'Standard-date'], b'%Y-%m-%d %H:%M:%S')
+                dateutil.strdate(catlog[r'Standard-date'], b'%Y-%m-%d %H:%M:%S')
             )
 
             # Commit author
-            self.changes[rev].author = self.recode(catlog[b'Creator'])
+            self.changes[rev].author = self.recode(catlog[r'Creator'])
 
             # Commit description
             self.changes[rev].summary = b'\n\n'.join(
-                (catlog[b'Summary'], catlog.get_payload())
+                (
+                    self.recode(catlog[r'Summary']),
+                    self.recode(catlog.get_payload()),
+                )
             )
             self.changes[rev].summary = self.recode(self.changes[rev].summary)
 
             # Commit revision origin when dealing with a branch or tag
-            if b'Continuation-of' in catlog:
+            if r'Continuation-of' in catlog:
                 self.changes[rev].continuationof = self.recode(
-                    catlog[b'Continuation-of']
+                    catlog[r'Continuation-of']
                 )
         except Exception:
             raise error.Abort(_(b'could not parse cat-log of %s') % rev)
--- a/hgext/fix.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/hgext/fix.py	Tue Nov 05 13:19:24 2019 -0800
@@ -126,7 +126,6 @@
 
 import collections
 import itertools
-import json
 import os
 import re
 import subprocess
@@ -642,7 +641,7 @@
             if fixer.shouldoutputmetadata():
                 try:
                     metadatajson, newerdata = stdout.split(b'\0', 1)
-                    metadata[fixername] = json.loads(metadatajson)
+                    metadata[fixername] = pycompat.json_loads(metadatajson)
                 except ValueError:
                     ui.warn(
                         _(b'ignored invalid output from fixer tool: %s\n')
--- a/hgext/fsmonitor/__init__.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/hgext/fsmonitor/__init__.py	Tue Nov 05 13:19:24 2019 -0800
@@ -132,6 +132,7 @@
     util,
 )
 from mercurial import match as matchmod
+from mercurial.utils import stringutil
 
 from . import (
     pywatchman,
@@ -189,10 +190,10 @@
         fm.write(
             b"fsmonitor-watchman-version",
             _(b" watchman binary version %s\n"),
-            v[b"version"],
+            pycompat.bytestr(v["version"]),
         )
     except watchmanclient.Unavailable as e:
-        err = str(e)
+        err = stringutil.forcebytestr(e)
     fm.condwrite(
         err,
         b"fsmonitor-watchman-error",
@@ -207,15 +208,23 @@
     if isinstance(ex, watchmanclient.Unavailable):
         # experimental config: fsmonitor.verbose
         if ex.warn and ui.configbool(b'fsmonitor', b'verbose'):
-            if b'illegal_fstypes' not in str(ex):
-                ui.warn(str(ex) + b'\n')
+            if b'illegal_fstypes' not in stringutil.forcebytestr(ex):
+                ui.warn(stringutil.forcebytestr(ex) + b'\n')
         if ex.invalidate:
             state.invalidate()
         # experimental config: fsmonitor.verbose
         if ui.configbool(b'fsmonitor', b'verbose'):
-            ui.log(b'fsmonitor', b'Watchman unavailable: %s\n', ex.msg)
+            ui.log(
+                b'fsmonitor',
+                b'Watchman unavailable: %s\n',
+                stringutil.forcebytestr(ex.msg),
+            )
     else:
-        ui.log(b'fsmonitor', b'Watchman exception: %s\n', ex)
+        ui.log(
+            b'fsmonitor',
+            b'Watchman exception: %s\n',
+            stringutil.forcebytestr(ex),
+        )
 
 
 def _hashignore(ignore):
@@ -227,8 +236,8 @@
 
     """
     sha1 = hashlib.sha1()
-    sha1.update(repr(ignore))
-    return sha1.hexdigest()
+    sha1.update(pycompat.byterepr(ignore))
+    return pycompat.sysbytes(sha1.hexdigest())
 
 
 _watchmanencoding = pywatchman.encoding.get_local_encoding()
@@ -245,12 +254,14 @@
     try:
         decoded = path.decode(_watchmanencoding)
     except UnicodeDecodeError as e:
-        raise error.Abort(str(e), hint=b'watchman encoding error')
+        raise error.Abort(
+            stringutil.forcebytestr(e), hint=b'watchman encoding error'
+        )
 
     try:
         encoded = decoded.encode(_fsencoding, 'strict')
     except UnicodeEncodeError as e:
-        raise error.Abort(str(e))
+        raise error.Abort(stringutil.forcebytestr(e))
 
     return encoded
 
@@ -372,7 +383,7 @@
     else:
         # We need to propagate the last observed clock up so that we
         # can use it for our next query
-        state.setlastclock(result[b'clock'])
+        state.setlastclock(pycompat.sysbytes(result[b'clock']))
         if result[b'is_fresh_instance']:
             if state.walk_on_invalidate:
                 state.invalidate()
@@ -396,8 +407,15 @@
     # for name case changes.
     for entry in result[b'files']:
         fname = entry[b'name']
+
+        # Watchman always give us a str. Normalize to bytes on Python 3
+        # using Watchman's encoding, if needed.
+        if not isinstance(fname, bytes):
+            fname = fname.encode(_watchmanencoding)
+
         if _fixencoding:
             fname = _watchmantofsencoding(fname)
+
         if switch_slashes:
             fname = fname.replace(b'\\', b'/')
         if normalize:
@@ -486,9 +504,9 @@
     for f in auditfail:
         results[f] = None
 
-    nf = iter(auditpass).next
+    nf = iter(auditpass)
     for st in util.statfiles([join(f) for f in auditpass]):
-        f = nf()
+        f = next(nf)
         if st or f in dmap:
             results[f] = st
 
@@ -916,7 +934,7 @@
             return
 
         try:
-            client = watchmanclient.client(repo.ui, repo._root)
+            client = watchmanclient.client(repo.ui, repo.root)
         except Exception as ex:
             _handleunavailable(ui, fsmonitorstate, ex)
             return
--- a/hgext/fsmonitor/pywatchman/__init__.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/hgext/fsmonitor/pywatchman/__init__.py	Tue Nov 05 13:19:24 2019 -0800
@@ -26,10 +26,8 @@
 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
-from __future__ import absolute_import
-from __future__ import division
-from __future__ import print_function
 # no unicode literals
+from __future__ import absolute_import, division, print_function
 
 import inspect
 import math
@@ -38,33 +36,22 @@
 import subprocess
 import time
 
+from . import capabilities, compat, encoding, load
+
+
 # Sometimes it's really hard to get Python extensions to compile,
 # so fall back to a pure Python implementation.
 try:
     from . import bser
+
     # Demandimport causes modules to be loaded lazily. Force the load now
     # so that we can fall back on pybser if bser doesn't exist
     bser.pdu_info
 except ImportError:
     from . import pybser as bser
 
-from mercurial.utils import (
-    procutil,
-)
 
-from mercurial import (
-    pycompat,
-)
-
-from . import (
-    capabilities,
-    compat,
-    encoding,
-    load,
-)
-
-
-if os.name == 'nt':
+if os.name == "nt":
     import ctypes
     import ctypes.wintypes
 
@@ -73,7 +60,7 @@
     GENERIC_WRITE = 0x40000000
     FILE_FLAG_OVERLAPPED = 0x40000000
     OPEN_EXISTING = 3
-    INVALID_HANDLE_VALUE = -1
+    INVALID_HANDLE_VALUE = ctypes.c_void_p(-1).value
     FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000
     FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100
     FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200
@@ -92,9 +79,11 @@
 
     class OVERLAPPED(ctypes.Structure):
         _fields_ = [
-            ("Internal", ULONG_PTR), ("InternalHigh", ULONG_PTR),
-            ("Offset", wintypes.DWORD), ("OffsetHigh", wintypes.DWORD),
-            ("hEvent", wintypes.HANDLE)
+            ("Internal", ULONG_PTR),
+            ("InternalHigh", ULONG_PTR),
+            ("Offset", wintypes.DWORD),
+            ("OffsetHigh", wintypes.DWORD),
+            ("hEvent", wintypes.HANDLE),
         ]
 
         def __init__(self):
@@ -107,9 +96,15 @@
     LPDWORD = ctypes.POINTER(wintypes.DWORD)
 
     CreateFile = ctypes.windll.kernel32.CreateFileA
-    CreateFile.argtypes = [wintypes.LPSTR, wintypes.DWORD, wintypes.DWORD,
-                           wintypes.LPVOID, wintypes.DWORD, wintypes.DWORD,
-                           wintypes.HANDLE]
+    CreateFile.argtypes = [
+        wintypes.LPSTR,
+        wintypes.DWORD,
+        wintypes.DWORD,
+        wintypes.LPVOID,
+        wintypes.DWORD,
+        wintypes.DWORD,
+        wintypes.HANDLE,
+    ]
     CreateFile.restype = wintypes.HANDLE
 
     CloseHandle = ctypes.windll.kernel32.CloseHandle
@@ -117,13 +112,23 @@
     CloseHandle.restype = wintypes.BOOL
 
     ReadFile = ctypes.windll.kernel32.ReadFile
-    ReadFile.argtypes = [wintypes.HANDLE, wintypes.LPVOID, wintypes.DWORD,
-                         LPDWORD, ctypes.POINTER(OVERLAPPED)]
+    ReadFile.argtypes = [
+        wintypes.HANDLE,
+        wintypes.LPVOID,
+        wintypes.DWORD,
+        LPDWORD,
+        ctypes.POINTER(OVERLAPPED),
+    ]
     ReadFile.restype = wintypes.BOOL
 
     WriteFile = ctypes.windll.kernel32.WriteFile
-    WriteFile.argtypes = [wintypes.HANDLE, wintypes.LPVOID, wintypes.DWORD,
-                          LPDWORD, ctypes.POINTER(OVERLAPPED)]
+    WriteFile.argtypes = [
+        wintypes.HANDLE,
+        wintypes.LPVOID,
+        wintypes.DWORD,
+        LPDWORD,
+        ctypes.POINTER(OVERLAPPED),
+    ]
     WriteFile.restype = wintypes.BOOL
 
     GetLastError = ctypes.windll.kernel32.GetLastError
@@ -135,34 +140,56 @@
     SetLastError.restype = None
 
     FormatMessage = ctypes.windll.kernel32.FormatMessageA
-    FormatMessage.argtypes = [wintypes.DWORD, wintypes.LPVOID, wintypes.DWORD,
-                              wintypes.DWORD, ctypes.POINTER(wintypes.LPSTR),
-                              wintypes.DWORD, wintypes.LPVOID]
+    FormatMessage.argtypes = [
+        wintypes.DWORD,
+        wintypes.LPVOID,
+        wintypes.DWORD,
+        wintypes.DWORD,
+        ctypes.POINTER(wintypes.LPSTR),
+        wintypes.DWORD,
+        wintypes.LPVOID,
+    ]
     FormatMessage.restype = wintypes.DWORD
 
     LocalFree = ctypes.windll.kernel32.LocalFree
 
     GetOverlappedResult = ctypes.windll.kernel32.GetOverlappedResult
-    GetOverlappedResult.argtypes = [wintypes.HANDLE,
-                                    ctypes.POINTER(OVERLAPPED), LPDWORD,
-                                    wintypes.BOOL]
+    GetOverlappedResult.argtypes = [
+        wintypes.HANDLE,
+        ctypes.POINTER(OVERLAPPED),
+        LPDWORD,
+        wintypes.BOOL,
+    ]
     GetOverlappedResult.restype = wintypes.BOOL
 
-    GetOverlappedResultEx = getattr(ctypes.windll.kernel32,
-                                    'GetOverlappedResultEx', None)
+    GetOverlappedResultEx = getattr(
+        ctypes.windll.kernel32, "GetOverlappedResultEx", None
+    )
     if GetOverlappedResultEx is not None:
-        GetOverlappedResultEx.argtypes = [wintypes.HANDLE,
-                                          ctypes.POINTER(OVERLAPPED), LPDWORD,
-                                          wintypes.DWORD, wintypes.BOOL]
+        GetOverlappedResultEx.argtypes = [
+            wintypes.HANDLE,
+            ctypes.POINTER(OVERLAPPED),
+            LPDWORD,
+            wintypes.DWORD,
+            wintypes.BOOL,
+        ]
         GetOverlappedResultEx.restype = wintypes.BOOL
 
     WaitForSingleObjectEx = ctypes.windll.kernel32.WaitForSingleObjectEx
-    WaitForSingleObjectEx.argtypes = [wintypes.HANDLE, wintypes.DWORD, wintypes.BOOL]
+    WaitForSingleObjectEx.argtypes = [
+        wintypes.HANDLE,
+        wintypes.DWORD,
+        wintypes.BOOL,
+    ]
     WaitForSingleObjectEx.restype = wintypes.DWORD
 
     CreateEvent = ctypes.windll.kernel32.CreateEventA
-    CreateEvent.argtypes = [LPDWORD, wintypes.BOOL, wintypes.BOOL,
-                            wintypes.LPSTR]
+    CreateEvent.argtypes = [
+        LPDWORD,
+        wintypes.BOOL,
+        wintypes.BOOL,
+        wintypes.LPSTR,
+    ]
     CreateEvent.restype = wintypes.HANDLE
 
     # Windows Vista is the minimum supported client for CancelIoEx.
@@ -178,9 +205,15 @@
 if _debugging:
 
     def log(fmt, *args):
-        print('[%s] %s' %
-              (time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime()),
-               fmt % args[:]))
+        print(
+            "[%s] %s"
+            % (
+                time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime()),
+                fmt % args[:],
+            )
+        )
+
+
 else:
 
     def log(fmt, *args):
@@ -193,8 +226,16 @@
     # FormatMessage will allocate memory and assign it here
     buf = ctypes.c_char_p()
     FormatMessage(
-        FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER
-        | FORMAT_MESSAGE_IGNORE_INSERTS, None, err, 0, buf, 0, None)
+        FORMAT_MESSAGE_FROM_SYSTEM
+        | FORMAT_MESSAGE_ALLOCATE_BUFFER
+        | FORMAT_MESSAGE_IGNORE_INSERTS,
+        None,
+        err,
+        0,
+        buf,
+        0,
+        None,
+    )
     try:
         return buf.value
     finally:
@@ -211,21 +252,30 @@
 
     def __str__(self):
         if self.cmd:
-            return '%s, while executing %s' % (self.msg, self.cmd)
+            return "%s, while executing %s" % (self.msg, self.cmd)
         return self.msg
 
 
+class BSERv1Unsupported(WatchmanError):
+    pass
+
+
+class UseAfterFork(WatchmanError):
+    pass
+
+
 class WatchmanEnvironmentError(WatchmanError):
     def __init__(self, msg, errno, errmsg, cmd=None):
         super(WatchmanEnvironmentError, self).__init__(
-            '{0}: errno={1} errmsg={2}'.format(msg, errno, errmsg),
-            cmd)
+            "{0}: errno={1} errmsg={2}".format(msg, errno, errmsg), cmd
+        )
 
 
 class SocketConnectError(WatchmanError):
     def __init__(self, sockpath, exc):
         super(SocketConnectError, self).__init__(
-            'unable to connect to %s: %s' % (sockpath, exc))
+            "unable to connect to %s: %s" % (sockpath, exc)
+        )
         self.sockpath = sockpath
         self.exc = exc
 
@@ -245,15 +295,16 @@
 
     self.msg is the message returned by watchman.
     """
+
     def __init__(self, msg, cmd=None):
         super(CommandError, self).__init__(
-            'watchman command error: %s' % (msg, ),
-            cmd,
+            "watchman command error: %s" % (msg,), cmd
         )
 
 
 class Transport(object):
     """ communication transport to the watchman server """
+
     buf = None
 
     def close(self):
@@ -289,7 +340,7 @@
         while True:
             b = self.readBytes(4096)
             if b"\n" in b:
-                result = b''.join(self.buf)
+                result = b"".join(self.buf)
                 (line, b) = b.split(b"\n", 1)
                 self.buf = [b]
                 return result + line
@@ -298,6 +349,7 @@
 
 class Codec(object):
     """ communication encoding for the watchman server """
+
     transport = None
 
     def __init__(self, transport):
@@ -315,9 +367,10 @@
 
 class UnixSocketTransport(Transport):
     """ local unix domain socket transport """
+
     sock = None
 
-    def __init__(self, sockpath, timeout, watchman_exe):
+    def __init__(self, sockpath, timeout):
         self.sockpath = sockpath
         self.timeout = timeout
 
@@ -331,8 +384,9 @@
             raise SocketConnectError(self.sockpath, e)
 
     def close(self):
-        self.sock.close()
-        self.sock = None
+        if self.sock:
+            self.sock.close()
+            self.sock = None
 
     def setTimeout(self, value):
         self.timeout = value
@@ -342,16 +396,16 @@
         try:
             buf = [self.sock.recv(size)]
             if not buf[0]:
-                raise WatchmanError('empty watchman response')
+                raise WatchmanError("empty watchman response")
             return buf[0]
         except socket.timeout:
-            raise SocketTimeout('timed out waiting for response')
+            raise SocketTimeout("timed out waiting for response")
 
     def write(self, data):
         try:
             self.sock.sendall(data)
         except socket.timeout:
-            raise SocketTimeout('timed out sending query command')
+            raise SocketTimeout("timed out sending query command")
 
 
 def _get_overlapped_result_ex_impl(pipe, olap, nbytes, millis, alertable):
@@ -364,7 +418,7 @@
     source code (see get_overlapped_result_ex_impl in stream_win.c). This
     way, maintenance should be simplified.
     """
-    log('Preparing to wait for maximum %dms', millis )
+    log("Preparing to wait for maximum %dms", millis)
     if millis != 0:
         waitReturnCode = WaitForSingleObjectEx(olap.hEvent, millis, alertable)
         if waitReturnCode == WAIT_OBJECT_0:
@@ -383,12 +437,12 @@
         elif waitReturnCode == WAIT_FAILED:
             # something went wrong calling WaitForSingleObjectEx
             err = GetLastError()
-            log('WaitForSingleObjectEx failed: %s', _win32_strerror(err))
+            log("WaitForSingleObjectEx failed: %s", _win32_strerror(err))
             return False
         else:
             # unexpected situation deserving investigation.
             err = GetLastError()
-            log('Unexpected error: %s', _win32_strerror(err))
+            log("Unexpected error: %s", _win32_strerror(err))
             return False
 
     return GetOverlappedResult(pipe, olap, nbytes, False)
@@ -397,36 +451,52 @@
 class WindowsNamedPipeTransport(Transport):
     """ connect to a named pipe """
 
-    def __init__(self, sockpath, timeout, watchman_exe):
+    def __init__(self, sockpath, timeout):
         self.sockpath = sockpath
         self.timeout = int(math.ceil(timeout * 1000))
         self._iobuf = None
 
-        self.pipe = CreateFile(sockpath, GENERIC_READ | GENERIC_WRITE, 0, None,
-                               OPEN_EXISTING, FILE_FLAG_OVERLAPPED, None)
+        if compat.PYTHON3:
+            sockpath = os.fsencode(sockpath)
+        self.pipe = CreateFile(
+            sockpath,
+            GENERIC_READ | GENERIC_WRITE,
+            0,
+            None,
+            OPEN_EXISTING,
+            FILE_FLAG_OVERLAPPED,
+            None,
+        )
 
-        if self.pipe == INVALID_HANDLE_VALUE:
+        err = GetLastError()
+        if self.pipe == INVALID_HANDLE_VALUE or self.pipe == 0:
             self.pipe = None
-            self._raise_win_err('failed to open pipe %s' % sockpath,
-                                GetLastError())
+            raise SocketConnectError(self.sockpath, self._make_win_err("", err))
 
         # event for the overlapped I/O operations
         self._waitable = CreateEvent(None, True, False, None)
+        err = GetLastError()
         if self._waitable is None:
-            self._raise_win_err('CreateEvent failed', GetLastError())
+            self._raise_win_err("CreateEvent failed", err)
 
         self._get_overlapped_result_ex = GetOverlappedResultEx
-        if (os.getenv('WATCHMAN_WIN7_COMPAT') == '1' or
-            self._get_overlapped_result_ex is None):
+        if (
+            os.getenv("WATCHMAN_WIN7_COMPAT") == "1"
+            or self._get_overlapped_result_ex is None
+        ):
             self._get_overlapped_result_ex = _get_overlapped_result_ex_impl
 
     def _raise_win_err(self, msg, err):
-        raise IOError('%s win32 error code: %d %s' %
-                      (msg, err, _win32_strerror(err)))
+        raise self._make_win_err(msg, err)
+
+    def _make_win_err(self, msg, err):
+        return IOError(
+            "%s win32 error code: %d %s" % (msg, err, _win32_strerror(err))
+        )
 
     def close(self):
         if self.pipe:
-            log('Closing pipe')
+            log("Closing pipe")
             CloseHandle(self.pipe)
         self.pipe = None
 
@@ -460,7 +530,7 @@
         olap = OVERLAPPED()
         olap.hEvent = self._waitable
 
-        log('made read buff of size %d', size)
+        log("made read buff of size %d", size)
 
         # ReadFile docs warn against sending in the nread parameter for async
         # operations, so we always collect it via GetOverlappedResultEx
@@ -469,23 +539,23 @@
         if not immediate:
             err = GetLastError()
             if err != ERROR_IO_PENDING:
-                self._raise_win_err('failed to read %d bytes' % size,
-                                    GetLastError())
+                self._raise_win_err("failed to read %d bytes" % size, err)
 
         nread = wintypes.DWORD()
-        if not self._get_overlapped_result_ex(self.pipe, olap, nread,
-                                              0 if immediate else self.timeout,
-                                              True):
+        if not self._get_overlapped_result_ex(
+            self.pipe, olap, nread, 0 if immediate else self.timeout, True
+        ):
             err = GetLastError()
             CancelIoEx(self.pipe, olap)
 
             if err == WAIT_TIMEOUT:
-                log('GetOverlappedResultEx timedout')
-                raise SocketTimeout('timed out after waiting %dms for read' %
-                                    self.timeout)
+                log("GetOverlappedResultEx timedout")
+                raise SocketTimeout(
+                    "timed out after waiting %dms for read" % self.timeout
+                )
 
-            log('GetOverlappedResultEx reports error %d', err)
-            self._raise_win_err('error while waiting for read', err)
+            log("GetOverlappedResultEx reports error %d", err)
+            self._raise_win_err("error while waiting for read", err)
 
         nread = nread.value
         if nread == 0:
@@ -494,7 +564,7 @@
             # other way this shows up is if the client has gotten in a weird
             # state, so let's bail out
             CancelIoEx(self.pipe, olap)
-            raise IOError('Async read yielded 0 bytes; unpossible!')
+            raise IOError("Async read yielded 0 bytes; unpossible!")
 
         # Holds precisely the bytes that we read from the prior request
         buf = buf[:nread]
@@ -511,21 +581,25 @@
         olap = OVERLAPPED()
         olap.hEvent = self._waitable
 
-        immediate = WriteFile(self.pipe, ctypes.c_char_p(data), len(data),
-                              None, olap)
+        immediate = WriteFile(
+            self.pipe, ctypes.c_char_p(data), len(data), None, olap
+        )
 
         if not immediate:
             err = GetLastError()
             if err != ERROR_IO_PENDING:
-                self._raise_win_err('failed to write %d bytes' % len(data),
-                                    GetLastError())
+                self._raise_win_err(
+                    "failed to write %d bytes to handle %r"
+                    % (len(data), self.pipe),
+                    err,
+                )
 
         # Obtain results, waiting if needed
         nwrote = wintypes.DWORD()
-        if self._get_overlapped_result_ex(self.pipe, olap, nwrote,
-                                          0 if immediate else self.timeout,
-                                          True):
-            log('made write of %d bytes', nwrote.value)
+        if self._get_overlapped_result_ex(
+            self.pipe, olap, nwrote, 0 if immediate else self.timeout, True
+        ):
+            log("made write of %d bytes", nwrote.value)
             return nwrote.value
 
         err = GetLastError()
@@ -535,10 +609,21 @@
         CancelIoEx(self.pipe, olap)
 
         if err == WAIT_TIMEOUT:
-            raise SocketTimeout('timed out after waiting %dms for write' %
-                                self.timeout)
-        self._raise_win_err('error while waiting for write of %d bytes' %
-                            len(data), err)
+            raise SocketTimeout(
+                "timed out after waiting %dms for write" % self.timeout
+            )
+        self._raise_win_err(
+            "error while waiting for write of %d bytes" % len(data), err
+        )
+
+
+def _default_binpath(binpath=None):
+    if binpath:
+        return binpath
+    # The test harness sets WATCHMAN_BINARY to the binary under test,
+    # so we use that by default, otherwise, allow resolving watchman
+    # from the users PATH.
+    return os.environ.get("WATCHMAN_BINARY", "watchman")
 
 
 class CLIProcessTransport(Transport):
@@ -560,13 +645,14 @@
     It is the responsibility of the caller to set the send and
     receive codecs appropriately.
     """
+
     proc = None
     closed = True
 
-    def __init__(self, sockpath, timeout, watchman_exe):
+    def __init__(self, sockpath, timeout, binpath=None):
         self.sockpath = sockpath
         self.timeout = timeout
-        self.watchman_exe = watchman_exe
+        self.binpath = _default_binpath(binpath)
 
     def close(self):
         if self.proc:
@@ -574,32 +660,32 @@
                 self.proc.kill()
             self.proc.stdin.close()
             self.proc.stdout.close()
+            self.proc.wait()
             self.proc = None
 
     def _connect(self):
         if self.proc:
             return self.proc
         args = [
-            self.watchman_exe,
-            '--sockname={0}'.format(self.sockpath),
-            '--logfile=/BOGUS',
-            '--statefile=/BOGUS',
-            '--no-spawn',
-            '--no-local',
-            '--no-pretty',
-            '-j',
+            self.binpath,
+            "--sockname={0}".format(self.sockpath),
+            "--logfile=/BOGUS",
+            "--statefile=/BOGUS",
+            "--no-spawn",
+            "--no-local",
+            "--no-pretty",
+            "-j",
         ]
-        self.proc = subprocess.Popen(pycompat.rapply(procutil.tonativestr,
-                                                     args),
-                                     stdin=subprocess.PIPE,
-                                     stdout=subprocess.PIPE)
+        self.proc = subprocess.Popen(
+            args, stdin=subprocess.PIPE, stdout=subprocess.PIPE
+        )
         return self.proc
 
     def readBytes(self, size):
         self._connect()
         res = self.proc.stdout.read(size)
-        if res == '':
-            raise WatchmanError('EOF on CLI process transport')
+        if not res:
+            raise WatchmanError("EOF on CLI process transport")
         return res
 
     def write(self, data):
@@ -616,13 +702,22 @@
 class BserCodec(Codec):
     """ use the BSER encoding.  This is the default, preferred codec """
 
+    def __init__(self, transport, value_encoding, value_errors):
+        super(BserCodec, self).__init__(transport)
+        self._value_encoding = value_encoding
+        self._value_errors = value_errors
+
     def _loads(self, response):
-        return bser.loads(response) # Defaults to BSER v1
+        return bser.loads(
+            response,
+            value_encoding=self._value_encoding,
+            value_errors=self._value_errors,
+        )
 
     def receive(self):
         buf = [self.transport.readBytes(sniff_len)]
         if not buf[0]:
-            raise WatchmanError('empty watchman response')
+            raise WatchmanError("empty watchman response")
 
         _1, _2, elen = bser.pdu_info(buf[0])
 
@@ -631,15 +726,15 @@
             buf.append(self.transport.readBytes(elen - rlen))
             rlen += len(buf[-1])
 
-        response = b''.join(buf)
+        response = b"".join(buf)
         try:
             res = self._loads(response)
             return res
         except ValueError as e:
-            raise WatchmanError('watchman response decode error: %s' % e)
+            raise WatchmanError("watchman response decode error: %s" % e)
 
     def send(self, *args):
-        cmd = bser.dumps(*args) # Defaults to BSER v1
+        cmd = bser.dumps(*args)  # Defaults to BSER v1
         self.transport.write(cmd)
 
 
@@ -648,74 +743,96 @@
         immutable object support """
 
     def _loads(self, response):
-        return bser.loads(response, False) # Defaults to BSER v1
+        return bser.loads(
+            response,
+            False,
+            value_encoding=self._value_encoding,
+            value_errors=self._value_errors,
+        )
 
 
 class Bser2WithFallbackCodec(BserCodec):
     """ use BSER v2 encoding """
 
-    def __init__(self, transport):
-        super(Bser2WithFallbackCodec, self).__init__(transport)
-        # Once the server advertises support for bser-v2 we should switch this
-        # to 'required' on Python 3.
-        self.send(["version", {"optional": ["bser-v2"]}])
+    def __init__(self, transport, value_encoding, value_errors):
+        super(Bser2WithFallbackCodec, self).__init__(
+            transport, value_encoding, value_errors
+        )
+        if compat.PYTHON3:
+            bserv2_key = "required"
+        else:
+            bserv2_key = "optional"
+
+        self.send(["version", {bserv2_key: ["bser-v2"]}])
 
         capabilities = self.receive()
 
-        if 'error' in capabilities:
-          raise Exception('Unsupported BSER version')
+        if "error" in capabilities:
+            raise BSERv1Unsupported(
+                "The watchman server version does not support Python 3. Please "
+                "upgrade your watchman server."
+            )
 
-        if capabilities['capabilities']['bser-v2']:
+        if capabilities["capabilities"]["bser-v2"]:
             self.bser_version = 2
             self.bser_capabilities = 0
         else:
             self.bser_version = 1
             self.bser_capabilities = 0
 
-    def _loads(self, response):
-        return bser.loads(response)
-
     def receive(self):
         buf = [self.transport.readBytes(sniff_len)]
         if not buf[0]:
-            raise WatchmanError('empty watchman response')
+            raise WatchmanError("empty watchman response")
 
         recv_bser_version, recv_bser_capabilities, elen = bser.pdu_info(buf[0])
 
-        if hasattr(self, 'bser_version'):
-          # Readjust BSER version and capabilities if necessary
-          self.bser_version = max(self.bser_version, recv_bser_version)
-          self.capabilities = self.bser_capabilities & recv_bser_capabilities
+        if hasattr(self, "bser_version"):
+            # Readjust BSER version and capabilities if necessary
+            self.bser_version = max(self.bser_version, recv_bser_version)
+            self.capabilities = self.bser_capabilities & recv_bser_capabilities
 
         rlen = len(buf[0])
         while elen > rlen:
             buf.append(self.transport.readBytes(elen - rlen))
             rlen += len(buf[-1])
 
-        response = b''.join(buf)
+        response = b"".join(buf)
         try:
             res = self._loads(response)
             return res
         except ValueError as e:
-            raise WatchmanError('watchman response decode error: %s' % e)
+            raise WatchmanError("watchman response decode error: %s" % e)
 
     def send(self, *args):
-        if hasattr(self, 'bser_version'):
-            cmd = bser.dumps(*args, version=self.bser_version,
-                capabilities=self.bser_capabilities)
+        if hasattr(self, "bser_version"):
+            cmd = bser.dumps(
+                *args,
+                version=self.bser_version,
+                capabilities=self.bser_capabilities
+            )
         else:
             cmd = bser.dumps(*args)
         self.transport.write(cmd)
 
 
+class ImmutableBser2Codec(Bser2WithFallbackCodec, ImmutableBserCodec):
+    """ use the BSER encoding, decoding values using the newer
+        immutable object support """
+
+    pass
+
+
 class JsonCodec(Codec):
     """ Use json codec.  This is here primarily for testing purposes """
+
     json = None
 
     def __init__(self, transport):
         super(JsonCodec, self).__init__(transport)
         # optional dep on json, only if JsonCodec is used
         import json
+
         self.json = json
 
     def receive(self):
@@ -727,7 +844,7 @@
             # but it's possible we might get non-ASCII bytes that are valid
             # UTF-8.
             if compat.PYTHON3:
-                line = line.decode('utf-8')
+                line = line.decode("utf-8")
             return self.json.loads(line)
         except Exception as e:
             print(e, line)
@@ -739,12 +856,13 @@
         # containing Unicode strings to Unicode string. Even with (the default)
         # ensure_ascii=True, dumps returns a Unicode string.
         if compat.PYTHON3:
-            cmd = cmd.encode('ascii')
+            cmd = cmd.encode("ascii")
         self.transport.write(cmd + b"\n")
 
 
 class client(object):
     """ Handles the communication with the watchman service """
+
     sockpath = None
     transport = None
     sendCodec = None
@@ -754,60 +872,100 @@
     subs = {}  # Keyed by subscription name
     sub_by_root = {}  # Keyed by root, then by subscription name
     logs = []  # When log level is raised
-    unilateral = ['log', 'subscription']
+    unilateral = ["log", "subscription"]
     tport = None
     useImmutableBser = None
-    watchman_exe = None
+    pid = None
 
-    def __init__(self,
-                 sockpath=None,
-                 timeout=1.0,
-                 transport=None,
-                 sendEncoding=None,
-                 recvEncoding=None,
-                 useImmutableBser=False,
-                 watchman_exe=None):
+    def __init__(
+        self,
+        sockpath=None,
+        timeout=1.0,
+        transport=None,
+        sendEncoding=None,
+        recvEncoding=None,
+        useImmutableBser=False,
+        # use False for these two because None has a special
+        # meaning
+        valueEncoding=False,
+        valueErrors=False,
+        binpath=None,
+    ):
         self.sockpath = sockpath
         self.timeout = timeout
         self.useImmutableBser = useImmutableBser
-        self.watchman_exe = watchman_exe
+        self.binpath = _default_binpath(binpath)
 
         if inspect.isclass(transport) and issubclass(transport, Transport):
             self.transport = transport
         else:
-            transport = transport or os.getenv('WATCHMAN_TRANSPORT') or 'local'
-            if transport == 'local' and os.name == 'nt':
+            transport = transport or os.getenv("WATCHMAN_TRANSPORT") or "local"
+            if transport == "local" and os.name == "nt":
                 self.transport = WindowsNamedPipeTransport
-            elif transport == 'local':
+            elif transport == "local":
                 self.transport = UnixSocketTransport
-            elif transport == 'cli':
+            elif transport == "cli":
                 self.transport = CLIProcessTransport
                 if sendEncoding is None:
-                    sendEncoding = 'json'
+                    sendEncoding = "json"
                 if recvEncoding is None:
                     recvEncoding = sendEncoding
             else:
-                raise WatchmanError('invalid transport %s' % transport)
+                raise WatchmanError("invalid transport %s" % transport)
 
-        sendEncoding = str(sendEncoding or os.getenv('WATCHMAN_ENCODING') or
-                           'bser')
-        recvEncoding = str(recvEncoding or os.getenv('WATCHMAN_ENCODING') or
-                           'bser')
+        sendEncoding = str(
+            sendEncoding or os.getenv("WATCHMAN_ENCODING") or "bser"
+        )
+        recvEncoding = str(
+            recvEncoding or os.getenv("WATCHMAN_ENCODING") or "bser"
+        )
 
         self.recvCodec = self._parseEncoding(recvEncoding)
         self.sendCodec = self._parseEncoding(sendEncoding)
 
+        # We want to act like the native OS methods as much as possible. This
+        # means returning bytestrings on Python 2 by default and Unicode
+        # strings on Python 3. However we take an optional argument that lets
+        # users override this.
+        if valueEncoding is False:
+            if compat.PYTHON3:
+                self.valueEncoding = encoding.get_local_encoding()
+                self.valueErrors = encoding.default_local_errors
+            else:
+                self.valueEncoding = None
+                self.valueErrors = None
+        else:
+            self.valueEncoding = valueEncoding
+            if valueErrors is False:
+                self.valueErrors = encoding.default_local_errors
+            else:
+                self.valueErrors = valueErrors
+
+    def _makeBSERCodec(self, codec):
+        def make_codec(transport):
+            return codec(transport, self.valueEncoding, self.valueErrors)
+
+        return make_codec
+
     def _parseEncoding(self, enc):
-        if enc == 'bser':
+        if enc == "bser":
             if self.useImmutableBser:
-                return ImmutableBserCodec
-            return BserCodec
-        elif enc == 'experimental-bser-v2':
-          return Bser2WithFallbackCodec
-        elif enc == 'json':
+                return self._makeBSERCodec(ImmutableBser2Codec)
+            return self._makeBSERCodec(Bser2WithFallbackCodec)
+        elif enc == "bser-v1":
+            if compat.PYTHON3:
+                raise BSERv1Unsupported(
+                    "Python 3 does not support the BSER v1 encoding: specify "
+                    '"bser" or omit the sendEncoding and recvEncoding '
+                    "arguments"
+                )
+            if self.useImmutableBser:
+                return self._makeBSERCodec(ImmutableBserCodec)
+            return self._makeBSERCodec(BserCodec)
+        elif enc == "json":
             return JsonCodec
         else:
-            raise WatchmanError('invalid encoding %s' % enc)
+            raise WatchmanError("invalid encoding %s" % enc)
 
     def _hasprop(self, result, name):
         if self.useImmutableBser:
@@ -817,26 +975,25 @@
     def _resolvesockname(self):
         # if invoked via a trigger, watchman will set this env var; we
         # should use it unless explicitly set otherwise
-        path = os.getenv('WATCHMAN_SOCK')
+        path = os.getenv("WATCHMAN_SOCK")
         if path:
             return path
 
-        cmd = [self.watchman_exe, '--output-encoding=bser', 'get-sockname']
+        cmd = [self.binpath, "--output-encoding=bser", "get-sockname"]
         try:
-            args = dict(stdout=subprocess.PIPE,
-                        stderr=subprocess.PIPE,
-                        close_fds=os.name != 'nt')
+            args = dict(
+                stdout=subprocess.PIPE, stderr=subprocess.PIPE
+            )  # noqa: C408
 
-            if os.name == 'nt':
+            if os.name == "nt":
                 # if invoked via an application with graphical user interface,
                 # this call will cause a brief command window pop-up.
                 # Using the flag STARTF_USESHOWWINDOW to avoid this behavior.
                 startupinfo = subprocess.STARTUPINFO()
                 startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
-                args['startupinfo'] = startupinfo
+                args["startupinfo"] = startupinfo
 
-            p = subprocess.Popen(pycompat.rapply(procutil.tonativestr, cmd),
-                                 **args)
+            p = subprocess.Popen(cmd, **args)
 
         except OSError as e:
             raise WatchmanError('"watchman" executable not in PATH (%s)' % e)
@@ -848,27 +1005,43 @@
             raise WatchmanError("watchman exited with code %d" % exitcode)
 
         result = bser.loads(stdout)
-        if b'error' in result:
-            raise WatchmanError('get-sockname error: %s' % result['error'])
+        if "error" in result:
+            raise WatchmanError("get-sockname error: %s" % result["error"])
 
-        return result[b'sockname']
+        return result["sockname"]
 
     def _connect(self):
         """ establish transport connection """
 
         if self.recvConn:
+            if self.pid != os.getpid():
+                raise UseAfterFork(
+                    "do not re-use a connection after fork; open a new client instead"
+                )
             return
 
         if self.sockpath is None:
             self.sockpath = self._resolvesockname()
 
-        self.tport = self.transport(self.sockpath, self.timeout, self.watchman_exe)
+        kwargs = {}
+        if self.transport == CLIProcessTransport:
+            kwargs["binpath"] = self.binpath
+
+        self.tport = self.transport(self.sockpath, self.timeout, **kwargs)
         self.sendConn = self.sendCodec(self.tport)
         self.recvConn = self.recvCodec(self.tport)
+        self.pid = os.getpid()
 
     def __del__(self):
         self.close()
 
+    def __enter__(self):
+        self._connect()
+        return self
+
+    def __exit__(self, exc_type, exc_value, exc_traceback):
+        self.close()
+
     def close(self):
         if self.tport:
             self.tport.close()
@@ -893,26 +1066,20 @@
 
         self._connect()
         result = self.recvConn.receive()
-        if self._hasprop(result, 'error'):
-            error = result['error']
-            if compat.PYTHON3 and isinstance(self.recvConn, BserCodec):
-                error = result['error'].decode('utf-8', 'surrogateescape')
-            raise CommandError(error)
+        if self._hasprop(result, "error"):
+            raise CommandError(result["error"])
 
-        if self._hasprop(result, 'log'):
-            log = result['log']
-            if compat.PYTHON3 and isinstance(self.recvConn, BserCodec):
-                log = log.decode('utf-8', 'surrogateescape')
-            self.logs.append(log)
+        if self._hasprop(result, "log"):
+            self.logs.append(result["log"])
 
-        if self._hasprop(result, 'subscription'):
-            sub = result['subscription']
+        if self._hasprop(result, "subscription"):
+            sub = result["subscription"]
             if not (sub in self.subs):
                 self.subs[sub] = []
             self.subs[sub].append(result)
 
             # also accumulate in {root,sub} keyed store
-            root = os.path.normcase(result['root'])
+            root = os.path.normpath(os.path.normcase(result["root"]))
             if not root in self.sub_by_root:
                 self.sub_by_root[root] = {}
             if not sub in self.sub_by_root[root]:
@@ -922,7 +1089,7 @@
         return result
 
     def isUnilateralResponse(self, res):
-        if 'unilateral' in res and res['unilateral']:
+        if "unilateral" in res and res["unilateral"]:
             return True
         # Fall back to checking for known unilateral responses
         for k in self.unilateral:
@@ -955,18 +1122,11 @@
         remove processing impacts both the unscoped and scoped stores
         for the subscription data.
         """
-        if compat.PYTHON3 and issubclass(self.recvCodec, BserCodec):
-            # People may pass in Unicode strings here -- but currently BSER only
-            # returns bytestrings. Deal with that.
-            if isinstance(root, str):
-                root = encoding.encode_local(root)
-            if isinstance(name, str):
-                name = name.encode('utf-8')
-
         if root is not None:
-            if not root in self.sub_by_root:
+            root = os.path.normpath(os.path.normcase(root))
+            if root not in self.sub_by_root:
                 return None
-            if not name in self.sub_by_root[root]:
+            if name not in self.sub_by_root[root]:
                 return None
             sub = self.sub_by_root[root][name]
             if remove:
@@ -976,7 +1136,7 @@
                     del self.subs[name]
             return sub
 
-        if not (name in self.subs):
+        if name not in self.subs:
             return None
         sub = self.subs[name]
         if remove:
@@ -992,7 +1152,7 @@
         and NOT returned via this method.
         """
 
-        log('calling client.query')
+        log("calling client.query")
         self._connect()
         try:
             self.sendConn.send(args)
@@ -1006,27 +1166,27 @@
             # When we can depend on Python 3, we can use PEP 3134
             # exception chaining here.
             raise WatchmanEnvironmentError(
-                'I/O error communicating with watchman daemon',
+                "I/O error communicating with watchman daemon",
                 ee.errno,
                 ee.strerror,
-                args)
+                args,
+            )
         except WatchmanError as ex:
             ex.setCommand(args)
             raise
 
     def capabilityCheck(self, optional=None, required=None):
         """ Perform a server capability check """
-        res = self.query('version', {
-            'optional': optional or [],
-            'required': required or []
-        })
+        res = self.query(
+            "version", {"optional": optional or [], "required": required or []}
+        )
 
-        if not self._hasprop(res, 'capabilities'):
+        if not self._hasprop(res, "capabilities"):
             # Server doesn't support capabilities, so we need to
             # synthesize the results based on the version
             capabilities.synthesize(res, optional)
-            if 'error' in res:
-                raise CommandError(res['error'])
+            if "error" in res:
+                raise CommandError(res["error"])
 
         return res
 
--- a/hgext/fsmonitor/pywatchman/bser.c	Mon Nov 04 00:16:44 2019 +0100
+++ b/hgext/fsmonitor/pywatchman/bser.c	Tue Nov 05 13:19:24 2019 -0800
@@ -175,7 +175,22 @@
     const char* item_name = NULL;
     PyObject* key = PyTuple_GET_ITEM(obj->keys, i);
 
-    item_name = PyBytes_AsString(key);
+    if (PyUnicode_Check(key)) {
+#if PY_MAJOR_VERSION >= 3
+      item_name = PyUnicode_AsUTF8(key);
+#else
+      PyObject* utf = PyUnicode_AsEncodedString(key, "utf-8", "ignore");
+      if (utf == NULL) {
+        goto bail;
+      }
+      item_name = PyBytes_AsString(utf);
+#endif
+    } else {
+      item_name = PyBytes_AsString(key);
+    }
+    if (item_name == NULL) {
+      goto bail;
+    }
     if (!strcmp(item_name, namestr)) {
       ret = PySequence_GetItem(obj->values, i);
       goto bail;
@@ -1147,11 +1162,15 @@
 }
 
 static PyObject* bser_load(PyObject* self, PyObject* args, PyObject* kw) {
-  PyObject *load, *string;
+  PyObject* load;
+  PyObject* load_method;
+  PyObject* string;
+  PyObject* load_method_args;
+  PyObject* load_method_kwargs;
   PyObject* fp = NULL;
   PyObject* mutable_obj = NULL;
-  const char* value_encoding = NULL;
-  const char* value_errors = NULL;
+  PyObject* value_encoding = NULL;
+  PyObject* value_errors = NULL;
 
   static char* kw_list[] = {
       "fp", "mutable", "value_encoding", "value_errors", NULL};
@@ -1159,7 +1178,7 @@
   if (!PyArg_ParseTupleAndKeywords(
           args,
           kw,
-          "OOzz:load",
+          "O|OOO:load",
           kw_list,
           &fp,
           &mutable_obj,
@@ -1172,8 +1191,33 @@
   if (load == NULL) {
     return NULL;
   }
-  string = PyObject_CallMethod(
-      load, "load", "OOzz", fp, mutable_obj, value_encoding, value_errors);
+  load_method = PyObject_GetAttrString(load, "load");
+  if (load_method == NULL) {
+    return NULL;
+  }
+  // Mandatory method arguments
+  load_method_args = Py_BuildValue("(O)", fp);
+  if (load_method_args == NULL) {
+    return NULL;
+  }
+  // Optional method arguments
+  load_method_kwargs = PyDict_New();
+  if (load_method_kwargs == NULL) {
+    return NULL;
+  }
+  if (mutable_obj) {
+    PyDict_SetItemString(load_method_kwargs, "mutable", mutable_obj);
+  }
+  if (value_encoding) {
+    PyDict_SetItemString(load_method_kwargs, "value_encoding", value_encoding);
+  }
+  if (value_errors) {
+    PyDict_SetItemString(load_method_kwargs, "value_errors", value_errors);
+  }
+  string = PyObject_Call(load_method, load_method_args, load_method_kwargs);
+  Py_DECREF(load_method_kwargs);
+  Py_DECREF(load_method_args);
+  Py_DECREF(load_method);
   Py_DECREF(load);
   return string;
 }
--- a/hgext/fsmonitor/pywatchman/capabilities.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/hgext/fsmonitor/pywatchman/capabilities.py	Tue Nov 05 13:19:24 2019 -0800
@@ -26,20 +26,20 @@
 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
-from __future__ import absolute_import
-from __future__ import division
-from __future__ import print_function
 # no unicode literals
+from __future__ import absolute_import, division, print_function
 
 import re
 
+
 def parse_version(vstr):
     res = 0
-    for n in vstr.split('.'):
+    for n in vstr.split("."):
         res = res * 1000
         res = res + int(n)
     return res
 
+
 cap_versions = {
     "cmd-watch-del-all": "3.1.1",
     "cmd-watch-project": "3.1",
@@ -49,23 +49,29 @@
     "wildmatch": "3.7",
 }
 
+
 def check(version, name):
     if name in cap_versions:
         return version >= parse_version(cap_versions[name])
     return False
 
+
 def synthesize(vers, opts):
     """ Synthesize a capability enabled version response
         This is a very limited emulation for relatively recent feature sets
     """
-    parsed_version = parse_version(vers['version'])
-    vers['capabilities'] = {}
-    for name in opts['optional']:
-        vers['capabilities'][name] = check(parsed_version, name)
-    for name in opts['required']:
+    parsed_version = parse_version(vers["version"])
+    vers["capabilities"] = {}
+    for name in opts["optional"]:
+        vers["capabilities"][name] = check(parsed_version, name)
+    failed = False  # noqa: F841 T25377293 Grandfathered in
+    for name in opts["required"]:
         have = check(parsed_version, name)
-        vers['capabilities'][name] = have
+        vers["capabilities"][name] = have
         if not have:
-            vers['error'] = 'client required capability `' + name + \
-                            '` is not supported by this server'
+            vers["error"] = (
+                "client required capability `"
+                + name
+                + "` is not supported by this server"
+            )
     return vers
--- a/hgext/fsmonitor/pywatchman/compat.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/hgext/fsmonitor/pywatchman/compat.py	Tue Nov 05 13:19:24 2019 -0800
@@ -26,20 +26,22 @@
 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
-from __future__ import absolute_import
-from __future__ import division
-from __future__ import print_function
 # no unicode literals
-
-'''Compatibility module across Python 2 and 3.'''
+from __future__ import absolute_import, division, print_function
 
 import sys
 
+
+"""Compatibility module across Python 2 and 3."""
+
+
+PYTHON2 = sys.version_info < (3, 0)
 PYTHON3 = sys.version_info >= (3, 0)
 
 # This is adapted from https://bitbucket.org/gutworth/six, and used under the
 # MIT license. See LICENSE for a full copyright notice.
 if PYTHON3:
+
     def reraise(tp, value, tb=None):
         try:
             if value is None:
@@ -50,16 +52,20 @@
         finally:
             value = None
             tb = None
+
+
 else:
-    exec('''
+    exec(
+        """
 def reraise(tp, value, tb=None):
     try:
         raise tp, value, tb
     finally:
         tb = None
-'''.strip())
+""".strip()
+    )
 
 if PYTHON3:
     UNICODE = str
 else:
-    UNICODE = unicode
+    UNICODE = unicode  # noqa: F821 We handled versioning above
--- a/hgext/fsmonitor/pywatchman/encoding.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/hgext/fsmonitor/pywatchman/encoding.py	Tue Nov 05 13:19:24 2019 -0800
@@ -26,48 +26,50 @@
 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
-from __future__ import absolute_import
-from __future__ import division
-from __future__ import print_function
 # no unicode literals
-
-'''Module to deal with filename encoding on the local system, as returned by
-Watchman.'''
+from __future__ import absolute_import, division, print_function
 
 import sys
 
-from . import (
-    compat,
-)
+from . import compat
+
+
+"""Module to deal with filename encoding on the local system, as returned by
+Watchman."""
+
 
 if compat.PYTHON3:
-    default_local_errors = 'surrogateescape'
+    default_local_errors = "surrogateescape"
 
     def get_local_encoding():
-        if sys.platform == 'win32':
+        if sys.platform == "win32":
             # Watchman always returns UTF-8 encoded strings on Windows.
-            return 'utf-8'
+            return "utf-8"
         # On the Python 3 versions we support, sys.getfilesystemencoding never
         # returns None.
         return sys.getfilesystemencoding()
+
+
 else:
     # Python 2 doesn't support surrogateescape, so use 'strict' by
     # default. Users can register a custom surrogateescape error handler and use
     # that if they so desire.
-    default_local_errors = 'strict'
+    default_local_errors = "strict"
 
     def get_local_encoding():
-        if sys.platform == 'win32':
+        if sys.platform == "win32":
             # Watchman always returns UTF-8 encoded strings on Windows.
-            return 'utf-8'
+            return "utf-8"
         fsencoding = sys.getfilesystemencoding()
         if fsencoding is None:
             # This is very unlikely to happen, but if it does, just use UTF-8
-            fsencoding = 'utf-8'
+            fsencoding = "utf-8"
         return fsencoding
 
+
 def encode_local(s):
     return s.encode(get_local_encoding(), default_local_errors)
 
+
 def decode_local(bs):
     return bs.decode(get_local_encoding(), default_local_errors)
--- a/hgext/fsmonitor/pywatchman/load.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/hgext/fsmonitor/pywatchman/load.py	Tue Nov 05 13:19:24 2019 -0800
@@ -26,17 +26,17 @@
 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
-from __future__ import absolute_import
-from __future__ import division
-from __future__ import print_function
 # no unicode literals
+from __future__ import absolute_import, division, print_function
+
+import ctypes
+
 
 try:
     from . import bser
 except ImportError:
     from . import pybser as bser
 
-import ctypes
 
 EMPTY_HEADER = b"\x00\x01\x05\x00\x00\x00\x00"
 
@@ -95,13 +95,15 @@
         ctypes.resize(buf, total_len)
 
     body = (ctypes.c_char * (total_len - len(header))).from_buffer(
-        buf, len(header))
+        buf, len(header)
+    )
     read_len = _read_bytes(fp, body)
     if read_len < len(body):
-        raise RuntimeError('bser data ended early')
+        raise RuntimeError("bser data ended early")
 
     return bser.loads(
         (ctypes.c_char * total_len).from_buffer(buf, 0),
         mutable,
         value_encoding,
-        value_errors)
+        value_errors,
+    )
--- a/hgext/fsmonitor/pywatchman/pybser.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/hgext/fsmonitor/pywatchman/pybser.py	Tue Nov 05 13:19:24 2019 -0800
@@ -26,10 +26,8 @@
 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
-from __future__ import absolute_import
-from __future__ import division
-from __future__ import print_function
 # no unicode literals
+from __future__ import absolute_import, division, print_function
 
 import binascii
 import collections
@@ -37,30 +35,31 @@
 import struct
 import sys
 
-from . import (
-    compat,
-)
+from . import compat
+
 
-BSER_ARRAY = b'\x00'
-BSER_OBJECT = b'\x01'
-BSER_BYTESTRING = b'\x02'
-BSER_INT8 = b'\x03'
-BSER_INT16 = b'\x04'
-BSER_INT32 = b'\x05'
-BSER_INT64 = b'\x06'
-BSER_REAL = b'\x07'
-BSER_TRUE = b'\x08'
-BSER_FALSE = b'\x09'
-BSER_NULL = b'\x0a'
-BSER_TEMPLATE = b'\x0b'
-BSER_SKIP = b'\x0c'
-BSER_UTF8STRING = b'\x0d'
+BSER_ARRAY = b"\x00"
+BSER_OBJECT = b"\x01"
+BSER_BYTESTRING = b"\x02"
+BSER_INT8 = b"\x03"
+BSER_INT16 = b"\x04"
+BSER_INT32 = b"\x05"
+BSER_INT64 = b"\x06"
+BSER_REAL = b"\x07"
+BSER_TRUE = b"\x08"
+BSER_FALSE = b"\x09"
+BSER_NULL = b"\x0a"
+BSER_TEMPLATE = b"\x0b"
+BSER_SKIP = b"\x0c"
+BSER_UTF8STRING = b"\x0d"
 
 if compat.PYTHON3:
     STRING_TYPES = (str, bytes)
     unicode = str
+
     def tobytes(i):
-        return str(i).encode('ascii')
+        return str(i).encode("ascii")
+
     long = int
 else:
     STRING_TYPES = (unicode, str)
@@ -72,6 +71,7 @@
 EMPTY_HEADER = b"\x00\x01\x05\x00\x00\x00\x00"
 EMPTY_HEADER_V2 = b"\x00\x02\x00\x00\x00\x00\x05\x00\x00\x00\x00"
 
+
 def _int_size(x):
     """Return the smallest size int that can store the value"""
     if -0x80 <= x <= 0x7F:
@@ -83,29 +83,34 @@
     elif long(-0x8000000000000000) <= x <= long(0x7FFFFFFFFFFFFFFF):
         return 8
     else:
-        raise RuntimeError('Cannot represent value: ' + str(x))
+        raise RuntimeError("Cannot represent value: " + str(x))
+
 
 def _buf_pos(buf, pos):
     ret = buf[pos]
-    # In Python 2, buf is a str array so buf[pos] is a string. In Python 3, buf
-    # is a bytes array and buf[pos] is an integer.
-    if compat.PYTHON3:
+    # Normalize the return type to bytes
+    if compat.PYTHON3 and not isinstance(ret, bytes):
         ret = bytes((ret,))
     return ret
 
+
 class _bser_buffer(object):
-
     def __init__(self, version):
         self.bser_version = version
         self.buf = ctypes.create_string_buffer(8192)
         if self.bser_version == 1:
-            struct.pack_into(tobytes(len(EMPTY_HEADER)) + b's', self.buf, 0,
-                             EMPTY_HEADER)
+            struct.pack_into(
+                tobytes(len(EMPTY_HEADER)) + b"s", self.buf, 0, EMPTY_HEADER
+            )
             self.wpos = len(EMPTY_HEADER)
         else:
             assert self.bser_version == 2
-            struct.pack_into(tobytes(len(EMPTY_HEADER_V2)) + b's', self.buf, 0,
-                             EMPTY_HEADER_V2)
+            struct.pack_into(
+                tobytes(len(EMPTY_HEADER_V2)) + b"s",
+                self.buf,
+                0,
+                EMPTY_HEADER_V2,
+            )
             self.wpos = len(EMPTY_HEADER_V2)
 
     def ensure_size(self, size):
@@ -117,42 +122,68 @@
         to_write = size + 1
         self.ensure_size(to_write)
         if size == 1:
-            struct.pack_into(b'=cb', self.buf, self.wpos, BSER_INT8, val)
+            struct.pack_into(b"=cb", self.buf, self.wpos, BSER_INT8, val)
         elif size == 2:
-            struct.pack_into(b'=ch', self.buf, self.wpos, BSER_INT16, val)
+            struct.pack_into(b"=ch", self.buf, self.wpos, BSER_INT16, val)
         elif size == 4:
-            struct.pack_into(b'=ci', self.buf, self.wpos, BSER_INT32, val)
+            struct.pack_into(b"=ci", self.buf, self.wpos, BSER_INT32, val)
         elif size == 8:
-            struct.pack_into(b'=cq', self.buf, self.wpos, BSER_INT64, val)
+            struct.pack_into(b"=cq", self.buf, self.wpos, BSER_INT64, val)
         else:
-            raise RuntimeError('Cannot represent this long value')
+            raise RuntimeError("Cannot represent this long value")
         self.wpos += to_write
 
-
     def append_string(self, s):
         if isinstance(s, unicode):
-            s = s.encode('utf-8')
+            s = s.encode("utf-8")
         s_len = len(s)
         size = _int_size(s_len)
         to_write = 2 + size + s_len
         self.ensure_size(to_write)
         if size == 1:
-            struct.pack_into(b'=ccb' + tobytes(s_len) + b's', self.buf,
-                self.wpos, BSER_BYTESTRING, BSER_INT8, s_len, s)
+            struct.pack_into(
+                b"=ccb" + tobytes(s_len) + b"s",
+                self.buf,
+                self.wpos,
+                BSER_BYTESTRING,
+                BSER_INT8,
+                s_len,
+                s,
+            )
         elif size == 2:
-            struct.pack_into(b'=cch' + tobytes(s_len) + b's', self.buf,
-                self.wpos, BSER_BYTESTRING, BSER_INT16, s_len, s)
+            struct.pack_into(
+                b"=cch" + tobytes(s_len) + b"s",
+                self.buf,
+                self.wpos,
+                BSER_BYTESTRING,
+                BSER_INT16,
+                s_len,
+                s,
+            )
         elif size == 4:
-            struct.pack_into(b'=cci' + tobytes(s_len) + b's', self.buf,
-                self.wpos, BSER_BYTESTRING, BSER_INT32, s_len, s)
+            struct.pack_into(
+                b"=cci" + tobytes(s_len) + b"s",
+                self.buf,
+                self.wpos,
+                BSER_BYTESTRING,
+                BSER_INT32,
+                s_len,
+                s,
+            )
         elif size == 8:
-            struct.pack_into(b'=ccq' + tobytes(s_len) + b's', self.buf,
-                self.wpos, BSER_BYTESTRING, BSER_INT64, s_len, s)
+            struct.pack_into(
+                b"=ccq" + tobytes(s_len) + b"s",
+                self.buf,
+                self.wpos,
+                BSER_BYTESTRING,
+                BSER_INT64,
+                s_len,
+                s,
+            )
         else:
-            raise RuntimeError('Cannot represent this string value')
+            raise RuntimeError("Cannot represent this string value")
         self.wpos += to_write
 
-
     def append_recursive(self, val):
         if isinstance(val, bool):
             needed = 1
@@ -161,12 +192,12 @@
                 to_encode = BSER_TRUE
             else:
                 to_encode = BSER_FALSE
-            struct.pack_into(b'=c', self.buf, self.wpos, to_encode)
+            struct.pack_into(b"=c", self.buf, self.wpos, to_encode)
             self.wpos += needed
         elif val is None:
             needed = 1
             self.ensure_size(needed)
-            struct.pack_into(b'=c', self.buf, self.wpos, BSER_NULL)
+            struct.pack_into(b"=c", self.buf, self.wpos, BSER_NULL)
             self.wpos += needed
         elif isinstance(val, (int, long)):
             self.append_long(val)
@@ -175,61 +206,106 @@
         elif isinstance(val, float):
             needed = 9
             self.ensure_size(needed)
-            struct.pack_into(b'=cd', self.buf, self.wpos, BSER_REAL, val)
+            struct.pack_into(b"=cd", self.buf, self.wpos, BSER_REAL, val)
             self.wpos += needed
-        elif isinstance(val, collections.Mapping) and \
-            isinstance(val, collections.Sized):
+        elif isinstance(val, collections.Mapping) and isinstance(
+            val, collections.Sized
+        ):
             val_len = len(val)
             size = _int_size(val_len)
             needed = 2 + size
             self.ensure_size(needed)
             if size == 1:
-                struct.pack_into(b'=ccb', self.buf, self.wpos, BSER_OBJECT,
-                    BSER_INT8, val_len)
+                struct.pack_into(
+                    b"=ccb",
+                    self.buf,
+                    self.wpos,
+                    BSER_OBJECT,
+                    BSER_INT8,
+                    val_len,
+                )
             elif size == 2:
-                struct.pack_into(b'=cch', self.buf, self.wpos, BSER_OBJECT,
-                    BSER_INT16, val_len)
+                struct.pack_into(
+                    b"=cch",
+                    self.buf,
+                    self.wpos,
+                    BSER_OBJECT,
+                    BSER_INT16,
+                    val_len,
+                )
             elif size == 4:
-                struct.pack_into(b'=cci', self.buf, self.wpos, BSER_OBJECT,
-                    BSER_INT32, val_len)
+                struct.pack_into(
+                    b"=cci",
+                    self.buf,
+                    self.wpos,
+                    BSER_OBJECT,
+                    BSER_INT32,
+                    val_len,
+                )
             elif size == 8:
-                struct.pack_into(b'=ccq', self.buf, self.wpos, BSER_OBJECT,
-                    BSER_INT64, val_len)
+                struct.pack_into(
+                    b"=ccq",
+                    self.buf,
+                    self.wpos,
+                    BSER_OBJECT,
+                    BSER_INT64,
+                    val_len,
+                )
             else:
-                raise RuntimeError('Cannot represent this mapping value')
+                raise RuntimeError("Cannot represent this mapping value")
             self.wpos += needed
             if compat.PYTHON3:
                 iteritems = val.items()
             else:
-                iteritems = val.iteritems()
+                iteritems = val.iteritems()  # noqa: B301 Checked version above
             for k, v in iteritems:
                 self.append_string(k)
                 self.append_recursive(v)
-        elif isinstance(val, collections.Iterable) and \
-            isinstance(val, collections.Sized):
+        elif isinstance(val, collections.Iterable) and isinstance(
+            val, collections.Sized
+        ):
             val_len = len(val)
             size = _int_size(val_len)
             needed = 2 + size
             self.ensure_size(needed)
             if size == 1:
-                struct.pack_into(b'=ccb', self.buf, self.wpos, BSER_ARRAY,
-                    BSER_INT8, val_len)
+                struct.pack_into(
+                    b"=ccb", self.buf, self.wpos, BSER_ARRAY, BSER_INT8, val_len
+                )
             elif size == 2:
-                struct.pack_into(b'=cch', self.buf, self.wpos, BSER_ARRAY,
-                    BSER_INT16, val_len)
+                struct.pack_into(
+                    b"=cch",
+                    self.buf,
+                    self.wpos,
+                    BSER_ARRAY,
+                    BSER_INT16,
+                    val_len,
+                )
             elif size == 4:
-                struct.pack_into(b'=cci', self.buf, self.wpos, BSER_ARRAY,
-                    BSER_INT32, val_len)
+                struct.pack_into(
+                    b"=cci",
+                    self.buf,
+                    self.wpos,
+                    BSER_ARRAY,
+                    BSER_INT32,
+                    val_len,
+                )
             elif size == 8:
-                struct.pack_into(b'=ccq', self.buf, self.wpos, BSER_ARRAY,
-                    BSER_INT64, val_len)
+                struct.pack_into(
+                    b"=ccq",
+                    self.buf,
+                    self.wpos,
+                    BSER_ARRAY,
+                    BSER_INT64,
+                    val_len,
+                )
             else:
-                raise RuntimeError('Cannot represent this sequence value')
+                raise RuntimeError("Cannot represent this sequence value")
             self.wpos += needed
             for v in val:
                 self.append_recursive(v)
         else:
-            raise RuntimeError('Cannot represent unknown value type')
+            raise RuntimeError("Cannot represent unknown value type")
 
 
 def dumps(obj, version=1, capabilities=0):
@@ -238,18 +314,19 @@
     # Now fill in the overall length
     if version == 1:
         obj_len = bser_buf.wpos - len(EMPTY_HEADER)
-        struct.pack_into(b'=i', bser_buf.buf, 3, obj_len)
+        struct.pack_into(b"=i", bser_buf.buf, 3, obj_len)
     else:
         obj_len = bser_buf.wpos - len(EMPTY_HEADER_V2)
-        struct.pack_into(b'=i', bser_buf.buf, 2, capabilities)
-        struct.pack_into(b'=i', bser_buf.buf, 7, obj_len)
-    return bser_buf.buf.raw[:bser_buf.wpos]
+        struct.pack_into(b"=i", bser_buf.buf, 2, capabilities)
+        struct.pack_into(b"=i", bser_buf.buf, 7, obj_len)
+    return bser_buf.buf.raw[: bser_buf.wpos]
+
 
 # This is a quack-alike with the bserObjectType in bser.c
 # It provides by getattr accessors and getitem for both index
 # and name.
 class _BunserDict(object):
-    __slots__ = ('_keys', '_values')
+    __slots__ = ("_keys", "_values")
 
     def __init__(self, keys, values):
         self._keys = keys
@@ -261,18 +338,19 @@
     def __getitem__(self, key):
         if isinstance(key, (int, long)):
             return self._values[key]
-        elif key.startswith('st_'):
+        elif key.startswith("st_"):
             # hack^Wfeature to allow mercurial to use "st_size" to
             # reference "size"
             key = key[3:]
         try:
             return self._values[self._keys.index(key)]
         except ValueError:
-            raise KeyError('_BunserDict has no key %s' % key)
+            raise KeyError("_BunserDict has no key %s" % key)
 
     def __len__(self):
         return len(self._keys)
 
+
 class Bunser(object):
     def __init__(self, mutable=True, value_encoding=None, value_errors=None):
         self.mutable = mutable
@@ -281,7 +359,7 @@
         if value_encoding is None:
             self.value_errors = None
         elif value_errors is None:
-            self.value_errors = 'strict'
+            self.value_errors = "strict"
         else:
             self.value_errors = value_errors
 
@@ -290,33 +368,35 @@
         try:
             int_type = _buf_pos(buf, pos)
         except IndexError:
-            raise ValueError('Invalid bser int encoding, pos out of range')
+            raise ValueError("Invalid bser int encoding, pos out of range")
         if int_type == BSER_INT8:
             needed = 2
-            fmt = b'=b'
+            fmt = b"=b"
         elif int_type == BSER_INT16:
             needed = 3
-            fmt = b'=h'
+            fmt = b"=h"
         elif int_type == BSER_INT32:
             needed = 5
-            fmt = b'=i'
+            fmt = b"=i"
         elif int_type == BSER_INT64:
             needed = 9
-            fmt = b'=q'
+            fmt = b"=q"
         else:
-            raise ValueError('Invalid bser int encoding 0x%s' %
-                             binascii.hexlify(int_type).decode('ascii'))
+            raise ValueError(
+                "Invalid bser int encoding 0x%s at position %s"
+                % (binascii.hexlify(int_type).decode("ascii"), pos)
+            )
         int_val = struct.unpack_from(fmt, buf, pos + 1)[0]
         return (int_val, pos + needed)
 
     def unser_utf8_string(self, buf, pos):
         str_len, pos = self.unser_int(buf, pos + 1)
-        str_val = struct.unpack_from(tobytes(str_len) + b's', buf, pos)[0]
-        return (str_val.decode('utf-8'), pos + str_len)
+        str_val = struct.unpack_from(tobytes(str_len) + b"s", buf, pos)[0]
+        return (str_val.decode("utf-8"), pos + str_len)
 
     def unser_bytestring(self, buf, pos):
         str_len, pos = self.unser_int(buf, pos + 1)
-        str_val = struct.unpack_from(tobytes(str_len) + b's', buf, pos)[0]
+        str_val = struct.unpack_from(tobytes(str_len) + b"s", buf, pos)[0]
         if self.value_encoding is not None:
             str_val = str_val.decode(self.value_encoding, self.value_errors)
             # str_len stays the same because that's the length in bytes
@@ -325,12 +405,12 @@
     def unser_array(self, buf, pos):
         arr_len, pos = self.unser_int(buf, pos + 1)
         arr = []
-        for i in range(arr_len):
+        for _ in range(arr_len):
             arr_item, pos = self.loads_recursive(buf, pos)
             arr.append(arr_item)
 
         if not self.mutable:
-          arr = tuple(arr)
+            arr = tuple(arr)
 
         return arr, pos
 
@@ -342,7 +422,7 @@
             keys = []
             vals = []
 
-        for i in range(obj_len):
+        for _ in range(obj_len):
             key, pos = self.unser_utf8_string(buf, pos)
             val, pos = self.loads_recursive(buf, pos)
             if self.mutable:
@@ -359,13 +439,13 @@
     def unser_template(self, buf, pos):
         val_type = _buf_pos(buf, pos + 1)
         if val_type != BSER_ARRAY:
-            raise RuntimeError('Expect ARRAY to follow TEMPLATE')
+            raise RuntimeError("Expect ARRAY to follow TEMPLATE")
         # force UTF-8 on keys
-        keys_bunser = Bunser(mutable=self.mutable, value_encoding='utf-8')
+        keys_bunser = Bunser(mutable=self.mutable, value_encoding="utf-8")
         keys, pos = keys_bunser.unser_array(buf, pos + 1)
         nitems, pos = self.unser_int(buf, pos)
         arr = []
-        for i in range(nitems):
+        for _ in range(nitems):
             if self.mutable:
                 obj = {}
             else:
@@ -392,11 +472,15 @@
 
     def loads_recursive(self, buf, pos):
         val_type = _buf_pos(buf, pos)
-        if (val_type == BSER_INT8 or val_type == BSER_INT16 or
-            val_type == BSER_INT32 or val_type == BSER_INT64):
+        if (
+            val_type == BSER_INT8
+            or val_type == BSER_INT16
+            or val_type == BSER_INT32
+            or val_type == BSER_INT64
+        ):
             return self.unser_int(buf, pos)
         elif val_type == BSER_REAL:
-            val = struct.unpack_from(b'=d', buf, pos + 1)[0]
+            val = struct.unpack_from(b"=d", buf, pos + 1)[0]
             return (val, pos + 9)
         elif val_type == BSER_TRUE:
             return (True, pos + 1)
@@ -415,23 +499,26 @@
         elif val_type == BSER_TEMPLATE:
             return self.unser_template(buf, pos)
         else:
-            raise ValueError('unhandled bser opcode 0x%s' %
-                             binascii.hexlify(val_type).decode('ascii'))
+            raise ValueError(
+                "unhandled bser opcode 0x%s"
+                % binascii.hexlify(val_type).decode("ascii")
+            )
 
 
 def _pdu_info_helper(buf):
+    bser_version = -1
     if buf[0:2] == EMPTY_HEADER[0:2]:
         bser_version = 1
         bser_capabilities = 0
         expected_len, pos2 = Bunser.unser_int(buf, 2)
     elif buf[0:2] == EMPTY_HEADER_V2[0:2]:
         if len(buf) < 8:
-            raise ValueError('Invalid BSER header')
+            raise ValueError("Invalid BSER header")
         bser_version = 2
         bser_capabilities = struct.unpack_from("I", buf, 2)[0]
         expected_len, pos2 = Bunser.unser_int(buf, 6)
     else:
-        raise ValueError('Invalid BSER header')
+        raise ValueError("Invalid BSER header")
 
     return bser_version, bser_capabilities, expected_len, pos2
 
@@ -470,14 +557,20 @@
     pos = info[3]
 
     if len(buf) != expected_len + pos:
-        raise ValueError('bser data len != header len')
+        raise ValueError(
+            "bser data len %d != header len %d" % (expected_len + pos, len(buf))
+        )
 
-    bunser = Bunser(mutable=mutable, value_encoding=value_encoding,
-                    value_errors=value_errors)
+    bunser = Bunser(
+        mutable=mutable,
+        value_encoding=value_encoding,
+        value_errors=value_errors,
+    )
 
     return bunser.loads_recursive(buf, pos)[0]
 
 
 def load(fp, mutable=True, value_encoding=None, value_errors=None):
     from . import load
+
     return load.load(fp, mutable, value_encoding, value_errors)
--- a/hgext/fsmonitor/state.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/hgext/fsmonitor/state.py	Tue Nov 05 13:19:24 2019 -0800
@@ -14,6 +14,7 @@
 
 from mercurial.i18n import _
 from mercurial import (
+    encoding,
     pathutil,
     util,
 )
@@ -81,7 +82,7 @@
                 self.invalidate()
                 return None, None, None
             diskhostname = state[0]
-            hostname = socket.gethostname()
+            hostname = encoding.strtolocal(socket.gethostname())
             if diskhostname != hostname:
                 # file got moved to a different host
                 self._ui.log(
@@ -127,7 +128,7 @@
 
         with file:
             file.write(struct.pack(_versionformat, _version))
-            file.write(socket.gethostname() + b'\0')
+            file.write(encoding.strtolocal(socket.gethostname()) + b'\0')
             file.write(clock + b'\0')
             file.write(ignorehash + b'\0')
             if notefiles:
--- a/hgext/fsmonitor/watchmanclient.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/hgext/fsmonitor/watchmanclient.py	Tue Nov 05 13:19:24 2019 -0800
@@ -9,7 +9,14 @@
 
 import getpass
 
-from mercurial import util
+from mercurial import (
+    encoding,
+    util,
+)
+from mercurial.utils import (
+    procutil,
+    stringutil,
+)
 
 from . import pywatchman
 
@@ -22,12 +29,14 @@
             self.warn = False
         self.invalidate = invalidate
 
-    def __str__(self):
+    def __bytes__(self):
         if self.warn:
             return b'warning: Watchman unavailable: %s' % self.msg
         else:
             return b'Watchman unavailable: %s' % self.msg
 
+    __str__ = encoding.strmethod(__bytes__)
+
 
 class WatchmanNoRoot(Unavailable):
     def __init__(self, root, msg):
@@ -92,15 +101,17 @@
                 self._watchmanclient = pywatchman.client(
                     timeout=self._timeout,
                     useImmutableBser=True,
-                    watchman_exe=watchman_exe,
+                    binpath=procutil.tonativestr(watchman_exe),
                 )
             return self._watchmanclient.query(*watchmanargs)
         except pywatchman.CommandError as ex:
             if b'unable to resolve root' in ex.msg:
-                raise WatchmanNoRoot(self._root, ex.msg)
+                raise WatchmanNoRoot(
+                    self._root, stringutil.forcebytestr(ex.msg)
+                )
             raise Unavailable(ex.msg)
         except pywatchman.WatchmanError as ex:
-            raise Unavailable(str(ex))
+            raise Unavailable(stringutil.forcebytestr(ex))
 
     def command(self, *args):
         try:
--- a/hgext/gpg.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/hgext/gpg.py	Tue Nov 05 13:19:24 2019 -0800
@@ -76,10 +76,9 @@
             fp = os.fdopen(fd, r'wb')
             fp.write(data)
             fp.close()
-            gpgcmd = b"%s --logger-fd 1 --status-fd 1 --verify \"%s\" \"%s\"" % (
-                self.path,
-                sigfile,
-                datafile,
+            gpgcmd = (
+                b"%s --logger-fd 1 --status-fd 1 --verify \"%s\" \"%s\""
+                % (self.path, sigfile, datafile,)
             )
             ret = procutil.filter(b"", gpgcmd)
         finally:
--- a/hgext/histedit.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/hgext/histedit.py	Tue Nov 05 13:19:24 2019 -0800
@@ -217,6 +217,7 @@
     copies,
     destutil,
     discovery,
+    encoding,
     error,
     exchange,
     extensions,
@@ -1117,7 +1118,7 @@
         self.pos = pos
         self.conflicts = []
 
-    def __str__(self):
+    def __bytes__(self):
         # Some actions ('fold' and 'roll') combine a patch with a previous one.
         # Add a marker showing which patch they apply to, and also omit the
         # description for 'roll' (since it will get discarded). Example display:
@@ -1135,10 +1136,16 @@
         desc = self.ctx.description().splitlines()[0].strip()
         if self.action == b'roll':
             desc = b''
-        return b"#{0:<2} {1:<6} {2}:{3}   {4}".format(
-            self.origpos, action, r, h, desc
+        return b"#%s %s %d:%s   %s" % (
+            (b'%d' % self.origpos).ljust(2),
+            action.ljust(6),
+            r,
+            h,
+            desc,
         )
 
+    __str__ = encoding.strmethod(__bytes__)
+
     def checkconflicts(self, other):
         if other.pos > self.pos and other.origpos <= self.origpos:
             if set(other.ctx.files()) & set(self.ctx.files()) != set():
@@ -1315,7 +1322,7 @@
     our list of rules"""
     commands = []
     for rules in rules:
-        commands.append(b"{0} {1}\n".format(rules.action, rules.ctx))
+        commands.append(b'%s %s\n' % (rules.action, rules.ctx))
     return commands
 
 
@@ -1324,7 +1331,7 @@
     whitespace characters, so that the color appears on the whole line"""
     maxy, maxx = win.getmaxyx()
     length = maxx - 1 - x
-    line = (b"{0:<%d}" % length).format(str(line).strip())[:length]
+    line = bytes(line).ljust(length)[:length]
     if y < 0:
         y = maxy + y
     if x < 0:
@@ -1395,17 +1402,17 @@
         maxy, maxx = win.getmaxyx()
         length = maxx - 3
 
-        line = b"changeset: {0}:{1:<12}".format(ctx.rev(), ctx)
+        line = b"changeset: %d:%s" % (ctx.rev(), ctx.hex()[:12])
         win.addstr(1, 1, line[:length])
 
-        line = b"user:      {0}".format(ctx.user())
+        line = b"user:      %s" % ctx.user()
         win.addstr(2, 1, line[:length])
 
         bms = repo.nodebookmarks(ctx.node())
-        line = b"bookmark:  {0}".format(b' '.join(bms))
+        line = b"bookmark:  %s" % b' '.join(bms)
         win.addstr(3, 1, line[:length])
 
-        line = b"summary:   {0}".format(ctx.description().splitlines()[0])
+        line = b"summary:   %s" % (ctx.description().splitlines()[0])
         win.addstr(4, 1, line[:length])
 
         line = b"files:     "
@@ -1425,8 +1432,8 @@
 
         conflicts = rule.conflicts
         if len(conflicts) > 0:
-            conflictstr = b','.join(map(lambda r: str(r.ctx), conflicts))
-            conflictstr = b"changed files overlap with {0}".format(conflictstr)
+            conflictstr = b','.join(map(lambda r: r.ctx.hex()[:12], conflicts))
+            conflictstr = b"changed files overlap with %s" % conflictstr
         else:
             conflictstr = b'no overlap'
 
@@ -1464,7 +1471,9 @@
 
         conflicts = [r.ctx for r in rules if r.conflicts]
         if len(conflicts) > 0:
-            line = b"potential conflict in %s" % b','.join(map(str, conflicts))
+            line = b"potential conflict in %s" % b','.join(
+                map(pycompat.bytestr, conflicts)
+            )
             addln(rulesscr, -1, 0, line, curses.color_pair(COLOR_WARN))
 
         for y, rule in enumerate(rules[start:]):
@@ -1601,7 +1610,7 @@
                 renderhelp(helpwin, state)
                 curses.doupdate()
                 # done rendering
-                ch = stdscr.getkey()
+                ch = encoding.strtolocal(stdscr.getkey())
         except curses.error:
             pass
 
@@ -1675,11 +1684,10 @@
         if type(rc) is list:
             ui.status(_(b"performing changes\n"))
             rules = makecommands(rc)
-            filename = repo.vfs.join(b'chistedit')
-            with open(filename, b'w+') as fp:
+            with repo.vfs(b'chistedit', b'w+') as fp:
                 for r in rules:
                     fp.write(r)
-            opts[b'commands'] = filename
+                opts['commands'] = fp.name
             return _texthistedit(ui, repo, *freeargs, **opts)
     except KeyboardInterrupt:
         pass
--- a/hgext/infinitepush/__init__.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/hgext/infinitepush/__init__.py	Tue Nov 05 13:19:24 2019 -0800
@@ -959,7 +959,7 @@
             service,
             eventtype=b'failure',
             elapsedms=(time.time() - start) * 1000,
-            errormsg=str(e),
+            errormsg=stringutil.forcebytestr(e),
             **kwargs
         )
         raise
@@ -1223,7 +1223,7 @@
             scratchbranchparttype,
             eventtype=b'failure',
             elapsedms=(time.time() - parthandlerstart) * 1000,
-            errormsg=str(e),
+            errormsg=stringutil.forcebytestr(e),
         )
         raise
     finally:
--- a/hgext/lfs/blobstore.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/hgext/lfs/blobstore.py	Tue Nov 05 13:19:24 2019 -0800
@@ -363,7 +363,7 @@
                 _(b'LFS error: %s') % _urlerrorreason(ex), hint=hint
             )
         try:
-            response = json.loads(rawjson)
+            response = pycompat.json_loads(rawjson)
         except ValueError:
             raise LfsRemoteError(
                 _(b'LFS server returns invalid JSON: %s')
--- a/hgext/lfs/wireprotolfsserver.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/hgext/lfs/wireprotolfsserver.py	Tue Nov 05 13:19:24 2019 -0800
@@ -133,7 +133,7 @@
         return True
 
     # XXX: specify an encoding?
-    lfsreq = json.loads(req.bodyfh.read())
+    lfsreq = pycompat.json_loads(req.bodyfh.read())
 
     # If no transfer handlers are explicitly requested, 'basic' is assumed.
     if r'basic' not in lfsreq.get(r'transfers', [r'basic']):
--- a/hgext/notify.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/hgext/notify.py	Tue Nov 05 13:19:24 2019 -0800
@@ -148,7 +148,7 @@
 from __future__ import absolute_import
 
 import email.errors as emailerrors
-import email.parser as emailparser
+import email.utils as emailutils
 import fnmatch
 import hashlib
 import socket
@@ -382,9 +382,8 @@
             )
             return
 
-        p = emailparser.Parser()
         try:
-            msg = p.parsestr(encoding.strfromlocal(data))
+            msg = mail.parsebytes(data)
         except emailerrors.MessageParseError as inst:
             raise error.Abort(inst)
 
@@ -392,16 +391,16 @@
         sender = msg[r'From']
         subject = msg[r'Subject']
         if sender is not None:
-            sender = encoding.strtolocal(sender)
+            sender = mail.headdecode(sender)
         if subject is not None:
-            subject = encoding.strtolocal(subject)
+            subject = mail.headdecode(subject)
         del msg[r'From'], msg[r'Subject']
 
         if not msg.is_multipart():
             # create fresh mime message from scratch
             # (multipart templates must take care of this themselves)
             headers = msg.items()
-            payload = msg.get_payload()
+            payload = msg.get_payload(decode=pycompat.ispy3)
             # for notification prefer readability over data precision
             msg = mail.mimeencode(self.ui, payload, self.charsets, self.test)
             # reinstate custom headers
@@ -440,7 +439,7 @@
             msg[r'Message-Id'] = messageid(ctx, self.domain, self.messageidseed)
         msg[r'To'] = encoding.strfromlocal(b', '.join(sorted(subs)))
 
-        msgtext = encoding.strtolocal(msg.as_string())
+        msgtext = msg.as_bytes() if pycompat.ispy3 else msg.as_string()
         if self.test:
             self.ui.write(msgtext)
             if not msgtext.endswith(b'\n'):
@@ -452,7 +451,7 @@
             )
             mail.sendmail(
                 self.ui,
-                stringutil.email(msg[r'From']),
+                emailutils.parseaddr(msg[r'From'])[1],
                 subs,
                 msgtext,
                 mbox=self.mbox,
--- a/hgext/patchbomb.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/hgext/patchbomb.py	Tue Nov 05 13:19:24 2019 -0800
@@ -960,7 +960,10 @@
                     hdr = pycompat.strurl(hdr)
                     change = True
                 if isinstance(val, bytes):
-                    val = pycompat.strurl(val)
+                    # header value should be ASCII since it's encoded by
+                    # mail.headencode(), but -n/--test disables it and raw
+                    # value of platform encoding is stored.
+                    val = encoding.strfromlocal(val)
                     if not change:
                         # prevent duplicate headers
                         del m[hdr]
--- a/hgext/phabricator.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/hgext/phabricator.py	Tue Nov 05 13:19:24 2019 -0800
@@ -152,8 +152,8 @@
             value = r1params[key][0]
             # we want to compare json payloads without worrying about ordering
             if value.startswith(b'{') and value.endswith(b'}'):
-                r1json = json.loads(value)
-                r2json = json.loads(r2params[key][0])
+                r1json = pycompat.json_loads(value)
+                r2json = pycompat.json_loads(r2params[key][0])
                 if r1json != r2json:
                     return False
             elif r2params[key][0] != value:
@@ -307,7 +307,7 @@
         if isinstance(x, pycompat.unicode)
         else x,
         # json.loads only accepts bytes from py3.6+
-        json.loads(encoding.unifromlocal(body)),
+        pycompat.json_loads(encoding.unifromlocal(body)),
     )
     if parsed.get(b'error_code'):
         msg = _(b'Conduit Error (%s): %s') % (
@@ -332,7 +332,7 @@
         lambda x: encoding.unitolocal(x)
         if isinstance(x, pycompat.unicode)
         else x,
-        json.loads(rawparams),
+        pycompat.json_loads(rawparams),
     )
     # json.dumps only accepts unicode strings
     result = pycompat.rapply(
@@ -441,7 +441,7 @@
                 )
                 unfi.ui.warn(
                     _(
-                        b'D%s: local tag removed - does not match '
+                        b'D%d: local tag removed - does not match '
                         b'Differential history\n'
                     )
                     % drev
@@ -1168,7 +1168,7 @@
                         writediffproperties(unfi[newnode], diffmap[old.node()])
                     except util.urlerr.urlerror:
                         ui.warnnoi18n(
-                            b'Failed to update metadata for D%s\n' % drevid
+                            b'Failed to update metadata for D%d\n' % drevid
                         )
                 # Remove local tags since it's no longer necessary
                 tagname = b'D%d' % drevid
@@ -1208,7 +1208,7 @@
         desc = ctx.description().splitlines()[0]
         oldnode, olddiff, drevid = oldmap.get(ctx.node(), (None, None, None))
         if drevid:
-            drevdesc = ui.label(b'D%s' % drevid, b'phabricator.drev')
+            drevdesc = ui.label(b'D%d' % drevid, b'phabricator.drev')
         else:
             drevdesc = ui.label(_(b'NEW'), b'phabricator.drev')
 
@@ -1613,7 +1613,7 @@
 
     actions = []
     for f in flags:
-        actions.append({b'type': f, b'value': b'true'})
+        actions.append({b'type': f, b'value': True})
 
     drevs = querydrev(repo, spec)
     for i, drev in enumerate(drevs):
--- a/mercurial/commands.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/mercurial/commands.py	Tue Nov 05 13:19:24 2019 -0800
@@ -2267,7 +2267,13 @@
             fm.write(b'value', b'%s\n', value)
         else:
             fm.write(b'name value', b'%s=%s\n', entryname, value)
-        fm.data(defaultvalue=defaultvalue)
+        if formatter.isprintable(defaultvalue):
+            fm.data(defaultvalue=defaultvalue)
+        elif isinstance(defaultvalue, list) and all(
+            formatter.isprintable(e) for e in defaultvalue
+        ):
+            fm.data(defaultvalue=fm.formatlist(defaultvalue, name=b'value'))
+        # TODO: no idea how to process unsupported defaultvalue types
         matched = True
     fm.end()
     if matched:
--- a/mercurial/config.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/mercurial/config.py	Tue Nov 05 13:19:24 2019 -0800
@@ -212,11 +212,9 @@
     def read(self, path, fp=None, sections=None, remap=None):
         if not fp:
             fp = util.posixfile(path, b'rb')
-        assert (
-            getattr(fp, 'mode', r'rb') == r'rb'
-        ), b'config files must be opened in binary mode, got fp=%r mode=%r' % (
-            fp,
-            fp.mode,
+        assert getattr(fp, 'mode', r'rb') == r'rb', (
+            b'config files must be opened in binary mode, got fp=%r mode=%r'
+            % (fp, fp.mode,)
         )
         self.parse(
             path, fp.read(), sections=sections, remap=remap, include=self.read
--- a/mercurial/context.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/mercurial/context.py	Tue Nov 05 13:19:24 2019 -0800
@@ -1187,10 +1187,9 @@
 
         assert (
             changeid is not None or fileid is not None or changectx is not None
-        ), b"bad args: changeid=%r, fileid=%r, changectx=%r" % (
-            changeid,
-            fileid,
-            changectx,
+        ), (
+            b"bad args: changeid=%r, fileid=%r, changectx=%r"
+            % (changeid, fileid, changectx,)
         )
 
         if filelog is not None:
--- a/mercurial/crecord.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/mercurial/crecord.py	Tue Nov 05 13:19:24 2019 -0800
@@ -1816,7 +1816,7 @@
             try:
                 patch = self.ui.edit(patch.getvalue(), b"", action=b"diff")
             except error.Abort as exc:
-                self.errorstr = str(exc)
+                self.errorstr = stringutil.forcebytestr(exc)
                 return None
             finally:
                 self.stdscr.clear()
--- a/mercurial/error.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/mercurial/error.py	Tue Nov 05 13:19:24 2019 -0800
@@ -111,6 +111,14 @@
 
     __bytes__ = _tobytes
 
+    if pycompat.ispy3:
+
+        def __str__(self):
+            # the output would be unreadable if the message was translated,
+            # but do not replace it with encoding.strfromlocal(), which
+            # may raise another exception.
+            return pycompat.sysstr(self.__bytes__())
+
 
 class HookLoadError(Abort):
     """raised when loading a hook fails, aborting an operation
--- a/mercurial/formatter.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/mercurial/formatter.py	Tue Nov 05 13:19:24 2019 -0800
@@ -136,6 +136,16 @@
 pickle = util.pickle
 
 
+def isprintable(obj):
+    """Check if the given object can be directly passed in to formatter's
+    write() and data() functions
+
+    Returns False if the object is unsupported or must be pre-processed by
+    formatdate(), formatdict(), or formatlist().
+    """
+    return isinstance(obj, (type(None), bool, int, pycompat.long, float, bytes))
+
+
 class _nullconverter(object):
     '''convert non-primitive data types to be processed by formatter'''
 
@@ -505,6 +515,10 @@
         if part not in self._parts:
             return
         ref = self._parts[part]
+        # None can't be put in the mapping dict since it means <unset>
+        for k, v in item.items():
+            if v is None:
+                item[k] = templateutil.wrappedvalue(v)
         self._out.write(self._t.render(ref, item))
 
     @util.propertycache
--- a/mercurial/localrepo.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/mercurial/localrepo.py	Tue Nov 05 13:19:24 2019 -0800
@@ -1417,7 +1417,7 @@
 
     def _refreshchangelog(self):
         """make sure the in memory changelog match the on-disk one"""
-        if b'changelog' in vars(self) and self.currenttransaction() is None:
+        if 'changelog' in vars(self) and self.currenttransaction() is None:
             del self.changelog
 
     @property
--- a/mercurial/mail.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/mercurial/mail.py	Tue Nov 05 13:19:24 2019 -0800
@@ -342,6 +342,7 @@
         s.decode('ascii')
     except UnicodeDecodeError:
         for ics in (encoding.encoding, encoding.fallbackencoding):
+            ics = pycompat.sysstr(ics)
             try:
                 u = s.decode(ics)
             except UnicodeDecodeError:
@@ -362,13 +363,13 @@
     if not display:
         # split into words?
         s, cs = _encode(ui, s, charsets)
-        return str(email.header.Header(s, cs))
+        return encoding.strtolocal(email.header.Header(s, cs).encode())
     return s
 
 
 def _addressencode(ui, name, addr, charsets=None):
     assert isinstance(addr, bytes)
-    name = headencode(ui, name, charsets)
+    name = encoding.strfromlocal(headencode(ui, name, charsets))
     try:
         acc, dom = addr.split(b'@')
         acc.decode('ascii')
@@ -440,6 +441,10 @@
         finally:
             fp.detach()
 
+    def parsebytes(data):
+        ep = email.parser.BytesParser()
+        return ep.parsebytes(data)
+
 
 else:
 
@@ -449,6 +454,10 @@
         ep = email.parser.Parser()
         return ep.parse(fp)
 
+    def parsebytes(data):
+        ep = email.parser.Parser()
+        return ep.parsestr(data)
+
 
 def headdecode(s):
     '''Decodes RFC-2047 header'''
@@ -458,7 +467,7 @@
             try:
                 uparts.append(part.decode(charset))
                 continue
-            except UnicodeDecodeError:
+            except (UnicodeDecodeError, LookupError):
                 pass
         # On Python 3, decode_header() may return either bytes or unicode
         # depending on whether the header has =?<charset>? or not
--- a/mercurial/manifest.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/mercurial/manifest.py	Tue Nov 05 13:19:24 2019 -0800
@@ -867,12 +867,15 @@
         return not self._dirs or all(m._isempty() for m in self._dirs.values())
 
     def __repr__(self):
-        return b'<treemanifest dir=%s, node=%s, loaded=%s, dirty=%s at 0x%x>' % (
-            self._dir,
-            hex(self._node),
-            bool(self._loadfunc is _noop),
-            self._dirty,
-            id(self),
+        return (
+            b'<treemanifest dir=%s, node=%s, loaded=%s, dirty=%s at 0x%x>'
+            % (
+                self._dir,
+                hex(self._node),
+                bool(self._loadfunc is _noop),
+                self._dirty,
+                id(self),
+            )
         )
 
     def dir(self):
--- a/mercurial/pycompat.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/mercurial/pycompat.py	Tue Nov 05 13:19:24 2019 -0800
@@ -12,6 +12,7 @@
 
 import getopt
 import inspect
+import json
 import os
 import shlex
 import sys
@@ -88,6 +89,7 @@
 
 if ispy3:
     import builtins
+    import codecs
     import functools
     import io
     import struct
@@ -347,6 +349,48 @@
     iteritems = lambda x: x.items()
     itervalues = lambda x: x.values()
 
+    # Python 3.5's json.load and json.loads require str. We polyfill its
+    # code for detecting encoding from bytes.
+    if sys.version_info[0:2] < (3, 6):
+
+        def _detect_encoding(b):
+            bstartswith = b.startswith
+            if bstartswith((codecs.BOM_UTF32_BE, codecs.BOM_UTF32_LE)):
+                return 'utf-32'
+            if bstartswith((codecs.BOM_UTF16_BE, codecs.BOM_UTF16_LE)):
+                return 'utf-16'
+            if bstartswith(codecs.BOM_UTF8):
+                return 'utf-8-sig'
+
+            if len(b) >= 4:
+                if not b[0]:
+                    # 00 00 -- -- - utf-32-be
+                    # 00 XX -- -- - utf-16-be
+                    return 'utf-16-be' if b[1] else 'utf-32-be'
+                if not b[1]:
+                    # XX 00 00 00 - utf-32-le
+                    # XX 00 00 XX - utf-16-le
+                    # XX 00 XX -- - utf-16-le
+                    return 'utf-16-le' if b[2] or b[3] else 'utf-32-le'
+            elif len(b) == 2:
+                if not b[0]:
+                    # 00 XX - utf-16-be
+                    return 'utf-16-be'
+                if not b[1]:
+                    # XX 00 - utf-16-le
+                    return 'utf-16-le'
+            # default
+            return 'utf-8'
+
+        def json_loads(s, *args, **kwargs):
+            if isinstance(s, (bytes, bytearray)):
+                s = s.decode(_detect_encoding(s), 'surrogatepass')
+
+            return json.loads(s, *args, **kwargs)
+
+    else:
+        json_loads = json.loads
+
 else:
     import cStringIO
 
@@ -424,6 +468,7 @@
     getargspec = inspect.getargspec
     iteritems = lambda x: x.iteritems()
     itervalues = lambda x: x.itervalues()
+    json_loads = json.loads
 
 isjython = sysplatform.startswith(b'java')
 
--- a/mercurial/scmutil.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/mercurial/scmutil.py	Tue Nov 05 13:19:24 2019 -0800
@@ -1854,7 +1854,7 @@
                 raise error.CorruptedState(e % self.firstlinekey)
             d.update(updatedict)
         except ValueError as e:
-            raise error.CorruptedState(str(e))
+            raise error.CorruptedState(stringutil.forcebytestr(e))
         return d
 
     def write(self, data, firstline=None):
--- a/mercurial/templatefilters.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/mercurial/templatefilters.py	Tue Nov 05 13:19:24 2019 -0800
@@ -31,9 +31,6 @@
 urlerr = util.urlerr
 urlreq = util.urlreq
 
-if pycompat.ispy3:
-    long = int
-
 # filters are callables like:
 #   fn(obj)
 # with:
@@ -329,7 +326,7 @@
         return b'false'
     elif obj is True:
         return b'true'
-    elif isinstance(obj, (int, long, float)):
+    elif isinstance(obj, (int, pycompat.long, float)):
         return pycompat.bytestr(obj)
     elif isinstance(obj, bytes):
         return b'"%s"' % encoding.jsonescape(obj, paranoid=paranoid)
--- a/mercurial/ui.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/mercurial/ui.py	Tue Nov 05 13:19:24 2019 -0800
@@ -1564,6 +1564,7 @@
         # - http://bugs.python.org/issue12833
         with self.timeblockedsection(b'stdio'):
             if usereadline:
+                self.flush()
                 prompt = encoding.strfromlocal(prompt)
                 line = encoding.strtolocal(pycompat.rawinput(prompt))
                 # When stdin is in binary mode on Windows, it can cause
--- a/setup.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/setup.py	Tue Nov 05 13:19:24 2019 -0800
@@ -6,32 +6,27 @@
 
 import os
 
-supportedpy = '~= 2.7'
-if os.environ.get('HGALLOWPYTHON3', ''):
-    # Mercurial will never work on Python 3 before 3.5 due to a lack
-    # of % formatting on bytestrings, and can't work on 3.6.0 or 3.6.1
-    # due to a bug in % formatting in bytestrings.
-    # We cannot support Python 3.5.0, 3.5.1, 3.5.2 because of bug in
-    # codecs.escape_encode() where it raises SystemError on empty bytestring
-    # bug link: https://bugs.python.org/issue25270
-    #
-    # TODO: when we actually work on Python 3, use this string as the
-    # actual supportedpy string.
-    supportedpy = ','.join(
-        [
-            '>=2.7',
-            '!=3.0.*',
-            '!=3.1.*',
-            '!=3.2.*',
-            '!=3.3.*',
-            '!=3.4.*',
-            '!=3.5.0',
-            '!=3.5.1',
-            '!=3.5.2',
-            '!=3.6.0',
-            '!=3.6.1',
-        ]
-    )
+# Mercurial will never work on Python 3 before 3.5 due to a lack
+# of % formatting on bytestrings, and can't work on 3.6.0 or 3.6.1
+# due to a bug in % formatting in bytestrings.
+# We cannot support Python 3.5.0, 3.5.1, 3.5.2 because of bug in
+# codecs.escape_encode() where it raises SystemError on empty bytestring
+# bug link: https://bugs.python.org/issue25270
+supportedpy = ','.join(
+    [
+        '>=2.7',
+        '!=3.0.*',
+        '!=3.1.*',
+        '!=3.2.*',
+        '!=3.3.*',
+        '!=3.4.*',
+        '!=3.5.0',
+        '!=3.5.1',
+        '!=3.5.2',
+        '!=3.6.0',
+        '!=3.6.1',
+    ]
+)
 
 import sys, platform
 import sysconfig
@@ -89,39 +84,6 @@
     printf(error, file=sys.stderr)
     sys.exit(1)
 
-# We don't yet officially support Python 3. But we want to allow developers to
-# hack on. Detect and disallow running on Python 3 by default. But provide a
-# backdoor to enable working on Python 3.
-if sys.version_info[0] != 2:
-    badpython = True
-
-    # Allow Python 3 from source checkouts.
-    if os.path.isdir('.hg') or 'HGPYTHON3' in os.environ:
-        badpython = False
-
-    if badpython:
-        error = """
-Python {py} detected.
-
-Mercurial currently has beta support for Python 3 and use of Python 2.7 is
-recommended for the best experience.
-
-Please re-run with Python 2.7 for a faster, less buggy experience.
-
-If you would like to beta test Mercurial with Python 3, this error can
-be suppressed by defining the HGPYTHON3 environment variable when invoking
-this command. No special environment variables or configuration changes are
-necessary to run `hg` with Python 3.
-
-See https://www.mercurial-scm.org/wiki/Python3 for more on Mercurial's
-Python 3 support.
-""".format(
-            py='.'.join('%d' % x for x in sys.version_info[0:2])
-        )
-
-        printf(error, file=sys.stderr)
-        sys.exit(1)
-
 if sys.version_info[0] >= 3:
     DYLIB_SUFFIX = sysconfig.get_config_vars()['EXT_SUFFIX']
 else:
--- a/tests/get-with-headers.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/tests/get-with-headers.py	Tue Nov 05 13:19:24 2019 -0800
@@ -98,7 +98,7 @@
         if formatjson:
             # json.dumps() will print trailing newlines. Eliminate them
             # to make tests easier to write.
-            data = json.loads(data)
+            data = pycompat.json_loads(data)
             lines = json.dumps(data, sort_keys=True, indent=2).splitlines()
             for line in lines:
                 bodyfh.write(pycompat.sysbytes(line.rstrip()))
--- a/tests/hghave.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/tests/hghave.py	Tue Nov 05 13:19:24 2019 -0800
@@ -1,5 +1,6 @@
 from __future__ import absolute_import, print_function
 
+import distutils.version
 import os
 import re
 import socket
@@ -828,6 +829,17 @@
     return os.path.exists('/dev/full')
 
 
+@check("ensurepip", "ensurepip module")
+def has_ensurepip():
+    try:
+        import ensurepip
+
+        ensurepip.bootstrap
+        return True
+    except ImportError:
+        return False
+
+
 @check("virtualenv", "Python virtualenv support")
 def has_virtualenv():
     try:
@@ -980,12 +992,10 @@
     return matchoutput('emacs --version', b'GNU Emacs 2(4.4|4.5|5|6|7|8|9)')
 
 
-# @check('black', 'the black formatter for python')
-@check('grey', 'grey, the fork of the black formatter for python')
+@check('black', 'the black formatter for python')
 def has_black():
-    # use that to actual black as soon as possible
-    # blackcmd = 'black --version'
-    blackcmd = 'python3 $RUNTESTDIR/../contrib/grey.py --version'
-    # version_regex = b'black, version \d'
-    version_regex = b'grey.py, version \d'
-    return matchoutput(blackcmd, version_regex)
+    blackcmd = 'black --version'
+    version_regex = b'black, version ([0-9a-b.]+)'
+    version = matchoutput(blackcmd, version_regex)
+    sv = distutils.version.StrictVersion
+    return version and sv(_strpath(version.group(1))) >= sv('19.10b0')
--- a/tests/run-tests.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/tests/run-tests.py	Tue Nov 05 13:19:24 2019 -0800
@@ -1504,7 +1504,7 @@
         py3switch = self._py3warnings and b' -3' or b''
         # Quote the python(3) executable for Windows
         cmd = b'"%s"%s "%s"' % (PYTHON, py3switch, self.path)
-        vlog("# Running", cmd)
+        vlog("# Running", cmd.decode("utf-8"))
         normalizenewlines = os.name == 'nt'
         result = self._runcommand(cmd, env, normalizenewlines=normalizenewlines)
         if self._aborted:
@@ -1589,7 +1589,7 @@
                 f.write(l)
 
         cmd = b'%s "%s"' % (self._shell, fname)
-        vlog("# Running", cmd)
+        vlog("# Running", cmd.decode("utf-8"))
 
         exitcode, output = self._runcommand(cmd, env)
 
@@ -1770,7 +1770,9 @@
             if l.startswith(b'#require'):
                 lsplit = l.split()
                 if len(lsplit) < 2 or lsplit[0] != b'#require':
-                    after.setdefault(pos, []).append('  !!! invalid #require\n')
+                    after.setdefault(pos, []).append(
+                        b'  !!! invalid #require\n'
+                    )
                 if not skipping:
                     haveresult, message = self._hghave(lsplit[1:])
                     if not haveresult:
@@ -1780,19 +1782,19 @@
             elif l.startswith(b'#if'):
                 lsplit = l.split()
                 if len(lsplit) < 2 or lsplit[0] != b'#if':
-                    after.setdefault(pos, []).append('  !!! invalid #if\n')
+                    after.setdefault(pos, []).append(b'  !!! invalid #if\n')
                 if skipping is not None:
-                    after.setdefault(pos, []).append('  !!! nested #if\n')
+                    after.setdefault(pos, []).append(b'  !!! nested #if\n')
                 skipping = not self._iftest(lsplit[1:])
                 after.setdefault(pos, []).append(l)
             elif l.startswith(b'#else'):
                 if skipping is None:
-                    after.setdefault(pos, []).append('  !!! missing #if\n')
+                    after.setdefault(pos, []).append(b'  !!! missing #if\n')
                 skipping = not skipping
                 after.setdefault(pos, []).append(l)
             elif l.startswith(b'#endif'):
                 if skipping is None:
-                    after.setdefault(pos, []).append('  !!! missing #if\n')
+                    after.setdefault(pos, []).append(b'  !!! missing #if\n')
                 skipping = None
                 after.setdefault(pos, []).append(l)
             elif skipping:
@@ -1841,7 +1843,7 @@
         if inpython:
             script.append(b'EOF\n')
         if skipping is not None:
-            after.setdefault(pos, []).append('  !!! missing #endif\n')
+            after.setdefault(pos, []).append(b'  !!! missing #endif\n')
         addsalt(n + 1, False)
         # Need to end any current per-command trace
         if activetrace:
@@ -3111,12 +3113,14 @@
                 'extensions.logexceptions=%s' % logexceptions.decode('utf-8')
             )
 
-        vlog("# Using TESTDIR", self._testdir)
-        vlog("# Using RUNTESTDIR", osenvironb[b'RUNTESTDIR'])
-        vlog("# Using HGTMP", self._hgtmp)
+        vlog("# Using TESTDIR", _strpath(self._testdir))
+        vlog("# Using RUNTESTDIR", _strpath(osenvironb[b'RUNTESTDIR']))
+        vlog("# Using HGTMP", _strpath(self._hgtmp))
         vlog("# Using PATH", os.environ["PATH"])
-        vlog("# Using", IMPL_PATH, osenvironb[IMPL_PATH])
-        vlog("# Writing to directory", self._outputdir)
+        vlog(
+            "# Using", _strpath(IMPL_PATH), _strpath(osenvironb[IMPL_PATH]),
+        )
+        vlog("# Writing to directory", _strpath(self._outputdir))
 
         try:
             return self._runtests(testdescs) or 0
@@ -3357,7 +3361,7 @@
         if self.options.keep_tmpdir:
             return
 
-        vlog("# Cleaning up HGTMP", self._hgtmp)
+        vlog("# Cleaning up HGTMP", _strpath(self._hgtmp))
         shutil.rmtree(self._hgtmp, True)
         for f in self._createdfiles:
             try:
@@ -3468,7 +3472,7 @@
         makedirs(self._pythondir)
         makedirs(self._bindir)
 
-        vlog("# Running", cmd)
+        vlog("# Running", cmd.decode("utf-8"))
         if subprocess.call(_strpath(cmd), shell=True) == 0:
             if not self.options.verbose:
                 try:
@@ -3643,13 +3647,11 @@
             if os.name == 'nt' and not p.endswith(b'.exe'):
                 p += b'.exe'
             found = self._findprogram(p)
+            p = p.decode("utf-8")
             if found:
-                vlog("# Found prerequisite", p, "at", found)
+                vlog("# Found prerequisite", p, "at", _strpath(found))
             else:
-                print(
-                    "WARNING: Did not find prerequisite tool: %s "
-                    % p.decode("utf-8")
-                )
+                print("WARNING: Did not find prerequisite tool: %s " % p)
 
 
 def aggregateexceptions(path):
--- a/tests/test-byteify-strings.t	Mon Nov 04 00:16:44 2019 +0100
+++ b/tests/test-byteify-strings.t	Tue Nov 05 13:19:24 2019 -0800
@@ -1,4 +1,4 @@
-#require py3
+#require py37
 
   $ byteify_strings () {
   >   $PYTHON "$TESTDIR/../contrib/byteify-strings.py" "$@"
--- a/tests/test-check-code.t	Mon Nov 04 00:16:44 2019 +0100
+++ b/tests/test-check-code.t	Tue Nov 05 13:19:24 2019 -0800
@@ -21,7 +21,6 @@
   Skipping contrib/automation/hgautomation/try_server.py it has no-che?k-code (glob)
   Skipping contrib/automation/hgautomation/windows.py it has no-che?k-code (glob)
   Skipping contrib/automation/hgautomation/winrm.py it has no-che?k-code (glob)
-  Skipping contrib/grey.py it has no-che?k-code (glob)
   Skipping contrib/packaging/hgpackaging/downloads.py it has no-che?k-code (glob)
   Skipping contrib/packaging/hgpackaging/inno.py it has no-che?k-code (glob)
   Skipping contrib/packaging/hgpackaging/py2exe.py it has no-che?k-code (glob)
--- a/tests/test-check-format.t	Mon Nov 04 00:16:44 2019 +0100
+++ b/tests/test-check-format.t	Tue Nov 05 13:19:24 2019 -0800
@@ -1,7 +1,5 @@
-#require grey
-
-(this should use the actual black as soon as possible)
+#require black
 
   $ cd $RUNTESTDIR/..
-  $ python3 contrib/grey.py --config=black.toml --check --diff `hg files 'set:**.py - hgext/fsmonitor/pywatchman/** - mercurial/thirdparty/** - "contrib/python-zstandard/**" - contrib/grey.py'`
+  $ black --config=black.toml --check --diff `hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"'`
 
--- a/tests/test-check-module-imports.t	Mon Nov 04 00:16:44 2019 +0100
+++ b/tests/test-check-module-imports.t	Tue Nov 05 13:19:24 2019 -0800
@@ -20,7 +20,6 @@
   > -X setup.py \
   > -X contrib/automation/ \
   > -X contrib/debugshell.py \
-  > -X contrib/grey.py \
   > -X contrib/hgweb.fcgi \
   > -X contrib/packaging/hg-docker \
   > -X contrib/packaging/hgpackaging/ \
--- a/tests/test-check-py3-compat.t	Mon Nov 04 00:16:44 2019 +0100
+++ b/tests/test-check-py3-compat.t	Tue Nov 05 13:19:24 2019 -0800
@@ -6,7 +6,6 @@
 #if no-py3
   $ testrepohg files 'set:(**.py)' \
   > -X contrib/automation/ \
-  > -X contrib/grey.py \
   > -X contrib/packaging/hgpackaging/ \
   > -X contrib/packaging/inno/ \
   > -X contrib/packaging/wix/ \
--- a/tests/test-config.t	Mon Nov 04 00:16:44 2019 +0100
+++ b/tests/test-config.t	Tue Nov 05 13:19:24 2019 -0800
@@ -87,6 +87,172 @@
    }
   ]
 
+Test config default of various types:
+
+ {"defaultvalue": ""} for -T'json(defaultvalue)' looks weird, but that's
+ how the templater works. Unknown keywords are evaluated to "".
+
+ dynamicdefault
+
+  $ hg config --config alias.foo= alias -Tjson
+  [
+   {
+    "name": "alias.foo",
+    "source": "--config",
+    "value": ""
+   }
+  ]
+  $ hg config --config alias.foo= alias -T'json(defaultvalue)'
+  [
+   {"defaultvalue": ""}
+  ]
+  $ hg config --config alias.foo= alias -T'{defaultvalue}\n'
+  
+
+ null
+
+  $ hg config --config auth.cookiefile= auth -Tjson
+  [
+   {
+    "defaultvalue": null,
+    "name": "auth.cookiefile",
+    "source": "--config",
+    "value": ""
+   }
+  ]
+  $ hg config --config auth.cookiefile= auth -T'json(defaultvalue)'
+  [
+   {"defaultvalue": null}
+  ]
+  $ hg config --config auth.cookiefile= auth -T'{defaultvalue}\n'
+  
+
+ false
+
+  $ hg config --config commands.commit.post-status= commands -Tjson
+  [
+   {
+    "defaultvalue": false,
+    "name": "commands.commit.post-status",
+    "source": "--config",
+    "value": ""
+   }
+  ]
+  $ hg config --config commands.commit.post-status= commands -T'json(defaultvalue)'
+  [
+   {"defaultvalue": false}
+  ]
+  $ hg config --config commands.commit.post-status= commands -T'{defaultvalue}\n'
+  False
+
+ true
+
+  $ hg config --config format.dotencode= format -Tjson
+  [
+   {
+    "defaultvalue": true,
+    "name": "format.dotencode",
+    "source": "--config",
+    "value": ""
+   }
+  ]
+  $ hg config --config format.dotencode= format -T'json(defaultvalue)'
+  [
+   {"defaultvalue": true}
+  ]
+  $ hg config --config format.dotencode= format -T'{defaultvalue}\n'
+  True
+
+ bytes
+
+  $ hg config --config commands.resolve.mark-check= commands -Tjson
+  [
+   {
+    "defaultvalue": "none",
+    "name": "commands.resolve.mark-check",
+    "source": "--config",
+    "value": ""
+   }
+  ]
+  $ hg config --config commands.resolve.mark-check= commands -T'json(defaultvalue)'
+  [
+   {"defaultvalue": "none"}
+  ]
+  $ hg config --config commands.resolve.mark-check= commands -T'{defaultvalue}\n'
+  none
+
+ empty list
+
+  $ hg config --config commands.show.aliasprefix= commands -Tjson
+  [
+   {
+    "defaultvalue": [],
+    "name": "commands.show.aliasprefix",
+    "source": "--config",
+    "value": ""
+   }
+  ]
+  $ hg config --config commands.show.aliasprefix= commands -T'json(defaultvalue)'
+  [
+   {"defaultvalue": []}
+  ]
+  $ hg config --config commands.show.aliasprefix= commands -T'{defaultvalue}\n'
+  
+
+ nonempty list
+
+  $ hg config --config progress.format= progress -Tjson
+  [
+   {
+    "defaultvalue": ["topic", "bar", "number", "estimate"],
+    "name": "progress.format",
+    "source": "--config",
+    "value": ""
+   }
+  ]
+  $ hg config --config progress.format= progress -T'json(defaultvalue)'
+  [
+   {"defaultvalue": ["topic", "bar", "number", "estimate"]}
+  ]
+  $ hg config --config progress.format= progress -T'{defaultvalue}\n'
+  topic bar number estimate
+
+ int
+
+  $ hg config --config profiling.freq= profiling -Tjson
+  [
+   {
+    "defaultvalue": 1000,
+    "name": "profiling.freq",
+    "source": "--config",
+    "value": ""
+   }
+  ]
+  $ hg config --config profiling.freq= profiling -T'json(defaultvalue)'
+  [
+   {"defaultvalue": 1000}
+  ]
+  $ hg config --config profiling.freq= profiling -T'{defaultvalue}\n'
+  1000
+
+ float
+
+  $ hg config --config profiling.showmax= profiling -Tjson
+  [
+   {
+    "defaultvalue": 0.999,
+    "name": "profiling.showmax",
+    "source": "--config",
+    "value": ""
+   }
+  ]
+  $ hg config --config profiling.showmax= profiling -T'json(defaultvalue)'
+  [
+   {"defaultvalue": 0.999}
+  ]
+  $ hg config --config profiling.showmax= profiling -T'{defaultvalue}\n'
+  0.999
+
 Test empty config source:
 
   $ cat <<EOF > emptysource.py
--- a/tests/test-debian-packages.t	Mon Nov 04 00:16:44 2019 +0100
+++ b/tests/test-debian-packages.t	Tue Nov 05 13:19:24 2019 -0800
@@ -13,18 +13,16 @@
   $ make deb > $OUTPUTDIR/build.log 2>&1
   $ cd $OUTPUTDIR
   $ ls *.deb | grep -v 'dbg'
-  mercurial-common_*.deb (glob)
   mercurial_*.deb (glob)
-main deb should have .so but no .py
+should have .so and .py
   $ dpkg --contents mercurial_*.deb | egrep '(localrepo|parsers)'
-  * ./usr/lib/python2.7/dist-packages/mercurial/cext/parsers*.so (glob)
-mercurial-common should have py but no .so or pyc
-  $ dpkg --contents mercurial-common_*.deb | egrep '(localrepo|parsers.*so)'
-  * ./usr/lib/python2.7/dist-packages/mercurial/localrepo.py (glob)
-zsh completions should be in the common package
-  $ dpkg --contents mercurial-common_*.deb | egrep 'zsh.*[^/]$'
+  * ./usr/lib/python3/dist-packages/mercurial/cext/parsers*.so (glob)
+  * ./usr/lib/python3/dist-packages/mercurial/localrepo.py (glob)
+  * ./usr/lib/python3/dist-packages/mercurial/pure/parsers.py (glob)
+should have zsh completions
+  $ dpkg --contents mercurial_*.deb | egrep 'zsh.*[^/]$'
   * ./usr/share/zsh/vendor-completions/_hg (glob)
-chg should be installed alongside hg, in the 'mercurial' package
+should have chg
   $ dpkg --contents mercurial_*.deb | egrep 'chg$'
   * ./usr/bin/chg (glob)
 chg should come with a man page
--- a/tests/test-flagprocessor.t	Mon Nov 04 00:16:44 2019 +0100
+++ b/tests/test-flagprocessor.t	Tue Nov 05 13:19:24 2019 -0800
@@ -209,12 +209,12 @@
       insertflagprocessor(flag, processor, flagprocessors)
     File "*/mercurial/revlogutils/flagutil.py", line *, in insertflagprocessor (glob)
       raise error.Abort(msg)
-  mercurial.error.Abort: b"cannot register multiple processors on flag '0x8'." (py3 !)
+  mercurial.error.Abort: cannot register multiple processors on flag '0x8'. (py3 !)
   Abort: cannot register multiple processors on flag '0x8'. (no-py3 !)
   *** failed to set up extension duplicate: cannot register multiple processors on flag '0x8'.
   $ hg st 2>&1 | egrep 'cannot register multiple processors|flagprocessorext'
     File "*/tests/flagprocessorext.py", line *, in extsetup (glob)
-  mercurial.error.Abort: b"cannot register multiple processors on flag '0x8'." (py3 !)
+  mercurial.error.Abort: cannot register multiple processors on flag '0x8'. (py3 !)
   Abort: cannot register multiple processors on flag '0x8'. (no-py3 !)
   *** failed to set up extension duplicate: cannot register multiple processors on flag '0x8'.
     File "*/tests/flagprocessorext.py", line *, in b64decode (glob)
--- a/tests/test-highlight.t	Mon Nov 04 00:16:44 2019 +0100
+++ b/tests/test-highlight.t	Tue Nov 05 13:19:24 2019 -0800
@@ -966,7 +966,9 @@
   $ cd ..
   $ hg init eucjp
   $ cd eucjp
-  $ "$PYTHON" -c 'print("\265\376")' >> eucjp.txt  # Japanese kanji "Kyo"
+  >>> with open('eucjp.txt', 'wb') as fh:
+  ...     # Japanese kanji "Kyo"
+  ...     fh.write(u'\265\376'.encode('utf-8')) and None
   $ hg ci -Ama
   adding eucjp.txt
   $ hgserveget () {
--- a/tests/test-hook.t	Mon Nov 04 00:16:44 2019 +0100
+++ b/tests/test-hook.t	Tue Nov 05 13:19:24 2019 -0800
@@ -988,7 +988,7 @@
   ModuleNotFoundError: No module named 'hgext_syntaxerror' (py36 !)
   Traceback (most recent call last): (py3 !)
   HookLoadError: preoutgoing.syntaxerror hook is invalid: import of "syntaxerror" failed (no-py3 !)
-  mercurial.error.HookLoadError: b'preoutgoing.syntaxerror hook is invalid: import of "syntaxerror" failed' (py3 !)
+  mercurial.error.HookLoadError: preoutgoing.syntaxerror hook is invalid: import of "syntaxerror" failed (py3 !)
   abort: preoutgoing.syntaxerror hook is invalid: import of "syntaxerror" failed
 
   $ echo '[hooks]' > ../a/.hg/hgrc
@@ -1161,7 +1161,7 @@
   ModuleNotFoundError: No module named 'hgext_importfail' (py36 !)
   Traceback (most recent call last):
   HookLoadError: precommit.importfail hook is invalid: import of "importfail" failed (no-py3 !)
-  mercurial.error.HookLoadError: b'precommit.importfail hook is invalid: import of "importfail" failed' (py3 !)
+  mercurial.error.HookLoadError: precommit.importfail hook is invalid: import of "importfail" failed (py3 !)
   abort: precommit.importfail hook is invalid: import of "importfail" failed
 
 Issue1827: Hooks Update & Commit not completely post operation
--- a/tests/test-install.t	Mon Nov 04 00:16:44 2019 +0100
+++ b/tests/test-install.t	Tue Nov 05 13:19:24 2019 -0800
@@ -236,25 +236,56 @@
 
 #endif
 
-#if py3
-  $ HGALLOWPYTHON3=1
-  $ export HGALLOWPYTHON3
-#endif
-
-#if virtualenv
-
 Verify that Mercurial is installable with pip. Note that this MUST be
 the last test in this file, because we do some nasty things to the
 shell environment in order to make the virtualenv work reliably.
 
+On Python 3, we use the venv module, which is part of the standard library.
+But some Linux distros strip out this module's functionality involving pip,
+so we have to look for the ensurepip module, which these distros strip out
+completely.
+On Python 2, we use the 3rd party virtualenv module, if available.
+
   $ cd $TESTTMP
+  $ unset PYTHONPATH
+
+#if py3 ensurepip
+  $ "$PYTHON" -m venv installenv >> pip.log
+
+Note: we use this weird path to run pip and hg to avoid platform differences,
+since it's bin on most platforms but Scripts on Windows.
+  $ ./installenv/*/pip install --no-index $TESTDIR/.. >> pip.log
+  $ ./installenv/*/hg debuginstall || cat pip.log
+  checking encoding (ascii)...
+  checking Python executable (*) (glob)
+  checking Python version (3.*) (glob)
+  checking Python lib (*)... (glob)
+  checking Python security support (*) (glob)
+  checking Mercurial version (*) (glob)
+  checking Mercurial custom build (*) (glob)
+  checking module policy (*) (glob)
+  checking installed modules (*/mercurial)... (glob)
+  checking registered compression engines (*) (glob)
+  checking available compression engines (*) (glob)
+  checking available compression engines for wire protocol (*) (glob)
+  checking "re2" regexp engine \((available|missing)\) (re)
+  checking templates ($TESTTMP/installenv/*/site-packages/mercurial/templates)... (glob)
+  checking default template ($TESTTMP/installenv/*/site-packages/mercurial/templates/map-cmdline.default) (glob)
+  checking commit editor... (*) (glob)
+  checking username (test)
+  no problems detected
+#endif
+
+#if no-py3 virtualenv
+
 Note: --no-site-packages is deprecated, but some places have an
 ancient virtualenv from their linux distro or similar and it's not yet
 the default for them.
-  $ unset PYTHONPATH
+
   $ "$PYTHON" -m virtualenv --no-site-packages --never-download installenv >> pip.log
   DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. (?)
   DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support (?)
+
 Note: we use this weird path to run pip and hg to avoid platform differences,
 since it's bin on most platforms but Scripts on Windows.
   $ ./installenv/*/pip install --no-index $TESTDIR/.. >> pip.log
@@ -263,8 +294,7 @@
   $ ./installenv/*/hg debuginstall || cat pip.log
   checking encoding (ascii)...
   checking Python executable (*) (glob)
-  checking Python version (2.*) (glob) (no-py3 !)
-  checking Python version (3.*) (glob) (py3 !)
+  checking Python version (2.*) (glob)
   checking Python lib (*)... (glob)
   checking Python security support (*) (glob)
     TLS 1.2 not supported by Python install; network connections lack modern security (?)
--- a/tests/test-keyword.t	Mon Nov 04 00:16:44 2019 +0100
+++ b/tests/test-keyword.t	Tue Nov 05 13:19:24 2019 -0800
@@ -256,7 +256,8 @@
 
 Pull from bundle and trigger notify
 
-  $ hg pull -u ../kw.hg
+  $ hg pull -u ../kw.hg | \
+  >  "$PYTHON" $TESTDIR/unwrap-message-id.py
   pulling from ../kw.hg
   requesting all changes
   adding changesets
--- a/tests/test-lfs-serve-access.t	Mon Nov 04 00:16:44 2019 +0100
+++ b/tests/test-lfs-serve-access.t	Tue Nov 05 13:19:24 2019 -0800
@@ -354,7 +354,8 @@
   $LOCALIP - - [$ERRDATE$] HG error:      localstore.download(oid, req.bodyfh) (glob)
   $LOCALIP - - [$ERRDATE$] HG error:      super(badstore, self).download(oid, src) (glob)
   $LOCALIP - - [$ERRDATE$] HG error:      _(b'corrupt remote lfs object: %s') % oid (glob)
-  $LOCALIP - - [$ERRDATE$] HG error:  LfsCorruptionError: corrupt remote lfs object: b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c (glob)
+  $LOCALIP - - [$ERRDATE$] HG error:  LfsCorruptionError: corrupt remote lfs object: b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c (no-py3 !)
+  $LOCALIP - - [$ERRDATE$] HG error:  hgext.lfs.blobstore.LfsCorruptionError: corrupt remote lfs object: b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c (py3 !)
   $LOCALIP - - [$ERRDATE$] HG error:   (glob)
   $LOCALIP - - [$ERRDATE$] Exception happened during processing request '/.hg/lfs/objects/276f73cfd75f9fb519810df5f5d96d6594ca2521abd86cbcd92122f7d51a1f3d': (glob)
   Traceback (most recent call last):
@@ -376,7 +377,8 @@
   $LOCALIP - - [$ERRDATE$] HG error:      blob = self._read(self.vfs, oid, verify) (glob)
   $LOCALIP - - [$ERRDATE$] HG error:      blobstore._verify(oid, b'dummy content') (glob)
   $LOCALIP - - [$ERRDATE$] HG error:      hint=_(b'run hg verify'), (glob)
-  $LOCALIP - - [$ERRDATE$] HG error:  LfsCorruptionError: detected corrupt lfs object: 276f73cfd75f9fb519810df5f5d96d6594ca2521abd86cbcd92122f7d51a1f3d (glob)
+  $LOCALIP - - [$ERRDATE$] HG error:  LfsCorruptionError: detected corrupt lfs object: 276f73cfd75f9fb519810df5f5d96d6594ca2521abd86cbcd92122f7d51a1f3d (no-py3 !)
+  $LOCALIP - - [$ERRDATE$] HG error:  hgext.lfs.blobstore.LfsCorruptionError: detected corrupt lfs object: 276f73cfd75f9fb519810df5f5d96d6594ca2521abd86cbcd92122f7d51a1f3d (py3 !)
   $LOCALIP - - [$ERRDATE$] HG error:   (glob)
 
 Basic Authorization headers are returned by the Batch API, and sent back with
--- a/tests/test-linerange.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/tests/test-linerange.py	Tue Nov 05 13:19:24 2019 -0800
@@ -2,6 +2,7 @@
 
 import unittest
 from mercurial import error, mdiff
+from mercurial.utils import stringutil
 
 # for readability, line numbers are 0-origin
 text1 = b'''
@@ -228,7 +229,10 @@
             try:
                 mdiff.blocksinrange(self.blocks, linerange2)
             except exctype as exc:
-                self.assertTrue('line range exceeds file size' in str(exc))
+                self.assertTrue(
+                    b'line range exceeds file size'
+                    in stringutil.forcebytestr(exc)
+                )
             else:
                 self.fail('%s not raised' % exctype.__name__)
 
--- a/tests/test-match.py	Mon Nov 04 00:16:44 2019 +0100
+++ b/tests/test-match.py	Tue Nov 05 13:19:24 2019 -0800
@@ -316,7 +316,7 @@
 
     # We're using includematcher instead of patterns because it behaves slightly
     # better (giving narrower results) than patternmatcher.
-    def testVisitdirIncludeIncludfe(self):
+    def testVisitdirIncludeInclude(self):
         m1 = matchmod.match(b'', b'', include=[b'path:dir/subdir'])
         m2 = matchmod.match(b'', b'', include=[b'rootfilesin:dir'])
         dm = matchmod.differencematcher(m1, m2)
@@ -430,7 +430,7 @@
 
     # We're using includematcher instead of patterns because it behaves slightly
     # better (giving narrower results) than patternmatcher.
-    def testVisitdirIncludeIncludfe(self):
+    def testVisitdirIncludeInclude(self):
         m1 = matchmod.match(b'', b'', include=[b'path:dir/subdir'])
         m2 = matchmod.match(b'', b'', include=[b'rootfilesin:dir'])
         im = matchmod.intersectmatchers(m1, m2)
@@ -644,7 +644,7 @@
 
     # We're using includematcher instead of patterns because it behaves slightly
     # better (giving narrower results) than patternmatcher.
-    def testVisitdirIncludeIncludfe(self):
+    def testVisitdirIncludeInclude(self):
         m1 = matchmod.match(b'', b'', include=[b'path:dir/subdir'])
         m2 = matchmod.match(b'', b'', include=[b'rootfilesin:dir'])
         um = matchmod.unionmatcher([m1, m2])
--- a/tests/test-notify-changegroup.t	Mon Nov 04 00:16:44 2019 +0100
+++ b/tests/test-notify-changegroup.t	Tue Nov 05 13:19:24 2019 -0800
@@ -39,6 +39,7 @@
 push
 
   $ hg --traceback --cwd b push ../a 2>&1 |
+  >     "$PYTHON" $TESTDIR/unwrap-message-id.py | \
   >     "$PYTHON" -c 'from __future__ import print_function ; import sys,re; print(re.sub("\n\t", " ", sys.stdin.read()), end="")'
   pushing to ../a
   searching for changes
@@ -93,6 +94,7 @@
 unbundle with correct source
 
   $ hg --config notify.sources=unbundle --cwd a unbundle ../test.hg 2>&1 |
+  >     "$PYTHON" $TESTDIR/unwrap-message-id.py | \
   >     "$PYTHON" -c 'from __future__ import print_function ; import sys,re; print(re.sub("\n\t", " ", sys.stdin.read()), end="")'
   adding changesets
   adding manifests
@@ -169,6 +171,7 @@
 push
 
   $ hg --traceback --cwd b --config notify.fromauthor=True push ../a 2>&1 |
+  >     "$PYTHON" $TESTDIR/unwrap-message-id.py | \
   >     "$PYTHON" -c 'from __future__ import print_function ; import sys,re; print(re.sub("\n\t", " ", sys.stdin.read()), end="")'
   pushing to ../a
   searching for changes
--- a/tests/test-notify.t	Mon Nov 04 00:16:44 2019 +0100
+++ b/tests/test-notify.t	Tue Nov 05 13:19:24 2019 -0800
@@ -196,7 +196,9 @@
 of the very long subject line
 pull (minimal config)
 
-  $ hg --traceback --cwd b --config notify.domain=example.com --config notify.messageidseed=example pull ../a | "$PYTHON" $TESTTMP/filter.py
+  $ hg --traceback --cwd b --config notify.domain=example.com --config notify.messageidseed=example pull ../a | \
+  >  "$PYTHON" $TESTDIR/unwrap-message-id.py | \
+  >  "$PYTHON" $TESTTMP/filter.py
   pulling from ../a
   searching for changes
   adding changesets
@@ -255,7 +257,9 @@
 
   $ hg --cwd b rollback
   repository tip rolled back to revision 0 (undo pull)
-  $ hg --traceback --cwd b pull ../a  | "$PYTHON" $TESTTMP/filter.py
+  $ hg --traceback --cwd b pull ../a | \
+  >  "$PYTHON" $TESTDIR/unwrap-message-id.py | \
+  >  "$PYTHON" $TESTTMP/filter.py
   pulling from ../a
   searching for changes
   adding changesets
@@ -303,7 +307,9 @@
 
   $ hg --cwd b rollback
   repository tip rolled back to revision 0 (undo pull)
-  $ hg --traceback --config notify.maxdiffstat=1 --cwd b pull ../a | "$PYTHON" $TESTTMP/filter.py
+  $ hg --traceback --config notify.maxdiffstat=1 --cwd b pull ../a | \
+  >  "$PYTHON" $TESTDIR/unwrap-message-id.py | \
+  >  "$PYTHON" $TESTTMP/filter.py
   pulling from ../a
   searching for changes
   adding changesets
@@ -354,7 +360,9 @@
   (branch merge, don't forget to commit)
   $ hg ci -m merge -d '3 0'
   $ cd ..
-  $ hg --traceback --cwd b pull ../a | "$PYTHON" $TESTTMP/filter.py
+  $ hg --traceback --cwd b pull ../a | \
+  >  "$PYTHON" $TESTDIR/unwrap-message-id.py | \
+  >  "$PYTHON" $TESTTMP/filter.py
   pulling from ../a
   searching for changes
   adding changesets
@@ -418,8 +426,9 @@
   > EOF
   $ echo a >> a/a
   $ hg --cwd a --encoding utf-8 commit -A -d '0 0' \
-  >   -m `"$PYTHON" -c 'print("\xc3\xa0\xc3\xa1\xc3\xa2\xc3\xa3\xc3\xa4")'`
+  >   -m `"$PYTHON" -c 'import sys; getattr(sys.stdout, "buffer", sys.stdout).write(b"\xc3\xa0\xc3\xa1\xc3\xa2\xc3\xa3\xc3\xa4")'`
   $ hg --traceback --cwd b --encoding utf-8 pull ../a | \
+  >   "$PYTHON" $TESTDIR/unwrap-message-id.py | \
   >   "$PYTHON" $TESTTMP/filter.py
   pulling from ../a
   searching for changes
@@ -433,7 +442,8 @@
   Content-Transfer-Encoding: 8bit
   X-Test: foo
   Date: * (glob)
-  Subject: \xc3\xa0... (esc)
+  Subject: \xc3\xa0... (esc) (no-py3 !)
+  Subject: =?utf-8?b?w6AuLi4=?= (py3 !)
   From: test@test.com
   X-Hg-Notification: changeset 0f25f9c22b4c
   Message-Id: <*> (glob)
@@ -473,7 +483,7 @@
   new changesets a846b5f6ebb7
   notify: sending 2 subscribers 1 changes
   (run 'hg update' to get a working copy)
-  $ "$PYTHON" $TESTTMP/filter.py < b/mbox
+  $ cat b/mbox | "$PYTHON" $TESTDIR/unwrap-message-id.py | "$PYTHON" $TESTTMP/filter.py
   From test@test.com ... ... .. ..:..:.. .... (re)
   MIME-Version: 1.0
   Content-Type: text/plain; charset="*" (glob)
@@ -533,7 +543,9 @@
   (branches are permanent and global, did you want a bookmark?)
   $ echo a >> a/a
   $ hg --cwd a ci -m test -d '1 0'
-  $ hg --traceback --cwd b pull ../a | "$PYTHON" $TESTTMP/filter.py
+  $ hg --traceback --cwd b pull ../a | \
+  >  "$PYTHON" $TESTDIR/unwrap-message-id.py | \
+  >  "$PYTHON" $TESTTMP/filter.py
   pulling from ../a
   searching for changes
   adding changesets
@@ -563,7 +575,9 @@
   1 files updated, 0 files merged, 0 files removed, 0 files unresolved
   $ echo a >> a/a
   $ hg --cwd a ci -m test -d '1 0'
-  $ hg --traceback --cwd b pull ../a | "$PYTHON" $TESTTMP/filter.py
+  $ hg --traceback --cwd b pull ../a | \
+  >  "$PYTHON" $TESTDIR/unwrap-message-id.py | \
+  >  "$PYTHON" $TESTTMP/filter.py
   pulling from ../a
   searching for changes
   adding changesets
@@ -592,7 +606,9 @@
   $ mv "$HGRCPATH.new" $HGRCPATH
   $ echo a >> a/a
   $ hg --cwd a commit -m 'default template'
-  $ hg --cwd b pull ../a -q | "$PYTHON" $TESTTMP/filter.py
+  $ hg --cwd b pull ../a -q | \
+  >  "$PYTHON" $TESTDIR/unwrap-message-id.py | \
+  >  "$PYTHON" $TESTTMP/filter.py
   MIME-Version: 1.0
   Content-Type: text/plain; charset="us-ascii"
   Content-Transfer-Encoding: 7bit
@@ -621,7 +637,9 @@
   > EOF
   $ echo a >> a/a
   $ hg --cwd a commit -m 'with style'
-  $ hg --cwd b pull ../a -q | "$PYTHON" $TESTTMP/filter.py
+  $ hg --cwd b pull ../a -q | \
+  >  "$PYTHON" $TESTDIR/unwrap-message-id.py | \
+  >  "$PYTHON" $TESTTMP/filter.py
   MIME-Version: 1.0
   Content-Type: text/plain; charset="us-ascii"
   Content-Transfer-Encoding: 7bit
@@ -644,7 +662,9 @@
   > EOF
   $ echo a >> a/a
   $ hg --cwd a commit -m 'with template'
-  $ hg --cwd b pull ../a -q | "$PYTHON" $TESTTMP/filter.py
+  $ hg --cwd b pull ../a -q | \
+  >  "$PYTHON" $TESTDIR/unwrap-message-id.py | \
+  >  "$PYTHON" $TESTTMP/filter.py
   MIME-Version: 1.0
   Content-Type: text/plain; charset="us-ascii"
   Content-Transfer-Encoding: 7bit
@@ -675,7 +695,8 @@
   > EOF
   $ hg commit -Am addfunction
   adding f1
-  $ hg --cwd ../b pull ../a
+  $ hg --cwd ../b pull ../a | \
+  >  "$PYTHON" $TESTDIR/unwrap-message-id.py
   pulling from ../a
   searching for changes
   adding changesets
@@ -718,7 +739,8 @@
   > }
   > EOF
   $ hg commit -m changefunction
-  $ hg --cwd ../b --config notify.showfunc=True pull ../a
+  $ hg --cwd ../b --config notify.showfunc=True pull ../a | \
+  >  "$PYTHON" $TESTDIR/unwrap-message-id.py
   pulling from ../a
   searching for changes
   adding changesets
--- a/tests/test-patchbomb.t	Mon Nov 04 00:16:44 2019 +0100
+++ b/tests/test-patchbomb.t	Tue Nov 05 13:19:24 2019 -0800
@@ -445,7 +445,9 @@
 
 utf-8 patch:
   $ "$PYTHON" -c 'fp = open("utf", "wb"); fp.write(b"h\xC3\xB6mma!\n"); fp.close();'
-  $ hg commit -A -d '4 0' -m 'utf-8 content'
+  $ hg commit -A -d '4 0' \
+  >   --encoding "utf-8" \
+  >   -m `"$PYTHON" -c 'import sys; getattr(sys.stdout, "buffer", sys.stdout).write(b"\xc3\xa7a")'`
   adding description
   adding utf
 
@@ -454,16 +456,16 @@
   this patch series consists of 1 patches.
   
   
-  displaying [PATCH] utf-8 content ...
+  displaying [PATCH] ?a ...
   MIME-Version: 1.0
   Content-Type: text/plain; charset="iso-8859-1"
   Content-Transfer-Encoding: quoted-printable
-  Subject: [PATCH] utf-8 content
-  X-Mercurial-Node: 909a00e13e9d78b575aeee23dddbada46d5a143f
+  Subject: [PATCH] ?a
+  X-Mercurial-Node: f81ef97829467e868fc405fccbcfa66217e4d3e6
   X-Mercurial-Series-Index: 1
   X-Mercurial-Series-Total: 1
-  Message-Id: <909a00e13e9d78b575ae.240@test-hostname>
-  X-Mercurial-Series-Id: <909a00e13e9d78b575ae.240@test-hostname>
+  Message-Id: <f81ef97829467e868fc4.240@test-hostname>
+  X-Mercurial-Series-Id: <f81ef97829467e868fc4.240@test-hostname>
   User-Agent: Mercurial-patchbomb/* (glob)
   Date: Thu, 01 Jan 1970 00:04:00 +0000
   From: quux
@@ -474,18 +476,18 @@
   # User test
   # Date 4 0
   #      Thu Jan 01 00:00:04 1970 +0000
-  # Node ID 909a00e13e9d78b575aeee23dddbada46d5a143f
+  # Node ID f81ef97829467e868fc405fccbcfa66217e4d3e6
   # Parent  ff2c9fa2018b15fa74b33363bda9527323e2a99f
-  utf-8 content
-  
-  diff -r ff2c9fa2018b -r 909a00e13e9d description
+  ?a
+  
+  diff -r ff2c9fa2018b -r f81ef9782946 description
   --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   +++ b/description	Thu Jan 01 00:00:04 1970 +0000
   @@ -0,0 +1,3 @@
   +a multiline
   +
   +description
-  diff -r ff2c9fa2018b -r 909a00e13e9d utf
+  diff -r ff2c9fa2018b -r f81ef9782946 utf
   --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   +++ b/utf	Thu Jan 01 00:00:04 1970 +0000
   @@ -0,0 +1,1 @@
@@ -497,35 +499,36 @@
   this patch series consists of 1 patches.
   
   
-  sending [PATCH] utf-8 content ...
+  sending [PATCH] ?a ...
 
   $ cat mbox
   From quux ... ... .. ..:..:.. .... (re)
   MIME-Version: 1.0
   Content-Type: text/plain; charset="utf-8"
   Content-Transfer-Encoding: base64
-  Subject: [PATCH] utf-8 content
-  X-Mercurial-Node: 909a00e13e9d78b575aeee23dddbada46d5a143f
+  Subject: [PATCH] ?a
+  X-Mercurial-Node: f81ef97829467e868fc405fccbcfa66217e4d3e6
   X-Mercurial-Series-Index: 1
   X-Mercurial-Series-Total: 1
-  Message-Id: <909a00e13e9d78b575ae.240@test-hostname>
-  X-Mercurial-Series-Id: <909a00e13e9d78b575ae.240@test-hostname>
+  Message-Id: <f81ef97829467e868fc4.240@test-hostname>
+  X-Mercurial-Series-Id: <f81ef97829467e868fc4.240@test-hostname>
   User-Agent: Mercurial-patchbomb/* (glob)
   Date: Thu, 01 Jan 1970 00:04:00 +0000
-  From: Q <quux>
+  From: Q <quux> (no-py3 !)
+  From: =?iso-8859-1?q?Q?= <quux> (py3 !)
   To: foo
   Cc: bar
   
   IyBIRyBjaGFuZ2VzZXQgcGF0Y2gKIyBVc2VyIHRlc3QKIyBEYXRlIDQgMAojICAgICAgVGh1IEph
-  biAwMSAwMDowMDowNCAxOTcwICswMDAwCiMgTm9kZSBJRCA5MDlhMDBlMTNlOWQ3OGI1NzVhZWVl
-  MjNkZGRiYWRhNDZkNWExNDNmCiMgUGFyZW50ICBmZjJjOWZhMjAxOGIxNWZhNzRiMzMzNjNiZGE5
-  NTI3MzIzZTJhOTlmCnV0Zi04IGNvbnRlbnQKCmRpZmYgLXIgZmYyYzlmYTIwMThiIC1yIDkwOWEw
-  MGUxM2U5ZCBkZXNjcmlwdGlvbgotLS0gL2Rldi9udWxsCVRodSBKYW4gMDEgMDA6MDA6MDAgMTk3
-  MCArMDAwMAorKysgYi9kZXNjcmlwdGlvbglUaHUgSmFuIDAxIDAwOjAwOjA0IDE5NzAgKzAwMDAK
-  QEAgLTAsMCArMSwzIEBACithIG11bHRpbGluZQorCitkZXNjcmlwdGlvbgpkaWZmIC1yIGZmMmM5
-  ZmEyMDE4YiAtciA5MDlhMDBlMTNlOWQgdXRmCi0tLSAvZGV2L251bGwJVGh1IEphbiAwMSAwMDow
-  MDowMCAxOTcwICswMDAwCisrKyBiL3V0ZglUaHUgSmFuIDAxIDAwOjAwOjA0IDE5NzAgKzAwMDAK
-  QEAgLTAsMCArMSwxIEBACitow7ZtbWEhCg==
+  biAwMSAwMDowMDowNCAxOTcwICswMDAwCiMgTm9kZSBJRCBmODFlZjk3ODI5NDY3ZTg2OGZjNDA1
+  ZmNjYmNmYTY2MjE3ZTRkM2U2CiMgUGFyZW50ICBmZjJjOWZhMjAxOGIxNWZhNzRiMzMzNjNiZGE5
+  NTI3MzIzZTJhOTlmCj9hCgpkaWZmIC1yIGZmMmM5ZmEyMDE4YiAtciBmODFlZjk3ODI5NDYgZGVz
+  Y3JpcHRpb24KLS0tIC9kZXYvbnVsbAlUaHUgSmFuIDAxIDAwOjAwOjAwIDE5NzAgKzAwMDAKKysr
+  IGIvZGVzY3JpcHRpb24JVGh1IEphbiAwMSAwMDowMDowNCAxOTcwICswMDAwCkBAIC0wLDAgKzEs
+  MyBAQAorYSBtdWx0aWxpbmUKKworZGVzY3JpcHRpb24KZGlmZiAtciBmZjJjOWZhMjAxOGIgLXIg
+  ZjgxZWY5NzgyOTQ2IHV0ZgotLS0gL2Rldi9udWxsCVRodSBKYW4gMDEgMDA6MDA6MDAgMTk3MCAr
+  MDAwMAorKysgYi91dGYJVGh1IEphbiAwMSAwMDowMDowNCAxOTcwICswMDAwCkBAIC0wLDAgKzEs
+  MSBAQAoraMO2bW1hIQo=
   
   
   >>> import base64
@@ -540,18 +543,18 @@
   # User test
   # Date 4 0
   #      Thu Jan 01 00:00:04 1970 +0000
-  # Node ID 909a00e13e9d78b575aeee23dddbada46d5a143f
+  # Node ID f81ef97829467e868fc405fccbcfa66217e4d3e6
   # Parent  ff2c9fa2018b15fa74b33363bda9527323e2a99f
-  utf-8 content
-  
-  diff -r ff2c9fa2018b -r 909a00e13e9d description
+  ?a
+  
+  diff -r ff2c9fa2018b -r f81ef9782946 description
   --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   +++ b/description	Thu Jan 01 00:00:04 1970 +0000
   @@ -0,0 +1,3 @@
   +a multiline
   +
   +description
-  diff -r ff2c9fa2018b -r 909a00e13e9d utf
+  diff -r ff2c9fa2018b -r f81ef9782946 utf
   --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   +++ b/utf	Thu Jan 01 00:00:04 1970 +0000
   @@ -0,0 +1,1 @@
@@ -574,11 +577,11 @@
   Content-Type: text/plain; charset="us-ascii"
   Content-Transfer-Encoding: quoted-printable
   Subject: [PATCH] long line
-  X-Mercurial-Node: a2ea8fc83dd8b93cfd86ac97b28287204ab806e1
+  X-Mercurial-Node: 0c7b871cb86b61a1c07e244393603c361e4a178d
   X-Mercurial-Series-Index: 1
   X-Mercurial-Series-Total: 1
-  Message-Id: <a2ea8fc83dd8b93cfd86.240@test-hostname>
-  X-Mercurial-Series-Id: <a2ea8fc83dd8b93cfd86.240@test-hostname>
+  Message-Id: <0c7b871cb86b61a1c07e.240@test-hostname>
+  X-Mercurial-Series-Id: <0c7b871cb86b61a1c07e.240@test-hostname>
   User-Agent: Mercurial-patchbomb/* (glob)
   Date: Thu, 01 Jan 1970 00:04:00 +0000
   From: quux
@@ -589,11 +592,11 @@
   # User test
   # Date 4 0
   #      Thu Jan 01 00:00:04 1970 +0000
-  # Node ID a2ea8fc83dd8b93cfd86ac97b28287204ab806e1
-  # Parent  909a00e13e9d78b575aeee23dddbada46d5a143f
+  # Node ID 0c7b871cb86b61a1c07e244393603c361e4a178d
+  # Parent  f81ef97829467e868fc405fccbcfa66217e4d3e6
   long line
   
-  diff -r 909a00e13e9d -r a2ea8fc83dd8 long
+  diff -r f81ef9782946 -r 0c7b871cb86b long
   --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   +++ b/long	Thu Jan 01 00:00:04 1970 +0000
   @@ -0,0 +1,4 @@
@@ -628,11 +631,11 @@
   Content-Type: text/plain; charset="us-ascii"
   Content-Transfer-Encoding: quoted-printable
   Subject: [PATCH] long line
-  X-Mercurial-Node: a2ea8fc83dd8b93cfd86ac97b28287204ab806e1
+  X-Mercurial-Node: 0c7b871cb86b61a1c07e244393603c361e4a178d
   X-Mercurial-Series-Index: 1
   X-Mercurial-Series-Total: 1
-  Message-Id: <a2ea8fc83dd8b93cfd86.240@test-hostname>
-  X-Mercurial-Series-Id: <a2ea8fc83dd8b93cfd86.240@test-hostname>
+  Message-Id: <0c7b871cb86b61a1c07e.240@test-hostname>
+  X-Mercurial-Series-Id: <0c7b871cb86b61a1c07e.240@test-hostname>
   User-Agent: Mercurial-patchbomb/* (glob)
   Date: Thu, 01 Jan 1970 00:04:00 +0000
   From: quux
@@ -643,11 +646,11 @@
   # User test
   # Date 4 0
   #      Thu Jan 01 00:00:04 1970 +0000
-  # Node ID a2ea8fc83dd8b93cfd86ac97b28287204ab806e1
-  # Parent  909a00e13e9d78b575aeee23dddbada46d5a143f
+  # Node ID 0c7b871cb86b61a1c07e244393603c361e4a178d
+  # Parent  f81ef97829467e868fc405fccbcfa66217e4d3e6
   long line
   
-  diff -r 909a00e13e9d -r a2ea8fc83dd8 long
+  diff -r f81ef9782946 -r 0c7b871cb86b long
   --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   +++ b/long	Thu Jan 01 00:00:04 1970 +0000
   @@ -0,0 +1,4 @@
@@ -690,11 +693,11 @@
   Content-Type: text/plain; charset="iso-8859-1"
   Content-Transfer-Encoding: quoted-printable
   Subject: [PATCH] isolatin 8-bit encoding
-  X-Mercurial-Node: 240fb913fc1b7ff15ddb9f33e73d82bf5277c720
+  X-Mercurial-Node: 4d6f44f466c96d89f2e7e865a70ff41d8b6eee37
   X-Mercurial-Series-Index: 1
   X-Mercurial-Series-Total: 1
-  Message-Id: <240fb913fc1b7ff15ddb.300@test-hostname>
-  X-Mercurial-Series-Id: <240fb913fc1b7ff15ddb.300@test-hostname>
+  Message-Id: <4d6f44f466c96d89f2e7.300@test-hostname>
+  X-Mercurial-Series-Id: <4d6f44f466c96d89f2e7.300@test-hostname>
   User-Agent: Mercurial-patchbomb/* (glob)
   Date: Thu, 01 Jan 1970 00:05:00 +0000
   From: quux
@@ -705,11 +708,11 @@
   # User test
   # Date 5 0
   #      Thu Jan 01 00:00:05 1970 +0000
-  # Node ID 240fb913fc1b7ff15ddb9f33e73d82bf5277c720
-  # Parent  a2ea8fc83dd8b93cfd86ac97b28287204ab806e1
+  # Node ID 4d6f44f466c96d89f2e7e865a70ff41d8b6eee37
+  # Parent  0c7b871cb86b61a1c07e244393603c361e4a178d
   isolatin 8-bit encoding
   
-  diff -r a2ea8fc83dd8 -r 240fb913fc1b isolatin
+  diff -r 0c7b871cb86b -r 4d6f44f466c9 isolatin
   --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   +++ b/isolatin	Thu Jan 01 00:00:05 1970 +0000
   @@ -0,0 +1,1 @@
@@ -937,11 +940,11 @@
   Content-Type: multipart/mixed; boundary="===*==" (glob)
   MIME-Version: 1.0
   Subject: [PATCH] test
-  X-Mercurial-Node: a2ea8fc83dd8b93cfd86ac97b28287204ab806e1
+  X-Mercurial-Node: 0c7b871cb86b61a1c07e244393603c361e4a178d
   X-Mercurial-Series-Index: 1
   X-Mercurial-Series-Total: 1
-  Message-Id: <a2ea8fc83dd8b93cfd86.60@test-hostname>
-  X-Mercurial-Series-Id: <a2ea8fc83dd8b93cfd86.60@test-hostname>
+  Message-Id: <0c7b871cb86b61a1c07e.60@test-hostname>
+  X-Mercurial-Series-Id: <0c7b871cb86b61a1c07e.60@test-hostname>
   User-Agent: Mercurial-patchbomb/* (glob)
   Date: Thu, 01 Jan 1970 00:01:00 +0000
   From: quux
@@ -958,11 +961,11 @@
   # User test
   # Date 4 0
   #      Thu Jan 01 00:00:04 1970 +0000
-  # Node ID a2ea8fc83dd8b93cfd86ac97b28287204ab806e1
-  # Parent  909a00e13e9d78b575aeee23dddbada46d5a143f
+  # Node ID 0c7b871cb86b61a1c07e244393603c361e4a178d
+  # Parent  f81ef97829467e868fc405fccbcfa66217e4d3e6
   long line
   
-  diff -r 909a00e13e9d -r a2ea8fc83dd8 long
+  diff -r f81ef9782946 -r 0c7b871cb86b long
   --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   +++ b/long	Thu Jan 01 00:00:04 1970 +0000
   @@ -0,0 +1,4 @@
@@ -1088,10 +1091,10 @@
   Content-Type: multipart/mixed; boundary="===*==" (glob)
   MIME-Version: 1.0
   Subject: [PATCH 3 of 3] long line
-  X-Mercurial-Node: a2ea8fc83dd8b93cfd86ac97b28287204ab806e1
+  X-Mercurial-Node: 0c7b871cb86b61a1c07e244393603c361e4a178d
   X-Mercurial-Series-Index: 3
   X-Mercurial-Series-Total: 3
-  Message-Id: <a2ea8fc83dd8b93cfd86.63@test-hostname>
+  Message-Id: <0c7b871cb86b61a1c07e.63@test-hostname>
   X-Mercurial-Series-Id: <8580ff50825a50c8f716.61@test-hostname>
   In-Reply-To: <patchbomb.60@test-hostname>
   References: <patchbomb.60@test-hostname>
@@ -1111,11 +1114,11 @@
   # User test
   # Date 4 0
   #      Thu Jan 01 00:00:04 1970 +0000
-  # Node ID a2ea8fc83dd8b93cfd86ac97b28287204ab806e1
-  # Parent  909a00e13e9d78b575aeee23dddbada46d5a143f
+  # Node ID 0c7b871cb86b61a1c07e244393603c361e4a178d
+  # Parent  f81ef97829467e868fc405fccbcfa66217e4d3e6
   long line
   
-  diff -r 909a00e13e9d -r a2ea8fc83dd8 long
+  diff -r f81ef9782946 -r 0c7b871cb86b long
   --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   +++ b/long	Thu Jan 01 00:00:04 1970 +0000
   @@ -0,0 +1,4 @@
@@ -1199,11 +1202,11 @@
   Content-Type: multipart/mixed; boundary="===*==" (glob)
   MIME-Version: 1.0
   Subject: [PATCH] test
-  X-Mercurial-Node: a2ea8fc83dd8b93cfd86ac97b28287204ab806e1
+  X-Mercurial-Node: 0c7b871cb86b61a1c07e244393603c361e4a178d
   X-Mercurial-Series-Index: 1
   X-Mercurial-Series-Total: 1
-  Message-Id: <a2ea8fc83dd8b93cfd86.60@test-hostname>
-  X-Mercurial-Series-Id: <a2ea8fc83dd8b93cfd86.60@test-hostname>
+  Message-Id: <0c7b871cb86b61a1c07e.60@test-hostname>
+  X-Mercurial-Series-Id: <0c7b871cb86b61a1c07e.60@test-hostname>
   User-Agent: Mercurial-patchbomb/* (glob)
   Date: Thu, 01 Jan 1970 00:01:00 +0000
   From: quux
@@ -1229,11 +1232,11 @@
   # User test
   # Date 4 0
   #      Thu Jan 01 00:00:04 1970 +0000
-  # Node ID a2ea8fc83dd8b93cfd86ac97b28287204ab806e1
-  # Parent  909a00e13e9d78b575aeee23dddbada46d5a143f
+  # Node ID 0c7b871cb86b61a1c07e244393603c361e4a178d
+  # Parent  f81ef97829467e868fc405fccbcfa66217e4d3e6
   long line
   
-  diff -r 909a00e13e9d -r a2ea8fc83dd8 long
+  diff -r f81ef9782946 -r 0c7b871cb86b long
   --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   +++ b/long	Thu Jan 01 00:00:04 1970 +0000
   @@ -0,0 +1,4 @@
@@ -1438,10 +1441,10 @@
   Content-Type: multipart/mixed; boundary="===*==" (glob)
   MIME-Version: 1.0
   Subject: [PATCH 3 of 3] long line
-  X-Mercurial-Node: a2ea8fc83dd8b93cfd86ac97b28287204ab806e1
+  X-Mercurial-Node: 0c7b871cb86b61a1c07e244393603c361e4a178d
   X-Mercurial-Series-Index: 3
   X-Mercurial-Series-Total: 3
-  Message-Id: <a2ea8fc83dd8b93cfd86.63@test-hostname>
+  Message-Id: <0c7b871cb86b61a1c07e.63@test-hostname>
   X-Mercurial-Series-Id: <8580ff50825a50c8f716.61@test-hostname>
   In-Reply-To: <patchbomb.60@test-hostname>
   References: <patchbomb.60@test-hostname>
@@ -1470,11 +1473,11 @@
   # User test
   # Date 4 0
   #      Thu Jan 01 00:00:04 1970 +0000
-  # Node ID a2ea8fc83dd8b93cfd86ac97b28287204ab806e1
-  # Parent  909a00e13e9d78b575aeee23dddbada46d5a143f
+  # Node ID 0c7b871cb86b61a1c07e244393603c361e4a178d
+  # Parent  f81ef97829467e868fc405fccbcfa66217e4d3e6
   long line
   
-  diff -r 909a00e13e9d -r a2ea8fc83dd8 long
+  diff -r f81ef9782946 -r 0c7b871cb86b long
   --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   +++ b/long	Thu Jan 01 00:00:04 1970 +0000
   @@ -0,0 +1,4 @@
@@ -1929,11 +1932,11 @@
   Content-Type: text/plain; charset="us-ascii"
   Content-Transfer-Encoding: 7bit
   Subject: [PATCH] Added tag two, two.diff for changeset ff2c9fa2018b
-  X-Mercurial-Node: 7aead2484924c445ad8ce2613df91f52f9e502ed
+  X-Mercurial-Node: 9cea7492c36bdda2c72e7dd5f35f7fc367adeb2c
   X-Mercurial-Series-Index: 1
   X-Mercurial-Series-Total: 1
-  Message-Id: <7aead2484924c445ad8c.60@test-hostname>
-  X-Mercurial-Series-Id: <7aead2484924c445ad8c.60@test-hostname>
+  Message-Id: <9cea7492c36bdda2c72e.60@test-hostname>
+  X-Mercurial-Series-Id: <9cea7492c36bdda2c72e.60@test-hostname>
   In-Reply-To: <baz>
   References: <baz>
   User-Agent: Mercurial-patchbomb/* (glob)
@@ -1946,11 +1949,11 @@
   # User test
   # Date 0 0
   #      Thu Jan 01 00:00:00 1970 +0000
-  # Node ID 7aead2484924c445ad8ce2613df91f52f9e502ed
-  # Parent  045ca29b1ea20e4940411e695e20e521f2f0f98e
+  # Node ID 9cea7492c36bdda2c72e7dd5f35f7fc367adeb2c
+  # Parent  3b775b32716d9b54291ccddf0a36ceea45449bfb
   Added tag two, two.diff for changeset ff2c9fa2018b
   
-  diff -r 045ca29b1ea2 -r 7aead2484924 .hgtags
+  diff -r 3b775b32716d -r 9cea7492c36b .hgtags
   --- a/.hgtags	Thu Jan 01 00:00:00 1970 +0000
   +++ b/.hgtags	Thu Jan 01 00:00:00 1970 +0000
   @@ -2,3 +2,5 @@
@@ -2397,9 +2400,12 @@
   User-Agent: Mercurial-patchbomb/* (glob)
   Date: Tue, 01 Jan 1980 00:01:00 +0000
   From: quux
-  To: spam <spam>, eggs, toast
-  Cc: foo, bar@example.com, "A, B <>" <a@example.com>
-  Bcc: "Quux, A." <quux>
+  To: spam <spam>, eggs, toast (no-py3 !)
+  Cc: foo, bar@example.com, "A, B <>" <a@example.com> (no-py3 !)
+  Bcc: "Quux, A." <quux> (no-py3 !)
+  To: =?iso-8859-1?q?spam?= <spam>, eggs, toast (py3 !)
+  Cc: foo, bar@example.com, =?iso-8859-1?q?A=2C_B_=3C=3E?= <a@example.com> (py3 !)
+  Bcc: =?iso-8859-1?q?Quux=2C_A=2E?= <quux> (py3 !)
   
   # HG changeset patch
   # User test
@@ -2601,17 +2607,17 @@
   |
   o  9:2f9fa9b998c5 d
   |
-  | o  8:7aead2484924 Added tag two, two.diff for changeset ff2c9fa2018b
+  | o  8:9cea7492c36b Added tag two, two.diff for changeset ff2c9fa2018b
   | |
-  | o  7:045ca29b1ea2 Added tag one, one.patch for changeset 97d72e5f12c7
+  | o  7:3b775b32716d Added tag one, one.patch for changeset 97d72e5f12c7
   | |
-  | o  6:5d5ef15dfe5e Added tag zero, zero.foo for changeset 8580ff50825a
+  | o  6:c41d7353114c Added tag zero, zero.foo for changeset 8580ff50825a
   | |
-  | o  5:240fb913fc1b isolatin 8-bit encoding
+  | o  5:4d6f44f466c9 isolatin 8-bit encoding
   | |
-  | o  4:a2ea8fc83dd8 long line
+  | o  4:0c7b871cb86b long line
   | |
-  | o  3:909a00e13e9d utf-8 content
+  | o  3:f81ef9782946 \xe7a (esc)
   | |
   | o  2:ff2c9fa2018b c
   |/
@@ -2673,15 +2679,16 @@
   @@ -0,0 +1,1 @@
   +c
   
-  displaying [PATCH 2 of 6] utf-8 content ...
+  displaying [PATCH 2 of 6] \xe7a ... (esc)
   MIME-Version: 1.0
   Content-Type: text/plain; charset="iso-8859-1"
   Content-Transfer-Encoding: quoted-printable
-  Subject: [PATCH 2 of 6] utf-8 content
-  X-Mercurial-Node: 909a00e13e9d78b575aeee23dddbada46d5a143f
+  Subject: [PATCH 2 of 6] \xe7a (esc) (no-py3 !)
+  Subject: =?utf-8?b?W1BBVENIIDIgb2YgNl0gw6dh?= (py3 !)
+  X-Mercurial-Node: f81ef97829467e868fc405fccbcfa66217e4d3e6
   X-Mercurial-Series-Index: 2
   X-Mercurial-Series-Total: 6
-  Message-Id: <909a00e13e9d78b575ae.315532862@test-hostname>
+  Message-Id: <f81ef97829467e868fc4.315532862@test-hostname>
   X-Mercurial-Series-Id: <ff2c9fa2018b15fa74b3.315532861@test-hostname>
   In-Reply-To: <patchbomb.315532860@test-hostname>
   References: <patchbomb.315532860@test-hostname>
@@ -2694,18 +2701,18 @@
   # User test
   # Date 4 0
   #      Thu Jan 01 00:00:04 1970 +0000
-  # Node ID 909a00e13e9d78b575aeee23dddbada46d5a143f
+  # Node ID f81ef97829467e868fc405fccbcfa66217e4d3e6
   # Parent  ff2c9fa2018b15fa74b33363bda9527323e2a99f
-  utf-8 content
-  
-  diff -r ff2c9fa2018b -r 909a00e13e9d description
+  =E7a
+  
+  diff -r ff2c9fa2018b -r f81ef9782946 description
   --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   +++ b/description	Thu Jan 01 00:00:04 1970 +0000
   @@ -0,0 +1,3 @@
   +a multiline
   +
   +description
-  diff -r ff2c9fa2018b -r 909a00e13e9d utf
+  diff -r ff2c9fa2018b -r f81ef9782946 utf
   --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   +++ b/utf	Thu Jan 01 00:00:04 1970 +0000
   @@ -0,0 +1,1 @@
@@ -2716,10 +2723,10 @@
   Content-Type: text/plain; charset="us-ascii"
   Content-Transfer-Encoding: quoted-printable
   Subject: [PATCH 3 of 6] long line
-  X-Mercurial-Node: a2ea8fc83dd8b93cfd86ac97b28287204ab806e1
+  X-Mercurial-Node: 0c7b871cb86b61a1c07e244393603c361e4a178d
   X-Mercurial-Series-Index: 3
   X-Mercurial-Series-Total: 6
-  Message-Id: <a2ea8fc83dd8b93cfd86.315532863@test-hostname>
+  Message-Id: <0c7b871cb86b61a1c07e.315532863@test-hostname>
   X-Mercurial-Series-Id: <ff2c9fa2018b15fa74b3.315532861@test-hostname>
   In-Reply-To: <patchbomb.315532860@test-hostname>
   References: <patchbomb.315532860@test-hostname>
@@ -2732,11 +2739,11 @@
   # User test
   # Date 4 0
   #      Thu Jan 01 00:00:04 1970 +0000
-  # Node ID a2ea8fc83dd8b93cfd86ac97b28287204ab806e1
-  # Parent  909a00e13e9d78b575aeee23dddbada46d5a143f
+  # Node ID 0c7b871cb86b61a1c07e244393603c361e4a178d
+  # Parent  f81ef97829467e868fc405fccbcfa66217e4d3e6
   long line
   
-  diff -r 909a00e13e9d -r a2ea8fc83dd8 long
+  diff -r f81ef9782946 -r 0c7b871cb86b long
   --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   +++ b/long	Thu Jan 01 00:00:04 1970 +0000
   @@ -0,0 +1,4 @@
@@ -2763,10 +2770,10 @@
   Content-Type: text/plain; charset="iso-8859-1"
   Content-Transfer-Encoding: quoted-printable
   Subject: [PATCH 4 of 6] isolatin 8-bit encoding
-  X-Mercurial-Node: 240fb913fc1b7ff15ddb9f33e73d82bf5277c720
+  X-Mercurial-Node: 4d6f44f466c96d89f2e7e865a70ff41d8b6eee37
   X-Mercurial-Series-Index: 4
   X-Mercurial-Series-Total: 6
-  Message-Id: <240fb913fc1b7ff15ddb.315532864@test-hostname>
+  Message-Id: <4d6f44f466c96d89f2e7.315532864@test-hostname>
   X-Mercurial-Series-Id: <ff2c9fa2018b15fa74b3.315532861@test-hostname>
   In-Reply-To: <patchbomb.315532860@test-hostname>
   References: <patchbomb.315532860@test-hostname>
@@ -2779,11 +2786,11 @@
   # User test
   # Date 5 0
   #      Thu Jan 01 00:00:05 1970 +0000
-  # Node ID 240fb913fc1b7ff15ddb9f33e73d82bf5277c720
-  # Parent  a2ea8fc83dd8b93cfd86ac97b28287204ab806e1
+  # Node ID 4d6f44f466c96d89f2e7e865a70ff41d8b6eee37
+  # Parent  0c7b871cb86b61a1c07e244393603c361e4a178d
   isolatin 8-bit encoding
   
-  diff -r a2ea8fc83dd8 -r 240fb913fc1b isolatin
+  diff -r 0c7b871cb86b -r 4d6f44f466c9 isolatin
   --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   +++ b/isolatin	Thu Jan 01 00:00:05 1970 +0000
   @@ -0,0 +1,1 @@
@@ -2794,10 +2801,10 @@
   Content-Type: text/plain; charset="us-ascii"
   Content-Transfer-Encoding: 7bit
   Subject: [PATCH 5 of 6] Added tag zero, zero.foo for changeset 8580ff50825a
-  X-Mercurial-Node: 5d5ef15dfe5e7bd3a4ee154b5fff76c7945ec433
+  X-Mercurial-Node: c41d7353114ccb07a50a822ad5ddf47051c88ec2
   X-Mercurial-Series-Index: 5
   X-Mercurial-Series-Total: 6
-  Message-Id: <5d5ef15dfe5e7bd3a4ee.315532865@test-hostname>
+  Message-Id: <c41d7353114ccb07a50a.315532865@test-hostname>
   X-Mercurial-Series-Id: <ff2c9fa2018b15fa74b3.315532861@test-hostname>
   In-Reply-To: <patchbomb.315532860@test-hostname>
   References: <patchbomb.315532860@test-hostname>
@@ -2810,11 +2817,11 @@
   # User test
   # Date 0 0
   #      Thu Jan 01 00:00:00 1970 +0000
-  # Node ID 5d5ef15dfe5e7bd3a4ee154b5fff76c7945ec433
-  # Parent  240fb913fc1b7ff15ddb9f33e73d82bf5277c720
+  # Node ID c41d7353114ccb07a50a822ad5ddf47051c88ec2
+  # Parent  4d6f44f466c96d89f2e7e865a70ff41d8b6eee37
   Added tag zero, zero.foo for changeset 8580ff50825a
   
-  diff -r 240fb913fc1b -r 5d5ef15dfe5e .hgtags
+  diff -r 4d6f44f466c9 -r c41d7353114c .hgtags
   --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
   +++ b/.hgtags	Thu Jan 01 00:00:00 1970 +0000
   @@ -0,0 +1,2 @@
--- a/tests/test-worker.t	Mon Nov 04 00:16:44 2019 +0100
+++ b/tests/test-worker.t	Tue Nov 05 13:19:24 2019 -0800
@@ -85,7 +85,7 @@
   $ hg --config "extensions.t=$abspath" --config 'worker.numcpus=8' \
   > test 100000.0 abort --traceback 2>&1 | egrep '(SystemExit|Abort)'
       raise error.Abort(b'known exception')
-  mercurial.error.Abort: b'known exception' (py3 !)
+  mercurial.error.Abort: known exception (py3 !)
   Abort: known exception (no-py3 !)
   SystemExit: 255
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/unwrap-message-id.py	Tue Nov 05 13:19:24 2019 -0800
@@ -0,0 +1,6 @@
+from __future__ import absolute_import, print_function
+
+import re
+import sys
+
+print(re.sub(r"(?<=Message-Id:) \n ", " ", sys.stdin.read()), end="")