|
| 1 | +# SPDX-FileCopyrightText: 2010-2024 Benjamin Peterson |
| 2 | +# SPDX-FileCopyrightText: 2026 Mitogen authors <https://github.com/mitogen-hq> |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +# Source: https://github.com/benjaminp/six/blob/1.17.0/six.py |
| 5 | +# |
| 6 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +# of this software and associated documentation files (the "Software"), to deal |
| 8 | +# in the Software without restriction, including without limitation the rights |
| 9 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +# copies of the Software, and to permit persons to whom the Software is |
| 11 | +# furnished to do so, subject to the following conditions: |
| 12 | +# |
| 13 | +# The above copyright notice and this permission notice shall be included in all |
| 14 | +# copies or substantial portions of the Software. |
| 15 | +# |
| 16 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +# SOFTWARE. |
| 23 | + |
| 24 | +from __future__ import absolute_import |
| 25 | + |
| 26 | +import sys |
| 27 | +import types |
| 28 | + |
| 29 | +if sys.version_info >= (3, 3): |
| 30 | + from shlex import quote as shlex_quote |
| 31 | +else: |
| 32 | + from pipes import quote as shlex_quote |
| 33 | + |
| 34 | + |
| 35 | +if sys.version_info >= (3, 0): |
| 36 | + exec_ = getattr(__import__('builtins'), 'exec') |
| 37 | + |
| 38 | + string_types = (str,) |
| 39 | + |
| 40 | + def reraise(tp, value, tb=None): |
| 41 | + try: |
| 42 | + if value is None: |
| 43 | + value = tp() |
| 44 | + if value.__traceback__ is not tb: |
| 45 | + raise value.with_traceback(tb) |
| 46 | + raise value |
| 47 | + finally: |
| 48 | + value = None |
| 49 | + tb = None |
| 50 | + |
| 51 | +else: |
| 52 | + string_types = (basestring,) |
| 53 | + |
| 54 | + def exec_(_code_, _globs_=None, _locs_=None): |
| 55 | + """Execute code in a namespace.""" |
| 56 | + if _globs_ is None: |
| 57 | + frame = sys._getframe(1) |
| 58 | + _globs_ = frame.f_globals |
| 59 | + if _locs_ is None: |
| 60 | + _locs_ = frame.f_locals |
| 61 | + del frame |
| 62 | + elif _locs_ is None: |
| 63 | + _locs_ = _globs_ |
| 64 | + exec("""exec _code_ in _globs_, _locs_""") |
| 65 | + |
| 66 | + exec_("""def reraise(tp, value, tb=None): |
| 67 | + try: |
| 68 | + raise tp, value, tb |
| 69 | + finally: |
| 70 | + tb = None |
| 71 | +""") |
| 72 | + |
| 73 | + |
| 74 | +def with_metaclass(meta, *bases): |
| 75 | + """Create a base class with a metaclass.""" |
| 76 | + # This requires a bit of explanation: the basic idea is to make a dummy |
| 77 | + # metaclass for one level of class instantiation that replaces itself with |
| 78 | + # the actual metaclass. |
| 79 | + class metaclass(type): |
| 80 | + |
| 81 | + def __new__(cls, name, this_bases, d): |
| 82 | + if sys.version_info[:2] >= (3, 7): |
| 83 | + # This version introduced PEP 560 that requires a bit |
| 84 | + # of extra care (we mimic what is done by __build_class__). |
| 85 | + resolved_bases = types.resolve_bases(bases) |
| 86 | + if resolved_bases is not bases: |
| 87 | + d['__orig_bases__'] = bases |
| 88 | + else: |
| 89 | + resolved_bases = bases |
| 90 | + return meta(name, resolved_bases, d) |
| 91 | + |
| 92 | + @classmethod |
| 93 | + def __prepare__(cls, name, this_bases): |
| 94 | + return meta.__prepare__(name, bases) |
| 95 | + return type.__new__(metaclass, 'temporary_class', (), {}) |
0 commit comments