[Python-checkins] r46414 - in sandbox/trunk/hotbuffer: Modules/_hotbuf.c README.txt test_hotbuf.py use_cases.py
martin.blais
python-checkins at python.org
Fri May 26 22:21:39 CEST 2006
Author: martin.blais
Date: Fri May 26 22:21:38 2006
New Revision: 46414
Added:
sandbox/trunk/hotbuffer/use_cases.py (contents, props changed)
Modified:
sandbox/trunk/hotbuffer/Modules/_hotbuf.c
sandbox/trunk/hotbuffer/README.txt
sandbox/trunk/hotbuffer/test_hotbuf.py
Log:
Removed remaining.
Modified: sandbox/trunk/hotbuffer/Modules/_hotbuf.c
==============================================================================
--- sandbox/trunk/hotbuffer/Modules/_hotbuf.c (original)
+++ sandbox/trunk/hotbuffer/Modules/_hotbuf.c Fri May 26 22:21:38 2006
@@ -498,18 +498,6 @@
}
-PyDoc_STRVAR(remaining__doc__,
- "B.remaining() -> int\n\
-\n\
-Returns the number of bytes between the current position and the limit.");
-
-static PyObject*
-hotbuf_remaining(PyHotbufObject *self)
-{
- return PyInt_FromSsize_t(self->b_limit - self->b_position);
-}
-
-
PyDoc_STRVAR(compact__doc__,
"B.compact()\n\
\n\
@@ -867,8 +855,6 @@
static Py_ssize_t
hotbuf_length(PyHotbufObject *self)
{
- /* Note: this is the same as 'remaining'. */
- assert(self->b_position <= self->b_limit);
return self->b_limit - self->b_position;
}
@@ -997,7 +983,6 @@
{"restore", (PyCFunction)hotbuf_restore, METH_VARARGS, restore__doc__},
{"flip", (PyCFunction)hotbuf_flip, METH_NOARGS, flip__doc__},
{"rewind", (PyCFunction)hotbuf_rewind, METH_NOARGS, rewind__doc__},
- {"remaining", (PyCFunction)hotbuf_remaining, METH_NOARGS, remaining__doc__},
{"compact", (PyCFunction)hotbuf_compact, METH_NOARGS, compact__doc__},
{"getbyte", (PyCFunction)hotbuf_getbyte, METH_NOARGS, get__doc__},
{"putbyte", (PyCFunction)hotbuf_putbyte, METH_O, put__doc__},
Modified: sandbox/trunk/hotbuffer/README.txt
==============================================================================
--- sandbox/trunk/hotbuffer/README.txt (original)
+++ sandbox/trunk/hotbuffer/README.txt Fri May 26 22:21:38 2006
@@ -23,24 +23,25 @@
TODO
====
-* Make push() and pop() instead of save() and restore()
+* Remove the advbytes argument to pop()
-* Need to select between PyObject_MALLOC and PyObject_MEMMALLOC
-
-* We need to make the client more easily remove the calls to remaining()
+* Make push() and pop() instead of save() and restore(), use a C stack
+ internally.
* Make it possible to read from a file directly into a hotbuf
- - implement the file protocol (read(), write()) on the buffer object
+ - implement the file protocol (read(), write()) on the hotbuf object
- is there a file protocol?
+* We need to select between PyObject_MALLOC and PyObject_MEMMALLOC
+
+
+
Features
--------
* Use descriptors rather than function calls to set the limit, etc.
-* Add restore() to the tests.
-
* Implement pack() in C
* Change the mark, this will make the loop easier to understand
Modified: sandbox/trunk/hotbuffer/test_hotbuf.py
==============================================================================
--- sandbox/trunk/hotbuffer/test_hotbuf.py (original)
+++ sandbox/trunk/hotbuffer/test_hotbuf.py Fri May 26 22:21:38 2006
@@ -84,9 +84,9 @@
(0, 104, 42))
# Play with remaining.
- self.assertEquals(b.remaining(), 104)
+ self.assertEquals(len(b), 104)
b.setposition(10)
- self.assertEquals(b.remaining(), 94)
+ self.assertEquals(len(b), 94)
# Play with advance.
self.assertEquals(b.position, 10)
Added: sandbox/trunk/hotbuffer/use_cases.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/hotbuffer/use_cases.py Fri May 26 22:21:38 2006
@@ -0,0 +1,120 @@
+#!/usr/bin/env python
+
+"""
+Use cases for the hot buffer.
+"""
+
+
+#-------------------------------------------------------------------------------
+# Case 1: parsing netstrings, not using exceptions.
+
+ # Loop over the entire input.
+ while 1:
+ # Loop over all the messages in the current buffer.
+ while hot:
+ # Read the length and parse the message.
+ length = hot.getbyte() # No error can occur here, since we're
+ # still hot.
+ if len(hot) < length:
+ # Rollback the length byte and exit the loop to fill the buffer
+ # with new data.
+ hot.advance(-1)
+ break
+
+ # Save the current window and window around the message content.
+ hot.push(1, length)
+
+ # Parse the message.
+ #
+ # - We are insured to be able to read all the message here because
+ # we checked for the length.
+ # - Exceptions will be programming errors.
+ # - You never need to deal with rollback of your transactions.
+
+ # (your code)
+
+ # Pop the message window and advance beyond the length.
+ hot.pop()
+ hot.advance(length + 1)
+
+ # Compact and read the next chunk of the buffer.
+ hot.compact()
+ s = read(len(hot))
+ if not s:
+ break # Finished the input, exit.
+
+#-------------------------------------------------------------------------------
+# Case 2: parsing netstrings, using exceptions. This is more or less the same
+# as Case 1, but without checking the length from Python, but the length is
+# automatically checked by push().
+
+ while 1:
+ # Catch when we hit the boundary.
+ try:
+ # Loop over all the messages in the current buffer.
+ while hot:
+ # Read the length.
+ length = hot.getbyte() # This may raise
+ hot.push(1, length) # This may raise as well
+
+ # Parse the message.
+ #
+ # - We are insured to be able to read all the message here
+ # because we checked for the length.
+ # - Exceptions will be programming errors.
+ # - You never need to deal with rollback of your transactions.
+
+ # (your code)
+
+ # Pop the message window and advance beyond the length.
+ hot.pop()
+ hot.advance(length + 1)
+ else:
+ raise hotbuf.BoundaryError
+
+ except hotbuf.BoundaryError:
+ # Rollback the failed transaction.
+ hot.pop()
+
+ # Compact and read the next chunk of the buffer.
+ hot.compact()
+ s = read(len(hot))
+ if not s:
+ break # Finished the input, exit.
+
+#-------------------------------------------------------------------------------
+# Case 3: arbitrary formats, may hit the boundary, cannot detect programming
+# errors.
+
+ while 1:
+ # Catch when we hit the boundary.
+ try:
+ # Loop over all the messages in the current buffer.
+ while hot:
+ hot.push()
+
+ # Parse the message.
+ #
+ # - We are insured to be able to read all the message here
+ # because we checked for the length.
+ # - Exceptions will be programming errors.
+ # - You never need to deal with rollback of your transactions.
+
+ # (your code)
+
+ # Pop the safe window and leave the current position and limit
+ # alone.
+ hot.pop(False)
+ else:
+ raise hotbuf.BoundaryError
+
+ except hotbuf.BoundaryError:
+ # Rollback the failed transaction.
+ hot.pop()
+
+ # Compact and read the next chunk of the buffer.
+ hot.compact()
+ s = read(len(hot))
+ if not s:
+ break # Finished the input, exit.
+
More information about the Python-checkins
mailing list