]> git.proxmox.com Git - mirror_frr.git/blame - tests/helpers/python/frrsix.py
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / tests / helpers / python / frrsix.py
CommitLineData
acddc0ed 1# SPDX-License-Identifier: MIT
a4b74d05
CF
2#
3# Copyright (c) 2010-2017 Benjamin Peterson
4#
a4b74d05
CF
5
6#
7# This code is taken from the six python2 to python3 compatibility module
8#
9
10import sys
11
12PY2 = sys.version_info[0] == 2
13PY3 = sys.version_info[0] == 3
14
701a0192 15
a4b74d05
CF
16def add_metaclass(metaclass):
17 """Class decorator for creating a class with a metaclass."""
701a0192 18
a4b74d05
CF
19 def wrapper(cls):
20 orig_vars = cls.__dict__.copy()
701a0192 21 slots = orig_vars.get("__slots__")
a4b74d05
CF
22 if slots is not None:
23 if isinstance(slots, str):
24 slots = [slots]
25 for slots_var in slots:
26 orig_vars.pop(slots_var)
701a0192 27 orig_vars.pop("__dict__", None)
28 orig_vars.pop("__weakref__", None)
a4b74d05 29 return metaclass(cls.__name__, cls.__bases__, orig_vars)
701a0192 30
a4b74d05
CF
31 return wrapper
32
701a0192 33
a4b74d05
CF
34if PY3:
35 import builtins
701a0192 36
37 exec_ = getattr(builtins, "exec")
a4b74d05
CF
38
39 def reraise(tp, value, tb=None):
40 try:
41 if value is None:
42 value = tp()
43 if value.__traceback__ is not tb:
44 raise value.with_traceback(tb)
45 raise value
46 finally:
47 value = None
48 tb = None
49
701a0192 50
a4b74d05 51else:
701a0192 52
a4b74d05
CF
53 def exec_(_code_, _globs_=None, _locs_=None):
54 """Execute code in a namespace."""
55 if _globs_ is None:
56 frame = sys._getframe(1)
57 _globs_ = frame.f_globals
58 if _locs_ is None:
59 _locs_ = frame.f_locals
60 del frame
61 elif _locs_ is None:
62 _locs_ = _globs_
63 exec("""exec _code_ in _globs_, _locs_""")
64
701a0192 65 exec_(
66 """def reraise(tp, value, tb=None):
a4b74d05
CF
67 try:
68 raise tp, value, tb
69 finally:
70 tb = None
701a0192 71"""
72 )