[Python-checkins] r46396 - in sandbox/trunk/hotbuffer: Modules/_hotbuf.c hotbuf.py test_hotbuf.py
bob.ippolito
python-checkins at python.org
Fri May 26 21:22:20 CEST 2006
Author: bob.ippolito
Date: Fri May 26 21:22:19 2006
New Revision: 46396
Modified:
sandbox/trunk/hotbuffer/Modules/_hotbuf.c
sandbox/trunk/hotbuffer/hotbuf.py
sandbox/trunk/hotbuffer/test_hotbuf.py
Log:
change to BoundaryError
Modified: sandbox/trunk/hotbuffer/Modules/_hotbuf.c
==============================================================================
--- sandbox/trunk/hotbuffer/Modules/_hotbuf.c (original)
+++ sandbox/trunk/hotbuffer/Modules/_hotbuf.c Fri May 26 21:22:19 2006
@@ -29,7 +29,7 @@
#include "fastsearch.h"
static PyTypeObject PyHotbuf_Type;
-static PyObject *HotbufOverflow = NULL;
+static PyObject *BoundaryError = NULL;
#define PyHotbuf_Check(op) PyObject_TypeCheck((op), &PyHotbuf_Type)
@@ -109,7 +109,7 @@
{
Py_ssize_t newposition = self->b_position + nbytes;
if (newposition > self->b_limit) {
- PyErr_SetString(HotbufOverflow,
+ PyErr_SetString(BoundaryError,
"position must be smaller than limit");
return -1;
}
@@ -262,7 +262,7 @@
return NULL;
if ( newposition > self->b_capacity ) {
- PyErr_SetString(HotbufOverflow,
+ PyErr_SetString(BoundaryError,
"position must be smaller than capacity");
return NULL;
}
@@ -313,7 +313,7 @@
return NULL;
if ( newlimit > self->b_capacity ) {
- PyErr_SetString(HotbufOverflow,
+ PyErr_SetString(BoundaryError,
"limit must be smaller than capacity");
return NULL;
}
@@ -335,7 +335,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 HotbufOverflow is raised.");
+capacity an BoundaryError is raised.");
static PyObject*
hotbuf_setwindow(PyHotbufObject *self, PyObject* arg)
@@ -349,7 +349,7 @@
newlimit = self->b_position + window;
if ( newlimit > self->b_capacity ) {
- PyErr_SetString(HotbufOverflow,
+ PyErr_SetString(BoundaryError,
"limit must be smaller than capacity");
return NULL;
}
@@ -380,7 +380,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 HotbufOverflow is raised if the\n\
+discards the mark's value. An BoundaryError 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.");
@@ -393,7 +393,7 @@
/* Validate that the mark is set. */
if ( self->b_mark_position == -1 || self->b_mark_limit == -1 ) {
- PyErr_SetString(HotbufOverflow,
+ PyErr_SetString(BoundaryError,
"mark has not been yet set");
return NULL;
}
@@ -411,7 +411,7 @@
/* Validate the new position, if specified */
newposition = self->b_mark_position + advbytes;
if (newposition > self->b_limit) {
- PyErr_SetString(HotbufOverflow,
+ PyErr_SetString(BoundaryError,
"new position must be smaller than limit");
return NULL;
}
@@ -576,7 +576,7 @@
Relative get methods. \n\
Reads something at this buffer's current position, \n\
and then increments the position.\n\
-An HotbufOverflow is raised if the position is at the end of the buffer.");
+An BoundaryError is raised if the position is at the end of the buffer.");
PyDoc_STRVAR(put__doc__,
"B.put*(data)\n\
@@ -584,14 +584,14 @@
Relative put methods. \n\
Writes the given byte into this buffer at the current position,\n\
and then increments the position.\n\
-An HotbufOverflow is raised if the position is at the end of the buffer.");
+An BoundaryError 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(HotbufOverflow, \
+ PyErr_SetString(BoundaryError, \
"attempted read beyond buffer limit"); \
return NULL; \
}
@@ -635,7 +635,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 HotbufOverflow is raised if the position is at the end of the buffer.");
+An BoundaryError is raised if the position is at the end of the buffer.");
static PyObject*
hotbuf_getstr(PyHotbufObject *self, PyObject* args)
@@ -677,7 +677,7 @@
\n\
Write a string of 'nbytes' bytes from the buffer and advance the \n\
position accordingly.\n\
-An HotbufOverflow is raised if the position is at the end of the buffer.");
+An BoundaryError is raised if the position is at the end of the buffer.");
static PyObject*
hotbuf_putstr(PyHotbufObject *self, PyObject* arg)
@@ -925,7 +925,7 @@
return -1;
if (newposition > self->b_capacity) {
- PyErr_SetString(HotbufOverflow,
+ PyErr_SetString(BoundaryError,
"position must be smaller than capacity");
return -1;
}
@@ -952,7 +952,7 @@
return -1;
if (newlimit > self->b_capacity) {
- PyErr_SetString(HotbufOverflow,
+ PyErr_SetString(BoundaryError,
"limit must be smaller than capacity");
return -1;
}
@@ -1116,13 +1116,13 @@
if (PyType_Ready(&PyHotbuf_Type) < 0)
return;
- if (HotbufOverflow == NULL) {
- HotbufOverflow = PyErr_NewException("hotbuf.HotbufOverflow", NULL, NULL);
- if (HotbufOverflow == NULL)
+ if (BoundaryError == NULL) {
+ BoundaryError = PyErr_NewException("hotbuf.BoundaryError", NULL, NULL);
+ if (BoundaryError == NULL)
return;
}
- Py_INCREF(HotbufOverflow);
- PyModule_AddObject(m, "HotbufOverflow", HotbufOverflow);
+ Py_INCREF(BoundaryError);
+ PyModule_AddObject(m, "BoundaryError", BoundaryError);
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 21:22:19 2006
@@ -4,7 +4,7 @@
A buffer class for fast I/O.
"""
-from _hotbuf import _hotbuf, HotbufOverflow
+from _hotbuf import _hotbuf, BoundaryError
from struct import Struct
_long = Struct('l')
@@ -15,5 +15,8 @@
"""
Pack using the given Struct object 'structobj', the remaining arguments.
"""
+ size = structobj.size
+ if size > len(self):
+ raise BoundaryError("attempted to write beyond buffer limit")
structobj.pack_to(self, 0, *values)
- self.advance(structobj.size)
+ self.advance(size)
Modified: sandbox/trunk/hotbuffer/test_hotbuf.py
==============================================================================
--- sandbox/trunk/hotbuffer/test_hotbuf.py (original)
+++ sandbox/trunk/hotbuffer/test_hotbuf.py Fri May 26 21:22:19 2006
@@ -6,7 +6,7 @@
# Licensed to PSF under a Contributor Agreement.
#
-from hotbuf import hotbuf, HotbufOverflow
+from hotbuf import hotbuf, BoundaryError
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(HotbufOverflow, b.setposition, CAPACITY + 1)
+ self.assertRaises(BoundaryError, b.setposition, CAPACITY + 1)
# Play with the limit
assert b.limit == CAPACITY
b.setlimit(CAPACITY - 10)
self.assertEquals(b.limit, CAPACITY - 10)
- self.assertRaises(HotbufOverflow, b.setlimit, CAPACITY + 1)
+ self.assertRaises(BoundaryError, 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(HotbufOverflow, b.setlimit, CAPACITY + 1)
+ self.assertRaises(BoundaryError, 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(HotbufOverflow, b.advance, CAPACITY)
+ self.assertRaises(BoundaryError, b.advance, CAPACITY)
# Play with setwindow()
b.clear()
@@ -121,7 +121,7 @@
b.putbyte(x)
# Test overflow.
- self.assertRaises(HotbufOverflow, b.putbyte, 42)
+ self.assertRaises(BoundaryError, b.putbyte, 42)
# Read all data from the buffer.
b.flip()
@@ -130,7 +130,7 @@
assert nx == x
# Test underflow.
- self.assertRaises(HotbufOverflow, b.putbyte, 42)
+ self.assertRaises(BoundaryError, b.putbyte, 42)
def test_str( self ):
b = hotbuf(256)
@@ -144,10 +144,10 @@
# Test overflow.
b.flip()
- self.assertRaises(HotbufOverflow, b.putstr, ' ' * 1000)
+ self.assertRaises(BoundaryError, b.putstr, ' ' * 1000)
# Test underflow.
- self.assertRaises(HotbufOverflow, b.getstr, 1000)
+ self.assertRaises(BoundaryError, b.getstr, 1000)
# Test getting the rest of the string.
b.clear()
More information about the Python-checkins
mailing list