[Zope3-checkins] CVS: Zope3/src/zodb - winlock.c:1.3

Tim Peters tim.one@comcast.net
Thu, 27 Feb 2003 16:27:20 -0500


Update of /cvs-repository/Zope3/src/zodb
In directory cvs.zope.org:/tmp/cvs-serv25405/src/zodb

Modified Files:
	winlock.c 
Log Message:
This only exposed the Win32 LockFile() function.  Added the Win32
UnlockFile() function too.  On Windows, closing a locked file doesn't
unlock the file -- the lock persists at least as long as the locking
process exists.  This doesn't appear to have mattered so far, but
Python 2.3 for Windows ships with a usable Berkeley database
implementation, and Zope3 under Python 2.3 on Windows suffers > 50
errors running the Berkeley tests because the .lock file never gets
unlocked.  Exposing UnlockFile() is a necessary step toward repairing
this.


=== Zope3/src/zodb/winlock.c 1.2 => 1.3 ===
--- Zope3/src/zodb/winlock.c:1.2	Wed Dec 25 09:12:16 2002
+++ Zope3/src/zodb/winlock.c	Thu Feb 27 16:27:18 2003
@@ -2,16 +2,16 @@
 
   Copyright (c) 2001, 2002 Zope Corporation and Contributors.
   All Rights Reserved.
-  
+
   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.
   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
   WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
   FOR A PARTICULAR PURPOSE
-  
+
  ****************************************************************************/
-static char winlock_doc_string[] = 
+static char winlock_doc_string[] =
 "Lock files on Windows."
 "\n"
 "$Id$\n";
@@ -25,32 +25,55 @@
 #include <windows.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)
 {
-  int fileno;
-  long h, ol, oh, ll, lh;
-  
-  if (! PyArg_ParseTuple(args, "illll", &fileno, &ol, &oh, &ll, &lh))
-    return NULL;
-
-  if ((h=_get_osfhandle(fileno))==-1) {
-    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;
+	return common(LockFile, args);
+}
+
+static PyObject *
+winunlock(PyObject *ignored, PyObject *args)
+{
+	return common(UnlockFile, args);
 }
 
 static struct PyMethodDef methods[] = {
-  {"LockFile",	(PyCFunction)winlock,	1,
-   "LockFile(fileno, offsetLow, offsetHigh, lengthLow, lengthHigh ) -- "
-   "Lock the file associated with fileno"},
-  {NULL,		NULL}		/* sentinel */
+    {"LockFile",	(PyCFunction)winlock,	METH_VARARGS,
+     "LockFile(fileno, offsetLow, offsetHigh, lengthLow, lengthHigh) -- "
+     "Lock the file associated with fileno"},
+
+    {"UnlockFile",	(PyCFunction)winunlock,	METH_VARARGS,
+     "UnlockFile(fileno, offsetLow, offsetHigh, lengthLow, lengthHigh) -- "
+     "Unlock the file associated with fileno"},
+
+    {NULL,		NULL}		/* sentinel */
 };
 #else
 
@@ -66,16 +89,17 @@
 #define DL_EXPORT(RTYPE) RTYPE
 #endif
 DL_EXPORT(void)
-initwinlock(void) {
-  PyObject *m, *d;
+initwinlock(void)
+{
+	PyObject *m, *d;
 
-  if (!(Error=PyString_FromString("winlock.error"))) 
-      return;
+	if (!(Error=PyString_FromString("winlock.error")))
+		return;
 
-  /* Create the module and add the functions */
-  m = Py_InitModule4("winlock", methods, winlock_doc_string, (PyObject*)NULL,
-		     PYTHON_API_VERSION);
+	/* Create the module and add the functions */
+	m = Py_InitModule4("winlock", methods, winlock_doc_string,
+			   (PyObject*)NULL, PYTHON_API_VERSION);
 
-  d = PyModule_GetDict(m);
-  PyDict_SetItemString(d, "error", Error);
+	d = PyModule_GetDict(m);
+	PyDict_SetItemString(d, "error", Error);
 }