Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 11b2306

Browse files
committed
Enable os.fsync() for Windows, mapping it to MS's _commit() there. The
docs here are best-guess: the MS docs I could find weren't clear, and some even claimed _commit() has no effect on Win32 systems (which is easily shown to be false just by trying it).
1 parent 9a9c436 commit 11b2306

3 files changed

Lines changed: 29 additions & 18 deletions

File tree

Doc/lib/libos.tex

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,13 @@ \subsection{File Descriptor Operations \label{os-fd-ops}}
449449

450450
\begin{funcdesc}{fsync}{fd}
451451
Force write of file with filedescriptor \var{fd} to disk.
452-
Availability: \UNIX.
452+
453+
On Windows this calls the MS \cfunction{_commit()} function. If you're
454+
starting with a Python file object \var{f}, first do
455+
\code{\var{f}.flush()}, and then do \code{os.fsync(\var{f}.fileno()},
456+
to ensure that all internal buffers associated with \var{f} are written
457+
to disk.
458+
Availability: \UNIX, and Windows starting in 2.3.
453459
\end{funcdesc}
454460

455461
\begin{funcdesc}{ftruncate}{fd, length}
@@ -921,7 +927,7 @@ \subsection{Files and Directories \label{os-file-dir}}
921927
\member{st_atime},
922928
\member{st_mtime},
923929
\member{st_ctime}.
924-
More items may be added at the end by some implementations.
930+
More items may be added at the end by some implementations.
925931
The standard module \refmodule{stat}\refstmodindex{stat} defines
926932
functions and constants that are useful for extracting information
927933
from a \ctype{stat} structure.

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ Tests
232232
Windows
233233
-------
234234

235+
- os.fsync() now exists on Windows, and calls the Microsoft _commit()
236+
function.
237+
235238
- New function winsound.MessageBeep() wraps the Win32 API
236239
MessageBeep().
237240

Modules/posixmodule.c

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ corresponding Unix manual entries for more information on calls.");
112112
#define HAVE_POPEN 1
113113
#define HAVE_SYSTEM 1
114114
#define HAVE_CWAIT 1
115+
#define HAVE_FSYNC 1
116+
#define fsync _commit
115117
#else
116118
#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
117119
/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
@@ -301,7 +303,7 @@ extern int lstat(const char *, struct stat *);
301303
# define STRUCT_STAT struct stat
302304
#endif
303305

304-
#if defined(MAJOR_IN_MKDEV)
306+
#if defined(MAJOR_IN_MKDEV)
305307
#include <sys/mkdev.h>
306308
#else
307309
#if defined(MAJOR_IN_SYSMACROS)
@@ -325,7 +327,7 @@ extern char **environ;
325327

326328
#if defined(__VMS)
327329
/* add some values to provide a similar environment like POSIX */
328-
static
330+
static
329331
void
330332
vms_add_posix_env(PyObject *d)
331333
{
@@ -516,8 +518,8 @@ static PyObject *_PyUnicode_FromFileSystemEncodedObject(register PyObject *obj)
516518
return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj),
517519
PyUnicode_GET_SIZE(obj));
518520
}
519-
return PyUnicode_FromEncodedObject(obj,
520-
Py_FileSystemDefaultEncoding,
521+
return PyUnicode_FromEncodedObject(obj,
522+
Py_FileSystemDefaultEncoding,
521523
"strict");
522524
}
523525

@@ -621,19 +623,19 @@ posix_fildes(PyObject *fdobj, int (*func)(int))
621623
}
622624

623625
#ifdef Py_WIN_WIDE_FILENAMES
624-
static int
626+
static int
625627
unicode_file_names(void)
626628
{
627629
static int canusewide = -1;
628630
if (canusewide == -1) {
629-
/* As per doc for ::GetVersion(), this is the correct test for
631+
/* As per doc for ::GetVersion(), this is the correct test for
630632
the Windows NT family. */
631633
canusewide = (GetVersion() < 0x80000000) ? 1 : 0;
632634
}
633635
return canusewide;
634636
}
635637
#endif
636-
638+
637639
static PyObject *
638640
posix_1str(PyObject *args, char *format, int (*func)(const char*),
639641
char *wformat, int (*wfunc)(const Py_UNICODE*))
@@ -678,7 +680,7 @@ posix_1str(PyObject *args, char *format, int (*func)(const char*),
678680
}
679681

680682
static PyObject *
681-
posix_2str(PyObject *args,
683+
posix_2str(PyObject *args,
682684
char *format,
683685
int (*func)(const char *, const char *),
684686
char *wformat,
@@ -971,7 +973,7 @@ _pystat_fromstructstat(STRUCT_STAT st)
971973
}
972974

973975
static PyObject *
974-
posix_do_stat(PyObject *self, PyObject *args,
976+
posix_do_stat(PyObject *self, PyObject *args,
975977
char *format,
976978
#ifdef __VMS
977979
int (*statfunc)(const char *, STRUCT_STAT *, ...),
@@ -1181,7 +1183,7 @@ posix_chdir(PyObject *self, PyObject *args)
11811183
#elif defined(PYOS_OS2) && defined(PYCC_GCC)
11821184
return posix_1str(args, "et:chdir", _chdir2, NULL, NULL);
11831185
#elif defined(__VMS)
1184-
return posix_1str(args, "et:chdir", (int (*)(const char *))chdir,
1186+
return posix_1str(args, "et:chdir", (int (*)(const char *))chdir,
11851187
NULL, NULL);
11861188
#else
11871189
return posix_1str(args, "et:chdir", chdir, NULL, NULL);
@@ -1314,7 +1316,7 @@ posix_lchown(PyObject *self, PyObject *args)
13141316
Py_BEGIN_ALLOW_THREADS
13151317
res = lchown(path, (uid_t) uid, (gid_t) gid);
13161318
Py_END_ALLOW_THREADS
1317-
if (res < 0)
1319+
if (res < 0)
13181320
return posix_error_with_allocated_filename(path);
13191321
PyMem_Free(path);
13201322
Py_INCREF(Py_None);
@@ -1646,7 +1648,7 @@ posix_listdir(PyObject *self, PyObject *args)
16461648
PyObject *w;
16471649

16481650
w = PyUnicode_FromEncodedObject(v,
1649-
Py_FileSystemDefaultEncoding,
1651+
Py_FileSystemDefaultEncoding,
16501652
"strict");
16511653
if (w != NULL) {
16521654
Py_DECREF(v);
@@ -1692,7 +1694,7 @@ posix__getfullpathname(PyObject *self, PyObject *args)
16921694
if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
16931695
Py_UNICODE woutbuf[MAX_PATH*2];
16941696
Py_UNICODE *wtemp;
1695-
if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
1697+
if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po),
16961698
sizeof(woutbuf)/sizeof(woutbuf[0]),
16971699
woutbuf, &wtemp))
16981700
return win32_error("GetFullPathName", "");
@@ -3185,7 +3187,7 @@ os2emx_popen3(PyObject *self, PyObject *args)
31853187
/*
31863188
* Variation on os2emx.popen2
31873189
*
3188-
* The result of this function is 2 pipes - the processes stdin,
3190+
* The result of this function is 2 pipes - the processes stdin,
31893191
* and stdout+stderr combined as a single pipe.
31903192
*/
31913193

@@ -3519,7 +3521,7 @@ _PyPopen(char *cmdstring, int mode, int n, int bufsize)
35193521
}
35203522
}
35213523
}
3522-
3524+
35233525
/*
35243526
* Clean up our localized references for the dictionary keys
35253527
* and value since PyDict_SetItem will Py_INCREF any copies
@@ -3936,7 +3938,7 @@ _PyPopenCreateProcess(char *cmdstring,
39363938
s3,
39373939
cmdstring);
39383940
/* Not passing CREATE_NEW_CONSOLE has been known to
3939-
cause random failures on win9x. Specifically a
3941+
cause random failures on win9x. Specifically a
39403942
dialog:
39413943
"Your program accessed mem currently in use at xxx"
39423944
and a hopeful warning about the stability of your

0 commit comments

Comments
 (0)