[Python-Dev] [Python-checkins] r84564 - in python/branches/py3k/Lib: ntpath.py test/test_ntpath.py (original) (raw)

Brian Curtin brian.curtin at gmail.com
Tue Sep 7 15:20:21 CEST 2010


On Tue, Sep 7, 2010 at 08:12, Nick Coghlan <ncoghlan at gmail.com> wrote:

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.

Similar idea(s) would also apply to the function right above that, samefile. I'll create a new issue for cleaning this up. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20100907/1b2fef49/attachment.html>



More information about the Python-Dev mailing list