[Python-checkins] r46385 - in sandbox/trunk/hotbuffer: Modules/_hotbuf.c hotbuf.py test_hotbuf.py

bob.ippolito python-checkins at python.org
Fri May 26 20:33:36 CEST 2006


Author: bob.ippolito
Date: Fri May 26 20:33:35 2006
New Revision: 46385

Modified:
   sandbox/trunk/hotbuffer/Modules/_hotbuf.c
   sandbox/trunk/hotbuffer/hotbuf.py
   sandbox/trunk/hotbuffer/test_hotbuf.py
Log:
change IndexError to HotbufOverflow

Modified: sandbox/trunk/hotbuffer/Modules/_hotbuf.c
==============================================================================
--- sandbox/trunk/hotbuffer/Modules/_hotbuf.c	(original)
+++ sandbox/trunk/hotbuffer/Modules/_hotbuf.c	Fri May 26 20:33:35 2006
@@ -23,6 +23,7 @@
 #endif
 
 static PyTypeObject PyHotbuf_Type;
+static PyObject *HotbufOverflow = NULL;
 
 #define PyHotbuf_Check(op) PyObject_TypeCheck((op), &PyHotbuf_Type)
 
@@ -102,7 +103,7 @@
 {
     Py_ssize_t newposition = self->b_position + nbytes;
     if (newposition > self->b_limit) {
-        PyErr_SetString(PyExc_IndexError,
+        PyErr_SetString(HotbufOverflow,
                         "position must be smaller than limit");
         return -1;
     }
@@ -255,7 +256,7 @@
         return NULL;
 
     if ( newposition > self->b_capacity ) {
-        PyErr_SetString(PyExc_IndexError,
+        PyErr_SetString(HotbufOverflow,
                         "position must be smaller than capacity");
         return NULL;
     }
@@ -306,7 +307,7 @@
         return NULL;
 
     if ( newlimit > self->b_capacity ) {
-        PyErr_SetString(PyExc_IndexError,
+        PyErr_SetString(HotbufOverflow,
                         "limit must be smaller than capacity");
         return NULL;
     }
@@ -328,7 +329,7 @@
 \n\
 Sets this buffer's limit to be beyond position by the given\n\
 number number of bytes. If the limit is larger than the \n\
-capacity an IndexError is raised.");
+capacity an HotbufOverflow is raised.");
 
 static PyObject*
 hotbuf_setwindow(PyHotbufObject *self, PyObject* arg)
@@ -342,7 +343,7 @@
 
     newlimit = self->b_position + window;
     if ( newlimit > self->b_capacity ) {
-        PyErr_SetString(PyExc_IndexError,
+        PyErr_SetString(HotbufOverflow,
                         "limit must be smaller than capacity");
         return NULL;
     }
@@ -373,7 +374,7 @@
 \n\
 Resets this buffer's position to the previously-marked\n\
 position and limit.  Invoking this method neither changes nor\n\
-discards the mark's value.  An IndexError is raised if the\n\
+discards the mark's value.  An HotbufOverflow is raised if the\n\
 mark has not been set.  This method returns the new\n\
 position's value.  If you specify a number of bytes via \n\
 'advbytes', the method advances the position by that many bytes.");
@@ -386,7 +387,7 @@
 
     /* Validate that the mark is set. */
     if ( self->b_mark_position == -1 || self->b_mark_limit == -1 ) {
-        PyErr_SetString(PyExc_IndexError,
+        PyErr_SetString(HotbufOverflow,
                         "mark has not been yet set");
         return NULL;
     }
@@ -404,7 +405,7 @@
     /* Validate the new position, if specified */
     newposition = self->b_mark_position + advbytes;
     if (newposition > self->b_limit) {
-        PyErr_SetString(PyExc_IndexError,
+        PyErr_SetString(HotbufOverflow,
                         "new position must be smaller than limit");
         return NULL;
     }
@@ -569,7 +570,7 @@
 Relative get methods. \n\
 Reads something at this buffer's current position, \n\
 and then increments the position.\n\
-An IndexError is raised if the position is at the end of the buffer.");
+An HotbufOverflow is raised if the position is at the end of the buffer.");
 
 PyDoc_STRVAR(put__doc__,
              "B.put*(data)\n\
@@ -577,14 +578,14 @@
 Relative put methods. \n\
 Writes the given byte into this buffer at the current position,\n\
 and then increments the position.\n\
-An IndexError is raised if the position is at the end of the buffer.");
+An HotbufOverflow is raised if the position is at the end of the buffer.");
 
 
 /* Check if we're going to be trying to year beyond the buffer active
    window limit, and if so, sets and error and return */
 #define CHECK_LIMIT_ERROR(sz)                                   \
     if ( (self->b_position + sz) > self->b_limit ) {            \
-        PyErr_SetString(PyExc_IndexError,                       \
+        PyErr_SetString(HotbufOverflow,                       \
                         "attempted read beyond buffer limit");  \
         return NULL;                                            \
     }
@@ -628,7 +629,7 @@
 Extract a string of 'nbytes' bytes from the buffer and advance the\n\
 position accordingly.  If 'nbytes' is not specified, get the string\n\
 up to the limit.\n\
-An IndexError is raised if the position is at the end of the buffer.");
+An HotbufOverflow is raised if the position is at the end of the buffer.");
 
 static PyObject*
 hotbuf_getstr(PyHotbufObject *self, PyObject* args)
@@ -670,7 +671,7 @@
 \n\
 Write a string of 'nbytes' bytes from the buffer and advance the \n\
 position accordingly.\n\
-An IndexError is raised if the position is at the end of the buffer.");
+An HotbufOverflow is raised if the position is at the end of the buffer.");
 
 static PyObject*
 hotbuf_putstr(PyHotbufObject *self, PyObject* arg)
@@ -857,7 +858,7 @@
         return -1;
 
     if (newposition > self->b_capacity) {
-        PyErr_SetString(PyExc_IndexError,
+        PyErr_SetString(HotbufOverflow,
                         "position must be smaller than capacity");
         return -1;
     }
@@ -884,7 +885,7 @@
         return -1;
 
     if (newlimit > self->b_capacity) {
-        PyErr_SetString(PyExc_IndexError,
+        PyErr_SetString(HotbufOverflow,
                         "limit must be smaller than capacity");
         return -1;
     }
@@ -1046,6 +1047,13 @@
     if (PyType_Ready(&PyHotbuf_Type) < 0)
         return;
 
+	if (HotbufOverflow == NULL) {
+		HotbufOverflow = PyErr_NewException("hotbuf.HotbufOverflow", NULL, NULL);
+		if (HotbufOverflow == NULL)
+			return;
+	}
+	Py_INCREF(HotbufOverflow);
+	PyModule_AddObject(m, "HotbufOverflow", HotbufOverflow);
     Py_INCREF((PyObject *)&PyHotbuf_Type);
     PyModule_AddObject(m, "HotbufType", (PyObject *)&PyHotbuf_Type);
     Py_INCREF((PyObject *)&PyHotbuf_Type);

Modified: sandbox/trunk/hotbuffer/hotbuf.py
==============================================================================
--- sandbox/trunk/hotbuffer/hotbuf.py	(original)
+++ sandbox/trunk/hotbuffer/hotbuf.py	Fri May 26 20:33:35 2006
@@ -4,7 +4,7 @@
 A buffer class for fast I/O.
 """
 
-from _hotbuf import _hotbuf
+from _hotbuf import _hotbuf, HotbufOverflow
 from struct import Struct
 
 _long = Struct('l')

Modified: sandbox/trunk/hotbuffer/test_hotbuf.py
==============================================================================
--- sandbox/trunk/hotbuffer/test_hotbuf.py	(original)
+++ sandbox/trunk/hotbuffer/test_hotbuf.py	Fri May 26 20:33:35 2006
@@ -6,7 +6,7 @@
 #  Licensed to PSF under a Contributor Agreement.
 #
 
-from hotbuf import hotbuf
+from hotbuf import hotbuf, HotbufOverflow
 from struct import Struct
 import unittest
 from test import test_support
@@ -32,18 +32,18 @@
         assert b.position == 0
         b.setposition(10)
         self.assertEquals(b.position, 10)
-        self.assertRaises(IndexError, b.setposition, CAPACITY + 1)
+        self.assertRaises(HotbufOverflow, b.setposition, CAPACITY + 1)
 
         # Play with the limit
         assert b.limit == CAPACITY
         b.setlimit(CAPACITY - 10)
         self.assertEquals(b.limit, CAPACITY - 10)
-        self.assertRaises(IndexError, b.setlimit, CAPACITY + 1)
+        self.assertRaises(HotbufOverflow, b.setlimit, CAPACITY + 1)
         b.setlimit(b.position - 1)
         self.assertEquals(b.position, b.limit)
 
         # Play with reset before the mark has been set.
-        self.assertRaises(IndexError, b.setlimit, CAPACITY + 1)
+        self.assertRaises(HotbufOverflow, b.setlimit, CAPACITY + 1)
 
         # Play with the mark
         b.setposition(10)
@@ -83,7 +83,7 @@
         b.advance(32)
         self.assertEquals(b.position, 42)
 
-        self.assertRaises(IndexError, b.advance, CAPACITY)
+        self.assertRaises(HotbufOverflow, b.advance, CAPACITY)
 
         # Play with setwindow()
         b.clear()
@@ -121,7 +121,7 @@
             b.putbyte(x)
 
         # Test overflow.
-        self.assertRaises(IndexError, b.putbyte, 42)
+        self.assertRaises(HotbufOverflow, b.putbyte, 42)
 
         # Read all data from the buffer.
         b.flip()
@@ -130,7 +130,7 @@
             assert nx == x
 
         # Test underflow.
-        self.assertRaises(IndexError, b.putbyte, 42)
+        self.assertRaises(HotbufOverflow, b.putbyte, 42)
 
     def test_str( self ):
         b = hotbuf(256)
@@ -144,10 +144,10 @@
         
         # Test overflow.
         b.flip()
-        self.assertRaises(IndexError, b.putstr, ' ' * 1000)
+        self.assertRaises(HotbufOverflow, b.putstr, ' ' * 1000)
         
         # Test underflow.
-        self.assertRaises(IndexError, b.getstr, 1000)
+        self.assertRaises(HotbufOverflow, b.getstr, 1000)
 
         # Test getting the rest of the string.
         b.clear()


More information about the Python-checkins mailing list