[Zodb-checkins] SVN: ZODB/branches/jim-zeo-blob/src/ZODB/ Updated winlock to use exception classes and created basic tests.

Jim Fulton jim at zope.com
Wed May 16 11:11:49 EDT 2007


Log message for revision 75806:
  Updated winlock to use exception classes and created basic tests.

Changed:
  A   ZODB/branches/jim-zeo-blob/src/ZODB/tests/testwinlock.py
  U   ZODB/branches/jim-zeo-blob/src/ZODB/winlock.c
  A   ZODB/branches/jim-zeo-blob/src/ZODB/winlock.txt

-=-
Added: ZODB/branches/jim-zeo-blob/src/ZODB/tests/testwinlock.py
===================================================================
--- ZODB/branches/jim-zeo-blob/src/ZODB/tests/testwinlock.py	                        (rev 0)
+++ ZODB/branches/jim-zeo-blob/src/ZODB/tests/testwinlock.py	2007-05-16 15:11:49 UTC (rev 75806)
@@ -0,0 +1,22 @@
+##############################################################################
+#
+# Copyright (c) 2004 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.
+#
+##############################################################################
+import os, sys, unittest
+from zope.testing import doctest
+
+def test_suite():
+    if sys.platform == 'win32':
+        return doctest.DocFileSuite(os.path.join('..', 'winlock.txt'))
+    else:
+        return unittest.TestSuite()
+

Modified: ZODB/branches/jim-zeo-blob/src/ZODB/winlock.c
===================================================================
--- ZODB/branches/jim-zeo-blob/src/ZODB/winlock.c	2007-05-16 14:47:28 UTC (rev 75805)
+++ ZODB/branches/jim-zeo-blob/src/ZODB/winlock.c	2007-05-16 15:11:49 UTC (rev 75806)
@@ -1,6 +1,6 @@
 /*****************************************************************************
 
-  Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+  Copyright (c) Zope Corporation and Contributors.
   All Rights Reserved.
 
   This software is subject to the provisions of the Zope Public License,
@@ -18,7 +18,7 @@
 
 #include "Python.h"
 
-static PyObject *Error;
+static PyObject *Error, *LockError;
 
 #ifdef MS_WIN32
 
@@ -31,49 +31,49 @@
 static PyObject *
 common(LOCK_FUNC func, PyObject *args)
 {
-	int fileno;
-	long h, ofslo, ofshi, lenlo, lenhi;
+  int fileno;
+  long h, ofslo=0, ofshi=0, lenlo=1, lenhi=0;
 
-	if (! PyArg_ParseTuple(args, "illll", &fileno,
-			       &ofslo, &ofshi,
-			       &lenlo, &lenhi))
-		return NULL;
+  if (! PyArg_ParseTuple(args, "i|llll", &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;
+  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(LockError, PyInt_FromLong(GetLastError()));
+  return NULL;
 }
 
 static PyObject *
 winlock(PyObject *ignored, PyObject *args)
 {
-	return common(LockFile, args);
+  return common(LockFile, args);
 }
 
 static PyObject *
 winunlock(PyObject *ignored, PyObject *args)
 {
-	return common(UnlockFile, args);
+  return common(UnlockFile, args);
 }
 
 static struct PyMethodDef methods[] = {
-    {"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 */
+  {"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
 
@@ -91,15 +91,18 @@
 DL_EXPORT(void)
 initwinlock(void)
 {
-	PyObject *m, *d;
+  PyObject *m, *d;
 
-	if (!(Error=PyString_FromString("winlock.error")))
-		return;
+  if (!(Error=PyErr_NewException("ZODB.winlock.Error", NULL, NULL)))
+    return;
+  if (!(LockError=PyErr_NewException("ZODB.winlock.LockError", NULL, NULL)))
+    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);
+  PyDict_SetItemString(d, "LockError", LockError);
 }

Added: ZODB/branches/jim-zeo-blob/src/ZODB/winlock.txt
===================================================================
--- ZODB/branches/jim-zeo-blob/src/ZODB/winlock.txt	                        (rev 0)
+++ ZODB/branches/jim-zeo-blob/src/ZODB/winlock.txt	2007-05-16 15:11:49 UTC (rev 75806)
@@ -0,0 +1,29 @@
+Windows File Locks
+==================
+
+The winlock module provids support for locking existing files.
+
+    >>> f = open('lock', 'w')
+
+    >>> import ZODB.winlock
+    >>> ZODB.winlock.LockFile(f.fileno())
+
+If we try to lock a file more than once, we'll get a lock error:
+
+    >>> try:
+    ...     ZODB.winlock.LockFile(f.fileno())
+    ... except ZODB.winlock.LockError:
+    ...     print "Can't lock file"
+    Can't lock file
+
+We use UnlockFile to remove the lock:
+
+    >>> ZODB.winlock.UnlockFile(f.fileno())
+    >>> ZODB.winlock.LockFile(f.fileno())
+    >>> ZODB.winlock.UnlockFile(f.fileno())
+
+It is up to applications to create and remove the lock file:
+
+    >>> f.close()
+    >>> import os
+    >>> os.remove('lock')



More information about the Zodb-checkins mailing list