[Python-checkins] r46190 - in python/branches/blais-bytebuf: Lib/test/test_hotbuf.py Modules/hotbufmodule.c

martin.blais python-checkins at python.org
Wed May 24 19:11:42 CEST 2006


Author: martin.blais
Date: Wed May 24 19:11:40 2006
New Revision: 46190

Modified:
   python/branches/blais-bytebuf/Lib/test/test_hotbuf.py
   python/branches/blais-bytebuf/Modules/hotbufmodule.c
Log:
Added getbyte/putbyte, ci before rename to add corresponding Python module

Modified: python/branches/blais-bytebuf/Lib/test/test_hotbuf.py
==============================================================================
--- python/branches/blais-bytebuf/Lib/test/test_hotbuf.py	(original)
+++ python/branches/blais-bytebuf/Lib/test/test_hotbuf.py	Wed May 24 19:11:40 2006
@@ -2,7 +2,7 @@
 #
 # $Id$
 #
-#  Copyright (C) 2005   Gregory P. Smith (greg at electricrain.com)
+#  Copyright (C) 2006   Martin Blais <blais at furius.ca>
 #  Licensed to PSF under a Contributor Agreement.
 #
 
@@ -11,18 +11,18 @@
 from test import test_support
 
 
+CAPACITY = 1024
+
 class HotbufTestCase(unittest.TestCase):
 
     def test_base( self ):
-        CAPACITY = 1024
-
         # Create a new hotbuf
         self.assertRaises(ValueError, hotbuf, -1)
         self.assertRaises(ValueError, hotbuf, 0)
         b = hotbuf(CAPACITY)
         self.assertEquals(len(b), CAPACITY)
         self.assertEquals(b.capacity(), CAPACITY)
-        
+
         # Play with the position
         assert b.position() == 0
         b.setposition(10)
@@ -39,7 +39,7 @@
 
         # Play with reset before the mark has been set.
         self.assertRaises(IndexError, b.setlimit, CAPACITY + 1)
-        
+
         # Play with the mark
         b.setposition(10)
         b.setlimit(100)
@@ -59,7 +59,7 @@
         b.flip()
         self.assertEquals((b.position(), b.limit(), b.mark()),
                           (0, 42, -1))
-        
+
         # Play with rewind.
         b.setposition(42)
         b.setlimit(104)
@@ -74,7 +74,6 @@
         self.assertEquals(b.remaining(), 94)
 
     def test_compact( self ):
-        CAPACITY = 1024
         b = hotbuf(CAPACITY)
 
         b.setposition(100)
@@ -84,12 +83,35 @@
         self.assertEquals((b.position(), b.limit(), b.mark()),
                           (100, CAPACITY, -1))
 
+    def test_byte( self ):
+        b = hotbuf(256)
+
+        # Fill up the buffer with bytes.
+        for x in xrange(256):
+            b.putbyte(x)
+
+        # Test overflow.
+        self.assertRaises(IndexError, b.putbyte, 42)
+
+        # Read all data from the buffer.
+        b.flip()
+        for x in xrange(256):
+            nx = b.getbyte()
+            print nx
+            assert nx == x
+
+        # Test underflow.
+        self.assertRaises(IndexError, b.putbyte, 42)
+
+
+
+
+
+
 
-        
 def test_main():
     test_support.run_unittest(HotbufTestCase)
 
-
 if __name__ == "__main__":
     test_main()
 

Modified: python/branches/blais-bytebuf/Modules/hotbufmodule.c
==============================================================================
--- python/branches/blais-bytebuf/Modules/hotbufmodule.c	(original)
+++ python/branches/blais-bytebuf/Modules/hotbufmodule.c	Wed May 24 19:11:40 2006
@@ -452,11 +452,6 @@
 }
 
 
-
-/* ===========================================================================
- * Object Methods (byte buffer interface)
- */
-
 PyDoc_STRVAR(compact__doc__,
 "B.compact()\n\
 \n\
@@ -512,665 +507,69 @@
 }
 
 
+
+/* ===========================================================================
+ * Object Methods (get/put methods)
+ */
 
-/*
-
-get
-
-public abstract byte get()
-
-    Relative get method. Reads the byte at this buffer's current position, and then increments the position.
-
-    Returns:
-        The byte at the buffer's current position 
-    Throws:
-        BufferUnderflowException - If the buffer's current position is not smaller than its limit
-
-put
-
-public abstract ByteBuffer put(byte b)
-
-    Relative put method  (optional operation).
-
-    Writes the given byte into this buffer at the current position, and then increments the position.
-
-    Parameters:
-        b - The byte to be written 
-    Returns:
-        This buffer 
-    Throws:
-        BufferOverflowException - If this buffer's current position is not smaller than its limit 
-        ReadOnlyBufferException - If this buffer is read-only
-
-get
-
-public abstract byte get(int index)
-
-    Absolute get method. Reads the byte at the given index.
-
-    Parameters:
-        index - The index from which the byte will be read 
-    Returns:
-        The byte at the given index 
-    Throws:
-        IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit
-
-put
-
-public abstract ByteBuffer put(int index,
-                               byte b)
-
-    Absolute put method  (optional operation).
-
-    Writes the given byte into this buffer at the given index.
-
-    Parameters:
-        index - The index at which the byte will be written
-        b - The byte value to be written 
-    Returns:
-        This buffer 
-    Throws:
-        IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit 
-        ReadOnlyBufferException - If this buffer is read-only
-
-get
-
-public ByteBuffer get(byte[] dst,
-                      int offset,
-                      int length)
-
-    Relative bulk get method.
-
-    This method transfers bytes from this buffer into the given destination array. If there are fewer bytes remaining in the buffer than are required to satisfy the request, that is, if length > remaining(), then no bytes are transferred and a BufferUnderflowException is thrown.
-
-    Otherwise, this method copies length bytes from this buffer into the given array, starting at the current position of this buffer and at the given offset in the array. The position of this buffer is then incremented by length.
-
-    In other words, an invocation of this method of the form src.get(dst, off, len) has exactly the same effect as the loop
-
-         for (int i = off; i < off + len; i++)
-             dst[i] = src.get(); 
-
-    except that it first checks that there are sufficient bytes in this buffer and it is potentially much more efficient.
-
-    Parameters:
-        dst - The array into which bytes are to be written
-        offset - The offset within the array of the first byte to be written; must be non-negative and no larger than dst.length
-        length - The maximum number of bytes to be written to the given array; must be non-negative and no larger than dst.length - offset 
-    Returns:
-        This buffer 
-    Throws:
-        BufferUnderflowException - If there are fewer than length bytes remaining in this buffer 
-        IndexOutOfBoundsException - If the preconditions on the offset and length parameters do not hold
-
-get
-
-public ByteBuffer get(byte[] dst)
-
-    Relative bulk get method.
-
-    This method transfers bytes from this buffer into the given destination array. An invocation of this method of the form src.get(a) behaves in exactly the same way as the invocation
-
-         src.get(a, 0, a.length) 
-
-    Returns:
-        This buffer 
-    Throws:
-        BufferUnderflowException - If there are fewer than length bytes remaining in this buffer
-
-put
-
-public ByteBuffer put(ByteBuffer src)
-
-    Relative bulk put method  (optional operation).
-
-    This method transfers the bytes remaining in the given source buffer into this buffer. If there are more bytes remaining in the source buffer than in this buffer, that is, if src.remaining() > remaining(), then no bytes are transferred and a BufferOverflowException is thrown.
-
-    Otherwise, this method copies n = src.remaining() bytes from the given buffer into this buffer, starting at each buffer's current position. The positions of both buffers are then incremented by n.
-
-    In other words, an invocation of this method of the form dst.put(src) has exactly the same effect as the loop
-
-         while (src.hasRemaining())
-             dst.put(src.get()); 
-
-    except that it first checks that there is sufficient space in this buffer and it is potentially much more efficient.
-
-    Parameters:
-        src - The source buffer from which bytes are to be read; must not be this buffer 
-    Returns:
-        This buffer 
-    Throws:
-        BufferOverflowException - If there is insufficient space in this buffer for the remaining bytes in the source buffer 
-        IllegalArgumentException - If the source buffer is this buffer 
-        ReadOnlyBufferException - If this buffer is read-only
-
-put
-
-public ByteBuffer put(byte[] src,
-                      int offset,
-                      int length)
-
-    Relative bulk put method  (optional operation).
-
-    This method transfers bytes into this buffer from the given source array. If there are more bytes to be copied from the array than remain in this buffer, that is, if length > remaining(), then no bytes are transferred and a BufferOverflowException is thrown.
-
-    Otherwise, this method copies length bytes from the given array into this buffer, starting at the given offset in the array and at the current position of this buffer. The position of this buffer is then incremented by length.
-
-    In other words, an invocation of this method of the form dst.put(src, off, len) has exactly the same effect as the loop
-
-         for (int i = off; i < off + len; i++)
-             dst.put(a[i]); 
-
-    except that it first checks that there is sufficient space in this buffer and it is potentially much more efficient.
-
-    Parameters:
-        src - The array from which bytes are to be read
-        offset - The offset within the array of the first byte to be read; must be non-negative and no larger than array.length
-        length - The number of bytes to be read from the given array; must be non-negative and no larger than array.length - offset 
-    Returns:
-        This buffer 
-    Throws:
-        BufferOverflowException - If there is insufficient space in this buffer 
-        IndexOutOfBoundsException - If the preconditions on the offset and length parameters do not hold 
-        ReadOnlyBufferException - If this buffer is read-only
-
-put
-
-public final ByteBuffer put(byte[] src)
-
-    Relative bulk put method  (optional operation).
-
-    This method transfers the entire content of the given source byte array into this buffer. An invocation of this method of the form dst.put(a) behaves in exactly the same way as the invocation
-
-         dst.put(a, 0, a.length) 
-
-    Returns:
-        This buffer 
-    Throws:
-        BufferOverflowException - If there is insufficient space in this buffer 
-        ReadOnlyBufferException - If this buffer is read-only
-
-getChar
-
-public abstract char getChar()
-
-    Relative get method for reading a char value.
-
-    Reads the next two bytes at this buffer's current position, composing them into a char value according to the current byte order, and then increments the position by two.
-
-    Returns:
-        The char value at the buffer's current position 
-    Throws:
-        BufferUnderflowException - If there are fewer than two bytes remaining in this buffer
-
-putChar
-
-public abstract ByteBuffer putChar(char value)
-
-    Relative put method for writing a char value  (optional operation).
-
-    Writes two bytes containing the given char value, in the current byte order, into this buffer at the current position, and then increments the position by two.
-
-    Parameters:
-        value - The char value to be written 
-    Returns:
-        This buffer 
-    Throws:
-        BufferOverflowException - If there are fewer than two bytes remaining in this buffer 
-        ReadOnlyBufferException - If this buffer is read-only
-
-getChar
-
-public abstract char getChar(int index)
-
-    Absolute get method for reading a char value.
-
-    Reads two bytes at the given index, composing them into a char value according to the current byte order.
-
-    Parameters:
-        index - The index from which the bytes will be read 
-    Returns:
-        The char value at the given index 
-    Throws:
-        IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit, minus one
-
-putChar
-
-public abstract ByteBuffer putChar(int index,
-                                   char value)
-
-    Absolute put method for writing a char value  (optional operation).
-
-    Writes two bytes containing the given char value, in the current byte order, into this buffer at the given index.
-
-    Parameters:
-        index - The index at which the bytes will be written
-        value - The char value to be written 
-    Returns:
-        This buffer 
-    Throws:
-        IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit, minus one 
-        ReadOnlyBufferException - If this buffer is read-only
-
-asCharBuffer
-
-public abstract CharBuffer asCharBuffer()
-
-    Creates a view of this byte buffer as a char buffer.
-
-    The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.
-
-    The new buffer's position will be zero, its capacity and its limit will be the number of bytes remaining in this buffer divided by two, and its mark will be undefined. The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only.
-
-    Returns:
-        A new char buffer
-
-getShort
-
-public abstract short getShort()
-
-    Relative get method for reading a short value.
-
-    Reads the next two bytes at this buffer's current position, composing them into a short value according to the current byte order, and then increments the position by two.
-
-    Returns:
-        The short value at the buffer's current position 
-    Throws:
-        BufferUnderflowException - If there are fewer than two bytes remaining in this buffer
-
-putShort
-
-public abstract ByteBuffer putShort(short value)
-
-    Relative put method for writing a short value  (optional operation).
-
-    Writes two bytes containing the given short value, in the current byte order, into this buffer at the current position, and then increments the position by two.
-
-    Parameters:
-        value - The short value to be written 
-    Returns:
-        This buffer 
-    Throws:
-        BufferOverflowException - If there are fewer than two bytes remaining in this buffer 
-        ReadOnlyBufferException - If this buffer is read-only
-
-getShort
-
-public abstract short getShort(int index)
-
-    Absolute get method for reading a short value.
-
-    Reads two bytes at the given index, composing them into a short value according to the current byte order.
-
-    Parameters:
-        index - The index from which the bytes will be read 
-    Returns:
-        The short value at the given index 
-    Throws:
-        IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit, minus one
-
-putShort
-
-public abstract ByteBuffer putShort(int index,
-                                    short value)
-
-    Absolute put method for writing a short value  (optional operation).
-
-    Writes two bytes containing the given short value, in the current byte order, into this buffer at the given index.
-
-    Parameters:
-        index - The index at which the bytes will be written
-        value - The short value to be written 
-    Returns:
-        This buffer 
-    Throws:
-        IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit, minus one 
-        ReadOnlyBufferException - If this buffer is read-only
-
-asShortBuffer
-
-public abstract ShortBuffer asShortBuffer()
-
-    Creates a view of this byte buffer as a short buffer.
-
-    The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.
-
-    The new buffer's position will be zero, its capacity and its limit will be the number of bytes remaining in this buffer divided by two, and its mark will be undefined. The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only.
-
-    Returns:
-        A new short buffer
-
-getInt
-
-public abstract int getInt()
-
-    Relative get method for reading an int value.
-
-    Reads the next four bytes at this buffer's current position, composing them into an int value according to the current byte order, and then increments the position by four.
-
-    Returns:
-        The int value at the buffer's current position 
-    Throws:
-        BufferUnderflowException - If there are fewer than four bytes remaining in this buffer
-
-putInt
-
-public abstract ByteBuffer putInt(int value)
-
-    Relative put method for writing an int value  (optional operation).
-
-    Writes four bytes containing the given int value, in the current byte order, into this buffer at the current position, and then increments the position by four.
-
-    Parameters:
-        value - The int value to be written 
-    Returns:
-        This buffer 
-    Throws:
-        BufferOverflowException - If there are fewer than four bytes remaining in this buffer 
-        ReadOnlyBufferException - If this buffer is read-only
-
-getInt
-
-public abstract int getInt(int index)
-
-    Absolute get method for reading an int value.
-
-    Reads four bytes at the given index, composing them into a int value according to the current byte order.
-
-    Parameters:
-        index - The index from which the bytes will be read 
-    Returns:
-        The int value at the given index 
-    Throws:
-        IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit, minus three
-
-putInt
-
-public abstract ByteBuffer putInt(int index,
-                                  int value)
-
-    Absolute put method for writing an int value  (optional operation).
-
-    Writes four bytes containing the given int value, in the current byte order, into this buffer at the given index.
-
-    Parameters:
-        index - The index at which the bytes will be written
-        value - The int value to be written 
-    Returns:
-        This buffer 
-    Throws:
-        IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit, minus three 
-        ReadOnlyBufferException - If this buffer is read-only
-
-asIntBuffer
-
-public abstract IntBuffer asIntBuffer()
-
-    Creates a view of this byte buffer as an int buffer.
-
-    The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.
-
-    The new buffer's position will be zero, its capacity and its limit will be the number of bytes remaining in this buffer divided by four, and its mark will be undefined. The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only.
-
-    Returns:
-        A new int buffer
-
-getLong
-
-public abstract long getLong()
-
-    Relative get method for reading a long value.
-
-    Reads the next eight bytes at this buffer's current position, composing them into a long value according to the current byte order, and then increments the position by eight.
-
-    Returns:
-        The long value at the buffer's current position 
-    Throws:
-        BufferUnderflowException - If there are fewer than eight bytes remaining in this buffer
-
-putLong
-
-public abstract ByteBuffer putLong(long value)
-
-    Relative put method for writing a long value  (optional operation).
-
-    Writes eight bytes containing the given long value, in the current byte order, into this buffer at the current position, and then increments the position by eight.
-
-    Parameters:
-        value - The long value to be written 
-    Returns:
-        This buffer 
-    Throws:
-        BufferOverflowException - If there are fewer than eight bytes remaining in this buffer 
-        ReadOnlyBufferException - If this buffer is read-only
-
-getLong
-
-public abstract long getLong(int index)
-
-    Absolute get method for reading a long value.
-
-    Reads eight bytes at the given index, composing them into a long value according to the current byte order.
-
-    Parameters:
-        index - The index from which the bytes will be read 
-    Returns:
-        The long value at the given index 
-    Throws:
-        IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit, minus seven
-
-putLong
-
-public abstract ByteBuffer putLong(int index,
-                                   long value)
-
-    Absolute put method for writing a long value  (optional operation).
-
-    Writes eight bytes containing the given long value, in the current byte order, into this buffer at the given index.
-
-    Parameters:
-        index - The index at which the bytes will be written
-        value - The long value to be written 
-    Returns:
-        This buffer 
-    Throws:
-        IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit, minus seven 
-        ReadOnlyBufferException - If this buffer is read-only
-
-asLongBuffer
-
-public abstract LongBuffer asLongBuffer()
-
-    Creates a view of this byte buffer as a long buffer.
-
-    The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.
-
-    The new buffer's position will be zero, its capacity and its limit will be the number of bytes remaining in this buffer divided by eight, and its mark will be undefined. The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only.
-
-    Returns:
-        A new long buffer
-
-getFloat
-
-public abstract float getFloat()
-
-    Relative get method for reading a float value.
-
-    Reads the next four bytes at this buffer's current position, composing them into a float value according to the current byte order, and then increments the position by four.
-
-    Returns:
-        The float value at the buffer's current position 
-    Throws:
-        BufferUnderflowException - If there are fewer than four bytes remaining in this buffer
-
-putFloat
-
-public abstract ByteBuffer putFloat(float value)
-
-    Relative put method for writing a float value  (optional operation).
-
-    Writes four bytes containing the given float value, in the current byte order, into this buffer at the current position, and then increments the position by four.
-
-    Parameters:
-        value - The float value to be written 
-    Returns:
-        This buffer 
-    Throws:
-        BufferOverflowException - If there are fewer than four bytes remaining in this buffer 
-        ReadOnlyBufferException - If this buffer is read-only
-
-getFloat
-
-public abstract float getFloat(int index)
-
-    Absolute get method for reading a float value.
-
-    Reads four bytes at the given index, composing them into a float value according to the current byte order.
-
-    Parameters:
-        index - The index from which the bytes will be read 
-    Returns:
-        The float value at the given index 
-    Throws:
-        IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit, minus three
-
-putFloat
-
-public abstract ByteBuffer putFloat(int index,
-                                    float value)
-
-    Absolute put method for writing a float value  (optional operation).
-
-    Writes four bytes containing the given float value, in the current byte order, into this buffer at the given index.
-
-    Parameters:
-        index - The index at which the bytes will be written
-        value - The float value to be written 
-    Returns:
-        This buffer 
-    Throws:
-        IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit, minus three 
-        ReadOnlyBufferException - If this buffer is read-only
-
-asFloatBuffer
-
-public abstract FloatBuffer asFloatBuffer()
-
-    Creates a view of this byte buffer as a float buffer.
-
-    The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.
-
-    The new buffer's position will be zero, its capacity and its limit will be the number of bytes remaining in this buffer divided by four, and its mark will be undefined. The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only.
-
-    Returns:
-        A new float buffer
-
-getDouble
-
-public abstract double getDouble()
-
-    Relative get method for reading a double value.
-
-    Reads the next eight bytes at this buffer's current position, composing them into a double value according to the current byte order, and then increments the position by eight.
-
-    Returns:
-        The double value at the buffer's current position 
-    Throws:
-        BufferUnderflowException - If there are fewer than eight bytes remaining in this buffer
-
-putDouble
-
-public abstract ByteBuffer putDouble(double value)
-
-    Relative put method for writing a double value  (optional operation).
-
-    Writes eight bytes containing the given double value, in the current byte order, into this buffer at the current position, and then increments the position by eight.
-
-    Parameters:
-        value - The double value to be written 
-    Returns:
-        This buffer 
-    Throws:
-        BufferOverflowException - If there are fewer than eight bytes remaining in this buffer 
-        ReadOnlyBufferException - If this buffer is read-only
-
-getDouble
-
-public abstract double getDouble(int index)
-
-    Absolute get method for reading a double value.
-
-    Reads eight bytes at the given index, composing them into a double value according to the current byte order.
-
-    Parameters:
-        index - The index from which the bytes will be read 
-    Returns:
-        The double value at the given index 
-    Throws:
-        IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit, minus seven
-
-putDouble
-
-public abstract ByteBuffer putDouble(int index,
-                                     double value)
-
-    Absolute put method for writing a double value  (optional operation).
-
-    Writes eight bytes containing the given double value, in the current byte order, into this buffer at the given index.
-
-    Parameters:
-        index - The index at which the bytes will be written
-        value - The double value to be written 
-    Returns:
-        This buffer 
-    Throws:
-        IndexOutOfBoundsException - If index is negative or not smaller than the buffer's limit, minus seven 
-        ReadOnlyBufferException - If this buffer is read-only
-
-asDoubleBuffer
-
-public abstract DoubleBuffer asDoubleBuffer()
-
-    Creates a view of this byte buffer as a double buffer.
-
-    The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.
-
-    The new buffer's position will be zero, its capacity and its limit will be the number of bytes remaining in this buffer divided by eight, and its mark will be undefined. The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only.
-
-    Returns:
-        A new double buffer
-
-*/
-
-
-
-/*
-
-order
-
-public final ByteOrder order()
-
-    Retrieves this buffer's byte order.
-
-    The byte order is used when reading or writing multibyte values,
-    and when creating buffers that are views of this byte buffer. The
-    order of a newly-created byte buffer is always BIG_ENDIAN.
+PyDoc_STRVAR(relative_get__doc__,
+"B.get*() -> data\n\
+\n\
+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.");
+
+PyDoc_STRVAR(relative_put__doc__,
+"B.put*(data)\n\
+\n\
+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.");
+
+
+/* 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,                       \
+                        "attempted read beyond buffer limit");  \
+        return NULL;                                            \
+    }
 
-    Returns:
-        This buffer's byte order
 
+static PyObject*
+hotbuf_getbyte(PyHotbufObject *self)
+{
+    unsigned char byte;
+    CHECK_LIMIT_ERROR(sizeof(byte));
 
-order
+    byte = *(unsigned char*)(self->b_ptr + self->b_position);
+    self->b_position += sizeof(byte);
+    return PyInt_FromLong(byte);
+}
 
-public final ByteBuffer order(ByteOrder bo)
+static PyObject*
+hotbuf_putbyte(PyHotbufObject *self, PyObject* arg)
+{
+    int byte_i;
+    unsigned char byte;
 
-    Modifies this buffer's byte order.
+    byte_i = PyInt_AsLong(arg);
+    if ( byte_i > 255 ) {
+        PyErr_SetString(PyExc_ValueError,
+                        "overflow for byte");
+        return NULL;
+    }
+    byte = (unsigned char)byte_i;
 
-    Parameters:
-        bo - The new byte order, either BIG_ENDIAN or LITTLE_ENDIAN 
-    Returns:
-        This buffer
+    CHECK_LIMIT_ERROR(sizeof(byte));
+    *(unsigned char*)(self->b_ptr + self->b_position) = byte;
+    self->b_position += sizeof(byte);
+    return Py_None;
+}
 
-*/
 
 
 
@@ -1265,6 +664,9 @@
 	{"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, relative_get__doc__},
+	{"putbyte", (PyCFunction)hotbuf_putbyte, METH_O, relative_put__doc__},
 	{NULL, NULL} /* sentinel */
 };
 


More information about the Python-checkins mailing list