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

Renato Cunha renatoc at gmail.com
Tue Jun 15 07:58:00 CDT 2010


# HG changeset patch
# User Renato Cunha <renatoc at gmail.com>
# Date 1276606337 10800
# Node ID 89a048544c8bafae069c888ea2b7d9add89460d0
# Parent  124683d5caf6d2ba212795ed0b05e653f41a07bc
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's 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 "bytes" function
 that merely returns its first argument in that case.

diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -32,6 +32,12 @@
     raise SystemExit(
         "Couldn't import standard bz2 (incomplete Python install).")
 
+try:
+    bytes
+except NameError:
+    def bytes(str, encoding=None):
+        return str
+
 import os, subprocess, time
 import shutil
 import tempfile
@@ -111,8 +117,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(bytes('Not trusting file', 'utf-8')) \
+              and not e.startswith(bytes('warning: Not importing', 'utf-8'))]
     if err:
         return ''
     return out


More information about the Mercurial-devel mailing list