Exemple #1
0
    def __call__(self, obj, p, cycle):
        r"""
        Format matrix.

        INPUT:

        - ``obj`` -- anything. Object to format.

        - ``p`` -- PrettyPrinter instance.

        - ``cycle`` -- boolean. Whether there is a cycle.

        OUTPUT:

        Boolean. Whether the representer is applicable to ``obj``. If
        ``True``, the string representation is appended to ``p``.

        EXAMPLES::

            sage: from sage.repl.display.fancy_repr import PlainPythonRepr
            sage: pp = PlainPythonRepr()
            sage: pp.format_string(type(1))
            "<type 'sage.rings.integer.Integer'>"

        Do not swallow a trailing newline at the end of the output of
        a custom representer. Note that it is undesirable to have a
        trailing newline, and if we don't display it you can't fix
        it::
    
            sage: class Newline(object):
            ....:     def __repr__(self):
            ....:         return 'newline\n'
            sage: n = Newline()
            sage: pp.format_string(n)
            'newline\n'
            sage: pp.format_string([n, n, n])
            '[newline\n, newline\n, newline\n]'
            sage: [n, n, n]
            [newline
             , newline
             , newline
             ]
        """
        klass = _safe_getattr(obj, '__class__', None) or type(obj)
        klass_repr = _safe_getattr(klass, '__repr__', None)
        if klass_repr in _baseclass_reprs:
            p.text(klass_repr(obj))
        else:
            # A user-provided repr. Find newlines and replace them with p.break_()
            output = _safe_repr(obj)
            for idx, output_line in enumerate(output.split('\n')):
                if idx:
                    p.break_()
                p.text(output_line)
        return True
Exemple #2
0
    def __call__(self, obj, p, cycle):
        r"""
        Format matrix.

        INPUT:

        - ``obj`` -- anything. Object to format.

        - ``p`` -- PrettyPrinter instance.

        - ``cycle`` -- boolean. Whether there is a cycle.

        OUTPUT:

        Boolean. Whether the representer is applicable to ``obj``. If
        ``True``, the string representation is appended to ``p``.

        EXAMPLES::

            sage: from sage.repl.display.fancy_repr import PlainPythonRepr
            sage: pp = PlainPythonRepr()
            sage: pp.format_string(type(1))
            "<type 'sage.rings.integer.Integer'>"

        Do not swallow a trailing newline at the end of the output of
        a custom representer. Note that it is undesirable to have a
        trailing newline, and if we don't display it you can't fix
        it::
    
            sage: class Newline(object):
            ....:     def __repr__(self):
            ....:         return 'newline\n'
            sage: n = Newline()
            sage: pp.format_string(n)
            'newline\n'
            sage: pp.format_string([n, n, n])
            '[newline\n, newline\n, newline\n]'
            sage: [n, n, n]
            [newline
             , newline
             , newline
             ]
        """
        klass = _safe_getattr(obj, '__class__', None) or type(obj)
        klass_repr = _safe_getattr(klass, '__repr__', None)
        if klass_repr in _baseclass_reprs:
            p.text(klass_repr(obj))
        else:
            # A user-provided repr. Find newlines and replace them with p.break_()
            output = _safe_repr(obj)
            for idx, output_line in enumerate(output.split('\n')):
                if idx:
                    p.break_()
                p.text(output_line)
        return True
    def _safe_get_formatter_method(obj, name):
        """Safely get a formatter method

        Enable classes formatter method
        """
        method = pretty._safe_getattr(obj, name, None)
        if callable(method):
            # obj claims to have repr method...
            if callable(pretty._safe_getattr(obj, '_ipython_canary_method_should_not_exist_', None)):
                # ...but don't trust proxy objects that claim to have everything
                return None
            return method
def _safe_get_formatter_method(obj, name):
    """Safely get a formatter method
    
    - Classes cannot have formatter methods, only instance
    - protect against proxy objects that claim to have everything
    """
    if inspect.isclass(obj):
        # repr methods only make sense on instances, not classes
        return None
    method = pretty._safe_getattr(obj, name, None)
    if callable(method):
        # obj claims to have repr method...
        if callable(pretty._safe_getattr(obj, '_ipython_canary_method_should_not_exist_', None)):
            # ...but don't trust proxy objects that claim to have everything
            return None
        return method
Exemple #5
0
def _safe_get_formatter_method(obj, name):
    """Safely get a formatter method
    
    - Classes cannot have formatter methods, only instance
    - protect against proxy objects that claim to have everything
    """
    if inspect.isclass(obj):
        # repr methods only make sense on instances, not classes
        return None
    method = pretty._safe_getattr(obj, name, None)
    if callable(method):
        # obj claims to have repr method...
        if callable(pretty._safe_getattr(obj, '_ipython_canary_method_should_not_exist_', None)):
            # ...but don't trust proxy objects that claim to have everything
            return None
        return method
Exemple #6
0
 def __call__(self, obj):
     """Compute the format for an object."""
     if self.enabled:
         # lookup registered printer
         try:
             printer = self.lookup(obj)
         except KeyError:
             pass
         else:
             return printer(obj)
         # Finally look for special method names
         method = pretty._safe_getattr(obj, self.print_method, None)
         if method is not None:
             return method()
         return None
     else:
         return None
Exemple #7
0
 def __call__(self, obj):
     """Compute the format for an object."""
     if self.enabled:
         # lookup registered printer
         try:
             printer = self.lookup(obj)
         except KeyError:
             pass
         else:
             return printer(obj)
         # Finally look for special method names
         method = pretty._safe_getattr(obj, self.print_method, None)
         if method is not None:
             return method()
         return None
     else:
         return None
Exemple #8
0
def _safe_get_formatter_method(obj, name):
    """Safely get a formatter method"""
    method = pretty._safe_getattr(obj, name, None)
    # formatter methods must be bound
    if _valid_formatter(method):
        return method
Exemple #9
0
    def __call__(self, obj, p, cycle):
        r"""
        Format matrix.

        INPUT:

        - ``obj`` -- anything. Object to format.

        - ``p`` -- PrettyPrinter instance.

        - ``cycle`` -- boolean. Whether there is a cycle.

        OUTPUT:

        Boolean. Whether the representer is applicable to ``obj``. If
        ``True``, the string representation is appended to ``p``.

        EXAMPLES::

            sage: from sage.repl.display.fancy_repr import PlainPythonRepr
            sage: pp = PlainPythonRepr()
            sage: pp.format_string(type(1))
            "<type 'sage.rings.integer.Integer'>"

        Do not swallow a trailing newline at the end of the output of
        a custom representer. Note that it is undesirable to have a
        trailing newline, and if we don't display it you can't fix
        it::

            sage: class Newline(object):
            ....:     def __repr__(self):
            ....:         return 'newline\n'
            sage: n = Newline()
            sage: pp.format_string(n)
            'newline\n'
            sage: pp.format_string([n, n, n])
            '[newline\n, newline\n, newline\n]'
            sage: [n, n, n]
            [newline
             , newline
             , newline
             ]
        """
        klass = _safe_getattr(obj, "__class__", None) or type(obj)
        klass_repr = _safe_getattr(klass, "__repr__", None)
        if klass_repr in _baseclass_reprs:
            p.text(klass_repr(obj))
        else:
            # A user-provided repr. Find newlines and replace them with p.break_()
            try:
                output = repr(obj)
            except Exception:
                import sys, traceback

                objrepr = object.__repr__(obj).replace("object at", "at")
                exc = traceback.format_exception_only(sys.exc_info()[0], sys.exc_info()[1])
                exc = ("".join(exc)).strip()
                output = "<repr({}) failed: {}>".format(objrepr, exc)
            for idx, output_line in enumerate(output.split("\n")):
                if idx:
                    p.break_()
                p.text(output_line)
        return True