[PATCH] py3: make files use absolute_import and print_function

Martijn Pieters mj at zopatista.com
Mon Jul 4 08:27:44 EDT 2016


On 3 July 2016 at 18:10, Pulkit Goyal <7895pulkit at gmail.com> wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit at gmail.com>
> # Date 1467565104 -19800
> #      Sun Jul 03 22:28:24 2016 +0530
> # Node ID d1d00ebd815da57194b22cbd5f7c09b9653e6074
> # Parent  3bd36647b893509dbc6efa2b4d439d833cc6debc
> 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 also followed.

This too is looking good.

> diff -r 3bd36647b893 -r d1d00ebd815d hgext/highlight/__init__.py
> --- a/hgext/highlight/__init__.py       Sat Jul 02 02:52:15 2016 +0530
> +++ b/hgext/highlight/__init__.py       Sun Jul 03 22:28:24 2016 +0530
> @@ -26,9 +26,21 @@
>  match (even matches with a low confidence score) will be used.
>  """
>
> -import highlight
> -from mercurial.hgweb import webcommands, webutil, common
> -from mercurial import extensions, encoding, fileset
> +from __future__ import absolute_import
> +
> +from . import highlight
> +from mercurial.hgweb import (
> +    common,
> +    webcommands,
> +    webutil,
> +)
> +
> +from mercurial import (
> +    encoding,
> +    extensions,
> +    fileset,
> +)
> +
>  # Note for extension authors: ONLY specify testedwith = 'internal' for
>  # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
>  # be specifying the version(s) of Mercurial they are tested with, or
> diff -r 3bd36647b893 -r d1d00ebd815d hgext/highlight/highlight.py
> --- a/hgext/highlight/highlight.py      Sat Jul 02 02:52:15 2016 +0530
> +++ b/hgext/highlight/highlight.py      Sun Jul 03 22:28:24 2016 +0530
> @@ -8,14 +8,27 @@
>  # The original module was split in an interface and an implementation
>  # file to defer pygments loading and speedup extension setup.
>
> +from __future__ import absolute_import
> +
> +import pygments
> +import pygments.formatters
> +import pygments.lexers
> +import pygments.util
> +
>  from mercurial import demandimport
>  demandimport.ignore.extend(['pkgutil', 'pkg_resources', '__main__'])
> -from mercurial import util, encoding
>
> -from pygments import highlight
> -from pygments.util import ClassNotFound
> -from pygments.lexers import guess_lexer, guess_lexer_for_filename, TextLexer
> -from pygments.formatters import HtmlFormatter
> +from mercurial import (
> +    encoding,
> +    util,
> +)
> +
> +highlight = pygments.highlight
> +ClassNotFound = pygments.util.ClassNotFound
> +guess_lexer = pygments.lexers.guess_lexer
> +guess_lexer_for_filename = pygments.lexers.guess_lexer_for_filename
> +TextLexer = pygments.lexers.TextLexer
> +HtmlFormatter = pygments.formatters.HtmlFormatter
>
>  SYNTAX_CSS = ('\n<link rel="stylesheet" href="{url}highlightcss" '
>                'type="text/css" />')
> diff -r 3bd36647b893 -r d1d00ebd815d hgext/share.py
> --- a/hgext/share.py    Sat Jul 02 02:52:15 2016 +0530
> +++ b/hgext/share.py    Sun Jul 03 22:28:24 2016 +0530
> @@ -37,10 +37,22 @@
>      The default naming mode is "identity."
>  '''
>
> +from __future__ import absolute_import
> +
> +import errno
>  from mercurial.i18n import _
> -from mercurial import cmdutil, commands, hg, util, extensions, bookmarks, error
> -from mercurial.hg import repository, parseurl
> -import errno
> +from mercurial import (
> +    bookmarks,
> +    cmdutil,
> +    commands,
> +    error,
> +    extensions,
> +    hg,
> +    util,
> +)
> +
> +repository = hg.repository
> +parseurl = hg.parseurl
>
>  cmdtable = {}
>  command = cmdutil.command(cmdtable)
> diff -r 3bd36647b893 -r d1d00ebd815d hgext/win32text.py
> --- a/hgext/win32text.py        Sat Jul 02 02:52:15 2016 +0530
> +++ b/hgext/win32text.py        Sun Jul 03 22:28:24 2016 +0530
> @@ -41,10 +41,16 @@
>    # or pretxnchangegroup.cr = python:hgext.win32text.forbidcr
>  '''
>
> +from __future__ import absolute_import
> +
> +import re
>  from mercurial.i18n import _
> -from mercurial.node import short
> -from mercurial import util
> -import re
> +from mercurial.node import (
> +    short,
> +)
> +from mercurial import (
> +    util,
> +)
>
>  # Note for extension authors: ONLY specify testedwith = 'internal' for
>  # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
> diff -r 3bd36647b893 -r d1d00ebd815d i18n/polib.py
> --- a/i18n/polib.py     Sat Jul 02 02:52:15 2016 +0530
> +++ b/i18n/polib.py     Sun Jul 03 22:28:24 2016 +0530
> @@ -13,6 +13,8 @@
>  :func:`~polib.mofile` convenience functions.
>  """
>
> +from __future__ import absolute_import
> +
>  __author__    = 'David Jean Louis <izimobil at gmail.com>'
>  __version__   = '0.6.4'
>  __all__       = ['pofile', 'POFile', 'POEntry', 'mofile', 'MOFile', 'MOEntry',
> diff -r 3bd36647b893 -r d1d00ebd815d tests/heredoctest.py
> --- a/tests/heredoctest.py      Sat Jul 02 02:52:15 2016 +0530
> +++ b/tests/heredoctest.py      Sun Jul 03 22:28:24 2016 +0530
> @@ -1,4 +1,4 @@
> -from __future__ import absolute_import
> +from __future__ import absolute_import, print_function
>
>  import sys
>
> diff -r 3bd36647b893 -r d1d00ebd815d tests/md5sum.py
> --- a/tests/md5sum.py   Sat Jul 02 02:52:15 2016 +0530
> +++ b/tests/md5sum.py   Sun Jul 03 22:28:24 2016 +0530
> @@ -6,12 +6,17 @@
>  # of the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2, which is
>  # GPL-compatible.
>
> -import sys, os
> +from __future__ import absolute_import
> +
> +import os
> +import sys
>
>  try:
> -    from hashlib import md5
> +    import hashlib
> +    md5 = hashlib.md5
>  except ImportError:
> -    from md5 import md5
> +    import md5
> +    md5 = md5.md5
>
>  try:
>      import msvcrt
> diff -r 3bd36647b893 -r d1d00ebd815d tests/readlink.py
> --- a/tests/readlink.py Sat Jul 02 02:52:15 2016 +0530
> +++ b/tests/readlink.py Sun Jul 03 22:28:24 2016 +0530
> @@ -1,8 +1,10 @@
>  #!/usr/bin/env python
>
> -from __future__ import print_function
> +from __future__ import absolute_import, print_function
>
> -import errno, os, sys
> +import errno
> +import os
> +import sys
>
>  for f in sys.argv[1:]:
>      try:
> diff -r 3bd36647b893 -r d1d00ebd815d tests/run-tests.py
> --- a/tests/run-tests.py        Sat Jul 02 02:52:15 2016 +0530
> +++ b/tests/run-tests.py        Sun Jul 03 22:28:24 2016 +0530
> @@ -43,31 +43,31 @@
>  # completes fairly quickly, includes both shell and Python scripts, and
>  # includes some scripts that run daemon processes.)
>
> -from __future__ import print_function
> +from __future__ import absolute_import, print_function
>
> -from distutils import version
>  import difflib
> +import distutils.version as version
>  import errno
>  import json
>  import optparse
>  import os
> +import random
> +import re
>  import shutil
> -import subprocess
>  import signal
>  import socket
> +import subprocess
>  import sys
>  import tempfile
> +import threading
>  import time
> -import random
> -import re
> -import threading
> -import killdaemons as killmod
> +import unittest
> +import xml.dom.minidom as minidom
> +
>  try:
>      import Queue as queue
>  except ImportError:
>      import queue
> -from xml.dom import minidom
> -import unittest
>
>  if os.environ.get('RTUNICODEPEDANTRY', False):
>      try:
> @@ -475,6 +475,7 @@
>          pass
>
>  def killdaemons(pidfile):
> +    import killdaemons as killmod
>      return killmod.killdaemons(pidfile, tryhard=False, remove=True,
>                                 logfn=vlog)
>
> @@ -2493,7 +2494,8 @@
>
>      def _outputcoverage(self):
>          """Produce code coverage output."""
> -        from coverage import coverage
> +        import coverage
> +        coverage = coverage.coverage
>
>          vlog('# Producing coverage report')
>          # chdir is the easiest way to get short, relative paths in the
> diff -r 3bd36647b893 -r d1d00ebd815d tests/test-check-py3-compat.t
> --- a/tests/test-check-py3-compat.t     Sat Jul 02 02:52:15 2016 +0530
> +++ b/tests/test-check-py3-compat.t     Sun Jul 03 22:28:24 2016 +0530
> @@ -8,17 +8,8 @@
>    hgext/fsmonitor/pywatchman/__init__.py requires print_function
>    hgext/fsmonitor/pywatchman/capabilities.py not using absolute_import
>    hgext/fsmonitor/pywatchman/pybser.py not using absolute_import
> -  hgext/highlight/__init__.py not using absolute_import
> -  hgext/highlight/highlight.py not using absolute_import
> -  hgext/share.py not using absolute_import
> -  hgext/win32text.py not using absolute_import
>    i18n/check-translation.py not using absolute_import
> -  i18n/polib.py not using absolute_import
>    setup.py not using absolute_import
> -  tests/heredoctest.py requires print_function
> -  tests/md5sum.py not using absolute_import
> -  tests/readlink.py not using absolute_import
> -  tests/run-tests.py not using absolute_import
>    tests/test-demandimport.py not using absolute_import
>
>  #if py3exe
> diff -r 3bd36647b893 -r d1d00ebd815d tests/test-commandserver.t
> --- a/tests/test-commandserver.t        Sat Jul 02 02:52:15 2016 +0530
> +++ b/tests/test-commandserver.t        Sun Jul 03 22:28:24 2016 +0530
> @@ -13,11 +13,12 @@
>    $ hg init repo
>    $ cd repo
>
> +  >>> from __future__ import print_function
>    >>> from hgclient import readchannel, runcommand, check
>    >>> @check
>    ... def hellomessage(server):
>    ...     ch, data = readchannel(server)
> -  ...     print '%c, %r' % (ch, data)
> +  ...     print('%c, %r' % (ch, data))
>    ...     # run an arbitrary command to make sure the next thing the server
>    ...     # sends isn't part of the hello message
>    ...     runcommand(server, ['id'])
> @@ -99,7 +100,7 @@
>    ...     server.stdin.close()
>    ...
>    ...     # server exits with 1 if the pipe closed while reading the command
> -  ...     print 'server exit code =', server.wait()
> +  ...     print('server exit code =', server.wait())
>    server exit code = 1
>
>    >>> from hgclient import readchannel, runcommand, check, stringio
> @@ -206,10 +207,11 @@
>  #endif
>
>    $ cat <<EOF > hook.py
> +  > from __future__ import print_function
>    > import sys
>    > def hook(**args):
> -  >     print 'hook talking'
> -  >     print 'now try to read something: %r' % sys.stdin.read()
> +  >     print('hook talking')
> +  >     print('now try to read something: %r' % sys.stdin.read())
>    > EOF
>
>    >>> from hgclient import readchannel, runcommand, check, stringio
> @@ -610,18 +612,19 @@
>
>  run commandserver in commandserver, which is silly but should work:
>
> +  >>> from __future__ import print_function
>    >>> from hgclient import readchannel, runcommand, check, stringio
>    >>> @check
>    ... def nested(server):
> -  ...     print '%c, %r' % readchannel(server)
> +  ...     print('%c, %r' % readchannel(server))
>    ...     class nestedserver(object):
>    ...         stdin = stringio('getencoding\n')
>    ...         stdout = stringio()
>    ...     runcommand(server, ['serve', '--cmdserver', 'pipe'],
>    ...                output=nestedserver.stdout, input=nestedserver.stdin)
>    ...     nestedserver.stdout.seek(0)
> -  ...     print '%c, %r' % readchannel(nestedserver)  # hello
> -  ...     print '%c, %r' % readchannel(nestedserver)  # getencoding
> +  ...     print('%c, %r' % readchannel(nestedserver))  # hello
> +  ...     print('%c, %r' % readchannel(nestedserver))  # getencoding
>    o, 'capabilities: getencoding runcommand\nencoding: *\npid: *' (glob)
>    *** runcommand serve --cmdserver pipe
>    o, 'capabilities: getencoding runcommand\nencoding: *\npid: *' (glob)
> @@ -632,11 +635,12 @@
>
>    $ cd ..
>
> +  >>> from __future__ import print_function
>    >>> from hgclient import readchannel, runcommand, check
>    >>> @check
>    ... def hellomessage(server):
>    ...     ch, data = readchannel(server)
> -  ...     print '%c, %r' % (ch, data)
> +  ...     print('%c, %r' % (ch, data))
>    ...     # run an arbitrary command to make sure the next thing the server
>    ...     # sends isn't part of the hello message
>    ...     runcommand(server, ['id'])
> @@ -672,11 +676,12 @@
>
>  #if unix-socket unix-permissions
>
> +  >>> from __future__ import print_function
>    >>> from hgclient import unixserver, readchannel, runcommand, check, stringio
>    >>> server = unixserver('.hg/server.sock', '.hg/server.log')
>    >>> def hellomessage(conn):
>    ...     ch, data = readchannel(conn)
> -  ...     print '%c, %r' % (ch, data)
> +  ...     print('%c, %r' % (ch, data))
>    ...     runcommand(conn, ['id'])
>    >>> check(hellomessage, server.connect)
>    o, 'capabilities: getencoding runcommand\nencoding: *\npid: *' (glob)
> @@ -723,6 +728,7 @@
>    > [cmdserver]
>    > log = inexistent/path.log
>    > EOF
> +  >>> from __future__ import print_function
>    >>> from hgclient import unixserver, readchannel, check
>    >>> server = unixserver('.hg/server.sock', '.hg/server.log')
>    >>> def earlycrash(conn):
> @@ -730,7 +736,7 @@
>    ...         try:
>    ...             ch, data = readchannel(conn)
>    ...             if not data.startswith('  '):
> -  ...                 print '%c, %r' % (ch, data)
> +  ...                 print('%c, %r' % (ch, data))
>    ...         except EOFError:
>    ...             break
>    >>> check(earlycrash, server.connect)
> diff -r 3bd36647b893 -r d1d00ebd815d tests/test-generaldelta.t
> --- a/tests/test-generaldelta.t Sat Jul 02 02:52:15 2016 +0530
> +++ b/tests/test-generaldelta.t Sun Jul 03 22:28:24 2016 +0530
> @@ -18,11 +18,12 @@
>    > done
>
>    $ cd ..
> +  >>> from __future__ import print_function
>    >>> import os
>    >>> regsize = os.stat("repo/.hg/store/00manifest.i").st_size
>    >>> gdsize = os.stat("gdrepo/.hg/store/00manifest.i").st_size
>    >>> if regsize < gdsize:
> -  ...     print 'generaldata increased size of manifest'
> +  ...     print('generaldata increased size of manifest')
>
>  Verify rev reordering doesnt create invalid bundles (issue4462)
>  This requires a commit tree that when pulled will reorder manifest revs such
> diff -r 3bd36647b893 -r d1d00ebd815d tests/test-hgweb-commands.t
> --- a/tests/test-hgweb-commands.t       Sat Jul 02 02:52:15 2016 +0530
> +++ b/tests/test-hgweb-commands.t       Sun Jul 03 22:28:24 2016 +0530
> @@ -2104,9 +2104,10 @@
>  Graph json escape of multibyte character
>
>    $ get-with-headers.py 127.0.0.1:$HGPORT 'graph/' > out
> +  >>> from __future__ import print_function
>    >>> for line in open("out"):
>    ...     if line.startswith("var data ="):
> -  ...         print line,
> +  ...         print(line, end='')
>    var data = [["061dd13ba3c3", [0, 1], [[0, 0, 1, -1, ""]], "\u80fd", "test", "1970-01-01", ["unstable", true], ["tip"], ["something"]], ["cad8025a2e87", [0, 1], [[0, 0, 1, 3, "FF0000"]], "branch commit with null character: \u0000", "test", "1970-01-01", ["unstable", false], [], []], ["1d22e65f027e", [0, 1], [[0, 0, 1, 3, ""]], "branch", "test", "1970-01-01", ["stable", true], [], []], ["a4f92ed23982", [0, 1], [[0, 0, 1, 3, ""]], "Added tag 1.0 for changeset 2ef0ac749a14", "test", "1970-01-01", ["default", true], [], []], ["2ef0ac749a14", [0, 1], [], "base", "test", "1970-01-01", ["default", false], ["1.0"], ["anotherthing"]]];
>
>  capabilities
> diff -r 3bd36647b893 -r d1d00ebd815d tests/test-import.t
> --- a/tests/test-import.t       Sat Jul 02 02:52:15 2016 +0530
> +++ b/tests/test-import.t       Sun Jul 03 22:28:24 2016 +0530
> @@ -53,7 +53,8 @@
>  regardless of the commit message in the patch)
>
>    $ cat > dummypatch.py <<EOF
> -  > print 'patching file a'
> +  > from __future__ import print_function
> +  > print('patching file a')
>    > file('a', 'wb').write('line2\n')
>    > EOF
>    $ hg clone -r0 a b
> diff -r 3bd36647b893 -r d1d00ebd815d tests/test-status.t
> --- a/tests/test-status.t       Sat Jul 02 02:52:15 2016 +0530
> +++ b/tests/test-status.t       Sun Jul 03 22:28:24 2016 +0530
> @@ -203,8 +203,9 @@
>    ]
>
>    $ hg status -A -Tpickle > pickle
> +  >>> from __future__ import print_function
>    >>> import pickle
> -  >>> print sorted((x['status'], x['path']) for x in pickle.load(open("pickle")))
> +  >>> print(sorted((x['status'], x['path']) for x in pickle.load(open("pickle"))))
>    [('!', 'deleted'), ('?', 'pickle'), ('?', 'unknown'), ('A', 'added'), ('A', 'copied'), ('C', '.hgignore'), ('C', 'modified'), ('I', 'ignored'), ('R', 'removed')]
>    $ rm pickle
>
> diff -r 3bd36647b893 -r d1d00ebd815d tests/test-unified-test.t
> --- a/tests/test-unified-test.t Sat Jul 02 02:52:15 2016 +0530
> +++ b/tests/test-unified-test.t Sun Jul 03 22:28:24 2016 +0530
> @@ -26,24 +26,25 @@
>
>  Doctest commands:
>
> -  >>> print 'foo'
> +  >>> from __future__ import print_function
> +  >>> print('foo')
>    foo
>    $ echo interleaved
>    interleaved
>    >>> for c in 'xyz':
> -  ...     print c
> +  ...     print(c)
>    x
>    y
>    z
> -  >>> print
> +  >>> print()
>
>    >>> foo = 'global name'
>    >>> def func():
> -  ...     print foo, 'should be visible in func()'
> +  ...     print(foo, 'should be visible in func()')
>    >>> func()
>    global name should be visible in func()
> -  >>> print '''multiline
> -  ... string'''
> +  >>> print('''multiline
> +  ... string''')
>    multiline
>    string
>
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel at mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel



-- 
Martijn Pieters


More information about the Mercurial-devel mailing list