Skip to content

wishlist/enh: better handling of AutoMagic with call parens (quit() and exit() ought not NameError, at least) #342

Closed
dwf opened this Issue Apr 8, 2011 · 8 comments

3 participants

@dwf
dwf commented Apr 8, 2011

Related to #337, people coming from generations-old IPython might still be tempted to type quit() and be miffed to find out it doesn't exist anymore. Ideally these two should probably 'just work', or the error should at least be something more sensible than a NameError.

In general trying to use automagic with call () parens should probably not NameError, but rather direct the user to %magic. I've been looking at InteractiveShell for a way to do this generally but I'm not sure there is a good one. Seems like catching a NameError in run_code is the way to go but we're already several levels down the call stack from being able to inspect the raw code to see if it is a magic gone wrong.

@takluyver
IPython member

I'd start by looking at amending AutomagicChecker (in core/prefilter.py) to catch these cases. We should already be checking we're not colliding with the user namespace.

@dwf
dwf commented Apr 8, 2011

Had a look at that already, the trouble I see is a) there's no way that I can see to cleanly halt processing and tell the user they made a mistake from within a Checker (though it just occurred to me that you might be able to do it via a custom Handler class that messes with the shell object), b) the whole collisions issue: right now "guiref = lambda x: 5" means that subsequent calls to guiref(3) get handed off to that lambda function, and I think that's the correct behaviour. So I guess this handler would have to inspect the user namespace for things that shadow a magic command and defer to that, but is there a way for the handler to abort and let the checker pipeline continue?

@takluyver
IPython member
@dwf
dwf commented Apr 9, 2011

Wait, checkers have a reference to the shell object too. So I guess a) is possible via a call to showtraceback(), and this is all accomplishable with some care in AutoMagicChecker.

@takluyver
IPython member

I'm not sure that calling showtraceback in a checker is the right way to do it. That sounds messy. But you could try the shell.auto_rewrite_input method in the handler. So exit() gets rewritten to exit.

@dwf
dwf commented Apr 9, 2011

Well, you only want to rewrite it in the case that there is no variable 'exit' in the user namespace; so I guess the AutoMagicChecker would conditionally dispatch one handler or the other? (Or none). I agree auto_rewrite_input should be called in that case.

I think exit() and quit() should probably be rewritten, but it's unclear what to do about other magics, if anything. That's where showtraceback would come in; raise another exception explaining that you don't call magics with parens like that in general. But I guess the two issues can be dealt with in separate patches.

@takluyver
IPython member
@minrk minrk removed the type-enhancement label Jan 14, 2015
@takluyver takluyver modified the milestone: old, wishlist Feb 19, 2015
@takluyver
IPython member

exit() and quit() now seem to work fine.

@takluyver takluyver closed this Feb 19, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.