[Python-3000-checkins] r58800 - in python/branches/py3k-pep3137: Lib/test/test_mmap.py Modules/mmapmodule.c

guido.van.rossum python-3000-checkins at python.org
Fri Nov 2 21:30:04 CET 2007


Author: guido.van.rossum
Date: Fri Nov  2 21:30:04 2007
New Revision: 58800

Modified:
   python/branches/py3k-pep3137/Lib/test/test_mmap.py
   python/branches/py3k-pep3137/Modules/mmapmodule.c
Log:
Fix up mmap.  It behaves more like bytes now, e.g. m[i] is an int,
and m[i:j] returns a PyString.


Modified: python/branches/py3k-pep3137/Lib/test/test_mmap.py
==============================================================================
--- python/branches/py3k-pep3137/Lib/test/test_mmap.py	(original)
+++ python/branches/py3k-pep3137/Lib/test/test_mmap.py	Fri Nov  2 21:30:04 2007
@@ -39,15 +39,15 @@
 
         self.assertEqual(len(m), 2*PAGESIZE)
 
-        self.assertEqual(m[0], b'\0')
+        self.assertEqual(m[0], 0)
         self.assertEqual(m[0:3], b'\0\0\0')
 
         # Modify the file's content
-        m[0] = b'3'
+        m[0] = b'3'[0]
         m[PAGESIZE +3: PAGESIZE +3+3] = b'bar'
 
         # Check that the modification worked
-        self.assertEqual(m[0], b'3')
+        self.assertEqual(m[0], b'3'[0])
         self.assertEqual(m[0:3], b'3\0\0')
         self.assertEqual(m[PAGESIZE-1 : PAGESIZE + 7], b'\0foobar\0')
 
@@ -297,11 +297,11 @@
         # anonymous mmap.mmap(-1, PAGE)
         m = mmap.mmap(-1, PAGESIZE)
         for x in range(PAGESIZE):
-            self.assertEqual(m[x], b'\0', "anonymously mmap'ed contents should be zero")
+            self.assertEqual(m[x], 0,
+                             "anonymously mmap'ed contents should be zero")
 
-        b = bytes(1)
         for x in range(PAGESIZE):
-            b[0] = x & 255
+            b = x & 0xff
             m[x] = b
             self.assertEqual(m[x], b)
 

Modified: python/branches/py3k-pep3137/Modules/mmapmodule.c
==============================================================================
--- python/branches/py3k-pep3137/Modules/mmapmodule.c	(original)
+++ python/branches/py3k-pep3137/Modules/mmapmodule.c	Fri Nov  2 21:30:04 2007
@@ -289,7 +289,7 @@
                 return 0;
         }
 	if ((self->access == ACCESS_WRITE) || (self->access == ACCESS_DEFAULT))
-		return 1;        
+		return 1;
 	PyErr_Format(PyExc_TypeError,
 		     "mmap can't resize a readonly or copy-on-write memory map.");
 	return 0;
@@ -601,10 +601,10 @@
 /* Functions for treating an mmap'ed file as a buffer */
 
 static int
-mmap_buffer_getbuf(mmap_object *self, Py_buffer *view, int flags) 
+mmap_buffer_getbuf(mmap_object *self, Py_buffer *view, int flags)
 {
 	CHECK_VALID(-1);
-        if (PyBuffer_FillInfo(view, self->data, self->size, 
+        if (PyBuffer_FillInfo(view, self->data, self->size,
                               (self->access == ACCESS_READ), flags) < 0)
                 return -1;
         self->exports++;
@@ -656,7 +656,7 @@
 				"mmap index out of range");
 			return NULL;
 		}
-		return PyBytes_FromStringAndSize(self->data + i, 1);
+		return PyInt_FromLong(Py_CHARMASK(self->data[i]));
 	}
 	else if (PySlice_Check(item)) {
 		Py_ssize_t start, stop, step, slicelen;
@@ -665,12 +665,12 @@
 				 &start, &stop, &step, &slicelen) < 0) {
 			return NULL;
 		}
-		
+
 		if (slicelen <= 0)
-			return PyBytes_FromStringAndSize("", 0);
+			return PyString_FromStringAndSize("", 0);
 		else if (step == 1)
-			return PyBytes_FromStringAndSize(self->data + start,
-							 slicelen);
+			return PyString_FromStringAndSize(self->data + start,
+							  slicelen);
 		else {
 			char *result_buf = (char *)PyMem_Malloc(slicelen);
 			Py_ssize_t cur, i;
@@ -682,8 +682,8 @@
 			     cur += step, i++) {
 			     	result_buf[i] = self->data[cur];
 			}
-			result = PyBytes_FromStringAndSize(result_buf,
-							   slicelen);
+			result = PyString_FromStringAndSize(result_buf,
+							    slicelen);
 			PyMem_Free(result_buf);
 			return result;
 		}
@@ -745,9 +745,12 @@
 {
 	CHECK_VALID(-1);
 
+	if (!is_writable(self))
+		return -1;
+
 	if (PyIndex_Check(item)) {
 		Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
-		const char *buf;
+		Py_ssize_t v;
 
 		if (i == -1 && PyErr_Occurred())
 			return -1;
@@ -755,28 +758,35 @@
 			i += self->size;
 		if (i < 0 || i > self->size) {
 			PyErr_SetString(PyExc_IndexError,
-				"mmap index out of range");
+					"mmap index out of range");
 			return -1;
 		}
 		if (value == NULL) {
 			PyErr_SetString(PyExc_TypeError,
-				"mmap object doesn't support item deletion");
+					"mmap doesn't support item deletion");
 			return -1;
 		}
-		if (!PyBytes_Check(value) || PyBytes_Size(value) != 1) {
-			PyErr_SetString(PyExc_IndexError,
-		          "mmap assignment must be length-1 bytes()");
+		if (!PyIndex_Check(value)) {
+			PyErr_SetString(PyExc_TypeError,
+					"mmap item value must be an int");
 			return -1;
 		}
-		if (!is_writable(self))
+		v = PyNumber_AsSsize_t(value, PyExc_TypeError);
+		if (v == -1 && PyErr_Occurred())
+			return -1;
+		if (v < 0 || v > 255) {
+			PyErr_SetString(PyExc_ValueError,
+					"mmap item value must be "
+					"in range(0, 256)");
 			return -1;
-		buf = PyBytes_AsString(value);
-		self->data[i] = buf[0];
+		}
+		self->data[i] = v;
 		return 0;
 	}
 	else if (PySlice_Check(item)) {
 		Py_ssize_t start, stop, step, slicelen;
-		
+                Py_buffer vbuf;
+
 		if (PySlice_GetIndicesEx((PySliceObject *)item,
 					 self->size, &start, &stop,
 					 &step, &slicelen) < 0) {
@@ -787,41 +797,32 @@
 				"mmap object doesn't support slice deletion");
 			return -1;
 		}
-		if (!PyBytes_Check(value)) {
-			PyErr_SetString(PyExc_IndexError,
-				"mmap slice assignment must be bytes");
+                if (PyObject_GetBuffer(value, &vbuf, PyBUF_SIMPLE) < 0)
 			return -1;
-		}
-		if (PyBytes_Size(value) != slicelen) {
+		if (vbuf.len != slicelen) {
 			PyErr_SetString(PyExc_IndexError,
 				"mmap slice assignment is wrong size");
+			PyObject_ReleaseBuffer(value, &vbuf);
 			return -1;
 		}
-		if (!is_writable(self))
-			return -1;
 
-		if (slicelen == 0)
-			return 0;
+		if (slicelen == 0) {
+		}
 		else if (step == 1) {
-			const char *buf = PyBytes_AsString(value);
-
-			if (buf == NULL)
-				return -1;
-			memcpy(self->data + start, buf, slicelen);
-			return 0;
+			memcpy(self->data + start, vbuf.buf, slicelen);
 		}
 		else {
 			Py_ssize_t cur, i;
-			const char *buf = PyBytes_AsString(value);
-			
-			if (buf == NULL)
-				return -1;
-			for (cur = start, i = 0; i < slicelen;
-			     cur += step, i++) {
-				self->data[cur] = buf[i];
+
+			for (cur = start, i = 0;
+			     i < slicelen;
+			     cur += step, i++)
+			{
+				self->data[cur] = ((char *)vbuf.buf)[i];
 			}
-			return 0;
 		}
+		PyObject_ReleaseBuffer(value, &vbuf);
+		return 0;
 	}
 	else {
 		PyErr_SetString(PyExc_TypeError,
@@ -886,9 +887,9 @@
 {
 	if (PyIndex_Check(o)) {
 		Py_ssize_t i = PyNumber_AsSsize_t(o, PyExc_OverflowError);
-		if (i==-1 && PyErr_Occurred()) 
+		if (i==-1 && PyErr_Occurred())
 			return -1;
-		if (i < 0) {	 
+		if (i < 0) {
 			PyErr_SetString(PyExc_OverflowError,
 					"memory mapped size must be positive");
 			return -1;
@@ -1118,8 +1119,8 @@
 			    (dwErr = GetLastError()) != NO_ERROR) {
 				Py_DECREF(m_obj);
 				return PyErr_SetFromWindowsErr(dwErr);
-			}	
-				    
+			}
+
 #if SIZEOF_SIZE_T > 4
 			m_obj->size = (((size_t)high)<<32) + low;
 #else


More information about the Python-3000-checkins mailing list