diff --git a/compat.html b/compat.html index db1924be17594fec3b16e3f3fd3ac6b0cfb4f12e_Y29tcGF0Lmh0bWw=..097e4f70c8033953beada167aad6d0db8cd9aa98_Y29tcGF0Lmh0bWw= 100644 --- a/compat.html +++ b/compat.html @@ -116,6 +116,16 @@ <div class="syntax python"><pre><span></span><span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s2">"filename"</span><span class="p">,</span> <span class="s2">"w"</span><span class="p">)</span><br/><span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">"stuff"</span><span class="p">)</span><br/><span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span><br/></pre></div> <p>or using the <tt class="docutils literal">with</tt> keyword</p> <div class="syntax python"><pre><span></span><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s2">"filename"</span><span class="p">,</span> <span class="s2">"w"</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span><br/> <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">"stuff"</span><span class="p">)</span><br/></pre></div> +<p>The same problem–not closing your files–can also show up if your +program opens a large number of files without closing them explicitly. +In that case, you can easily hit the system limit on the number of file +descriptors that are allowed to be opened at the same time.</p> +<p>Since release 5.4, PyPy can be run with the command-line option <tt class="docutils literal"><span class="pre">-X</span> +<span class="pre">track-resources</span></tt> (as in, <tt class="docutils literal">pypy <span class="pre">-X</span> <span class="pre">track-resources</span> myprogram.py</tt>). +This produces a ResourceWarning when the GC closes a non-closed file or +socket. The traceback for the place where the file or socket was +allocated is given as well, which aids finding places where <tt class="docutils literal">close()</tt> +is missing.</p> <p>Similarly, remember that you must <tt class="docutils literal">close()</tt> a non-exhausted generator in order to have its pending <tt class="docutils literal">finally</tt> or <tt class="docutils literal">with</tt> clauses executed immediately:</p> diff --git a/source/compat.txt b/source/compat.txt index db1924be17594fec3b16e3f3fd3ac6b0cfb4f12e_c291cmNlL2NvbXBhdC50eHQ=..097e4f70c8033953beada167aad6d0db8cd9aa98_c291cmNlL2NvbXBhdC50eHQ= 100644 --- a/source/compat.txt +++ b/source/compat.txt @@ -86,6 +86,18 @@ with open("filename", "w") as f: f.write("stuff") +The same problem---not closing your files---can also show up if your +program opens a large number of files without closing them explicitly. +In that case, you can easily hit the system limit on the number of file +descriptors that are allowed to be opened at the same time. + +Since release 5.4, PyPy can be run with the command-line option ``-X +track-resources`` (as in, ``pypy -X track-resources myprogram.py``). +This produces a ResourceWarning when the GC closes a non-closed file or +socket. The traceback for the place where the file or socket was +allocated is given as well, which aids finding places where ``close()`` +is missing. + Similarly, remember that you must ``close()`` a non-exhausted generator in order to have its pending ``finally`` or ``with`` clauses executed immediately: