Class Py2kBuffer

  • All Implemented Interfaces:
    java.io.Serializable, BufferProtocol

    public class Py2kBuffer
    extends PySequence
    implements BufferProtocol
    Implementation of the Python buffer type. buffer is being superseded in Python 2.7 by memoryview, and is provided here to support legacy Python code. Use memoryview if you can.

    buffer and memoryview both wrap the same Jython buffer API: that designed for memoryview. In CPython, a new C API (which Jython's resembles) was introduced with memoryview. Because of this, buffer and memoryview may be supplied as arguments in the same places, and will accept as arguments the same (one-dimensional byte-array) types. Their behaviour differs as detailed in the documentation.

    See Also:
    Serialized Form
    • Field Detail

      • TYPE

        public static final PyType TYPE
    • Constructor Detail

      • Py2kBuffer

        public Py2kBuffer​(BufferProtocol object,
                          int offset,
                          int size)
        Construct a Py2kBuffer from an object supporting the BufferProtocol. The buffer takes no lease on the PyBuffer at present, but for each action performed obtains a new one and releases it. (Major difference from memoryview.) Note that when size=-1 is given, the buffer reflects the changing size of the underlying object.
        Parameters:
        object - the object on which this is to be a buffer.
        offset - into the array exposed by the object (0 for start).
        size - of the slice or -1 for all of the object.
    • Method Detail

      • __len__

        public int __len__()
        Description copied from class: PyObject
        Equivalent to the standard Python __len__ method. Part of the mapping discipline.
        Overrides:
        __len__ in class PyObject
        Returns:
        the length of the object
      • __repr__

        public PyString __repr__()
        Description copied from class: PyObject
        Equivalent to the standard Python __repr__ method. This method should not typically need to be overrriden. The easiest way to configure the string representation of a PyObject is to override the standard Java toString method.
        Overrides:
        __repr__ in class PyObject
      • __str__

        public PyString __str__()
        Description copied from class: PyObject
        Equivalent to the standard Python __str__ method. This method should not typically need to be overridden. The easiest way to configure the string representation of a PyObject is to override the standard Java toString method.
        Overrides:
        __str__ in class PyObject
      • __add__

        public PyObject __add__​(PyObject other)
        Equivalent to the standard Python __add__ method. A buffer implements this as concatenation and returns a str (PyString) result.
        Overrides:
        __add__ in class PyObject
        Parameters:
        other - the object to perform this binary operation with (the right-hand operand).
        Returns:
        the result of the add, or null if this operation is not defined.
      • __mul__

        public PyObject __mul__​(PyObject o)
        Equivalent to the standard Python __mul__ method. On a buffer it returns a str containing the buffer contents n times.
        Overrides:
        __mul__ in class PyObject
        Parameters:
        o - the object to perform this binary operation with (the right-hand operand).
        Returns:
        the result of the mul, or null if this operation is not defined
      • __rmul__

        public PyObject __rmul__​(PyObject o)
        Equivalent to the standard Python __rmul__ method. On a buffer it returns a str containing the buffer contents n times.
        Overrides:
        __rmul__ in class PyObject
        Parameters:
        o - the object to perform this binary operation with (the left-hand operand).
        Returns:
        the result of the mul, or null if this operation is not defined.
      • getBuffer

        public PyBuffer getBuffer​(int flags)
        Method by which the consumer requests the buffer from the exporter. The consumer provides information on its intended method of navigation and the features the buffer object is asked (or assumed) to provide. Each consumer requesting a buffer in this way, when it has finished using it, should make a corresponding call to PyBuffer.release() on the buffer it obtained, since some objects alter their behaviour while buffers are exported.

        The PyBuffer returned from this method is provided directly by the underlying object on which this buffer was constructed, taking account of the slicing arguments (offset and size), if these were given when the buffer was constructed.

        Specified by:
        getBuffer in interface BufferProtocol
        Parameters:
        flags - specifying features demanded and the navigational capabilities of the consumer
        Returns:
        exported buffer
      • pyset

        public void pyset​(int index,
                          PyObject value)
                   throws PyException
        Sets the indexed element of the buffer to the given value, treating the operation as assignment to a slice of length one. This is different from the same operation on a byte array, where the assigned value must be an int: here it must have the buffer API and length one. This is an extension point called by PySequence in its implementation of PySequence.__setitem__(int, org.python.core.PyObject) It is guaranteed by PySequence that the index is within the bounds of the buffer. Any other clients calling pyset(int, PyObject) must make the same guarantee.
        Parameters:
        index - index of the element to set.
        value - to set this element to, regarded as a buffer of length one unit.
        Throws:
        PyException(AttributeError) - if value cannot be converted to an integer
        PyException(ValueError) - if value<0 or value>255
        PyException