<div class="gmail_quote">On Tue, Sep 7, 2010 at 08:12, Nick Coghlan <span dir="ltr"><<a href="mailto:ncoghlan@gmail.com">ncoghlan@gmail.com</a>></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 <<a href="mailto:python-checkins@python.org">python-checkins@python.org</a>> wrote:<br>
> Modified: python/branches/py3k/Lib/ntpath.py<br>
> ==============================================================================<br>
> --- python/branches/py3k/Lib/ntpath.py (original)<br>
> +++ python/branches/py3k/Lib/ntpath.py Mon Sep 6 21:46:17 2010<br>
> @@ -10,7 +10,6 @@<br>
> import stat<br>
> import genericpath<br>
> from genericpath import *<br>
> -from nt import _getfileinformation<br>
><br>
> __all__ = ["normcase","isabs","join","splitdrive","split","splitext",<br>
> "basename","dirname","commonprefix","getsize","getmtime",<br>
> @@ -656,4 +655,10 @@<br>
><br>
> def sameopenfile(f1, f2):<br>
> """Test whether two file objects reference the same file"""<br>
> - return _getfileinformation(f1) == _getfileinformation(f2)<br>
> + try:<br>
> + from nt import _getfileinformation<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>
> + return f1 == f2<br>
<br>
Given the potential deadlock problems with imports inside functions,<br>
I'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__ = "Test whether two file objects reference the same file"<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>
"""Test whether two file objects reference the same file"""<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'll create a new issue for cleaning this up.</div></div>