Top Description Fields Constructors Methods
org.python.core

public Class Py2kBuffer

extends PySequence
implements BufferProtocol
Class Inheritance
All Implemented Interfaces
org.python.core.BufferProtocol
Annotations
@Untraversable
@ExposedType
name:buffer
doc:buffer(object [, offset[, size]]) Create a new buffer object which references the given object. The buffer will reference a slice of the target object from the start of the object (or at the specified offset). The slice will extend to the end of the target object (or with the specified size).
base:PyObject
isBaseType:false
Imports
org.python.core.buffer.BaseBuffer, .SimpleStringBuffer, org.python.core.util.StringUtil, org.python.expose.ExposedMethod, .ExposedNew, .ExposedType, .MethodType

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.

Field Summary

Modifier and TypeField and Description
private final BufferProtocol
object

The underlying object on which the buffer was created.

private final int
offset

The offset (in bytes) into the offered object at which the buffer starts.

private static String[]
paramNames

Names of arguments in the constructor (for ArgParser).

private final int
size

Number of bytes to include in the buffer (or -1 for all available).

private static final String
private static final String
public static final PyType
Inherited from org.python.core.PySequence:
delegator

Constructor Summary

AccessConstructor and Description
public
Py2kBuffer(BufferProtocol
the object on which this is to be a buffer.
object
,
int
into the array exposed by the object (0 for start).
offset
,
int
of the slice or -1 for all of the object.
size
)

Construct a Py2kBuffer from an object supporting the BufferProtocol.

Method Summary

Modifier and TypeMethod and Description
public PyObject
__add__(PyObject
the object to perform this binary operation with (the right-hand operand).
other
)

Overrides org.python.core.PyObject.__add__.

Equivalent to the standard Python __add__ method.

public int
__len__()

Overrides org.python.core.PyObject.__len__.

Equivalent to the standard Python __len__ method.

public PyObject
__mul__(PyObject
the object to perform this binary operation with (the right-hand operand).
o
)

Overrides org.python.core.PyObject.__mul__.

Equivalent to the standard Python __mul__ method.

public PyString
__repr__()

Overrides org.python.core.PyObject.__repr__.

Equivalent to the standard Python __repr__ method.

public PyObject
__rmul__(PyObject
the object to perform this binary operation with (the left-hand operand).
o
)

Overrides org.python.core.PyObject.__rmul__.

Equivalent to the standard Python __rmul__ method.

public PyString
__str__()

Overrides org.python.core.PyObject.__str__.

Equivalent to the standard Python __str__ method.

private static BufferProtocol

Returns:

PyObject supporting BufferProtocol, if not null.
asBufferableOrNull
(PyObject
the object to access.
obj
)

Return a PyObject bearing the interface BufferProtocol and equivalent to the argument, or return null.

pack-priv final PyObject
pack-priv final PyObject
pack-priv final PyString
pack-priv final PyObject
pack-priv final PyString
private int

Returns:

1, 0 or -1 as this>b, this==b, or this<b respectively, or -2 if the comparison is not implemented
buffer_cmp
(PyObject b)

Comparison function between this buffer and any other object.

private int

Returns:

0 if this==b, or +1 or -1 if this!=b, or -2 if the comparison is not implemented
buffer_cmpeq
(PyObject b)

Fail-fast comparison function between byte array types and any other object, for when the test is only for equality.

pack-priv static PyObject
buffer_new(PyNewWrapper new_, boolean init, PyType subtype, PyObject[] args, String[] keywords)

private static int

Returns:

1, 0 or -1 as a>b, a==b, or a<b respectively
compare
(PyBuffer
left-hand wrapped array in the comparison
a
,
PyBuffer
right-hand wrapped object in the comparison
b
)

Comparison function between two buffers of bytes, returning 1, 0 or -1 as a>b, a==b, or a<b respectively.

private PyBuffer

Returns:

a PyBuffer onto the specified slice.
getBuffer
()

Every action on the buffer must obtain a new PyBuffer reflecting (this buffer's slice of) the contents of the backing object.

public PyBuffer
getBuffer(int
specifying features demanded and the navigational capabilities of the consumer
flags
)

Implements org.python.core.BufferProtocol.getBuffer.

Method by which the consumer requests the buffer from the exporter.

protected synchronized PyString

Returns:

a PyString corresponding the the given range of elements.
getslice
(int
the position of the first element.
start
,
int
one more than the position of the last element.
stop
,
int
the step size.
step
)

Implements abstract org.python.core.PySequence.getslice.

Returns a slice of elements from this sequence as a PyString.

protected PyString

Returns:

one-character string formed from the byte at the index
pyget
(int
index of the element to get.
index
)

Implements abstract org.python.core.PySequence.pyget.

Gets the indexed element of the buffer as a one byte string.

public synchronized void
pyset(int
index of the element to set.
index
,
PyObject
to set this element to, regarded as a buffer of length one unit.
value
)

Overrides org.python.core.PySequence.pyset.

Sets the indexed element of the buffer to the given value, treating the operation as assignment to a slice of length one.

protected synchronized PyString

Returns:

a PyString repeating this buffer (as a str) that many times
repeat
(int
the number of times to repeat this.
count
)

Implements abstract org.python.core.PySequence.repeat.

buffer*int represents repetition in Python, and returns a str ( bytes) object.

protected synchronized void
setslice(int
the position of the first element.
start
,
int
one more than the position of the last element.
stop
,
int
the step size.
step
,
PyObject
an object consistent with the slice assignment
value
)

Overrides org.python.core.PySequence.setslice.

Sets the given range of elements according to Python slice assignment semantics.

public String
toString()

Overrides org.python.core.PyObject.toString.

Returns a string representation of the object.

Inherited from org.python.core.PySequence:
__delitem____delslice____eq____finditem____finditem____ge____getitem____getslice____gt____iter____le____lt____ne____nonzero____setitem____setitem____setslice____tojava__boundToSequencecmpdeldelRangedelslicefastSequenceisMappingTypeisNumberTypeisSequenceTypeisSubTyperunsupportedopMessageseq___delitem__seq___delslice__seq___eq__seq___finditem__seq___finditem__seq___ge__seq___getitem__seq___getslice__seq___gt__seq___iter__seq___le__seq___lt__seq___ne__seq___nonzero__seq___setitem__seq___setslice__sliceLengthunsupportedopMessage

Field Detail

objectback to summary
private final BufferProtocol object

The underlying object on which the buffer was created.

offsetback to summary
private final int offset

The offset (in bytes) into the offered object at which the buffer starts.

paramNamesback to summary
private static String[] paramNames

Names of arguments in the constructor (for ArgParser).

sizeback to summary
private final int size

Number of bytes to include in the buffer (or -1 for all available).

tobytes_docback to summary
private static final String tobytes_doc
tolist_docback to summary
private static final String tolist_doc
TYPEback to summary
public static final PyType TYPE

Hides org.python.core.PyObject.TYPE.

Constructor Detail

Py2kBufferback to summary
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:BufferProtocol

the object on which this is to be a buffer.

offset:int

into the array exposed by the object (0 for start).

size:int

of the slice or -1 for all of the object.

Method Detail

__add__back to summary
public PyObject __add__(PyObject other)

Overrides org.python.core.PyObject.__add__.

Doc from org.python.core.PyObject.__add__.

Equivalent to the standard Python __add__ method. A buffer implements this as concatenation and returns a str (PyString) result.

Parameters
other:PyObject

the object to perform this binary operation with (the right-hand operand).

Returns:PyObject

the result of the add, or null if this operation is not defined.

Annotations
@Override

__len__back to summary
public int __len__()

Overrides org.python.core.PyObject.__len__.

Doc from org.python.core.PyObject.__len__.

Equivalent to the standard Python __len__ method. Part of the mapping discipline.

Returns:int

the length of the object

Annotations
@Override

__mul__back to summary
public PyObject __mul__(PyObject o)

Overrides org.python.core.PyObject.__mul__.

Doc from org.python.core.PyObject.__mul__.

Equivalent to the standard Python __mul__ method. On a buffer it returns a str containing the buffer contents n times.

Parameters
o:PyObject

the object to perform this binary operation with (the right-hand operand).

Returns:PyObject

the result of the mul, or null if this operation is not defined

Annotations
@Override

__repr__back to summary
public PyString __repr__()

Overrides org.python.core.PyObject.__repr__.

Doc from org.python.core.PyObject.__repr__.

Equivalent to the standard Python __repr__ method. Each sub-class of PyObject is likely to re-define this method to provide for its own reproduction.

Annotations
@Override
__rmul__back to summary
public PyObject __rmul__(PyObject o)

Overrides org.python.core.PyObject.__rmul__.

Doc from org.python.core.PyObject.__rmul__.

Equivalent to the standard Python __rmul__ method. On a buffer it returns a str containing the buffer contents n times.

Parameters
o:PyObject

the object to perform this binary operation with (the left-hand operand).

Returns:PyObject

the result of the mul, or null if this operation is not defined.

Annotations
@Override

__str__back to summary
public PyString __str__()

Overrides org.python.core.PyObject.__str__.

Doc from org.python.core.PyObject.__str__.

Equivalent to the standard Python __str__ method. The default implementation (in PyObject) calls __repr__(), making it unnecessary to override __str__ in sub-classes of PyObject where both forms are the same. A common choice is to provide the same implementation to __str__ and toString, for consistency in the printed form of objects between Python and Java.

Annotations
@Override
asBufferableOrNullback to summary
private static BufferProtocol asBufferableOrNull(PyObject obj)

Return a PyObject bearing the interface BufferProtocol and equivalent to the argument, or return null. This is a helper function to those methods that accept a range of types supporting the buffer API. Normally the return is exactly the argument, except in the case of a PyUnicode, which will be converted to a PyString according to Py2k semantics, equivalent to a UTF16BE encoding to bytes (for Py2k compatibility).

Parameters
obj:PyObject

the object to access.

Returns:BufferProtocol

PyObject supporting BufferProtocol, if not null.

buffer___add__back to summary
pack-priv final PyObject buffer___add__(PyObject other)
Annotations
@ExposedMethod
type:BINARY
doc:x.__add__(y) <==> x+y
buffer___mul__back to summary
pack-priv final PyObject buffer___mul__(PyObject o)
Annotations
@ExposedMethod
type:BINARY
doc:x.__mul__(n) <==> x*n
buffer___repr__back to summary
pack-priv final PyString buffer___repr__()
Annotations
@ExposedMethod
doc:x.__repr__() <==> repr(x)
buffer___rmul__back to summary
pack-priv final PyObject buffer___rmul__(PyObject o)
Annotations
@ExposedMethod
type:BINARY
doc:x.__rmul__(n) <==> n*x
buffer___str__back to summary
pack-priv final PyString buffer___str__()
Annotations
@ExposedMethod
doc:x.__str__() <==> str(x)
buffer_cmpback to summary
private int buffer_cmp(PyObject b)

Comparison function between this buffer and any other object. The inequality comparison operators are based on this.

Returns:int

1, 0 or -1 as this>b, this==b, or this<b respectively, or -2 if the comparison is not implemented

buffer_cmpeqback to summary
private int buffer_cmpeq(PyObject b)

Fail-fast comparison function between byte array types and any other object, for when the test is only for equality. The inequality comparison operators __eq__ and __ne__ are based on this.

Returns:int

0 if this==b, or +1 or -1 if this!=b, or -2 if the comparison is not implemented

buffer_newback to summary
pack-priv static PyObject buffer_new(PyNewWrapper new_, boolean init, PyType subtype, PyObject[] args, String[] keywords)
Annotations
@ExposedNew
compareback to summary
private static int compare(PyBuffer a, PyBuffer b)

Comparison function between two buffers of bytes, returning 1, 0 or -1 as a>b, a==b, or a<b respectively. The comparison is by value, using Python unsigned byte conventions, left-to-right (low to high index). Zero bytes are significant, even at the end of the array: [65,66,67]<"ABC\0", for example and [] is less than every non-empty b, while []=="".

Parameters
a:PyBuffer

left-hand wrapped array in the comparison

b:PyBuffer

right-hand wrapped object in the comparison

Returns:int

1, 0 or -1 as a>b, a==b, or a<b respectively

getBufferback to summary
private PyBuffer getBuffer()

Every action on the buffer must obtain a new PyBuffer reflecting (this buffer's slice of) the contents of the backing object.

Returns:PyBuffer

a PyBuffer onto the specified slice.

getBufferback to summary
public PyBuffer getBuffer(int flags)

Implements org.python.core.BufferProtocol.getBuffer.

Doc from org.python.core.BufferProtocol.getBuffer.

Method by which the consumer requests the buffer from the exporter. The consumer provides information on its ability to understand buffer navigation. 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, or PyBuffer#close() using try-with-resources, 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.

Parameters
flags:int

specifying features demanded and the navigational capabilities of the consumer

Returns:PyBuffer

exported buffer

Annotations
@Override

getsliceback to summary
protected synchronized PyString getslice(int start, int stop, int step)

Implements abstract org.python.core.PySequence.getslice.

Returns a slice of elements from this sequence as a PyString.

Parameters
start:int

the position of the first element.

stop:int

one more than the position of the last element.

step:int

the step size.

Returns:PyString

a PyString corresponding the the given range of elements.

Annotations
@Override

pygetback to summary
protected PyString pyget(int index)

Implements abstract org.python.core.PySequence.pyget.

Gets the indexed element of the buffer as a one byte string. This is an extension point called by PySequence in its implementation of __getitem__. It is guaranteed by PySequence that the index is within the bounds of the buffer.

Parameters
index:int

index of the element to get.

Returns:PyString

one-character string formed from the byte at the index

Annotations
@Override

pysetback to summary
public synchronized void pyset(int index, PyObject value) throws PyException

Overrides org.python.core.PySequence.pyset.

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 __setitem__ 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:int

index of the element to set.

value:PyObject

to set this element to, regarded as a buffer of length one unit.

Annotations
@Override
Exceptions
PyException:
  • AttributeError if value cannot be converted to an integer
  • ValueError if value<0 or value%gt;255
repeatback to summary
protected synchronized PyString repeat(int count)

Implements abstract org.python.core.PySequence.repeat.

buffer*int represents repetition in Python, and returns a str ( bytes) object.

Parameters
count:int

the number of times to repeat this.

Returns:PyString

a PyString repeating this buffer (as a str) that many times

Annotations
@Override

setsliceback to summary
protected synchronized void setslice(int start, int stop, int step, PyObject value)

Overrides org.python.core.PySequence.setslice.

Sets the given range of elements according to Python slice assignment semantics. If the step size is one, it is a simple slice and the operation is equivalent to replacing that slice, with the value, accessing the value via the buffer protocol.

a = bytearray(b'abcdefghijklmnopqrst')
m = buffer(a)
m[2:7] = "ABCDE"
Results in a=bytearray(b'abABCDEhijklmnopqrst').

If the step size is one, but stop-start does not match the length of the right-hand-side a ValueError is thrown.

If the step size is not one, and start!=stop, the slice defines a certain number of elements to be replaced. This function is not available in Python 2.7 (but it is in Python 3.3).

a = bytearray(b'abcdefghijklmnopqrst')
a[2:12:2] = iter( [65, 66, 67, long(68), "E"] )
Results in a=bytearray(b'abAdBfChDjElmnopqrst') in Python 3.3.
Parameters
start:int

the position of the first element.

stop:int

one more than the position of the last element.

step:int

the step size.

value:PyObject

an object consistent with the slice assignment

Annotations
@Override
toStringback to summary
public String toString()

Overrides org.python.core.PyObject.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override