[Python-checkins] r84564 - in python/branches/py3k/Lib: ntpath.py test/test_ntpath.py

Nick Coghlan ncoghlan at gmail.com
Tue Sep 7 15:12:33 CEST 2010


On Tue, Sep 7, 2010 at 5:46 AM, brian.curtin <python-checkins at python.org> wrote:
> Modified: python/branches/py3k/Lib/ntpath.py
> ==============================================================================
> --- python/branches/py3k/Lib/ntpath.py  (original)
> +++ python/branches/py3k/Lib/ntpath.py  Mon Sep  6 21:46:17 2010
> @@ -10,7 +10,6 @@
>  import stat
>  import genericpath
>  from genericpath import *
> -from nt import _getfileinformation
>
>  __all__ = ["normcase","isabs","join","splitdrive","split","splitext",
>            "basename","dirname","commonprefix","getsize","getmtime",
> @@ -656,4 +655,10 @@
>
>  def sameopenfile(f1, f2):
>     """Test whether two file objects reference the same file"""
> -    return _getfileinformation(f1) == _getfileinformation(f2)
> +    try:
> +        from nt import _getfileinformation
> +        return _getfileinformation(f1) == _getfileinformation(f2)
> +    except ImportError:
> +        # On other operating systems, return True if the file descriptors
> +        # are the same.
> +        return f1 == f2

Given the potential deadlock problems with imports inside functions,
I'd prefer to see this written as either:

try:
  from nt import _getfileinformation
  def sameopenfile(f1, f2):
    return _getfileinformation(f1) == _getfileinformation(f2)
except ImportError:
  # On other operating systems, return True if the file descriptors
  # are the same.
  def sameopenfile(f1, f2):
    return f1 == f2
sameopenfile.__doc__ =  "Test whether two file objects reference the same file"

or as:

try:
  from nt import _getfileinformation
except ImportError:
  # On other operating systems, file comparison is by file descriptor anyway
  # so a separate file information object is unnecessary
  def _getfileinformation(f): return f

def sameopenfile(f1, f2):
  """Test whether two file objects reference the same file"""
  return _getfileinformation(f1) == _getfileinformation(f2)

The latter is cleaner code, while the former is likely an unnecessary
micro-optimisation.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-checkins mailing list