[PATCH 3 of 3] py3: make files use absolute_import and print_function

Martijn Pieters mj at zopatista.com
Fri Jul 1 09:47:54 EDT 2016


On 30 June 2016 at 10:47, Pulkit Goyal <7895pulkit at gmail.com> wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit at gmail.com>
> # Date 1467279903 -19800
> #      Thu Jun 30 15:15:03 2016 +0530
> # Node ID 3b93444153b718659c137963c261ad793c8c4366
> # Parent  91320eb3d9ce90433ec843525e5563dbad5193e2
> py3: make files use absolute_import and print_function
>
> This patch includes addition of absolute_import and print_function to the
>  files where they are missing. The modern importing conventions are followed.
>

..8<.. snip ..8<..

> diff -r 91320eb3d9ce -r 3b93444153b7 setup.py
> --- a/setup.py  Tue Jun 28 17:03:21 2016 +0530
> +++ b/setup.py  Thu Jun 30 15:15:03 2016 +0530
> @@ -4,7 +4,52 @@
>  # 'python setup.py install', or
>  # 'python setup.py --help' for more options
>
> -import sys, platform
> +from __future__ import absolute_import
> +
> +import ctypes
> +import distutils
> +import distutils.command.build
> +import distutils.command.build_ext
> +import distutils.command.build_py
> +import distutils.command.build_scripts
> +import distutils.command.install_lib
> +import distutils.command.install_scripts
> +import distutils.core
> +import distutils.dist
> +import distutils.errors
> +import distutils.spawn
> +import distutils.sysconfig
> +import distutils.version
> +import os
> +import platform
> +import re
> +import shutil
> +import stat
> +import subprocess
> +import sys
> +import tempfile
> +import time
> +
> +log = distutils.log
> +file_util = distutils.file_util
> +Command = distutils.core.Command
> +Extension = distutils.core.Extension
> +Distribution = distutils.dist.Distribution
> +build = distutils.command.build.build
> +build_ext = distutils.command.build_ext.build_ext
> +build_py = distutils.command.build_py.build_py
> +build_scripts = distutils.command.build_scripts.build_scripts
> +install_lib = distutils.command.install_lib.install_lib
> +install_scripts = distutils.command.install_scripts.install_scripts
> +spawn = distutils.spawn.spawn
> +find_executable = distutils.spawn.find_executable
> +CCompilerError = distutils.errors.CCompilerError,
> +DistutilsError = distutils.errors.DistutilsError,
> +DistutilsExecError = distutils.errors.DistutilsExecError,
> +get_python_inc = distutils.sysconfig.get_python_inc
> +get_config_var = distutils.sysconfig.get_config_var
> +StrictVersion = distutils.version.StrictVersion
> +
>  if getattr(sys, 'version_info', (0, 0, 0)) < (2, 6, 0, 'final'):
>      raise SystemExit("Mercurial requires Python 2.6 or later.")
>
> @@ -57,33 +102,11 @@
>
>  ispypy = "PyPy" in sys.version
>
> -import ctypes
> -import os, stat, subprocess, time
> -import re
> -import shutil
> -import tempfile
> -from distutils import log
>  if 'FORCE_SETUPTOOLS' in os.environ:
> -    from setuptools import setup
> +    import setuptools
> +    setup = setuptools.setup
>  else:
> -    from distutils.core import setup
> -from distutils.core import Command, Extension
> -from distutils.dist import Distribution
> -from distutils.command.build import build
> -from distutils.command.build_ext import build_ext
> -from distutils.command.build_py import build_py
> -from distutils.command.build_scripts import build_scripts
> -from distutils.command.install_lib import install_lib
> -from distutils.command.install_scripts import install_scripts
> -from distutils.spawn import spawn, find_executable
> -from distutils import file_util
> -from distutils.errors import (
> -    CCompilerError,
> -    DistutilsError,
> -    DistutilsExecError,
> -)
> -from distutils.sysconfig import get_python_inc, get_config_var
> -from distutils.version import StrictVersion
> +    setup = distutils.core.setup
>
>  scripts = ['hg']
>  if os.name == 'nt':
> @@ -127,7 +150,7 @@
>      py2exe.Distribution # silence unused import warning
>      py2exeloaded = True
>      # import py2exe's patched Distribution class
> -    from distutils.core import Distribution
> +    Distribution = distutils.core.Distribution
>  except ImportError:
>      py2exeloaded = False
>
> @@ -565,7 +588,7 @@
>      ]
>
>  try:
> -    from distutils import cygwinccompiler
> +    import distutils.cygwinccompiler as cygwinccompiler
>
>      # the -mno-cygwin option has been deprecated for years
>      compiler = cygwinccompiler.Mingw32CCompiler
> diff -r 91320eb3d9ce -r 3b93444153b7 tests/heredoctest.py
> --- a/tests/heredoctest.py      Tue Jun 28 17:03:21 2016 +0530
> +++ b/tests/heredoctest.py      Thu Jun 30 15:15:03 2016 +0530
> @@ -1,4 +1,4 @@
> -from __future__ import absolute_import
> +from __future__ import absolute_import, print_function
>
>  import sys
>

setup.py is not subject to the Mercurial demandimport hack, so we
really don't need to force attribute access.

Also, note that loads of those imports were listed *after* the Python
version test for a reason; many of those modules can't be imported on
older Python versions, and now the version test will never be reached.
Please do leave that test in place *at the top*.

For the same reason, we can't use any `from __future__ import` syntax;
that too breaks on old Python versions.

On the whole, better leave setup.py alone here. It runs fine on Python
2 and 3 already.


..8<.. snip ..8<..

-- 
Martijn Pieters


More information about the Mercurial-devel mailing list