magic in IPythonDemo fails when not located at top of demo file #1102

Closed
agardelein opened this Issue Dec 4, 2011 · 3 comments

Projects

None yet

3 participants

@agardelein

Recently switched from 0.10 to 0.11 and found this which is apparently a regression (neglecting messages related to history logging)

bomberx% cat aa.py
lsmagic

bomberx% cat aa2.py
print 2222
lsmagic

bomberx% ipython --automagic
Python 2.7.2+ (default, Nov 30 2011, 19:22:03)
Type "copyright", "credits" or "license" for more information.

IPython 0.11 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.

In [1]: from IPython.lib.demo import IPythonDemo

In [2]: mydemo1=IPythonDemo('aa.py')

In [3]: mydemo1()
********************** <aa.py> block # 0 (0 remaining) **********************
lsmagic

****************** Press to quit, to execute... ******************
Available magic functions:
%alias %autocall %autoindent %automagic %bookmark %cd %colors %cpaste %debug %dhist %dirs %doctest_mode %ed %edit %env %gui %hist %history %install_default_config %install_profiles %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %lsmagic %macro %magic %page %paste %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %popd %pprint %precision %profile %prun %psearch %psource %pushd %pwd %pycat %pylab %quickref %recall %rehashx %reload_ext %rep %rerun %reset %reset_selective %run %save %sc %sx %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode

Automagic is ON, % prefix NOT needed for magic functions.

******************************** END OF DEMO ********************************
************** Use <demo_name>.reset() if you want to rerun it. **************

In [5]: ERROR! Session/line number was not unique in database. History logging moved to new session 34

In [5]: mydemo2=IPythonDemo('aa2.py')

In [6]: mydemo2()
********************** <aa2.py> block # 0 (0 remaining) **********************
print 2222
lsmagic

****************** Press to quit, to execute... ******************
2222


NameError Traceback (most recent call last)
/home/arnaud/src/scope/ in ()
1 print 2222
----> 2 lsmagic

NameError: name 'lsmagic' is not defined

******************************** END OF DEMO ********************************
************** Use <demo_name>.reset() if you want to rerun it. **************

In [8]:

@minrk
Member
minrk commented Dec 4, 2011

It's not quite 'top of demo file', because the issue applies to each segment of the demo.

The source of the difference:

In >= 0.11, each segment is treated as one cell (a single multiline input). The magic transform doesn't act on multiline cells, so lsmagic is left unchanged.

Possible real fix:

split ast nodes, so that lines will be treated as separate inputs in the demo. This actually won't work, because IPython code is not parse-able as Python code, but perhaps something similar could be done.

User fix: don't rely on automagic or autocall in multiline segments.

@takluyver
Member

I believe there is an option to do the prefiltering with multiline_specials, so it will allow automagics on more than a single line.

The warning about history should (I think) be gone in the development version.

@agardelein

IPythonLineDemo with a loop seems a workaround solution as it is on a line by line basis.

bomberx% ipython --automagic
Python 2.7.2+ (default, Nov 30 2011, 19:22:03)
Type "copyright", "credits" or "license" for more information.

IPython 0.11 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.

In [1]: from IPython.lib.demo import IPythonLineDemo

In [2]: mydemo=IPythonLineDemo('aa2.py')

In [3]: while not mydemo.finished:
...: mydemo()
...:
********************** <aa2.py> block # 0 (1 remaining) **********************
print 2222

********************************** output: **********************************
2222
********************** <aa2.py> block # 1 (0 remaining) **********************
lsmagic

********************************** output: **********************************
ERROR! Session/line number was not unique in database. History logging moved to new session 41
Available magic functions:
%alias %autocall %autoindent %automagic %bookmark %cd %colors %cpaste %debug %dhist %dirs %doctest_mode %ed %edit %env %gui %hist %history %install_default_config %install_profiles %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %lsmagic %macro %magic %page %paste %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %popd %pprint %precision %profile %prun %psearch %psource %pushd %pwd %pycat %pylab %quickref %recall %rehashx %reload_ext %rep %rerun %reset %reset_selective %run %save %sc %sx %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode

Automagic is ON, % prefix NOT needed for magic functions.

******************************** END OF DEMO ********************************
************** Use <demo_name>.reset() if you want to rerun it. **************

In [6]:

@agardelein agardelein closed this Dec 7, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment