]>
Commit | Line | Data |
---|---|---|
a4b74d05 CF |
1 | # |
2 | # Copyright (c) 2010-2017 Benjamin Peterson | |
3 | # | |
4 | # Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 | # of this software and associated documentation files (the "Software"), to deal | |
6 | # in the Software without restriction, including without limitation the rights | |
7 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 | # copies of the Software, and to permit persons to whom the Software is | |
9 | # furnished to do so, subject to the following conditions: | |
10 | # | |
11 | # The above copyright notice and this permission notice shall be included in all | |
12 | # copies or substantial portions of the Software. | |
13 | # | |
14 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
20 | # SOFTWARE. | |
21 | # | |
22 | ||
23 | # | |
24 | # This code is taken from the six python2 to python3 compatibility module | |
25 | # | |
26 | ||
27 | import sys | |
28 | ||
29 | PY2 = sys.version_info[0] == 2 | |
30 | PY3 = sys.version_info[0] == 3 | |
31 | ||
32 | def add_metaclass(metaclass): | |
33 | """Class decorator for creating a class with a metaclass.""" | |
34 | def wrapper(cls): | |
35 | orig_vars = cls.__dict__.copy() | |
36 | slots = orig_vars.get('__slots__') | |
37 | if slots is not None: | |
38 | if isinstance(slots, str): | |
39 | slots = [slots] | |
40 | for slots_var in slots: | |
41 | orig_vars.pop(slots_var) | |
42 | orig_vars.pop('__dict__', None) | |
43 | orig_vars.pop('__weakref__', None) | |
44 | return metaclass(cls.__name__, cls.__bases__, orig_vars) | |
45 | return wrapper | |
46 | ||
47 | if PY3: | |
48 | import builtins | |
49 | exec_ = getattr(builtins,'exec') | |
50 | ||
51 | def reraise(tp, value, tb=None): | |
52 | try: | |
53 | if value is None: | |
54 | value = tp() | |
55 | if value.__traceback__ is not tb: | |
56 | raise value.with_traceback(tb) | |
57 | raise value | |
58 | finally: | |
59 | value = None | |
60 | tb = None | |
61 | ||
62 | else: | |
63 | def exec_(_code_, _globs_=None, _locs_=None): | |
64 | """Execute code in a namespace.""" | |
65 | if _globs_ is None: | |
66 | frame = sys._getframe(1) | |
67 | _globs_ = frame.f_globals | |
68 | if _locs_ is None: | |
69 | _locs_ = frame.f_locals | |
70 | del frame | |
71 | elif _locs_ is None: | |
72 | _locs_ = _globs_ | |
73 | exec("""exec _code_ in _globs_, _locs_""") | |
74 | ||
75 | exec_("""def reraise(tp, value, tb=None): | |
76 | try: | |
77 | raise tp, value, tb | |
78 | finally: | |
79 | tb = None | |
80 | """) |