Package org.python.modules._io
Class PyIOBase
java.lang.Object
org.python.core.PyObject
org.python.modules._io.PyIOBase
- All Implemented Interfaces:
Serializable,FinalizableBuiltin,Traverseproc
- Direct Known Subclasses:
PyIOBaseDerived,PyRawIOBase
The Python module
_io._IOBase, on which the io module depends directly.
Implementation note: The code is based heavily on the Jython 2.6-ish
_fileio.PyFileIO, the purely Java-accessible IOBase (both
Philip Jenvey's work), and the Python implementation in Lib/_pyio. We do not simply
delegate to the implementation in org.python.core.io because of the need to override
parts of the implementation in Python subclasses. A call to close(), for example, is
required to call flush(), but if delegated to the pure Java implementation would not
call the version of flush() overridden in a Python sub-class of
_io._IOBase. Equally, the use made by PyRawIOBase.read(int) of
readinto(bytearray) would not pick up the version of readinto defined
in Python.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class org.python.core.PyObject
PyObject.ConversionException -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final PyTypestatic final Stringstatic final StringFields inherited from class org.python.core.PyObject
gcMonitorGlobal -
Method Summary
Modifier and TypeMethodDescriptionvoidFinalizableBuiltin.__del_builtin__()is the built-in's own finalizer, whileFinalizablePyObjectDerived.__del_derived__()refers to an instance's in-dict__del__.Called at the start of a context-managed suite (supporting thewithclause).booleanCalled at the end of a context-managed suite (supporting thewithclause), and will normally close the stream.__iter__()Return an iterator on whichnextmay be repeatedly called to produce (usually) lines from this stream or file.Return the next element of the sequence that this is an iterator for.final voidvoid_checkClosed(String msg) Raise an error if the underlying IO stream is closed.final voidRaise an error if the underlying IO stream is not readable.void_checkReadable(String msg) Raise an error if the underlying IO stream is not readable.final voidRaise an error if the pointer of underlying IO stream is not capable of being positioned.void_checkSeekable(String msg) Raise an error if the pointer of underlying IO stream is not capable of being positioned.final voidRaise an error if the underlying IO stream is not writable.void_checkWritable(String msg) Raise an error if the underlying IO stream is not writable.voidclose()Close the stream.final booleanclosed()Is the stream closed against further client operations?final voidclosed_readonly(boolean value) xxx implements where meaningfulfileno()Return a file descriptor for the stream.voidflush()Flush write buffers, or no-op for read-only and non-blocking streams.booleanisatty()Is the stream known to be an interactive console?next()May be called repeatedly to produce (usually) lines from this stream or file.booleanreadable()Is the stream readable?readline()Return one line of text (bytes terminates by'\n'), or the whole stream, whichever is shorter.readline(int limit) Return one line of text (bytes terminates by'\n'), or the specified number of bytes, or the whole stream, whichever is shortest.Read a stream as a sequence of lines.booleanOptional operation.final longseek(long pos) Position the read or write pointer at a given byte offsetposrelative to the start.longseek(long pos, int whence) Position the read or write pointer at a given byte offsetposrelative to a position indicated bywhence.booleanseekable()Is the stream capable of positioning the read/write pointer?longtell()Get the current stream position.intTraverses all directly containedPyObjects.longtruncate()Truncate file tosizebytes to the current position (as reported bytell()).longtruncate(long size) Truncate file tosizebytes.booleanwritable()Is the stream writable?voidwritelines(PyObject lines) Write an iterable sequence of strings to the stream.Methods inherited from class org.python.core.PyObject
__abs__, __add__, __and__, __call__, __call__, __call__, __call__, __call__, __call__, __call__, __call__, __call__, __call__, __call__, __call__, __call__, __call__, __call__, __call__, __cmp__, __coerce__, __coerce_ex__, __complex__, __contains__, __delattr__, __delattr__, __delete__, __delitem__, __delitem__, __delslice__, __delslice__, __dir__, __div__, __divmod__, __ensure_finalizer__, __eq__, __findattr__, __findattr__, __findattr_ex__, __finditem__, __finditem__, __finditem__, __float__, __floordiv__, __format__, __ge__, __get__, __getattr__, __getattr__, __getitem__, __getitem__, __getnewargs__, __getslice__, __getslice__, __gt__, __hash__, __hex__, __iadd__, __iand__, __idiv__, __idivmod__, __ifloordiv__, __ilshift__, __imod__, __imul__, __index__, __int__, __invert__, __ior__, __ipow__, __irshift__, __isub__, __itruediv__, __ixor__, __le__, __len__, __long__, __lshift__, __lt__, __mod__, __mul__, __ne__, __neg__, __nonzero__, __not__, __oct__, __or__, __pos__, __pow__, __pow__, __radd__, __rand__, __rdiv__, __rdivmod__, __reduce__, __reduce_ex__, __reduce_ex__, __repr__, __rfloordiv__, __rlshift__, __rmod__, __rmul__, __ror__, __rpow__, __rrshift__, __rshift__, __rsub__, __rtruediv__, __rxor__, __set__, __setattr__, __setattr__, __setitem__, __setitem__, __setitem__, __setslice__, __setslice__, __str__, __sub__, __tojava__, __truediv__, __trunc__, __unicode__, __xor__, _add, _and, _callextra, _cmp, _div, _divmod, _doget, _doget, _doset, _eq, _floordiv, _ge, _gt, _iadd, _iand, _idiv, _idivmod, _ifloordiv, _ilshift, _imod, _imul, _in, _ior, _ipow, _irshift, _is, _isnot, _isub, _itruediv, _ixor, _jcall, _jcallexc, _jthrow, _le, _lshift, _lt, _mod, _mul, _ne, _notin, _or, _pow, _rshift, _sub, _truediv, _xor, asDouble, asIndex, asIndex, asInt, asInt, asIterable, asLong, asLong, asName, asName, asString, asString, asStringOrNull, asStringOrNull, bit_length, conjugate, delDict, delType, dispatch__init__, equals, fastGetClass, getDict, getType, hashCode, implementsDescrDelete, implementsDescrGet, implementsDescrSet, invoke, invoke, invoke, invoke, invoke, invoke, isCallable, isDataDescr, isIndex, isInteger, isMappingType, isNumberType, isSequenceType, noAttributeError, object___subclasshook__, readonlyAttributeError, setDict, setType, toString
-
Field Details
-
TYPE
-
seek_doc
- See Also:
-
tell_doc
- See Also:
-
truncate_doc
- See Also:
-
flush_doc
- See Also:
-
close_doc
- See Also:
-
closed_doc
- See Also:
-
seekable_doc
- See Also:
-
readable_doc
- See Also:
-
writable_doc
- See Also:
-
fileno_doc
- See Also:
-
isatty_doc
- See Also:
-
readline_doc
- See Also:
-
readlines_doc
- See Also:
-
writelines_doc
- See Also:
-
-
Method Details
-
fastGetDict
Description copied from class:PyObjectxxx implements where meaningful- Overrides:
fastGetDictin classPyObject- Returns:
- internal object per instance dict or null
-
seek
public long seek(long pos, int whence) Position the read or write pointer at a given byte offsetposrelative to a position indicated bywhence.- If
whence=0, the position will be set toposbytes. - If
whence=1 the position will be set to the current position pluspos. - If
whence=2 the position will be set to the stream size pluspos(and usuallypos<=0).
- Parameters:
pos- relative to the specified pointwhence- 0=from start, 1=from here, 2=from end- Returns:
- the new current position
- If
-
seek
public final long seek(long pos) Position the read or write pointer at a given byte offsetposrelative to the start.- Parameters:
pos- relative to the start- Returns:
- the new current position
-
tell
public long tell()Get the current stream position.- Returns:
- stream position
-
truncate
public long truncate(long size) Truncate file tosizebytes.- Parameters:
size- requested for stream- Returns:
- the new size
-
truncate
public long truncate()Truncate file tosizebytes to the current position (as reported bytell()).- Returns:
- the new size
-
flush
public void flush()Flush write buffers, or no-op for read-only and non-blocking streams. Irrespective of the concrete type of the i/o object, locally-buffered write data is written downstream. Whether the downstream in object is also flushed depends upon the specific type of this object. -
closed_readonly
public final void closed_readonly(boolean value) -
close
public void close()Close the stream. If closed already, this is a no-op. -
seekable
Is the stream capable of positioning the read/write pointer?- Returns:
Trueif may be positioned- Throws:
PyException-ValueErrorif the object is closed to client operations
-
_checkSeekable
Raise an error if the pointer of underlying IO stream is not capable of being positioned.- Parameters:
msg- optional custom message- Throws:
PyException-ValueErrorif the object is closed to client operationsPyException-IOErrorif the stream is not capable of being positioned.
-
_checkSeekable
public final void _checkSeekable()Raise an error if the pointer of underlying IO stream is not capable of being positioned.- Throws:
PyException-ValueErrorif the object is closed to client operationsPyException-IOErrorif the stream is not capable of being positioned.
-
readable
Is the stream readable?- Returns:
trueif readable- Throws:
PyException-ValueErrorif the object is closed to client operations
-
_checkReadable
Raise an error if the underlying IO stream is not readable.- Parameters:
msg- optional custom message- Throws:
PyException-ValueErrorif the object is closed to client operationsPyException-IOErrorif the stream is not readable.
-
_checkReadable
public final void _checkReadable()Raise an error if the underlying IO stream is not readable.- Throws:
PyException-ValueErrorif the object is closed to client operationsPyException-IOErrorif the stream is not readable.
-
writable
Is the stream writable?- Returns:
trueif writable- Throws:
PyException-ValueErrorif the object is closed to client operations
-
_checkWritable
Raise an error if the underlying IO stream is not writable.- Parameters:
msg- optional custom message- Throws:
PyException-ValueErrorif the object is closed to client operationsPyException-IOErrorif the stream is not writable.
-
_checkWritable
Raise an error if the underlying IO stream is not writable.- Throws:
PyException-ValueErrorif the object is closed to client operationsPyException-IOErrorif the stream is not writable.
-
closed
public final boolean closed()Is the stream closed against further client operations?- Returns:
trueif closed
-
_checkClosed
Raise an error if the underlying IO stream is closed. (Note opposite sense from_checkSeekable(java.lang.String), etc..- Parameters:
msg- optional custom message- Throws:
PyException-ValueErrorif the object is closed to client operations
-
_checkClosed
- Throws:
PyException
-
__enter__
Called at the start of a context-managed suite (supporting thewithclause).- Returns:
- this object
-
__exit__
Called at the end of a context-managed suite (supporting thewithclause), and will normally close the stream.- Returns:
- false
-
fileno
Return a file descriptor for the stream. A CPython file descriptor is an int, but this is not the natural choice in Jython, since Java has no such convention of using integers. File descriptors should be passed around opaquely, so their actual type is irrelevant, as long as (say)_jyio.open(PyObject[], String[])accepts the type thatRawIOBase.fileno()returns.- Returns:
- a file descriptor (as opaque object)
-
isatty
public boolean isatty()Is the stream known to be an interactive console? This relies on the ability of the underlying stream to know, which is not always possible.- Returns:
trueif a console:falseif not or we can't tell
-
readline
Return one line of text (bytes terminates by'\n'), or the specified number of bytes, or the whole stream, whichever is shortest.- Parameters:
limit- maximum number of bytes (<0 means no limit)- Returns:
- the line (or fragment)
-
readline
Return one line of text (bytes terminates by'\n'), or the whole stream, whichever is shorter.- Returns:
- the line (or fragment)
-
__iter__
Return an iterator on whichnextmay be repeatedly called to produce (usually) lines from this stream or file. -
__iternext__
Description copied from class:PyObjectReturn the next element of the sequence that this is an iterator for. Returns null when the end of the sequence is reached.- Overrides:
__iternext__in classPyObject
-
next
May be called repeatedly to produce (usually) lines from this stream or file.- Returns:
- next line from the stream or file
- Throws:
PyException-StopIterationwhen iteration has reached a natural conclusionPyException-ValueErrorif the file or stream is closedPyException-IOErrorreflecting an I/O error in during the read
-
readlines
Read a stream as a sequence of lines.- Parameters:
hint- stop reading lines after this many bytes (if not EOF first)- Returns:
- list containing the lines read
-
writelines
Write an iterable sequence of strings to the stream.- Parameters:
lines-
-
__del_builtin__
public void __del_builtin__()Description copied from interface:FinalizableBuiltinFinalizableBuiltin.__del_builtin__()is the built-in's own finalizer, whileFinalizablePyObjectDerived.__del_derived__()refers to an instance's in-dict__del__. A FinalizeTrigger callsFinalizablePyObjectDerived.__del_derived__()first and - if existent -FinalizableBuiltin.__del_builtin__()after that. A plainFinalizablePyObject.__del__()would behave as overridden byFinalizablePyObjectDerived.__del_derived__(), i.e. won't be called if the type implementsFinalizablePyObjectDerived, whileFinalizableBuiltin.__del_builtin__()is called in any case.- Specified by:
__del_builtin__in interfaceFinalizableBuiltin
-
traverse
Description copied from interface:TraverseprocTraverses all directly containedPyObjects. Like in CPython,argmust be passed unmodified tovisitas its second parameter. IfVisitproc.visit(PyObject, Object)returns nonzero, this return value must be returned immediately by traverse.Visitproc.visit(PyObject, Object)must not be called with anullPyObject-argument.- Specified by:
traversein interfaceTraverseproc
-
refersDirectlyTo
Description copied from interface:TraverseprocOptional operation. Should only be implemented if it is more efficient than callingTraverseproc.traverse(Visitproc, Object)with a visitproc that just watches out forob. Must returnfalseifobisnull.- Specified by:
refersDirectlyToin interfaceTraverseproc
-