Skip to content

Commit

Permalink
Better error messages for common magic commands.
Browse files Browse the repository at this point in the history
Print a helpful message when the user calls a magic command with a missing
argument, rather than a long traceback.
  • Loading branch information
bfroehle committed Aug 23, 2012
1 parent 144f08a commit 4932093
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 2 deletions.
7 changes: 6 additions & 1 deletion IPython/core/magics/code.py
Expand Up @@ -21,7 +21,7 @@
from urllib2 import urlopen

# Our own packages
from IPython.core.error import TryNext, StdinNotImplementedError
from IPython.core.error import TryNext, StdinNotImplementedError, UsageError
from IPython.core.macro import Macro
from IPython.core.magic import Magics, magics_class, line_magic
from IPython.core.oinspect import find_file, find_source_lines
Expand Down Expand Up @@ -73,6 +73,8 @@ def save(self, parameter_s=''):
"""

opts,args = self.parse_options(parameter_s,'fra',mode='list')
if not args:
raise UsageError('Missing filename.')
raw = 'r' in opts
force = 'f' in opts
append = 'a' in opts
Expand Down Expand Up @@ -178,6 +180,9 @@ def load(self, arg_s):
%load http://www.example.com/myscript.py
"""
opts,args = self.parse_options(arg_s,'y')
if not args:
raise UsageError('Missing filename, URL, input history range, '
'or macro.')

contents = self.shell.find_user_code(args)
l = len(contents)
Expand Down
7 changes: 7 additions & 0 deletions IPython/core/magics/extension.py
Expand Up @@ -16,6 +16,7 @@
import os

# Our own packages
from IPython.core.error import UsageError
from IPython.core.magic import Magics, magics_class, line_magic

#-----------------------------------------------------------------------------
Expand Down Expand Up @@ -56,14 +57,20 @@ def install_ext(self, parameter_s=''):
@line_magic
def load_ext(self, module_str):
"""Load an IPython extension by its module name."""
if not module_str:
raise UsageError('Missing module name.')
return self.shell.extension_manager.load_extension(module_str)

@line_magic
def unload_ext(self, module_str):
"""Unload an IPython extension by its module name."""
if not module_str:
raise UsageError('Missing module name.')
self.shell.extension_manager.unload_extension(module_str)

@line_magic
def reload_ext(self, module_str):
"""Reload an IPython extension by its module name."""
if not module_str:
raise UsageError('Missing module name.')
self.shell.extension_manager.reload_extension(module_str)
4 changes: 3 additions & 1 deletion IPython/core/magics/namespace.py
Expand Up @@ -19,7 +19,7 @@

# Our own packages
from IPython.core import page
from IPython.core.error import StdinNotImplementedError
from IPython.core.error import StdinNotImplementedError, UsageError
from IPython.core.magic import Magics, magics_class, line_magic
from IPython.testing.skipdoctest import skip_doctest
from IPython.utils.encoding import DEFAULT_ENCODING
Expand Down Expand Up @@ -92,6 +92,8 @@ def pdoc(self, parameter_s='', namespaces=None):
@line_magic
def psource(self, parameter_s='', namespaces=None):
"""Print (or run through pager) the source code for an object."""
if not parameter_s:
raise UsageError('Missing object name.')
self.shell._inspect('psource',parameter_s, namespaces)

@line_magic
Expand Down
3 changes: 3 additions & 0 deletions IPython/core/magics/osm.py
Expand Up @@ -681,6 +681,9 @@ def pycat(self, parameter_s=''):
%pycat myMacro
%pycat http://www.example.com/myscript.py
"""
if not parameter_s:
raise UsageError('Missing filename, URL, input history range, '
'or macro.')

try :
cont = self.shell.find_user_code(parameter_s)
Expand Down
4 changes: 4 additions & 0 deletions IPython/zmq/zmqshell.py
Expand Up @@ -30,6 +30,7 @@
from IPython.core import page
from IPython.core.autocall import ZMQExitAutocall
from IPython.core.displaypub import DisplayPublisher
from IPython.core.error import UsageError
from IPython.core.magics import MacroToEdit, CodeMagics
from IPython.core.magic import magics_class, line_magic, Magics
from IPython.core.payloadpage import install_payload_page
Expand Down Expand Up @@ -349,6 +350,9 @@ def less(self, arg_s):
"""Show a file through the pager.
Files ending in .py are syntax-highlighted."""
if not arg_s:
raise UsageError('Missing filename.')

cont = open(arg_s).read()
if arg_s.endswith('.py'):
cont = self.shell.pycolorize(cont)
Expand Down

0 comments on commit 4932093

Please sign in to comment.