Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add backport of Python 2.6+'s `collections.namedtuple` and use it for…

… the `RevCache` tuple.
  • Loading branch information...
commit d30c3ab3b963608ad46aba1973451ba20c09489c 1 parent 70713b1
@hvr authored
Showing with 164 additions and 45 deletions.
  1. +7 −45 tracext/git/PyGIT.py
  2. +157 −0 tracext/git/future27.py
View
52 tracext/git/PyGIT.py
@@ -14,6 +14,8 @@
from __future__ import with_statement
+from future27 import namedtuple
+
import os, re, sys, time, weakref
from collections import deque
from functools import partial
@@ -149,48 +151,6 @@ def getInstance(self):
% (("","weak ")[is_weak], id(self.__inst), self.__repo))
return self.__inst
-# generated with Python 2.6's collections.namedtuple -- we can't depend on 2.6 yet...
-class RevCache(tuple):
- 'RevCache(youngest_rev, oldest_rev, rev_dict, tag_set, srev_dict, branch_dict)'
-
- __slots__ = ()
- _fields = ('youngest_rev', 'oldest_rev', 'rev_dict', 'tag_set', 'srev_dict', 'branch_dict')
-
- def __new__(cls, youngest_rev, oldest_rev, rev_dict, tag_set, srev_dict, branch_dict):
- return tuple.__new__(cls, (youngest_rev, oldest_rev, rev_dict, tag_set, srev_dict, branch_dict))
-
- @classmethod
- def _make(cls, iterable, new=tuple.__new__, len=len):
- 'Make a new RevCache object from a sequence or iterable'
- result = new(cls, iterable)
- if len(result) != 6:
- raise TypeError('Expected 6 arguments, got %d' % len(result))
- return result
-
- def __repr__(self):
- return 'RevCache(youngest_rev=%r, oldest_rev=%r, rev_dict=%r, tag_set=%r, srev_dict=%r, branch_dict=%r)' % self
-
- def _asdict(t):
- 'Return a new dict which maps field names to their values'
- return {'youngest_rev': t[0], 'oldest_rev': t[1], 'rev_dict': t[2], 'tag_set': t[3], 'srev_dict': t[4], 'branch_dict': t[5]}
-
- def _replace(self, **kwds):
- 'Return a new RevCache object replacing specified fields with new values'
- result = self._make(map(kwds.pop, ('youngest_rev', 'oldest_rev', 'rev_dict', 'tag_set', 'srev_dict', 'branch_dict'), self))
- if kwds:
- raise ValueError('Got unexpected field names: %r' % kwds.keys())
- return result
-
- def __getnewargs__(self):
- return tuple(self)
-
- youngest_rev = property(itemgetter(0))
- oldest_rev = property(itemgetter(1))
- rev_dict = property(itemgetter(2))
- tag_set = property(itemgetter(3))
- srev_dict = property(itemgetter(4))
- branch_dict = property(itemgetter(5))
-
class Storage(object):
"""
@@ -199,6 +159,8 @@ class Storage(object):
__SREV_MIN = 4 # minimum short-rev length
+ RevCache = namedtuple('RevCache', 'youngest_rev oldest_rev rev_dict tag_set srev_dict branch_dict')
+
@staticmethod
def __rev_key(rev):
assert len(rev) >= 4
@@ -329,7 +291,7 @@ def get_rev_cache(self):
may rebuild cache on the fly if required
- returns RevCache tupel
+ returns RevCache tuple
"""
with self.__rev_cache_lock:
@@ -423,7 +385,7 @@ def __rev_reuse(rev):
new_sdb = tmp
# atomically update self.__rev_cache
- self.__rev_cache = RevCache(youngest, oldest, new_db, new_tags, new_sdb, new_branches)
+ self.__rev_cache = Storage.RevCache(youngest, oldest, new_db, new_tags, new_sdb, new_branches)
ts1 = time.time()
self.logger.debug("rebuilt commit tree db for %d with %d entries (took %.1f ms)"
% (id(self), len(new_db), 1000*(ts1-ts0)))
@@ -433,7 +395,7 @@ def __rev_reuse(rev):
return self.__rev_cache
# with self.__rev_cache_lock
- # see RevCache namedtupel
+ # see RevCache namedtuple
rev_cache = property(get_rev_cache)
def _get_branches(self):
View
157 tracext/git/future27.py
@@ -0,0 +1,157 @@
+# Copyright (C) 2001-2010 Python Software Foundation; All Rights Reserved
+
+# This file contains backports for Python 2.5 based on Python 2.7's standard library
+
+__all__ = ['namedtuple']
+
+from operator import itemgetter as _itemgetter
+from keyword import iskeyword as _iskeyword
+import sys as _sys
+
+#################################################################
+# collections.namedtuple
+
+try:
+ # try to use the standard library's namedtuple...
+ from collections import namedtuple
+
+except ImportError:
+ # use namedtuple backport
+
+ # the factory function
+ def namedtuple(typename, field_names, verbose=False):
+ """Returns a new subclass of tuple with named fields.
+
+ >>> Point = namedtuple('Point', 'x y')
+ >>> Point.__doc__ # docstring for the new class
+ 'Point(x, y)'
+ >>> p = Point(11, y=22) # instantiate with positional args or keywords
+ >>> p[0] + p[1] # indexable like a plain tuple
+ 33
+ >>> x, y = p # unpack like a regular tuple
+ >>> x, y
+ (11, 22)
+ >>> p.x + p.y # fields also accessable by name
+ 33
+ >>> d = p._asdict() # convert to a dictionary
+ >>> d['x']
+ 11
+ >>> Point(**d) # convert from a dictionary
+ Point(x=11, y=22)
+ >>> p._replace(x=100) # _replace() is like str.replace() but targets named fields
+ Point(x=100, y=22)
+
+ """
+
+ # Parse and validate the field names. Validation serves two purposes,
+ # generating informative error messages and preventing template injection attacks.
+ if isinstance(field_names, basestring):
+ field_names = field_names.replace(',', ' ').split() # names separated by whitespace and/or commas
+ field_names = tuple(map(str, field_names))
+ for name in (typename,) + field_names:
+ if not all(c.isalnum() or c=='_' for c in name):
+ raise ValueError('Type names and field names can only contain alphanumeric characters and underscores: %r' % name)
+ if _iskeyword(name):
+ raise ValueError('Type names and field names cannot be a keyword: %r' % name)
+ if name[0].isdigit():
+ raise ValueError('Type names and field names cannot start with a number: %r' % name)
+ seen_names = set()
+ for name in field_names:
+ if name.startswith('_'):
+ raise ValueError('Field names cannot start with an underscore: %r' % name)
+ if name in seen_names:
+ raise ValueError('Encountered duplicate field name: %r' % name)
+ seen_names.add(name)
+
+ # Create and fill-in the class template
+ numfields = len(field_names)
+ argtxt = repr(field_names).replace("'", "")[1:-1] # tuple repr without parens or quotes
+ reprtxt = ', '.join('%s=%%r' % name for name in field_names)
+ dicttxt = ', '.join('%r: t[%d]' % (name, pos) for pos, name in enumerate(field_names))
+ template = '''class %(typename)s(tuple):
+ '%(typename)s(%(argtxt)s)' \n
+ __slots__ = () \n
+ _fields = %(field_names)r \n
+ def __new__(cls, %(argtxt)s):
+ return tuple.__new__(cls, (%(argtxt)s)) \n
+ @classmethod
+ def _make(cls, iterable, new=tuple.__new__, len=len):
+ 'Make a new %(typename)s object from a sequence or iterable'
+ result = new(cls, iterable)
+ if len(result) != %(numfields)d:
+ raise TypeError('Expected %(numfields)d arguments, got %%d' %% len(result))
+ return result \n
+ def __repr__(self):
+ return '%(typename)s(%(reprtxt)s)' %% self \n
+ def _asdict(t):
+ 'Return a new dict which maps field names to their values'
+ return {%(dicttxt)s} \n
+ def _replace(self, **kwds):
+ 'Return a new %(typename)s object replacing specified fields with new values'
+ result = self._make(map(kwds.pop, %(field_names)r, self))
+ if kwds:
+ raise ValueError('Got unexpected field names: %%r' %% kwds.keys())
+ return result \n
+ def __getnewargs__(self):
+ return tuple(self) \n\n''' % locals()
+ for i, name in enumerate(field_names):
+ template += ' %s = property(itemgetter(%d))\n' % (name, i)
+ if verbose:
+ print template
+
+ # Execute the template string in a temporary namespace and
+ # support tracing utilities by setting a value for frame.f_globals['__name__']
+ namespace = dict(itemgetter=_itemgetter, __name__='namedtuple_%s' % typename)
+ try:
+ exec template in namespace
+ except SyntaxError, e:
+ raise SyntaxError(e.message + ':\n' + template)
+ result = namespace[typename]
+
+ # For pickling to work, the __module__ variable needs to be set to the frame
+ # where the named tuple is created. Bypass this step in enviroments where
+ # sys._getframe is not defined (Jython for example).
+ if hasattr(_sys, '_getframe'):
+ result.__module__ = _sys._getframe(1).f_globals['__name__']
+
+ return result
+
+
+
+############################################################################
+# unit test
+
+if __name__ == '__main__':
+ # verify that instances can be pickled
+ from cPickle import loads, dumps
+ Point = namedtuple('Point', 'x, y', True)
+ p = Point(x=10, y=20)
+ assert p == loads(dumps(p))
+
+ # test and demonstrate ability to override methods
+ class Point(namedtuple('Point', 'x y')):
+ __slots__ = ()
+ @property
+ def hypot(self):
+ return (self.x ** 2 + self.y ** 2) ** 0.5
+ def __str__(self):
+ return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot)
+
+ for p in Point(3, 4), Point(14, 5/7.):
+ print p
+
+ class Point(namedtuple('Point', 'x y')):
+ 'Point class with optimized _make() and _replace() without error-checking'
+ __slots__ = ()
+ _make = classmethod(tuple.__new__)
+ def _replace(self, _map=map, **kwds):
+ return self._make(_map(kwds.get, ('x', 'y'), self))
+
+ print Point(11, 22)._replace(x=100)
+
+ Point3D = namedtuple('Point3D', Point._fields + ('z',))
+ print Point3D.__doc__
+
+ import doctest
+ TestResults = namedtuple('TestResults', 'failed attempted')
+ print TestResults(*doctest.testmod())
Please sign in to comment.
Something went wrong with that request. Please try again.