From 43225494d0286803a42b281c14ac13e473dc0be4 2011-03-13 12:51:48 From: Thomas Kluyver Date: 2011-03-13 12:51:48 Subject: [PATCH] Improve macro test, modernise magic_macro code. --- diff --git a/IPython/core/magic.py b/IPython/core/magic.py index a9fdfe6..9dc5d39 100644 --- a/IPython/core/magic.py +++ b/IPython/core/magic.py @@ -2030,17 +2030,16 @@ Currently the magic system has the following functions:\n""" In [60]: exec In[44:48]+In[49]""" opts,args = self.parse_options(parameter_s,'r',mode='list') - if not args: - macs = [k for k,v in self.shell.user_ns.items() if isinstance(v, Macro)] - macs.sort() - return macs + if not args: # List existing macros + return sorted(k for k,v in self.shell.user_ns.iteritems() if\ + isinstance(v, Macro)) if len(args) == 1: raise UsageError( "%macro insufficient args; usage '%macro name n1-n2 n3-4...") name,ranges = args[0], args[1:] #print 'rng',ranges # dbg - lines = self.extract_input_slices(ranges,opts.has_key('r')) + lines = self.extract_input_slices(ranges,'r' in opts) macro = Macro("\n".join(lines)) self.shell.define_macro(name, macro) print 'Macro `%s` created. To execute, type its name (without quotes).' % name @@ -2076,7 +2075,7 @@ Currently the magic system has the following functions:\n""" if ans.lower() not in ['y','yes']: print 'Operation cancelled.' return - cmds = '\n'.join(self.extract_input_slices(ranges,opts.has_key('r'))) + cmds = '\n'.join(self.extract_input_slices(ranges, 'r' in opts)) with open(fname,'w') as f: f.write(cmds) print 'The following commands were written to file `%s`:' % fname diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index 189f272..07185c5 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -183,8 +183,8 @@ def test_macro(): ip.magic("macro test 1-3") nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n") - # List macros. This goes to stdout, so just check it doesn't crash. - ip.magic("macro") + # List macros. + assert "test" in ip.magic("macro") # XXX failing for now, until we get clearcmd out of quarantine. But we should