diff options
Diffstat (limited to 'bitbake/lib/concurrent/futures/_compat.py')
-rw-r--r-- | bitbake/lib/concurrent/futures/_compat.py | 101 |
1 files changed, 0 insertions, 101 deletions
diff --git a/bitbake/lib/concurrent/futures/_compat.py b/bitbake/lib/concurrent/futures/_compat.py deleted file mode 100644 index 11462326b5..0000000000 --- a/bitbake/lib/concurrent/futures/_compat.py +++ /dev/null | |||
@@ -1,101 +0,0 @@ | |||
1 | from keyword import iskeyword as _iskeyword | ||
2 | from operator import itemgetter as _itemgetter | ||
3 | import sys as _sys | ||
4 | |||
5 | |||
6 | def namedtuple(typename, field_names): | ||
7 | """Returns a new subclass of tuple with named fields. | ||
8 | |||
9 | >>> Point = namedtuple('Point', 'x y') | ||
10 | >>> Point.__doc__ # docstring for the new class | ||
11 | 'Point(x, y)' | ||
12 | >>> p = Point(11, y=22) # instantiate with positional args or keywords | ||
13 | >>> p[0] + p[1] # indexable like a plain tuple | ||
14 | 33 | ||
15 | >>> x, y = p # unpack like a regular tuple | ||
16 | >>> x, y | ||
17 | (11, 22) | ||
18 | >>> p.x + p.y # fields also accessable by name | ||
19 | 33 | ||
20 | >>> d = p._asdict() # convert to a dictionary | ||
21 | >>> d['x'] | ||
22 | 11 | ||
23 | >>> Point(**d) # convert from a dictionary | ||
24 | Point(x=11, y=22) | ||
25 | >>> p._replace(x=100) # _replace() is like str.replace() but targets named fields | ||
26 | Point(x=100, y=22) | ||
27 | |||
28 | """ | ||
29 | |||
30 | # Parse and validate the field names. Validation serves two purposes, | ||
31 | # generating informative error messages and preventing template injection attacks. | ||
32 | if isinstance(field_names, basestring): | ||
33 | field_names = field_names.replace(',', ' ').split() # names separated by whitespace and/or commas | ||
34 | field_names = tuple(map(str, field_names)) | ||
35 | for name in (typename,) + field_names: | ||
36 | if not all(c.isalnum() or c=='_' for c in name): | ||
37 | raise ValueError('Type names and field names can only contain alphanumeric characters and underscores: %r' % name) | ||
38 | if _iskeyword(name): | ||
39 | raise ValueError('Type names and field names cannot be a keyword: %r' % name) | ||
40 | if name[0].isdigit(): | ||
41 | raise ValueError('Type names and field names cannot start with a number: %r' % name) | ||
42 | seen_names = set() | ||
43 | for name in field_names: | ||
44 | if name.startswith('_'): | ||
45 | raise ValueError('Field names cannot start with an underscore: %r' % name) | ||
46 | if name in seen_names: | ||
47 | raise ValueError('Encountered duplicate field name: %r' % name) | ||
48 | seen_names.add(name) | ||
49 | |||
50 | # Create and fill-in the class template | ||
51 | numfields = len(field_names) | ||
52 | argtxt = repr(field_names).replace("'", "")[1:-1] # tuple repr without parens or quotes | ||
53 | reprtxt = ', '.join('%s=%%r' % name for name in field_names) | ||
54 | dicttxt = ', '.join('%r: t[%d]' % (name, pos) for pos, name in enumerate(field_names)) | ||
55 | template = '''class %(typename)s(tuple): | ||
56 | '%(typename)s(%(argtxt)s)' \n | ||
57 | __slots__ = () \n | ||
58 | _fields = %(field_names)r \n | ||
59 | def __new__(_cls, %(argtxt)s): | ||
60 | return _tuple.__new__(_cls, (%(argtxt)s)) \n | ||
61 | @classmethod | ||
62 | def _make(cls, iterable, new=tuple.__new__, len=len): | ||
63 | 'Make a new %(typename)s object from a sequence or iterable' | ||
64 | result = new(cls, iterable) | ||
65 | if len(result) != %(numfields)d: | ||
66 | raise TypeError('Expected %(numfields)d arguments, got %%d' %% len(result)) | ||
67 | return result \n | ||
68 | def __repr__(self): | ||
69 | return '%(typename)s(%(reprtxt)s)' %% self \n | ||
70 | def _asdict(t): | ||
71 | 'Return a new dict which maps field names to their values' | ||
72 | return {%(dicttxt)s} \n | ||
73 | def _replace(_self, **kwds): | ||
74 | 'Return a new %(typename)s object replacing specified fields with new values' | ||
75 | result = _self._make(map(kwds.pop, %(field_names)r, _self)) | ||
76 | if kwds: | ||
77 | raise ValueError('Got unexpected field names: %%r' %% kwds.keys()) | ||
78 | return result \n | ||
79 | def __getnewargs__(self): | ||
80 | return tuple(self) \n\n''' % locals() | ||
81 | for i, name in enumerate(field_names): | ||
82 | template += ' %s = _property(_itemgetter(%d))\n' % (name, i) | ||
83 | |||
84 | # Execute the template string in a temporary namespace and | ||
85 | # support tracing utilities by setting a value for frame.f_globals['__name__'] | ||
86 | namespace = dict(_itemgetter=_itemgetter, __name__='namedtuple_%s' % typename, | ||
87 | _property=property, _tuple=tuple) | ||
88 | try: | ||
89 | exec(template, namespace) | ||
90 | except SyntaxError: | ||
91 | e = _sys.exc_info()[1] | ||
92 | raise SyntaxError(e.message + ':\n' + template) | ||
93 | result = namespace[typename] | ||
94 | |||
95 | # For pickling to work, the __module__ variable needs to be set to the frame | ||
96 | # where the named tuple is created. Bypass this step in enviroments where | ||
97 | # sys._getframe is not defined (Jython for example). | ||
98 | if hasattr(_sys, '_getframe'): | ||
99 | result.__module__ = _sys._getframe(1).f_globals.get('__name__', '__main__') | ||
100 | |||
101 | return result | ||