Top Fields Constructors Methods
org.python.modules._io

public Class PyFileIO

extends PyRawIOBase
Class Inheritance
Known Direct Subclasses
org.python.modules._io.PyFileIODerived
Annotations
@Untraversable
@ExposedType
name:_io.FileIO
base:PyRawIOBase
Imports
java.io.IOException, .InputStream, .OutputStream, java.nio.ByteBuffer, org.python.core.ArgParser, .BuiltinDocs, .Py, .PyArray, .PyBuffer, .PyException, .PyJavaType, .PyLong, .PyNewWrapper, .PyObject, .PyString, .PyType, .PyUnicode, .Untraversable, org.python.core.io.FileIO, .RawIOBase, .StreamIO, org.python.expose.ExposedGet, .ExposedMethod, .ExposedNew, .ExposedSet, .ExposedType, jnr.constants.platform.Errno

Field Summary

Modifier and TypeField and Description
public final boolean
closefd

Whether to close the underlying stream on closing this object.

private static final PyString
private RawIOBase
ioDelegate

The FileIO or StreamIO to which we delegate operations not complete locally.

public final PyString
mode

The mode as a PyString based on readable and writable

private static final String[]
private boolean
readable

Set true when stream must be readable = reading | updating

private boolean
seekable

Set true when stream is seekable

private boolean
seekableKnown

Set true when we have made the seekable test

public static final PyType
private boolean
writable

Set true when stream must be writable = writing | updating | appending

Inherited from org.python.modules._io.PyRawIOBase:
docread_docreadall_docreadinto_docwrite_doc

Constructor Summary

AccessConstructor and Description
public
PyFileIO(PyObject
path or descriptor on which this should be constructed
file
,
OpenMode
type of access specified
mode
,
boolean
if false, do not close fd on call to close()
closefd
)

Construct an open _io.FileIO starting with an object that may be a file name or a file descriptor (actually a RawIOBase).

public
PyFileIO(PyType
for which construction is occurring
subtype
,
PyObject
path or descriptor on which this should be constructed
file
,
OpenMode
type of access specified
mode
,
boolean
if false, do not close file on call to close()
closefd
)

Construct an open _io.FileIO for a sub-class constructor starting with an object that may be a file name or a file descriptor (actually a RawIOBase).

Method Summary

Modifier and TypeMethod and Description
private final long
_truncate()

Common to FileIO_truncate(null) and truncate().

private final long
_truncate(long size)

Common to FileIO_truncate(size) and truncate(size).

public void
close()

Overrides org.python.modules._io.PyIOBase.close.

Close the underlying ioDelegate only if closefd was specified as (or defaulted to) True.

private PyException

Returns:

ValueError to throw
closedValueError
()

Convenience method providing the exception when an method requires the file to be open, and it isn't.

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

Create a PyFileIO and its FileIO delegate from the arguments.

pack-priv final synchronized void
pack-priv final PyObject
pack-priv final void
pack-priv final boolean
pack-priv final boolean
pack-priv final PyLong
pack-priv final long
FileIO_seek(long pos, int whence)

pack-priv final boolean
pack-priv final String
pack-priv final long
pack-priv final boolean
pack-priv final PyLong
public PyObject
fileno()

Overrides org.python.modules._io.PyIOBase.fileno.

Return a file descriptor for the stream.

public void
flush()

Overrides org.python.modules._io.PyIOBase.flush.

Flush write buffers, or no-op for read-only and non-blocking streams.

public boolean
isatty()

Overrides org.python.modules._io.PyIOBase.isatty.

Is the stream known to be an interactive console?

public final void
public boolean
readable()

Overrides org.python.modules._io.PyIOBase.readable.

Is the stream readable?

public PyObject
readinto(PyObject
byte array to try to fill
buf
)

Overrides org.python.modules._io.PyRawIOBase.readinto.

Read up to len(b) bytes into bytearray b and return the number of bytes read.

public long
seek(long
relative to the specified point
pos
,
int
0=from start, 1=from here, 2=from end
whence
)

Overrides org.python.modules._io.PyIOBase.seek.

Position the read or write pointer at a given byte offset pos relative to a position indicated by whence.

public boolean
seekable()

Overrides org.python.modules._io.PyIOBase.seekable.

Is the stream capable of positioning the read/write pointer?

private void
setDelegate(PyObject
name or descriptor
file
,
OpenMode
parsed file open mode
mode
)

Helper function that turns the arguments of the most general constructor, or __new__, into a FileIO, assigned to ioDelegate.

private PyException

Returns:

ValueError to throw
tailoredValueError
(String
type of operation not valid ("read" or "writ" in practice).
action
)

Convenience method providing the exception when an method requires the file to be open, readable or writable, and it isn't.

public String
toString()

Overrides org.python.core.PyObject.toString.

Returns a string representation of the object.

public long
truncate()

Overrides org.python.modules._io.PyIOBase.truncate.

Truncate file to size bytes to the current position (as reported by tell()).

public long
truncate(long
requested for stream
size
)

Overrides org.python.modules._io.PyIOBase.truncate.

Truncate file to size bytes.

public boolean
writable()

Overrides org.python.modules._io.PyIOBase.writable.

Is the stream writable?

public PyObject
write(PyObject
buffer of bytes to be written
buf
)

Overrides org.python.modules._io.PyRawIOBase.write.

Write the given bytes or bytearray object to the underlying raw stream and return the number of bytes written.

Inherited from org.python.modules._io.PyRawIOBase:
_RawIOBase__new___RawIOBase_read_RawIOBase_readall_RawIOBase_readinto_RawIOBase_writereadreadall

Field Detail

closefdback to summary
public final boolean closefd

Whether to close the underlying stream on closing this object.

Annotations
@ExposedGet
doc:True if the file descriptor will be closed

defaultModeback to summary
private static final PyString defaultMode
ioDelegateback to summary
private RawIOBase ioDelegate

The FileIO or StreamIO to which we delegate operations not complete locally.

modeback to summary
public final PyString mode

The mode as a PyString based on readable and writable

Annotations
@ExposedGet
doc:String giving the file mode: \'rb\', \'rb+\', or \'wb\'

openArgsback to summary
private static final String[] openArgs
readableback to summary
private boolean readable

Set true when stream must be readable = reading | updating

seekableback to summary
private boolean seekable

Set true when stream is seekable

seekableKnownback to summary
private boolean seekableKnown

Set true when we have made the seekable test

TYPEback to summary
public static final PyType TYPE

Hides org.python.modules._io.PyRawIOBase.TYPE.

writableback to summary
private boolean writable

Set true when stream must be writable = writing | updating | appending

Constructor Detail

PyFileIOback to summary
public PyFileIO(PyObject file, OpenMode mode, boolean closefd)

Construct an open _io.FileIO starting with an object that may be a file name or a file descriptor (actually a RawIOBase). Only the relevant flags within the parsed mode object are consulted (so that flags meaningful to this sub-class need not be processed out).

Parameters
file:PyObject

path or descriptor on which this should be constructed

mode:OpenMode

type of access specified

closefd:boolean

if false, do not close fd on call to close()

PyFileIOback to summary
public PyFileIO(PyType subtype, PyObject file, OpenMode mode, boolean closefd)

Construct an open _io.FileIO for a sub-class constructor starting with an object that may be a file name or a file descriptor (actually a RawIOBase). Only the relevant flags within the parsed mode object are consulted (so that flags meaningful to this sub-class need not be processed out).

Parameters
subtype:PyType

for which construction is occurring

file:PyObject

path or descriptor on which this should be constructed

mode:OpenMode

type of access specified

closefd:boolean

if false, do not close file on call to close()

Method Detail

_truncateback to summary
private final long _truncate()

Common to FileIO_truncate(null) and truncate().

_truncateback to summary
private final long _truncate(long size)

Common to FileIO_truncate(size) and truncate(size).

closeback to summary
public void close()

Overrides org.python.modules._io.PyIOBase.close.

Close the underlying ioDelegate only if closefd was specified as (or defaulted to) True.

Annotations
@Override
closedValueErrorback to summary
private PyException closedValueError()

Convenience method providing the exception when an method requires the file to be open, and it isn't.

Returns:PyException

ValueError to throw

FileIO___new__back to summary
pack-priv static PyObject FileIO___new__(PyNewWrapper new_, boolean init, PyType subtype, PyObject[] args, String[] keywords)

Create a PyFileIO and its FileIO delegate from the arguments.

Annotations
@ExposedNew
FileIO_closeback to summary
pack-priv final synchronized void FileIO_close()
Annotations
@ExposedMethod
FileIO_filenoback to summary
pack-priv final PyObject FileIO_fileno()
Annotations
@ExposedMethod
doc:Returns underlying file descriptor if one exists. An IOError is raised if the IO object does not use a file descriptor.
FileIO_flushback to summary
pack-priv final void FileIO_flush()
Annotations
@ExposedMethod
doc:Flush write buffers.
FileIO_isattyback to summary
pack-priv final boolean FileIO_isatty()
Annotations
@ExposedMethod
doc:Return whether this is an \'interactive\' stream. Return False if it can\'t be determined.
FileIO_readableback to summary
pack-priv final boolean FileIO_readable()
Annotations
@ExposedMethod
doc:Return whether object was opened for reading. If False, read() will raise IOError.
FileIO_readintoback to summary
pack-priv final PyLong FileIO_readinto(PyObject buf)
Annotations
@ExposedMethod
doc:Read up to len(b) bytes into bytearray b and return the number of bytes read. If the object is in non-blocking mode and no bytes are available, None is returned.
FileIO_seekback to summary
pack-priv final long FileIO_seek(long pos, int whence)
Annotations
@ExposedMethod
defaults:0
doc:Change stream position. Change the stream position to byte offset offset. offset is interpreted relative to the position indicated by whence. Values for whence are: * 0 -- start of stream (the default); offset should be zero or positive * 1 -- current stream position; offset may be negative * 2 -- end of stream; offset is usually negative Return the new absolute position.
FileIO_seekableback to summary
pack-priv final boolean FileIO_seekable()
Annotations
@ExposedMethod
doc:Return whether object supports random access. If False, seek(), tell() and truncate() will raise IOError. This method may need to do a test seek().
FileIO_toStringback to summary
pack-priv final String FileIO_toString()
Annotations
@ExposedMethod
names:__str__, __repr__
doc:x.__str__() <==> str(x)
FileIO_truncateback to summary
pack-priv final long FileIO_truncate(PyObject size)
Annotations
@ExposedMethod
defaults:null
doc:Truncate file to size bytes. File pointer is left unchanged. Size defaults to the current IO position as reported by tell(). Returns the new size.
FileIO_writableback to summary
pack-priv final boolean FileIO_writable()
Annotations
@ExposedMethod
doc:Return whether object was opened for writing. If False, read() will raise IOError.
FileIO_writeback to summary
pack-priv final PyLong FileIO_write(PyObject buf)
Annotations
@ExposedMethod
doc:Write the given bytes or bytearray object, b, to the underlying raw stream and return the number of bytes written.
filenoback to summary
public PyObject fileno()

Overrides org.python.modules._io.PyIOBase.fileno.

Doc from org.python.modules._io.PyIOBase.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 that FileIO#fileno() returns.

Returns:PyObject

a file descriptor (as opaque object)

Annotations
@Override

flushback to summary
public void flush()

Overrides org.python.modules._io.PyIOBase.flush.

Doc from org.python.modules._io.PyIOBase.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.

Annotations
@Override
isattyback to summary
public boolean isatty()

Overrides org.python.modules._io.PyIOBase.isatty.

Doc from org.python.modules._io.PyIOBase.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:boolean

true if a console: false if not or we can't tell

Annotations
@Override

mode_readonlyback to summary
public final void mode_readonly(PyString value)
Annotations
@ExposedSet
name:mode
readableback to summary
public boolean readable() throws PyException

Overrides org.python.modules._io.PyIOBase.readable.

Doc from org.python.modules._io.PyIOBase.readable.

Is the stream readable?

Returns:boolean

true if readable

Annotations
@Override
Exceptions
PyException:
ValueError if the object is closed to client operations

readintoback to summary
public PyObject readinto(PyObject buf)

Overrides org.python.modules._io.PyRawIOBase.readinto.

Doc from org.python.modules._io.PyRawIOBase.readinto.

Read up to len(b) bytes into bytearray b and return the number of bytes read. If the object is in non-blocking mode and no bytes are available, None is returned.";

Parameters
buf:PyObject

byte array to try to fill

Returns:PyObject

number of bytes actually read or Py.None (when a non-blocking source is not ready with further data)

Annotations
@Override

seekback to summary
public long seek(long pos, int whence)

Overrides org.python.modules._io.PyIOBase.seek.

Doc from org.python.modules._io.PyIOBase.seek.

Position the read or write pointer at a given byte offset pos relative to a position indicated by whence.

  • If whence=0, the position will be set to pos bytes.
  • If whence=1 the position will be set to the current position plus pos.
  • If whence=2 the position will be set to the stream size plus pos (and usually pos<=0).
Parameters
pos:long

relative to the specified point

whence:int

0=from start, 1=from here, 2=from end

Returns:long

the new current position

Annotations
@Override

seekableback to summary
public boolean seekable()

Overrides org.python.modules._io.PyIOBase.seekable.

Doc from org.python.modules._io.PyIOBase.seekable.

Is the stream capable of positioning the read/write pointer?

Returns:boolean

True if may be positioned

Annotations
@Override

setDelegateback to summary
private void setDelegate(PyObject file, OpenMode mode)

Helper function that turns the arguments of the most general constructor, or __new__, into a FileIO, assigned to ioDelegate. It enforces rules on closefd and the type of object that may be a file descriptor, and assigns the name attribute to the string name or the file descriptor (see Python docs for io.FileIO.name). This places the logic of those several operations in one place.

In many cases (such as construction from a file name, the FileIO is a newly-opened file. When the file object passed in is a file descriptor, the FileIO may be created to wrap that existing stream.

Parameters
file:PyObject

name or descriptor

mode:OpenMode

parsed file open mode

tailoredValueErrorback to summary
private PyException tailoredValueError(String action)

Convenience method providing the exception when an method requires the file to be open, readable or writable, and it isn't. If the file is closed, return the message for that, otherwise, one about reading or writing.

Parameters
action:String

type of operation not valid ("read" or "writ" in practice).

Returns:PyException

ValueError to throw

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

truncateback to summary
public long truncate()

Overrides org.python.modules._io.PyIOBase.truncate.

Doc from org.python.modules._io.PyIOBase.truncate.

Truncate file to size bytes to the current position (as reported by tell()).

Returns:long

the new size

Annotations
@Override

truncateback to summary
public long truncate(long size)

Overrides org.python.modules._io.PyIOBase.truncate.

Doc from org.python.modules._io.PyIOBase.truncate.

Truncate file to size bytes.

Parameters
size:long

requested for stream

Returns:long

the new size

Annotations
@Override

writableback to summary
public boolean writable() throws PyException

Overrides org.python.modules._io.PyIOBase.writable.

Doc from org.python.modules._io.PyIOBase.writable.

Is the stream writable?

Returns:boolean

true if writable

Annotations
@Override
Exceptions
PyException:
ValueError if the object is closed to client operations

writeback to summary
public PyObject write(PyObject buf)

Overrides org.python.modules._io.PyRawIOBase.write.

Doc from org.python.modules._io.PyRawIOBase.write.

Write the given bytes or bytearray object to the underlying raw stream and return the number of bytes written.

Parameters
buf:PyObject

buffer of bytes to be written

Returns:PyObject

the number of bytes written

Annotations
@Override