sys.displayhook

Here are the examples of the python api sys.displayhook taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

75 Examples 7

Example 51

Project: ipython-yf
Source File: printfuture.py
View license
def load_ipython_extension(ip):
    class Hook(ip.displayhook_class):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.delayed = [ ]

        def delayedprint(self, future):
            self.delayed.append((future.number, future.result()))

        def pre_prompt(self, ip):
            ec = self.shell.execution_count
            try:
                for num, result in self.delayed:
                    self.shell.execution_count = num
                    sys.displayhook(result)
                self.delayed = [ ]
            finally:
                self.shell.execution_count = ec

        def __call__(self, result=None):
            super().__call__(result)
            if isinstance(result, Future):
                result.number = self.shell.execution_count
                result.add_done_callback(self.delayedprint)

    ip.displayhook_class = Hook
    ip.init_displayhook()
    ip.set_hook("pre_prompt_hook", ip.displayhook.pre_prompt)

Example 52

Project: TrustRouter
Source File: test_sys.py
View license
    def test_lost_displayhook(self):
        del sys.displayhook
        code = compile("42", "<string>", "single")
        self.assertRaises(RuntimeError, eval, code)

Example 53

Project: tuskar
Source File: eventlet_backdoor.py
View license
def initialize_if_enabled():
    backdoor_locals = {
        'exit': _dont_use_this,      # So we don't exit the entire process
        'quit': _dont_use_this,      # So we don't exit the entire process
        'fo': _find_objects,
        'pgt': _print_greenthreads,
        'pnt': _print_nativethreads,
    }

    if CONF.backdoor_port is None:
        return None

    # NOTE(johannes): The standard sys.displayhook will print the value of
    # the last expression and set it to __builtin__._, which overwrites
    # the __builtin__._ that gettext sets. Let's switch to using pprint
    # since it won't interact poorly with gettext, and it's easier to
    # read the output too.
    def displayhook(val):
        if val is not None:
            pprint.pprint(val)
    sys.displayhook = displayhook

    sock = eventlet.listen(('localhost', CONF.backdoor_port))
    port = sock.getsockname()[1]
    eventlet.spawn_n(eventlet.backdoor.backdoor_server, sock,
                     locals=backdoor_locals)
    return port

Example 54

Project: brython
Source File: test_sys.py
View license
    def test_lost_displayhook(self):
        del sys.displayhook
        code = compile("42", "<string>", "single")
        self.assertRaises(RuntimeError, eval, code)

Example 55

View license
def running_from_notebook():
    return isinstance(sys.displayhook, zmq_displayhook.ZMQShellDisplayHook)

Example 56

Project: vxfld
Source File: eventlet_backdoor.py
View license
def initialize_if_enabled(backdoor_port):
    """ Launch eventlet's backdoor server.
    """
    backdoor_locals = {
        'exit': _dont_use_this,      # So we don't exit the entire process
        'quit': _dont_use_this,      # So we don't exit the entire process
        'fo': _find_objects,
        'pgt': _print_greenthreads,
        'pnt': _print_nativethreads,
        'spew': eventlet.debug.spew,
        'unspew': eventlet.debug.unspew,
        'fhl': eventlet.debug.format_hub_listeners
    }

    sys.displayhook = _displayhook
    eventlet.debug.hub_listener_stacks(state=True)
    eventlet.debug.hub_blocking_detection(state=True)
    eventlet.spawn_n(eventlet.backdoor.backdoor_server,
                     eventlet.listen(('localhost', backdoor_port)),
                     locals=backdoor_locals)

Example 57

Project: popcorn_maker
Source File: doctests.py
View license
    def setUp(self):
        if self._result_var is not None:
            self._old_displayhook = sys.displayhook
            sys.displayhook = self._displayhook
        super(DocTestCase, self).setUp()

Example 58

Project: popcorn_maker
Source File: doctests.py
View license
    def tearDown(self):
        super(DocTestCase, self).tearDown()
        if self._result_var is not None:
            sys.displayhook = self._old_displayhook
            delattr(builtin_mod, self._result_var)

Example 59

Project: popcorn_maker
Source File: doctests.py
View license
    def setUp(self):
        if self._result_var is not None:
            self._old_displayhook = sys.displayhook
            sys.displayhook = self._displayhook
        super(DocFileCase, self).setUp()

Example 60

Project: popcorn_maker
Source File: doctests.py
View license
    def tearDown(self):
        super(DocFileCase, self).tearDown()
        if self._result_var is not None:
            sys.displayhook = self._old_displayhook
            delattr(builtin_mod, self._result_var)

Example 61

Project: popcorn_maker
Source File: doctests.py
View license
    def setUp(self):
        if self._result_var is not None:
            self._old_displayhook = sys.displayhook
            sys.displayhook = self._displayhook
        super(DocTestCase, self).setUp()

Example 62

Project: popcorn_maker
Source File: doctests.py
View license
    def tearDown(self):
        super(DocTestCase, self).tearDown()
        if self._result_var is not None:
            sys.displayhook = self._old_displayhook
            delattr(builtin_mod, self._result_var)

Example 63

Project: popcorn_maker
Source File: doctests.py
View license
    def setUp(self):
        if self._result_var is not None:
            self._old_displayhook = sys.displayhook
            sys.displayhook = self._displayhook
        super(DocFileCase, self).setUp()

Example 64

Project: popcorn_maker
Source File: doctests.py
View license
    def tearDown(self):
        super(DocFileCase, self).tearDown()
        if self._result_var is not None:
            sys.displayhook = self._old_displayhook
            delattr(builtin_mod, self._result_var)

Example 65

Project: iot-utilities
Source File: test_sys.py
View license
    def test_lost_displayhook(self):
        del sys.displayhook
        code = compile("42", "<string>", "single")
        self.assertRaises(RuntimeError, eval, code)

Example 66

Project: iot-utilities
Source File: test_sys.py
View license
    def test_lost_displayhook(self):
        del sys.displayhook
        code = compile("42", "<string>", "single")
        self.assertRaises(RuntimeError, eval, code)

Example 67

Project: iot-utilities
Source File: test_sys.py
View license
    def test_lost_displayhook(self):
        del sys.displayhook
        code = compile("42", "<string>", "single")
        self.assertRaises(RuntimeError, eval, code)

Example 68

Project: iot-utilities
Source File: test_sys.py
View license
    def test_lost_displayhook(self):
        del sys.displayhook
        code = compile("42", "<string>", "single")
        self.assertRaises(RuntimeError, eval, code)

Example 69

Project: dreampie
Source File: __init__.py
View license
    def __init__(self, port):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.connect(('localhost', port))

        # Mask SIGINT/Ctrl-C
        mask_sigint()
        
        # Make sys.displayhook change self.last_res
        self.last_res = None
        sys.displayhook = self.displayhook

        # Trick things like pdb into thinking that the namespace we create is
        # the main module
        mainmodule = types.ModuleType('__main__')
        sys.modules['__main__'] = mainmodule
        self.locs = mainmodule.__dict__
        
        # Add '' to sys.path, to be like the regular Python interpreter
        sys.path.insert(0, '')
        
        # Set sys.argv to [''], to be like the regular Python interpreter
        # (Otherwise multiprocessing on win32 starts running subp_main.py)
        sys.argv = ['']

        # Adjust exit and quit objects
        __builtin__.exit = __builtin__.quit = Quit()
        
        self.gui_handlers = [GtkHandler(), GIHandler(), Qt4Handler(), TkHandler()]
        self.idle_paused = False

        self.gid = 0
        self.flags = 0
        
        # Config
        self.is_pprint = False
        self.is_matplotlib_ia_switch = False
        self.is_matplotlib_ia_warn = False
        self.reshist_size = 0
        
        # Did we already handle matplotlib in non-interactive mode?
        self.matplotlib_ia_handled = False
        
        # The result history index of the next value to enter the history
        self.reshist_counter = 0

        # Run endless loop
        self.loop()

Example 70

Project: dreampie
Source File: __init__.py
View license
    def __init__(self, port):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.connect(('localhost', port))

        # Mask SIGINT/Ctrl-C
        mask_sigint()
        
        # Make sys.displayhook change self.last_res
        self.last_res = None
        sys.displayhook = self.displayhook

        # Trick things like pdb into thinking that the namespace we create is
        # the main module
        mainmodule = types.ModuleType('__main__')
        sys.modules['__main__'] = mainmodule
        self.locs = mainmodule.__dict__
        
        # Add '' to sys.path, to be like the regular Python interpreter
        sys.path.insert(0, '')
        
        # Set sys.argv to [''], to be like the regular Python interpreter
        # (Otherwise multiprocessing on win32 starts running subp_main.py)
        sys.argv = ['']

        # Adjust exit and quit objects
        __builtin__.exit = __builtin__.quit = Quit()
        
        self.gui_handlers = [GtkHandler(), GIHandler(), Qt4Handler(), TkHandler()]
        self.idle_paused = False

        self.gid = 0
        self.flags = 0
        
        # Config
        self.is_pprint = False
        self.is_matplotlib_ia_switch = False
        self.is_matplotlib_ia_warn = False
        self.reshist_size = 0
        
        # Did we already handle matplotlib in non-interactive mode?
        self.matplotlib_ia_handled = False
        
        # The result history index of the next value to enter the history
        self.reshist_counter = 0

        # Run endless loop
        self.loop()

Example 71

Project: blazar
Source File: eventlet_backdoor.py
View license
def initialize_if_enabled():
    backdoor_locals = {
        'exit': _dont_use_this,      # So we don't exit the entire process
        'quit': _dont_use_this,      # So we don't exit the entire process
        'fo': _find_objects,
        'pgt': _print_greenthreads,
        'pnt': _print_nativethreads,
    }

    if CONF.backdoor_port is None:
        return None

    start_port, end_port = _parse_port_range(str(CONF.backdoor_port))

    # NOTE(johannes): The standard sys.displayhook will print the value of
    # the last expression and set it to __builtin__._, which overwrites
    # the __builtin__._ that gettext sets. Let's switch to using pprint
    # since it won't interact poorly with gettext, and it's easier to
    # read the output too.
    def displayhook(val):
        if val is not None:
            pprint.pprint(val)
    sys.displayhook = displayhook

    sock = _listen('localhost', start_port, end_port, eventlet.listen)

    # In the case of backdoor port being zero, a port number is assigned by
    # listen().  In any case, pull the port number out here.
    port = sock.getsockname()[1]
    LOG.info(
        _LI('Eventlet backdoor listening on %(port)s for process %(pid)d') %
        {'port': port, 'pid': os.getpid()}
    )
    eventlet.spawn_n(eventlet.backdoor.backdoor_server, sock,
                     locals=backdoor_locals)
    return port

Example 72

Project: cloudpulse
Source File: eventlet_backdoor.py
View license
def initialize_if_enabled():
    backdoor_locals = {
        'exit': _dont_use_this,      # So we don't exit the entire process
        'quit': _dont_use_this,      # So we don't exit the entire process
        'fo': _find_objects,
        'pgt': _print_greenthreads,
        'pnt': _print_nativethreads,
    }

    if CONF.backdoor_port is None:
        return None

    start_port, end_port = _parse_port_range(str(CONF.backdoor_port))

    # NOTE(johannes): The standard sys.displayhook will print the value of
    # the last expression and set it to __builtin__._, which overwrites
    # the __builtin__._ that gettext sets. Let's switch to using pprint
    # since it won't interact poorly with gettext, and it's easier to
    # read the output too.
    def displayhook(val):
        if val is not None:
            pprint.pprint(val)
    sys.displayhook = displayhook

    sock = _listen('localhost', start_port, end_port, eventlet.listen)

    # In the case of backdoor port being zero, a port number is assigned by
    # listen().  In any case, pull the port number out here.
    port = sock.getsockname()[1]
    LOG.info(
        _LI('Eventlet backdoor listening on %(port)s for process %(pid)d'),
        {'port': port, 'pid': os.getpid()}
    )
    eventlet.spawn_n(eventlet.backdoor.backdoor_server, sock,
                     locals=backdoor_locals)
    return port

Example 73

Project: oslo.service
Source File: eventlet_backdoor.py
View license
def _initialize_if_enabled(conf):
    conf.register_opts(_options.eventlet_backdoor_opts)
    backdoor_locals = {
        'exit': _dont_use_this,      # So we don't exit the entire process
        'quit': _dont_use_this,      # So we don't exit the entire process
        'fo': _find_objects,
        'pgt': _print_greenthreads,
        'pnt': _print_nativethreads,
    }

    if conf.backdoor_port is None and conf.backdoor_socket is None:
        return None

    if conf.backdoor_socket is None:
        start_port, end_port = _parse_port_range(str(conf.backdoor_port))
        sock = _listen('localhost', start_port, end_port, eventlet.listen)
        # In the case of backdoor port being zero, a port number is assigned by
        # listen().  In any case, pull the port number out here.
        where_running = sock.getsockname()[1]
    else:
        sock = _try_open_unix_domain_socket(conf.backdoor_socket)
        where_running = conf.backdoor_socket

    # NOTE(johannes): The standard sys.displayhook will print the value of
    # the last expression and set it to __builtin__._, which overwrites
    # the __builtin__._ that gettext sets. Let's switch to using pprint
    # since it won't interact poorly with gettext, and it's easier to
    # read the output too.
    def displayhook(val):
        if val is not None:
            pprint.pprint(val)
    sys.displayhook = displayhook

    LOG.info(
        _LI('Eventlet backdoor listening on %(where_running)s for'
            ' process %(pid)d'),
        {'where_running': where_running, 'pid': os.getpid()}
    )
    thread = eventlet.spawn(eventlet.backdoor.backdoor_server, sock,
                            locals=backdoor_locals)
    return (where_running, thread)

Example 74

Project: rack
Source File: eventlet_backdoor.py
View license
def initialize_if_enabled():
    backdoor_locals = {
        'exit': _dont_use_this,      # So we don't exit the entire process
        'quit': _dont_use_this,      # So we don't exit the entire process
        'fo': _find_objects,
        'pgt': _print_greenthreads,
        'pnt': _print_nativethreads,
    }

    if CONF.backdoor_port is None:
        return None

    start_port, end_port = _parse_port_range(str(CONF.backdoor_port))

    # NOTE(johannes): The standard sys.displayhook will print the value of
    # the last expression and set it to __builtin__._, which overwrites
    # the __builtin__._ that gettext sets. Let's switch to using pprint
    # since it won't interact poorly with gettext, and it's easier to
    # read the output too.
    def displayhook(val):
        if val is not None:
            pprint.pprint(val)
    sys.displayhook = displayhook

    sock = _listen('localhost', start_port, end_port, eventlet.listen)

    # In the case of backdoor port being zero, a port number is assigned by
    # listen().  In any case, pull the port number out here.
    port = sock.getsockname()[1]
    LOG.info(_('Eventlet backdoor listening on %(port)s for process %(pid)d') %
             {'port': port, 'pid': os.getpid()})
    eventlet.spawn_n(eventlet.backdoor.backdoor_server, sock,
                     locals=backdoor_locals)
    return port

Example 75

Project: sticks
Source File: eventlet_backdoor.py
View license
def initialize_if_enabled():
    backdoor_locals = {
        'exit': _dont_use_this,      # So we don't exit the entire process
        'quit': _dont_use_this,      # So we don't exit the entire process
        'fo': _find_objects,
        'pgt': _print_greenthreads,
        'pnt': _print_nativethreads,
    }

    if CONF.backdoor_port is None:
        return None

    start_port, end_port = _parse_port_range(str(CONF.backdoor_port))

    # NOTE(johannes): The standard sys.displayhook will print the value of
    # the last expression and set it to __builtin__._, which overwrites
    # the __builtin__._ that gettext sets. Let's switch to using pprint
    # since it won't interact poorly with gettext, and it's easier to
    # read the output too.
    def displayhook(val):
        if val is not None:
            pprint.pprint(val)
    sys.displayhook = displayhook

    sock = _listen('localhost', start_port, end_port, eventlet.listen)

    # In the case of backdoor port being zero, a port number is assigned by
    # listen().  In any case, pull the port number out here.
    port = sock.getsockname()[1]
    LOG.info(
        _LI('Eventlet backdoor listening on %(port)s for process %(pid)d') %
        {'port': port, 'pid': os.getpid()}
    )
    eventlet.spawn_n(eventlet.backdoor.backdoor_server, sock,
                     locals=backdoor_locals)
    return port