<div class="gmail_quote">On Tue, Sep 7, 2010 at 08:12, Nick Coghlan <span dir="ltr">&lt;<a href="mailto:ncoghlan@gmail.com">ncoghlan@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
On Tue, Sep 7, 2010 at 5:46 AM, brian.curtin &lt;<a href="mailto:python-checkins@python.org">python-checkins@python.org</a>&gt; wrote:<br>
&gt; Modified: python/branches/py3k/Lib/ntpath.py<br>
&gt; ==============================================================================<br>
&gt; --- python/branches/py3k/Lib/ntpath.py  (original)<br>
&gt; +++ python/branches/py3k/Lib/ntpath.py  Mon Sep  6 21:46:17 2010<br>
&gt; @@ -10,7 +10,6 @@<br>
&gt;  import stat<br>
&gt;  import genericpath<br>
&gt;  from genericpath import *<br>
&gt; -from nt import _getfileinformation<br>
&gt;<br>
&gt;  __all__ = [&quot;normcase&quot;,&quot;isabs&quot;,&quot;join&quot;,&quot;splitdrive&quot;,&quot;split&quot;,&quot;splitext&quot;,<br>
&gt;            &quot;basename&quot;,&quot;dirname&quot;,&quot;commonprefix&quot;,&quot;getsize&quot;,&quot;getmtime&quot;,<br>
&gt; @@ -656,4 +655,10 @@<br>
&gt;<br>
&gt;  def sameopenfile(f1, f2):<br>
&gt;     &quot;&quot;&quot;Test whether two file objects reference the same file&quot;&quot;&quot;<br>
&gt; -    return _getfileinformation(f1) == _getfileinformation(f2)<br>
&gt; +    try:<br>
&gt; +        from nt import _getfileinformation<br>
&gt; +        return _getfileinformation(f1) == _getfileinformation(f2)<br>
&gt; +    except ImportError:<br>
&gt; +        # On other operating systems, return True if the file descriptors<br>
&gt; +        # are the same.<br>
&gt; +        return f1 == f2<br>
<br>
Given the potential deadlock problems with imports inside functions,<br>
I&#39;d prefer to see this written as either:<br>
<br>
try:<br>
  from nt import _getfileinformation<br>
  def sameopenfile(f1, f2):<br>
    return _getfileinformation(f1) == _getfileinformation(f2)<br>
except ImportError:<br>
  # On other operating systems, return True if the file descriptors<br>
  # are the same.<br>
  def sameopenfile(f1, f2):<br>
    return f1 == f2<br>
sameopenfile.__doc__ =  &quot;Test whether two file objects reference the same file&quot;<br>
<br>
or as:<br>
<br>
try:<br>
  from nt import _getfileinformation<br>
except ImportError:<br>
  # On other operating systems, file comparison is by file descriptor anyway<br>
  # so a separate file information object is unnecessary<br>
  def _getfileinformation(f): return f<br>
<br>
def sameopenfile(f1, f2):<br>
  &quot;&quot;&quot;Test whether two file objects reference the same file&quot;&quot;&quot;<br>
  return _getfileinformation(f1) == _getfileinformation(f2)<br>
<br>
The latter is cleaner code, while the former is likely an unnecessary<br>
micro-optimisation.<br>
<br>
Cheers,<br>
Nick.</blockquote><div><br></div><div>Similar idea(s) would also apply to the function right above that, samefile. I&#39;ll create a new issue for cleaning this up.</div></div>