|
| 1 | +from __future__ import absolute_import, division, print_function, unicode_literals |
| 2 | + |
| 3 | +import os |
| 4 | +import six |
| 5 | +import dill |
| 6 | + |
| 7 | + |
| 8 | +class FakeType(object): |
| 9 | + @classmethod |
| 10 | + def _safe_repr(cls, v): |
| 11 | + try: |
| 12 | + return repr(v) |
| 13 | + except Exception as e: |
| 14 | + return "repr error: %s" % str(e) |
| 15 | + |
| 16 | + @classmethod |
| 17 | + def _convert_dict(cls, v): |
| 18 | + return {cls._convert(k): cls._convert(i) for k, i in v.items()} |
| 19 | + |
| 20 | + @classmethod |
| 21 | + def _convert_obj(cls, obj): |
| 22 | + try: |
| 23 | + return FakeClass(cls._safe_repr(obj), cls._convert_dict(obj.__dict__)) |
| 24 | + except Exception: |
| 25 | + return cls._convert(obj) |
| 26 | + |
| 27 | + @classmethod |
| 28 | + def _convert_seq(cls, v): |
| 29 | + return map(cls._convert, v) |
| 30 | + |
| 31 | + @classmethod |
| 32 | + def _convert(cls, v): |
| 33 | + |
| 34 | + if v is None: |
| 35 | + return v |
| 36 | + |
| 37 | + if dill is not None: |
| 38 | + try: |
| 39 | + dill.dumps(v) |
| 40 | + return v |
| 41 | + except Exception: |
| 42 | + return cls._safe_repr(v) |
| 43 | + else: |
| 44 | + from datetime import date, time, datetime, timedelta |
| 45 | + |
| 46 | + BUILTIN = (str, unicode, int, long, float, date, time, datetime, timedelta) if six.PY2 \ |
| 47 | + else (str, int, float, date, time, datetime, timedelta) # noqa: F821 |
| 48 | + |
| 49 | + if type(v) in BUILTIN: |
| 50 | + return v |
| 51 | + |
| 52 | + if isinstance(v, (tuple, list, set)): |
| 53 | + return type(v)(cls._convert_seq(v)) |
| 54 | + |
| 55 | + if isinstance(v, dict): |
| 56 | + return cls._convert_dict(v) |
| 57 | + |
| 58 | + return cls._safe_repr(v) |
| 59 | + |
| 60 | + |
| 61 | +class FakeTraceback(FakeType): |
| 62 | + |
| 63 | + def __init__(self, traceback): |
| 64 | + self.tb_frame = FakeFrame( |
| 65 | + traceback.tb_frame) if traceback.tb_frame else None |
| 66 | + self.tb_lineno = traceback.tb_lineno |
| 67 | + self.tb_next = FakeTraceback( |
| 68 | + traceback.tb_next) if traceback.tb_next else None |
| 69 | + self.tb_lasti = 0 |
| 70 | + |
| 71 | + |
| 72 | +class FakeFrame(FakeType): |
| 73 | + def __init__(self, frame): |
| 74 | + self.f_code = FakeCode(frame.f_code) |
| 75 | + self.f_locals = self._convert_dict(frame.f_locals) |
| 76 | + if "self" in frame.f_locals: |
| 77 | + self.f_locals["self"] = self._convert_obj(frame.f_locals["self"]) |
| 78 | + self.f_globals = self._convert_dict(frame.f_globals) |
| 79 | + self.f_lineno = frame.f_lineno |
| 80 | + self.f_back = FakeFrame(frame.f_back) if frame.f_back else None |
| 81 | + |
| 82 | + |
| 83 | +class FakeClass(FakeType): |
| 84 | + |
| 85 | + def __init__(self, repr, vals): |
| 86 | + self.__repr = repr |
| 87 | + self.__dict__.update(vars) |
| 88 | + |
| 89 | + def __repr__(self): |
| 90 | + return self.__repr |
| 91 | + |
| 92 | + |
| 93 | +class FakeCode(FakeType): |
| 94 | + |
| 95 | + def __init__(self, code): |
| 96 | + self.co_filename = os.path.abspath(code.co_filename) |
| 97 | + self.co_name = code.co_name |
| 98 | + self.co_argcount = code.co_argcount |
| 99 | + self.co_consts = tuple(FakeCode(c) if hasattr( |
| 100 | + c, "co_filename") else c for c in code.co_consts) |
| 101 | + self.co_firstlineno = code.co_firstlineno |
| 102 | + self.co_lnotab = code.co_lnotab |
| 103 | + self.co_varnames = code.co_varnames |
| 104 | + self.co_flags = code.co_flags |
| 105 | + self.co_code = code.co_code |
| 106 | + self._co_lines = list(code.co_lines()) if hasattr( |
| 107 | + code, "co_lines") else [] |
| 108 | + if hasattr(code, "co_kwonlyargcount"): |
| 109 | + self.co_kwonlyargcount = code.co_kwonlyargcount |
| 110 | + |
| 111 | + def co_lines(self): |
| 112 | + return iter(self._co_lines) |
0 commit comments