diff --git a/IPython/core/magics/basic.py b/IPython/core/magics/basic.py index 11da19b..397df88 100644 --- a/IPython/core/magics/basic.py +++ b/IPython/core/magics/basic.py @@ -601,6 +601,8 @@ Defaulting color scheme to 'NoColor'""" if args.export: cells = [] hist = list(self.shell.history_manager.get_range()) + if(len(hist)<=1): + raise ValueError('History is empty, cannot export') for session, execution_count, input in hist[:-1]: cells.append(v4.new_code_cell( execution_count=execution_count, diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index d4e5104..e1ec546 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -623,9 +623,13 @@ def test_extension(): @skipIf(dec.module_not_available('IPython.nbformat'), 'nbformat not importable') class NotebookExportMagicTests(TestCase): def test_notebook_export_json(self): + _ip = get_ipython() + _ip.history_manager.reset() # Clear any existing history. + cmds = [u"a=1", u"def b():\n return a**2", u"print(a, b())"] + for i, cmd in enumerate(cmds, start=1): + _ip.history_manager.store_inputs(i, cmd) with TemporaryDirectory() as td: outfile = os.path.join(td, "nb.ipynb") - _ip.ex(py3compat.u_format(u"u = {u}'héllo'")) _ip.magic("notebook -e %s" % outfile)