Index: Doc/library/os.rst =================================================================== --- Doc/library/os.rst (revision 85106) +++ Doc/library/os.rst (working copy) @@ -1062,9 +1062,12 @@ Create a hard link pointing to *source* named *link_name*. - Availability: Unix. + Availability: Unix, Windows. + .. versionchanged:: 3.2 + Added Windows support. + .. function:: listdir(path='.') Return a list containing the names of the entries in the directory given by Index: Lib/test/test_os.py =================================================================== --- Lib/test/test_os.py (revision 85106) +++ Lib/test/test_os.py (working copy) @@ -853,6 +853,33 @@ if hasattr(os, "write"): self.check(os.write, b" ") + +class LinkTests(unittest.TestCase): + def setUp(self): + self.file1 = support.TESTFN + self.file2 = os.path.join(support.TESTFN + "2") + + for file in (self.file1, self.file2): + if os.path.exists(file): + os.unlink(file) + + tearDown = setUp + + def _test_link(self, file1, file2): + with open(file1, "w") as f1: + f1.write("test") + + os.link(file1, file2) + with open(file1, "r") as f1, open(file2, "r") as f2: + self.assertTrue(os.path.sameopenfile(f1.fileno(), f2.fileno())) + + def test_link(self): + self._test_link(self.file1, self.file2) + + def test_link_bytes(self): + self._test_link(bytes(self.file1, sys.getfilesystemencoding()), + bytes(self.file2, sys.getfilesystemencoding())) + if sys.platform != 'win32': class Win32ErrorTests(unittest.TestCase): pass @@ -1231,6 +1258,7 @@ FSEncodingTests, PidTests, LoginTests, + LinkTests, ) if __name__ == "__main__": Index: Modules/posixmodule.c =================================================================== --- Modules/posixmodule.c (revision 85106) +++ Modules/posixmodule.c (working copy) @@ -2247,7 +2247,37 @@ } #endif /* HAVE_LINK */ +#ifdef MS_WINDOWS +PyDoc_STRVAR(win32_link__doc__, +"link(src, dst)\n\n\ +Create a hard link to a file."); +static PyObject * +win32_link(PyObject *self, PyObject *args) +{ + PyObject *osrc, *odst; + char *src, *dst; + BOOL rslt; + + if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc, + PyUnicode_FSConverter, &odst)) + return NULL; + + src = PyBytes_AsString(osrc); + dst = PyBytes_AsString(odst); + + Py_BEGIN_ALLOW_THREADS + rslt = CreateHardLink(dst, src, NULL); + Py_END_ALLOW_THREADS + + if (rslt == 0) + return posix_error(); + + Py_RETURN_NONE; +} +#endif /* MS_WINDOWS */ + + PyDoc_STRVAR(posix_listdir__doc__, "listdir([path]) -> list_of_strings\n\n\ Return a list containing the names of the entries in the directory.\n\ @@ -7796,6 +7826,7 @@ #ifdef MS_WINDOWS {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, {"kill", win32_kill, METH_VARARGS, win32_kill__doc__}, + {"link", win32_link, METH_VARARGS, win32_link__doc__}, #endif #ifdef HAVE_SETUID {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},