-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathPyFileIO.java
More file actions
516 lines (441 loc) · 17.1 KB
/
PyFileIO.java
File metadata and controls
516 lines (441 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/* Copyright (c)2013 Jython Developers */
package org.python.modules._io;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import org.python.core.ArgParser;
import org.python.core.BuiltinDocs;
import org.python.core.Py;
import org.python.core.PyArray;
import org.python.core.PyBuffer;
import org.python.core.PyException;
import org.python.core.PyJavaType;
import org.python.core.PyLong;
import org.python.core.PyNewWrapper;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.core.PyType;
import org.python.core.PyUnicode;
import org.python.core.Untraversable;
import org.python.core.io.FileIO;
import org.python.core.io.RawIOBase;
import org.python.core.io.StreamIO;
import org.python.expose.ExposedGet;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedNew;
import org.python.expose.ExposedSet;
import org.python.expose.ExposedType;
import jnr.constants.platform.Errno;
@Untraversable
@ExposedType(name = "_io.FileIO", base = PyRawIOBase.class)
public class PyFileIO extends PyRawIOBase {
public static final PyType TYPE = PyType.fromClass(PyFileIO.class);
/** The {@link FileIO} or {@link StreamIO} to which we delegate operations not complete locally. */
private RawIOBase ioDelegate;
/*
* Implementation note: CPython fileio does not use the base-class, possibly overridden,
* readable(), writable() and seekable(). Instead it sets local variables for readable and
* writable using the open mode, and returns these as readable() and writable(), while using
* them internally. The local variable seekable (and seekable()) is worked out from a one-time
* trial seek.
*/
/** Set true when stream must be <code>readable = reading | updating</code> */
private boolean readable;
/** Set true when stream must be <code>writable = writing | updating | appending</code> */
private boolean writable;
/** Set true when we have made the seekable test */
private boolean seekableKnown;
/** Set true when stream is seekable */
private boolean seekable;
/** Whether to close the underlying stream on closing this object. */
@ExposedGet(doc = "True if the file descriptor will be closed")
public final boolean closefd;
/** The mode as a PyString based on readable and writable */
@ExposedGet(doc = "String giving the file mode: 'rb', 'rb+', or 'wb'")
public final PyString mode;
@ExposedSet(name = "mode")
public final void mode_readonly(PyString value) {
readonlyAttributeError("mode");
}
private static final PyString defaultMode = new PyString("r");
/**
* Construct an open <code>_io.FileIO</code> starting with an object that may be a file name or
* a file descriptor (actually a {@link 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).
*
* @param file path or descriptor on which this should be constructed
* @param mode type of access specified
* @param closefd if <code>false</code>, do not close <code>fd</code> on call to
* <code>close()</code>
*/
public PyFileIO(PyObject file, OpenMode mode, boolean closefd) {
this(TYPE, file, mode, closefd);
}
/**
* Construct an open <code>_io.FileIO</code> for a sub-class constructor starting with an object
* that may be a file name or a file descriptor (actually a {@link 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).
*
* @param subtype for which construction is occurring
* @param file path or descriptor on which this should be constructed
* @param mode type of access specified
* @param closefd if <code>false</code>, do not close <code>file</code> on call to
* <code>close()</code>
*/
public PyFileIO(PyType subtype, PyObject file, OpenMode mode, boolean closefd) {
super(subtype);
// Establish the direction(s) of flow
readable = mode.reading | mode.updating;
writable = mode.writing | mode.updating | mode.appending;
// Assign a delegate according to the file argument
this.closefd = closefd;
setDelegate(file, mode);
// The mode string of a raw file always asserts it is binary: "rb", "rb+", or "wb".
if (readable) {
this.mode = new PyString(writable ? "rb+" : "rb");
} else {
this.mode = new PyString("wb");
}
}
/**
* Helper function that turns the arguments of the most general constructor, or
* <code>__new__</code>, into a {@link FileIO}, assigned to {@link #ioDelegate}. It enforces
* rules on {@link #closefd} and the type of object that may be a file descriptor, and assigns
* the <code>name</code> 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.
* <p>
* 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.
*
* @param file name or descriptor
* @param mode parsed file open mode
*/
private void setDelegate(PyObject file, OpenMode mode) {
if (file instanceof PyString) {
// Open a file by name
if (!closefd) {
throw Py.ValueError("Cannot use closefd=False with file name");
}
ioDelegate = new FileIO((PyString)file, mode.forFileIO());
} else {
/*
* Build an _io.FileIO from an existing "file descriptor", which we may or may not want
* closed at the end. A CPython file descriptor is an int, but this is not the natural
* choice in Jython, and file descriptors should be treated as opaque.
*/
Object fd = file.__tojava__(Object.class);
if (fd instanceof FileIO || fd instanceof StreamIO) {
/*
* It is the "Jython file descriptor", of a type suitable to be the ioDelegate. The
* allowed types are able to give us a non-null InputStream or OutputStream,
* according to direction.
*/
ioDelegate = (RawIOBase)fd;
}
}
// If we couldn't figure it out, ioDelegate will still be null
if (ioDelegate == null) {
// The file was a type we don't know how to use
throw Py.TypeError(String.format("invalid file: %s", file.__repr__().asString()));
} else {
if (ioDelegate.closed()) {
// A closed file descriptor is a "bad descriptor"
throw Py.OSError(Errno.EBADF);
}
if ((readable && !ioDelegate.readable()) || (writable && !ioDelegate.writable())) {
// Requested mode in conflict with underlying file or stream
throw tailoredValueError(readable ? "read" : "writ");
}
// The name is either the textual name or a file descriptor (see Python docs)
fastGetDict().__setitem__("name", file);
}
}
private static final String[] openArgs = {"file", "mode", "closefd"};
/**
* Create a {@link PyFileIO} and its <code>FileIO</code> delegate from the arguments.
*/
@ExposedNew
static PyObject FileIO___new__(PyNewWrapper new_, boolean init, PyType subtype,
PyObject[] args, String[] keywords) {
ArgParser ap = new ArgParser("FileIO", args, keywords, openArgs, 1);
PyObject file = ap.getPyObject(0);
PyObject m = ap.getPyObject(1, defaultMode);
boolean closefd = Py.py2boolean(ap.getPyObject(2, Py.True));
// Decode the mode string and check it
OpenMode mode = new OpenMode(m.asString()) {
{
invalid |= universal | text; // These other modes are invalid
}
};
mode.checkValid();
if (subtype == TYPE) {
return new PyFileIO(subtype, file, mode, closefd);
} else {
return new PyFileIODerived(subtype, file, mode, closefd);
}
}
/*
* ===========================================================================================
* Exposed methods in the order they appear in CPython's fileio.c method table
* ===========================================================================================
*/
// _RawIOBase.read is correct for us
// _RawIOBase.readall is correct for us
@Override
public PyObject readinto(PyObject buf) {
return FileIO_readinto(buf);
}
@ExposedMethod(doc = readinto_doc)
final PyLong FileIO_readinto(PyObject buf) {
int count;
if (!readable) { // ... (or closed)
throw tailoredValueError("read");
}
if (buf instanceof PyArray) {
// Special case: PyArray knows how to read into itself
PyArray a = (PyArray)buf;
try {
// The ioDelegate, if readable, can always provide an InputStream (see setDelegate)
InputStream is = ioDelegate.asInputStream();
count = a.fillFromStream(is);
count *= a.getItemsize();
} catch (IOException ioe) {
throw Py.IOError(ioe);
}
} else {
// Perform the operation through a buffer view on the object
try (PyBuffer pybuf = writablePyBuffer(buf)) {
ByteBuffer byteBuffer = pybuf.getNIOByteBuffer();
synchronized (ioDelegate) {
count = ioDelegate.readinto(byteBuffer);
}
}
}
return new PyLong(count);
}
@Override
public PyObject write(PyObject buf) {
return FileIO_write(buf);
}
@ExposedMethod(doc = write_doc)
final PyLong FileIO_write(PyObject buf) {
int count;
if (!writable) { // ... (or closed)
throw tailoredValueError("writ");
}
if (buf instanceof PyArray) {
// Special case: PyArray knows how to write itself
try {
// The ioDelegate, if writable, can always provide an OutputStream (see setDelegate)
OutputStream os = ioDelegate.asOutputStream();
count = ((PyArray)buf).toStream(os);
} catch (IOException ioe) {
throw Py.IOError(ioe);
}
} else {
// Get or synthesise a buffer API on the object to be written
try (PyBuffer pybuf = readablePyBuffer(buf)) {
// Access the data as a java.nio.ByteBuffer [pos:limit] within possibly larger array
ByteBuffer byteBuffer = pybuf.getNIOByteBuffer();
synchronized (ioDelegate) {
count = ioDelegate.write(byteBuffer);
}
}
}
return new PyLong(count);
}
@Override
public long seek(long pos, int whence) {
return FileIO_seek(pos, whence);
}
@ExposedMethod(defaults = "0", doc = seek_doc)
final long FileIO_seek(long pos, int whence) {
if (__closed) {
throw closedValueError();
}
synchronized (ioDelegate) {
return ioDelegate.seek(pos, whence);
}
}
// _IOBase.tell() is correct for us
@Override
public long truncate() {
return _truncate();
}
@Override
public long truncate(long size) {
return _truncate(size);
}
@ExposedMethod(defaults = "null", doc = truncate_doc)
final long FileIO_truncate(PyObject size) {
return (size != null) ? _truncate(size.asLong()) : _truncate();
}
/** Common to FileIO_truncate(null) and truncate(). */
private final long _truncate() {
if (!writable) { // ... (or closed)
throw tailoredValueError("writ");
}
synchronized (ioDelegate) {
return ioDelegate.truncate(ioDelegate.tell());
}
}
/** Common to FileIO_truncate(size) and truncate(size). */
private final long _truncate(long size) {
if (!writable) { // ... (or closed)
throw tailoredValueError("writ");
}
synchronized (ioDelegate) {
return ioDelegate.truncate(size);
}
}
/**
* Close the underlying ioDelegate only if <code>closefd</code> was specified as (or defaulted
* to) <code>True</code>.
*/
@Override
public void close() {
FileIO_close();
}
@ExposedMethod
final synchronized void FileIO_close() {
try {
// Close this object to further input (also calls flush)
super.close();
} finally {
// Now close downstream (if required to)
if (closefd) {
ioDelegate.close();
}
// This saves us doing two tests for each action (when the file is open)
readable = false;
writable = false;
}
}
@Override
public boolean seekable() {
return FileIO_seekable();
}
@ExposedMethod(doc = seekable_doc)
final boolean FileIO_seekable() {
if (__closed) {
throw closedValueError();
}
if (!seekableKnown) {
try {
ioDelegate.seek(0, 1); // Trial seek
seekable = true;
} catch (PyException exc) {
if (!exc.match(Py.IOError)) {
throw exc;
}
seekable = false;
}
seekableKnown = true;
}
return seekable;
}
@Override
public boolean readable() throws PyException {
return FileIO_readable();
}
@ExposedMethod(doc = readable_doc)
final boolean FileIO_readable() {
if (__closed) {
throw closedValueError();
}
return readable;
}
@Override
public boolean writable() throws PyException {
return FileIO_writable();
}
@ExposedMethod(doc = writable_doc)
final boolean FileIO_writable() {
if (__closed) {
throw closedValueError();
}
return writable;
}
@Override
public PyObject fileno() {
return FileIO_fileno();
}
@ExposedMethod(doc = fileno_doc)
final PyObject FileIO_fileno() {
return PyJavaType.wrapJavaObject(ioDelegate.fileno());
}
@Override
public boolean isatty() {
return FileIO_isatty();
}
@ExposedMethod(doc = isatty_doc)
final boolean FileIO_isatty() {
if (__closed) {
throw closedValueError();
}
return ioDelegate.isatty();
}
// fileio.c has no flush(), but why not, when there is fdflush()?
// And it is a no-op for Jython io.FileIO, but why when there is FileChannel.force()?
@Override
public void flush() {
FileIO_flush();
}
@ExposedMethod(doc = "Flush write buffers.")
final void FileIO_flush() {
if (writable()) {
// Check for *downstream* close. (Locally, closed means "closed to client actions".)
ioDelegate.checkClosed();
ioDelegate.flush();
}
}
@ExposedMethod(names = {"__str__", "__repr__"}, doc = BuiltinDocs.object___str___doc)
final String FileIO_toString() {
if (closed()) {
return "<_io.FileIO [closed]>";
} else {
PyObject name = fastGetDict().__finditem__("name");
if (name != null && (name instanceof PyString)) {
String xname = name.asString();
if (name instanceof PyUnicode) {
xname = PyString.encode_UnicodeEscape(xname, false);
}
return String.format("<_io.FileIO name='%s' mode='%s'>", xname, mode);
} else {
return String.format("<_io.FileIO fd=%s mode='%s'>", fileno(), mode);
}
}
}
@Override
public String toString() {
return FileIO_toString().toString();
}
/**
* Convenience method providing the exception when an method requires the file to be open, and
* it isn't.
*
* @return ValueError to throw
*/
private PyException closedValueError() {
return Py.ValueError("I/O operation on closed file");
}
/**
* 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.
*
* @param action type of operation not valid ("read" or "writ" in practice).
* @return ValueError to throw
*/
private PyException tailoredValueError(String action) {
if (action == null || __closed) {
return closedValueError();
} else {
return Py.ValueError("File not open for " + action + "ing");
}
}
}