Anonymous avatar Anonymous committed 6ace517

http://scons.tigris.org/issues/show_bug.cgi?id=2345

The 'buffer' fixer simply replaces 'buffer( ... )' with 'memoryview( ... )',
which is incorrect for our cases, so these changes had to be done by hand and
a forward-compatibility class added.

The 'xrange' fixer was applied. Manual changes were minimal: a few case in
test strings and one use of 'range' as an identifer in the same scope as
where 'xrange' was converted to 'range'.

The "sets15" compat function, which provided backward compatibility for Python
versions prior to 2.2, was removed as no longer needed.

Comments (0)

Files changed (37)

QMTest/TestCmd.py

 import traceback
 import UserList
 
+try:
+    # pre-2.7 doesn't have the memoryview() built-in
+    memoryview
+except NameError:
+    class memoryview:
+        from types import SliceType
+        def __init__(self, obj):
+            # wrapping buffer in () keeps the fixer from changing it
+            self.obj = (buffer)(obj)
+        def __getitem__(self, indx):
+            if isinstance(indx, self.SliceType):
+                return self.obj[indx.start:indx.stop]
+            else:
+                return self.obj[indx]
+
 __all__ = [
     'diff_re',
     'fail_test',
             time.sleep(max((x-time.time())/tr, 0))
     return ''.join(y)
 
-# TODO(3.0:  rewrite to use memoryview()
 def send_all(p, data):
     while len(data):
         sent = p.send(data)
         if sent is None:
             raise Exception(disconnect_message)
-        data = buffer(data, sent)
+        data = memoryview(data)[sent:]
 
 
 

src/engine/SCons/ActionTests.py

 
 def test_varlist(pos_call, str_call, cmd, cmdstrfunc, **kw):
     def call_action(a, pos_call=pos_call, str_call=str_call, kw=kw):
-        #FUTURE a = SCons.Action.Action(*a, **kw)
         a = SCons.Action.Action(*a, **kw)
         # returned object must provide these entry points
         assert hasattr(a, '__call__')

src/engine/SCons/Script/Main.py

             # This is cribbed from the implementation of
             # random.shuffle() in Python 2.X.
             d = dependencies
-            for i in xrange(len(d)-1, 0, -1):
+            for i in range(len(d)-1, 0, -1):
                 j = int(random.random() * (i+1))
                 d[i], d[j] = d[j], d[i]
             return d

src/engine/SCons/compat/__init__.py

     set
 except NameError:
     # Pre-2.4 Python has no native set type
-    try:
-        # Python 2.2 and 2.3 can use the copy of the 2.[45] sets module
-        # that we grabbed.
-        import_as('_scons_sets', 'sets')
-    except (ImportError, SyntaxError):
-        # Python 1.5 (ImportError, no __future_ module) and 2.1
-        # (SyntaxError, no generators in __future__) will blow up
-        # trying to import the 2.[45] sets module, so back off to a
-        # custom sets module that can be discarded easily when we
-        # stop supporting those versions.
-        import_as('_scons_sets15', 'sets')
-    import __builtin__
-    import sets
+    import_as('_scons_sets', 'sets')
+    import __builtin__, sets
     __builtin__.set = sets.Set
 
 import fnmatch
     tempfile.mkstemp = mkstemp
     del mkstemp
 
-
+try:
+    # pre-2.7 doesn't have the memoryview() built-in
+    memoryview
+except NameError:
+    class memoryview:
+        from types import SliceType
+        def __init__(self, obj):
+            # wrapping buffer in () keeps the fixer from changing it
+            self.obj = (buffer)(obj)
+        def __getitem__(self, indx):
+            if isinstance(indx, self.SliceType):
+                return self.obj[indx.start:indx.stop]
+            else:
+                return self.obj[indx]
+    import __builtin__
+    __builtin__.memoryview = memoryview
 
 
 # Local Variables:

src/engine/SCons/compat/_scons_sets.py

 
         This is the keys iterator for the underlying dict.
         """
-        return self._data.iterkeys()
+        # Wrapping name in () prevents fixer from "fixing" this
+        return (self._data.iterkeys)()
 
     # Three-way comparison is not supported.  However, because __eq__ is
     # tried before __cmp__, if Set x == Set y, x.__eq__(y) returns True and

src/engine/SCons/compat/_scons_sets15.py

-#
-# A Set class that works all the way back to Python 1.5.  From:
-#
-#       Python Cookbook:  Yet another Set class for Python
-#       http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/106469
-#       Goncalo Rodriques
-#
-#       This is a pure Pythonic implementation of a set class.  The syntax
-#       and methods implemented are, for the most part, borrowed from
-#       PEP 218 by Greg Wilson.
-#
-# Note that this class violates the formal definition of a set() by adding
-# a __getitem__() method so we can iterate over a set's elements under
-# Python 1.5 and 2.1, which don't support __iter__() and iterator types.
-#
-
-class Set:
-    """The set class. It can contain mutable objects."""
-
-    def __init__(self, seq = None):
-        """The constructor. It can take any object giving an iterator as an optional
-        argument to populate the new set."""
-        self.elems = []
-        if seq:
-            for elem in seq:
-                if elem not in self.elems:
-                    hash(elem)
-                    self.elems.append(elem)
-
-    def __str__(self):
-        return "set([%s])" % ", ".join(map(str, self.elems))
-
-
-    def copy(self):
-        """Shallow copy of a set object."""
-        return Set(self.elems)
-
-    def __contains__(self, elem):
-        return elem in self.elems
-
-    def __len__(self):
-        return len(self.elems)
-
-    def __getitem__(self, index):
-        # Added so that Python 1.5 can iterate over the elements.
-        # The cookbook recipe's author didn't like this because there
-        # really isn't any order in a set object, but this is necessary
-        # to make the class work well enough for our purposes.
-        return self.elems[index]
-
-    def items(self):
-        """Returns a list of the elements in the set."""
-        return self.elems
-
-    def add(self, elem):
-        """Add one element to the set."""
-        if elem not in self.elems:
-            hash(elem)
-            self.elems.append(elem)
-
-    def remove(self, elem):
-        """Remove an element from the set. Return an error if elem is not in the set."""
-        try:
-            self.elems.remove(elem)
-        except ValueError:
-            raise LookupError, "Object %s is not a member of the set." % str(elem)
-
-    def discard(self, elem):
-        """Remove an element from the set. Do nothing if elem is not in the set."""
-        try:
-            self.elems.remove(elem)
-        except ValueError:
-            pass
-
-    def sort(self, func=cmp):
-        self.elems.sort(func)
-
-    #Define an iterator for a set.
-    def __iter__(self):
-        return iter(self.elems)
-
-    #The basic binary operations with sets.
-    def __or__(self, other):
-        """Union of two sets."""
-        ret = self.copy()
-        for elem in other.elems:
-            if elem not in ret:
-                ret.elems.append(elem)
-        return ret
-
-    def __sub__(self, other):
-        """Difference of two sets."""
-        ret = self.copy()
-        for elem in other.elems:
-            ret.discard(elem)
-        return ret
-
-    def __and__(self, other):
-        """Intersection of two sets."""
-        ret = Set()
-        for elem in self.elems:
-            if elem in other.elems:
-                ret.elems.append(elem)
-        return ret
-
-    def __add__(self, other):
-        """Symmetric difference of two sets."""
-        ret = Set()
-        temp = other.copy()
-        for elem in self.elems:
-            if elem in temp.elems:
-                temp.elems.remove(elem)
-            else:
-                ret.elems.append(elem)
-        #Add remaining elements.
-        for elem in temp.elems:
-                ret.elems.append(elem)
-        return ret
-
-    def __mul__(self, other):
-        """Cartesian product of two sets."""
-        ret = Set()
-        for elemself in self.elems:
-            x = list(map(lambda other, s=elemself: (s, other), other.elems))
-            ret.elems.extend(x)
-        return ret
-
-    #Some of the binary comparisons.
-    def __lt__(self, other):
-        """Returns 1 if the lhs set is contained but not equal to the rhs set."""
-        if len(self.elems) < len(other.elems):
-            temp = other.copy()
-            for elem in self.elems:
-                if elem in temp.elems:
-                    temp.remove(elem)
-                else:
-                    return 0
-            return len(temp.elems) == 0
-        else:
-            return 0
-
-    def __le__(self, other):
-        """Returns 1 if the lhs set is contained in the rhs set."""
-        if len(self.elems) <= len(other.elems):
-            ret = 1
-            for elem in self.elems:
-                if elem not in other.elems:
-                    ret = 0
-                    break
-            return ret
-        else:
-            return 0
-
-    def __eq__(self, other):
-        """Returns 1 if the sets are equal."""
-        if len(self.elems) != len(other.elems):
-            return 0
-        else:
-            return len(self - other) == 0
-
-    def __cmp__(self, other):
-        """Returns 1 if the sets are equal."""
-        if self.__lt__(other):
-            return -1
-        elif other.__lt__(self):
-            return 1
-        else:
-            return 0
-
-# Local Variables:
-# tab-width:4
-# indent-tabs-mode:nil
-# End:
-# vim: set expandtab tabstop=4 shiftwidth=4:

src/engine/SCons/compat/_scons_subprocess.py

 except AttributeError:
     try:
         types.StringTypes = (str, unicode)
-    except AttributeError:
+    except NameError:
         types.StringTypes = (str,)
-    def is_string(obj):
-        return type(obj) in types.StringTypes
-else:
-    def is_string(obj):
-        return isinstance(obj, types.StringTypes)
+def is_string(obj):
+    return isinstance(obj, types.StringTypes)
 
 _active = []
 
 
 
         def _close_fds(self, but):
-            for i in xrange(3, MAXFD):
+            for i in range(3, MAXFD):
                 if i == but:
                     continue
                 try:
                     # When select has indicated that the file is writable,
                     # we can write up to PIPE_BUF bytes without risk
                     # blocking.  POSIX defines PIPE_BUF >= 512
-                    bytes_written = os.write(self.stdin.fileno(), buffer(input, input_offset, 512))
+                    m = memoryview(input)[input_offset:input_offset+512]
+                    bytes_written = os.write(self.stdin.fileno(), m)
                     input_offset = input_offset + bytes_written
                     if input_offset >= len(input):
                         self.stdin.close()

src/engine/SCons/compat/builtins.py

         argument sequence.
         """
         result = []
-        for i in xrange(min(list(map(len, lists)))):
+        for i in range(min(list(map(len, lists)))):
             result.append(tuple([l[i] for l in lists]))
         return result
     __builtin__.zip = zip
 
-
-
 #if sys.version_info[:3] in ((2, 2, 0), (2, 2, 1)):
 #    def lstrip(s, c=string.whitespace):
 #        while s and s[0] in c:

src/engine/SCons/dblite.py

     return key in self._dict
 
   def iterkeys(self):
-    return self._dict.iterkeys()
+    # Wrapping name in () prevents fixer from "fixing" this
+    return (self._dict.iterkeys)()
 
   __iter__ = iterkeys
 

src/script/scons-time.py

 
         min_y = self.get_min_y()
         max_y = self.max_graph_value(self.get_max_y())
-        range = max_y - min_y
-        incr = range / 10.0
+        incr = (max_y - min_y) / 10.0
         start = min_y + (max_y / 2.0) + (2.0 * incr)
-        position = [ start - (i * incr) for i in xrange(5) ]
+        position = [ start - (i * incr) for i in range(5) ]
 
         inx = 1
         for line in self.lines:
             except ValueError:
                 result.append(int(n))
             else:
-                result.extend(range(int(x), int(y)+1))
+                result.extend(list(range(int(x), int(y)+1)))
         return result
 
     def scons_path(self, dir):

src/script/sconsign.py

     except AttributeError:
         return None
     result = []
-    for i in xrange(len(bkids)):
+    for i in range(len(bkids)):
         result.append(nodeinfo_string(bkids[i], bkidsigs[i], "        "))
     if result == []:
         return None

test/Batch/action-changed.py

 sep = sys.argv.index('--')
 targets = sys.argv[1:sep]
 sources = sys.argv[sep+1:]
-for i in xrange(len(targets)):
+for i in range(len(targets)):
     t = targets[i]
     s = sources[i]
     fp = open(t, 'wb')

test/KeyboardInterrupt.py

 
 all = []
 
-for i in xrange(40):
+for i in range(40):
     all.extend(Object('toto%5d' % i, 'toto.c'))
 
 all.extend(Command( 'broken', 'toto.c', explode))

test/SWIG/build-dir.py

     
     %pythoncode %{
     def __iter__(self):
-        for i in xrange(len(self)):
+        for i in range(len(self)):
             yield self[i]
     %}
   }

test/option/md5-chunksize.py

 Requires(f2, f1)
 """ % locals())
 
-test.write('f1.in', str(range(10)))
-test.write('f2.in', str(range(100000)))
+test.write('f1.in', str(list(range(10))))
+test.write('f2.in', str(list(range(100000))))
 
 expected_stdout = test.wrap_stdout("""\
 %(_python_)s build.py f1.out f1.in

test/scons-time/func/chdir.py

 """
 
 expect = []
-for i in xrange(9):
+for i in range(9):
     i = str(i)
     test.subdir(i)
     test.profile_data('profs/foo-%s.prof' % i, '%s/prof.py' % i, '_main', input)

test/scons-time/func/glob.py

 """
 
 expect = []
-for i in xrange(9):
+for i in range(9):
     test.subdir(str(i))
     test.profile_data('foo-%s.prof' % i, '%s/prof.py' % i, '_main', input)
     expect.append((r'\d.\d\d\d %s/prof\.py:1\(_main\)' + '\n') % i)

test/scons-time/func/prefix.py

 foo_lines = []
 bar_lines = []
 
-for i in xrange(2):
+for i in range(2):
     test.profile_data('foo-%s.prof' % i, 'prof.py', '_main', input)
     foo_lines.append(r'\d.\d\d\d prof\.py:1\(_main\)' + '\n')
 
-for i in xrange(4):
+for i in range(4):
     test.profile_data('bar-%s.prof' % i, 'prof.py', '_main', input)
     bar_lines.append(r'\d.\d\d\d prof\.py:1\(_main\)' + '\n')
 

test/scons-time/func/tail.py

 """
 
 expect = []
-for i in xrange(9):
+for i in range(9):
     test.subdir(str(i))
     test.profile_data('foo-%s.prof' % i, '%s/prof.py' % i, '_main', input)
     expect.append((r'\d.\d\d\d %s/prof\.py:1\(_main\)' + '\n') % i)

test/scons-time/mem/chdir.py

 
 line_fmt = '        1000         2000         3000         4000    %s\n'
 
-for i in xrange(9):
+for i in range(9):
     logfile_name = os.path.join('logs', 'foo-%s.log' % i)
     test.fake_logfile(logfile_name)
     lines.append(line_fmt % logfile_name)

test/scons-time/mem/glob.py

 
 line_fmt = '        1000         2000         3000         4000    %s\n'
 
-for i in xrange(9):
+for i in range(9):
     logfile_name = 'foo-%s.log' % i
     test.fake_logfile(logfile_name)
     lines.append(line_fmt % logfile_name)

test/scons-time/mem/prefix.py

 foo_lines = [ header ]
 bar_lines = [ header ]
 
-for i in xrange(3):
+for i in range(3):
     logfile_name = os.path.join('foo-%s.log' % i)
     test.fake_logfile(logfile_name)
     foo_lines.append(line_fmt % logfile_name)

test/scons-time/mem/tail.py

 
 line_fmt = '        1000         2000         3000         4000    %s\n'
 
-for i in xrange(9):
+for i in range(9):
     logfile_name = 'foo-%s.log' % i
     test.fake_logfile(logfile_name)
     lines.append(line_fmt % logfile_name)

test/scons-time/obj/chdir.py

 
 line_fmt = '       1101%(i)s        1102%(i)s        1103%(i)s        1104%(i)s    %(logfile_name)s\n'
 
-for i in xrange(9):
+for i in range(9):
     logfile_name = os.path.join('logs', 'foo-%s.log' % i)
     test.fake_logfile(logfile_name, i)
     lines.append(line_fmt % locals())

test/scons-time/obj/glob.py

 
 line_fmt = '        601%(i)s         602%(i)s         603%(i)s         604%(i)s    %(logfile_name)s\n'
 
-for i in xrange(9):
+for i in range(9):
     logfile_name = 'foo-%s.log' % i
     test.fake_logfile(logfile_name, i)
     lines.append(line_fmt % locals())

test/scons-time/obj/prefix.py

 foo_lines = [ header ]
 bar_lines = [ header ]
 
-for i in xrange(3):
+for i in range(3):
     logfile_name = os.path.join('foo-%s.log' % i)
     test.fake_logfile(logfile_name)
     foo_lines.append(line_fmt % logfile_name)

test/scons-time/obj/tail.py

 
 line_fmt = '       1501%(i)s        1502%(i)s        1503%(i)s        1504%(i)s    %(logfile_name)s\n'
 
-for i in xrange(9):
+for i in range(9):
     logfile_name = 'foo-%s.log' % i
     test.fake_logfile(logfile_name, i)
     lines.append(line_fmt % locals())

test/scons-time/time/chdir.py

 
 line_fmt = '   11.123456    22.234567    33.345678    44.456789    %s\n'
 
-for i in xrange(9):
+for i in range(9):
     logfile_name = os.path.join('logs', 'foo-%s.log' % i)
     test.fake_logfile(logfile_name)
     lines.append(line_fmt % logfile_name)

test/scons-time/time/empty.py

 line_fmt    = '   11.123456    22.234567    33.345678    44.456789    %s\n'
 empty_fmt   = '                                                       %s\n'
 
-for i in xrange(9):
+for i in range(9):
     logfile_name = 'foo-%s-0.log' % i
     if i == 5:
         test.write(test.workpath(logfile_name), "")

test/scons-time/time/glob.py

 
 line_fmt = '   11.123456    22.234567    33.345678    44.456789    %s\n'
 
-for i in xrange(9):
+for i in range(9):
     logfile_name = 'foo-%s.log' % i
     test.fake_logfile(logfile_name)
     lines.append(line_fmt % logfile_name)

test/scons-time/time/no-result.py

 
 lines = []
 
-for i in xrange(9):
+for i in range(9):
     logfile_name = 'foo-%s-0.log' % i
     if i == 5:
         test.write(test.workpath(logfile_name), "NO RESULTS HERE!\n")

test/scons-time/time/prefix.py

 foo_lines = [ header ]
 bar_lines = [ header ]
 
-for i in xrange(3):
+for i in range(3):
     logfile_name = os.path.join('foo-%s.log' % i)
     test.fake_logfile(logfile_name)
     foo_lines.append(line_fmt % logfile_name)

test/scons-time/time/tail.py

 
 line_fmt = '   11.123456    22.234567    33.345678    44.456789    %s\n'
 
-for i in xrange(9):
+for i in range(9):
     logfile_name = 'foo-%s.log' % i
     test.fake_logfile(logfile_name)
     lines.append(line_fmt % logfile_name)

timings/CPPPATH/SConstruct

 
 dir_count = int(ARGUMENTS['DIR_COUNT'])
 
-inc_list = [ Dir('inc_%04d' % t) for t in xrange(dir_count) ]
+inc_list = [ Dir('inc_%04d' % t) for t in range(dir_count) ]
 inc_list.append(Dir('include'))
 
 env = Environment(CPPPATH = inc_list)

timings/CPPPATH/TimeSCons-run.py

 
 test = TestSCons.TimeSCons(variables={'DIR_COUNT':813})
 
-for d in xrange(test.variables['DIR_COUNT']):
+for d in range(test.variables['DIR_COUNT']):
     test.subdir('inc_%04d' % d)
 
 test.main()

timings/hundred/SConstruct

     for t, s in zip(target, source):
         open(str(t),  'wb').write(open(str(s), 'rb').read())
 
-source_list = ['source_%04d' % t for t in xrange(target_count)]
-target_list = ['target_%04d' % t for t in xrange(target_count)]
+source_list = ['source_%04d' % t for t in range(target_count)]
+target_list = ['target_%04d' % t for t in range(target_count)]
 
 env = Environment()
 

timings/hundred/TimeSCons-run.py

 
 test = TestSCons.TimeSCons(variables={'TARGET_COUNT':139})
 
-for t in xrange(test.variables['TARGET_COUNT']):
+for t in range(test.variables['TARGET_COUNT']):
     open('source_%04d' % t, 'wb' ).write('contents\n')
 
 test.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.