##// END OF EJS Templates
Change order in which things are tried in %edit, so filenames starting with numbers can be edited....
Thomas Kluyver -
Show More
@@ -2275,75 +2275,73 b' Currently the magic system has the following functions:\\n"""'
2275 use_temp = True
2275 use_temp = True
2276
2276
2277 data = ''
2277 data = ''
2278 if args[0].isdigit():
2278 if args.endswith('.py'):
2279 # Mode where user specifies ranges of lines, like in %macro.
2280 # This means that you can't edit files whose names begin with
2281 # numbers this way. Tough.
2282 data = self.extract_input_lines(args, opts_raw)
2283 elif args.endswith('.py'):
2284 filename = make_filename(args)
2279 filename = make_filename(args)
2285 use_temp = False
2280 use_temp = False
2286 elif args:
2281 elif args:
2287 try:
2282 # Mode where user specifies ranges of lines, like in %macro.
2288 # Load the parameter given as a variable. If not a string,
2283 data = self.extract_input_lines(args, opts_raw)
2289 # process it as an object instead (below)
2284 if not data:
2290
2291 #print '*** args',args,'type',type(args) # dbg
2292 data = eval(args, self.shell.user_ns)
2293 if not isinstance(data, basestring):
2294 raise DataIsObject
2295
2296 except (NameError,SyntaxError):
2297 # given argument is not a variable, try as a filename
2298 filename = make_filename(args)
2299 if filename is None:
2300 warn("Argument given (%s) can't be found as a variable "
2301 "or as a filename." % args)
2302 return
2303 use_temp = False
2304
2305 except DataIsObject:
2306 # macros have a special edit function
2307 if isinstance(data, Macro):
2308 self._edit_macro(args,data)
2309 return
2310
2311 # For objects, try to edit the file where they are defined
2312 try:
2285 try:
2313 filename = inspect.getabsfile(data)
2286 # Load the parameter given as a variable. If not a string,
2314 if 'fakemodule' in filename.lower() and inspect.isclass(data):
2287 # process it as an object instead (below)
2315 # class created by %edit? Try to find source
2288
2316 # by looking for method definitions instead, the
2289 #print '*** args',args,'type',type(args) # dbg
2317 # __module__ in those classes is FakeModule.
2290 data = eval(args, self.shell.user_ns)
2318 attrs = [getattr(data, aname) for aname in dir(data)]
2291 if not isinstance(data, basestring):
2319 for attr in attrs:
2292 raise DataIsObject
2320 if not inspect.ismethod(attr):
2293
2321 continue
2294 except (NameError,SyntaxError):
2322 filename = inspect.getabsfile(attr)
2295 # given argument is not a variable, try as a filename
2323 if filename and 'fakemodule' not in filename.lower():
2324 # change the attribute to be the edit target instead
2325 data = attr
2326 break
2327
2328 datafile = 1
2329 except TypeError:
2330 filename = make_filename(args)
2296 filename = make_filename(args)
2331 datafile = 1
2297 if filename is None:
2332 warn('Could not find file where `%s` is defined.\n'
2298 warn("Argument given (%s) can't be found as a variable "
2333 'Opening a file named `%s`' % (args,filename))
2299 "or as a filename." % args)
2334 # Now, make sure we can actually read the source (if it was in
2300 return
2335 # a temp file it's gone by now).
2301 use_temp = False
2336 if datafile:
2302
2303 except DataIsObject:
2304 # macros have a special edit function
2305 if isinstance(data, Macro):
2306 self._edit_macro(args,data)
2307 return
2308
2309 # For objects, try to edit the file where they are defined
2337 try:
2310 try:
2338 if lineno is None:
2311 filename = inspect.getabsfile(data)
2339 lineno = inspect.getsourcelines(data)[1]
2312 if 'fakemodule' in filename.lower() and inspect.isclass(data):
2340 except IOError:
2313 # class created by %edit? Try to find source
2314 # by looking for method definitions instead, the
2315 # __module__ in those classes is FakeModule.
2316 attrs = [getattr(data, aname) for aname in dir(data)]
2317 for attr in attrs:
2318 if not inspect.ismethod(attr):
2319 continue
2320 filename = inspect.getabsfile(attr)
2321 if filename and 'fakemodule' not in filename.lower():
2322 # change the attribute to be the edit target instead
2323 data = attr
2324 break
2325
2326 datafile = 1
2327 except TypeError:
2341 filename = make_filename(args)
2328 filename = make_filename(args)
2342 if filename is None:
2329 datafile = 1
2343 warn('The file `%s` where `%s` was defined cannot '
2330 warn('Could not find file where `%s` is defined.\n'
2344 'be read.' % (filename,data))
2331 'Opening a file named `%s`' % (args,filename))
2345 return
2332 # Now, make sure we can actually read the source (if it was in
2346 use_temp = False
2333 # a temp file it's gone by now).
2334 if datafile:
2335 try:
2336 if lineno is None:
2337 lineno = inspect.getsourcelines(data)[1]
2338 except IOError:
2339 filename = make_filename(args)
2340 if filename is None:
2341 warn('The file `%s` where `%s` was defined cannot '
2342 'be read.' % (filename,data))
2343 return
2344 use_temp = False
2347
2345
2348 if use_temp:
2346 if use_temp:
2349 filename = self.shell.mktempfile(data)
2347 filename = self.shell.mktempfile(data)
@@ -2370,7 +2368,7 b' Currently the magic system has the following functions:\\n"""'
2370 print
2368 print
2371 else:
2369 else:
2372 print 'done. Executing edited code...'
2370 print 'done. Executing edited code...'
2373 if opts_r:
2371 if opts_raw:
2374 self.shell.run_cell(file_read(filename))
2372 self.shell.run_cell(file_read(filename))
2375 else:
2373 else:
2376 self.shell.safe_execfile(filename,self.shell.user_ns,
2374 self.shell.safe_execfile(filename,self.shell.user_ns,
General Comments 0
You need to be logged in to leave comments. Login now