From 77eecfa94df1e23e19599de7349cf60845a01242 Mon Sep 17 00:00:00 2001
From: Guido van Rossum <guido@python.org>
Date: Thu, 17 Jul 1997 22:56:01 +0000
Subject: Patches by AMK to check that the db is still open.

---
 Modules/bsddbmodule.c | 20 ++++++++++++++++++++
 Modules/dbmmodule.c   | 14 ++++++++++++++
 Modules/gdbmmodule.c  | 20 ++++++++++++++++++++
 3 files changed, 54 insertions(+)

diff --git a/Modules/bsddbmodule.c b/Modules/bsddbmodule.c
index 203180f..0c9915a 100644
--- a/Modules/bsddbmodule.c
+++ b/Modules/bsddbmodule.c
@@ -58,6 +58,9 @@ typedef struct {
 staticforward PyTypeObject Bsddbtype;
 
 #define is_bsddbobject(v) ((v)->ob_type == &Bsddbtype)
+#define check_bsddbobject_open(v) if ((v)->di_bsddb == NULL) \
+               { PyErr_SetString(BsddbError, "BSDDB object has already been closed"); \
+                 return NULL; }
 
 static PyObject *BsddbError;
 
@@ -206,6 +209,10 @@ static int
 bsddb_length(dp)
 	bsddbobject *dp;
 {
+        if (dp->di_bsddb == NULL) {
+                 PyErr_SetString(BsddbError, "BSDDB object has already been closed"); 
+                 return -1; 
+        }
 	if (dp->di_size < 0) {
 		DBT krec, drec;
 		int status;
@@ -237,6 +244,8 @@ bsddb_subscript(dp, key)
 
 	if (!PyArg_Parse(key, "s#", &data, &size))
 		return NULL;
+        check_bsddbobject_open(dp);
+	
 	krec.data = data;
 	krec.size = size;
 
@@ -267,6 +276,10 @@ bsddb_ass_sub(dp, key, value)
 				"bsddb key type must be string");
 		return -1;
 	}
+        if (dp->di_bsddb == NULL) {
+                 PyErr_SetString(BsddbError, "BSDDB object has already been closed"); 
+                 return -1; 
+        }
 	krec.data = data;
 	krec.size = size;
 	dp->di_size = -1;
@@ -341,6 +354,7 @@ bsddb_keys(dp, args)
 
 	if (!PyArg_NoArgs(args))
 		return NULL;
+	check_bsddbobject_open(dp);
 	list = PyList_New(0);
 	if (list == NULL)
 		return NULL;
@@ -383,6 +397,7 @@ bsddb_has_key(dp, args)
 
 	if (!PyArg_Parse(args, "s#", &data, &size))
 		return NULL;
+	check_bsddbobject_open(dp);
 	krec.data = data;
 	krec.size = size;
 
@@ -407,6 +422,7 @@ bsddb_set_location(dp, key)
 
 	if (!PyArg_Parse(key, "s#", &data, &size))
 		return NULL;
+	check_bsddbobject_open(dp);
 	krec.data = data;
 	krec.size = size;
 
@@ -435,6 +451,7 @@ bsddb_seq(dp, args, sequence_request)
 	if (!PyArg_NoArgs(args))
 		return NULL;
 
+	check_bsddbobject_open(dp);
 	krec.data = 0;
 	krec.size = 0;
 
@@ -487,6 +504,9 @@ bsddb_sync(dp, args)
 {
 	int status;
 
+	if (!PyArg_NoArgs(args))
+		return NULL;
+	check_bsddbobject_open(dp);
 	status = (dp->di_bsddb->sync)(dp->di_bsddb, 0);
 	if (status != 0) {
 		PyErr_SetFromErrno(BsddbError);
diff --git a/Modules/dbmmodule.c b/Modules/dbmmodule.c
index 4c82a9f..0b0cb70 100644
--- a/Modules/dbmmodule.c
+++ b/Modules/dbmmodule.c
@@ -48,6 +48,9 @@ typedef struct {
 staticforward PyTypeObject Dbmtype;
 
 #define is_dbmobject(v) ((v)->ob_type == &Dbmtype)
+#define check_dbmobject_open(v) if ((v)->di_dbm == NULL) \
+               { PyErr_SetString(DbmError, "DBM object has already been closed"); \
+                 return NULL; }
 
 static PyObject *DbmError;
 
@@ -86,6 +89,10 @@ static int
 dbm_length(dp)
 	dbmobject *dp;
 {
+        if (dp->di_dbm == NULL) {
+                 PyErr_SetString(DbmError, "DBM object has already been closed"); 
+                 return -1; 
+        }
         if ( dp->di_size < 0 ) {
 		datum key;
 		int size;
@@ -109,6 +116,7 @@ register PyObject *key;
 	if (!PyArg_Parse(key, "s#", &krec.dptr, &krec.dsize) )
 		return NULL;
 	
+        check_dbmobject_open(dp);
 	drec = dbm_fetch(dp->di_dbm, krec);
 	if ( drec.dptr == 0 ) {
 		PyErr_SetString(PyExc_KeyError,
@@ -135,6 +143,10 @@ PyObject *v, *w;
 				"dbm mappings have string indices only");
 		return -1;
 	}
+        if (dp->di_dbm == NULL) {
+                 PyErr_SetString(DbmError, "DBM object has already been closed"); 
+                 return -1;
+        }
 	dp->di_size = -1;
 	if (w == NULL) {
 		if ( dbm_delete(dp->di_dbm, krec) < 0 ) {
@@ -195,6 +207,7 @@ PyObject *args;
 
 	if (!PyArg_NoArgs(args))
 		return NULL;
+        check_dbmobject_open(dp);
 	v = PyList_New(0);
 	if (v == NULL)
 		return NULL;
@@ -224,6 +237,7 @@ PyObject *args;
 	
 	if (!PyArg_Parse(args, "s#", &key.dptr, &key.dsize))
 		return NULL;
+        check_dbmobject_open(dp);
 	val = dbm_fetch(dp->di_dbm, key);
 	return PyInt_FromLong(val.dptr != NULL);
 }
diff --git a/Modules/gdbmmodule.c b/Modules/gdbmmodule.c
index 6cace28..23ec831 100644
--- a/Modules/gdbmmodule.c
+++ b/Modules/gdbmmodule.c
@@ -57,6 +57,11 @@ typedef struct {
 staticforward PyTypeObject Dbmtype;
 
 #define is_dbmobject(v) ((v)->ob_type == &Dbmtype)
+#define check_dbmobject_open(v) if ((v)->di_dbm == NULL) \
+               { PyErr_SetString(DbmError, "GDBM object has already been closed"); \
+                 return NULL; }
+
+
 
 static PyObject *DbmError;
 
@@ -100,6 +105,10 @@ static int
 dbm_length(dp)
 	dbmobject *dp;
 {
+        if (dp->di_dbm == NULL) {
+                 PyErr_SetString(DbmError, "GDBM object has already been closed"); 
+                 return -1; 
+        }
         if ( dp->di_size < 0 ) {
 		datum key,okey;
 		int size;
@@ -151,6 +160,10 @@ PyObject *v, *w;
 				"gdbm mappings have string indices only");
 		return -1;
 	}
+        if (dp->di_dbm == NULL) {
+                 PyErr_SetString(DbmError, "GDBM object has already been closed"); 
+                 return -1; 
+        }
 	dp->di_size = -1;
 	if (w == NULL) {
 		if ( gdbm_delete(dp->di_dbm, krec) < 0 ) {
@@ -214,6 +227,8 @@ PyObject *args;
 	if (!PyArg_NoArgs(args))
 		return NULL;
 
+	check_dbmobject_open(dp);
+
 	v = PyList_New(0);
 	if (v == NULL)
 		return NULL;
@@ -250,6 +265,7 @@ PyObject *args;
 	
 	if (!PyArg_Parse(args, "s#", &key.dptr, &key.dsize))
 		return NULL;
+	check_dbmobject_open(dp);
 	return PyInt_FromLong((long) gdbm_exists(dp->di_dbm, key));
 }
 
@@ -263,6 +279,7 @@ PyObject *args;
 
 	if (!PyArg_NoArgs(args))
 		return NULL;
+	check_dbmobject_open(dp);
 	key = gdbm_firstkey(dp->di_dbm);
 	if (key.dptr) {
 		v = PyString_FromStringAndSize(key.dptr, key.dsize);
@@ -284,6 +301,7 @@ PyObject *args;
 
 	if (!PyArg_Parse(args, "s#", &key.dptr, &key.dsize))
 		return NULL;
+	check_dbmobject_open(dp);
 	nextkey = gdbm_nextkey(dp->di_dbm, key);
 	if (nextkey.dptr) {
 		v = PyString_FromStringAndSize(nextkey.dptr, nextkey.dsize);
@@ -302,6 +320,7 @@ PyObject *args;
 {
 	if (!PyArg_NoArgs(args))
 		return NULL;
+	check_dbmobject_open(dp);
 	errno = 0;
 	if (gdbm_reorganize(dp->di_dbm) < 0) {
 		if (errno != 0)
@@ -322,6 +341,7 @@ dbm_sync(dp, args)
 {
 	if (!PyArg_NoArgs(args))
 		return NULL;
+	check_dbmobject_open(dp);
 	gdbm_sync(dp->di_dbm);
 	Py_INCREF(Py_None);
 	return Py_None;
-- 
cgit v0.12

