Пример #1
0
def get_all_property_paths(obj, prefix=""):
    props = []
    for prop_name, prop in inspect.getmembers(obj):
        path = prefix +  prop_name
        if is_exception(prop, prop_name) or known_pesky(prop_name):
            continue

        if (
            not private(prop_name) and
            not repeat_property(path, prop_name) and
            not past_depth(path, 4) and
            not is_exception(prop, prop_name) and
            no_args(prop) and
            not known_pesky(prop_name) and
            not inspect.isabstract(prop) and
            not inspect.istraceback(prop) and
            not inspect.isgenerator(prop) and
            not inspect.ismodule(prop)
        ):
            #print path, prop_name, private(prop_name), repeat_property(path, prop_name)
            props += get_all_property_paths(prop, prefix + prop_name + ".")

        if not inspect.isclass(prop):
            props.append(path)

    return props
Пример #2
0
    def _where(cls, action, args):

        frame_info: Optional[inspect.Traceback] = None
        # Only inspect the slooow stack introspection if print()'ing or logging level is sufficient.
        if not cls._is_logging_on() or cls.logger.isEnabledFor(logging.INFO):
            syspath_caller: FrameType = inspect.currentframe().f_back.f_back
            if not (inspect.istraceback(syspath_caller) or inspect.isframe(syspath_caller)):
                return
            frame_info: inspect.Traceback = inspect.getframeinfo(syspath_caller)

        if frame_info:
            filename = PurePath(frame_info.filename)
            try:
                filename = PurePath(frame_info.filename).relative_to(sys.base_prefix)
            except ValueError:
                filename_orig = filename
                cwd = Path.cwd()
                while filename == filename_orig:
                    try:
                        filename = PurePath(frame_info.filename).relative_to(cwd)
                        break
                    except ValueError:
                        cwd = cwd.parent

            message = f"sys.path.{action}{args} from {filename}:{frame_info.lineno}"
            cls._inform_user(message)
Пример #3
0
 def _find_lineno(self, obj, source_lines):
     lineno = None
     if inspect.ismodule(obj):
         lineno = 0
     if inspect.isclass(obj):
         if source_lines is None:
             return
         pat = re.compile('^\\s*class\\s*%s\\b' %
                          getattr(obj, '__name__', '-'))
         for (i, line) in enumerate(source_lines):
             while pat.match(line):
                 lineno = i
                 break
     if inspect.ismethod(obj):
         obj = obj.__func__
     if inspect.isfunction(obj):
         obj = obj.__code__
     if inspect.istraceback(obj):
         obj = obj.tb_frame
     if inspect.isframe(obj):
         obj = obj.f_code
     if inspect.iscode(obj):
         lineno = getattr(obj, 'co_firstlineno', None) - 1
     if lineno is not None:
         if source_lines is None:
             return lineno + 1
         pat = re.compile('(^|.*:)\\s*\\w*("|\')')
         for lineno in range(lineno, len(source_lines)):
             while pat.match(source_lines[lineno]):
                 return lineno
Пример #4
0
def findsource(object):
    """Return the entire source file and starting line number for an object.

    The argument may be a module, class, method, function, traceback, frame,
    or code object.  The source code is returned as a list of all the lines
    in the file and the line number indexes a line in that list.  An IOError
    is raised if the source code cannot be retrieved."""
    file = inspect.getsourcefile(object) or inspect.getfile(object)
    module = inspect.getmodule(object, file)
    if module:
        lines = linecache.getlines(file, module.__dict__)
    else:
        lines = linecache.getlines(file)
    if not lines:
        raise IOError('could not get source code')

    if inspect.ismodule(object):
        return lines, 0

    if inspect.isclass(object):
        name = object.__name__
        pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
        # make some effort to find the best matching class definition:
        # use the one with the least indentation, which is the one
        # that's most probably not inside a function definition.
        candidates = []
        for i in range(len(lines)):
            match = pat.match(lines[i])
            if match:
                # if it's at toplevel, it's already the best one
                if lines[i][0] == 'c':
                    return lines, i
                # else add whitespace to candidate list
                candidates.append((match.group(1), i))
        if candidates:
            # this will sort by whitespace, and by line number,
            # less whitespace first
            candidates.sort()
            return lines, candidates[0][1]
        else:
            raise IOError('could not find class definition')

    if inspect.ismethod(object):
        object = object.im_func
    if isinstance(object, FunctionType):
        object = sys.get_func_code(object)
    if inspect.istraceback(object):
        object = object.tb_frame
    if inspect.isframe(object):
        object = object.f_code
    if inspect.iscode(object):
        if not hasattr(object, 'co_firstlineno'):
            raise IOError('could not find function definition')
        lnum = object.co_firstlineno - 1
        pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
        while lnum > 0:
            if pat.match(lines[lnum]): break
            lnum = lnum - 1
        return lines, lnum
    raise IOError('could not find code object')
Пример #5
0
 def default(self, o: Any) -> Any:
     if is_iterable_no_collection(o):
         o = list(o)
     elif isinstance(o, (bytearray, memoryview)):
         o = bytes(o)
     if isinstance(o, Enum):
         return str(o)
     if hasattr(o, "__dict__"):
         if isinstance(o.__dict__, dict):
             return o.__dict__
         else:
             return str(o)
     if isinstance(o, (date, datetime, time)):
         return o.isoformat()
     if isinstance(o, bytes):
         try:
             return o.decode("utf-8")
         except UnicodeDecodeError:
             return str(o)
     if hasattr(o, "hexdigest"):
         return o.hexdigest()
     if hasattr(o, "hex_string"):
         return o.hex_string()
     if istraceback(o):
         return "".join(traceback.format_tb(o)).strip()
     return o
Пример #6
0
def getframeinfo(frame, context=1):
    """Get information about a frame or traceback object.

    A tuple of five things is returned: the filename, the line number of
    the current line, the function name, a list of lines of context from
    the source code, and the index of the current line within that list.
    The optional second argument specifies the number of lines of context
    to return, which are centered around the current line."""
    if istraceback(frame):
        lineno = frame.tb_lineno
        frame = frame.tb_frame
    else:
        lineno = frame.f_lineno
    if not isframe(frame):
        raise TypeError('{!r} is not a frame or traceback object'.format(frame))

    filename = getsourcefile(frame) or getfile(frame)
    if context > 0:
        start = lineno - 1 - context//2
        try:
            lines, _ = findsource(frame)
        except IOError:
            lines = index = None
        else:
            start = max(start, 1)
            start = max(0, min(start, len(lines) - context))
            lines = lines[start:start+context]
            index = lineno - 1 - start
    else:
        lines = index = None

    return Traceback(filename, lineno, frame.f_code.co_name, lines, index)
Пример #7
0
def _is_object_pickle_preferred(obj) -> bool:
    """
        Object 转成 bytes 的规则:
            1) object 有属性 "__getstate__" / "__setstate__" 时用 pickles , len(x) > 2k  时压缩

            2) inspect ismodule, isclass, ismethod, isfunction, isgeneratorfunction, isgenerator,
                iscoroutinefunction, iscoroutine, istraceback, iscode, isbuiltin 成立时,用 pickles
                len(x) > 2k  时压缩

            3) str 类型的,转成 pickle,为了能够压缩

            4) sys.getsizeof(x) <= 2k 时 pickles , > 2k 时 arrow

        NOTE: 发现有一个平衡点,在 2k 以下的数据, arrow 得到的字节数大约是 pickle 的五到十倍
        """
    if obj is None:
        return True
    if hasattr(obj, "__getstate__") and hasattr(obj, "__setstate__"):
        return True
    if inspect.ismodule(obj) or inspect.isclass(obj) or inspect.ismethod(obj) or inspect.isfunction(obj) \
            or inspect.isgeneratorfunction(obj) or inspect.isgenerator(obj) or inspect.iscoroutinefunction(obj) \
            or inspect.iscoroutine(obj) or inspect.istraceback(obj) or inspect.iscode(obj) or inspect.isbuiltin(obj):
        return True
    if isinstance(obj, str):
        return True
    if sys.getsizeof(obj) <= 2048:
        return True
    return False
Пример #8
0
def findsource(object):
    """Return the entire source file and starting line number for an object.

    The argument may be a module, class, method, function, traceback, frame,
    or code object.  The source code is returned as a list of all the lines
    in the file and the line number indexes a line in that list.  An IOError
    is raised if the source code cannot be retrieved."""
    file = inspect.getsourcefile(object) or inspect.getfile(object)
    module = inspect.getmodule(object, file)
    if module:
        lines = linecache.getlines(file, module.__dict__)
    else:
        lines = linecache.getlines(file)
    if not lines:
        raise IOError('could not get source code')

    if inspect.ismodule(object):
        return lines, 0

    if inspect.isclass(object):
        name = object.__name__
        pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
        # make some effort to find the best matching class definition:
        # use the one with the least indentation, which is the one
        # that's most probably not inside a function definition.
        candidates = []
        for i in range(len(lines)):
            match = pat.match(lines[i])
            if match:
                # if it's at toplevel, it's already the best one
                if lines[i][0] == 'c':
                    return lines, i
                # else add whitespace to candidate list
                candidates.append((match.group(1), i))
        if candidates:
            # this will sort by whitespace, and by line number,
            # less whitespace first
            candidates.sort()
            return lines, candidates[0][1]
        else:
            raise IOError('could not find class definition')

    if inspect.ismethod(object):
        object = object.__func__
    if inspect.isfunction(object):
        object = sys.get_func_code(object)
    if inspect.istraceback(object):
        object = object.tb_frame
    if inspect.isframe(object):
        object = object.f_code
    if inspect.iscode(object):
        if not hasattr(object, 'co_firstlineno'):
            raise IOError('could not find function definition')
        lnum = object.co_firstlineno - 1
        pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
        while lnum > 0:
            if pat.match(lines[lnum]): break
            lnum = lnum - 1
        return lines, lnum
    raise IOError('could not find code object')
Пример #9
0
    def executemany(self, query, *args, **kwargs):
        if not self.profile:
            return self.cursor.executemany(query, *args, **kwargs)

        start = oelite.util.now()
        ret = self.cursor.executemany(query, *args, **kwargs)
        stop = oelite.util.now()
        delta = stop - start

        frame = sys._getframe(1)
        if inspect.istraceback(frame):
            lineno = frame.tb_lineno
            frame = frame.tb_frame
        else:
            lineno = frame.f_lineno
        filename = inspect.getsourcefile(frame) or inspect.getfile(frame)
        t = (filename, lineno)
        self.current_record = record = [0, delta, 0]
        try:
            query_stats[t].append(record)
        except KeyError:
            query_stats[t] = [record]
        if t not in query_sample:
            query_sample[t] = query

        if ret is self.cursor:
            return self
        return ret
Пример #10
0
def extract_context(obj):
    if obj is None:
        return obj
    if isinstance(obj, (int, float)):
        return obj
    if isinstance(obj, six.string_types):
        return obj
    if isinstance(obj, dict):
        return dict(
            (extract_context(k), extract_context(v)) for k, v in obj.items())
    if isinstance(obj, (list, tuple)):
        return [extract_context(x) for x in obj]
    if isinstance(obj, Context):
        return extract_context(getattr(obj, _context_key))
    if isinstance(obj, helios.rpc.RPCResult):
        try:
            return ['RPCResult', obj.unwrap()]
        except:
            exc_type, exc_value, exc_trace = sys.exc_info()
            return {
                'exc-type': extract_context(exc_type),
                'exc-value': extract_context(exc_value),
                'traceback': extract_context(exc_trace)
            }
    if inspect.istraceback(obj):
        return extract_context(traceback.extract_tb(obj))
    return repr(obj)
Пример #11
0
    def import_from(s1, module):
        syms = inspect.getmembers(module)
        str_syms = dir(module)
        name_as = ""
        if len(s1) == 4:
            name_as = s1[3][1]


        if not (s1[1][1] in str_syms):
            print("import error")
            exit()
        else:
            for sym in syms:
                if sym[0] == s1[1][1]:
                    if inspect.isfunction(sym[1]):
                        if len(s1) == 4:
                            GLOBAL_SYMBOL_LIST.append(Function(name_as))
                        else:
                            GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                    elif inspect.isbuiltin(sym[1]):
                        if len(s1) == 4:
                            GLOBAL_SYMBOL_LIST.append(Function(name_as))
                        else:
                            GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                    elif inspect.ismethod(sym[1]):
                        pass
                    elif inspect.isgeneratorfunction:
                        if len(s1) == 4:
                            GLOBAL_SYMBOL_LIST.append(Function(name_as))
                        else:
                            GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                    elif inspect.isgenerator(sym[1]):
                        pass
                    elif inspect.istraceback(sym[1]):
                        pass
                    elif inspect.isframe(sym[1]):
                        pass
                    elif inspect.iscode(sym[1]):
                        pass
                    elif inspect.isroutine(sym[1]):
                        pass
                    elif inspect.isabstract(sym[1]):
                        pass
                    elif inspect.ismemberdescriptor(sym[1]):
                        pass
                    elif inspect.isdatadescriptor(sym[1]):
                        pass
                    elif inspect.isdatadescriptor(sym[1]):
                        pass
                    elif inspect.isgetsetdescriptor(sym[1]):
                        pass
                    elif inspect.ismemberdescriptor(sym[1]):
                        pass
                    elif inspect.isclass(sym[1]):
                        if len(s1) == 4:
                            GLOBAL_SYMBOL_LIST.append(Class(name_as))
                        else:
                            GLOBAL_SYMBOL_LIST.append(Class(sym[0]))
                    else:
                        print(sym[0])
Пример #12
0
 def import_name(s1):
     if s1[0] in NON_TERMINAL:
         if s1[0] in NON_TERMINAL and s1[0] == 286:
             dot_name = ""
             module_name = ""
             for name in s1[1]:
                 if not isinstance(name, int):
                     module_name += name[1]
             if len(s1) == 2:
                 dot_name = module_name
             elif len(s1) == 4:
                 dot_name = s1[3][1]
             try:
                 module = importlib.import_module(module_name)
             except ImportError:
                 print("Import Error, No module named " + module_name)
                 exit()
             new_module = Module(module_name)
             new_module.SYMBOL_LIST = []
             syms = inspect.getmembers(module)
             for sym in syms:
                 if inspect.isfunction(sym[1]):
                     #new_module.SYMBOL_LIST.append(Function(dot_name+'.' + sym[0]))
                     new_module.SYMBOL_LIST.append(Function(sym[0]))
                 elif inspect.isbuiltin(sym[1]):
                     new_module.SYMBOL_LIST.append(Function(sym[0]))
                 elif inspect.ismethod(sym[1]):
                     pass
                 elif inspect.isgeneratorfunction:
                     new_module.SYMBOL_LIST.append(Function(sym[0]))
                 elif inspect.isgenerator(sym[1]):
                     pass
                 elif inspect.istraceback(sym[1]):
                     pass
                 elif inspect.isframe(sym[1]):
                     pass
                 elif inspect.iscode(sym[1]):
                     pass
                 elif inspect.isroutine(sym[1]):
                     pass
                 elif inspect.isabstract(sym[1]):
                     pass
                 elif inspect.ismemberdescriptor(sym[1]):
                     pass
                 elif inspect.isdatadescriptor(sym[1]):
                     pass
                 elif inspect.isdatadescriptor(sym[1]):
                     pass
                 elif inspect.isgetsetdescriptor(sym[1]):
                     pass
                 elif inspect.ismemberdescriptor(sym[1]):
                     pass
                 elif inspect.isclass(sym[1]):
                     new_module.SYMBOL_LIST.append(Class(sym[0], [], []))
                 else:
                     print(sym[0])
                 self.local_names.append(new_module)
         else:
             for j in range(1,len(s1)):
                 import_name(s1[j])
Пример #13
0
 def print_variable_type(self, objType):
     default_vars = [
         "__builtins__", "__doc__", "__path__", "__cached__", "__file__",
         "__name__", "__package__", "__version__"
     ]
     self.dprint("[@ %s ]" % objType.__name__)
     self.indent()
     self.indent()
     for ModObj in self.Modules:
         for name in dir(ModObj):
             obj = getattr(ModObj, name)
             #print(name, ":", type(obj))
             if not (inspect.isclass(obj) or inspect.isfunction(obj)
                     or inspect.isroutine(obj) or inspect.isfunction(obj)
                     or inspect.isgeneratorfunction(obj)
                     or inspect.isgenerator(obj) or inspect.istraceback(obj)
                     or inspect.isframe(obj) or inspect.iscode(obj)
                     or inspect.isabstract(obj)
                     or inspect.ismethoddescriptor(obj)
                     or inspect.isdatadescriptor(obj)
                     or inspect.isgetsetdescriptor(obj)
                     or inspect.ismemberdescriptor(obj)
                     or inspect.isbuiltin(obj)):
                 if name not in default_vars:
                     if type(obj) is objType:
                         ObjName = ModObj.__name__ + '.' + name
                         self.dprint("%s" % ObjName)
     self.dedent()
     self.dedent()
Пример #14
0
 def print_variable_type(self, objType):
     default_vars=["__builtins__", "__doc__","__path__", "__cached__", "__file__", "__name__", "__package__", "__version__"]
     self.dprint("[ %s ]" %objType.__name__)
     self.indent();self.indent()
     for ModObj in self.Modules:
         for name in dir(ModObj):
             obj = getattr(ModObj, name)
             #print(name, ":", type(obj))
             if not (inspect.isclass(obj) or
                 inspect.isfunction(obj)  or
                 inspect.isroutine(obj)  or
                 inspect.isfunction(obj)  or
                 inspect.isgeneratorfunction(obj)  or
                 inspect.isgenerator(obj)  or
                 inspect.istraceback(obj)  or
                 inspect.isframe(obj)      or
                 inspect.iscode(obj)       or
                 inspect.isabstract(obj)   or
                 inspect.ismethoddescriptor(obj)  or
                 inspect.isdatadescriptor(obj)    or
                 inspect.isgetsetdescriptor(obj)  or
                 inspect.ismemberdescriptor(obj)  or
                 inspect.isbuiltin(obj)):
                 if name not in default_vars:
                     if type(obj) is  objType:
                         ObjName = ModObj.__name__ + '.' + name
                         self.dprint("%s" %ObjName)
     self.dedent();self.dedent()
Пример #15
0
def source_findable(python_object):
    """Check if inspect.getfile has a chance to find the source."""
    return (inspect.ismodule(python_object) or inspect.isclass(python_object)
            or inspect.ismethod(python_object)
            or inspect.isfunction(python_object)
            or inspect.istraceback(python_object)
            or inspect.isframe(python_object) or inspect.iscode(python_object))
Пример #16
0
 def _find_lineno(self, obj, source_lines):
     lineno = None
     if inspect.ismodule(obj):
         lineno = 0
     if inspect.isclass(obj):
         if source_lines is None:
             return
         pat = re.compile('^\\s*class\\s*%s\\b' % getattr(obj, '__name__', '-'))
         for (i, line) in enumerate(source_lines):
             while pat.match(line):
                 lineno = i
                 break
     if inspect.ismethod(obj):
         obj = obj.__func__
     if inspect.isfunction(obj):
         obj = obj.__code__
     if inspect.istraceback(obj):
         obj = obj.tb_frame
     if inspect.isframe(obj):
         obj = obj.f_code
     if inspect.iscode(obj):
         lineno = getattr(obj, 'co_firstlineno', None) - 1
     if lineno is not None:
         if source_lines is None:
             return lineno + 1
         pat = re.compile('(^|.*:)\\s*\\w*("|\')')
         for lineno in range(lineno, len(source_lines)):
             while pat.match(source_lines[lineno]):
                 return lineno
Пример #17
0
    def _find_lineno(self, obj, source_lines):
        """
        Return a line number of the given object's docstring.  Note:
        this method assumes that the object has a docstring.
        """
        lineno = None

        # Find the line number for modules.
        if inspect.ismodule(obj):
            lineno = 0

        # Find the line number for classes.
        # Note: this could be fooled if a class is defined multiple
        # times in a single file.
        if inspect.isclass(obj):
            if source_lines is None:
                return None
            pat = re.compile(r'^\s*class\s*%s\b' %
                             getattr(obj, '__name__', '-'))
            for i, line in enumerate(source_lines):
                if pat.match(line):
                    lineno = i
                    break

        # Find the line number for functions & methods.
        if inspect.ismethod(obj): obj = obj.__func__
        if inspect.isfunction(obj): obj = six.get_function_code(obj)
        if inspect.istraceback(obj): obj = obj.tb_frame
        if inspect.isframe(obj): obj = obj.f_code
        if inspect.iscode(obj):
            lineno = getattr(obj, 'co_firstlineno', None)-1

        # Find the line number where the docstring starts.  Assume
        # that it's the first line that begins with a quote mark.
        # Note: this 
Пример #18
0
    def default(self, obj):

        # First convert the object
        obj = self.convert(obj)

        # Check if the object is convertible
        try:
            json.JSONEncoder().encode(obj)
            return obj

        except TypeError:
            pass

        # Custom conversion
        if type(obj) == Exception or isinstance(
                obj, Exception) or type(obj) == type:
            return str(obj)

        elif istraceback(obj):
            return "".join(traceback.format_tb(obj)).strip()

        elif hasattr(obj, "repr_json"):
            return obj.repr_json()

        elif isinstance(obj, complex):
            return str(obj)

        else:
            try:
                return json.JSONEncoder.default(self, obj)

            except TypeError:
                return "%s not JSON serializable" % obj.__class__.__name__
    def import_from(s1, module):
        syms = inspect.getmembers(module)
        str_syms = dir(module)
        name_as = ""
        if len(s1) == 4:
            name_as = s1[3][1]

        if not (s1[1][1] in str_syms):
            print("import error")
            exit()
        else:
            for sym in syms:
                if sym[0] == s1[1][1]:
                    if inspect.isfunction(sym[1]):
                        if len(s1) == 4:
                            GLOBAL_SYMBOL_LIST.append(Function(name_as))
                        else:
                            GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                    elif inspect.isbuiltin(sym[1]):
                        if len(s1) == 4:
                            GLOBAL_SYMBOL_LIST.append(Function(name_as))
                        else:
                            GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                    elif inspect.ismethod(sym[1]):
                        pass
                    elif inspect.isgeneratorfunction:
                        if len(s1) == 4:
                            GLOBAL_SYMBOL_LIST.append(Function(name_as))
                        else:
                            GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                    elif inspect.isgenerator(sym[1]):
                        pass
                    elif inspect.istraceback(sym[1]):
                        pass
                    elif inspect.isframe(sym[1]):
                        pass
                    elif inspect.iscode(sym[1]):
                        pass
                    elif inspect.isroutine(sym[1]):
                        pass
                    elif inspect.isabstract(sym[1]):
                        pass
                    elif inspect.ismemberdescriptor(sym[1]):
                        pass
                    elif inspect.isdatadescriptor(sym[1]):
                        pass
                    elif inspect.isdatadescriptor(sym[1]):
                        pass
                    elif inspect.isgetsetdescriptor(sym[1]):
                        pass
                    elif inspect.ismemberdescriptor(sym[1]):
                        pass
                    elif inspect.isclass(sym[1]):
                        if len(s1) == 4:
                            GLOBAL_SYMBOL_LIST.append(Class(name_as))
                        else:
                            GLOBAL_SYMBOL_LIST.append(Class(sym[0]))
                    else:
                        print(sym[0])
 def _handle_obj(self,obj):
     if (inspect.ismethod(obj) or inspect.isclass(obj) or inspect.istraceback(obj) or inspect.isbuiltin(obj) or inspect.ismodule(obj) or inspect.isfunction(obj) or inspect.iscode(obj)):
         return False
     module = inspect.getmodule(obj)
     if module:
         if(module.__name__.find(self._project_mark)!=-1):
             return True
     return False 
Пример #21
0
def _pydoc_isdata_override(object):
    # yes, import in function is evil.
    # so is this function...
    import inspect
    return not (isinstance(object, partial) or
                inspect.ismodule(object) or inspect.isclass(object) or
                inspect.isroutine(object) or inspect.isframe(object) or
                inspect.istraceback(object) or inspect.iscode(object))
Пример #22
0
def _pydoc_isdata_override(object):
    # yes, import in function is evil.
    # so is this function...
    import inspect
    return not (isinstance(object, partial) or inspect.ismodule(object)
                or inspect.isclass(object) or inspect.isroutine(object)
                or inspect.isframe(object) or inspect.istraceback(object)
                or inspect.iscode(object))
Пример #23
0
    def import_name(s1):
        if s1[0] in NON_TERMINAL:
            if s1[0] in NON_TERMINAL and s1[0] == 286:
                dot_name = ""
                module_name = ""
                for name in s1[1]:
                    if type(name) != type(1):
                        module_name += name[1]
                if len(s1) == 2:
                    dot_name = module_name
                elif len(s1) == 4:
                    dot_name = s1[3][1]
                try:
                    module = importlib.import_module(module_name)
                except ImportError:
                    print("Import Error, No module named " + module_name)
                    exit()

                a = dir(module)
                syms = inspect.getmembers(module)
                for sym in syms:
                    if inspect.isfunction(sym[1]):
                        GLOBAL_SYMBOL_LIST.append(Function(dot_name + "." + sym[0]))
                    elif inspect.isbuiltin(sym[1]):
                        GLOBAL_SYMBOL_LIST.append(Function(dot_name + "." + sym[0]))
                    elif inspect.ismethod(sym[1]):
                        pass
                    elif inspect.isgeneratorfunction:
                        GLOBAL_SYMBOL_LIST.append(Function(dot_name + "." + sym[0]))
                    elif inspect.isgenerator(sym[1]):
                        pass
                    elif inspect.istraceback(sym[1]):
                        pass
                    elif inspect.isframe(sym[1]):
                        pass
                    elif inspect.iscode(sym[1]):
                        pass
                    elif inspect.isroutine(sym[1]):
                        pass
                    elif inspect.isabstract(sym[1]):
                        pass
                    elif inspect.ismemberdescriptor(sym[1]):
                        pass
                    elif inspect.isdatadescriptor(sym[1]):
                        pass
                    elif inspect.isdatadescriptor(sym[1]):
                        pass
                    elif inspect.isgetsetdescriptor(sym[1]):
                        pass
                    elif inspect.ismemberdescriptor(sym[1]):
                        pass
                    elif inspect.isclass(sym[1]):
                        GLOBAL_SYMBOL_LIST.append(Class(dot_name + "." + sym[0], [], []))
                    else:
                        print(sym[0])
            else:
                for j in range(1, len(s1)):
                    import_name(s1[j])
 def parse_import(st):
     if st[0] == 283:
         import_name(st[2])
     elif st[0] == 284:
         module_name = ""
         if type(st[2]) != type(1):
             for name in st[2]:
                 if type(name) != type(1):
                     module_name += name[1]
         try:
             module = importlib.import_module(module_name)
         except ImportError:
             print("Import Error, No module named " + module_name)
             exit()
         if len(st) == 5 and st[4][1] == "*":
             syms = inspect.getmembers(module)
             str_syms = dir(module)
             name_as = ""
             for sym in syms:
                 if inspect.isfunction(sym[1]):
                     GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                 elif inspect.isbuiltin(sym[1]):
                     GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                 elif inspect.ismethod(sym[1]):
                     pass
                 elif inspect.isgeneratorfunction:
                     GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                 elif inspect.isgenerator(sym[1]):
                     pass
                 elif inspect.istraceback(sym[1]):
                     pass
                 elif inspect.isframe(sym[1]):
                     pass
                 elif inspect.iscode(sym[1]):
                     pass
                 elif inspect.isroutine(sym[1]):
                     pass
                 elif inspect.isabstract(sym[1]):
                     pass
                 elif inspect.ismemberdescriptor(sym[1]):
                     pass
                 elif inspect.isdatadescriptor(sym[1]):
                     pass
                 elif inspect.isdatadescriptor(sym[1]):
                     pass
                 elif inspect.isgetsetdescriptor(sym[1]):
                     pass
                 elif inspect.ismemberdescriptor(sym[1]):
                     pass
                 elif inspect.isclass(sym[1]):
                     GLOBAL_SYMBOL_LIST.append(Class(sym[0]))
                 else:
                     print(sym[0])
         else:
             for counter in range(len(st[4])):
                 if not isinstance(st[4][counter],
                                   int) and st[4][counter][0] == 285:
                     import_from(st[4][counter], module)
Пример #25
0
 def parse_import(st):
     if st[0] == 283:
         import_name(st[2])
     elif st[0] == 284:
         module_name = ""
         if type(st[2]) != type(1):
             for name in st[2]:
                 if type(name) != type(1):
                     module_name += name[1]
         try:
             module = importlib.import_module(module_name)
         except ImportError:
             print("Import Error, No module named " + module_name)
             exit()
         if len(st)==5 and st[4][1] == "*":
             syms = inspect.getmembers(module)
             str_syms = dir(module)
             name_as = ""
             for sym in syms:
                 if inspect.isfunction(sym[1]):
                     GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                 elif inspect.isbuiltin(sym[1]):
                     GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                 elif inspect.ismethod(sym[1]):
                     pass
                 elif inspect.isgeneratorfunction:
                     GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                 elif inspect.isgenerator(sym[1]):
                     pass
                 elif inspect.istraceback(sym[1]):
                     pass
                 elif inspect.isframe(sym[1]):
                     pass
                 elif inspect.iscode(sym[1]):
                     pass
                 elif inspect.isroutine(sym[1]):
                     pass
                 elif inspect.isabstract(sym[1]):
                     pass
                 elif inspect.ismemberdescriptor(sym[1]):
                     pass
                 elif inspect.isdatadescriptor(sym[1]):
                     pass
                 elif inspect.isdatadescriptor(sym[1]):
                     pass
                 elif inspect.isgetsetdescriptor(sym[1]):
                     pass
                 elif inspect.ismemberdescriptor(sym[1]):
                     pass
                 elif inspect.isclass(sym[1]):
                     GLOBAL_SYMBOL_LIST.append(Class(sym[0]))
                 else:
                     print(sym[0])
         else:
             for counter in range(len(st[4])):
                 if not isinstance(st[4][counter],int) and st[4][counter][0] == 285:
                     import_from(st[4][counter], module)
Пример #26
0
def source_findable(python_object):
    """Check if inspect.getfile has a chance to find the source."""
    return (inspect.ismodule(python_object) or
            inspect.isclass(python_object) or
            inspect.ismethod(python_object) or
            inspect.isfunction(python_object) or
            inspect.istraceback(python_object) or
            inspect.isframe(python_object) or
            inspect.iscode(python_object))
Пример #27
0
    def _get_frames(self, exc_traceback):
        if not exc_traceback or inspect.istraceback(exc_traceback):
            return inspect.getinnerframes(exc_traceback)

        if self._is_stack_frame_type(exc_traceback):
            return exc_traceback

        raise DeveloperException(
            "Expected traceback type. Got '{}' instead.".format(
                type(exc_traceback)))
Пример #28
0
def user_frame(tb):
    if not inspect.istraceback(tb):
        raise ValueError('could not retrieve frame: argument not a traceback')

    for finfo in reversed(inspect.getinnerframes(tb)):
        relpath = os.path.relpath(finfo.filename, sys.path[0])
        if relpath.split(os.sep)[0] != 'reframe':
            return finfo

    return None
Пример #29
0
def _is_visible_type(attribute):
    return not (inspect.isfunction(attribute) or inspect.ismethod(attribute)
                or inspect.isbuiltin(attribute) or inspect.isroutine(attribute)
                or inspect.isclass(attribute) or inspect.ismodule(attribute)
                or inspect.istraceback(attribute) or inspect.isframe(attribute)
                or inspect.iscode(attribute) or inspect.isabstract(attribute)
                or inspect.ismethoddescriptor(attribute)
                or inspect.isdatadescriptor(attribute)
                or inspect.isgetsetdescriptor(attribute)
                or inspect.ismemberdescriptor(attribute))
Пример #30
0
 def _default_json_handler(obj):
     '''Prints dates in ISO format'''
     if isinstance(obj, (datetime.date, datetime.time)):
         return obj.isoformat()
     elif istraceback(obj):
         tb = ''.join(traceback.format_tb(obj))
         return tb.strip()
     elif isinstance(obj, Exception):
         return "Exception: %s" % str(obj)
     return str(obj)
Пример #31
0
 def _default_json_handler(obj):
     '''Prints dates in ISO format'''
     if isinstance(obj, (datetime.date, datetime.time)):
         return obj.isoformat()
     elif istraceback(obj):
         tb = ''.join(traceback.format_tb(obj))
         return tb.strip()
     elif isinstance(obj, Exception):
         return "Exception: %s" % str(obj)
     return str(obj)
Пример #32
0
    def setUp(self):
        try:
            self.parent()
        except Exception as e:
            self.theException = e
            exc_info = sys.exc_info()

            self.assertTrue(inspect.istraceback(exc_info[2]))
            self.msg = raygunmsgs.RaygunErrorMessage(
                exc_info[0], exc_info[1], exc_info[2],
                {'transmitLocalVariables': True})
Пример #33
0
def getframeinfo(frame, context=1):
    """
    Get information about a frame or traceback object.

    A tuple of five things is returned: the filename, the line number of
    the current line, the function name, a list of lines of context from
    the source code, and the index of the current line within that list.
    The optional second argument specifies the number of lines of context
    to return, which are centered around the current line.

    This originally comes from ``inspect`` but is modified to handle issues
    with ``findsource()``.
    """
    if inspect.istraceback(frame):
        lineno = frame.tb_lineno
        frame = frame.tb_frame
    else:
        lineno = frame.f_lineno
    if not inspect.isframe(frame):
        raise TypeError('arg is not a frame or traceback object')

    filename = inspect.getsourcefile(frame) or inspect.getfile(frame)
    if context > 0:
        start = lineno - 1 - context // 2
        try:
            lines, lnum = inspect.findsource(frame)
        except Exception:  # findsource raises platform-dependant exceptions
            first_lines = lines = index = None
        else:
            start = max(start, 1)
            start = max(0, min(start, len(lines) - context))
            first_lines = lines[:2]
            lines = lines[start:(start + context)]
            index = lineno - 1 - start
    else:
        first_lines = lines = index = None

    # Code taken from Django's ExceptionReporter._get_lines_from_file
    if first_lines and isinstance(first_lines[0], bytes):
        encoding = 'ascii'
        for line in first_lines[:2]:
            # File coding may be specified. Match pattern from PEP-263
            # (http://www.python.org/dev/peps/pep-0263/)
            match = re.search(br'coding[:=]\s*([-\w.]+)', line)
            if match:
                encoding = match.group(1).decode('ascii')
                break
        lines = [line.decode(encoding, 'replace') for line in lines]

    if hasattr(inspect, 'Traceback'):
        return inspect.Traceback(filename, lineno, frame.f_code.co_name, lines,
                                 index)
    else:
        return (filename, lineno, frame.f_code.co_name, lines, index)
Пример #34
0
 def _default_handler(self, obj):
     if isinstance(obj, datetime):
         return obj.isoformat()
     elif istraceback(obj):
         tb = ''.join(traceback.format_tb(obj))
         return tb.strip().split('\n')
     elif isinstance(obj, Exception):
         return "Exception: %s" % repr(obj)
     elif callable(obj):
         return obj()
     return str(obj)
Пример #35
0
def findsource(object):
    # Return the entire source file and starting line number for an object.

    file = getsourcefile(object) or getfile(object)
    globals_dict = None
    if inspect.isframe(object):
        globals_dict = object.f_globals
    else:
        module = getmodule(object, file)
        if module:
            globals_dict = module.__dict__
    lines = linecache.getlines(file, globals_dict)
    if not lines:
        raise IOError('could not get source code')

    if ismodule(object):
        return lines, 0

    if isclass(object):
        name = object.__name__
        pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
        candidates = []
        for i in range(len(lines)):
            match = pat.match(lines[i])
            if match:
                if lines[i][0] == 'c':
                    return lines, i
                candidates.append((match.group(1), i))
        if candidates:
            candidates.sort()
            return lines, candidates[0][1]
        else:
            raise IOError('could not find class definition')

    if ismethod(object):
        object = object.im_func
    if isfunction(object):
        object = object.func_code
    if istraceback(object):
        object = object.tb_frame
    if isframe(object):
        object = object.f_code
    if iscode(object):
        if not hasattr(object, 'co_firstlineno'):
            raise IOError('could not find function definition')
        pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
        pmatch = pat.match
        lnum = min(object.co_firstlineno,len(lines))-1
        while lnum > 0:
            if pmatch(lines[lnum]): break
            lnum -= 1
 
        return lines, lnum
    raise IOError('could not find code object')
Пример #36
0
 def default(self, obj):
     if isinstance(obj, (datetime.date, datetime.datetime, datetime.time)):
         return obj.isoformat()
     elif isinstance(obj, set):
         return list(obj)
     elif istraceback(obj):
         with io.StringIO() as string_io:
             traceback.print_tb(obj, file=string_io)
             return string_io.getvalue()
     elif isinstance(obj, Exception):
         return repr(obj)
     return super().default(obj)
Пример #37
0
def _find_traceback_in_frame(frame):
    if stackless_wrap and hasattr(stackless_wrap, "frame"):
        # Really hacking code...
        # See prickelpit.c
        obj = stackless_wrap.frame.__reduce__(frame)
        try:
            for traceback in reversed(obj[-1][-1]):
                if inspect.istraceback(traceback):
                    return traceback
        except:
            pass
    return None
Пример #38
0
def _get_line(obj):
    if inspect.ismethod(obj):
        obj = obj.__func__
    if inspect.isfunction(obj):
        obj = obj.__code__
    if inspect.istraceback(obj):
        obj = obj.tb_frame
    if inspect.isframe(obj):
        obj = obj.f_code
    if inspect.iscode(obj) and hasattr(obj, 'co_firstlineno'):
        return obj.co_firstlineno
    return None
Пример #39
0
def _find_traceback_in_frame(frame):
    if stackless_wrap and hasattr(stackless_wrap, "frame"):
        # Really hacking code...
        # See prickelpit.c
        obj = stackless_wrap.frame.__reduce__(frame)
        try:
            for traceback in reversed(obj[-1][-1]):
                if inspect.istraceback(traceback):
                    return traceback
        except BaseException:
            pass
    return None
Пример #40
0
 def _clonable(of):
     sets = of.__dict__.copy()
     sets.update(type(of).__dict__.copy())
     final = sets.copy()
     for key in sets:
         v = sets[key]
         if isfunction(v) or ismethod(v) or isgeneratorfunction(v) or isgenerator(v) \
         or isroutine(v) or isabstract(v) or isclass(v) or ismodule(v) or istraceback(v) \
         or isframe(v) or iscode(v) or isbuiltin(v) or ismethoddescriptor(v) \
         or isdatadescriptor(v) or isgetsetdescriptor(v) or ismemberdescriptor(v) \
         or v is None or v == '__main__' or key == '__module__': final.pop(key)
     return final
Пример #41
0
 def _default_handler(obj):
     """Prints dates in ISO format"""
     datetimes = (datetime.date, datetime.time, datetime.datetime)
     if isinstance(obj, datetimes):
         return obj.astimezone(self.tz_beijing) \
             .strftime(self.datefmt)
     elif istraceback(obj):
         tb = ''.join(traceback.format_tb(obj))
         return tb.strip()
     elif isinstance(obj, Exception):
         return "Exception: %s" % str(obj)
     return str(obj)
Пример #42
0
def getframeinfo(frame, context=1):
    """
    Get information about a frame or traceback object.

    A tuple of five things is returned: the filename, the line number of
    the current line, the function name, a list of lines of context from
    the source code, and the index of the current line within that list.
    The optional second argument specifies the number of lines of context
    to return, which are centered around the current line.

    This originally comes from ``inspect`` but is modified to handle issues
    with ``findsource()``.
    """
    if inspect.istraceback(frame):
        lineno = frame.tb_lineno
        frame = frame.tb_frame
    else:
        lineno = frame.f_lineno
    if not inspect.isframe(frame):
        raise TypeError('arg is not a frame or traceback object')

    filename = inspect.getsourcefile(frame) or inspect.getfile(frame)
    if context > 0:
        start = lineno - 1 - context // 2
        try:
            lines, lnum = inspect.findsource(frame)
        except Exception:   # findsource raises platform-dependant exceptions
            first_lines = lines = index = None
        else:
            start = max(start, 1)
            start = max(0, min(start, len(lines) - context))
            first_lines = lines[:2]
            lines = lines[start:(start + context)]
            index = lineno - 1 - start
    else:
        first_lines = lines = index = None

    # Code taken from Django's ExceptionReporter._get_lines_from_file
    if first_lines and isinstance(first_lines[0], bytes):
        encoding = 'ascii'
        for line in first_lines[:2]:
            # File coding may be specified. Match pattern from PEP-263
            # (http://www.python.org/dev/peps/pep-0263/)
            match = re.search(br'coding[:=]\s*([-\w.]+)', line)
            if match:
                encoding = match.group(1).decode('ascii')
                break
        lines = [line.decode(encoding, 'replace') for line in lines]

    if hasattr(inspect, 'Traceback'):
        return inspect.Traceback(filename, lineno, frame.f_code.co_name, lines, index)
    else:
        return (filename, lineno, frame.f_code.co_name, lines, index)
Пример #43
0
def user_frame(tb):
    if not inspect.istraceback(tb):
        raise ValueError('could not retrieve frame: argument not a traceback')

    for finfo in reversed(inspect.getinnerframes(tb)):
        module = inspect.getmodule(finfo.frame)
        if module is None:
            continue

        if not module.__name__.startswith('reframe'):
            return finfo

    return None
Пример #44
0
 def _default_handler(self, obj):
     if isinstance(obj, datetime):
         return obj.isoformat()
     elif istraceback(obj):
         tb = "".join(traceback.format_tb(obj))
         return tb.strip().split("\n")
     elif isinstance(obj, Exception):
         return "Exception: %s" % repr(obj)
     elif type(obj) is type:
         return str(obj)
     elif isinstance(obj, CallableWrapper):
         return obj()
     return str(obj)
Пример #45
0
 def _clonable(of):
     sets = of.__dict__.copy()
     sets.update(type(of).__dict__.copy())
     final = sets.copy()
     for key in sets:
         v = sets[key]
         if isfunction(v) or ismethod(v) or isgeneratorfunction(v) or isgenerator(v) \
         or isroutine(v) or isabstract(v) or isclass(v) or ismodule(v) or istraceback(v) \
         or isframe(v) or iscode(v) or isbuiltin(v) or ismethoddescriptor(v) \
         or isdatadescriptor(v) or isgetsetdescriptor(v) or ismemberdescriptor(v) \
         or v is None or v == '__main__' or key == '__module__':
             final.pop(key)
     return final
Пример #46
0
def _get_object_to_check(python_object):
    """Check if inspect.getfile has a chance to find the source."""
    if (inspect.ismodule(python_object) or inspect.isclass(python_object)
            or inspect.ismethod(python_object)
            or inspect.isfunction(python_object)
            or inspect.istraceback(python_object)
            or inspect.isframe(python_object)
            or inspect.iscode(python_object)):
        return python_object

    try:
        return python_object.__class__
    except AttributeError:
        raise TypeError  # Prevents computation of `repr` within inspect.
Пример #47
0
def DoFind(f, mod):
    import linecache

    if inspect.ismodule(mod):
        return f, 0, 0

    lines = linecache.getlines(f)

    if inspect.isclass(mod):
        name = mod.__name__
        pat = re.compile(r"^\s*class\s*" + name + r"\b")
        for i in xrange(len(lines)):
            if pat.match(lines[i]):
                return f, i, 0

        return f, 0, 0

    if inspect.ismethod(mod):
        mod = mod.im_func

    if inspect.isfunction(mod):
        try:
            mod = mod.func_code
        except AttributeError:
            mod = mod.__code__  # python 3k

    if inspect.istraceback(mod):
        mod = mod.tb_frame

    if inspect.isframe(mod):
        mod = mod.f_code

    if inspect.iscode(mod):
        if not hasattr(mod, "co_filename"):
            return None, 0, 0

        if not hasattr(mod, "co_firstlineno"):
            return mod.co_filename, 0, 0

        lnum = mod.co_firstlineno
        pat = re.compile(r"^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)")
        while lnum > 0:
            if pat.match(lines[lnum]):
                break
            lnum -= 1

        return f, lnum, 0

    raise RuntimeError("Do not know about: " + f + " " + str(mod))
Пример #48
0
def _isinstance(object):
    '''True if object is a class instance type (and is not a builtin)'''
    if _hascode(object) or isclass(object) or ismodule(object):
        return False
    if istraceback(object) or isframe(object) or iscode(object):
        return False
    # special handling (numpy arrays, ...)
    if not getmodule(object) and getmodule(type(object)).__name__ in ['numpy']:
        return True
    _types = ('<class ',"<type 'instance'>")
    if not repr(type(object)).startswith(_types): #FIXME: weak hack
        return False
    if not getmodule(object) or object.__module__ in ['builtins','__builtin__'] or getname(object, force=True) in ['array']:
        return False
    return True # by process of elimination... it's what we want
Пример #49
0
def _get_object_to_check(python_object):
    """Check if inspect.getfile has a chance to find the source."""
    if (inspect.ismodule(python_object) or
            inspect.isclass(python_object) or
            inspect.ismethod(python_object) or
            inspect.isfunction(python_object) or
            inspect.istraceback(python_object) or
            inspect.isframe(python_object) or
            inspect.iscode(python_object)):
        return python_object

    try:
        return python_object.__class__
    except AttributeError:
        raise TypeError  # Prevents computation of `repr` within inspect.
Пример #50
0
    def test_exception(self):
        self.debugger_events = []
        debugger = ManagedDebugger(self._debugger_step)
        debugger.run("raise ValueError()")
        assert len(self.debugger_events) == 3
        assert self.debugger_events[1][0] == DebuggerEvent.Exception
        kwargs = self.debugger_events[1][2]

        assert 'frame' in kwargs
        assert 'ex_type' in kwargs
        assert 'traceback' in kwargs

        assert isinstance(kwargs['ex_type'], type)
        assert inspect.istraceback(kwargs['traceback'])
        assert inspect.isframe(kwargs['frame'])
Пример #51
0
def get_last_or_frame_exception():
    """Intended to be used going into post mortem routines.  If
    sys.last_traceback is set, we will return that and assume that
    this is what post-mortem will want. If sys.last_traceback has not
    been set, then perhaps we *about* to raise an error and are
    fielding an exception. So assume that sys.exc_info()[2]
    is where we want to look."""

    try:
        if inspect.istraceback(sys.last_traceback):
            # We do have a traceback so prefer that.
            return sys.last_type, sys.last_value, sys.last_traceback
    except AttributeError:
        pass
    return sys.exc_info()
Пример #52
0
def get_last_or_frame_exception():
    """Intended to be used going into post mortem routines.  If
    sys.last_traceback is set, we will return that and assume that
    this is what post-mortem will want. If sys.last_traceback has not
    been set, then perhaps we *about* to raise an error and are
    fielding an exception. So assume that sys.exc_info()[2]
    is where we want to look."""

    try:
        if inspect.istraceback(sys.last_traceback):
            # We do have a traceback so prefer that.
            return sys.last_type, sys.last_value, sys.last_traceback
    except AttributeError:
        pass
    return sys.exc_info()
Пример #53
0
    def get_frame_info(frame):
        """Get a basic description of the frame. Faster than the version
        in inspect.py (which does more)

        :return: a basic description of the frame.
        :rtype: FrameInfo
        """
        if istraceback(frame):
            lineno = frame.tb_lineno
            frame = frame.tb_frame
        else:
            lineno = frame.f_lineno

        filename = getfile(frame)
        func_name = frame.f_code.co_name
        return FrameInfo(filename, lineno, func_name)
Пример #54
0
def _is_visible_type(attribute):
    return not (
        inspect.isfunction(attribute)
        or inspect.ismethod(attribute)
        or inspect.isbuiltin(attribute)
        or inspect.isroutine(attribute)
        or inspect.isclass(attribute)
        or inspect.ismodule(attribute)
        or inspect.istraceback(attribute)
        or inspect.isframe(attribute)
        or inspect.iscode(attribute)
        or inspect.isabstract(attribute)
        or inspect.ismethoddescriptor(attribute)
        or inspect.isdatadescriptor(attribute)
        or inspect.isgetsetdescriptor(attribute)
        or inspect.ismemberdescriptor(attribute)
    )
Пример #55
0
 def _default_json_handler(obj):
     '''Prints dates in ISO format'''
     if isinstance(obj, datetime.datetime):
         if obj.year < 1900:
             # strftime do not work with date < 1900
             return obj.isoformat()
         return obj.strftime(self.datefmt or '%Y-%m-%dT%H:%M')
     elif isinstance(obj, datetime.date):
         return obj.isoformat()
     elif isinstance(obj, datetime.time):
         return obj.strftime('%H:%M')
     elif istraceback(obj):
         tb = ''.join(traceback.format_tb(obj))
         return tb.strip()
     elif isinstance(obj, Exception):
         return "Exception: %s" % str(obj)
     return str(obj)
Пример #56
0
def code(func):
    '''get the code object for the given function or method

    NOTE: use dill.source.getsource(CODEOBJ) to get the source code
    '''
    if PY3:
        im_func = '__func__'
        func_code = '__code__'
    else:
        im_func = 'im_func'
        func_code = 'func_code'
    if ismethod(func): func = getattr(func, im_func)
    if isfunction(func): func = getattr(func, func_code)
    if istraceback(func): func = func.tb_frame
    if isframe(func): func = func.f_code
    if iscode(func): return func
    return
Пример #57
0
def getframeinfo(frame):
    """Get information about a frame or traceback object.

    A tuple of five things is returned: the filename, the line number of
    the current line, the function name, a list of lines of context from
    the source code, and the index of the current line within that list.
    The optional second argument specifies the number of lines of context
    to return, which are centered around the current line."""
    if inspect.istraceback(frame):
        lineno = frame.tb_lineno
        frame = frame.tb_frame
    else:
        lineno = frame.f_lineno
    if not inspect.isframe(frame):
        raise TypeError('arg is not a frame or traceback object')

    return FastTraceback(frame.f_code.co_filename, lineno)
Пример #58
0
def getClassInfo(_class):

    parentClassName = inspect.getclasstree([_class])[0][0].__name__

    writeDefinition("class %s(%s):\n" % (_class.__name__, parentClassName))
    writeDefinition("\n")

    indent()

    for memberName in _class.__dict__:
        member = getattr(_class, memberName)
        if inspect.isbuiltin(member):
            getMemberInfo(member, isBuiltIn=True)
            continue
        if inspect.ismethod(member):
            getMemberInfo(member)
            continue
        if inspect.isfunction(member):
            getMemberInfo(member)
            continue
        if inspect.isroutine(member):
            getMemberInfo(member, isBuiltIn=True)
            continue
        if inspect.istraceback(member):
            getMemberInfo(member)
            continue
        if inspect.isframe(member):
            getMemberInfo(member)
            continue
        if inspect.iscode(member):
            getMemberInfo(member)
            continue
        if inspect.ismethoddescriptor(member):
            getMemberInfo(member)
            continue
        if inspect.isdatadescriptor(member):
            getMemberInfo(member)
            continue
        if inspect.isgetsetdescriptor(member):
            getMemberInfo(member)
            continue
        if inspect.ismemberdescriptor(member):
            getMemberInfo(member)

    dedent()