[PATCH] setup.py: Adjustments to make setup.py run in py3k, round two

Renato Cunha renatoc at gmail.com
Tue Jun 15 15:56:57 CDT 2010


# HG changeset patch
# User Renato Cunha <renatoc at gmail.com>
# Date 1276635406 10800
# Node ID 31624cfa65d13f0110d32f4b0b34cb1d600c94a4
# Parent  e7ad1f1c55781827df2ea275f0b318e0561074ec
setup.py: Adjustments to make setup.py run in py3k.

In py3k, subprocess.Popen.communicate's output are bytes objects. String
literals are Unicode objects. Thus, when a bytes object startswith method is
called, with string literals, it fails. What this patch does is:

 * Convert the string (unicode in py3k) literals to bytes objects;
 * As "bytes" is not a builtin in python < 2.6, it defines a "B" helper
 function that merely returns its argument, as suggested by Antoine Pitrou.

diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -9,6 +9,17 @@
 if not hasattr(sys, 'version_info') or sys.version_info < (2, 4, 0, 'final'):
     raise SystemExit("Mercurial requires Python 2.4 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')
+else:
+    def B(s):
+        '''A helper function to emulate 2.6+ bytes literals using string
+        literals.'''
+        return s
+
 # Solaris Python packaging brain damage
 try:
     import hashlib
@@ -111,8 +122,8 @@
     # 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('Not trusting file') \
-              and not e.startswith('warning: Not importing')]
+           if not e.startswith(B('Not trusting file')) \
+              and not e.startswith(B('warning: Not importing'))]
     if err:
         return ''
     return out


More information about the Mercurial-devel mailing list