diff --git a/IPython/DPyGetOpt.py b/IPython/DPyGetOpt.py index d23f1da..dd2cf86 100644 --- a/IPython/DPyGetOpt.py +++ b/IPython/DPyGetOpt.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """DPyGetOpt -- Demiurge Python GetOptions Module - $Id: DPyGetOpt.py 1980 2006-12-12 21:48:46Z vivainio $ + $Id: DPyGetOpt.py 2872 2007-11-25 17:58:05Z fperez $ This module is modeled after perl's Getopt::Long module-- which is, in turn, modeled after GNU's extended getopt() function. @@ -33,7 +33,7 @@ and -baz options that appear on within the parsed argument list must have a real number argument and that the accumulated list of values will be available under the name 'foo' -$Id: DPyGetOpt.py 1980 2006-12-12 21:48:46Z vivainio $""" +$Id: DPyGetOpt.py 2872 2007-11-25 17:58:05Z fperez $""" #***************************************************************************** # @@ -74,9 +74,18 @@ import string import sys import types -arg_error = 'DPyGetOpt Argument Error' -spec_error = 'DPyGetOpt Specification Error' -term_error = 'DPyGetOpt Termination Error' +class Error(Exception): + """Base class for exceptions in the DPyGetOpt module.""" + +class ArgumentError(Error): + """Exception indicating an error in the arguments passed to + DPyGetOpt.processArguments.""" + +class SpecificationError(Error): + """Exception indicating an error with an option specification.""" + +class TerminationError(Error): + """Exception indicating an error with an option processing terminator.""" specificationExpr = re.compile('(?P.)(?P.)(?P@?)') @@ -214,7 +223,7 @@ class DPyGetOpt: """ Adds the option described by oTuple (name, (type, mode, default), alias) to optionTuples. Adds index keyed under name - to optionNames. Raises spec_error if name already in + to optionNames. Raises SpecificationError if name already in optionNames """ (name, (type, mode, default, multi), realName) = oTuple @@ -222,11 +231,13 @@ class DPyGetOpt: # verify name and add to option names dictionary if self.optionNames.has_key(name): if realName: - raise spec_error, 'Alias \'' + name + '\' for \'' + realName + \ - '\' already used for another option or alias.' + raise SpecificationError('Alias \'' + name + '\' for \'' + + realName + + '\' already used for another option or alias.') else: - raise spec_error, 'Option named \'' + name + \ - '\' specified more than once. Specification: ' + option + raise SpecificationError('Option named \'' + name + + '\' specified more than once. Specification: ' + + option) # validated. add to optionNames self.optionNames[name] = self.tupleIndex @@ -244,11 +255,13 @@ class DPyGetOpt: # verify name and add to option names dictionary if self.optionNames.has_key(alias): if realName: - raise spec_error, 'Negated alias \'' + name + '\' for \'' + realName + \ - '\' already used for another option or alias.' + raise SpecificationError('Negated alias \'' + name + + '\' for \'' + realName + + '\' already used for another option or alias.') else: - raise spec_error, 'Negated option named \'' + name + \ - '\' specified more than once. Specification: ' + option + raise SpecificationError('Negated option named \'' + name + + '\' specified more than once. Specification: ' + + option) # validated. add to optionNames self.optionNames[alias] = self.tupleIndex @@ -299,7 +312,8 @@ class DPyGetOpt: # break into names, specification match = splitExpr.match(option) if match is None: - raise spec_error, 'Invalid specification {' + option + '}' + raise SpecificationError('Invalid specification {' + option + + '}') names = match.group('names') specification = match.group('spec') @@ -328,7 +342,8 @@ class DPyGetOpt: match = specificationExpr.match(specification) if match is None: # failed to parse, die - raise spec_error, 'Invalid configuration for option \'' + option + '\'' + raise SpecificationError('Invalid configuration for option \'' + + option + '\'') # determine mode required = match.group('required') @@ -337,7 +352,8 @@ class DPyGetOpt: elif required == ':': argMode = ArgOptional else: - raise spec_error, 'Unknown requirement configuration \'' + required + '\'' + raise SpecificationError('Unknown requirement configuration \'' + + required + '\'') # determine type type = match.group('type') @@ -351,7 +367,8 @@ class DPyGetOpt: argType = RealArgType argDefault = 1 else: - raise spec_error, 'Unknown type specifier \'' + type + '\'' + raise SpecificationError('Unknown type specifier \'' + + type + '\'') # determine quantity if match.group('multi') == '@': @@ -425,7 +442,7 @@ class DPyGetOpt: terminator. If it is, sets self.terminator to the full name of the terminator. - If more than one terminator matched, raises a term_error with a + If more than one terminator matched, raises a TerminationError with a string describing the ambiguity. """ @@ -445,8 +462,8 @@ class DPyGetOpt: if not len(terms): return None elif len(terms) > 1: - raise term_error, 'Ambiguous terminator \'' + optionName + \ - '\' matches ' + repr(terms) + raise TerminationError('Ambiguous terminator \'' + optionName + + '\' matches ' + repr(terms)) self.terminator = terms[0] return self.terminator @@ -529,10 +546,11 @@ class DPyGetOpt: tuples = self._getArgTuple(optName) if tuples == None: - raise arg_error, 'Illegal option \'' + arg + '\'' + raise ArgumentError('Illegal option \'' + arg + '\'') elif len(tuples) > 1: - raise arg_error, 'Ambiguous option \'' + arg + '\'; matches ' + \ - repr(map(lambda x: x[0], tuples)) + raise ArgumentError('Ambiguous option \'' + arg + + '\'; matches ' + + repr(map(lambda x: x[0], tuples))) else: config = tuples[0] @@ -545,8 +563,9 @@ class DPyGetOpt: if (optMode == ArgRequired): if (not nextArg) or self._isTerminator(nextArg): # print nextArg - raise arg_error, 'Option \'' + arg + \ - '\' requires an argument of type ' + optType + raise ArgumentError('Option \'' + arg + + '\' requires an argument of type ' + + optType) if (not optMode == None) and nextArg and (not self._isTerminator(nextArg)): # nextArg defined, option configured to possibly consume arg @@ -559,15 +578,17 @@ class DPyGetOpt: except: # only raise conversion error if REQUIRED to consume argument if optMode == ArgRequired: - raise arg_error, 'Invalid argument to option \'' + arg + \ - '\'; should be \'' + optType + '\'' + raise ArgumentError('Invalid argument to option \'' + + arg + '\'; should be \'' + + optType + '\'') else: optionValue = optDefault - except arg_error: - raise arg_error, sys.exc_value + except ArgumentError: + raise except: - raise arg_error, '(' + arg + \ - ') Conversion function for \'' + optType + '\' not found.' + raise ArgumentError('(' + arg + + ') Conversion function for \'' + + optType + '\' not found.') else: optionValue = optDefault @@ -583,7 +604,8 @@ class DPyGetOpt: else: # only one value per if self.isPosixCompliant and self.optionValues.has_key(realName): - raise arg_error, 'Argument \'' + arg + '\' occurs multiple times.' + raise ArgumentError('Argument \'' + arg + + '\' occurs multiple times.') self.optionValues[realName] = optionValue @@ -610,25 +632,25 @@ def _test(): """ try: DPyGetOpt(['foo', 'bar=s', 'foo']) - except: - print 'EXCEPTION (should be \'foo\' already used..): ' + sys.exc_value + except Error, exc: + print 'EXCEPTION (should be \'foo\' already used..): %s' % exc try: DPyGetOpt(['foo|bar|apple=s@', 'baz|apple!']) - except: - print 'EXCEPTION (should be duplicate alias/name error): ' + sys.exc_value + except Error, exc: + print 'EXCEPTION (should be duplicate alias/name error): %s' % exc x = DPyGetOpt(['apple|atlas=i@', 'application|executable=f@']) try: x.processArguments(['-app', '29.3']) - except: - print 'EXCEPTION (should be ambiguous argument): ' + sys.exc_value + except Error, exc: + print 'EXCEPTION (should be ambiguous argument): %s' % exc x = DPyGetOpt(['foo'], ['antigravity', 'antithesis']) try: x.processArguments(['-foo', 'anti']) - except: - print 'EXCEPTION (should be ambiguous terminator): ' + sys.exc_value + except Error, exc: + print 'EXCEPTION (should be ambiguous terminator): %s' % exc profile = ['plain-option', 'boolean-option!', diff --git a/IPython/Magic.py b/IPython/Magic.py index f75bc96..ad0d875 100644 --- a/IPython/Magic.py +++ b/IPython/Magic.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """Magic functions for InteractiveShell. -$Id: Magic.py 2841 2007-10-10 00:17:26Z fperez $""" +$Id: Magic.py 2872 2007-11-25 17:58:05Z fperez $""" #***************************************************************************** # Copyright (C) 2001 Janko Hauser and @@ -1405,8 +1405,9 @@ Currently the magic system has the following functions:\n""" The file is executed in a namespace initially consisting only of __name__=='__main__' and sys.argv constructed as indicated. It thus - sees its environment as if it were being run as a stand-alone - program. But after execution, the IPython interactive namespace gets + sees its environment as if it were being run as a stand-alone program + (except for sharing global objects such as previously imported + modules). But after execution, the IPython interactive namespace gets updated with all variables defined in the program (except for __name__ and sys.argv). This allows for very convenient loading of code for interactive work, while giving each program a 'clean sheet' to run in. diff --git a/IPython/genutils.py b/IPython/genutils.py index b43b8d8..1dd9a0b 100644 --- a/IPython/genutils.py +++ b/IPython/genutils.py @@ -5,7 +5,7 @@ General purpose utilities. This is a grab-bag of stuff I find useful in most programs I write. Some of these things are also convenient when working at the command line. -$Id: genutils.py 2847 2007-10-24 15:16:24Z vivainio $""" +$Id: genutils.py 2872 2007-11-25 17:58:05Z fperez $""" #***************************************************************************** # Copyright (C) 2001-2006 Fernando Perez. @@ -620,9 +620,9 @@ def process_cmdline(argv,names=[],defaults={},usage=''): try: getopt.processArguments(argv) - except: + except DPyGetOpt.ArgumentError, exc: print usage - warn(`sys.exc_value`,level=4) + warn('"%s"' % exc,level=4) defaults.update(getopt.optionValues) args = getopt.freeValues diff --git a/IPython/ipmaker.py b/IPython/ipmaker.py index 1676e0e..953b459 100644 --- a/IPython/ipmaker.py +++ b/IPython/ipmaker.py @@ -6,7 +6,7 @@ Requires Python 2.1 or better. This file contains the main make_IPython() starter function. -$Id: ipmaker.py 2723 2007-09-07 07:44:16Z fperez $""" +$Id: ipmaker.py 2872 2007-11-25 17:58:05Z fperez $""" #***************************************************************************** # Copyright (C) 2001-2006 Fernando Perez. @@ -301,9 +301,9 @@ object? -> Details about 'object'. ?object also works, ?? prints more. try: getopt.processArguments(argv) - except: + except DPyGetOpt.ArgumentError, exc: print cmd_line_usage - warn('\nError in Arguments: ' + `sys.exc_value`) + warn('\nError in Arguments: "%s"' % exc) sys.exit(1) # convert the options dict to a struct for much lighter syntax later diff --git a/doc/ChangeLog b/doc/ChangeLog index f4121d9..91e1752 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,10 @@ +2007-11-24 Fernando Perez + + * IPython/DPyGetOpt.py (ArgumentError): Apply patch by Paul Mueller + , to fix deprecated string exceptions in + options handling. Unicode fix in %whos (committed a while ago) + was also contributed by Paul. + 2007-11-23 Darren Dale * ipy_traits_completer.py: let traits_completer respect the user's readline_omit__names setting.