MCPcopy Index your code
hub / github.com/ipython/ipython / _find_edit_target

Method _find_edit_target

IPython/core/magics/code.py:408–535  ·  view source on GitHub ↗

Utility method used by magic_edit to find what to edit.

(shell, args, opts, last_call)

Source from the content-addressed store, hash-verified

406
407 @staticmethod
408 def _find_edit_target(shell, args, opts, last_call):
409 """Utility method used by magic_edit to find what to edit."""
410
411 def make_filename(arg):
412 "Make a filename from the given args"
413 try:
414 filename = get_py_filename(arg)
415 except IOError:
416 # If it ends with .py but doesn't already exist, assume we want
417 # a new file.
418 if arg.endswith('.py'):
419 filename = arg
420 else:
421 filename = None
422 return filename
423
424 # Set a few locals from the options for convenience:
425 opts_prev = 'p' in opts
426 opts_raw = 'r' in opts
427
428 # custom exceptions
429 class DataIsObject(Exception): pass
430
431 # Default line number value
432 lineno = opts.get('n',None)
433
434 if opts_prev:
435 args = '_%s' % last_call[0]
436 if args not in shell.user_ns:
437 args = last_call[1]
438
439 # by default this is done with temp files, except when the given
440 # arg is a filename
441 use_temp = True
442
443 data = ''
444
445 # First, see if the arguments should be a filename.
446 filename = make_filename(args)
447 if filename:
448 use_temp = False
449 elif args:
450 # Mode where user specifies ranges of lines, like in %macro.
451 data = shell.extract_input_lines(args, opts_raw)
452 if not data:
453 try:
454 # Load the parameter given as a variable. If not a string,
455 # process it as an object instead (below)
456
457 # print('*** args',args,'type',type(args)) # dbg
458 data = eval(args, shell.user_ns)
459 if not isinstance(data, str):
460 raise DataIsObject
461
462 except (NameError,SyntaxError):
463 # given argument is not a variable, try as a filename
464 filename = make_filename(args)
465 if filename is None:

Callers 2

editMethod · 0.95
_run_edit_testFunction · 0.95

Calls 8

find_fileFunction · 0.90
find_source_linesFunction · 0.90
MacroToEditClass · 0.85
getMethod · 0.80
extract_input_linesMethod · 0.80
matchMethod · 0.80
mktempfileMethod · 0.80

Tested by 1

_run_edit_testFunction · 0.76