The Sage Input Hook

This is a hook into the IPython input prompt and will be called periodically (every 100ms) while Python is sitting idle. We use it to reload attached files if they have changed.

IPython has analogous code to set an input hook, but we are not using their implementation. For once, it unsets signal handlers which will disable Ctrl-C.

sage.repl.inputhook.install()

Install the Sage input hook

EXAMPLES:

sage: from sage.repl.inputhook import install
sage: install()
sage.repl.inputhook.sage_inputhook()

The input hook.

This function will be called every 100ms when IPython is idle at the command prompt.

EXAMPLES:

sage: from sage.repl.interpreter import get_test_shell
sage: shell = get_test_shell()
sage: tmp = tmp_filename(ext='.py')
sage: f = open(tmp, 'w'); f.write('a = 2\n'); f.close()
sage: shell.run_cell('%attach ' + tmp)
sage: shell.run_cell('a')
2
sage: sleep(1)  # filesystem timestamp granularity
sage: f = open(tmp, 'w'); f.write('a = 3\n'); f.close()

Note that the doctests are never really at the command prompt, so we call the input hook manually:

sage: shell.run_cell('from sage.repl.inputhook import sage_inputhook')
sage: shell.run_cell('sage_inputhook()')
### reloading attached file tmp_....py modified at ... ###
0

sage: shell.run_cell('a')
3
sage: shell.run_cell('detach({0})'.format(repr(tmp)))
sage: shell.run_cell('attached_files()')
[]
sage: shell.quit()
sage.repl.inputhook.uninstall()

Uninstall the Sage input hook

EXAMPLES:

sage: from sage.repl.inputhook import uninstall
sage: uninstall()