Skip to content

Commit

Permalink
Merge pull request ipython#2329 from bfroehle/usage_error
Browse files Browse the repository at this point in the history
Better error messages for common magic commands.
  • Loading branch information
Carreau committed Aug 25, 2012
2 parents f3de03e + c304fdc commit d4b3ac9
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 8 deletions.
7 changes: 6 additions & 1 deletion IPython/core/magics/code.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
7 changes: 1 addition & 6 deletions IPython/frontend/qt/console/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,6 @@ def populate_all_magic_menu(self, listofmagic=None):
self.all_magic_menu.clear()


protected_magic = set(["more","less","load_ext","pycat","loadpy","load","save","psource"])
mlist=ast.literal_eval(listofmagic)
for magic in mlist:
cell = (magic['type'] == 'cell')
Expand All @@ -625,11 +624,7 @@ def populate_all_magic_menu(self, listofmagic=None):
prefix='%'
magic_menu = self._get_magic_menu(mclass)

if name in protected_magic:
suffix = '?'
else :
suffix = ''
pmagic = '%s%s%s'%(prefix,name,suffix)
pmagic = '%s%s'%(prefix,name)

xaction = QtGui.QAction(pmagic,
self,
Expand Down
4 changes: 4 additions & 0 deletions IPython/zmq/zmqshell.py
Original file line number Diff line number Diff line change
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 d4b3ac9

Please sign in to comment.