[PATCH 3 of 5] util.py: Improved the check of the existence of the 'buffer' builtin

Renato Cunha renatoc at gmail.com
Fri Jul 2 23:26:55 CDT 2010


# HG changeset patch
# User Renato Cunha <renatoc at gmail.com>
# Date 1278104636 10800
# Node ID 6c82a0c29e179f7e05e2b9541b50326dcc1c9550
# Parent  78aed677e3b0ba3e14ee79a26d06274197e2c5a2
util.py: Improved the check of 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