Index: Doc/library/os.path.rst =================================================================== --- Doc/library/os.path.rst (revision 83101) +++ Doc/library/os.path.rst (working copy) @@ -249,9 +249,11 @@ Return ``True`` if the file descriptors *fp1* and *fp2* refer to the same file. - Availability: Unix. + Availability: Unix, Windows. + .. versionchanged:: 3.2 Added Windows support. + .. function:: samestat(stat1, stat2) Return ``True`` if the stat tuples *stat1* and *stat2* refer to the same file. Index: Lib/ntpath.py =================================================================== --- Lib/ntpath.py (revision 83101) +++ Lib/ntpath.py (working copy) @@ -10,6 +10,7 @@ import stat import genericpath from genericpath import * +from nt import _getfileinformation __all__ = ["normcase","isabs","join","splitdrive","split","splitext", "basename","dirname","commonprefix","getsize","getmtime", @@ -17,7 +18,7 @@ "ismount", "expanduser","expandvars","normpath","abspath", "splitunc","curdir","pardir","sep","pathsep","defpath","altsep", "extsep","devnull","realpath","supports_unicode_filenames","relpath", - "samefile",] + "samefile", "sameopenfile",] # strings representing various path-related bits and pieces # These are primarily for export; internally, they are hardcoded. @@ -639,3 +640,7 @@ # Also, on other operating systems, fake this method with a # Windows-XP approximation. return abspath(f1) == abspath(f2) + +def sameopenfile(f1, f2): + """Test whether two file objects reference the same file""" + return _getfileinformation(f1) == _getfileinformation(f2) Index: Lib/test/test_ntpath.py =================================================================== --- Lib/test/test_ntpath.py (revision 83101) +++ Lib/test/test_ntpath.py (working copy) @@ -2,6 +2,7 @@ import os from test.support import TestFailed from test import support, test_genericpath +from tempfile import TemporaryFile import unittest @@ -233,8 +234,20 @@ tester('ntpath.relpath("/", "/")', '.') tester('ntpath.relpath("/a", "/a")', '.') tester('ntpath.relpath("/a/b", "/a/b")', '.') + + def test_sameopenfile(self): + with TemporaryFile() as tf1, TemporaryFile() as tf2: + # Make sure the same file is really the same + self.assertTrue(ntpath.sameopenfile(tf1.fileno(), tf1.fileno())) + # Make sure different files are really different + self.assertFalse(ntpath.sameopenfile(tf1.fileno(), tf2.fileno())) + # Make sure invalid values don't cause issues + with self.assertRaises(ValueError): + # Invalid file descriptors shouldn't display assert + # dialogs (#4804) + ntpath.sameopenfile(-1, -1) + - class NtCommonTest(test_genericpath.CommonTest): pathmodule = ntpath attributes = ['relpath', 'splitunc'] Index: Modules/posixmodule.c =================================================================== --- Modules/posixmodule.c (revision 83101) +++ Modules/posixmodule.c (working copy) @@ -2736,6 +2736,34 @@ return result; } /* end of posix__getfinalpathname */ + +static PyObject * +posix__getfileinformation(PyObject *self, PyObject *args) +{ + HANDLE hFile; + BY_HANDLE_FILE_INFORMATION info; + int fd; + + if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd)) + return NULL; + + if (!_PyVerify_fd(fd)) { + PyErr_SetString(PyExc_ValueError, + "received invalid file descriptor"); + return NULL; + } + + hFile = _get_osfhandle(fd); + if (hFile == INVALID_HANDLE_VALUE) + return win32_error("_getfileinformation", NULL); + + if (!GetFileInformationByHandle(hFile, &info)) + return win32_error("_getfileinformation", NULL); + + return Py_BuildValue("iii", info.dwVolumeSerialNumber, + info.nFileIndexHigh, + info.nFileIndexLow); +} #endif /* MS_WINDOWS */ PyDoc_STRVAR(posix_mkdir__doc__, @@ -7872,6 +7900,7 @@ #ifdef MS_WINDOWS {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL}, + {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL}, #endif #ifdef HAVE_GETLOADAVG {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},