[Python-3000-checkins] r57006 - in python/branches/py3k-buffer: Modules/_sre.c Objects/bufferobject.c Objects/bytesobject.c

travis.oliphant python-3000-checkins at python.org
Tue Aug 14 07:40:26 CEST 2007


Author: travis.oliphant
Date: Tue Aug 14 07:40:25 2007
New Revision: 57006

Modified:
   python/branches/py3k-buffer/Modules/_sre.c
   python/branches/py3k-buffer/Objects/bufferobject.c
   python/branches/py3k-buffer/Objects/bytesobject.c
Log:
py3k-buffer: fixed problem with passing flags through buffer of unicode objects.

Modified: python/branches/py3k-buffer/Modules/_sre.c
==============================================================================
--- python/branches/py3k-buffer/Modules/_sre.c	(original)
+++ python/branches/py3k-buffer/Modules/_sre.c	Tue Aug 14 07:40:25 2007
@@ -1675,6 +1675,7 @@
     PyBuffer view;
 
     /* get pointer to string buffer */
+    view.len = -1;
     buffer = Py_Type(string)->tp_as_buffer;
     if (!buffer || !buffer->bf_getbuffer || 
         (*buffer->bf_getbuffer)(string, &view, PyBUF_SIMPLE) < 0) {
@@ -1713,6 +1714,10 @@
     *p_length = size;
     *p_charsize = charsize;
 
+    if (ptr == NULL) {
+            PyErr_SetString(PyExc_ValueError, 
+                            "Buffer is NULL");
+    }
     return ptr;
 }
 

Modified: python/branches/py3k-buffer/Objects/bufferobject.c
==============================================================================
--- python/branches/py3k-buffer/Objects/bufferobject.c	(original)
+++ python/branches/py3k-buffer/Objects/bufferobject.c	Tue Aug 14 07:40:25 2007
@@ -16,7 +16,7 @@
 
 
 static int
-get_buf(PyBufferObject *self, PyBuffer *view)
+get_buf(PyBufferObject *self, PyBuffer *view, int flags)
 {
 	if (self->b_base == NULL) {
 		view->buf = self->b_ptr;
@@ -25,7 +25,7 @@
 	else {
 		Py_ssize_t count, offset;
 		PyBufferProcs *bp = self->b_base->ob_type->tp_as_buffer;
-                if ((*bp->bf_getbuffer)(self->b_base, view, PyBUF_SIMPLE) < 0) return 0;
+                if ((*bp->bf_getbuffer)(self->b_base, view, flags) < 0) return 0;
                 count = view->len;
 		/* apply constraints to the start/end */
 		if (self->b_offset > count)
@@ -48,7 +48,7 @@
 buffer_getbuf(PyBufferObject *self, PyBuffer *view, int flags)
 {
         if (view == NULL) return 0;
-        if (!get_buf(self, view))
+        if (!get_buf(self, view, flags))
                 return -1;
         return PyBuffer_FillInfo(view, view->buf, view->len, view->readonly, flags);
 }
@@ -226,12 +226,12 @@
 }
 
 static int
-get_bufx(PyObject *obj, PyBuffer *view)
+get_bufx(PyObject *obj, PyBuffer *view, int flags)
 {
 	PyBufferProcs *bp;
 
 	if (PyBuffer_Check(obj)) {
-		if (!get_buf((PyBufferObject *)obj, view)) {
+		if (!get_buf((PyBufferObject *)obj, view, flags)) {
 			PyErr_Clear();
 			return 0;
 		}
@@ -256,9 +256,9 @@
         PyBuffer v1, v2;
 
 	ok = 1;
-	if (!get_bufx(self, &v1))
+	if (!get_bufx(self, &v1, PyBUF_SIMPLE))
 		ok = 0;
-	if (!get_bufx(other, &v2)) {
+	if (!get_bufx(other, &v2, PyBUF_SIMPLE)) {
                 if (ok) PyObject_ReleaseBuffer(self, &v1);
 		ok = 0;
         }
@@ -322,16 +322,7 @@
 	if ( self->b_hash != -1 )
 		return self->b_hash;
 
-	/* XXX potential bugs here, a readonly buffer does not imply that the
-	 * underlying memory is immutable.  b_readonly is a necessary but not
-	 * sufficient condition for a buffer to be hashable.  Perhaps it would
-	 * be better to only allow hashing if the underlying object is known to
-	 * be immutable (e.g. PyString_Check() is true).  Another idea would
-	 * be to call tp_hash on the underlying object and see if it raises
-	 * an error. */
-	if ( !self->b_readonly )
-
-	if (!get_buf(self, &view))
+        if (!get_buf(self, &view, PyBUF_SIMPLE))
 		return -1;
         if (!(view.readonly)) {
                 PyErr_SetString(PyExc_TypeError,
@@ -359,7 +350,7 @@
         PyBuffer view;
         PyObject *res;
 
-	if (!get_buf(self, &view))
+	if (!get_buf(self, &view, PyBUF_SIMPLE))
 		return NULL;
 	res = PyString_FromStringAndSize((const char *)view.buf, view.len);
         PyObject_ReleaseBuffer(self->b_base, &view);
@@ -373,7 +364,7 @@
 {
         PyBuffer view;
 
-	if (!get_buf(self, &view))
+	if (!get_buf(self, &view, PyBUF_SIMPLE))
 		return -1;
         PyObject_ReleaseBuffer(self->b_base, &view);
 	return view.len;
@@ -394,7 +385,7 @@
 		return NULL;
 	}
 
- 	if (!get_buf(self, &view))
+ 	if (!get_buf(self, &view, PyBUF_SIMPLE))
  		return NULL;
  
 	/* optimize special case */
@@ -434,7 +425,7 @@
 
 	if ( count < 0 )
 		count = 0;
-	if (!get_buf(self, &view))
+	if (!get_buf(self, &view, PyBUF_SIMPLE))
 		return NULL;
 	ob = PyBytes_FromStringAndSize(NULL, view.len * count);
 	if ( ob == NULL )
@@ -457,7 +448,7 @@
         PyBuffer view;
         PyObject *ob;
 
-	if (!get_buf(self, &view))
+	if (!get_buf(self, &view, PyBUF_SIMPLE))
 		return NULL;
 	if ( idx < 0 || idx >= view.len ) {
 		PyErr_SetString(PyExc_IndexError, "buffer index out of range");
@@ -473,7 +464,7 @@
 {
         PyObject *ob;
         PyBuffer view;
-	if (!get_buf(self, &view))
+	if (!get_buf(self, &view, PyBUF_SIMPLE))
 		return NULL;
 	if ( left < 0 )
 		left = 0;
@@ -495,7 +486,7 @@
 	PyBufferProcs *pb;
         PyBuffer view, view2;
 
-	if (!get_buf(self, &view))
+	if (!get_buf(self, &view, PyBUF_SIMPLE))
 		return -1;
         
 	if ( self->b_readonly || view.readonly ) {
@@ -552,7 +543,7 @@
 		PyErr_BadArgument();
 		return -1;
 	}
-	if (!get_buf(self, &v1))
+	if (!get_buf(self, &v1, PyBUF_SIMPLE))
                 return -1;
 
 	if ( self->b_readonly || v1.readonly) {

Modified: python/branches/py3k-buffer/Objects/bytesobject.c
==============================================================================
--- python/branches/py3k-buffer/Objects/bytesobject.c	(original)
+++ python/branches/py3k-buffer/Objects/bytesobject.c	Tue Aug 14 07:40:25 2007
@@ -53,11 +53,16 @@
 bytes_getbuffer(PyBytesObject *obj, PyBuffer *view, int flags)
 {        
         int ret;
+        void *ptr;
         if (view == NULL) {
                 obj->ob_exports++;
                 return 0;
         }
-        ret = PyBuffer_FillInfo(view, obj->ob_bytes, Py_Size(obj), 0, flags);
+        if (obj->ob_bytes == NULL) 
+                ptr = "";
+        else
+                ptr = obj->ob_bytes;
+        ret = PyBuffer_FillInfo(view, ptr, Py_Size(obj), 0, flags);
         if (ret >= 0) {
                 obj->ob_exports++;
         }
@@ -2019,9 +2024,11 @@
 bytes_replace(PyBytesObject *self, PyObject *args)
 {
     Py_ssize_t count = -1;
-    PyObject *from, *to;
+    PyObject *from, *to, *res;
     const char *from_s, *to_s;
     Py_ssize_t from_len, to_len;
+    int relfrom=0, relto=0;
+    PyBuffer vfrom, vto;
 
     if (!PyArg_ParseTuple(args, "OO|n:replace", &from, &to, &count))
         return NULL;
@@ -2030,21 +2037,38 @@
         from_s = PyBytes_AS_STRING(from);
         from_len = PyBytes_GET_SIZE(from);
     }
-    /* XXX -> use the modern buffer interface */
-    else if (PyObject_AsCharBuffer(from, &from_s, &from_len))
-        return NULL;
+    else {
+            if (PyObject_GetBuffer(from, &vfrom, PyBUF_CHARACTER) < 0) 
+                    return NULL;
+            from_s = vfrom.buf;
+            from_len = vfrom.len;
+            relfrom = 1;
+    }
 
     if (PyBytes_Check(to)) {
         to_s = PyBytes_AS_STRING(to);
         to_len = PyBytes_GET_SIZE(to);
     }
-    /* XXX -> use the modern buffer interface */
-    else if (PyObject_AsCharBuffer(to, &to_s, &to_len))
-        return NULL;
+    else {
+            if (PyObject_GetBuffer(to, &vto, PyBUF_CHARACTER) < 0) {
+                    if (relfrom)
+                            PyObject_ReleaseBuffer(from, &vfrom);
+                    return NULL;
+            }
+            to_s = vto.buf;
+            to_len = vto.len;
+            relto = 1;
+    }
 
-    return (PyObject *)replace((PyBytesObject *) self,
-                               from_s, from_len,
-                               to_s, to_len, count);
+    res = (PyObject *)replace((PyBytesObject *) self,
+                              from_s, from_len,
+                              to_s, to_len, count);
+
+    if (relfrom)
+            PyObject_ReleaseBuffer(from, &vfrom);
+    if (relto)
+            PyObject_ReleaseBuffer(to, &vto);
+    return res;
 }
 
 


More information about the Python-3000-checkins mailing list