示例#1
0
    def get_args(self):
        """Check if an unclosed parenthesis exists, then attempt to get the
        argspec() for it. On success, update self.funcprops,self.arg_pos and
        return True, otherwise set self.funcprops to None and return False"""

        self.current_func = None

        if not self.config.arg_spec:
            return False

        func, arg_number = self._funcname_and_argnum(self.current_line)
        if not func:
            return False

        try:
            if inspection.is_eval_safe_name(func):
                f = self.get_object(func)
            else:
                try:
                    fake_cursor = self.current_line.index(func) + len(func)
                    f = simpleeval.evaluate_current_attribute(
                            fake_cursor, self.current_line, self.interp.locals)
                except simpleeval.EvaluationError:
                    return False
        except Exception:
            # another case of needing to catch every kind of error
            # since user code is run in the case of descriptors
            # XXX: Make sure you raise here if you're debugging the completion
            # stuff !
            return False

        if inspect.isclass(f):
            class_f = None

            if (hasattr(f, '__init__') and
                    f.__init__ is not object.__init__):
                class_f = f.__init__
            if ((not class_f or
                 not inspection.getfuncprops(func, class_f)) and
                    hasattr(f, '__new__') and
                    f.__new__ is not object.__new__ and
                    # py3
                    f.__new__.__class__ is not object.__new__.__class__):

                class_f = f.__new__

            if class_f:
                f = class_f

        self.current_func = f
        self.funcprops = inspection.getfuncprops(func, f)
        if self.funcprops:
            self.arg_pos = arg_number
            return True
        self.arg_pos = None
        return False
示例#2
0
    def get_args(self):
        """Check if an unclosed parenthesis exists, then attempt to get the
        argspec() for it. On success, update self.funcprops,self.arg_pos and
        return True, otherwise set self.funcprops to None and return False"""

        self.current_func = None

        if not self.config.arg_spec:
            return False

        func, arg_number = self._funcname_and_argnum(self.current_line)
        if not func:
            return False

        try:
            if inspection.is_eval_safe_name(func):
                f = self.get_object(func)
            else:
                try:
                    fake_cursor = self.current_line.index(func) + len(func)
                    f = simpleeval.evaluate_current_attribute(
                            fake_cursor, self.current_line, self.interp.locals)
                except simpleeval.EvaluationError:
                    return False
        except Exception:
            # another case of needing to catch every kind of error
            # since user code is run in the case of descriptors
            # XXX: Make sure you raise here if you're debugging the completion
            # stuff !
            return False

        if inspect.isclass(f):
            class_f = None

            if (hasattr(f, '__init__') and
                    f.__init__ is not object.__init__):
                class_f = f.__init__
            if ((not class_f or
                 not inspection.getfuncprops(func, class_f)) and
                    hasattr(f, '__new__') and
                    f.__new__ is not object.__new__ and
                    # py3
                    f.__new__.__class__ is not object.__new__.__class__):

                class_f = f.__new__

            if class_f:
                f = class_f

        self.current_func = f
        self.funcprops = inspection.getfuncprops(func, f)
        if self.funcprops:
            self.arg_pos = arg_number
            return True
        self.arg_pos = None
        return False
示例#3
0
 def get_source_of_current_name(self):
     """Return the source code of the object which is bound to the
     current name in the current input line. Return `None` if the
     source cannot be found."""
     try:
         obj = self.current_func
         if obj is None:
             line = self.current_line()
             if inspection.is_eval_safe_name(line):
                 obj = self.get_object(line)
         source = inspect.getsource(obj)
     except (AttributeError, IOError, NameError, TypeError):
         return None
     else:
         return source
示例#4
0
文件: repl.py 项目: fenhl/blython
 def get_source_of_current_name(self):
     """Return the source code of the object which is bound to the
     current name in the current input line. Throw `SourceNotFound` if the
     source cannot be found."""
     obj = self.current_func
     try:
         if obj is None:
             line = self.current_line
             if not line.strip():
                 raise SourceNotFound("Nothing to get source of")
             if inspection.is_eval_safe_name(line):
                 obj = self.get_object(line)
             return inspect.getsource(obj)
     except (AttributeError, NameError), e:
         msg = "Cannot get source: " + str(e)
示例#5
0
文件: repl.py 项目: 5monkeys/bpython
 def get_source_of_current_name(self):
     """Return the source code of the object which is bound to the
     current name in the current input line. Return `None` if the
     source cannot be found."""
     try:
         obj = self.current_func
         if obj is None:
             line = self.current_line()
             if inspection.is_eval_safe_name(line):
                 obj = self.get_object(line)
         source = inspect.getsource(obj)
     except (AttributeError, IOError, NameError, TypeError):
         return None
     else:
         return source
示例#6
0
 def get_source_of_current_name(self):
     """Return the source code of the object which is bound to the
     current name in the current input line. Throw `SourceNotFound` if the
     source cannot be found. Returns bytestring in py2, unicode in py3."""
     obj = self.current_func
     try:
         if obj is None:
             line = self.current_line
             if not line.strip():
                 raise SourceNotFound(_("Nothing to get source of"))
             if inspection.is_eval_safe_name(line):
                 obj = self.get_object(line)
         return inspect.getsource(obj)
     except (AttributeError, NameError) as e:
         msg = _("Cannot get source: %s") % (str(e), )
     except IOError as e:
         msg = str(e)
     except TypeError as e:
         if "built-in" in str(e):
             msg = _("Cannot access source of %r") % (obj, )
         else:
             msg = _("No source code found for %s") % (self.current_line, )
     raise SourceNotFound(msg)
示例#7
0
    def get_source_of_current_name(self):
        """Return the unicode source code of the object which is bound to the
        current name in the current input line. Throw `SourceNotFound` if the
        source cannot be found."""

        obj = self.current_func
        try:
            if obj is None:
                line = self.current_line
                if not line.strip():
                    raise SourceNotFound(_("Nothing to get source of"))
                if inspection.is_eval_safe_name(line):
                    obj = self.get_object(line)
            return inspection.get_source_unicode(obj)
        except (AttributeError, NameError) as e:
            msg = _(u"Cannot get source: %s") % (e, )
        except IOError as e:
            msg = u"%s" % (e, )
        except TypeError as e:
            if "built-in" in u"%s" % (e, ):
                msg = _("Cannot access source of %r") % (obj, )
            else:
                msg = _("No source code found for %s") % (self.current_line, )
        raise SourceNotFound(msg)
示例#8
0
    def get_args(self):
        """Check if an unclosed parenthesis exists, then attempt to get the
        argspec() for it. On success, update self.funcprops,self.arg_pos and
        return True, otherwise set self.funcprops to None and return False"""

        self.current_func = None

        if not self.config.arg_spec:
            return False

        # Get the name of the current function and where we are in
        # the arguments
        stack = [['', 0, '']]
        try:
            for (token, value) in PythonLexer().get_tokens(
                    self.current_line):
                if token is Token.Punctuation:
                    if value in '([{':
                        stack.append(['', 0, value])
                    elif value in ')]}':
                        stack.pop()
                    elif value == ',':
                        try:
                            stack[-1][1] += 1
                        except TypeError:
                            stack[-1][1] = ''
                        stack[-1][0] = ''
                    elif value == ':' and stack[-1][2] == 'lambda':
                        stack.pop()
                    else:
                        stack[-1][0] = ''
                elif (token is Token.Name or token in Token.Name.subtypes or
                      token is Token.Operator and value == '.'):
                    stack[-1][0] += value
                elif token is Token.Operator and value == '=':
                    stack[-1][1] = stack[-1][0]
                    stack[-1][0] = ''
                elif token is Token.Keyword and value == 'lambda':
                    stack.append(['', 0, value])
                else:
                    stack[-1][0] = ''
            while stack[-1][2] in '[{':
                stack.pop()
            _, arg_number, _ = stack.pop()
            func, _, _ = stack.pop()
        except IndexError:
            return False
        if not func:
            return False

        try:
            if inspection.is_eval_safe_name(func):
                f = self.get_object(func)
            else:
                try:
                    fake_cursor = self.current_line.index(func) + len(func)
                    f = simpleeval.evaluate_current_attribute(
                            fake_cursor, self.current_line, self.interp.locals)
                except simpleeval.EvaluationError:
                    return False
        except Exception:
            # another case of needing to catch every kind of error
            # since user code is run in the case of descriptors
            # XXX: Make sure you raise here if you're debugging the completion
            # stuff !
            return False

        if inspect.isclass(f):
            class_f = None

            if (hasattr(f, '__init__') and
                    f.__init__ is not object.__init__):
                class_f = f.__init__
            if ((not class_f or
                 not inspection.getfuncprops(func, class_f)) and
                    hasattr(f, '__new__') and
                    f.__new__ is not object.__new__ and
                    # py3
                    f.__new__.__class__ is not object.__new__.__class__):

                class_f = f.__new__

            if class_f:
                f = class_f

        self.current_func = f
        self.funcprops = inspection.getfuncprops(func, f)
        if self.funcprops:
            self.arg_pos = arg_number
            return True
        self.arg_pos = None
        return False
示例#9
0
    def get_args(self):
        """Check if an unclosed parenthesis exists, then attempt to get the
        argspec() for it. On success, update self.funcprops,self.arg_pos and
        return True, otherwise set self.funcprops to None and return False"""

        self.current_func = None

        if not self.config.arg_spec:
            return False

        # Get the name of the current function and where we are in
        # the arguments
        stack = [['', 0, '']]
        try:
            for (token, value) in PythonLexer().get_tokens(self.current_line):
                if token is Token.Punctuation:
                    if value in '([{':
                        stack.append(['', 0, value])
                    elif value in ')]}':
                        stack.pop()
                    elif value == ',':
                        try:
                            stack[-1][1] += 1
                        except TypeError:
                            stack[-1][1] = ''
                        stack[-1][0] = ''
                    elif value == ':' and stack[-1][2] == 'lambda':
                        stack.pop()
                    else:
                        stack[-1][0] = ''
                elif (token is Token.Name or token in Token.Name.subtypes
                      or token is Token.Operator and value == '.'):
                    stack[-1][0] += value
                elif token is Token.Operator and value == '=':
                    stack[-1][1] = stack[-1][0]
                    stack[-1][0] = ''
                elif token is Token.Keyword and value == 'lambda':
                    stack.append(['', 0, value])
                else:
                    stack[-1][0] = ''
            while stack[-1][2] in '[{':
                stack.pop()
            _, arg_number, _ = stack.pop()
            func, _, _ = stack.pop()
        except IndexError:
            return False
        if not func:
            return False

        try:
            if inspection.is_eval_safe_name(func):
                f = self.get_object(func)
            else:
                try:
                    fake_cursor = self.current_line.index(func) + len(func)
                    f = simpleeval.evaluate_current_attribute(
                        fake_cursor, self.current_line, self.interp.locals)
                except simpleeval.EvaluationError:
                    return False
        except Exception:
            # another case of needing to catch every kind of error
            # since user code is run in the case of descriptors
            # XXX: Make sure you raise here if you're debugging the completion
            # stuff !
            return False

        if inspect.isclass(f):
            class_f = None

            if (hasattr(f, '__init__') and f.__init__ is not object.__init__):
                class_f = f.__init__
            if ((not class_f or not inspection.getfuncprops(func, class_f))
                    and hasattr(f, '__new__')
                    and f.__new__ is not object.__new__ and
                    # py3
                    f.__new__.__class__ is not object.__new__.__class__):

                class_f = f.__new__

            if class_f:
                f = class_f

        self.current_func = f
        self.funcprops = inspection.getfuncprops(func, f)
        if self.funcprops:
            self.arg_pos = arg_number
            return True
        self.arg_pos = None
        return False