class_docstr: Magics related to code management (loading, saving, editing, ...).
docstr: Utility method used by magic_edit to find what to edit.
code: def _find_edit_target(shell, args, opts, last_call):
      """Utility method used by magic_edit to find what to edit."""
  
      def make_filename(arg):
          "Make a filename from the given args"
          try:
              filename = get_py_filename(arg)
          except IOError:
              # If it ends with .py but doesn't already exist, assume we want
              # a new file.
              if arg.endswith(".py"):
                  filename = arg
              else:
                  filename = None
          return filename
  
      # Set a few locals from the options for convenience:
      opts_prev = "p" in opts
      opts_raw = "r" in opts
  
      # custom exceptions
      class DataIsObject(Exception):
          pass
  
      # Default line number value
      lineno = opts.get("n", None)
  
      if opts_prev:
          args = "_%s" % last_call[0]
          if args not in shell.user_ns:
              args = last_call[1]
  
      # by default this is done with temp files, except when the given
      # arg is a filename
      use_temp = True
  
      data = ""
  
      # First, see if the arguments should be a filename.
      filename = make_filename(args)
      if filename:
          use_temp = False
      elif args:
          # Mode where user specifies ranges of lines, like in %macro.
          data = shell.extract_input_lines(args, opts_raw)
          if not data:
              try:
                  # Load the parameter given as a variable. If not a string,
                  # process it as an object instead (below)
  
                  # print '*** args',args,'type',type(args)  # dbg
                  data = eval(args, shell.user_ns)
                  if not isinstance(data, str):
                      raise DataIsObject
  
              except (NameError, SyntaxError):
                  # given argument is not a variable, try as a filename
                  filename = make_filename(args)
                  if filename is None:
                      warn(
                          "Argument given (%s) can't be found as a variable "
                          "or as a filename." % args
                      )
                      return (None, None, None)
                  use_temp = False
  
              except DataIsObject as e:
                  # macros have a special edit function
                  if isinstance(data, Macro):
                      raise MacroToEdit(data) from e
  
                  # For objects, try to edit the file where they are defined
                  filename = find_file(data)
                  if filename:
                      if (
                          "fakemodule" in filename.lower()
                          and inspect.isclass(data)
                      ):
                          # class created by %edit? Try to find source
                          # by looking for method definitions instead, the
                          # __module__ in those classes is FakeModule.
                          attrs = [
                              getattr(data, aname)
                              for aname in dir(data)
                          ]
                          for attr in attrs:
                              if not inspect.ismethod(attr):
                                  continue
                              filename = find_file(attr)
                              if (
                                  filename
                                  and "fakemodule"
                                  not in filename.lower()
                              ):
                                  # change the attribute to be the edit
                                  # target instead
                                  data = attr
                                  break
  
                      m = ipython_input_pat.match(
                          os.path.basename(filename)
                      )
                      if m:
                          raise InteractivelyDefined(
                              int(m.groups()[0])
                          ) from e
  
                      datafile = 1
                  if filename is None:
                      filename = make_filename(args)
                      datafile = 1
                      if filename is not None:
                          # only warn about this if we get a real name
                          warn(
                              "Could not find file where `%s` is defined.\n"
                              "Opening a file named `%s`"
                              % (args, filename)
                          )
                  # Now, make sure we can actually read the source (if it was
                  # in a temp file it's gone by now).
                  if datafile:
                      if lineno is None:
                          lineno = find_source_lines(data)
                      if lineno is None:
                          filename = make_filename(args)
                          if filename is None:
                              warn(
                                  "The file where `%s` was defined "
                                  "cannot be read or found." % data
                              )
                              return (None, None, None)
                  use_temp = False
  
      if use_temp:
          filename = shell.mktempfile(data)
          print("IPython will make a temporary file named:", filename)
  
      # use last_call to remember the state of the previous call, but don't
      # let it be clobbered by successive '-p' calls.
      try:
          last_call[0] = shell.displayhook.prompt_count
          if not opts_prev:
              last_call[1] = args
      except:
          pass
  
      return filename, lineno, use_temp
  
imports: import inspect
  import io
  import os
  import re
  import sys
  import ast
  from itertools import chain
  from urllib.request import Request, urlopen
  from urllib.parse import urlencode
  from pathlib import Path
  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
  from IPython.core.release import version
  from IPython.testing.skipdoctest import skip_doctest
  from IPython.utils.contexts import preserve_keys
  from IPython.utils.path import get_py_filename
  from warnings import warn
  from logging import error
  from IPython.utils.text import get_text_list
label_desc: Use image node_id 6 for calling the CodeMagics obj's underlying member method code with example usage: obj._find_edit_target(shell, args, opts, last_call) and returns: filename,  lineno,  use_temp, filename, None,  None,  None, None,  None,  None