Commit 6e62a0b8 authored by Tim Peters's avatar Tim Peters

Backporting from Zope3/ZODB4: exposed the Win32 UnlockFile function,

which is used by Barry's new LockFile class.  The effect of leaving
regions of a file locked when closing the file, or when exiting the
process, is undefined on Windows, and the new scheme restricts itself
to operations with defined semantics.
parent 7d4716e8
...@@ -2,19 +2,19 @@ ...@@ -2,19 +2,19 @@
Copyright (c) 2001, 2002 Zope Corporation and Contributors. Copyright (c) 2001, 2002 Zope Corporation and Contributors.
All Rights Reserved. All Rights Reserved.
This software is subject to the provisions of the Zope Public License, This software is subject to the provisions of the Zope Public License,
Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution. Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
FOR A PARTICULAR PURPOSE FOR A PARTICULAR PURPOSE
****************************************************************************/ ****************************************************************************/
static char winlock_doc_string[] = static char winlock_doc_string[] =
"Lock files on Windows." "Lock files on Windows."
"\n" "\n"
"$Id: winlock.c,v 1.8 2002/02/11 23:40:42 gvanrossum Exp $\n"; "$Id: winlock.c,v 1.9 2003/02/28 20:37:59 tim_one Exp $\n";
#include "Python.h" #include "Python.h"
...@@ -25,32 +25,55 @@ static PyObject *Error; ...@@ -25,32 +25,55 @@ static PyObject *Error;
#include <windows.h> #include <windows.h>
#include <io.h> #include <io.h>
static PyObject * /* LOCK_FUNC is the shared type of Win32 LockFile and UnlockFile. */
typedef WINBASEAPI BOOL WINAPI LOCK_FUNC(HANDLE, DWORD, DWORD, DWORD, DWORD);
static PyObject *
common(LOCK_FUNC func, PyObject *args)
{
int fileno;
long h, ofslo, ofshi, lenlo, lenhi;
if (! PyArg_ParseTuple(args, "illll", &fileno,
&ofslo, &ofshi,
&lenlo, &lenhi))
return NULL;
h = _get_osfhandle(fileno);
if (h == -1) {
PyErr_SetString(Error, "_get_osfhandle failed");
return NULL;
}
if (func((HANDLE)h, ofslo, ofshi, lenlo, lenhi)) {
Py_INCREF(Py_None);
return Py_None;
}
PyErr_SetObject(Error, PyInt_FromLong(GetLastError()));
return NULL;
}
static PyObject *
winlock(PyObject *ignored, PyObject *args) winlock(PyObject *ignored, PyObject *args)
{ {
int fileno; return common(LockFile, args);
long h, ol, oh, ll, lh; }
if (! PyArg_ParseTuple(args, "illll", &fileno, &ol, &oh, &ll, &lh)) static PyObject *
return NULL; winunlock(PyObject *ignored, PyObject *args)
{
if ((h=_get_osfhandle(fileno))==-1) { return common(UnlockFile, args);
PyErr_SetString(Error, "_get_osfhandle failed");
return NULL;
}
if (LockFile((HANDLE)h, ol, oh, ll, lh)) {
Py_INCREF(Py_None);
return Py_None;
}
PyErr_SetObject(Error, PyInt_FromLong(GetLastError()));
return NULL;
} }
static struct PyMethodDef methods[] = { static struct PyMethodDef methods[] = {
{"LockFile", (PyCFunction)winlock, 1, {"LockFile", (PyCFunction)winlock, METH_VARARGS,
"LockFile(fileno, offsetLow, offsetHigh, lengthLow, lengthHigh ) -- " "LockFile(fileno, offsetLow, offsetHigh, lengthLow, lengthHigh) -- "
"Lock the file associated with fileno"}, "Lock the file associated with fileno"},
{NULL, NULL} /* sentinel */
{"UnlockFile", (PyCFunction)winunlock, METH_VARARGS,
"UnlockFile(fileno, offsetLow, offsetHigh, lengthLow, lengthHigh) -- "
"Unlock the file associated with fileno"},
{NULL, NULL} /* sentinel */
}; };
#else #else
...@@ -66,16 +89,17 @@ static struct PyMethodDef methods[] = { ...@@ -66,16 +89,17 @@ static struct PyMethodDef methods[] = {
#define DL_EXPORT(RTYPE) RTYPE #define DL_EXPORT(RTYPE) RTYPE
#endif #endif
DL_EXPORT(void) DL_EXPORT(void)
initwinlock(void) { initwinlock(void)
PyObject *m, *d; {
PyObject *m, *d;
if (!(Error=PyString_FromString("winlock.error"))) if (!(Error=PyString_FromString("winlock.error")))
return; return;
/* Create the module and add the functions */ /* Create the module and add the functions */
m = Py_InitModule4("winlock", methods, winlock_doc_string, (PyObject*)NULL, m = Py_InitModule4("winlock", methods, winlock_doc_string,
PYTHON_API_VERSION); (PyObject*)NULL, PYTHON_API_VERSION);
d = PyModule_GetDict(m); d = PyModule_GetDict(m);
PyDict_SetItemString(d, "error", Error); PyDict_SetItemString(d, "error", Error);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment