

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>os &mdash; ArcherBashPYDocs 1.0.0. documentation</title>
  

  
  

  
  <link href='https://fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic|Roboto+Slab:400,700|Inconsolata:400,700&subset=latin,cyrillic' rel='stylesheet' type='text/css'>

  
  
    

  

  
  
    <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  

  

  
    <link rel="top" title="ArcherBashPYDocs 1.0.0. documentation" href="../index.html"/>
        <link rel="up" title="Module code" href="index.html"/> 

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.6.2/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav" role="document">

  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-nav-search">
        
          <a href="../index.html" class="fa fa-home"> ArcherBashPYDocs</a>
        
        
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
      </div>

      <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
        
          
          
              <ul class="simple">
</ul>

          
        
      </div>
      &nbsp;
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
        <a href="../index.html">ArcherBashPYDocs</a>
      </nav>


      
      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
    <li><a href="../index.html">Docs</a> &raquo;</li>
      
          <li><a href="index.html">Module code</a> &raquo;</li>
      
    <li>os</li>
      <li class="wy-breadcrumbs-aside">
        
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document">
            
  <h1>Source code for os</h1><div class="highlight"><pre>
<span class="sd">r&quot;&quot;&quot;OS routines for NT or Posix depending on what system we&#39;re on.</span>

<span class="sd">This exports:</span>
<span class="sd">  - all functions from posix, nt or ce, e.g. unlink, stat, etc.</span>
<span class="sd">  - os.path is either posixpath or ntpath</span>
<span class="sd">  - os.name is either &#39;posix&#39;, &#39;nt&#39; or &#39;ce&#39;.</span>
<span class="sd">  - os.curdir is a string representing the current directory (&#39;.&#39; or &#39;:&#39;)</span>
<span class="sd">  - os.pardir is a string representing the parent directory (&#39;..&#39; or &#39;::&#39;)</span>
<span class="sd">  - os.sep is the (or a most common) pathname separator (&#39;/&#39; or &#39;:&#39; or &#39;\\&#39;)</span>
<span class="sd">  - os.extsep is the extension separator (always &#39;.&#39;)</span>
<span class="sd">  - os.altsep is the alternate pathname separator (None or &#39;/&#39;)</span>
<span class="sd">  - os.pathsep is the component separator used in $PATH etc</span>
<span class="sd">  - os.linesep is the line separator in text files (&#39;\r&#39; or &#39;\n&#39; or &#39;\r\n&#39;)</span>
<span class="sd">  - os.defpath is the default search path for executables</span>
<span class="sd">  - os.devnull is the file path of the null device (&#39;/dev/null&#39;, etc.)</span>

<span class="sd">Programs that import and use &#39;os&#39; stand a better chance of being</span>
<span class="sd">portable between different platforms.  Of course, they must then</span>
<span class="sd">only use functions that are defined by all platforms (e.g., unlink</span>
<span class="sd">and opendir), and leave all pathname manipulation to os.path</span>
<span class="sd">(e.g., split and join).</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="c">#&#39;</span>

<span class="kn">import</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">errno</span>
<span class="kn">import</span> <span class="nn">stat</span> <span class="kn">as</span> <span class="nn">st</span>

<span class="n">_names</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">builtin_module_names</span>

<span class="c"># Note:  more names are added to __all__ later.</span>
<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;altsep&quot;</span><span class="p">,</span> <span class="s">&quot;curdir&quot;</span><span class="p">,</span> <span class="s">&quot;pardir&quot;</span><span class="p">,</span> <span class="s">&quot;sep&quot;</span><span class="p">,</span> <span class="s">&quot;pathsep&quot;</span><span class="p">,</span> <span class="s">&quot;linesep&quot;</span><span class="p">,</span>
           <span class="s">&quot;defpath&quot;</span><span class="p">,</span> <span class="s">&quot;name&quot;</span><span class="p">,</span> <span class="s">&quot;path&quot;</span><span class="p">,</span> <span class="s">&quot;devnull&quot;</span><span class="p">,</span> <span class="s">&quot;SEEK_SET&quot;</span><span class="p">,</span> <span class="s">&quot;SEEK_CUR&quot;</span><span class="p">,</span>
           <span class="s">&quot;SEEK_END&quot;</span><span class="p">,</span> <span class="s">&quot;fsencode&quot;</span><span class="p">,</span> <span class="s">&quot;fsdecode&quot;</span><span class="p">,</span> <span class="s">&quot;get_exec_path&quot;</span><span class="p">,</span> <span class="s">&quot;fdopen&quot;</span><span class="p">,</span>
           <span class="s">&quot;popen&quot;</span><span class="p">,</span> <span class="s">&quot;extsep&quot;</span><span class="p">]</span>

<span class="k">def</span> <span class="nf">_exists</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">name</span> <span class="ow">in</span> <span class="nb">globals</span><span class="p">()</span>

<span class="k">def</span> <span class="nf">_get_exports_list</span><span class="p">(</span><span class="n">module</span><span class="p">):</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="n">module</span><span class="o">.</span><span class="n">__all__</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">n</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">dir</span><span class="p">(</span><span class="n">module</span><span class="p">)</span> <span class="k">if</span> <span class="n">n</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">!=</span> <span class="s">&#39;_&#39;</span><span class="p">]</span>

<span class="c"># Any new dependencies of the os module and/or changes in path separator</span>
<span class="c"># requires updating importlib as well.</span>
<span class="k">if</span> <span class="s">&#39;posix&#39;</span> <span class="ow">in</span> <span class="n">_names</span><span class="p">:</span>
    <span class="n">name</span> <span class="o">=</span> <span class="s">&#39;posix&#39;</span>
    <span class="n">linesep</span> <span class="o">=</span> <span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span>
    <span class="kn">from</span> <span class="nn">posix</span> <span class="kn">import</span> <span class="o">*</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="kn">from</span> <span class="nn">posix</span> <span class="kn">import</span> <span class="n">_exit</span>
        <span class="n">__all__</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#39;_exit&#39;</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
        <span class="k">pass</span>
    <span class="kn">import</span> <span class="nn">posixpath</span> <span class="kn">as</span> <span class="nn">path</span>

    <span class="k">try</span><span class="p">:</span>
        <span class="kn">from</span> <span class="nn">posix</span> <span class="kn">import</span> <span class="n">_have_functions</span>
    <span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
        <span class="k">pass</span>

<span class="k">elif</span> <span class="s">&#39;nt&#39;</span> <span class="ow">in</span> <span class="n">_names</span><span class="p">:</span>
    <span class="n">name</span> <span class="o">=</span> <span class="s">&#39;nt&#39;</span>
    <span class="n">linesep</span> <span class="o">=</span> <span class="s">&#39;</span><span class="se">\r\n</span><span class="s">&#39;</span>
    <span class="kn">from</span> <span class="nn">nt</span> <span class="kn">import</span> <span class="o">*</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="kn">from</span> <span class="nn">nt</span> <span class="kn">import</span> <span class="n">_exit</span>
        <span class="n">__all__</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#39;_exit&#39;</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
        <span class="k">pass</span>
    <span class="kn">import</span> <span class="nn">ntpath</span> <span class="kn">as</span> <span class="nn">path</span>

    <span class="kn">import</span> <span class="nn">nt</span>
    <span class="n">__all__</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">_get_exports_list</span><span class="p">(</span><span class="n">nt</span><span class="p">))</span>
    <span class="k">del</span> <span class="n">nt</span>

    <span class="k">try</span><span class="p">:</span>
        <span class="kn">from</span> <span class="nn">nt</span> <span class="kn">import</span> <span class="n">_have_functions</span>
    <span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
        <span class="k">pass</span>

<span class="k">elif</span> <span class="s">&#39;ce&#39;</span> <span class="ow">in</span> <span class="n">_names</span><span class="p">:</span>
    <span class="n">name</span> <span class="o">=</span> <span class="s">&#39;ce&#39;</span>
    <span class="n">linesep</span> <span class="o">=</span> <span class="s">&#39;</span><span class="se">\r\n</span><span class="s">&#39;</span>
    <span class="kn">from</span> <span class="nn">ce</span> <span class="kn">import</span> <span class="o">*</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="kn">from</span> <span class="nn">ce</span> <span class="kn">import</span> <span class="n">_exit</span>
        <span class="n">__all__</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#39;_exit&#39;</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
        <span class="k">pass</span>
    <span class="c"># We can use the standard Windows path.</span>
    <span class="kn">import</span> <span class="nn">ntpath</span> <span class="kn">as</span> <span class="nn">path</span>

    <span class="kn">import</span> <span class="nn">ce</span>
    <span class="n">__all__</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">_get_exports_list</span><span class="p">(</span><span class="n">ce</span><span class="p">))</span>
    <span class="k">del</span> <span class="n">ce</span>

    <span class="k">try</span><span class="p">:</span>
        <span class="kn">from</span> <span class="nn">ce</span> <span class="kn">import</span> <span class="n">_have_functions</span>
    <span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
        <span class="k">pass</span>

<span class="k">else</span><span class="p">:</span>
    <span class="k">raise</span> <span class="ne">ImportError</span><span class="p">(</span><span class="s">&#39;no os specific module found&#39;</span><span class="p">)</span>

<span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="s">&#39;os.path&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">path</span>
<span class="kn">from</span> <span class="nn">os.path</span> <span class="kn">import</span> <span class="p">(</span><span class="n">curdir</span><span class="p">,</span> <span class="n">pardir</span><span class="p">,</span> <span class="n">sep</span><span class="p">,</span> <span class="n">pathsep</span><span class="p">,</span> <span class="n">defpath</span><span class="p">,</span> <span class="n">extsep</span><span class="p">,</span> <span class="n">altsep</span><span class="p">,</span>
    <span class="n">devnull</span><span class="p">)</span>

<span class="k">del</span> <span class="n">_names</span>


<span class="k">if</span> <span class="n">_exists</span><span class="p">(</span><span class="s">&quot;_have_functions&quot;</span><span class="p">):</span>
    <span class="n">_globals</span> <span class="o">=</span> <span class="nb">globals</span><span class="p">()</span>
    <span class="k">def</span> <span class="nf">_add</span><span class="p">(</span><span class="nb">str</span><span class="p">,</span> <span class="n">fn</span><span class="p">):</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">fn</span> <span class="ow">in</span> <span class="n">_globals</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="nb">str</span> <span class="ow">in</span> <span class="n">_have_functions</span><span class="p">):</span>
            <span class="n">_set</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">_globals</span><span class="p">[</span><span class="n">fn</span><span class="p">])</span>

    <span class="n">_set</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_FACCESSAT&quot;</span><span class="p">,</span>  <span class="s">&quot;access&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_FCHMODAT&quot;</span><span class="p">,</span>   <span class="s">&quot;chmod&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_FCHOWNAT&quot;</span><span class="p">,</span>   <span class="s">&quot;chown&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_FSTATAT&quot;</span><span class="p">,</span>    <span class="s">&quot;stat&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_FUTIMESAT&quot;</span><span class="p">,</span>  <span class="s">&quot;utime&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_LINKAT&quot;</span><span class="p">,</span>     <span class="s">&quot;link&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_MKDIRAT&quot;</span><span class="p">,</span>    <span class="s">&quot;mkdir&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_MKFIFOAT&quot;</span><span class="p">,</span>   <span class="s">&quot;mkfifo&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_MKNODAT&quot;</span><span class="p">,</span>    <span class="s">&quot;mknod&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_OPENAT&quot;</span><span class="p">,</span>     <span class="s">&quot;open&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_READLINKAT&quot;</span><span class="p">,</span> <span class="s">&quot;readlink&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_RENAMEAT&quot;</span><span class="p">,</span>   <span class="s">&quot;rename&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_SYMLINKAT&quot;</span><span class="p">,</span>  <span class="s">&quot;symlink&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_UNLINKAT&quot;</span><span class="p">,</span>   <span class="s">&quot;unlink&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_UNLINKAT&quot;</span><span class="p">,</span>   <span class="s">&quot;rmdir&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_UTIMENSAT&quot;</span><span class="p">,</span>  <span class="s">&quot;utime&quot;</span><span class="p">)</span>
    <span class="n">supports_dir_fd</span> <span class="o">=</span> <span class="n">_set</span>

    <span class="n">_set</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_FACCESSAT&quot;</span><span class="p">,</span>  <span class="s">&quot;access&quot;</span><span class="p">)</span>
    <span class="n">supports_effective_ids</span> <span class="o">=</span> <span class="n">_set</span>

    <span class="n">_set</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_FCHDIR&quot;</span><span class="p">,</span>     <span class="s">&quot;chdir&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_FCHMOD&quot;</span><span class="p">,</span>     <span class="s">&quot;chmod&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_FCHOWN&quot;</span><span class="p">,</span>     <span class="s">&quot;chown&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_FDOPENDIR&quot;</span><span class="p">,</span>  <span class="s">&quot;listdir&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_FEXECVE&quot;</span><span class="p">,</span>    <span class="s">&quot;execve&quot;</span><span class="p">)</span>
    <span class="n">_set</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">stat</span><span class="p">)</span> <span class="c"># fstat always works</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_FTRUNCATE&quot;</span><span class="p">,</span>  <span class="s">&quot;truncate&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_FUTIMENS&quot;</span><span class="p">,</span>   <span class="s">&quot;utime&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_FUTIMES&quot;</span><span class="p">,</span>    <span class="s">&quot;utime&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_FPATHCONF&quot;</span><span class="p">,</span>  <span class="s">&quot;pathconf&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">_exists</span><span class="p">(</span><span class="s">&quot;statvfs&quot;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">_exists</span><span class="p">(</span><span class="s">&quot;fstatvfs&quot;</span><span class="p">):</span> <span class="c"># mac os x10.3</span>
        <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_FSTATVFS&quot;</span><span class="p">,</span> <span class="s">&quot;statvfs&quot;</span><span class="p">)</span>
    <span class="n">supports_fd</span> <span class="o">=</span> <span class="n">_set</span>

    <span class="n">_set</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_FACCESSAT&quot;</span><span class="p">,</span>  <span class="s">&quot;access&quot;</span><span class="p">)</span>
    <span class="c"># Some platforms don&#39;t support lchmod().  Often the function exists</span>
    <span class="c"># anyway, as a stub that always returns ENOSUP or perhaps EOPNOTSUPP.</span>
    <span class="c"># (No, I don&#39;t know why that&#39;s a good design.)  ./configure will detect</span>
    <span class="c"># this and reject it--so HAVE_LCHMOD still won&#39;t be defined on such</span>
    <span class="c"># platforms.  This is Very Helpful.</span>
    <span class="c">#</span>
    <span class="c"># However, sometimes platforms without a working lchmod() *do* have</span>
    <span class="c"># fchmodat().  (Examples: Linux kernel 3.2 with glibc 2.15,</span>
    <span class="c"># OpenIndiana 3.x.)  And fchmodat() has a flag that theoretically makes</span>
    <span class="c"># it behave like lchmod().  So in theory it would be a suitable</span>
    <span class="c"># replacement for lchmod().  But when lchmod() doesn&#39;t work, fchmodat()&#39;s</span>
    <span class="c"># flag doesn&#39;t work *either*.  Sadly ./configure isn&#39;t sophisticated</span>
    <span class="c"># enough to detect this condition--it only determines whether or not</span>
    <span class="c"># fchmodat() minimally works.</span>
    <span class="c">#</span>
    <span class="c"># Therefore we simply ignore fchmodat() when deciding whether or not</span>
    <span class="c"># os.chmod supports follow_symlinks.  Just checking lchmod() is</span>
    <span class="c"># sufficient.  After all--if you have a working fchmodat(), your</span>
    <span class="c"># lchmod() almost certainly works too.</span>
    <span class="c">#</span>
    <span class="c"># _add(&quot;HAVE_FCHMODAT&quot;,   &quot;chmod&quot;)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_FCHOWNAT&quot;</span><span class="p">,</span>   <span class="s">&quot;chown&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_FSTATAT&quot;</span><span class="p">,</span>    <span class="s">&quot;stat&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_LCHFLAGS&quot;</span><span class="p">,</span>   <span class="s">&quot;chflags&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_LCHMOD&quot;</span><span class="p">,</span>     <span class="s">&quot;chmod&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">_exists</span><span class="p">(</span><span class="s">&quot;lchown&quot;</span><span class="p">):</span> <span class="c"># mac os x10.3</span>
        <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_LCHOWN&quot;</span><span class="p">,</span> <span class="s">&quot;chown&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_LINKAT&quot;</span><span class="p">,</span>     <span class="s">&quot;link&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_LUTIMES&quot;</span><span class="p">,</span>    <span class="s">&quot;utime&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_LSTAT&quot;</span><span class="p">,</span>      <span class="s">&quot;stat&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_FSTATAT&quot;</span><span class="p">,</span>    <span class="s">&quot;stat&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;HAVE_UTIMENSAT&quot;</span><span class="p">,</span>  <span class="s">&quot;utime&quot;</span><span class="p">)</span>
    <span class="n">_add</span><span class="p">(</span><span class="s">&quot;MS_WINDOWS&quot;</span><span class="p">,</span>      <span class="s">&quot;stat&quot;</span><span class="p">)</span>
    <span class="n">supports_follow_symlinks</span> <span class="o">=</span> <span class="n">_set</span>

    <span class="k">del</span> <span class="n">_set</span>
    <span class="k">del</span> <span class="n">_have_functions</span>
    <span class="k">del</span> <span class="n">_globals</span>
    <span class="k">del</span> <span class="n">_add</span>


<span class="c"># Python uses fixed values for the SEEK_ constants; they are mapped</span>
<span class="c"># to native constants if necessary in posixmodule.c</span>
<span class="c"># Other possible SEEK values are directly imported from posixmodule.c</span>
<span class="n">SEEK_SET</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">SEEK_CUR</span> <span class="o">=</span> <span class="mi">1</span>
<span class="n">SEEK_END</span> <span class="o">=</span> <span class="mi">2</span>

<span class="c"># Super directory utilities.</span>
<span class="c"># (Inspired by Eric Raymond; the doc strings are mostly his)</span>

<div class="viewcode-block" id="makedirs"><a class="viewcode-back" href="../os.html#os.makedirs">[docs]</a><span class="k">def</span> <span class="nf">makedirs</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="mi">0</span><span class="n">o777</span><span class="p">,</span> <span class="n">exist_ok</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;makedirs(name [, mode=0o777][, exist_ok=False])</span>

<span class="sd">    Super-mkdir; create a leaf directory and all intermediate ones.  Works like</span>
<span class="sd">    mkdir, except that any intermediate path segment (not just the rightmost)</span>
<span class="sd">    will be created if it does not exist. If the target directory already</span>
<span class="sd">    exists, raise an OSError if exist_ok is False. Otherwise no exception is</span>
<span class="sd">    raised.  This is recursive.</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">head</span><span class="p">,</span> <span class="n">tail</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">tail</span><span class="p">:</span>
        <span class="n">head</span><span class="p">,</span> <span class="n">tail</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">head</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">head</span> <span class="ow">and</span> <span class="n">tail</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">head</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">makedirs</span><span class="p">(</span><span class="n">head</span><span class="p">,</span> <span class="n">mode</span><span class="p">,</span> <span class="n">exist_ok</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">FileExistsError</span><span class="p">:</span>
            <span class="c"># be happy if someone already created the path</span>
            <span class="k">pass</span>
        <span class="n">cdir</span> <span class="o">=</span> <span class="n">curdir</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">tail</span><span class="p">,</span> <span class="nb">bytes</span><span class="p">):</span>
            <span class="n">cdir</span> <span class="o">=</span> <span class="nb">bytes</span><span class="p">(</span><span class="n">curdir</span><span class="p">,</span> <span class="s">&#39;ASCII&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">tail</span> <span class="o">==</span> <span class="n">cdir</span><span class="p">:</span>           <span class="c"># xxx/newdir/. exists if xxx/newdir exists</span>
            <span class="k">return</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">mkdir</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">mode</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">exist_ok</span> <span class="ow">or</span> <span class="n">e</span><span class="o">.</span><span class="n">errno</span> <span class="o">!=</span> <span class="n">errno</span><span class="o">.</span><span class="n">EEXIST</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
            <span class="k">raise</span>
</div>
<div class="viewcode-block" id="removedirs"><a class="viewcode-back" href="../os.html#os.removedirs">[docs]</a><span class="k">def</span> <span class="nf">removedirs</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;removedirs(name)</span>

<span class="sd">    Super-rmdir; remove a leaf directory and all empty intermediate</span>
<span class="sd">    ones.  Works like rmdir except that, if the leaf directory is</span>
<span class="sd">    successfully removed, directories corresponding to rightmost path</span>
<span class="sd">    segments will be pruned away until either the whole path is</span>
<span class="sd">    consumed or an error occurs.  Errors during this latter phase are</span>
<span class="sd">    ignored -- they generally mean that a directory was not empty.</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">rmdir</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
    <span class="n">head</span><span class="p">,</span> <span class="n">tail</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">tail</span><span class="p">:</span>
        <span class="n">head</span><span class="p">,</span> <span class="n">tail</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">head</span><span class="p">)</span>
    <span class="k">while</span> <span class="n">head</span> <span class="ow">and</span> <span class="n">tail</span><span class="p">:</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">rmdir</span><span class="p">(</span><span class="n">head</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">OSError</span><span class="p">:</span>
            <span class="k">break</span>
        <span class="n">head</span><span class="p">,</span> <span class="n">tail</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">head</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="renames"><a class="viewcode-back" href="../os.html#os.renames">[docs]</a><span class="k">def</span> <span class="nf">renames</span><span class="p">(</span><span class="n">old</span><span class="p">,</span> <span class="n">new</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;renames(old, new)</span>

<span class="sd">    Super-rename; create directories as necessary and delete any left</span>
<span class="sd">    empty.  Works like rename, except creation of any intermediate</span>
<span class="sd">    directories needed to make the new pathname good is attempted</span>
<span class="sd">    first.  After the rename, directories corresponding to rightmost</span>
<span class="sd">    path segments of the old name will be pruned way until either the</span>
<span class="sd">    whole path is consumed or a nonempty directory is found.</span>

<span class="sd">    Note: this function can fail with the new directory structure made</span>
<span class="sd">    if you lack permissions needed to unlink the leaf directory or</span>
<span class="sd">    file.</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">head</span><span class="p">,</span> <span class="n">tail</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">new</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">head</span> <span class="ow">and</span> <span class="n">tail</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">head</span><span class="p">):</span>
        <span class="n">makedirs</span><span class="p">(</span><span class="n">head</span><span class="p">)</span>
    <span class="n">rename</span><span class="p">(</span><span class="n">old</span><span class="p">,</span> <span class="n">new</span><span class="p">)</span>
    <span class="n">head</span><span class="p">,</span> <span class="n">tail</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">old</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">head</span> <span class="ow">and</span> <span class="n">tail</span><span class="p">:</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">removedirs</span><span class="p">(</span><span class="n">head</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">OSError</span><span class="p">:</span>
            <span class="k">pass</span>
</div>
<span class="n">__all__</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s">&quot;makedirs&quot;</span><span class="p">,</span> <span class="s">&quot;removedirs&quot;</span><span class="p">,</span> <span class="s">&quot;renames&quot;</span><span class="p">])</span>

<div class="viewcode-block" id="walk"><a class="viewcode-back" href="../os.html#os.walk">[docs]</a><span class="k">def</span> <span class="nf">walk</span><span class="p">(</span><span class="n">top</span><span class="p">,</span> <span class="n">topdown</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">onerror</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">followlinks</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Directory tree generator.</span>

<span class="sd">    For each directory in the directory tree rooted at top (including top</span>
<span class="sd">    itself, but excluding &#39;.&#39; and &#39;..&#39;), yields a 3-tuple</span>

<span class="sd">        dirpath, dirnames, filenames</span>

<span class="sd">    dirpath is a string, the path to the directory.  dirnames is a list of</span>
<span class="sd">    the names of the subdirectories in dirpath (excluding &#39;.&#39; and &#39;..&#39;).</span>
<span class="sd">    filenames is a list of the names of the non-directory files in dirpath.</span>
<span class="sd">    Note that the names in the lists are just names, with no path components.</span>
<span class="sd">    To get a full path (which begins with top) to a file or directory in</span>
<span class="sd">    dirpath, do os.path.join(dirpath, name).</span>

<span class="sd">    If optional arg &#39;topdown&#39; is true or not specified, the triple for a</span>
<span class="sd">    directory is generated before the triples for any of its subdirectories</span>
<span class="sd">    (directories are generated top down).  If topdown is false, the triple</span>
<span class="sd">    for a directory is generated after the triples for all of its</span>
<span class="sd">    subdirectories (directories are generated bottom up).</span>

<span class="sd">    When topdown is true, the caller can modify the dirnames list in-place</span>
<span class="sd">    (e.g., via del or slice assignment), and walk will only recurse into the</span>
<span class="sd">    subdirectories whose names remain in dirnames; this can be used to prune the</span>
<span class="sd">    search, or to impose a specific order of visiting.  Modifying dirnames when</span>
<span class="sd">    topdown is false is ineffective, since the directories in dirnames have</span>
<span class="sd">    already been generated by the time dirnames itself is generated. No matter</span>
<span class="sd">    the value of topdown, the list of subdirectories is retrieved before the</span>
<span class="sd">    tuples for the directory and its subdirectories are generated.</span>

<span class="sd">    By default errors from the os.listdir() call are ignored.  If</span>
<span class="sd">    optional arg &#39;onerror&#39; is specified, it should be a function; it</span>
<span class="sd">    will be called with one argument, an OSError instance.  It can</span>
<span class="sd">    report the error to continue with the walk, or raise the exception</span>
<span class="sd">    to abort the walk.  Note that the filename is available as the</span>
<span class="sd">    filename attribute of the exception object.</span>

<span class="sd">    By default, os.walk does not follow symbolic links to subdirectories on</span>
<span class="sd">    systems that support them.  In order to get this functionality, set the</span>
<span class="sd">    optional argument &#39;followlinks&#39; to true.</span>

<span class="sd">    Caution:  if you pass a relative pathname for top, don&#39;t change the</span>
<span class="sd">    current working directory between resumptions of walk.  walk never</span>
<span class="sd">    changes the current directory, and assumes that the client doesn&#39;t</span>
<span class="sd">    either.</span>

<span class="sd">    Example:</span>

<span class="sd">    import os</span>
<span class="sd">    from os.path import join, getsize</span>
<span class="sd">    for root, dirs, files in os.walk(&#39;python/Lib/email&#39;):</span>
<span class="sd">        print(root, &quot;consumes&quot;, end=&quot;&quot;)</span>
<span class="sd">        print(sum([getsize(join(root, name)) for name in files]), end=&quot;&quot;)</span>
<span class="sd">        print(&quot;bytes in&quot;, len(files), &quot;non-directory files&quot;)</span>
<span class="sd">        if &#39;CVS&#39; in dirs:</span>
<span class="sd">            dirs.remove(&#39;CVS&#39;)  # don&#39;t visit CVS directories</span>

<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">islink</span><span class="p">,</span> <span class="n">join</span><span class="p">,</span> <span class="n">isdir</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">islink</span><span class="p">,</span> <span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">,</span> <span class="n">path</span><span class="o">.</span><span class="n">isdir</span>

    <span class="c"># We may not have read permission for top, in which case we can&#39;t</span>
    <span class="c"># get a list of the files the directory contains.  os.walk</span>
    <span class="c"># always suppressed the exception then, rather than blow up for a</span>
    <span class="c"># minor reason when (say) a thousand readable directories are still</span>
    <span class="c"># left to visit.  That logic is copied here.</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="c"># Note that listdir is global in this module due</span>
        <span class="c"># to earlier import-*.</span>
        <span class="n">names</span> <span class="o">=</span> <span class="n">listdir</span><span class="p">(</span><span class="n">top</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">err</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">onerror</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">onerror</span><span class="p">(</span><span class="n">err</span><span class="p">)</span>
        <span class="k">return</span>

    <span class="n">dirs</span><span class="p">,</span> <span class="n">nondirs</span> <span class="o">=</span> <span class="p">[],</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">names</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">isdir</span><span class="p">(</span><span class="n">join</span><span class="p">(</span><span class="n">top</span><span class="p">,</span> <span class="n">name</span><span class="p">)):</span>
            <span class="n">dirs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">nondirs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">topdown</span><span class="p">:</span>
        <span class="k">yield</span> <span class="n">top</span><span class="p">,</span> <span class="n">dirs</span><span class="p">,</span> <span class="n">nondirs</span>
    <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">dirs</span><span class="p">:</span>
        <span class="n">new_path</span> <span class="o">=</span> <span class="n">join</span><span class="p">(</span><span class="n">top</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">followlinks</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">islink</span><span class="p">(</span><span class="n">new_path</span><span class="p">):</span>
            <span class="k">yield from</span> <span class="n">walk</span><span class="p">(</span><span class="n">new_path</span><span class="p">,</span> <span class="n">topdown</span><span class="p">,</span> <span class="n">onerror</span><span class="p">,</span> <span class="n">followlinks</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">topdown</span><span class="p">:</span>
        <span class="k">yield</span> <span class="n">top</span><span class="p">,</span> <span class="n">dirs</span><span class="p">,</span> <span class="n">nondirs</span>
</div>
<span class="n">__all__</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&quot;walk&quot;</span><span class="p">)</span>

<span class="k">if</span> <span class="p">{</span><span class="nb">open</span><span class="p">,</span> <span class="n">stat</span><span class="p">}</span> <span class="o">&lt;=</span> <span class="n">supports_dir_fd</span> <span class="ow">and</span> <span class="p">{</span><span class="n">listdir</span><span class="p">,</span> <span class="n">stat</span><span class="p">}</span> <span class="o">&lt;=</span> <span class="n">supports_fd</span><span class="p">:</span>

    <span class="k">def</span> <span class="nf">fwalk</span><span class="p">(</span><span class="n">top</span><span class="o">=</span><span class="s">&quot;.&quot;</span><span class="p">,</span> <span class="n">topdown</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">onerror</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">follow_symlinks</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">dir_fd</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Directory tree generator.</span>

<span class="sd">        This behaves exactly like walk(), except that it yields a 4-tuple</span>

<span class="sd">            dirpath, dirnames, filenames, dirfd</span>

<span class="sd">        `dirpath`, `dirnames` and `filenames` are identical to walk() output,</span>
<span class="sd">        and `dirfd` is a file descriptor referring to the directory `dirpath`.</span>

<span class="sd">        The advantage of fwalk() over walk() is that it&#39;s safe against symlink</span>
<span class="sd">        races (when follow_symlinks is False).</span>

<span class="sd">        If dir_fd is not None, it should be a file descriptor open to a directory,</span>
<span class="sd">          and top should be relative; top will then be relative to that directory.</span>
<span class="sd">          (dir_fd is always supported for fwalk.)</span>

<span class="sd">        Caution:</span>
<span class="sd">        Since fwalk() yields file descriptors, those are only valid until the</span>
<span class="sd">        next iteration step, so you should dup() them if you want to keep them</span>
<span class="sd">        for a longer period.</span>

<span class="sd">        Example:</span>

<span class="sd">        import os</span>
<span class="sd">        for root, dirs, files, rootfd in os.fwalk(&#39;python/Lib/email&#39;):</span>
<span class="sd">            print(root, &quot;consumes&quot;, end=&quot;&quot;)</span>
<span class="sd">            print(sum([os.stat(name, dir_fd=rootfd).st_size for name in files]),</span>
<span class="sd">                  end=&quot;&quot;)</span>
<span class="sd">            print(&quot;bytes in&quot;, len(files), &quot;non-directory files&quot;)</span>
<span class="sd">            if &#39;CVS&#39; in dirs:</span>
<span class="sd">                dirs.remove(&#39;CVS&#39;)  # don&#39;t visit CVS directories</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c"># Note: To guard against symlink races, we use the standard</span>
        <span class="c"># lstat()/open()/fstat() trick.</span>
        <span class="n">orig_st</span> <span class="o">=</span> <span class="n">stat</span><span class="p">(</span><span class="n">top</span><span class="p">,</span> <span class="n">follow_symlinks</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">dir_fd</span><span class="o">=</span><span class="n">dir_fd</span><span class="p">)</span>
        <span class="n">topfd</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">top</span><span class="p">,</span> <span class="n">O_RDONLY</span><span class="p">,</span> <span class="n">dir_fd</span><span class="o">=</span><span class="n">dir_fd</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">follow_symlinks</span> <span class="ow">or</span> <span class="p">(</span><span class="n">st</span><span class="o">.</span><span class="n">S_ISDIR</span><span class="p">(</span><span class="n">orig_st</span><span class="o">.</span><span class="n">st_mode</span><span class="p">)</span> <span class="ow">and</span>
                                    <span class="n">path</span><span class="o">.</span><span class="n">samestat</span><span class="p">(</span><span class="n">orig_st</span><span class="p">,</span> <span class="n">stat</span><span class="p">(</span><span class="n">topfd</span><span class="p">)))):</span>
                <span class="k">yield from</span> <span class="n">_fwalk</span><span class="p">(</span><span class="n">topfd</span><span class="p">,</span> <span class="n">top</span><span class="p">,</span> <span class="n">topdown</span><span class="p">,</span> <span class="n">onerror</span><span class="p">,</span> <span class="n">follow_symlinks</span><span class="p">)</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="n">close</span><span class="p">(</span><span class="n">topfd</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_fwalk</span><span class="p">(</span><span class="n">topfd</span><span class="p">,</span> <span class="n">toppath</span><span class="p">,</span> <span class="n">topdown</span><span class="p">,</span> <span class="n">onerror</span><span class="p">,</span> <span class="n">follow_symlinks</span><span class="p">):</span>
        <span class="c"># Note: This uses O(depth of the directory tree) file descriptors: if</span>
        <span class="c"># necessary, it can be adapted to only require O(1) FDs, see issue</span>
        <span class="c"># #13734.</span>

        <span class="n">names</span> <span class="o">=</span> <span class="n">listdir</span><span class="p">(</span><span class="n">topfd</span><span class="p">)</span>
        <span class="n">dirs</span><span class="p">,</span> <span class="n">nondirs</span> <span class="o">=</span> <span class="p">[],</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">names</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="c"># Here, we don&#39;t use AT_SYMLINK_NOFOLLOW to be consistent with</span>
                <span class="c"># walk() which reports symlinks to directories as directories.</span>
                <span class="c"># We do however check for symlinks before recursing into</span>
                <span class="c"># a subdirectory.</span>
                <span class="k">if</span> <span class="n">st</span><span class="o">.</span><span class="n">S_ISDIR</span><span class="p">(</span><span class="n">stat</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">dir_fd</span><span class="o">=</span><span class="n">topfd</span><span class="p">)</span><span class="o">.</span><span class="n">st_mode</span><span class="p">):</span>
                    <span class="n">dirs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">nondirs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
            <span class="k">except</span> <span class="n">FileNotFoundError</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="c"># Add dangling symlinks, ignore disappeared files</span>
                    <span class="k">if</span> <span class="n">st</span><span class="o">.</span><span class="n">S_ISLNK</span><span class="p">(</span><span class="n">stat</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">dir_fd</span><span class="o">=</span><span class="n">topfd</span><span class="p">,</span> <span class="n">follow_symlinks</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
                                <span class="o">.</span><span class="n">st_mode</span><span class="p">):</span>
                        <span class="n">nondirs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
                <span class="k">except</span> <span class="n">FileNotFoundError</span><span class="p">:</span>
                    <span class="k">continue</span>

        <span class="k">if</span> <span class="n">topdown</span><span class="p">:</span>
            <span class="k">yield</span> <span class="n">toppath</span><span class="p">,</span> <span class="n">dirs</span><span class="p">,</span> <span class="n">nondirs</span><span class="p">,</span> <span class="n">topfd</span>

        <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">dirs</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">orig_st</span> <span class="o">=</span> <span class="n">stat</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">dir_fd</span><span class="o">=</span><span class="n">topfd</span><span class="p">,</span> <span class="n">follow_symlinks</span><span class="o">=</span><span class="n">follow_symlinks</span><span class="p">)</span>
                <span class="n">dirfd</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">O_RDONLY</span><span class="p">,</span> <span class="n">dir_fd</span><span class="o">=</span><span class="n">topfd</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">err</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">onerror</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
                    <span class="n">onerror</span><span class="p">(</span><span class="n">err</span><span class="p">)</span>
                <span class="k">return</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">follow_symlinks</span> <span class="ow">or</span> <span class="n">path</span><span class="o">.</span><span class="n">samestat</span><span class="p">(</span><span class="n">orig_st</span><span class="p">,</span> <span class="n">stat</span><span class="p">(</span><span class="n">dirfd</span><span class="p">)):</span>
                    <span class="n">dirpath</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">toppath</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
                    <span class="k">yield from</span> <span class="n">_fwalk</span><span class="p">(</span><span class="n">dirfd</span><span class="p">,</span> <span class="n">dirpath</span><span class="p">,</span> <span class="n">topdown</span><span class="p">,</span> <span class="n">onerror</span><span class="p">,</span> <span class="n">follow_symlinks</span><span class="p">)</span>
            <span class="k">finally</span><span class="p">:</span>
                <span class="n">close</span><span class="p">(</span><span class="n">dirfd</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">topdown</span><span class="p">:</span>
            <span class="k">yield</span> <span class="n">toppath</span><span class="p">,</span> <span class="n">dirs</span><span class="p">,</span> <span class="n">nondirs</span><span class="p">,</span> <span class="n">topfd</span>

    <span class="n">__all__</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&quot;fwalk&quot;</span><span class="p">)</span>

<span class="c"># Make sure os.environ exists, at least</span>
<span class="k">try</span><span class="p">:</span>
    <span class="n">environ</span>
<span class="k">except</span> <span class="ne">NameError</span><span class="p">:</span>
    <span class="n">environ</span> <span class="o">=</span> <span class="p">{}</span>

<div class="viewcode-block" id="execl"><a class="viewcode-back" href="../os.html#os.execl">[docs]</a><span class="k">def</span> <span class="nf">execl</span><span class="p">(</span><span class="nb">file</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;execl(file, *args)</span>

<span class="sd">    Execute the executable file with argument list args, replacing the</span>
<span class="sd">    current process. &quot;&quot;&quot;</span>
    <span class="n">execv</span><span class="p">(</span><span class="nb">file</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="execle"><a class="viewcode-back" href="../os.html#os.execle">[docs]</a><span class="k">def</span> <span class="nf">execle</span><span class="p">(</span><span class="nb">file</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;execle(file, *args, env)</span>

<span class="sd">    Execute the executable file with argument list args and</span>
<span class="sd">    environment env, replacing the current process. &quot;&quot;&quot;</span>
    <span class="n">env</span> <span class="o">=</span> <span class="n">args</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
    <span class="n">execve</span><span class="p">(</span><span class="nb">file</span><span class="p">,</span> <span class="n">args</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="n">env</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="execlp"><a class="viewcode-back" href="../os.html#os.execlp">[docs]</a><span class="k">def</span> <span class="nf">execlp</span><span class="p">(</span><span class="nb">file</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;execlp(file, *args)</span>

<span class="sd">    Execute the executable file (which is searched for along $PATH)</span>
<span class="sd">    with argument list args, replacing the current process. &quot;&quot;&quot;</span>
    <span class="n">execvp</span><span class="p">(</span><span class="nb">file</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="execlpe"><a class="viewcode-back" href="../os.html#os.execlpe">[docs]</a><span class="k">def</span> <span class="nf">execlpe</span><span class="p">(</span><span class="nb">file</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;execlpe(file, *args, env)</span>

<span class="sd">    Execute the executable file (which is searched for along $PATH)</span>
<span class="sd">    with argument list args and environment env, replacing the current</span>
<span class="sd">    process. &quot;&quot;&quot;</span>
    <span class="n">env</span> <span class="o">=</span> <span class="n">args</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
    <span class="n">execvpe</span><span class="p">(</span><span class="nb">file</span><span class="p">,</span> <span class="n">args</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="n">env</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="execvp"><a class="viewcode-back" href="../os.html#os.execvp">[docs]</a><span class="k">def</span> <span class="nf">execvp</span><span class="p">(</span><span class="nb">file</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;execvp(file, args)</span>

<span class="sd">    Execute the executable file (which is searched for along $PATH)</span>
<span class="sd">    with argument list args, replacing the current process.</span>
<span class="sd">    args may be a list or tuple of strings. &quot;&quot;&quot;</span>
    <span class="n">_execvpe</span><span class="p">(</span><span class="nb">file</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="execvpe"><a class="viewcode-back" href="../os.html#os.execvpe">[docs]</a><span class="k">def</span> <span class="nf">execvpe</span><span class="p">(</span><span class="nb">file</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">env</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;execvpe(file, args, env)</span>

<span class="sd">    Execute the executable file (which is searched for along $PATH)</span>
<span class="sd">    with argument list args and environment env , replacing the</span>
<span class="sd">    current process.</span>
<span class="sd">    args may be a list or tuple of strings. &quot;&quot;&quot;</span>
    <span class="n">_execvpe</span><span class="p">(</span><span class="nb">file</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">env</span><span class="p">)</span>
</div>
<span class="n">__all__</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s">&quot;execl&quot;</span><span class="p">,</span><span class="s">&quot;execle&quot;</span><span class="p">,</span><span class="s">&quot;execlp&quot;</span><span class="p">,</span><span class="s">&quot;execlpe&quot;</span><span class="p">,</span><span class="s">&quot;execvp&quot;</span><span class="p">,</span><span class="s">&quot;execvpe&quot;</span><span class="p">])</span>

<span class="k">def</span> <span class="nf">_execvpe</span><span class="p">(</span><span class="nb">file</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">env</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">env</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">exec_func</span> <span class="o">=</span> <span class="n">execve</span>
        <span class="n">argrest</span> <span class="o">=</span> <span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="n">env</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">exec_func</span> <span class="o">=</span> <span class="n">execv</span>
        <span class="n">argrest</span> <span class="o">=</span> <span class="p">(</span><span class="n">args</span><span class="p">,)</span>
        <span class="n">env</span> <span class="o">=</span> <span class="n">environ</span>

    <span class="n">head</span><span class="p">,</span> <span class="n">tail</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="nb">file</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">head</span><span class="p">:</span>
        <span class="n">exec_func</span><span class="p">(</span><span class="nb">file</span><span class="p">,</span> <span class="o">*</span><span class="n">argrest</span><span class="p">)</span>
        <span class="k">return</span>
    <span class="n">last_exc</span> <span class="o">=</span> <span class="n">saved_exc</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="n">saved_tb</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="n">path_list</span> <span class="o">=</span> <span class="n">get_exec_path</span><span class="p">(</span><span class="n">env</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">name</span> <span class="o">!=</span> <span class="s">&#39;nt&#39;</span><span class="p">:</span>
        <span class="nb">file</span> <span class="o">=</span> <span class="n">fsencode</span><span class="p">(</span><span class="nb">file</span><span class="p">)</span>
        <span class="n">path_list</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="n">fsencode</span><span class="p">,</span> <span class="n">path_list</span><span class="p">)</span>
    <span class="k">for</span> <span class="nb">dir</span> <span class="ow">in</span> <span class="n">path_list</span><span class="p">:</span>
        <span class="n">fullname</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">dir</span><span class="p">,</span> <span class="nb">file</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">exec_func</span><span class="p">(</span><span class="n">fullname</span><span class="p">,</span> <span class="o">*</span><span class="n">argrest</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="n">last_exc</span> <span class="o">=</span> <span class="n">e</span>
            <span class="n">tb</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">exc_info</span><span class="p">()[</span><span class="mi">2</span><span class="p">]</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">errno</span> <span class="o">!=</span> <span class="n">errno</span><span class="o">.</span><span class="n">ENOENT</span> <span class="ow">and</span> <span class="n">e</span><span class="o">.</span><span class="n">errno</span> <span class="o">!=</span> <span class="n">errno</span><span class="o">.</span><span class="n">ENOTDIR</span>
                <span class="ow">and</span> <span class="n">saved_exc</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">):</span>
                <span class="n">saved_exc</span> <span class="o">=</span> <span class="n">e</span>
                <span class="n">saved_tb</span> <span class="o">=</span> <span class="n">tb</span>
    <span class="k">if</span> <span class="n">saved_exc</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">saved_exc</span><span class="o">.</span><span class="n">with_traceback</span><span class="p">(</span><span class="n">saved_tb</span><span class="p">)</span>
    <span class="k">raise</span> <span class="n">last_exc</span><span class="o">.</span><span class="n">with_traceback</span><span class="p">(</span><span class="n">tb</span><span class="p">)</span>


<div class="viewcode-block" id="get_exec_path"><a class="viewcode-back" href="../os.html#os.get_exec_path">[docs]</a><span class="k">def</span> <span class="nf">get_exec_path</span><span class="p">(</span><span class="n">env</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Returns the sequence of directories that will be searched for the</span>
<span class="sd">    named executable (similar to a shell) when launching a process.</span>

<span class="sd">    *env* must be an environment variable dict or None.  If *env* is None,</span>
<span class="sd">    os.environ will be used.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c"># Use a local import instead of a global import to limit the number of</span>
    <span class="c"># modules loaded at startup: the os module is always loaded at startup by</span>
    <span class="c"># Python. It may also avoid a bootstrap issue.</span>
    <span class="kn">import</span> <span class="nn">warnings</span>

    <span class="k">if</span> <span class="n">env</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">env</span> <span class="o">=</span> <span class="n">environ</span>

    <span class="c"># {b&#39;PATH&#39;: ...}.get(&#39;PATH&#39;) and {&#39;PATH&#39;: ...}.get(b&#39;PATH&#39;) emit a</span>
    <span class="c"># BytesWarning when using python -b or python -bb: ignore the warning</span>
    <span class="k">with</span> <span class="n">warnings</span><span class="o">.</span><span class="n">catch_warnings</span><span class="p">():</span>
        <span class="n">warnings</span><span class="o">.</span><span class="n">simplefilter</span><span class="p">(</span><span class="s">&quot;ignore&quot;</span><span class="p">,</span> <span class="n">BytesWarning</span><span class="p">)</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="n">path_list</span> <span class="o">=</span> <span class="n">env</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;PATH&#39;</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">TypeError</span><span class="p">:</span>
            <span class="n">path_list</span> <span class="o">=</span> <span class="bp">None</span>

        <span class="k">if</span> <span class="n">supports_bytes_environ</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">path_listb</span> <span class="o">=</span> <span class="n">env</span><span class="p">[</span><span class="n">b</span><span class="s">&#39;PATH&#39;</span><span class="p">]</span>
            <span class="k">except</span> <span class="p">(</span><span class="ne">KeyError</span><span class="p">,</span> <span class="ne">TypeError</span><span class="p">):</span>
                <span class="k">pass</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">path_list</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                        <span class="s">&quot;env cannot contain &#39;PATH&#39; and b&#39;PATH&#39; keys&quot;</span><span class="p">)</span>
                <span class="n">path_list</span> <span class="o">=</span> <span class="n">path_listb</span>

            <span class="k">if</span> <span class="n">path_list</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">path_list</span><span class="p">,</span> <span class="nb">bytes</span><span class="p">):</span>
                <span class="n">path_list</span> <span class="o">=</span> <span class="n">fsdecode</span><span class="p">(</span><span class="n">path_list</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">path_list</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">path_list</span> <span class="o">=</span> <span class="n">defpath</span>
    <span class="k">return</span> <span class="n">path_list</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">pathsep</span><span class="p">)</span>


<span class="c"># Change environ to automatically call putenv(), unsetenv if they exist.</span></div>
<span class="kn">from</span> <span class="nn">_collections_abc</span> <span class="kn">import</span> <span class="n">MutableMapping</span>

<span class="k">class</span> <span class="nc">_Environ</span><span class="p">(</span><span class="n">MutableMapping</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">encodekey</span><span class="p">,</span> <span class="n">decodekey</span><span class="p">,</span> <span class="n">encodevalue</span><span class="p">,</span> <span class="n">decodevalue</span><span class="p">,</span> <span class="n">putenv</span><span class="p">,</span> <span class="n">unsetenv</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">encodekey</span> <span class="o">=</span> <span class="n">encodekey</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">decodekey</span> <span class="o">=</span> <span class="n">decodekey</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">encodevalue</span> <span class="o">=</span> <span class="n">encodevalue</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">decodevalue</span> <span class="o">=</span> <span class="n">decodevalue</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">putenv</span> <span class="o">=</span> <span class="n">putenv</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">unsetenv</span> <span class="o">=</span> <span class="n">unsetenv</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_data</span> <span class="o">=</span> <span class="n">data</span>

    <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">encodekey</span><span class="p">(</span><span class="n">key</span><span class="p">)]</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="c"># raise KeyError with the original key value</span>
            <span class="k">raise</span> <span class="ne">KeyError</span><span class="p">(</span><span class="n">key</span><span class="p">)</span> <span class="kn">from</span> <span class="bp">None</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">decodevalue</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__setitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="n">key</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">encodekey</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
        <span class="n">value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">encodevalue</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">putenv</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>

    <span class="k">def</span> <span class="nf">__delitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
        <span class="n">encodedkey</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">encodekey</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">unsetenv</span><span class="p">(</span><span class="n">encodedkey</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="n">encodedkey</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="c"># raise KeyError with the original key value</span>
            <span class="k">raise</span> <span class="ne">KeyError</span><span class="p">(</span><span class="n">key</span><span class="p">)</span> <span class="kn">from</span> <span class="bp">None</span>

    <span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">:</span>
            <span class="k">yield</span> <span class="bp">self</span><span class="o">.</span><span class="n">decodekey</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">&#39;environ({{{}}})&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span>
            <span class="p">(</span><span class="s">&#39;{!r}: {!r}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">decodekey</span><span class="p">(</span><span class="n">key</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">decodevalue</span><span class="p">(</span><span class="n">value</span><span class="p">))</span>
            <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="o">.</span><span class="n">items</span><span class="p">())))</span>

    <span class="k">def</span> <span class="nf">copy</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">dict</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">setdefault</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">key</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="p">:</span>
            <span class="bp">self</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
        <span class="k">return</span> <span class="bp">self</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>

<span class="k">try</span><span class="p">:</span>
    <span class="n">_putenv</span> <span class="o">=</span> <span class="n">putenv</span>
<span class="k">except</span> <span class="ne">NameError</span><span class="p">:</span>
    <span class="n">_putenv</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">:</span> <span class="bp">None</span>
<span class="k">else</span><span class="p">:</span>
    <span class="k">if</span> <span class="s">&quot;putenv&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">__all__</span><span class="p">:</span>
        <span class="n">__all__</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&quot;putenv&quot;</span><span class="p">)</span>

<span class="k">try</span><span class="p">:</span>
    <span class="n">_unsetenv</span> <span class="o">=</span> <span class="n">unsetenv</span>
<span class="k">except</span> <span class="ne">NameError</span><span class="p">:</span>
    <span class="n">_unsetenv</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">key</span><span class="p">:</span> <span class="n">_putenv</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="s">&quot;&quot;</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
    <span class="k">if</span> <span class="s">&quot;unsetenv&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">__all__</span><span class="p">:</span>
        <span class="n">__all__</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&quot;unsetenv&quot;</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">_createenviron</span><span class="p">():</span>
    <span class="k">if</span> <span class="n">name</span> <span class="o">==</span> <span class="s">&#39;nt&#39;</span><span class="p">:</span>
        <span class="c"># Where Env Var Names Must Be UPPERCASE</span>
        <span class="k">def</span> <span class="nf">check_str</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s">&quot;str expected, not </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="nb">type</span><span class="p">(</span><span class="n">value</span><span class="p">)</span><span class="o">.</span><span class="n">__name__</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">value</span>
        <span class="n">encode</span> <span class="o">=</span> <span class="n">check_str</span>
        <span class="n">decode</span> <span class="o">=</span> <span class="nb">str</span>
        <span class="k">def</span> <span class="nf">encodekey</span><span class="p">(</span><span class="n">key</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">encode</span><span class="p">(</span><span class="n">key</span><span class="p">)</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span>
        <span class="n">data</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">environ</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">data</span><span class="p">[</span><span class="n">encodekey</span><span class="p">(</span><span class="n">key</span><span class="p">)]</span> <span class="o">=</span> <span class="n">value</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="c"># Where Env Var Names Can Be Mixed Case</span>
        <span class="n">encoding</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">getfilesystemencoding</span><span class="p">()</span>
        <span class="k">def</span> <span class="nf">encode</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s">&quot;str expected, not </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="nb">type</span><span class="p">(</span><span class="n">value</span><span class="p">)</span><span class="o">.</span><span class="n">__name__</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">value</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="n">encoding</span><span class="p">,</span> <span class="s">&#39;surrogateescape&#39;</span><span class="p">)</span>
        <span class="k">def</span> <span class="nf">decode</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">value</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="n">encoding</span><span class="p">,</span> <span class="s">&#39;surrogateescape&#39;</span><span class="p">)</span>
        <span class="n">encodekey</span> <span class="o">=</span> <span class="n">encode</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">environ</span>
    <span class="k">return</span> <span class="n">_Environ</span><span class="p">(</span><span class="n">data</span><span class="p">,</span>
        <span class="n">encodekey</span><span class="p">,</span> <span class="n">decode</span><span class="p">,</span>
        <span class="n">encode</span><span class="p">,</span> <span class="n">decode</span><span class="p">,</span>
        <span class="n">_putenv</span><span class="p">,</span> <span class="n">_unsetenv</span><span class="p">)</span>

<span class="c"># unicode environ</span>
<span class="n">environ</span> <span class="o">=</span> <span class="n">_createenviron</span><span class="p">()</span>
<span class="k">del</span> <span class="n">_createenviron</span>


<div class="viewcode-block" id="getenv"><a class="viewcode-back" href="../os.html#os.getenv">[docs]</a><span class="k">def</span> <span class="nf">getenv</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Get an environment variable, return None if it doesn&#39;t exist.</span>
<span class="sd">    The optional second argument can specify an alternate default.</span>
<span class="sd">    key, default and the result are str.&quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">default</span><span class="p">)</span>
</div>
<span class="n">supports_bytes_environ</span> <span class="o">=</span> <span class="p">(</span><span class="n">name</span> <span class="o">!=</span> <span class="s">&#39;nt&#39;</span><span class="p">)</span>
<span class="n">__all__</span><span class="o">.</span><span class="n">extend</span><span class="p">((</span><span class="s">&quot;getenv&quot;</span><span class="p">,</span> <span class="s">&quot;supports_bytes_environ&quot;</span><span class="p">))</span>

<span class="k">if</span> <span class="n">supports_bytes_environ</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">_check_bytes</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">bytes</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s">&quot;bytes expected, not </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="nb">type</span><span class="p">(</span><span class="n">value</span><span class="p">)</span><span class="o">.</span><span class="n">__name__</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">value</span>

    <span class="c"># bytes environ</span>
    <span class="n">environb</span> <span class="o">=</span> <span class="n">_Environ</span><span class="p">(</span><span class="n">environ</span><span class="o">.</span><span class="n">_data</span><span class="p">,</span>
        <span class="n">_check_bytes</span><span class="p">,</span> <span class="nb">bytes</span><span class="p">,</span>
        <span class="n">_check_bytes</span><span class="p">,</span> <span class="nb">bytes</span><span class="p">,</span>
        <span class="n">_putenv</span><span class="p">,</span> <span class="n">_unsetenv</span><span class="p">)</span>
    <span class="k">del</span> <span class="n">_check_bytes</span>

    <span class="k">def</span> <span class="nf">getenvb</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Get an environment variable, return None if it doesn&#39;t exist.</span>
<span class="sd">        The optional second argument can specify an alternate default.</span>
<span class="sd">        key, default and the result are bytes.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">environb</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">default</span><span class="p">)</span>

    <span class="n">__all__</span><span class="o">.</span><span class="n">extend</span><span class="p">((</span><span class="s">&quot;environb&quot;</span><span class="p">,</span> <span class="s">&quot;getenvb&quot;</span><span class="p">))</span>

<span class="k">def</span> <span class="nf">_fscodec</span><span class="p">():</span>
    <span class="n">encoding</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">getfilesystemencoding</span><span class="p">()</span>
    <span class="k">if</span> <span class="n">encoding</span> <span class="o">==</span> <span class="s">&#39;mbcs&#39;</span><span class="p">:</span>
        <span class="n">errors</span> <span class="o">=</span> <span class="s">&#39;strict&#39;</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">errors</span> <span class="o">=</span> <span class="s">&#39;surrogateescape&#39;</span>

    <span class="k">def</span> <span class="nf">fsencode</span><span class="p">(</span><span class="n">filename</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Encode filename to the filesystem encoding with &#39;surrogateescape&#39; error</span>
<span class="sd">        handler, return bytes unchanged. On Windows, use &#39;strict&#39; error handler if</span>
<span class="sd">        the file system encoding is &#39;mbcs&#39; (which is the default encoding).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="nb">bytes</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">filename</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">filename</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="n">encoding</span><span class="p">,</span> <span class="n">errors</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s">&quot;expect bytes or str, not </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="nb">type</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span><span class="o">.</span><span class="n">__name__</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">fsdecode</span><span class="p">(</span><span class="n">filename</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Decode filename from the filesystem encoding with &#39;surrogateescape&#39; error</span>
<span class="sd">        handler, return str unchanged. On Windows, use &#39;strict&#39; error handler if</span>
<span class="sd">        the file system encoding is &#39;mbcs&#39; (which is the default encoding).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">filename</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="nb">bytes</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">filename</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="n">encoding</span><span class="p">,</span> <span class="n">errors</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s">&quot;expect bytes or str, not </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="nb">type</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span><span class="o">.</span><span class="n">__name__</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">fsencode</span><span class="p">,</span> <span class="n">fsdecode</span>

<span class="n">fsencode</span><span class="p">,</span> <span class="n">fsdecode</span> <span class="o">=</span> <span class="n">_fscodec</span><span class="p">()</span>
<span class="k">del</span> <span class="n">_fscodec</span>

<span class="c"># Supply spawn*() (probably only for Unix)</span>
<span class="k">if</span> <span class="n">_exists</span><span class="p">(</span><span class="s">&quot;fork&quot;</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">_exists</span><span class="p">(</span><span class="s">&quot;spawnv&quot;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">_exists</span><span class="p">(</span><span class="s">&quot;execv&quot;</span><span class="p">):</span>

    <span class="n">P_WAIT</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">P_NOWAIT</span> <span class="o">=</span> <span class="n">P_NOWAITO</span> <span class="o">=</span> <span class="mi">1</span>

    <span class="n">__all__</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s">&quot;P_WAIT&quot;</span><span class="p">,</span> <span class="s">&quot;P_NOWAIT&quot;</span><span class="p">,</span> <span class="s">&quot;P_NOWAITO&quot;</span><span class="p">])</span>

    <span class="c"># XXX Should we support P_DETACH?  I suppose it could fork()**2</span>
    <span class="c"># and close the std I/O streams.  Also, P_OVERLAY is the same</span>
    <span class="c"># as execv*()?</span>

    <span class="k">def</span> <span class="nf">_spawnvef</span><span class="p">(</span><span class="n">mode</span><span class="p">,</span> <span class="nb">file</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">env</span><span class="p">,</span> <span class="n">func</span><span class="p">):</span>
        <span class="c"># Internal helper; func is the exec*() function to use</span>
        <span class="n">pid</span> <span class="o">=</span> <span class="n">fork</span><span class="p">()</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">pid</span><span class="p">:</span>
            <span class="c"># Child</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">env</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
                    <span class="n">func</span><span class="p">(</span><span class="nb">file</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">func</span><span class="p">(</span><span class="nb">file</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">env</span><span class="p">)</span>
            <span class="k">except</span><span class="p">:</span>
                <span class="n">_exit</span><span class="p">(</span><span class="mi">127</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c"># Parent</span>
            <span class="k">if</span> <span class="n">mode</span> <span class="o">==</span> <span class="n">P_NOWAIT</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">pid</span> <span class="c"># Caller is responsible for waiting!</span>
            <span class="k">while</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">wpid</span><span class="p">,</span> <span class="n">sts</span> <span class="o">=</span> <span class="n">waitpid</span><span class="p">(</span><span class="n">pid</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">WIFSTOPPED</span><span class="p">(</span><span class="n">sts</span><span class="p">):</span>
                    <span class="k">continue</span>
                <span class="k">elif</span> <span class="n">WIFSIGNALED</span><span class="p">(</span><span class="n">sts</span><span class="p">):</span>
                    <span class="k">return</span> <span class="o">-</span><span class="n">WTERMSIG</span><span class="p">(</span><span class="n">sts</span><span class="p">)</span>
                <span class="k">elif</span> <span class="n">WIFEXITED</span><span class="p">(</span><span class="n">sts</span><span class="p">):</span>
                    <span class="k">return</span> <span class="n">WEXITSTATUS</span><span class="p">(</span><span class="n">sts</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">OSError</span><span class="p">(</span><span class="s">&quot;Not stopped, signaled or exited???&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">spawnv</span><span class="p">(</span><span class="n">mode</span><span class="p">,</span> <span class="nb">file</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;spawnv(mode, file, args) -&gt; integer</span>

<span class="sd">Execute file with arguments from args in a subprocess.</span>
<span class="sd">If mode == P_NOWAIT return the pid of the process.</span>
<span class="sd">If mode == P_WAIT return the process&#39;s exit code if it exits normally;</span>
<span class="sd">otherwise return -SIG, where SIG is the signal that killed it. &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">_spawnvef</span><span class="p">(</span><span class="n">mode</span><span class="p">,</span> <span class="nb">file</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="bp">None</span><span class="p">,</span> <span class="n">execv</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">spawnve</span><span class="p">(</span><span class="n">mode</span><span class="p">,</span> <span class="nb">file</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">env</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;spawnve(mode, file, args, env) -&gt; integer</span>

<span class="sd">Execute file with arguments from args in a subprocess with the</span>
<span class="sd">specified environment.</span>
<span class="sd">If mode == P_NOWAIT return the pid of the process.</span>
<span class="sd">If mode == P_WAIT return the process&#39;s exit code if it exits normally;</span>
<span class="sd">otherwise return -SIG, where SIG is the signal that killed it. &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">_spawnvef</span><span class="p">(</span><span class="n">mode</span><span class="p">,</span> <span class="nb">file</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">env</span><span class="p">,</span> <span class="n">execve</span><span class="p">)</span>

    <span class="c"># Note: spawnvp[e] is&#39;t currently supported on Windows</span>

    <span class="k">def</span> <span class="nf">spawnvp</span><span class="p">(</span><span class="n">mode</span><span class="p">,</span> <span class="nb">file</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;spawnvp(mode, file, args) -&gt; integer</span>

<span class="sd">Execute file (which is looked for along $PATH) with arguments from</span>
<span class="sd">args in a subprocess.</span>
<span class="sd">If mode == P_NOWAIT return the pid of the process.</span>
<span class="sd">If mode == P_WAIT return the process&#39;s exit code if it exits normally;</span>
<span class="sd">otherwise return -SIG, where SIG is the signal that killed it. &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">_spawnvef</span><span class="p">(</span><span class="n">mode</span><span class="p">,</span> <span class="nb">file</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="bp">None</span><span class="p">,</span> <span class="n">execvp</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">spawnvpe</span><span class="p">(</span><span class="n">mode</span><span class="p">,</span> <span class="nb">file</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">env</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;spawnvpe(mode, file, args, env) -&gt; integer</span>

<span class="sd">Execute file (which is looked for along $PATH) with arguments from</span>
<span class="sd">args in a subprocess with the supplied environment.</span>
<span class="sd">If mode == P_NOWAIT return the pid of the process.</span>
<span class="sd">If mode == P_WAIT return the process&#39;s exit code if it exits normally;</span>
<span class="sd">otherwise return -SIG, where SIG is the signal that killed it. &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">_spawnvef</span><span class="p">(</span><span class="n">mode</span><span class="p">,</span> <span class="nb">file</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">env</span><span class="p">,</span> <span class="n">execvpe</span><span class="p">)</span>


    <span class="n">__all__</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s">&quot;spawnv&quot;</span><span class="p">,</span> <span class="s">&quot;spawnve&quot;</span><span class="p">,</span> <span class="s">&quot;spawnvp&quot;</span><span class="p">,</span> <span class="s">&quot;spawnvpe&quot;</span><span class="p">])</span>


<span class="k">if</span> <span class="n">_exists</span><span class="p">(</span><span class="s">&quot;spawnv&quot;</span><span class="p">):</span>
    <span class="c"># These aren&#39;t supplied by the basic Windows code</span>
    <span class="c"># but can be easily implemented in Python</span>

<div class="viewcode-block" id="spawnl"><a class="viewcode-back" href="../os.html#os.spawnl">[docs]</a>    <span class="k">def</span> <span class="nf">spawnl</span><span class="p">(</span><span class="n">mode</span><span class="p">,</span> <span class="nb">file</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;spawnl(mode, file, *args) -&gt; integer</span>

<span class="sd">Execute file with arguments from args in a subprocess.</span>
<span class="sd">If mode == P_NOWAIT return the pid of the process.</span>
<span class="sd">If mode == P_WAIT return the process&#39;s exit code if it exits normally;</span>
<span class="sd">otherwise return -SIG, where SIG is the signal that killed it. &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">spawnv</span><span class="p">(</span><span class="n">mode</span><span class="p">,</span> <span class="nb">file</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="spawnle"><a class="viewcode-back" href="../os.html#os.spawnle">[docs]</a>    <span class="k">def</span> <span class="nf">spawnle</span><span class="p">(</span><span class="n">mode</span><span class="p">,</span> <span class="nb">file</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;spawnle(mode, file, *args, env) -&gt; integer</span>

<span class="sd">Execute file with arguments from args in a subprocess with the</span>
<span class="sd">supplied environment.</span>
<span class="sd">If mode == P_NOWAIT return the pid of the process.</span>
<span class="sd">If mode == P_WAIT return the process&#39;s exit code if it exits normally;</span>
<span class="sd">otherwise return -SIG, where SIG is the signal that killed it. &quot;&quot;&quot;</span>
        <span class="n">env</span> <span class="o">=</span> <span class="n">args</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">spawnve</span><span class="p">(</span><span class="n">mode</span><span class="p">,</span> <span class="nb">file</span><span class="p">,</span> <span class="n">args</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="n">env</span><span class="p">)</span>

</div>
    <span class="n">__all__</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s">&quot;spawnl&quot;</span><span class="p">,</span> <span class="s">&quot;spawnle&quot;</span><span class="p">])</span>


<span class="k">if</span> <span class="n">_exists</span><span class="p">(</span><span class="s">&quot;spawnvp&quot;</span><span class="p">):</span>
    <span class="c"># At the moment, Windows doesn&#39;t implement spawnvp[e],</span>
    <span class="c"># so it won&#39;t have spawnlp[e] either.</span>
    <span class="k">def</span> <span class="nf">spawnlp</span><span class="p">(</span><span class="n">mode</span><span class="p">,</span> <span class="nb">file</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;spawnlp(mode, file, *args) -&gt; integer</span>

<span class="sd">Execute file (which is looked for along $PATH) with arguments from</span>
<span class="sd">args in a subprocess with the supplied environment.</span>
<span class="sd">If mode == P_NOWAIT return the pid of the process.</span>
<span class="sd">If mode == P_WAIT return the process&#39;s exit code if it exits normally;</span>
<span class="sd">otherwise return -SIG, where SIG is the signal that killed it. &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">spawnvp</span><span class="p">(</span><span class="n">mode</span><span class="p">,</span> <span class="nb">file</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">spawnlpe</span><span class="p">(</span><span class="n">mode</span><span class="p">,</span> <span class="nb">file</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;spawnlpe(mode, file, *args, env) -&gt; integer</span>

<span class="sd">Execute file (which is looked for along $PATH) with arguments from</span>
<span class="sd">args in a subprocess with the supplied environment.</span>
<span class="sd">If mode == P_NOWAIT return the pid of the process.</span>
<span class="sd">If mode == P_WAIT return the process&#39;s exit code if it exits normally;</span>
<span class="sd">otherwise return -SIG, where SIG is the signal that killed it. &quot;&quot;&quot;</span>
        <span class="n">env</span> <span class="o">=</span> <span class="n">args</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">spawnvpe</span><span class="p">(</span><span class="n">mode</span><span class="p">,</span> <span class="nb">file</span><span class="p">,</span> <span class="n">args</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="n">env</span><span class="p">)</span>


    <span class="n">__all__</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s">&quot;spawnlp&quot;</span><span class="p">,</span> <span class="s">&quot;spawnlpe&quot;</span><span class="p">])</span>


<span class="c"># Supply os.popen()</span>
<div class="viewcode-block" id="popen"><a class="viewcode-back" href="../os.html#os.popen">[docs]</a><span class="k">def</span> <span class="nf">popen</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s">&quot;r&quot;</span><span class="p">,</span> <span class="n">buffering</span><span class="o">=-</span><span class="mi">1</span><span class="p">):</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s">&quot;invalid cmd type (</span><span class="si">%s</span><span class="s">, expected string)&quot;</span> <span class="o">%</span> <span class="nb">type</span><span class="p">(</span><span class="n">cmd</span><span class="p">))</span>
    <span class="k">if</span> <span class="n">mode</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">(</span><span class="s">&quot;r&quot;</span><span class="p">,</span> <span class="s">&quot;w&quot;</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;invalid mode </span><span class="si">%r</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">mode</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">buffering</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">buffering</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;popen() does not support unbuffered streams&quot;</span><span class="p">)</span>
    <span class="kn">import</span> <span class="nn">subprocess</span><span class="o">,</span> <span class="nn">io</span>
    <span class="k">if</span> <span class="n">mode</span> <span class="o">==</span> <span class="s">&quot;r&quot;</span><span class="p">:</span>
        <span class="n">proc</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">Popen</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span>
                                <span class="n">shell</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
                                <span class="n">stdout</span><span class="o">=</span><span class="n">subprocess</span><span class="o">.</span><span class="n">PIPE</span><span class="p">,</span>
                                <span class="n">bufsize</span><span class="o">=</span><span class="n">buffering</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">_wrap_close</span><span class="p">(</span><span class="n">io</span><span class="o">.</span><span class="n">TextIOWrapper</span><span class="p">(</span><span class="n">proc</span><span class="o">.</span><span class="n">stdout</span><span class="p">),</span> <span class="n">proc</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">proc</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">Popen</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span>
                                <span class="n">shell</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
                                <span class="n">stdin</span><span class="o">=</span><span class="n">subprocess</span><span class="o">.</span><span class="n">PIPE</span><span class="p">,</span>
                                <span class="n">bufsize</span><span class="o">=</span><span class="n">buffering</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">_wrap_close</span><span class="p">(</span><span class="n">io</span><span class="o">.</span><span class="n">TextIOWrapper</span><span class="p">(</span><span class="n">proc</span><span class="o">.</span><span class="n">stdin</span><span class="p">),</span> <span class="n">proc</span><span class="p">)</span>

<span class="c"># Helper for popen() -- a proxy for a file whose close waits for the process</span></div>
<span class="k">class</span> <span class="nc">_wrap_close</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">stream</span><span class="p">,</span> <span class="n">proc</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_stream</span> <span class="o">=</span> <span class="n">stream</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_proc</span> <span class="o">=</span> <span class="n">proc</span>
    <span class="k">def</span> <span class="nf">close</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_stream</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
        <span class="n">returncode</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_proc</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">returncode</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">None</span>
        <span class="k">if</span> <span class="n">name</span> <span class="o">==</span> <span class="s">&#39;nt&#39;</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">returncode</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">returncode</span> <span class="o">&lt;&lt;</span> <span class="mi">8</span>  <span class="c"># Shift left to match old behavior</span>
    <span class="k">def</span> <span class="nf">__enter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span>
    <span class="k">def</span> <span class="nf">__exit__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
    <span class="k">def</span> <span class="nf">__getattr__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_stream</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">iter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_stream</span><span class="p">)</span>

<span class="c"># Supply os.fdopen()</span>
<div class="viewcode-block" id="fdopen"><a class="viewcode-back" href="../os.html#os.fdopen">[docs]</a><span class="k">def</span> <span class="nf">fdopen</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="nb">int</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s">&quot;invalid fd type (</span><span class="si">%s</span><span class="s">, expected integer)&quot;</span> <span class="o">%</span> <span class="nb">type</span><span class="p">(</span><span class="n">fd</span><span class="p">))</span>
    <span class="kn">import</span> <span class="nn">io</span>
    <span class="k">return</span> <span class="n">io</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span></div>
</pre></div>

          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2015, Weldon Henson.
    </p>
  </div>

  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
  
</footer>
        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../',
            VERSION:'1.0.0.',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true
        };
    </script>
      <script type="text/javascript" src="../_static/jquery.js"></script>
      <script type="text/javascript" src="../_static/underscore.js"></script>
      <script type="text/javascript" src="../_static/doctools.js"></script>
      <script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

  

  
  
    <script type="text/javascript" src="../_static/js/theme.js"></script>
  

  
  
  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.StickyNav.enable();
      });
  </script>
   

</body>
</html>