[PATCH 3 of 9] util.py: improved the check for the existence of the 'buffer' builtin

Renato Cunha renatoc at gmail.com
Wed Jul 14 21:18:36 CDT 2010


# HG changeset patch
# User Renato Cunha <renatoc at gmail.com>
# Date 1279159183 10800
# Branch stable
# Node ID f1975eb501930bb8f3cb56ceac2eb7137d9a8c57
# Parent  c819375205632865c65a92eac72266f3bc84cd3a
util.py: improved the check for the existence of the 'buffer' builtin

2to3 is unable to translate '__builtin__' calls to 'builtins' when hasattr is
used (as in hasattr(__builtin__, buffer)). Translating the check to the format

try:
    whatever
except NameError
    # define whatever
    __builtin__.whatever = whatever

is a correct way of checking for the name and has the benefit of being
translated by 2to3. This patch implements the same idea for the aforementioned
example.

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -40,7 +40,9 @@
 
 def fakebuffer(sliceable, offset=0):
     return sliceable[offset:]
-if not hasattr(__builtin__, 'buffer'):
+try:
+    buffer
+except NameError:
     __builtin__.buffer = fakebuffer
 
 import subprocess


More information about the Mercurial-devel mailing list