[PATCH] setup.py: use bytes literals

Gregory Szorc gregory.szorc at gmail.com
Sun Dec 13 17:44:44 UTC 2015


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1450028453 28800
#      Sun Dec 13 09:40:53 2015 -0800
# Node ID daca434aae4f860fa7e38b30ee6712bed76da6b1
# Parent  944af8e2eb4cddf96ba5b8a96854528b40979715
setup.py: use bytes literals

The b() helper was needed because Python < 2.6 didn't support bytes
literals (b''). Now that we don't support Python < 2.6, we no longer
need this helper.

diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -4,32 +4,24 @@
 # 'python setup.py install', or
 # 'python setup.py --help' for more options
 
 import sys, platform
 if getattr(sys, 'version_info', (0, 0, 0)) < (2, 6, 0, 'final'):
     raise SystemExit("Mercurial requires Python 2.6 or later.")
 
 if sys.version_info[0] >= 3:
-    def b(s):
-        '''A helper function to emulate 2.6+ bytes literals using string
-        literals.'''
-        return s.encode('latin1')
     printf = eval('print')
     libdir_escape = 'unicode_escape'
 else:
     libdir_escape = 'string_escape'
-    def b(s):
-        '''A helper function to emulate 2.6+ bytes literals using string
-        literals.'''
-        return s
     def printf(*args, **kwargs):
         f = kwargs.get('file', sys.stdout)
         end = kwargs.get('end', '\n')
-        f.write(b(' ').join(args) + end)
+        f.write(b' '.join(args) + end)
 
 # Solaris Python packaging brain damage
 try:
     import hashlib
     sha = hashlib.sha1()
 except ImportError:
     try:
         import sha
@@ -167,22 +159,22 @@ def runcmd(cmd, env):
 def runhg(cmd, env):
     out, err = runcmd(cmd, env)
     # If root is executing setup.py, but the repository is owned by
     # another user (as in "sudo python setup.py install") we will get
     # trust warnings since the .hg/hgrc file is untrusted. That is
     # fine, we don't want to load it anyway.  Python may warn about
     # a missing __init__.py in mercurial/locale, we also ignore that.
     err = [e for e in err.splitlines()
-           if not e.startswith(b('not trusting file')) \
-              and not e.startswith(b('warning: Not importing')) \
-              and not e.startswith(b('obsolete feature not enabled'))]
+           if not e.startswith(b'not trusting file') \
+              and not e.startswith(b'warning: Not importing') \
+              and not e.startswith(b'obsolete feature not enabled')]
     if err:
         printf("stderr from '%s':" % (' '.join(cmd)), file=sys.stderr)
-        printf(b('\n').join([b('  ') + e for e in err]), file=sys.stderr)
+        printf(b'\n'.join([b'  ' + e for e in err]), file=sys.stderr)
         return ''
     return out
 
 version = ''
 
 # Execute hg out of this directory with a custom environment which takes care
 # to not use any hgrc files and do no localization.
 env = {'HGMODULEPOLICY': 'py',
@@ -511,28 +503,28 @@ class hginstallscripts(install_scripts):
             libdir =  uplevel * ('..' + os.sep) + self.install_lib[len(common):]
 
         for outfile in self.outfiles:
             fp = open(outfile, 'rb')
             data = fp.read()
             fp.close()
 
             # skip binary files
-            if b('\0') in data:
+            if b'\0' in data:
                 continue
 
             # During local installs, the shebang will be rewritten to the final
             # install path. During wheel packaging, the shebang has a special
             # value.
             if data.startswith(b'#!python'):
                 log.info('not rewriting @LIBDIR@ in %s because install path '
                          'not known' % outfile)
                 continue
 
-            data = data.replace(b('@LIBDIR@'), libdir.encode(libdir_escape))
+            data = data.replace(b'@LIBDIR@', libdir.encode(libdir_escape))
             fp = open(outfile, 'wb')
             fp.write(data)
             fp.close()
 
 cmdclass = {'build': hgbuild,
             'build_mo': hgbuildmo,
             'build_ext': hgbuildext,
             'build_py': hgbuildpy,


More information about the Mercurial-devel mailing list