class_docstr: This is the main class interface for Pexpect. Use this class to start
  and control child applications. 
class_docstr_tok: ["This","is","the","main","class","interface","for","Pexpect",".","Use","this","class","to","start","and","control","child","applications","."]
docstr: This is the constructor. The command parameter may be a string that
  includes a command and any arguments to the command. For example::
  
      child = pexpect.spawn('/usr/bin/ftp')
      child = pexpect.spawn('/usr/bin/ssh user@example.com')
      child = pexpect.spawn('ls -latr /tmp')
  
  You may also construct it with a list of arguments like so::
  
      child = pexpect.spawn('/usr/bin/ftp', [])
      child = pexpect.spawn('/usr/bin/ssh', ['user@example.com'])
      child = pexpect.spawn('ls', ['-latr', '/tmp'])
  
  After this the child application will be created and will be ready to
  talk to. For normal use, see expect() and send() and sendline().
  
  Remember that Pexpect does NOT interpret shell meta characters such as
  redirect, pipe, or wild cards (``>``, ``|``, or ``*``). This is a
  common mistake.  If you want to run a command and pipe it through
  another command then you must also start a shell. For example::
  
      child = pexpect.spawn('/bin/bash -c "ls -l | grep LOG > logs.txt"')
      child.expect(pexpect.EOF)
  
  The second form of spawn (where you pass a list of arguments) is useful
  in situations where you wish to spawn a command and pass it its own
  argument list. This can make syntax more clear. For example, the
  following is equivalent to the previous example::
  
      shell_cmd = 'ls -l | grep LOG > logs.txt'
      child = pexpect.spawn('/bin/bash', ['-c', shell_cmd])
      child.expect(pexpect.EOF)
  
  The maxread attribute sets the read buffer size. This is maximum number
  of bytes that Pexpect will try to read from a TTY at one time. Setting
  the maxread size to 1 will turn off buffering. Setting the maxread
  value higher may help performance in cases where large amounts of
  output are read back from the child. This feature is useful in
  conjunction with searchwindowsize.
  
  When the keyword argument *searchwindowsize* is None (default), the
  full buffer is searched at each iteration of receiving incoming data.
  The default number of bytes scanned at each iteration is very large
  and may be reduced to collaterally reduce search cost.  After
  :meth:`~.expect` returns, the full buffer attribute remains up to
  size *maxread* irrespective of *searchwindowsize* value.
  
  When the keyword argument ``timeout`` is specified as a number,
  (default: *30*), then :class:`TIMEOUT` will be raised after the value
  specified has elapsed, in seconds, for any of the :meth:`~.expect`
  family of method calls.  When None, TIMEOUT will not be raised, and
  :meth:`~.expect` may block indefinitely until match.
  
  
  The logfile member turns on or off logging. All input and output will
  be copied to the given file object. Set logfile to None to stop
  logging. This is the default. Set logfile to sys.stdout to echo
  everything to standard output. The logfile is flushed after each write.
  
  Example log input and output to a file::
  
      child = pexpect.spawn('some_command')
      fout = open('mylog.txt','wb')
      child.logfile = fout
  
  Example log to stdout::
  
      # In Python 2:
      child = pexpect.spawn('some_command')
      child.logfile = sys.stdout
  
      # In Python 3, we'll use the ``encoding`` argument to decode data
      # from the subprocess and handle it as unicode:
      child = pexpect.spawn('some_command', encoding='utf-8')
      child.logfile = sys.stdout
  
  The logfile_read and logfile_send members can be used to separately log
  the input from the child and output sent to the child. Sometimes you
  don't want to see everything you write to the child. You only want to
  log what the child sends back. For example::
  
      child = pexpect.spawn('some_command')
      child.logfile_read = sys.stdout
  
  You will need to pass an encoding to spawn in the above code if you are
  using Python 3.
  
  To separately log output sent to the child use logfile_send::
  
      child.logfile_send = fout
  
  If ``ignore_sighup`` is True, the child process will ignore SIGHUP
  signals. The default is False from Pexpect 4.0, meaning that SIGHUP
  will be handled normally by the child.
  
  The delaybeforesend helps overcome a weird behavior that many users
  were experiencing. The typical problem was that a user would expect() a
  "Password:" prompt and then immediately call sendline() to send the
  password. The user would then see that their password was echoed back
  to them. Passwords don't normally echo. The problem is caused by the
  fact that most applications print out the "Password" prompt and then
  turn off stdin echo, but if you send your password before the
  application turned off echo, then you get your password echoed.
  Normally this wouldn't be a problem when interacting with a human at a
  real keyboard. If you introduce a slight delay just before writing then
  this seems to clear up the problem. This was such a common problem for
  many users that I decided that the default pexpect behavior should be
  to sleep just before writing to the child application. 1/20th of a
  second (50 ms) seems to be enough to clear up the problem. You can set
  delaybeforesend to None to return to the old behavior.
  
  Note that spawn is clever about finding commands on your path.
  It uses the same logic that "which" uses to find executables.
  
  If you wish to get the exit status of the child you must call the
  close() method. The exit or signal status of the child will be stored
  in self.exitstatus or self.signalstatus. If the child exited normally
  then exitstatus will store the exit return code and signalstatus will
  be None. If the child was terminated abnormally with a signal then
  signalstatus will store the signal value and exitstatus will be None::
  
      child = pexpect.spawn('some_command')
      child.close()
      print(child.exitstatus, child.signalstatus)
  
  If you need more detail you can also read the self.status member which
  stores the status returned by os.waitpid. You can interpret this using
  os.WIFEXITED/os.WEXITSTATUS or os.WIFSIGNALED/os.TERMSIG.
  
  The echo attribute may be set to False to disable echoing of input.
  As a pseudo-terminal, all input echoed by the "keyboard" (send()
  or sendline()) will be repeated to output.  For many cases, it is
  not desirable to have echo enabled, and it may be later disabled
  using setecho(False) followed by waitnoecho().  However, for some
  platforms such as Solaris, this is not possible, and should be
  disabled immediately on spawn.
  
  If preexec_fn is given, it will be called in the child process before
  launching the given command. This is useful to e.g. reset inherited
  signal handlers.
  
  The dimensions attribute specifies the size of the pseudo-terminal as
  seen by the subprocess, and is specified as a two-entry tuple (rows,
  columns). If this is unspecified, the defaults in ptyprocess will apply.
  
  The use_poll attribute enables using select.poll() over select.select()
  for socket handling. This is handy if your system could have > 1024 fds
docstr_tok: ["This","is","the","constructor",".","The","command","parameter","may","be","a","string","that","includes","a","command","and","any","arguments","to","the","command",".","For","example",":",":","child","=","pexpect.spawn","(","'\/usr\/bin\/ftp","'",")","child","=","pexpect.spawn","(","'\/usr\/bin\/ssh","user","@","example.com","'",")","child","=","pexpect.spawn","(","'ls","-latr","\/tmp","'",")","You","may","also","construct","it","with","a","list","of","arguments","like","so",":",":","child","=","pexpect.spawn","(","'\/usr\/bin\/ftp","'",",","[","]",")","child","=","pexpect.spawn","(","'\/usr\/bin\/ssh","'",",","[","'user","@","example.com","'","]",")","child","=","pexpect.spawn","(","'ls","'",",","[","'-latr","'",",","'\/tmp","'","]",")","After","this","the","child","application","will","be","created","and","will","be","ready","to","talk","to",".","For","normal","use",",","see","expect","(",")","and","send","(",")","and","sendline","(",")",".","Remember","that","Pexpect","does","NOT","interpret","shell","meta","characters","such","as","redirect",",","pipe",",","or","wild","cards","(","``",">","``",",","``","|","``",",","or","``","*","``",")",".","This","is","a","common","mistake",".","If","you","want","to","run","a","command","and","pipe","it","through","another","command","then","you","must","also","start","a","shell",".","For","example",":",":","child","=","pexpect.spawn","(","'\/bin\/bash","-c","``","ls","-l","|","grep","LOG",">","logs.txt","''","'",")","child.expect","(","pexpect.EOF",")","The","second","form","of","spawn","(","where","you","pass","a","list","of","arguments",")","is","useful","in","situations","where","you","wish","to","spawn","a","command","and","pass","it","its","own","argument","list",".","This","can","make","syntax","more","clear",".","For","example",",","the","following","is","equivalent","to","the","previous","example",":",":","shell_cmd","=","'ls","-l","|","grep","LOG",">","logs.txt'","child","=","pexpect.spawn","(","'\/bin\/bash","'",",","[","'-c","'",",","shell_cmd","]",")","child.expect","(","pexpect.EOF",")","The","maxread","attribute","sets","the","read","buffer","size",".","This","is","maximum","number","of","bytes","that","Pexpect","will","try","to","read","from","a","TTY","at","one","time",".","Setting","the","maxread","size","to","1","will","turn","off","buffering",".","Setting","the","maxread","value","higher","may","help","performance","in","cases","where","large","amounts","of","output","are","read","back","from","the","child",".","This","feature","is","useful","in","conjunction","with","searchwindowsize",".","When","the","keyword","argument","*","searchwindowsize","*","is","None","(","default",")",",","the","full","buffer","is","searched","at","each","iteration","of","receiving","incoming","data",".","The","default","number","of","bytes","scanned","at","each","iteration","is","very","large","and","may","be","reduced","to","collaterally","reduce","search","cost",".","After",":","meth",":","`","~.expect","`","returns",",","the","full","buffer","attribute","remains","up","to","size","*","maxread","*","irrespective","of","*","searchwindowsize","*","value",".","When","the","keyword","argument","``","timeout","``","is","specified","as","a","number",",","(","default",":","*","30","*",")",",","then",":","class",":","`","TIMEOUT","`","will","be","raised","after","the","value","specified","has","elapsed",",","in","seconds",",","for","any","of","the",":","meth",":","`","~.expect","`","family","of","method","calls",".","When","None",",","TIMEOUT","will","not","be","raised",",","and",":","meth",":","`","~.expect","`","may","block","indefinitely","until","match",".","The","logfile","member","turns","on","or","off","logging",".","All","input","and","output","will","be","copied","to","the","given","file","object",".","Set","logfile","to","None","to","stop","logging",".","This","is","the","default",".","Set","logfile","to","sys.stdout","to","echo","everything","to","standard","output",".","The","logfile","is","flushed","after","each","write",".","Example","log","input","and","output","to","a","file",":",":","child","=","pexpect.spawn","(","'some_command","'",")","fout","=","open","(","'mylog.txt","'",",","'wb","'",")","child.logfile","=","fout","Example","log","to","stdout",":",":","#","In","Python","2",":","child","=","pexpect.spawn","(","'some_command","'",")","child.logfile","=","sys.stdout","#","In","Python","3",",","we","'ll","use","the","``","encoding","``","argument","to","decode","data","#","from","the","subprocess","and","handle","it","as","unicode",":","child","=","pexpect.spawn","(","'some_command","'",",","encoding='utf-8","'",")","child.logfile","=","sys.stdout","The","logfile_read","and","logfile_send","members","can","be","used","to","separately","log","the","input","from","the","child","and","output","sent","to","the","child",".","Sometimes","you","do","n't","want","to","see","everything","you","write","to","the","child",".","You","only","want","to","log","what","the","child","sends","back",".","For","example",":",":","child","=","pexpect.spawn","(","'some_command","'",")","child.logfile_read","=","sys.stdout","You","will","need","to","pass","an","encoding","to","spawn","in","the","above","code","if","you","are","using","Python","3",".","To","separately","log","output","sent","to","the","child","use","logfile_send",":",":","child.logfile_send","=","fout","If","``","ignore_sighup","``","is","True",",","the","child","process","will","ignore","SIGHUP","signals",".","The","default","is","False","from","Pexpect","4.0",",","meaning","that","SIGHUP","will","be","handled","normally","by","the","child",".","The","delaybeforesend","helps","overcome","a","weird","behavior","that","many","users","were","experiencing",".","The","typical","problem","was","that","a","user","would","expect","(",")","a","''","Password",":","''","prompt","and","then","immediately","call","sendline","(",")","to","send","the","password",".","The","user","would","then","see","that","their","password","was","echoed","back","to","them",".","Passwords","do","n't","normally","echo",".","The","problem","is","caused","by","the","fact","that","most","applications","print","out","the","``","Password","''","prompt","and","then","turn","off","stdin","echo",",","but","if","you","send","your","password","before","the","application","turned","off","echo",",","then","you","get","your","password","echoed",".","Normally","this","would","n't","be","a","problem","when","interacting","with","a","human","at","a","real","keyboard",".","If","you","introduce","a","slight","delay","just","before","writing","then","this","seems","to","clear","up","the","problem",".","This","was","such","a","common","problem","for","many","users","that","I","decided","that","the","default","pexpect","behavior","should","be","to","sleep","just","before","writing","to","the","child","application",".","1\/20th","of","a","second","(","50","ms",")","seems","to","be","enough","to","clear","up","the","problem",".","You","can","set","delaybeforesend","to","None","to","return","to","the","old","behavior",".","Note","that","spawn","is","clever","about","finding","commands","on","your","path",".","It","uses","the","same","logic","that","``","which","''","uses","to","find","executables",".","If","you","wish","to","get","the","exit","status","of","the","child","you","must","call","the","close","(",")","method",".","The","exit","or","signal","status","of","the","child","will","be","stored","in","self.exitstatus","or","self.signalstatus",".","If","the","child","exited","normally","then","exitstatus","will","store","the","exit","return","code","and","signalstatus","will","be","None",".","If","the","child","was","terminated","abnormally","with","a","signal","then","signalstatus","will","store","the","signal","value","and","exitstatus","will","be","None",":",":","child","=","pexpect.spawn","(","'some_command","'",")","child.close","(",")","print","(","child.exitstatus",",","child.signalstatus",")","If","you","need","more","detail","you","can","also","read","the","self.status","member","which","stores","the","status","returned","by","os.waitpid",".","You","can","interpret","this","using","os.WIFEXITED\/os.WEXITSTATUS","or","os.WIFSIGNALED\/os.TERMSIG",".","The","echo","attribute","may","be","set","to","False","to","disable","echoing","of","input",".","As","a","pseudo-terminal",",","all","input","echoed","by","the","``","keyboard","''","(","send","(",")","or","sendline","(",")",")","will","be","repeated","to","output",".","For","many","cases",",","it","is","not","desirable","to","have","echo","enabled",",","and","it","may","be","later","disabled","using","setecho","(","False",")","followed","by","waitnoecho","(",")",".","However",",","for","some","platforms","such","as","Solaris",",","this","is","not","possible",",","and","should","be","disabled","immediately","on","spawn",".","If","preexec_fn","is","given",",","it","will","be","called","in","the","child","process","before","launching","the","given","command",".","This","is","useful","to","e.g",".","reset","inherited","signal","handlers",".","The","dimensions","attribute","specifies","the","size","of","the","pseudo-terminal","as","seen","by","the","subprocess",",","and","is","specified","as","a","two-entry","tuple","(","rows",",","columns",")",".","If","this","is","unspecified",",","the","defaults","in","ptyprocess","will","apply",".","The","use_poll","attribute","enables","using","select.poll","(",")","over","select.select","(",")","for","socket","handling",".","This","is","handy","if","your","system","could","have",">","1024","fds"]
code: def __init__(
      self,
      command,
      args=[],
      timeout=30,
      maxread=2000,
      searchwindowsize=None,
      logfile=None,
      cwd=None,
      env=None,
      ignore_sighup=False,
      echo=True,
      preexec_fn=None,
      encoding=None,
      codec_errors="strict",
      dimensions=None,
      use_poll=False,
  ):
      """This is the constructor. The command parameter may be a string that
      includes a command and any arguments to the command. For example::
  
          child = pexpect.spawn('/usr/bin/ftp')
          child = pexpect.spawn('/usr/bin/ssh user@example.com')
          child = pexpect.spawn('ls -latr /tmp')
  
      You may also construct it with a list of arguments like so::
  
          child = pexpect.spawn('/usr/bin/ftp', [])
          child = pexpect.spawn('/usr/bin/ssh', ['user@example.com'])
          child = pexpect.spawn('ls', ['-latr', '/tmp'])
  
      After this the child application will be created and will be ready to
      talk to. For normal use, see expect() and send() and sendline().
  
      Remember that Pexpect does NOT interpret shell meta characters such as
      redirect, pipe, or wild cards (``>``, ``|``, or ``*``). This is a
      common mistake.  If you want to run a command and pipe it through
      another command then you must also start a shell. For example::
  
          child = pexpect.spawn('/bin/bash -c "ls -l | grep LOG > logs.txt"')
          child.expect(pexpect.EOF)
  
      The second form of spawn (where you pass a list of arguments) is useful
      in situations where you wish to spawn a command and pass it its own
      argument list. This can make syntax more clear. For example, the
      following is equivalent to the previous example::
  
          shell_cmd = 'ls -l | grep LOG > logs.txt'
          child = pexpect.spawn('/bin/bash', ['-c', shell_cmd])
          child.expect(pexpect.EOF)
  
      The maxread attribute sets the read buffer size. This is maximum number
      of bytes that Pexpect will try to read from a TTY at one time. Setting
      the maxread size to 1 will turn off buffering. Setting the maxread
      value higher may help performance in cases where large amounts of
      output are read back from the child. This feature is useful in
      conjunction with searchwindowsize.
  
      When the keyword argument *searchwindowsize* is None (default), the
      full buffer is searched at each iteration of receiving incoming data.
      The default number of bytes scanned at each iteration is very large
      and may be reduced to collaterally reduce search cost.  After
      :meth:`~.expect` returns, the full buffer attribute remains up to
      size *maxread* irrespective of *searchwindowsize* value.
  
      When the keyword argument ``timeout`` is specified as a number,
      (default: *30*), then :class:`TIMEOUT` will be raised after the value
      specified has elapsed, in seconds, for any of the :meth:`~.expect`
      family of method calls.  When None, TIMEOUT will not be raised, and
      :meth:`~.expect` may block indefinitely until match.
  
  
      The logfile member turns on or off logging. All input and output will
      be copied to the given file object. Set logfile to None to stop
      logging. This is the default. Set logfile to sys.stdout to echo
      everything to standard output. The logfile is flushed after each write.
  
      Example log input and output to a file::
  
          child = pexpect.spawn('some_command')
          fout = open('mylog.txt','wb')
          child.logfile = fout
  
      Example log to stdout::
  
          # In Python 2:
          child = pexpect.spawn('some_command')
          child.logfile = sys.stdout
  
          # In Python 3, we'll use the ``encoding`` argument to decode data
          # from the subprocess and handle it as unicode:
          child = pexpect.spawn('some_command', encoding='utf-8')
          child.logfile = sys.stdout
  
      The logfile_read and logfile_send members can be used to separately log
      the input from the child and output sent to the child. Sometimes you
      don't want to see everything you write to the child. You only want to
      log what the child sends back. For example::
  
          child = pexpect.spawn('some_command')
          child.logfile_read = sys.stdout
  
      You will need to pass an encoding to spawn in the above code if you are
      using Python 3.
  
      To separately log output sent to the child use logfile_send::
  
          child.logfile_send = fout
  
      If ``ignore_sighup`` is True, the child process will ignore SIGHUP
      signals. The default is False from Pexpect 4.0, meaning that SIGHUP
      will be handled normally by the child.
  
      The delaybeforesend helps overcome a weird behavior that many users
      were experiencing. The typical problem was that a user would expect() a
      "Password:" prompt and then immediately call sendline() to send the
      password. The user would then see that their password was echoed back
      to them. Passwords don't normally echo. The problem is caused by the
      fact that most applications print out the "Password" prompt and then
      turn off stdin echo, but if you send your password before the
      application turned off echo, then you get your password echoed.
      Normally this wouldn't be a problem when interacting with a human at a
      real keyboard. If you introduce a slight delay just before writing then
      this seems to clear up the problem. This was such a common problem for
      many users that I decided that the default pexpect behavior should be
      to sleep just before writing to the child application. 1/20th of a
      second (50 ms) seems to be enough to clear up the problem. You can set
      delaybeforesend to None to return to the old behavior.
  
      Note that spawn is clever about finding commands on your path.
      It uses the same logic that "which" uses to find executables.
  
      If you wish to get the exit status of the child you must call the
      close() method. The exit or signal status of the child will be stored
      in self.exitstatus or self.signalstatus. If the child exited normally
      then exitstatus will store the exit return code and signalstatus will
      be None. If the child was terminated abnormally with a signal then
      signalstatus will store the signal value and exitstatus will be None::
  
          child = pexpect.spawn('some_command')
          child.close()
          print(child.exitstatus, child.signalstatus)
  
      If you need more detail you can also read the self.status member which
      stores the status returned by os.waitpid. You can interpret this using
      os.WIFEXITED/os.WEXITSTATUS or os.WIFSIGNALED/os.TERMSIG.
  
      The echo attribute may be set to False to disable echoing of input.
      As a pseudo-terminal, all input echoed by the "keyboard" (send()
      or sendline()) will be repeated to output.  For many cases, it is
      not desirable to have echo enabled, and it may be later disabled
      using setecho(False) followed by waitnoecho().  However, for some
      platforms such as Solaris, this is not possible, and should be
      disabled immediately on spawn.
  
      If preexec_fn is given, it will be called in the child process before
      launching the given command. This is useful to e.g. reset inherited
      signal handlers.
  
      The dimensions attribute specifies the size of the pseudo-terminal as
      seen by the subprocess, and is specified as a two-entry tuple (rows,
      columns). If this is unspecified, the defaults in ptyprocess will apply.
  
      The use_poll attribute enables using select.poll() over select.select()
      for socket handling. This is handy if your system could have > 1024 fds
      """
      super(spawn, self).__init__(
          timeout=timeout,
          maxread=maxread,
          searchwindowsize=searchwindowsize,
          logfile=logfile,
          encoding=encoding,
          codec_errors=codec_errors,
      )
      self.STDIN_FILENO = pty.STDIN_FILENO
      self.STDOUT_FILENO = pty.STDOUT_FILENO
      self.STDERR_FILENO = pty.STDERR_FILENO
      self.str_last_chars = 100
      self.cwd = cwd
      self.env = env
      self.echo = echo
      self.ignore_sighup = ignore_sighup
      self.__irix_hack = sys.platform.lower().startswith("irix")
      if command is None:
          self.command = None
          self.args = None
          self.name = "<pexpect factory incomplete>"
      else:
          self._spawn(command, args, preexec_fn, dimensions)
      self.use_poll = use_poll
  
imports: import os
  import sys
  import time
  import pty
  import tty
  import errno
  import signal
  from contextlib import contextmanager
  import ptyprocess
  from ptyprocess.ptyprocess import use_native_pty_fork
  from .exceptions import ExceptionPexpect, EOF, TIMEOUT
  from .spawnbase import SpawnBase
  from .utils import which, split_command_line, select_ignore_interrupts, poll_ignore_interrupts
label_desc: Use image node_id 1 to create a new spawn object from inherited base classes: SpawnBase with example: obj = spawn(command, args, timeout, maxread, searchwindowsize, logfile, cwd, env, ignore_sighup, echo, preexec_fn, encoding, codec_errors, dimensions, use_poll)