AppPkg/Applications/Python/Python-2.7.10: Initial Checkin part 2/5.
The Modules directory from the cPython 2.7.10 distribution. These files are unchanged and set the baseline for subsequent commits. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Daryl McDaniel <edk2-lists@mc2research.org> git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@18738 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
committed by
darylm503
parent
c8042e1076
commit
7eb75bccb5
777
AppPkg/Applications/Python/Python-2.7.10/Modules/_io/_iomodule.c
Normal file
777
AppPkg/Applications/Python/Python-2.7.10/Modules/_io/_iomodule.c
Normal file
@ -0,0 +1,777 @@
|
||||
/*
|
||||
An implementation of the new I/O lib as defined by PEP 3116 - "New I/O"
|
||||
|
||||
Classes defined here: UnsupportedOperation, BlockingIOError.
|
||||
Functions defined here: open().
|
||||
|
||||
Mostly written by Amaury Forgeot d'Arc
|
||||
*/
|
||||
|
||||
#define PY_SSIZE_T_CLEAN
|
||||
#include "Python.h"
|
||||
#include "structmember.h"
|
||||
#include "_iomodule.h"
|
||||
|
||||
#ifdef HAVE_SYS_TYPES_H
|
||||
#include <sys/types.h>
|
||||
#endif /* HAVE_SYS_TYPES_H */
|
||||
|
||||
#ifdef HAVE_SYS_STAT_H
|
||||
#include <sys/stat.h>
|
||||
#endif /* HAVE_SYS_STAT_H */
|
||||
|
||||
|
||||
/* Various interned strings */
|
||||
|
||||
PyObject *_PyIO_str_close;
|
||||
PyObject *_PyIO_str_closed;
|
||||
PyObject *_PyIO_str_decode;
|
||||
PyObject *_PyIO_str_encode;
|
||||
PyObject *_PyIO_str_fileno;
|
||||
PyObject *_PyIO_str_flush;
|
||||
PyObject *_PyIO_str_getstate;
|
||||
PyObject *_PyIO_str_isatty;
|
||||
PyObject *_PyIO_str_newlines;
|
||||
PyObject *_PyIO_str_nl;
|
||||
PyObject *_PyIO_str_read;
|
||||
PyObject *_PyIO_str_read1;
|
||||
PyObject *_PyIO_str_readable;
|
||||
PyObject *_PyIO_str_readinto;
|
||||
PyObject *_PyIO_str_readline;
|
||||
PyObject *_PyIO_str_reset;
|
||||
PyObject *_PyIO_str_seek;
|
||||
PyObject *_PyIO_str_seekable;
|
||||
PyObject *_PyIO_str_setstate;
|
||||
PyObject *_PyIO_str_tell;
|
||||
PyObject *_PyIO_str_truncate;
|
||||
PyObject *_PyIO_str_writable;
|
||||
PyObject *_PyIO_str_write;
|
||||
|
||||
PyObject *_PyIO_empty_str;
|
||||
PyObject *_PyIO_empty_bytes;
|
||||
PyObject *_PyIO_zero;
|
||||
|
||||
|
||||
PyDoc_STRVAR(module_doc,
|
||||
"The io module provides the Python interfaces to stream handling. The\n"
|
||||
"builtin open function is defined in this module.\n"
|
||||
"\n"
|
||||
"At the top of the I/O hierarchy is the abstract base class IOBase. It\n"
|
||||
"defines the basic interface to a stream. Note, however, that there is no\n"
|
||||
"separation between reading and writing to streams; implementations are\n"
|
||||
"allowed to raise an IOError if they do not support a given operation.\n"
|
||||
"\n"
|
||||
"Extending IOBase is RawIOBase which deals simply with the reading and\n"
|
||||
"writing of raw bytes to a stream. FileIO subclasses RawIOBase to provide\n"
|
||||
"an interface to OS files.\n"
|
||||
"\n"
|
||||
"BufferedIOBase deals with buffering on a raw byte stream (RawIOBase). Its\n"
|
||||
"subclasses, BufferedWriter, BufferedReader, and BufferedRWPair buffer\n"
|
||||
"streams that are readable, writable, and both respectively.\n"
|
||||
"BufferedRandom provides a buffered interface to random access\n"
|
||||
"streams. BytesIO is a simple stream of in-memory bytes.\n"
|
||||
"\n"
|
||||
"Another IOBase subclass, TextIOBase, deals with the encoding and decoding\n"
|
||||
"of streams into text. TextIOWrapper, which extends it, is a buffered text\n"
|
||||
"interface to a buffered raw stream (`BufferedIOBase`). Finally, StringIO\n"
|
||||
"is a in-memory stream for text.\n"
|
||||
"\n"
|
||||
"Argument names are not part of the specification, and only the arguments\n"
|
||||
"of open() are intended to be used as keyword arguments.\n"
|
||||
"\n"
|
||||
"data:\n"
|
||||
"\n"
|
||||
"DEFAULT_BUFFER_SIZE\n"
|
||||
"\n"
|
||||
" An int containing the default buffer size used by the module's buffered\n"
|
||||
" I/O classes. open() uses the file's blksize (as obtained by os.stat) if\n"
|
||||
" possible.\n"
|
||||
);
|
||||
|
||||
|
||||
/*
|
||||
* BlockingIOError extends IOError
|
||||
*/
|
||||
|
||||
static int
|
||||
blockingioerror_init(PyBlockingIOErrorObject *self, PyObject *args,
|
||||
PyObject *kwds)
|
||||
{
|
||||
PyObject *myerrno = NULL, *strerror = NULL;
|
||||
PyObject *baseargs = NULL;
|
||||
Py_ssize_t written = 0;
|
||||
|
||||
assert(PyTuple_Check(args));
|
||||
|
||||
self->written = 0;
|
||||
if (!PyArg_ParseTuple(args, "OO|n:BlockingIOError",
|
||||
&myerrno, &strerror, &written))
|
||||
return -1;
|
||||
|
||||
baseargs = PyTuple_Pack(2, myerrno, strerror);
|
||||
if (baseargs == NULL)
|
||||
return -1;
|
||||
/* This will take care of initializing of myerrno and strerror members */
|
||||
if (((PyTypeObject *)PyExc_IOError)->tp_init(
|
||||
(PyObject *)self, baseargs, kwds) == -1) {
|
||||
Py_DECREF(baseargs);
|
||||
return -1;
|
||||
}
|
||||
Py_DECREF(baseargs);
|
||||
|
||||
self->written = written;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static PyMemberDef blockingioerror_members[] = {
|
||||
{"characters_written", T_PYSSIZET, offsetof(PyBlockingIOErrorObject, written), 0},
|
||||
{NULL} /* Sentinel */
|
||||
};
|
||||
|
||||
static PyTypeObject _PyExc_BlockingIOError = {
|
||||
PyVarObject_HEAD_INIT(NULL, 0)
|
||||
"BlockingIOError", /*tp_name*/
|
||||
sizeof(PyBlockingIOErrorObject), /*tp_basicsize*/
|
||||
0, /*tp_itemsize*/
|
||||
0, /*tp_dealloc*/
|
||||
0, /*tp_print*/
|
||||
0, /*tp_getattr*/
|
||||
0, /*tp_setattr*/
|
||||
0, /*tp_compare */
|
||||
0, /*tp_repr*/
|
||||
0, /*tp_as_number*/
|
||||
0, /*tp_as_sequence*/
|
||||
0, /*tp_as_mapping*/
|
||||
0, /*tp_hash */
|
||||
0, /*tp_call*/
|
||||
0, /*tp_str*/
|
||||
0, /*tp_getattro*/
|
||||
0, /*tp_setattro*/
|
||||
0, /*tp_as_buffer*/
|
||||
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
|
||||
PyDoc_STR("Exception raised when I/O would block "
|
||||
"on a non-blocking I/O stream"), /* tp_doc */
|
||||
0, /* tp_traverse */
|
||||
0, /* tp_clear */
|
||||
0, /* tp_richcompare */
|
||||
0, /* tp_weaklistoffset */
|
||||
0, /* tp_iter */
|
||||
0, /* tp_iternext */
|
||||
0, /* tp_methods */
|
||||
blockingioerror_members, /* tp_members */
|
||||
0, /* tp_getset */
|
||||
0, /* tp_base */
|
||||
0, /* tp_dict */
|
||||
0, /* tp_descr_get */
|
||||
0, /* tp_descr_set */
|
||||
0, /* tp_dictoffset */
|
||||
(initproc)blockingioerror_init, /* tp_init */
|
||||
0, /* tp_alloc */
|
||||
0, /* tp_new */
|
||||
};
|
||||
PyObject *PyExc_BlockingIOError = (PyObject *)&_PyExc_BlockingIOError;
|
||||
|
||||
|
||||
/*
|
||||
* The main open() function
|
||||
*/
|
||||
PyDoc_STRVAR(open_doc,
|
||||
"Open file and return a stream. Raise IOError upon failure.\n"
|
||||
"\n"
|
||||
"file is either a text or byte string giving the name (and the path\n"
|
||||
"if the file isn't in the current working directory) of the file to\n"
|
||||
"be opened or an integer file descriptor of the file to be\n"
|
||||
"wrapped. (If a file descriptor is given, it is closed when the\n"
|
||||
"returned I/O object is closed, unless closefd is set to False.)\n"
|
||||
"\n"
|
||||
"mode is an optional string that specifies the mode in which the file\n"
|
||||
"is opened. It defaults to 'r' which means open for reading in text\n"
|
||||
"mode. Other common values are 'w' for writing (truncating the file if\n"
|
||||
"it already exists), and 'a' for appending (which on some Unix systems,\n"
|
||||
"means that all writes append to the end of the file regardless of the\n"
|
||||
"current seek position). In text mode, if encoding is not specified the\n"
|
||||
"encoding used is platform dependent. (For reading and writing raw\n"
|
||||
"bytes use binary mode and leave encoding unspecified.) The available\n"
|
||||
"modes are:\n"
|
||||
"\n"
|
||||
"========= ===============================================================\n"
|
||||
"Character Meaning\n"
|
||||
"--------- ---------------------------------------------------------------\n"
|
||||
"'r' open for reading (default)\n"
|
||||
"'w' open for writing, truncating the file first\n"
|
||||
"'a' open for writing, appending to the end of the file if it exists\n"
|
||||
"'b' binary mode\n"
|
||||
"'t' text mode (default)\n"
|
||||
"'+' open a disk file for updating (reading and writing)\n"
|
||||
"'U' universal newline mode (for backwards compatibility; unneeded\n"
|
||||
" for new code)\n"
|
||||
"========= ===============================================================\n"
|
||||
"\n"
|
||||
"The default mode is 'rt' (open for reading text). For binary random\n"
|
||||
"access, the mode 'w+b' opens and truncates the file to 0 bytes, while\n"
|
||||
"'r+b' opens the file without truncation.\n"
|
||||
"\n"
|
||||
"Python distinguishes between files opened in binary and text modes,\n"
|
||||
"even when the underlying operating system doesn't. Files opened in\n"
|
||||
"binary mode (appending 'b' to the mode argument) return contents as\n"
|
||||
"bytes objects without any decoding. In text mode (the default, or when\n"
|
||||
"'t' is appended to the mode argument), the contents of the file are\n"
|
||||
"returned as strings, the bytes having been first decoded using a\n"
|
||||
"platform-dependent encoding or using the specified encoding if given.\n"
|
||||
"\n"
|
||||
"buffering is an optional integer used to set the buffering policy.\n"
|
||||
"Pass 0 to switch buffering off (only allowed in binary mode), 1 to select\n"
|
||||
"line buffering (only usable in text mode), and an integer > 1 to indicate\n"
|
||||
"the size of a fixed-size chunk buffer. When no buffering argument is\n"
|
||||
"given, the default buffering policy works as follows:\n"
|
||||
"\n"
|
||||
"* Binary files are buffered in fixed-size chunks; the size of the buffer\n"
|
||||
" is chosen using a heuristic trying to determine the underlying device's\n"
|
||||
" \"block size\" and falling back on `io.DEFAULT_BUFFER_SIZE`.\n"
|
||||
" On many systems, the buffer will typically be 4096 or 8192 bytes long.\n"
|
||||
"\n"
|
||||
"* \"Interactive\" text files (files for which isatty() returns True)\n"
|
||||
" use line buffering. Other text files use the policy described above\n"
|
||||
" for binary files.\n"
|
||||
"\n"
|
||||
"encoding is the name of the encoding used to decode or encode the\n"
|
||||
"file. This should only be used in text mode. The default encoding is\n"
|
||||
"platform dependent, but any encoding supported by Python can be\n"
|
||||
"passed. See the codecs module for the list of supported encodings.\n"
|
||||
"\n"
|
||||
"errors is an optional string that specifies how encoding errors are to\n"
|
||||
"be handled---this argument should not be used in binary mode. Pass\n"
|
||||
"'strict' to raise a ValueError exception if there is an encoding error\n"
|
||||
"(the default of None has the same effect), or pass 'ignore' to ignore\n"
|
||||
"errors. (Note that ignoring encoding errors can lead to data loss.)\n"
|
||||
"See the documentation for codecs.register for a list of the permitted\n"
|
||||
"encoding error strings.\n"
|
||||
"\n"
|
||||
"newline controls how universal newlines works (it only applies to text\n"
|
||||
"mode). It can be None, '', '\\n', '\\r', and '\\r\\n'. It works as\n"
|
||||
"follows:\n"
|
||||
"\n"
|
||||
"* On input, if newline is None, universal newlines mode is\n"
|
||||
" enabled. Lines in the input can end in '\\n', '\\r', or '\\r\\n', and\n"
|
||||
" these are translated into '\\n' before being returned to the\n"
|
||||
" caller. If it is '', universal newline mode is enabled, but line\n"
|
||||
" endings are returned to the caller untranslated. If it has any of\n"
|
||||
" the other legal values, input lines are only terminated by the given\n"
|
||||
" string, and the line ending is returned to the caller untranslated.\n"
|
||||
"\n"
|
||||
"* On output, if newline is None, any '\\n' characters written are\n"
|
||||
" translated to the system default line separator, os.linesep. If\n"
|
||||
" newline is '', no translation takes place. If newline is any of the\n"
|
||||
" other legal values, any '\\n' characters written are translated to\n"
|
||||
" the given string.\n"
|
||||
"\n"
|
||||
"If closefd is False, the underlying file descriptor will be kept open\n"
|
||||
"when the file is closed. This does not work when a file name is given\n"
|
||||
"and must be True in that case.\n"
|
||||
"\n"
|
||||
"open() returns a file object whose type depends on the mode, and\n"
|
||||
"through which the standard file operations such as reading and writing\n"
|
||||
"are performed. When open() is used to open a file in a text mode ('w',\n"
|
||||
"'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open\n"
|
||||
"a file in a binary mode, the returned class varies: in read binary\n"
|
||||
"mode, it returns a BufferedReader; in write binary and append binary\n"
|
||||
"modes, it returns a BufferedWriter, and in read/write mode, it returns\n"
|
||||
"a BufferedRandom.\n"
|
||||
"\n"
|
||||
"It is also possible to use a string or bytearray as a file for both\n"
|
||||
"reading and writing. For strings StringIO can be used like a file\n"
|
||||
"opened in a text mode, and for bytes a BytesIO can be used like a file\n"
|
||||
"opened in a binary mode.\n"
|
||||
);
|
||||
|
||||
static PyObject *
|
||||
io_open(PyObject *self, PyObject *args, PyObject *kwds)
|
||||
{
|
||||
char *kwlist[] = {"file", "mode", "buffering",
|
||||
"encoding", "errors", "newline",
|
||||
"closefd", NULL};
|
||||
PyObject *file;
|
||||
char *mode = "r";
|
||||
int buffering = -1, closefd = 1;
|
||||
char *encoding = NULL, *errors = NULL, *newline = NULL;
|
||||
unsigned i;
|
||||
|
||||
int reading = 0, writing = 0, appending = 0, updating = 0;
|
||||
int text = 0, binary = 0, universal = 0;
|
||||
|
||||
char rawmode[5], *m;
|
||||
int line_buffering;
|
||||
long isatty;
|
||||
|
||||
PyObject *raw, *modeobj = NULL, *buffer, *wrapper, *result = NULL;
|
||||
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|sizzzi:open", kwlist,
|
||||
&file, &mode, &buffering,
|
||||
&encoding, &errors, &newline,
|
||||
&closefd)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!PyUnicode_Check(file) &&
|
||||
!PyBytes_Check(file) &&
|
||||
!PyNumber_Check(file)) {
|
||||
PyObject *repr = PyObject_Repr(file);
|
||||
if (repr != NULL) {
|
||||
PyErr_Format(PyExc_TypeError, "invalid file: %s",
|
||||
PyString_AS_STRING(repr));
|
||||
Py_DECREF(repr);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Decode mode */
|
||||
for (i = 0; i < strlen(mode); i++) {
|
||||
char c = mode[i];
|
||||
|
||||
switch (c) {
|
||||
case 'r':
|
||||
reading = 1;
|
||||
break;
|
||||
case 'w':
|
||||
writing = 1;
|
||||
break;
|
||||
case 'a':
|
||||
appending = 1;
|
||||
break;
|
||||
case '+':
|
||||
updating = 1;
|
||||
break;
|
||||
case 't':
|
||||
text = 1;
|
||||
break;
|
||||
case 'b':
|
||||
binary = 1;
|
||||
break;
|
||||
case 'U':
|
||||
universal = 1;
|
||||
reading = 1;
|
||||
break;
|
||||
default:
|
||||
goto invalid_mode;
|
||||
}
|
||||
|
||||
/* c must not be duplicated */
|
||||
if (strchr(mode+i+1, c)) {
|
||||
invalid_mode:
|
||||
PyErr_Format(PyExc_ValueError, "invalid mode: '%s'", mode);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
m = rawmode;
|
||||
if (reading) *(m++) = 'r';
|
||||
if (writing) *(m++) = 'w';
|
||||
if (appending) *(m++) = 'a';
|
||||
if (updating) *(m++) = '+';
|
||||
*m = '\0';
|
||||
|
||||
/* Parameters validation */
|
||||
if (universal) {
|
||||
if (writing || appending) {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"can't use U and writing mode at once");
|
||||
return NULL;
|
||||
}
|
||||
reading = 1;
|
||||
}
|
||||
|
||||
if (text && binary) {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"can't have text and binary mode at once");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (reading + writing + appending > 1) {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"must have exactly one of read/write/append mode");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (binary && encoding != NULL) {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"binary mode doesn't take an encoding argument");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (binary && errors != NULL) {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"binary mode doesn't take an errors argument");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (binary && newline != NULL) {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"binary mode doesn't take a newline argument");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Create the Raw file stream */
|
||||
raw = PyObject_CallFunction((PyObject *)&PyFileIO_Type,
|
||||
"Osi", file, rawmode, closefd);
|
||||
if (raw == NULL)
|
||||
return NULL;
|
||||
result = raw;
|
||||
|
||||
modeobj = PyUnicode_FromString(mode);
|
||||
if (modeobj == NULL)
|
||||
goto error;
|
||||
|
||||
/* buffering */
|
||||
{
|
||||
PyObject *res = PyObject_CallMethod(raw, "isatty", NULL);
|
||||
if (res == NULL)
|
||||
goto error;
|
||||
isatty = PyLong_AsLong(res);
|
||||
Py_DECREF(res);
|
||||
if (isatty == -1 && PyErr_Occurred())
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (buffering == 1 || (buffering < 0 && isatty)) {
|
||||
buffering = -1;
|
||||
line_buffering = 1;
|
||||
}
|
||||
else
|
||||
line_buffering = 0;
|
||||
|
||||
if (buffering < 0) {
|
||||
buffering = DEFAULT_BUFFER_SIZE;
|
||||
#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
|
||||
{
|
||||
struct stat st;
|
||||
int fileno;
|
||||
PyObject *res = PyObject_CallMethod(raw, "fileno", NULL);
|
||||
if (res == NULL)
|
||||
goto error;
|
||||
|
||||
fileno = _PyInt_AsInt(res);
|
||||
Py_DECREF(res);
|
||||
if (fileno == -1 && PyErr_Occurred())
|
||||
goto error;
|
||||
|
||||
if (fstat(fileno, &st) >= 0 && st.st_blksize > 1)
|
||||
buffering = st.st_blksize;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
if (buffering < 0) {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"invalid buffering size");
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* if not buffering, returns the raw file object */
|
||||
if (buffering == 0) {
|
||||
if (!binary) {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"can't have unbuffered text I/O");
|
||||
goto error;
|
||||
}
|
||||
|
||||
Py_DECREF(modeobj);
|
||||
return result;
|
||||
}
|
||||
|
||||
/* wraps into a buffered file */
|
||||
{
|
||||
PyObject *Buffered_class;
|
||||
|
||||
if (updating)
|
||||
Buffered_class = (PyObject *)&PyBufferedRandom_Type;
|
||||
else if (writing || appending)
|
||||
Buffered_class = (PyObject *)&PyBufferedWriter_Type;
|
||||
else if (reading)
|
||||
Buffered_class = (PyObject *)&PyBufferedReader_Type;
|
||||
else {
|
||||
PyErr_Format(PyExc_ValueError,
|
||||
"unknown mode: '%s'", mode);
|
||||
goto error;
|
||||
}
|
||||
|
||||
buffer = PyObject_CallFunction(Buffered_class, "Oi", raw, buffering);
|
||||
}
|
||||
if (buffer == NULL)
|
||||
goto error;
|
||||
result = buffer;
|
||||
Py_DECREF(raw);
|
||||
|
||||
|
||||
/* if binary, returns the buffered file */
|
||||
if (binary) {
|
||||
Py_DECREF(modeobj);
|
||||
return result;
|
||||
}
|
||||
|
||||
/* wraps into a TextIOWrapper */
|
||||
wrapper = PyObject_CallFunction((PyObject *)&PyTextIOWrapper_Type,
|
||||
"Osssi",
|
||||
buffer,
|
||||
encoding, errors, newline,
|
||||
line_buffering);
|
||||
if (wrapper == NULL)
|
||||
goto error;
|
||||
result = wrapper;
|
||||
Py_DECREF(buffer);
|
||||
|
||||
if (PyObject_SetAttrString(wrapper, "mode", modeobj) < 0)
|
||||
goto error;
|
||||
Py_DECREF(modeobj);
|
||||
return result;
|
||||
|
||||
error:
|
||||
if (result != NULL) {
|
||||
PyObject *exc, *val, *tb, *close_result;
|
||||
PyErr_Fetch(&exc, &val, &tb);
|
||||
close_result = PyObject_CallMethod(result, "close", NULL);
|
||||
_PyErr_ReplaceException(exc, val, tb);
|
||||
Py_XDECREF(close_result);
|
||||
Py_DECREF(result);
|
||||
}
|
||||
Py_XDECREF(modeobj);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Private helpers for the io module.
|
||||
*/
|
||||
|
||||
Py_off_t
|
||||
PyNumber_AsOff_t(PyObject *item, PyObject *err)
|
||||
{
|
||||
Py_off_t result;
|
||||
PyObject *runerr;
|
||||
PyObject *value = PyNumber_Index(item);
|
||||
if (value == NULL)
|
||||
return -1;
|
||||
|
||||
if (PyInt_Check(value)) {
|
||||
/* We assume a long always fits in a Py_off_t... */
|
||||
result = (Py_off_t) PyInt_AS_LONG(value);
|
||||
goto finish;
|
||||
}
|
||||
|
||||
/* We're done if PyLong_AsSsize_t() returns without error. */
|
||||
result = PyLong_AsOff_t(value);
|
||||
if (result != -1 || !(runerr = PyErr_Occurred()))
|
||||
goto finish;
|
||||
|
||||
/* Error handling code -- only manage OverflowError differently */
|
||||
if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError))
|
||||
goto finish;
|
||||
|
||||
PyErr_Clear();
|
||||
/* If no error-handling desired then the default clipping
|
||||
is sufficient.
|
||||
*/
|
||||
if (!err) {
|
||||
assert(PyLong_Check(value));
|
||||
/* Whether or not it is less than or equal to
|
||||
zero is determined by the sign of ob_size
|
||||
*/
|
||||
if (_PyLong_Sign(value) < 0)
|
||||
result = PY_OFF_T_MIN;
|
||||
else
|
||||
result = PY_OFF_T_MAX;
|
||||
}
|
||||
else {
|
||||
/* Otherwise replace the error with caller's error object. */
|
||||
PyErr_Format(err,
|
||||
"cannot fit '%.200s' into an offset-sized integer",
|
||||
item->ob_type->tp_name);
|
||||
}
|
||||
|
||||
finish:
|
||||
Py_DECREF(value);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/* Basically the "n" format code with the ability to turn None into -1. */
|
||||
int
|
||||
_PyIO_ConvertSsize_t(PyObject *obj, void *result) {
|
||||
Py_ssize_t limit;
|
||||
if (obj == Py_None) {
|
||||
limit = -1;
|
||||
}
|
||||
else if (PyNumber_Check(obj)) {
|
||||
limit = PyNumber_AsSsize_t(obj, PyExc_OverflowError);
|
||||
if (limit == -1 && PyErr_Occurred())
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"integer argument expected, got '%.200s'",
|
||||
Py_TYPE(obj)->tp_name);
|
||||
return 0;
|
||||
}
|
||||
*((Py_ssize_t *)result) = limit;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Module definition
|
||||
*/
|
||||
|
||||
PyObject *_PyIO_os_module = NULL;
|
||||
PyObject *_PyIO_locale_module = NULL;
|
||||
PyObject *_PyIO_unsupported_operation = NULL;
|
||||
|
||||
static PyMethodDef module_methods[] = {
|
||||
{"open", (PyCFunction)io_open, METH_VARARGS|METH_KEYWORDS, open_doc},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
PyMODINIT_FUNC
|
||||
init_io(void)
|
||||
{
|
||||
PyObject *m = Py_InitModule4("_io", module_methods,
|
||||
module_doc, NULL, PYTHON_API_VERSION);
|
||||
if (m == NULL)
|
||||
return;
|
||||
|
||||
/* put os in the module state */
|
||||
_PyIO_os_module = PyImport_ImportModule("os");
|
||||
if (_PyIO_os_module == NULL)
|
||||
goto fail;
|
||||
|
||||
#define ADD_TYPE(type, name) \
|
||||
if (PyType_Ready(type) < 0) \
|
||||
goto fail; \
|
||||
Py_INCREF(type); \
|
||||
if (PyModule_AddObject(m, name, (PyObject *)type) < 0) { \
|
||||
Py_DECREF(type); \
|
||||
goto fail; \
|
||||
}
|
||||
|
||||
/* DEFAULT_BUFFER_SIZE */
|
||||
if (PyModule_AddIntMacro(m, DEFAULT_BUFFER_SIZE) < 0)
|
||||
goto fail;
|
||||
|
||||
/* UnsupportedOperation inherits from ValueError and IOError */
|
||||
_PyIO_unsupported_operation = PyObject_CallFunction(
|
||||
(PyObject *)&PyType_Type, "s(OO){}",
|
||||
"UnsupportedOperation", PyExc_ValueError, PyExc_IOError);
|
||||
if (_PyIO_unsupported_operation == NULL)
|
||||
goto fail;
|
||||
Py_INCREF(_PyIO_unsupported_operation);
|
||||
if (PyModule_AddObject(m, "UnsupportedOperation",
|
||||
_PyIO_unsupported_operation) < 0)
|
||||
goto fail;
|
||||
|
||||
/* BlockingIOError */
|
||||
_PyExc_BlockingIOError.tp_base = (PyTypeObject *) PyExc_IOError;
|
||||
ADD_TYPE(&_PyExc_BlockingIOError, "BlockingIOError");
|
||||
|
||||
/* Concrete base types of the IO ABCs.
|
||||
(the ABCs themselves are declared through inheritance in io.py)
|
||||
*/
|
||||
ADD_TYPE(&PyIOBase_Type, "_IOBase");
|
||||
ADD_TYPE(&PyRawIOBase_Type, "_RawIOBase");
|
||||
ADD_TYPE(&PyBufferedIOBase_Type, "_BufferedIOBase");
|
||||
ADD_TYPE(&PyTextIOBase_Type, "_TextIOBase");
|
||||
|
||||
/* Implementation of concrete IO objects. */
|
||||
/* FileIO */
|
||||
PyFileIO_Type.tp_base = &PyRawIOBase_Type;
|
||||
ADD_TYPE(&PyFileIO_Type, "FileIO");
|
||||
|
||||
/* BytesIO */
|
||||
PyBytesIO_Type.tp_base = &PyBufferedIOBase_Type;
|
||||
ADD_TYPE(&PyBytesIO_Type, "BytesIO");
|
||||
|
||||
/* StringIO */
|
||||
PyStringIO_Type.tp_base = &PyTextIOBase_Type;
|
||||
ADD_TYPE(&PyStringIO_Type, "StringIO");
|
||||
|
||||
/* BufferedReader */
|
||||
PyBufferedReader_Type.tp_base = &PyBufferedIOBase_Type;
|
||||
ADD_TYPE(&PyBufferedReader_Type, "BufferedReader");
|
||||
|
||||
/* BufferedWriter */
|
||||
PyBufferedWriter_Type.tp_base = &PyBufferedIOBase_Type;
|
||||
ADD_TYPE(&PyBufferedWriter_Type, "BufferedWriter");
|
||||
|
||||
/* BufferedRWPair */
|
||||
PyBufferedRWPair_Type.tp_base = &PyBufferedIOBase_Type;
|
||||
ADD_TYPE(&PyBufferedRWPair_Type, "BufferedRWPair");
|
||||
|
||||
/* BufferedRandom */
|
||||
PyBufferedRandom_Type.tp_base = &PyBufferedIOBase_Type;
|
||||
ADD_TYPE(&PyBufferedRandom_Type, "BufferedRandom");
|
||||
|
||||
/* TextIOWrapper */
|
||||
PyTextIOWrapper_Type.tp_base = &PyTextIOBase_Type;
|
||||
ADD_TYPE(&PyTextIOWrapper_Type, "TextIOWrapper");
|
||||
|
||||
/* IncrementalNewlineDecoder */
|
||||
ADD_TYPE(&PyIncrementalNewlineDecoder_Type, "IncrementalNewlineDecoder");
|
||||
|
||||
/* Interned strings */
|
||||
if (!(_PyIO_str_close = PyString_InternFromString("close")))
|
||||
goto fail;
|
||||
if (!(_PyIO_str_closed = PyString_InternFromString("closed")))
|
||||
goto fail;
|
||||
if (!(_PyIO_str_decode = PyString_InternFromString("decode")))
|
||||
goto fail;
|
||||
if (!(_PyIO_str_encode = PyString_InternFromString("encode")))
|
||||
goto fail;
|
||||
if (!(_PyIO_str_fileno = PyString_InternFromString("fileno")))
|
||||
goto fail;
|
||||
if (!(_PyIO_str_flush = PyString_InternFromString("flush")))
|
||||
goto fail;
|
||||
if (!(_PyIO_str_getstate = PyString_InternFromString("getstate")))
|
||||
goto fail;
|
||||
if (!(_PyIO_str_isatty = PyString_InternFromString("isatty")))
|
||||
goto fail;
|
||||
if (!(_PyIO_str_newlines = PyString_InternFromString("newlines")))
|
||||
goto fail;
|
||||
if (!(_PyIO_str_nl = PyString_InternFromString("\n")))
|
||||
goto fail;
|
||||
if (!(_PyIO_str_read = PyString_InternFromString("read")))
|
||||
goto fail;
|
||||
if (!(_PyIO_str_read1 = PyString_InternFromString("read1")))
|
||||
goto fail;
|
||||
if (!(_PyIO_str_readable = PyString_InternFromString("readable")))
|
||||
goto fail;
|
||||
if (!(_PyIO_str_readinto = PyString_InternFromString("readinto")))
|
||||
goto fail;
|
||||
if (!(_PyIO_str_readline = PyString_InternFromString("readline")))
|
||||
goto fail;
|
||||
if (!(_PyIO_str_reset = PyString_InternFromString("reset")))
|
||||
goto fail;
|
||||
if (!(_PyIO_str_seek = PyString_InternFromString("seek")))
|
||||
goto fail;
|
||||
if (!(_PyIO_str_seekable = PyString_InternFromString("seekable")))
|
||||
goto fail;
|
||||
if (!(_PyIO_str_setstate = PyString_InternFromString("setstate")))
|
||||
goto fail;
|
||||
if (!(_PyIO_str_tell = PyString_InternFromString("tell")))
|
||||
goto fail;
|
||||
if (!(_PyIO_str_truncate = PyString_InternFromString("truncate")))
|
||||
goto fail;
|
||||
if (!(_PyIO_str_write = PyString_InternFromString("write")))
|
||||
goto fail;
|
||||
if (!(_PyIO_str_writable = PyString_InternFromString("writable")))
|
||||
goto fail;
|
||||
|
||||
if (!(_PyIO_empty_str = PyUnicode_FromStringAndSize(NULL, 0)))
|
||||
goto fail;
|
||||
if (!(_PyIO_empty_bytes = PyBytes_FromStringAndSize(NULL, 0)))
|
||||
goto fail;
|
||||
if (!(_PyIO_zero = PyLong_FromLong(0L)))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
|
||||
fail:
|
||||
Py_CLEAR(_PyIO_os_module);
|
||||
Py_CLEAR(_PyIO_unsupported_operation);
|
||||
Py_DECREF(m);
|
||||
}
|
170
AppPkg/Applications/Python/Python-2.7.10/Modules/_io/_iomodule.h
Normal file
170
AppPkg/Applications/Python/Python-2.7.10/Modules/_io/_iomodule.h
Normal file
@ -0,0 +1,170 @@
|
||||
/*
|
||||
* Declarations shared between the different parts of the io module
|
||||
*/
|
||||
|
||||
/* ABCs */
|
||||
extern PyTypeObject PyIOBase_Type;
|
||||
extern PyTypeObject PyRawIOBase_Type;
|
||||
extern PyTypeObject PyBufferedIOBase_Type;
|
||||
extern PyTypeObject PyTextIOBase_Type;
|
||||
|
||||
/* Concrete classes */
|
||||
extern PyTypeObject PyFileIO_Type;
|
||||
extern PyTypeObject PyBytesIO_Type;
|
||||
extern PyTypeObject PyStringIO_Type;
|
||||
extern PyTypeObject PyBufferedReader_Type;
|
||||
extern PyTypeObject PyBufferedWriter_Type;
|
||||
extern PyTypeObject PyBufferedRWPair_Type;
|
||||
extern PyTypeObject PyBufferedRandom_Type;
|
||||
extern PyTypeObject PyTextIOWrapper_Type;
|
||||
extern PyTypeObject PyIncrementalNewlineDecoder_Type;
|
||||
|
||||
|
||||
extern int _PyIO_ConvertSsize_t(PyObject *, void *);
|
||||
|
||||
/* These functions are used as METH_NOARGS methods, are normally called
|
||||
* with args=NULL, and return a new reference.
|
||||
* BUT when args=Py_True is passed, they return a borrowed reference.
|
||||
*/
|
||||
extern PyObject* _PyIOBase_check_readable(PyObject *self, PyObject *args);
|
||||
extern PyObject* _PyIOBase_check_writable(PyObject *self, PyObject *args);
|
||||
extern PyObject* _PyIOBase_check_seekable(PyObject *self, PyObject *args);
|
||||
extern PyObject* _PyIOBase_check_closed(PyObject *self, PyObject *args);
|
||||
|
||||
/* Helper for finalization.
|
||||
This function will revive an object ready to be deallocated and try to
|
||||
close() it. It returns 0 if the object can be destroyed, or -1 if it
|
||||
is alive again. */
|
||||
extern int _PyIOBase_finalize(PyObject *self);
|
||||
|
||||
/* Returns true if the given FileIO object is closed.
|
||||
Doesn't check the argument type, so be careful! */
|
||||
extern int _PyFileIO_closed(PyObject *self);
|
||||
|
||||
/* Shortcut to the core of the IncrementalNewlineDecoder.decode method */
|
||||
extern PyObject *_PyIncrementalNewlineDecoder_decode(
|
||||
PyObject *self, PyObject *input, int final);
|
||||
|
||||
/* Finds the first line ending between `start` and `end`.
|
||||
If found, returns the index after the line ending and doesn't touch
|
||||
`*consumed`.
|
||||
If not found, returns -1 and sets `*consumed` to the number of characters
|
||||
which can be safely put aside until another search.
|
||||
|
||||
NOTE: for performance reasons, `end` must point to a NUL character ('\0').
|
||||
Otherwise, the function will scan further and return garbage. */
|
||||
extern Py_ssize_t _PyIO_find_line_ending(
|
||||
int translated, int universal, PyObject *readnl,
|
||||
Py_UNICODE *start, Py_UNICODE *end, Py_ssize_t *consumed);
|
||||
|
||||
/* Return 1 if an EnvironmentError with errno == EINTR is set (and then
|
||||
clears the error indicator), 0 otherwise.
|
||||
Should only be called when PyErr_Occurred() is true.
|
||||
*/
|
||||
extern int _PyIO_trap_eintr(void);
|
||||
|
||||
#define DEFAULT_BUFFER_SIZE (8 * 1024) /* bytes */
|
||||
|
||||
typedef struct {
|
||||
/* This is the equivalent of PyException_HEAD in 3.x */
|
||||
PyObject_HEAD
|
||||
PyObject *dict;
|
||||
PyObject *args;
|
||||
PyObject *message;
|
||||
|
||||
PyObject *myerrno;
|
||||
PyObject *strerror;
|
||||
PyObject *filename; /* Not used, but part of the IOError object */
|
||||
Py_ssize_t written;
|
||||
} PyBlockingIOErrorObject;
|
||||
extern PyObject *PyExc_BlockingIOError;
|
||||
|
||||
/*
|
||||
* Offset type for positioning.
|
||||
*/
|
||||
|
||||
/* Printing a variable of type off_t (with e.g., PyString_FromFormat)
|
||||
correctly and without producing compiler warnings is surprisingly painful.
|
||||
We identify an integer type whose size matches off_t and then: (1) cast the
|
||||
off_t to that integer type and (2) use the appropriate conversion
|
||||
specification. The cast is necessary: gcc complains about formatting a
|
||||
long with "%lld" even when both long and long long have the same
|
||||
precision. */
|
||||
|
||||
#if defined(MS_WIN64) || defined(MS_WINDOWS)
|
||||
|
||||
/* Windows uses long long for offsets */
|
||||
typedef PY_LONG_LONG Py_off_t;
|
||||
# define PyLong_AsOff_t PyLong_AsLongLong
|
||||
# define PyLong_FromOff_t PyLong_FromLongLong
|
||||
# define PY_OFF_T_MAX PY_LLONG_MAX
|
||||
# define PY_OFF_T_MIN PY_LLONG_MIN
|
||||
# define PY_OFF_T_COMPAT PY_LONG_LONG /* type compatible with off_t */
|
||||
# define PY_PRIdOFF "lld" /* format to use for that type */
|
||||
|
||||
#else
|
||||
|
||||
/* Other platforms use off_t */
|
||||
typedef off_t Py_off_t;
|
||||
#if (SIZEOF_OFF_T == SIZEOF_SIZE_T)
|
||||
# define PyLong_AsOff_t PyLong_AsSsize_t
|
||||
# define PyLong_FromOff_t PyLong_FromSsize_t
|
||||
# define PY_OFF_T_MAX PY_SSIZE_T_MAX
|
||||
# define PY_OFF_T_MIN PY_SSIZE_T_MIN
|
||||
# define PY_OFF_T_COMPAT Py_ssize_t
|
||||
# define PY_PRIdOFF "zd"
|
||||
#elif (HAVE_LONG_LONG && SIZEOF_OFF_T == SIZEOF_LONG_LONG)
|
||||
# define PyLong_AsOff_t PyLong_AsLongLong
|
||||
# define PyLong_FromOff_t PyLong_FromLongLong
|
||||
# define PY_OFF_T_MAX PY_LLONG_MAX
|
||||
# define PY_OFF_T_MIN PY_LLONG_MIN
|
||||
# define PY_OFF_T_COMPAT PY_LONG_LONG
|
||||
# define PY_PRIdOFF "lld"
|
||||
#elif (SIZEOF_OFF_T == SIZEOF_LONG)
|
||||
# define PyLong_AsOff_t PyLong_AsLong
|
||||
# define PyLong_FromOff_t PyLong_FromLong
|
||||
# define PY_OFF_T_MAX LONG_MAX
|
||||
# define PY_OFF_T_MIN LONG_MIN
|
||||
# define PY_OFF_T_COMPAT long
|
||||
# define PY_PRIdOFF "ld"
|
||||
#else
|
||||
# error off_t does not match either size_t, long, or long long!
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
extern Py_off_t PyNumber_AsOff_t(PyObject *item, PyObject *err);
|
||||
|
||||
/* Implementation details */
|
||||
|
||||
extern PyObject *_PyIO_os_module;
|
||||
extern PyObject *_PyIO_locale_module;
|
||||
extern PyObject *_PyIO_unsupported_operation;
|
||||
|
||||
extern PyObject *_PyIO_str_close;
|
||||
extern PyObject *_PyIO_str_closed;
|
||||
extern PyObject *_PyIO_str_decode;
|
||||
extern PyObject *_PyIO_str_encode;
|
||||
extern PyObject *_PyIO_str_fileno;
|
||||
extern PyObject *_PyIO_str_flush;
|
||||
extern PyObject *_PyIO_str_getstate;
|
||||
extern PyObject *_PyIO_str_isatty;
|
||||
extern PyObject *_PyIO_str_newlines;
|
||||
extern PyObject *_PyIO_str_nl;
|
||||
extern PyObject *_PyIO_str_read;
|
||||
extern PyObject *_PyIO_str_read1;
|
||||
extern PyObject *_PyIO_str_readable;
|
||||
extern PyObject *_PyIO_str_readinto;
|
||||
extern PyObject *_PyIO_str_readline;
|
||||
extern PyObject *_PyIO_str_reset;
|
||||
extern PyObject *_PyIO_str_seek;
|
||||
extern PyObject *_PyIO_str_seekable;
|
||||
extern PyObject *_PyIO_str_setstate;
|
||||
extern PyObject *_PyIO_str_tell;
|
||||
extern PyObject *_PyIO_str_truncate;
|
||||
extern PyObject *_PyIO_str_writable;
|
||||
extern PyObject *_PyIO_str_write;
|
||||
|
||||
extern PyObject *_PyIO_empty_str;
|
||||
extern PyObject *_PyIO_empty_bytes;
|
||||
extern PyObject *_PyIO_zero;
|
2446
AppPkg/Applications/Python/Python-2.7.10/Modules/_io/bufferedio.c
Normal file
2446
AppPkg/Applications/Python/Python-2.7.10/Modules/_io/bufferedio.c
Normal file
File diff suppressed because it is too large
Load Diff
909
AppPkg/Applications/Python/Python-2.7.10/Modules/_io/bytesio.c
Normal file
909
AppPkg/Applications/Python/Python-2.7.10/Modules/_io/bytesio.c
Normal file
@ -0,0 +1,909 @@
|
||||
#include "Python.h"
|
||||
#include "structmember.h" /* for offsetof() */
|
||||
#include "_iomodule.h"
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
char *buf;
|
||||
Py_ssize_t pos;
|
||||
Py_ssize_t string_size;
|
||||
size_t buf_size;
|
||||
PyObject *dict;
|
||||
PyObject *weakreflist;
|
||||
} bytesio;
|
||||
|
||||
#define CHECK_CLOSED(self) \
|
||||
if ((self)->buf == NULL) { \
|
||||
PyErr_SetString(PyExc_ValueError, \
|
||||
"I/O operation on closed file."); \
|
||||
return NULL; \
|
||||
}
|
||||
|
||||
/* Internal routine to get a line from the buffer of a BytesIO
|
||||
object. Returns the length between the current position to the
|
||||
next newline character. */
|
||||
static Py_ssize_t
|
||||
get_line(bytesio *self, char **output)
|
||||
{
|
||||
char *n;
|
||||
const char *str_end;
|
||||
Py_ssize_t len;
|
||||
|
||||
assert(self->buf != NULL);
|
||||
|
||||
/* Move to the end of the line, up to the end of the string, s. */
|
||||
str_end = self->buf + self->string_size;
|
||||
for (n = self->buf + self->pos;
|
||||
n < str_end && *n != '\n';
|
||||
n++);
|
||||
|
||||
/* Skip the newline character */
|
||||
if (n < str_end)
|
||||
n++;
|
||||
|
||||
/* Get the length from the current position to the end of the line. */
|
||||
len = n - (self->buf + self->pos);
|
||||
*output = self->buf + self->pos;
|
||||
|
||||
assert(len >= 0);
|
||||
assert(self->pos < PY_SSIZE_T_MAX - len);
|
||||
self->pos += len;
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
/* Internal routine for changing the size of the buffer of BytesIO objects.
|
||||
The caller should ensure that the 'size' argument is non-negative. Returns
|
||||
0 on success, -1 otherwise. */
|
||||
static int
|
||||
resize_buffer(bytesio *self, size_t size)
|
||||
{
|
||||
/* Here, unsigned types are used to avoid dealing with signed integer
|
||||
overflow, which is undefined in C. */
|
||||
size_t alloc = self->buf_size;
|
||||
char *new_buf = NULL;
|
||||
|
||||
assert(self->buf != NULL);
|
||||
|
||||
/* For simplicity, stay in the range of the signed type. Anyway, Python
|
||||
doesn't allow strings to be longer than this. */
|
||||
if (size > PY_SSIZE_T_MAX)
|
||||
goto overflow;
|
||||
|
||||
if (size < alloc / 2) {
|
||||
/* Major downsize; resize down to exact size. */
|
||||
alloc = size + 1;
|
||||
}
|
||||
else if (size < alloc) {
|
||||
/* Within allocated size; quick exit */
|
||||
return 0;
|
||||
}
|
||||
else if (size <= alloc * 1.125) {
|
||||
/* Moderate upsize; overallocate similar to list_resize() */
|
||||
alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
|
||||
}
|
||||
else {
|
||||
/* Major upsize; resize up to exact size */
|
||||
alloc = size + 1;
|
||||
}
|
||||
|
||||
if (alloc > ((size_t)-1) / sizeof(char))
|
||||
goto overflow;
|
||||
new_buf = (char *)PyMem_Realloc(self->buf, alloc * sizeof(char));
|
||||
if (new_buf == NULL) {
|
||||
PyErr_NoMemory();
|
||||
return -1;
|
||||
}
|
||||
self->buf_size = alloc;
|
||||
self->buf = new_buf;
|
||||
|
||||
return 0;
|
||||
|
||||
overflow:
|
||||
PyErr_SetString(PyExc_OverflowError,
|
||||
"new buffer size too large");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Internal routine for writing a string of bytes to the buffer of a BytesIO
|
||||
object. Returns the number of bytes written, or -1 on error. */
|
||||
static Py_ssize_t
|
||||
write_bytes(bytesio *self, const char *bytes, Py_ssize_t len)
|
||||
{
|
||||
assert(self->buf != NULL);
|
||||
assert(self->pos >= 0);
|
||||
assert(len >= 0);
|
||||
|
||||
if ((size_t)self->pos + len > self->buf_size) {
|
||||
if (resize_buffer(self, (size_t)self->pos + len) < 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (self->pos > self->string_size) {
|
||||
/* In case of overseek, pad with null bytes the buffer region between
|
||||
the end of stream and the current position.
|
||||
|
||||
0 lo string_size hi
|
||||
| |<---used--->|<----------available----------->|
|
||||
| | <--to pad-->|<---to write---> |
|
||||
0 buf position
|
||||
*/
|
||||
memset(self->buf + self->string_size, '\0',
|
||||
(self->pos - self->string_size) * sizeof(char));
|
||||
}
|
||||
|
||||
/* Copy the data to the internal buffer, overwriting some of the existing
|
||||
data if self->pos < self->string_size. */
|
||||
memcpy(self->buf + self->pos, bytes, len);
|
||||
self->pos += len;
|
||||
|
||||
/* Set the new length of the internal string if it has changed. */
|
||||
if (self->string_size < self->pos) {
|
||||
self->string_size = self->pos;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
bytesio_get_closed(bytesio *self)
|
||||
{
|
||||
if (self->buf == NULL) {
|
||||
Py_RETURN_TRUE;
|
||||
}
|
||||
else {
|
||||
Py_RETURN_FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(readable_doc,
|
||||
"readable() -> bool. Returns True if the IO object can be read.");
|
||||
|
||||
PyDoc_STRVAR(writable_doc,
|
||||
"writable() -> bool. Returns True if the IO object can be written.");
|
||||
|
||||
PyDoc_STRVAR(seekable_doc,
|
||||
"seekable() -> bool. Returns True if the IO object can be seeked.");
|
||||
|
||||
/* Generic getter for the writable, readable and seekable properties */
|
||||
static PyObject *
|
||||
return_not_closed(bytesio *self)
|
||||
{
|
||||
CHECK_CLOSED(self);
|
||||
Py_RETURN_TRUE;
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(flush_doc,
|
||||
"flush() -> None. Does nothing.");
|
||||
|
||||
static PyObject *
|
||||
bytesio_flush(bytesio *self)
|
||||
{
|
||||
CHECK_CLOSED(self);
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(getval_doc,
|
||||
"getvalue() -> bytes.\n"
|
||||
"\n"
|
||||
"Retrieve the entire contents of the BytesIO object.");
|
||||
|
||||
static PyObject *
|
||||
bytesio_getvalue(bytesio *self)
|
||||
{
|
||||
CHECK_CLOSED(self);
|
||||
return PyBytes_FromStringAndSize(self->buf, self->string_size);
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(isatty_doc,
|
||||
"isatty() -> False.\n"
|
||||
"\n"
|
||||
"Always returns False since BytesIO objects are not connected\n"
|
||||
"to a tty-like device.");
|
||||
|
||||
static PyObject *
|
||||
bytesio_isatty(bytesio *self)
|
||||
{
|
||||
CHECK_CLOSED(self);
|
||||
Py_RETURN_FALSE;
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(tell_doc,
|
||||
"tell() -> current file position, an integer\n");
|
||||
|
||||
static PyObject *
|
||||
bytesio_tell(bytesio *self)
|
||||
{
|
||||
CHECK_CLOSED(self);
|
||||
return PyLong_FromSsize_t(self->pos);
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(read_doc,
|
||||
"read([size]) -> read at most size bytes, returned as a string.\n"
|
||||
"\n"
|
||||
"If the size argument is negative, read until EOF is reached.\n"
|
||||
"Return an empty string at EOF.");
|
||||
|
||||
static PyObject *
|
||||
bytesio_read(bytesio *self, PyObject *args)
|
||||
{
|
||||
Py_ssize_t size, n;
|
||||
char *output;
|
||||
PyObject *arg = Py_None;
|
||||
|
||||
CHECK_CLOSED(self);
|
||||
|
||||
if (!PyArg_ParseTuple(args, "|O:read", &arg))
|
||||
return NULL;
|
||||
|
||||
if (PyNumber_Check(arg)) {
|
||||
size = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
|
||||
if (size == -1 && PyErr_Occurred())
|
||||
return NULL;
|
||||
}
|
||||
else if (arg == Py_None) {
|
||||
/* Read until EOF is reached, by default. */
|
||||
size = -1;
|
||||
}
|
||||
else {
|
||||
PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
|
||||
Py_TYPE(arg)->tp_name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* adjust invalid sizes */
|
||||
n = self->string_size - self->pos;
|
||||
if (size < 0 || size > n) {
|
||||
size = n;
|
||||
if (size < 0)
|
||||
size = 0;
|
||||
}
|
||||
|
||||
assert(self->buf != NULL);
|
||||
output = self->buf + self->pos;
|
||||
self->pos += size;
|
||||
|
||||
return PyBytes_FromStringAndSize(output, size);
|
||||
}
|
||||
|
||||
|
||||
PyDoc_STRVAR(read1_doc,
|
||||
"read1(size) -> read at most size bytes, returned as a string.\n"
|
||||
"\n"
|
||||
"If the size argument is negative or omitted, read until EOF is reached.\n"
|
||||
"Return an empty string at EOF.");
|
||||
|
||||
static PyObject *
|
||||
bytesio_read1(bytesio *self, PyObject *n)
|
||||
{
|
||||
PyObject *arg, *res;
|
||||
|
||||
arg = PyTuple_Pack(1, n);
|
||||
if (arg == NULL)
|
||||
return NULL;
|
||||
res = bytesio_read(self, arg);
|
||||
Py_DECREF(arg);
|
||||
return res;
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(readline_doc,
|
||||
"readline([size]) -> next line from the file, as a string.\n"
|
||||
"\n"
|
||||
"Retain newline. A non-negative size argument limits the maximum\n"
|
||||
"number of bytes to return (an incomplete line may be returned then).\n"
|
||||
"Return an empty string at EOF.\n");
|
||||
|
||||
static PyObject *
|
||||
bytesio_readline(bytesio *self, PyObject *args)
|
||||
{
|
||||
Py_ssize_t size, n;
|
||||
char *output;
|
||||
PyObject *arg = Py_None;
|
||||
|
||||
CHECK_CLOSED(self);
|
||||
|
||||
if (!PyArg_ParseTuple(args, "|O:readline", &arg))
|
||||
return NULL;
|
||||
|
||||
if (PyNumber_Check(arg)) {
|
||||
size = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
|
||||
if (size == -1 && PyErr_Occurred())
|
||||
return NULL;
|
||||
}
|
||||
else if (arg == Py_None) {
|
||||
/* No size limit, by default. */
|
||||
size = -1;
|
||||
}
|
||||
else {
|
||||
PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
|
||||
Py_TYPE(arg)->tp_name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
n = get_line(self, &output);
|
||||
|
||||
if (size >= 0 && size < n) {
|
||||
size = n - size;
|
||||
n -= size;
|
||||
self->pos -= size;
|
||||
}
|
||||
|
||||
return PyBytes_FromStringAndSize(output, n);
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(readlines_doc,
|
||||
"readlines([size]) -> list of strings, each a line from the file.\n"
|
||||
"\n"
|
||||
"Call readline() repeatedly and return a list of the lines so read.\n"
|
||||
"The optional size argument, if given, is an approximate bound on the\n"
|
||||
"total number of bytes in the lines returned.\n");
|
||||
|
||||
static PyObject *
|
||||
bytesio_readlines(bytesio *self, PyObject *args)
|
||||
{
|
||||
Py_ssize_t maxsize, size, n;
|
||||
PyObject *result, *line;
|
||||
char *output;
|
||||
PyObject *arg = Py_None;
|
||||
|
||||
CHECK_CLOSED(self);
|
||||
|
||||
if (!PyArg_ParseTuple(args, "|O:readlines", &arg))
|
||||
return NULL;
|
||||
|
||||
if (PyNumber_Check(arg)) {
|
||||
maxsize = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
|
||||
if (maxsize == -1 && PyErr_Occurred())
|
||||
return NULL;
|
||||
}
|
||||
else if (arg == Py_None) {
|
||||
/* No size limit, by default. */
|
||||
maxsize = -1;
|
||||
}
|
||||
else {
|
||||
PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
|
||||
Py_TYPE(arg)->tp_name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size = 0;
|
||||
result = PyList_New(0);
|
||||
if (!result)
|
||||
return NULL;
|
||||
|
||||
while ((n = get_line(self, &output)) != 0) {
|
||||
line = PyBytes_FromStringAndSize(output, n);
|
||||
if (!line)
|
||||
goto on_error;
|
||||
if (PyList_Append(result, line) == -1) {
|
||||
Py_DECREF(line);
|
||||
goto on_error;
|
||||
}
|
||||
Py_DECREF(line);
|
||||
size += n;
|
||||
if (maxsize > 0 && size >= maxsize)
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
|
||||
on_error:
|
||||
Py_DECREF(result);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(readinto_doc,
|
||||
"readinto(bytearray) -> int. Read up to len(b) bytes into b.\n"
|
||||
"\n"
|
||||
"Returns number of bytes read (0 for EOF), or None if the object\n"
|
||||
"is set not to block as has no data to read.");
|
||||
|
||||
static PyObject *
|
||||
bytesio_readinto(bytesio *self, PyObject *args)
|
||||
{
|
||||
Py_buffer buf;
|
||||
Py_ssize_t len, n;
|
||||
|
||||
CHECK_CLOSED(self);
|
||||
|
||||
if (!PyArg_ParseTuple(args, "w*", &buf))
|
||||
return NULL;
|
||||
|
||||
len = buf.len;
|
||||
/* adjust invalid sizes */
|
||||
n = self->string_size - self->pos;
|
||||
if (len > n) {
|
||||
len = n;
|
||||
if (len < 0)
|
||||
len = 0;
|
||||
}
|
||||
|
||||
memcpy(buf.buf, self->buf + self->pos, len);
|
||||
assert(self->pos + len < PY_SSIZE_T_MAX);
|
||||
assert(len >= 0);
|
||||
self->pos += len;
|
||||
|
||||
PyBuffer_Release(&buf);
|
||||
return PyLong_FromSsize_t(len);
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(truncate_doc,
|
||||
"truncate([size]) -> int. Truncate the file to at most size bytes.\n"
|
||||
"\n"
|
||||
"Size defaults to the current file position, as returned by tell().\n"
|
||||
"The current file position is unchanged. Returns the new size.\n");
|
||||
|
||||
static PyObject *
|
||||
bytesio_truncate(bytesio *self, PyObject *args)
|
||||
{
|
||||
Py_ssize_t size;
|
||||
PyObject *arg = Py_None;
|
||||
|
||||
CHECK_CLOSED(self);
|
||||
|
||||
if (!PyArg_ParseTuple(args, "|O:truncate", &arg))
|
||||
return NULL;
|
||||
|
||||
if (PyNumber_Check(arg)) {
|
||||
size = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
|
||||
if (size == -1 && PyErr_Occurred())
|
||||
return NULL;
|
||||
}
|
||||
else if (arg == Py_None) {
|
||||
/* Truncate to current position if no argument is passed. */
|
||||
size = self->pos;
|
||||
}
|
||||
else {
|
||||
PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
|
||||
Py_TYPE(arg)->tp_name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (size < 0) {
|
||||
PyErr_Format(PyExc_ValueError,
|
||||
"negative size value %zd", size);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (size < self->string_size) {
|
||||
self->string_size = size;
|
||||
if (resize_buffer(self, size) < 0)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return PyLong_FromSsize_t(size);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
bytesio_iternext(bytesio *self)
|
||||
{
|
||||
char *next;
|
||||
Py_ssize_t n;
|
||||
|
||||
CHECK_CLOSED(self);
|
||||
|
||||
n = get_line(self, &next);
|
||||
|
||||
if (!next || n == 0)
|
||||
return NULL;
|
||||
|
||||
return PyBytes_FromStringAndSize(next, n);
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(seek_doc,
|
||||
"seek(pos, whence=0) -> int. Change stream position.\n"
|
||||
"\n"
|
||||
"Seek to byte offset pos relative to position indicated by whence:\n"
|
||||
" 0 Start of stream (the default). pos should be >= 0;\n"
|
||||
" 1 Current position - pos may be negative;\n"
|
||||
" 2 End of stream - pos usually negative.\n"
|
||||
"Returns the new absolute position.");
|
||||
|
||||
static PyObject *
|
||||
bytesio_seek(bytesio *self, PyObject *args)
|
||||
{
|
||||
PyObject *posobj;
|
||||
Py_ssize_t pos;
|
||||
int mode = 0;
|
||||
|
||||
CHECK_CLOSED(self);
|
||||
|
||||
if (!PyArg_ParseTuple(args, "O|i:seek", &posobj, &mode))
|
||||
return NULL;
|
||||
|
||||
pos = PyNumber_AsSsize_t(posobj, PyExc_OverflowError);
|
||||
if (pos == -1 && PyErr_Occurred())
|
||||
return NULL;
|
||||
|
||||
if (pos < 0 && mode == 0) {
|
||||
PyErr_Format(PyExc_ValueError,
|
||||
"negative seek value %zd", pos);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* mode 0: offset relative to beginning of the string.
|
||||
mode 1: offset relative to current position.
|
||||
mode 2: offset relative the end of the string. */
|
||||
if (mode == 1) {
|
||||
if (pos > PY_SSIZE_T_MAX - self->pos) {
|
||||
PyErr_SetString(PyExc_OverflowError,
|
||||
"new position too large");
|
||||
return NULL;
|
||||
}
|
||||
pos += self->pos;
|
||||
}
|
||||
else if (mode == 2) {
|
||||
if (pos > PY_SSIZE_T_MAX - self->string_size) {
|
||||
PyErr_SetString(PyExc_OverflowError,
|
||||
"new position too large");
|
||||
return NULL;
|
||||
}
|
||||
pos += self->string_size;
|
||||
}
|
||||
else if (mode != 0) {
|
||||
PyErr_Format(PyExc_ValueError,
|
||||
"invalid whence (%i, should be 0, 1 or 2)", mode);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (pos < 0)
|
||||
pos = 0;
|
||||
self->pos = pos;
|
||||
|
||||
return PyLong_FromSsize_t(self->pos);
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(write_doc,
|
||||
"write(bytes) -> int. Write bytes to file.\n"
|
||||
"\n"
|
||||
"Return the number of bytes written.");
|
||||
|
||||
static PyObject *
|
||||
bytesio_write(bytesio *self, PyObject *obj)
|
||||
{
|
||||
Py_ssize_t n = 0;
|
||||
Py_buffer buf;
|
||||
PyObject *result = NULL;
|
||||
|
||||
CHECK_CLOSED(self);
|
||||
|
||||
if (PyObject_GetBuffer(obj, &buf, PyBUF_CONTIG_RO) < 0)
|
||||
return NULL;
|
||||
|
||||
if (buf.len != 0)
|
||||
n = write_bytes(self, buf.buf, buf.len);
|
||||
if (n >= 0)
|
||||
result = PyLong_FromSsize_t(n);
|
||||
|
||||
PyBuffer_Release(&buf);
|
||||
return result;
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(writelines_doc,
|
||||
"writelines(sequence_of_strings) -> None. Write strings to the file.\n"
|
||||
"\n"
|
||||
"Note that newlines are not added. The sequence can be any iterable\n"
|
||||
"object producing strings. This is equivalent to calling write() for\n"
|
||||
"each string.");
|
||||
|
||||
static PyObject *
|
||||
bytesio_writelines(bytesio *self, PyObject *v)
|
||||
{
|
||||
PyObject *it, *item;
|
||||
PyObject *ret;
|
||||
|
||||
CHECK_CLOSED(self);
|
||||
|
||||
it = PyObject_GetIter(v);
|
||||
if (it == NULL)
|
||||
return NULL;
|
||||
|
||||
while ((item = PyIter_Next(it)) != NULL) {
|
||||
ret = bytesio_write(self, item);
|
||||
Py_DECREF(item);
|
||||
if (ret == NULL) {
|
||||
Py_DECREF(it);
|
||||
return NULL;
|
||||
}
|
||||
Py_DECREF(ret);
|
||||
}
|
||||
Py_DECREF(it);
|
||||
|
||||
/* See if PyIter_Next failed */
|
||||
if (PyErr_Occurred())
|
||||
return NULL;
|
||||
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(close_doc,
|
||||
"close() -> None. Disable all I/O operations.");
|
||||
|
||||
static PyObject *
|
||||
bytesio_close(bytesio *self)
|
||||
{
|
||||
if (self->buf != NULL) {
|
||||
PyMem_Free(self->buf);
|
||||
self->buf = NULL;
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
/* Pickling support.
|
||||
|
||||
Note that only pickle protocol 2 and onward are supported since we use
|
||||
extended __reduce__ API of PEP 307 to make BytesIO instances picklable.
|
||||
|
||||
Providing support for protocol < 2 would require the __reduce_ex__ method
|
||||
which is notably long-winded when defined properly.
|
||||
|
||||
For BytesIO, the implementation would similar to one coded for
|
||||
object.__reduce_ex__, but slightly less general. To be more specific, we
|
||||
could call bytesio_getstate directly and avoid checking for the presence of
|
||||
a fallback __reduce__ method. However, we would still need a __newobj__
|
||||
function to use the efficient instance representation of PEP 307.
|
||||
*/
|
||||
|
||||
static PyObject *
|
||||
bytesio_getstate(bytesio *self)
|
||||
{
|
||||
PyObject *initvalue = bytesio_getvalue(self);
|
||||
PyObject *dict;
|
||||
PyObject *state;
|
||||
|
||||
if (initvalue == NULL)
|
||||
return NULL;
|
||||
if (self->dict == NULL) {
|
||||
Py_INCREF(Py_None);
|
||||
dict = Py_None;
|
||||
}
|
||||
else {
|
||||
dict = PyDict_Copy(self->dict);
|
||||
if (dict == NULL)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
state = Py_BuildValue("(OnN)", initvalue, self->pos, dict);
|
||||
Py_DECREF(initvalue);
|
||||
return state;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
bytesio_setstate(bytesio *self, PyObject *state)
|
||||
{
|
||||
PyObject *result;
|
||||
PyObject *position_obj;
|
||||
PyObject *dict;
|
||||
Py_ssize_t pos;
|
||||
|
||||
assert(state != NULL);
|
||||
|
||||
/* We allow the state tuple to be longer than 3, because we may need
|
||||
someday to extend the object's state without breaking
|
||||
backward-compatibility. */
|
||||
if (!PyTuple_Check(state) || Py_SIZE(state) < 3) {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"%.200s.__setstate__ argument should be 3-tuple, got %.200s",
|
||||
Py_TYPE(self)->tp_name, Py_TYPE(state)->tp_name);
|
||||
return NULL;
|
||||
}
|
||||
/* Reset the object to its default state. This is only needed to handle
|
||||
the case of repeated calls to __setstate__. */
|
||||
self->string_size = 0;
|
||||
self->pos = 0;
|
||||
|
||||
/* Set the value of the internal buffer. If state[0] does not support the
|
||||
buffer protocol, bytesio_write will raise the appropriate TypeError. */
|
||||
result = bytesio_write(self, PyTuple_GET_ITEM(state, 0));
|
||||
if (result == NULL)
|
||||
return NULL;
|
||||
Py_DECREF(result);
|
||||
|
||||
/* Set carefully the position value. Alternatively, we could use the seek
|
||||
method instead of modifying self->pos directly to better protect the
|
||||
object internal state against errneous (or malicious) inputs. */
|
||||
position_obj = PyTuple_GET_ITEM(state, 1);
|
||||
if (!PyIndex_Check(position_obj)) {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"second item of state must be an integer, not %.200s",
|
||||
Py_TYPE(position_obj)->tp_name);
|
||||
return NULL;
|
||||
}
|
||||
pos = PyNumber_AsSsize_t(position_obj, PyExc_OverflowError);
|
||||
if (pos == -1 && PyErr_Occurred())
|
||||
return NULL;
|
||||
if (pos < 0) {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"position value cannot be negative");
|
||||
return NULL;
|
||||
}
|
||||
self->pos = pos;
|
||||
|
||||
/* Set the dictionary of the instance variables. */
|
||||
dict = PyTuple_GET_ITEM(state, 2);
|
||||
if (dict != Py_None) {
|
||||
if (!PyDict_Check(dict)) {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"third item of state should be a dict, got a %.200s",
|
||||
Py_TYPE(dict)->tp_name);
|
||||
return NULL;
|
||||
}
|
||||
if (self->dict) {
|
||||
/* Alternatively, we could replace the internal dictionary
|
||||
completely. However, it seems more practical to just update it. */
|
||||
if (PyDict_Update(self->dict, dict) < 0)
|
||||
return NULL;
|
||||
}
|
||||
else {
|
||||
Py_INCREF(dict);
|
||||
self->dict = dict;
|
||||
}
|
||||
}
|
||||
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static void
|
||||
bytesio_dealloc(bytesio *self)
|
||||
{
|
||||
_PyObject_GC_UNTRACK(self);
|
||||
if (self->buf != NULL) {
|
||||
PyMem_Free(self->buf);
|
||||
self->buf = NULL;
|
||||
}
|
||||
Py_CLEAR(self->dict);
|
||||
if (self->weakreflist != NULL)
|
||||
PyObject_ClearWeakRefs((PyObject *) self);
|
||||
Py_TYPE(self)->tp_free(self);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
bytesio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||
{
|
||||
bytesio *self;
|
||||
|
||||
assert(type != NULL && type->tp_alloc != NULL);
|
||||
self = (bytesio *)type->tp_alloc(type, 0);
|
||||
if (self == NULL)
|
||||
return NULL;
|
||||
|
||||
/* tp_alloc initializes all the fields to zero. So we don't have to
|
||||
initialize them here. */
|
||||
|
||||
self->buf = (char *)PyMem_Malloc(0);
|
||||
if (self->buf == NULL) {
|
||||
Py_DECREF(self);
|
||||
return PyErr_NoMemory();
|
||||
}
|
||||
|
||||
return (PyObject *)self;
|
||||
}
|
||||
|
||||
static int
|
||||
bytesio_init(bytesio *self, PyObject *args, PyObject *kwds)
|
||||
{
|
||||
char *kwlist[] = {"initial_bytes", NULL};
|
||||
PyObject *initvalue = NULL;
|
||||
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:BytesIO", kwlist,
|
||||
&initvalue))
|
||||
return -1;
|
||||
|
||||
/* In case, __init__ is called multiple times. */
|
||||
self->string_size = 0;
|
||||
self->pos = 0;
|
||||
|
||||
if (initvalue && initvalue != Py_None) {
|
||||
PyObject *res;
|
||||
res = bytesio_write(self, initvalue);
|
||||
if (res == NULL)
|
||||
return -1;
|
||||
Py_DECREF(res);
|
||||
self->pos = 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
bytesio_sizeof(bytesio *self, void *unused)
|
||||
{
|
||||
Py_ssize_t res;
|
||||
|
||||
res = sizeof(bytesio);
|
||||
if (self->buf)
|
||||
res += self->buf_size;
|
||||
return PyLong_FromSsize_t(res);
|
||||
}
|
||||
|
||||
static int
|
||||
bytesio_traverse(bytesio *self, visitproc visit, void *arg)
|
||||
{
|
||||
Py_VISIT(self->dict);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
bytesio_clear(bytesio *self)
|
||||
{
|
||||
Py_CLEAR(self->dict);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static PyGetSetDef bytesio_getsetlist[] = {
|
||||
{"closed", (getter)bytesio_get_closed, NULL,
|
||||
"True if the file is closed."},
|
||||
{NULL}, /* sentinel */
|
||||
};
|
||||
|
||||
static struct PyMethodDef bytesio_methods[] = {
|
||||
{"readable", (PyCFunction)return_not_closed, METH_NOARGS, readable_doc},
|
||||
{"seekable", (PyCFunction)return_not_closed, METH_NOARGS, seekable_doc},
|
||||
{"writable", (PyCFunction)return_not_closed, METH_NOARGS, writable_doc},
|
||||
{"close", (PyCFunction)bytesio_close, METH_NOARGS, close_doc},
|
||||
{"flush", (PyCFunction)bytesio_flush, METH_NOARGS, flush_doc},
|
||||
{"isatty", (PyCFunction)bytesio_isatty, METH_NOARGS, isatty_doc},
|
||||
{"tell", (PyCFunction)bytesio_tell, METH_NOARGS, tell_doc},
|
||||
{"write", (PyCFunction)bytesio_write, METH_O, write_doc},
|
||||
{"writelines", (PyCFunction)bytesio_writelines, METH_O, writelines_doc},
|
||||
{"read1", (PyCFunction)bytesio_read1, METH_O, read1_doc},
|
||||
{"readinto", (PyCFunction)bytesio_readinto, METH_VARARGS, readinto_doc},
|
||||
{"readline", (PyCFunction)bytesio_readline, METH_VARARGS, readline_doc},
|
||||
{"readlines", (PyCFunction)bytesio_readlines, METH_VARARGS, readlines_doc},
|
||||
{"read", (PyCFunction)bytesio_read, METH_VARARGS, read_doc},
|
||||
{"getvalue", (PyCFunction)bytesio_getvalue, METH_NOARGS, getval_doc},
|
||||
{"seek", (PyCFunction)bytesio_seek, METH_VARARGS, seek_doc},
|
||||
{"truncate", (PyCFunction)bytesio_truncate, METH_VARARGS, truncate_doc},
|
||||
{"__getstate__", (PyCFunction)bytesio_getstate, METH_NOARGS, NULL},
|
||||
{"__setstate__", (PyCFunction)bytesio_setstate, METH_O, NULL},
|
||||
{"__sizeof__", (PyCFunction)bytesio_sizeof, METH_NOARGS, NULL},
|
||||
{NULL, NULL} /* sentinel */
|
||||
};
|
||||
|
||||
PyDoc_STRVAR(bytesio_doc,
|
||||
"BytesIO([buffer]) -> object\n"
|
||||
"\n"
|
||||
"Create a buffered I/O implementation using an in-memory bytes\n"
|
||||
"buffer, ready for reading and writing.");
|
||||
|
||||
PyTypeObject PyBytesIO_Type = {
|
||||
PyVarObject_HEAD_INIT(NULL, 0)
|
||||
"_io.BytesIO", /*tp_name*/
|
||||
sizeof(bytesio), /*tp_basicsize*/
|
||||
0, /*tp_itemsize*/
|
||||
(destructor)bytesio_dealloc, /*tp_dealloc*/
|
||||
0, /*tp_print*/
|
||||
0, /*tp_getattr*/
|
||||
0, /*tp_setattr*/
|
||||
0, /*tp_reserved*/
|
||||
0, /*tp_repr*/
|
||||
0, /*tp_as_number*/
|
||||
0, /*tp_as_sequence*/
|
||||
0, /*tp_as_mapping*/
|
||||
0, /*tp_hash*/
|
||||
0, /*tp_call*/
|
||||
0, /*tp_str*/
|
||||
0, /*tp_getattro*/
|
||||
0, /*tp_setattro*/
|
||||
0, /*tp_as_buffer*/
|
||||
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
|
||||
Py_TPFLAGS_HAVE_GC, /*tp_flags*/
|
||||
bytesio_doc, /*tp_doc*/
|
||||
(traverseproc)bytesio_traverse, /*tp_traverse*/
|
||||
(inquiry)bytesio_clear, /*tp_clear*/
|
||||
0, /*tp_richcompare*/
|
||||
offsetof(bytesio, weakreflist), /*tp_weaklistoffset*/
|
||||
PyObject_SelfIter, /*tp_iter*/
|
||||
(iternextfunc)bytesio_iternext, /*tp_iternext*/
|
||||
bytesio_methods, /*tp_methods*/
|
||||
0, /*tp_members*/
|
||||
bytesio_getsetlist, /*tp_getset*/
|
||||
0, /*tp_base*/
|
||||
0, /*tp_dict*/
|
||||
0, /*tp_descr_get*/
|
||||
0, /*tp_descr_set*/
|
||||
offsetof(bytesio, dict), /*tp_dictoffset*/
|
||||
(initproc)bytesio_init, /*tp_init*/
|
||||
0, /*tp_alloc*/
|
||||
bytesio_new, /*tp_new*/
|
||||
};
|
1118
AppPkg/Applications/Python/Python-2.7.10/Modules/_io/fileio.c
Normal file
1118
AppPkg/Applications/Python/Python-2.7.10/Modules/_io/fileio.c
Normal file
File diff suppressed because it is too large
Load Diff
913
AppPkg/Applications/Python/Python-2.7.10/Modules/_io/iobase.c
Normal file
913
AppPkg/Applications/Python/Python-2.7.10/Modules/_io/iobase.c
Normal file
@ -0,0 +1,913 @@
|
||||
/*
|
||||
An implementation of the I/O abstract base classes hierarchy
|
||||
as defined by PEP 3116 - "New I/O"
|
||||
|
||||
Classes defined here: IOBase, RawIOBase.
|
||||
|
||||
Written by Amaury Forgeot d'Arc and Antoine Pitrou
|
||||
*/
|
||||
|
||||
|
||||
#define PY_SSIZE_T_CLEAN
|
||||
#include "Python.h"
|
||||
#include "structmember.h"
|
||||
#include "_iomodule.h"
|
||||
|
||||
/*
|
||||
* IOBase class, an abstract class
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
|
||||
PyObject *dict;
|
||||
PyObject *weakreflist;
|
||||
} iobase;
|
||||
|
||||
PyDoc_STRVAR(iobase_doc,
|
||||
"The abstract base class for all I/O classes, acting on streams of\n"
|
||||
"bytes. There is no public constructor.\n"
|
||||
"\n"
|
||||
"This class provides dummy implementations for many methods that\n"
|
||||
"derived classes can override selectively; the default implementations\n"
|
||||
"represent a file that cannot be read, written or seeked.\n"
|
||||
"\n"
|
||||
"Even though IOBase does not declare read, readinto, or write because\n"
|
||||
"their signatures will vary, implementations and clients should\n"
|
||||
"consider those methods part of the interface. Also, implementations\n"
|
||||
"may raise a IOError when operations they do not support are called.\n"
|
||||
"\n"
|
||||
"The basic type used for binary data read from or written to a file is\n"
|
||||
"bytes. bytearrays are accepted too, and in some cases (such as\n"
|
||||
"readinto) needed. Text I/O classes work with str data.\n"
|
||||
"\n"
|
||||
"Note that calling any method (except additional calls to close(),\n"
|
||||
"which are ignored) on a closed stream should raise a ValueError.\n"
|
||||
"\n"
|
||||
"IOBase (and its subclasses) support the iterator protocol, meaning\n"
|
||||
"that an IOBase object can be iterated over yielding the lines in a\n"
|
||||
"stream.\n"
|
||||
"\n"
|
||||
"IOBase also supports the :keyword:`with` statement. In this example,\n"
|
||||
"fp is closed after the suite of the with statement is complete:\n"
|
||||
"\n"
|
||||
"with open('spam.txt', 'r') as fp:\n"
|
||||
" fp.write('Spam and eggs!')\n");
|
||||
|
||||
/* Use this macro whenever you want to check the internal `closed` status
|
||||
of the IOBase object rather than the virtual `closed` attribute as returned
|
||||
by whatever subclass. */
|
||||
|
||||
#define IS_CLOSED(self) \
|
||||
PyObject_HasAttrString(self, "__IOBase_closed")
|
||||
|
||||
/* Internal methods */
|
||||
static PyObject *
|
||||
iobase_unsupported(const char *message)
|
||||
{
|
||||
PyErr_SetString(_PyIO_unsupported_operation, message);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Positionning */
|
||||
|
||||
PyDoc_STRVAR(iobase_seek_doc,
|
||||
"Change stream position.\n"
|
||||
"\n"
|
||||
"Change the stream position to the given byte offset. The offset is\n"
|
||||
"interpreted relative to the position indicated by whence. Values\n"
|
||||
"for whence are:\n"
|
||||
"\n"
|
||||
"* 0 -- start of stream (the default); offset should be zero or positive\n"
|
||||
"* 1 -- current stream position; offset may be negative\n"
|
||||
"* 2 -- end of stream; offset is usually negative\n"
|
||||
"\n"
|
||||
"Return the new absolute position.");
|
||||
|
||||
static PyObject *
|
||||
iobase_seek(PyObject *self, PyObject *args)
|
||||
{
|
||||
return iobase_unsupported("seek");
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(iobase_tell_doc,
|
||||
"Return current stream position.");
|
||||
|
||||
static PyObject *
|
||||
iobase_tell(PyObject *self, PyObject *args)
|
||||
{
|
||||
return PyObject_CallMethod(self, "seek", "ii", 0, 1);
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(iobase_truncate_doc,
|
||||
"Truncate file to size bytes.\n"
|
||||
"\n"
|
||||
"File pointer is left unchanged. Size defaults to the current IO\n"
|
||||
"position as reported by tell(). Returns the new size.");
|
||||
|
||||
static PyObject *
|
||||
iobase_truncate(PyObject *self, PyObject *args)
|
||||
{
|
||||
return iobase_unsupported("truncate");
|
||||
}
|
||||
|
||||
/* Flush and close methods */
|
||||
|
||||
PyDoc_STRVAR(iobase_flush_doc,
|
||||
"Flush write buffers, if applicable.\n"
|
||||
"\n"
|
||||
"This is not implemented for read-only and non-blocking streams.\n");
|
||||
|
||||
static PyObject *
|
||||
iobase_flush(PyObject *self, PyObject *args)
|
||||
{
|
||||
/* XXX Should this return the number of bytes written??? */
|
||||
if (IS_CLOSED(self)) {
|
||||
PyErr_SetString(PyExc_ValueError, "I/O operation on closed file.");
|
||||
return NULL;
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(iobase_close_doc,
|
||||
"Flush and close the IO object.\n"
|
||||
"\n"
|
||||
"This method has no effect if the file is already closed.\n");
|
||||
|
||||
static int
|
||||
iobase_closed(PyObject *self)
|
||||
{
|
||||
PyObject *res;
|
||||
int closed;
|
||||
/* This gets the derived attribute, which is *not* __IOBase_closed
|
||||
in most cases! */
|
||||
res = PyObject_GetAttr(self, _PyIO_str_closed);
|
||||
if (res == NULL)
|
||||
return 0;
|
||||
closed = PyObject_IsTrue(res);
|
||||
Py_DECREF(res);
|
||||
return closed;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
iobase_closed_get(PyObject *self, void *context)
|
||||
{
|
||||
return PyBool_FromLong(IS_CLOSED(self));
|
||||
}
|
||||
|
||||
PyObject *
|
||||
_PyIOBase_check_closed(PyObject *self, PyObject *args)
|
||||
{
|
||||
if (iobase_closed(self)) {
|
||||
PyErr_SetString(PyExc_ValueError, "I/O operation on closed file.");
|
||||
return NULL;
|
||||
}
|
||||
if (args == Py_True)
|
||||
return Py_None;
|
||||
else
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
/* XXX: IOBase thinks it has to maintain its own internal state in
|
||||
`__IOBase_closed` and call flush() by itself, but it is redundant with
|
||||
whatever behaviour a non-trivial derived class will implement. */
|
||||
|
||||
static PyObject *
|
||||
iobase_close(PyObject *self, PyObject *args)
|
||||
{
|
||||
PyObject *res;
|
||||
|
||||
if (IS_CLOSED(self))
|
||||
Py_RETURN_NONE;
|
||||
|
||||
res = PyObject_CallMethodObjArgs(self, _PyIO_str_flush, NULL);
|
||||
PyObject_SetAttrString(self, "__IOBase_closed", Py_True);
|
||||
if (res == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
Py_XDECREF(res);
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
/* Finalization and garbage collection support */
|
||||
|
||||
int
|
||||
_PyIOBase_finalize(PyObject *self)
|
||||
{
|
||||
PyObject *res;
|
||||
PyObject *tp, *v, *tb;
|
||||
int closed = 1;
|
||||
int is_zombie;
|
||||
|
||||
/* If _PyIOBase_finalize() is called from a destructor, we need to
|
||||
resurrect the object as calling close() can invoke arbitrary code. */
|
||||
is_zombie = (Py_REFCNT(self) == 0);
|
||||
if (is_zombie) {
|
||||
++Py_REFCNT(self);
|
||||
}
|
||||
PyErr_Fetch(&tp, &v, &tb);
|
||||
/* If `closed` doesn't exist or can't be evaluated as bool, then the
|
||||
object is probably in an unusable state, so ignore. */
|
||||
res = PyObject_GetAttr(self, _PyIO_str_closed);
|
||||
if (res == NULL)
|
||||
PyErr_Clear();
|
||||
else {
|
||||
closed = PyObject_IsTrue(res);
|
||||
Py_DECREF(res);
|
||||
if (closed == -1)
|
||||
PyErr_Clear();
|
||||
}
|
||||
if (closed == 0) {
|
||||
res = PyObject_CallMethodObjArgs((PyObject *) self, _PyIO_str_close,
|
||||
NULL);
|
||||
/* Silencing I/O errors is bad, but printing spurious tracebacks is
|
||||
equally as bad, and potentially more frequent (because of
|
||||
shutdown issues). */
|
||||
if (res == NULL)
|
||||
PyErr_Clear();
|
||||
else
|
||||
Py_DECREF(res);
|
||||
}
|
||||
PyErr_Restore(tp, v, tb);
|
||||
if (is_zombie) {
|
||||
if (--Py_REFCNT(self) != 0) {
|
||||
/* The object lives again. The following code is taken from
|
||||
slot_tp_del in typeobject.c. */
|
||||
Py_ssize_t refcnt = Py_REFCNT(self);
|
||||
_Py_NewReference(self);
|
||||
Py_REFCNT(self) = refcnt;
|
||||
/* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
|
||||
* we need to undo that. */
|
||||
_Py_DEC_REFTOTAL;
|
||||
/* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
|
||||
* chain, so no more to do there.
|
||||
* If COUNT_ALLOCS, the original decref bumped tp_frees, and
|
||||
* _Py_NewReference bumped tp_allocs: both of those need to be
|
||||
* undone.
|
||||
*/
|
||||
#ifdef COUNT_ALLOCS
|
||||
--Py_TYPE(self)->tp_frees;
|
||||
--Py_TYPE(self)->tp_allocs;
|
||||
#endif
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
iobase_traverse(iobase *self, visitproc visit, void *arg)
|
||||
{
|
||||
Py_VISIT(self->dict);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
iobase_clear(iobase *self)
|
||||
{
|
||||
if (_PyIOBase_finalize((PyObject *) self) < 0)
|
||||
return -1;
|
||||
Py_CLEAR(self->dict);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Destructor */
|
||||
|
||||
static void
|
||||
iobase_dealloc(iobase *self)
|
||||
{
|
||||
/* NOTE: since IOBaseObject has its own dict, Python-defined attributes
|
||||
are still available here for close() to use.
|
||||
However, if the derived class declares a __slots__, those slots are
|
||||
already gone.
|
||||
*/
|
||||
if (_PyIOBase_finalize((PyObject *) self) < 0) {
|
||||
/* When called from a heap type's dealloc, the type will be
|
||||
decref'ed on return (see e.g. subtype_dealloc in typeobject.c). */
|
||||
if (PyType_HasFeature(Py_TYPE(self), Py_TPFLAGS_HEAPTYPE))
|
||||
Py_INCREF(Py_TYPE(self));
|
||||
return;
|
||||
}
|
||||
_PyObject_GC_UNTRACK(self);
|
||||
if (self->weakreflist != NULL)
|
||||
PyObject_ClearWeakRefs((PyObject *) self);
|
||||
Py_CLEAR(self->dict);
|
||||
Py_TYPE(self)->tp_free((PyObject *) self);
|
||||
}
|
||||
|
||||
/* Inquiry methods */
|
||||
|
||||
PyDoc_STRVAR(iobase_seekable_doc,
|
||||
"Return whether object supports random access.\n"
|
||||
"\n"
|
||||
"If False, seek(), tell() and truncate() will raise IOError.\n"
|
||||
"This method may need to do a test seek().");
|
||||
|
||||
static PyObject *
|
||||
iobase_seekable(PyObject *self, PyObject *args)
|
||||
{
|
||||
Py_RETURN_FALSE;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
_PyIOBase_check_seekable(PyObject *self, PyObject *args)
|
||||
{
|
||||
PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_seekable, NULL);
|
||||
if (res == NULL)
|
||||
return NULL;
|
||||
if (res != Py_True) {
|
||||
Py_CLEAR(res);
|
||||
PyErr_SetString(PyExc_IOError, "File or stream is not seekable.");
|
||||
return NULL;
|
||||
}
|
||||
if (args == Py_True) {
|
||||
Py_DECREF(res);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(iobase_readable_doc,
|
||||
"Return whether object was opened for reading.\n"
|
||||
"\n"
|
||||
"If False, read() will raise IOError.");
|
||||
|
||||
static PyObject *
|
||||
iobase_readable(PyObject *self, PyObject *args)
|
||||
{
|
||||
Py_RETURN_FALSE;
|
||||
}
|
||||
|
||||
/* May be called with any object */
|
||||
PyObject *
|
||||
_PyIOBase_check_readable(PyObject *self, PyObject *args)
|
||||
{
|
||||
PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_readable, NULL);
|
||||
if (res == NULL)
|
||||
return NULL;
|
||||
if (res != Py_True) {
|
||||
Py_CLEAR(res);
|
||||
PyErr_SetString(PyExc_IOError, "File or stream is not readable.");
|
||||
return NULL;
|
||||
}
|
||||
if (args == Py_True) {
|
||||
Py_DECREF(res);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(iobase_writable_doc,
|
||||
"Return whether object was opened for writing.\n"
|
||||
"\n"
|
||||
"If False, read() will raise IOError.");
|
||||
|
||||
static PyObject *
|
||||
iobase_writable(PyObject *self, PyObject *args)
|
||||
{
|
||||
Py_RETURN_FALSE;
|
||||
}
|
||||
|
||||
/* May be called with any object */
|
||||
PyObject *
|
||||
_PyIOBase_check_writable(PyObject *self, PyObject *args)
|
||||
{
|
||||
PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_writable, NULL);
|
||||
if (res == NULL)
|
||||
return NULL;
|
||||
if (res != Py_True) {
|
||||
Py_CLEAR(res);
|
||||
PyErr_SetString(PyExc_IOError, "File or stream is not writable.");
|
||||
return NULL;
|
||||
}
|
||||
if (args == Py_True) {
|
||||
Py_DECREF(res);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Context manager */
|
||||
|
||||
static PyObject *
|
||||
iobase_enter(PyObject *self, PyObject *args)
|
||||
{
|
||||
if (_PyIOBase_check_closed(self, Py_True) == NULL)
|
||||
return NULL;
|
||||
|
||||
Py_INCREF(self);
|
||||
return self;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
iobase_exit(PyObject *self, PyObject *args)
|
||||
{
|
||||
return PyObject_CallMethodObjArgs(self, _PyIO_str_close, NULL);
|
||||
}
|
||||
|
||||
/* Lower-level APIs */
|
||||
|
||||
/* XXX Should these be present even if unimplemented? */
|
||||
|
||||
PyDoc_STRVAR(iobase_fileno_doc,
|
||||
"Returns underlying file descriptor if one exists.\n"
|
||||
"\n"
|
||||
"An IOError is raised if the IO object does not use a file descriptor.\n");
|
||||
|
||||
static PyObject *
|
||||
iobase_fileno(PyObject *self, PyObject *args)
|
||||
{
|
||||
return iobase_unsupported("fileno");
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(iobase_isatty_doc,
|
||||
"Return whether this is an 'interactive' stream.\n"
|
||||
"\n"
|
||||
"Return False if it can't be determined.\n");
|
||||
|
||||
static PyObject *
|
||||
iobase_isatty(PyObject *self, PyObject *args)
|
||||
{
|
||||
if (_PyIOBase_check_closed(self, Py_True) == NULL)
|
||||
return NULL;
|
||||
Py_RETURN_FALSE;
|
||||
}
|
||||
|
||||
/* Readline(s) and writelines */
|
||||
|
||||
PyDoc_STRVAR(iobase_readline_doc,
|
||||
"Read and return a line from the stream.\n"
|
||||
"\n"
|
||||
"If limit is specified, at most limit bytes will be read.\n"
|
||||
"\n"
|
||||
"The line terminator is always b'\\n' for binary files; for text\n"
|
||||
"files, the newlines argument to open can be used to select the line\n"
|
||||
"terminator(s) recognized.\n");
|
||||
|
||||
static PyObject *
|
||||
iobase_readline(PyObject *self, PyObject *args)
|
||||
{
|
||||
/* For backwards compatibility, a (slowish) readline(). */
|
||||
|
||||
Py_ssize_t limit = -1;
|
||||
int has_peek = 0;
|
||||
PyObject *buffer, *result;
|
||||
Py_ssize_t old_size = -1;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "|O&:readline", &_PyIO_ConvertSsize_t, &limit)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (PyObject_HasAttrString(self, "peek"))
|
||||
has_peek = 1;
|
||||
|
||||
buffer = PyByteArray_FromStringAndSize(NULL, 0);
|
||||
if (buffer == NULL)
|
||||
return NULL;
|
||||
|
||||
while (limit < 0 || Py_SIZE(buffer) < limit) {
|
||||
Py_ssize_t nreadahead = 1;
|
||||
PyObject *b;
|
||||
|
||||
if (has_peek) {
|
||||
PyObject *readahead = PyObject_CallMethod(self, "peek", "i", 1);
|
||||
if (readahead == NULL) {
|
||||
/* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
|
||||
when EINTR occurs so we needn't do it ourselves. */
|
||||
if (_PyIO_trap_eintr()) {
|
||||
continue;
|
||||
}
|
||||
goto fail;
|
||||
}
|
||||
if (!PyBytes_Check(readahead)) {
|
||||
PyErr_Format(PyExc_IOError,
|
||||
"peek() should have returned a bytes object, "
|
||||
"not '%.200s'", Py_TYPE(readahead)->tp_name);
|
||||
Py_DECREF(readahead);
|
||||
goto fail;
|
||||
}
|
||||
if (PyBytes_GET_SIZE(readahead) > 0) {
|
||||
Py_ssize_t n = 0;
|
||||
const char *buf = PyBytes_AS_STRING(readahead);
|
||||
if (limit >= 0) {
|
||||
do {
|
||||
if (n >= PyBytes_GET_SIZE(readahead) || n >= limit)
|
||||
break;
|
||||
if (buf[n++] == '\n')
|
||||
break;
|
||||
} while (1);
|
||||
}
|
||||
else {
|
||||
do {
|
||||
if (n >= PyBytes_GET_SIZE(readahead))
|
||||
break;
|
||||
if (buf[n++] == '\n')
|
||||
break;
|
||||
} while (1);
|
||||
}
|
||||
nreadahead = n;
|
||||
}
|
||||
Py_DECREF(readahead);
|
||||
}
|
||||
|
||||
b = PyObject_CallMethod(self, "read", "n", nreadahead);
|
||||
if (b == NULL) {
|
||||
/* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
|
||||
when EINTR occurs so we needn't do it ourselves. */
|
||||
if (_PyIO_trap_eintr()) {
|
||||
continue;
|
||||
}
|
||||
goto fail;
|
||||
}
|
||||
if (!PyBytes_Check(b)) {
|
||||
PyErr_Format(PyExc_IOError,
|
||||
"read() should have returned a bytes object, "
|
||||
"not '%.200s'", Py_TYPE(b)->tp_name);
|
||||
Py_DECREF(b);
|
||||
goto fail;
|
||||
}
|
||||
if (PyBytes_GET_SIZE(b) == 0) {
|
||||
Py_DECREF(b);
|
||||
break;
|
||||
}
|
||||
|
||||
old_size = PyByteArray_GET_SIZE(buffer);
|
||||
PyByteArray_Resize(buffer, old_size + PyBytes_GET_SIZE(b));
|
||||
memcpy(PyByteArray_AS_STRING(buffer) + old_size,
|
||||
PyBytes_AS_STRING(b), PyBytes_GET_SIZE(b));
|
||||
|
||||
Py_DECREF(b);
|
||||
|
||||
if (PyByteArray_AS_STRING(buffer)[PyByteArray_GET_SIZE(buffer) - 1] == '\n')
|
||||
break;
|
||||
}
|
||||
|
||||
result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(buffer),
|
||||
PyByteArray_GET_SIZE(buffer));
|
||||
Py_DECREF(buffer);
|
||||
return result;
|
||||
fail:
|
||||
Py_DECREF(buffer);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
iobase_iter(PyObject *self)
|
||||
{
|
||||
if (_PyIOBase_check_closed(self, Py_True) == NULL)
|
||||
return NULL;
|
||||
|
||||
Py_INCREF(self);
|
||||
return self;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
iobase_iternext(PyObject *self)
|
||||
{
|
||||
PyObject *line = PyObject_CallMethodObjArgs(self, _PyIO_str_readline, NULL);
|
||||
|
||||
if (line == NULL)
|
||||
return NULL;
|
||||
|
||||
if (PyObject_Size(line) == 0) {
|
||||
Py_DECREF(line);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return line;
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(iobase_readlines_doc,
|
||||
"Return a list of lines from the stream.\n"
|
||||
"\n"
|
||||
"hint can be specified to control the number of lines read: no more\n"
|
||||
"lines will be read if the total size (in bytes/characters) of all\n"
|
||||
"lines so far exceeds hint.");
|
||||
|
||||
static PyObject *
|
||||
iobase_readlines(PyObject *self, PyObject *args)
|
||||
{
|
||||
Py_ssize_t hint = -1, length = 0;
|
||||
PyObject *result;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "|O&:readlines", &_PyIO_ConvertSsize_t, &hint)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
result = PyList_New(0);
|
||||
if (result == NULL)
|
||||
return NULL;
|
||||
|
||||
if (hint <= 0) {
|
||||
/* XXX special-casing this made sense in the Python version in order
|
||||
to remove the bytecode interpretation overhead, but it could
|
||||
probably be removed here. */
|
||||
PyObject *ret = PyObject_CallMethod(result, "extend", "O", self);
|
||||
if (ret == NULL) {
|
||||
Py_DECREF(result);
|
||||
return NULL;
|
||||
}
|
||||
Py_DECREF(ret);
|
||||
return result;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
PyObject *line = PyIter_Next(self);
|
||||
if (line == NULL) {
|
||||
if (PyErr_Occurred()) {
|
||||
Py_DECREF(result);
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
break; /* StopIteration raised */
|
||||
}
|
||||
|
||||
if (PyList_Append(result, line) < 0) {
|
||||
Py_DECREF(line);
|
||||
Py_DECREF(result);
|
||||
return NULL;
|
||||
}
|
||||
length += PyObject_Size(line);
|
||||
Py_DECREF(line);
|
||||
|
||||
if (length > hint)
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
iobase_writelines(PyObject *self, PyObject *args)
|
||||
{
|
||||
PyObject *lines, *iter, *res;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "O:writelines", &lines)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (_PyIOBase_check_closed(self, Py_True) == NULL)
|
||||
return NULL;
|
||||
|
||||
iter = PyObject_GetIter(lines);
|
||||
if (iter == NULL)
|
||||
return NULL;
|
||||
|
||||
while (1) {
|
||||
PyObject *line = PyIter_Next(iter);
|
||||
if (line == NULL) {
|
||||
if (PyErr_Occurred()) {
|
||||
Py_DECREF(iter);
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
break; /* Stop Iteration */
|
||||
}
|
||||
|
||||
res = NULL;
|
||||
do {
|
||||
res = PyObject_CallMethodObjArgs(self, _PyIO_str_write, line, NULL);
|
||||
} while (res == NULL && _PyIO_trap_eintr());
|
||||
Py_DECREF(line);
|
||||
if (res == NULL) {
|
||||
Py_DECREF(iter);
|
||||
return NULL;
|
||||
}
|
||||
Py_DECREF(res);
|
||||
}
|
||||
Py_DECREF(iter);
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static PyMethodDef iobase_methods[] = {
|
||||
{"seek", iobase_seek, METH_VARARGS, iobase_seek_doc},
|
||||
{"tell", iobase_tell, METH_NOARGS, iobase_tell_doc},
|
||||
{"truncate", iobase_truncate, METH_VARARGS, iobase_truncate_doc},
|
||||
{"flush", iobase_flush, METH_NOARGS, iobase_flush_doc},
|
||||
{"close", iobase_close, METH_NOARGS, iobase_close_doc},
|
||||
|
||||
{"seekable", iobase_seekable, METH_NOARGS, iobase_seekable_doc},
|
||||
{"readable", iobase_readable, METH_NOARGS, iobase_readable_doc},
|
||||
{"writable", iobase_writable, METH_NOARGS, iobase_writable_doc},
|
||||
|
||||
{"_checkClosed", _PyIOBase_check_closed, METH_NOARGS},
|
||||
{"_checkSeekable", _PyIOBase_check_seekable, METH_NOARGS},
|
||||
{"_checkReadable", _PyIOBase_check_readable, METH_NOARGS},
|
||||
{"_checkWritable", _PyIOBase_check_writable, METH_NOARGS},
|
||||
|
||||
{"fileno", iobase_fileno, METH_NOARGS, iobase_fileno_doc},
|
||||
{"isatty", iobase_isatty, METH_NOARGS, iobase_isatty_doc},
|
||||
|
||||
{"__enter__", iobase_enter, METH_NOARGS},
|
||||
{"__exit__", iobase_exit, METH_VARARGS},
|
||||
|
||||
{"readline", iobase_readline, METH_VARARGS, iobase_readline_doc},
|
||||
{"readlines", iobase_readlines, METH_VARARGS, iobase_readlines_doc},
|
||||
{"writelines", iobase_writelines, METH_VARARGS},
|
||||
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
static PyGetSetDef iobase_getset[] = {
|
||||
{"closed", (getter)iobase_closed_get, NULL, NULL},
|
||||
{NULL}
|
||||
};
|
||||
|
||||
|
||||
PyTypeObject PyIOBase_Type = {
|
||||
PyVarObject_HEAD_INIT(NULL, 0)
|
||||
"_io._IOBase", /*tp_name*/
|
||||
sizeof(iobase), /*tp_basicsize*/
|
||||
0, /*tp_itemsize*/
|
||||
(destructor)iobase_dealloc, /*tp_dealloc*/
|
||||
0, /*tp_print*/
|
||||
0, /*tp_getattr*/
|
||||
0, /*tp_setattr*/
|
||||
0, /*tp_compare */
|
||||
0, /*tp_repr*/
|
||||
0, /*tp_as_number*/
|
||||
0, /*tp_as_sequence*/
|
||||
0, /*tp_as_mapping*/
|
||||
0, /*tp_hash */
|
||||
0, /*tp_call*/
|
||||
0, /*tp_str*/
|
||||
0, /*tp_getattro*/
|
||||
0, /*tp_setattro*/
|
||||
0, /*tp_as_buffer*/
|
||||
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
|
||||
| Py_TPFLAGS_HAVE_GC, /*tp_flags*/
|
||||
iobase_doc, /* tp_doc */
|
||||
(traverseproc)iobase_traverse, /* tp_traverse */
|
||||
(inquiry)iobase_clear, /* tp_clear */
|
||||
0, /* tp_richcompare */
|
||||
offsetof(iobase, weakreflist), /* tp_weaklistoffset */
|
||||
iobase_iter, /* tp_iter */
|
||||
iobase_iternext, /* tp_iternext */
|
||||
iobase_methods, /* tp_methods */
|
||||
0, /* tp_members */
|
||||
iobase_getset, /* tp_getset */
|
||||
0, /* tp_base */
|
||||
0, /* tp_dict */
|
||||
0, /* tp_descr_get */
|
||||
0, /* tp_descr_set */
|
||||
offsetof(iobase, dict), /* tp_dictoffset */
|
||||
0, /* tp_init */
|
||||
0, /* tp_alloc */
|
||||
PyType_GenericNew, /* tp_new */
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* RawIOBase class, Inherits from IOBase.
|
||||
*/
|
||||
PyDoc_STRVAR(rawiobase_doc,
|
||||
"Base class for raw binary I/O.");
|
||||
|
||||
/*
|
||||
* The read() method is implemented by calling readinto(); derived classes
|
||||
* that want to support read() only need to implement readinto() as a
|
||||
* primitive operation. In general, readinto() can be more efficient than
|
||||
* read().
|
||||
*
|
||||
* (It would be tempting to also provide an implementation of readinto() in
|
||||
* terms of read(), in case the latter is a more suitable primitive operation,
|
||||
* but that would lead to nasty recursion in case a subclass doesn't implement
|
||||
* either.)
|
||||
*/
|
||||
|
||||
static PyObject *
|
||||
rawiobase_read(PyObject *self, PyObject *args)
|
||||
{
|
||||
Py_ssize_t n = -1;
|
||||
PyObject *b, *res;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "|n:read", &n)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (n < 0)
|
||||
return PyObject_CallMethod(self, "readall", NULL);
|
||||
|
||||
/* TODO: allocate a bytes object directly instead and manually construct
|
||||
a writable memoryview pointing to it. */
|
||||
b = PyByteArray_FromStringAndSize(NULL, n);
|
||||
if (b == NULL)
|
||||
return NULL;
|
||||
|
||||
res = PyObject_CallMethodObjArgs(self, _PyIO_str_readinto, b, NULL);
|
||||
if (res == NULL || res == Py_None) {
|
||||
Py_DECREF(b);
|
||||
return res;
|
||||
}
|
||||
|
||||
n = PyNumber_AsSsize_t(res, PyExc_ValueError);
|
||||
Py_DECREF(res);
|
||||
if (n == -1 && PyErr_Occurred()) {
|
||||
Py_DECREF(b);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
res = PyBytes_FromStringAndSize(PyByteArray_AsString(b), n);
|
||||
Py_DECREF(b);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
PyDoc_STRVAR(rawiobase_readall_doc,
|
||||
"Read until EOF, using multiple read() call.");
|
||||
|
||||
static PyObject *
|
||||
rawiobase_readall(PyObject *self, PyObject *args)
|
||||
{
|
||||
int r;
|
||||
PyObject *chunks = PyList_New(0);
|
||||
PyObject *result;
|
||||
|
||||
if (chunks == NULL)
|
||||
return NULL;
|
||||
|
||||
while (1) {
|
||||
PyObject *data = PyObject_CallMethod(self, "read",
|
||||
"i", DEFAULT_BUFFER_SIZE);
|
||||
if (!data) {
|
||||
/* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
|
||||
when EINTR occurs so we needn't do it ourselves. */
|
||||
if (_PyIO_trap_eintr()) {
|
||||
continue;
|
||||
}
|
||||
Py_DECREF(chunks);
|
||||
return NULL;
|
||||
}
|
||||
if (data == Py_None) {
|
||||
if (PyList_GET_SIZE(chunks) == 0) {
|
||||
Py_DECREF(chunks);
|
||||
return data;
|
||||
}
|
||||
Py_DECREF(data);
|
||||
break;
|
||||
}
|
||||
if (!PyBytes_Check(data)) {
|
||||
Py_DECREF(chunks);
|
||||
Py_DECREF(data);
|
||||
PyErr_SetString(PyExc_TypeError, "read() should return bytes");
|
||||
return NULL;
|
||||
}
|
||||
if (PyBytes_GET_SIZE(data) == 0) {
|
||||
/* EOF */
|
||||
Py_DECREF(data);
|
||||
break;
|
||||
}
|
||||
r = PyList_Append(chunks, data);
|
||||
Py_DECREF(data);
|
||||
if (r < 0) {
|
||||
Py_DECREF(chunks);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
result = _PyBytes_Join(_PyIO_empty_bytes, chunks);
|
||||
Py_DECREF(chunks);
|
||||
return result;
|
||||
}
|
||||
|
||||
static PyMethodDef rawiobase_methods[] = {
|
||||
{"read", rawiobase_read, METH_VARARGS},
|
||||
{"readall", rawiobase_readall, METH_NOARGS, rawiobase_readall_doc},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
PyTypeObject PyRawIOBase_Type = {
|
||||
PyVarObject_HEAD_INIT(NULL, 0)
|
||||
"_io._RawIOBase", /*tp_name*/
|
||||
0, /*tp_basicsize*/
|
||||
0, /*tp_itemsize*/
|
||||
0, /*tp_dealloc*/
|
||||
0, /*tp_print*/
|
||||
0, /*tp_getattr*/
|
||||
0, /*tp_setattr*/
|
||||
0, /*tp_compare */
|
||||
0, /*tp_repr*/
|
||||
0, /*tp_as_number*/
|
||||
0, /*tp_as_sequence*/
|
||||
0, /*tp_as_mapping*/
|
||||
0, /*tp_hash */
|
||||
0, /*tp_call*/
|
||||
0, /*tp_str*/
|
||||
0, /*tp_getattro*/
|
||||
0, /*tp_setattro*/
|
||||
0, /*tp_as_buffer*/
|
||||
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
|
||||
rawiobase_doc, /* tp_doc */
|
||||
0, /* tp_traverse */
|
||||
0, /* tp_clear */
|
||||
0, /* tp_richcompare */
|
||||
0, /* tp_weaklistoffset */
|
||||
0, /* tp_iter */
|
||||
0, /* tp_iternext */
|
||||
rawiobase_methods, /* tp_methods */
|
||||
0, /* tp_members */
|
||||
0, /* tp_getset */
|
||||
&PyIOBase_Type, /* tp_base */
|
||||
0, /* tp_dict */
|
||||
0, /* tp_descr_get */
|
||||
0, /* tp_descr_set */
|
||||
0, /* tp_dictoffset */
|
||||
0, /* tp_init */
|
||||
0, /* tp_alloc */
|
||||
0, /* tp_new */
|
||||
};
|
895
AppPkg/Applications/Python/Python-2.7.10/Modules/_io/stringio.c
Normal file
895
AppPkg/Applications/Python/Python-2.7.10/Modules/_io/stringio.c
Normal file
@ -0,0 +1,895 @@
|
||||
#define PY_SSIZE_T_CLEAN
|
||||
#include "Python.h"
|
||||
#include "structmember.h"
|
||||
#include "_iomodule.h"
|
||||
|
||||
/* Implementation note: the buffer is always at least one character longer
|
||||
than the enclosed string, for proper functioning of _PyIO_find_line_ending.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
Py_UNICODE *buf;
|
||||
Py_ssize_t pos;
|
||||
Py_ssize_t string_size;
|
||||
size_t buf_size;
|
||||
|
||||
char ok; /* initialized? */
|
||||
char closed;
|
||||
char readuniversal;
|
||||
char readtranslate;
|
||||
PyObject *decoder;
|
||||
PyObject *readnl;
|
||||
PyObject *writenl;
|
||||
|
||||
PyObject *dict;
|
||||
PyObject *weakreflist;
|
||||
} stringio;
|
||||
|
||||
#define CHECK_INITIALIZED(self) \
|
||||
if (self->ok <= 0) { \
|
||||
PyErr_SetString(PyExc_ValueError, \
|
||||
"I/O operation on uninitialized object"); \
|
||||
return NULL; \
|
||||
}
|
||||
|
||||
#define CHECK_CLOSED(self) \
|
||||
if (self->closed) { \
|
||||
PyErr_SetString(PyExc_ValueError, \
|
||||
"I/O operation on closed file"); \
|
||||
return NULL; \
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(stringio_doc,
|
||||
"Text I/O implementation using an in-memory buffer.\n"
|
||||
"\n"
|
||||
"The initial_value argument sets the value of object. The newline\n"
|
||||
"argument is like the one of TextIOWrapper's constructor.");
|
||||
|
||||
|
||||
/* Internal routine for changing the size, in terms of characters, of the
|
||||
buffer of StringIO objects. The caller should ensure that the 'size'
|
||||
argument is non-negative. Returns 0 on success, -1 otherwise. */
|
||||
static int
|
||||
resize_buffer(stringio *self, size_t size)
|
||||
{
|
||||
/* Here, unsigned types are used to avoid dealing with signed integer
|
||||
overflow, which is undefined in C. */
|
||||
size_t alloc = self->buf_size;
|
||||
Py_UNICODE *new_buf = NULL;
|
||||
|
||||
assert(self->buf != NULL);
|
||||
|
||||
/* Reserve one more char for line ending detection. */
|
||||
size = size + 1;
|
||||
/* For simplicity, stay in the range of the signed type. Anyway, Python
|
||||
doesn't allow strings to be longer than this. */
|
||||
if (size > PY_SSIZE_T_MAX)
|
||||
goto overflow;
|
||||
|
||||
if (size < alloc / 2) {
|
||||
/* Major downsize; resize down to exact size. */
|
||||
alloc = size + 1;
|
||||
}
|
||||
else if (size < alloc) {
|
||||
/* Within allocated size; quick exit */
|
||||
return 0;
|
||||
}
|
||||
else if (size <= alloc * 1.125) {
|
||||
/* Moderate upsize; overallocate similar to list_resize() */
|
||||
alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
|
||||
}
|
||||
else {
|
||||
/* Major upsize; resize up to exact size */
|
||||
alloc = size + 1;
|
||||
}
|
||||
|
||||
if (alloc > ((size_t)-1) / sizeof(Py_UNICODE))
|
||||
goto overflow;
|
||||
new_buf = (Py_UNICODE *)PyMem_Realloc(self->buf,
|
||||
alloc * sizeof(Py_UNICODE));
|
||||
if (new_buf == NULL) {
|
||||
PyErr_NoMemory();
|
||||
return -1;
|
||||
}
|
||||
self->buf_size = alloc;
|
||||
self->buf = new_buf;
|
||||
|
||||
return 0;
|
||||
|
||||
overflow:
|
||||
PyErr_SetString(PyExc_OverflowError,
|
||||
"new buffer size too large");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Internal routine for writing a whole PyUnicode object to the buffer of a
|
||||
StringIO object. Returns 0 on success, or -1 on error. */
|
||||
static Py_ssize_t
|
||||
write_str(stringio *self, PyObject *obj)
|
||||
{
|
||||
Py_UNICODE *str;
|
||||
Py_ssize_t len;
|
||||
PyObject *decoded = NULL;
|
||||
assert(self->buf != NULL);
|
||||
assert(self->pos >= 0);
|
||||
|
||||
if (self->decoder != NULL) {
|
||||
decoded = _PyIncrementalNewlineDecoder_decode(
|
||||
self->decoder, obj, 1 /* always final */);
|
||||
}
|
||||
else {
|
||||
decoded = obj;
|
||||
Py_INCREF(decoded);
|
||||
}
|
||||
if (self->writenl) {
|
||||
PyObject *translated = PyUnicode_Replace(
|
||||
decoded, _PyIO_str_nl, self->writenl, -1);
|
||||
Py_DECREF(decoded);
|
||||
decoded = translated;
|
||||
}
|
||||
if (decoded == NULL)
|
||||
return -1;
|
||||
|
||||
assert(PyUnicode_Check(decoded));
|
||||
str = PyUnicode_AS_UNICODE(decoded);
|
||||
len = PyUnicode_GET_SIZE(decoded);
|
||||
|
||||
assert(len >= 0);
|
||||
|
||||
/* This overflow check is not strictly necessary. However, it avoids us to
|
||||
deal with funky things like comparing an unsigned and a signed
|
||||
integer. */
|
||||
if (self->pos > PY_SSIZE_T_MAX - len) {
|
||||
PyErr_SetString(PyExc_OverflowError,
|
||||
"new position too large");
|
||||
goto fail;
|
||||
}
|
||||
if (self->pos + len > self->string_size) {
|
||||
if (resize_buffer(self, self->pos + len) < 0)
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (self->pos > self->string_size) {
|
||||
/* In case of overseek, pad with null bytes the buffer region between
|
||||
the end of stream and the current position.
|
||||
|
||||
0 lo string_size hi
|
||||
| |<---used--->|<----------available----------->|
|
||||
| | <--to pad-->|<---to write---> |
|
||||
0 buf position
|
||||
|
||||
*/
|
||||
memset(self->buf + self->string_size, '\0',
|
||||
(self->pos - self->string_size) * sizeof(Py_UNICODE));
|
||||
}
|
||||
|
||||
/* Copy the data to the internal buffer, overwriting some of the
|
||||
existing data if self->pos < self->string_size. */
|
||||
memcpy(self->buf + self->pos, str, len * sizeof(Py_UNICODE));
|
||||
self->pos += len;
|
||||
|
||||
/* Set the new length of the internal string if it has changed. */
|
||||
if (self->string_size < self->pos) {
|
||||
self->string_size = self->pos;
|
||||
}
|
||||
|
||||
Py_DECREF(decoded);
|
||||
return 0;
|
||||
|
||||
fail:
|
||||
Py_XDECREF(decoded);
|
||||
return -1;
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(stringio_getvalue_doc,
|
||||
"Retrieve the entire contents of the object.");
|
||||
|
||||
static PyObject *
|
||||
stringio_getvalue(stringio *self)
|
||||
{
|
||||
CHECK_INITIALIZED(self);
|
||||
CHECK_CLOSED(self);
|
||||
return PyUnicode_FromUnicode(self->buf, self->string_size);
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(stringio_tell_doc,
|
||||
"Tell the current file position.");
|
||||
|
||||
static PyObject *
|
||||
stringio_tell(stringio *self)
|
||||
{
|
||||
CHECK_INITIALIZED(self);
|
||||
CHECK_CLOSED(self);
|
||||
return PyLong_FromSsize_t(self->pos);
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(stringio_read_doc,
|
||||
"Read at most n characters, returned as a string.\n"
|
||||
"\n"
|
||||
"If the argument is negative or omitted, read until EOF\n"
|
||||
"is reached. Return an empty string at EOF.\n");
|
||||
|
||||
static PyObject *
|
||||
stringio_read(stringio *self, PyObject *args)
|
||||
{
|
||||
Py_ssize_t size, n;
|
||||
Py_UNICODE *output;
|
||||
PyObject *arg = Py_None;
|
||||
|
||||
CHECK_INITIALIZED(self);
|
||||
if (!PyArg_ParseTuple(args, "|O:read", &arg))
|
||||
return NULL;
|
||||
CHECK_CLOSED(self);
|
||||
|
||||
if (PyNumber_Check(arg)) {
|
||||
size = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
|
||||
if (size == -1 && PyErr_Occurred())
|
||||
return NULL;
|
||||
}
|
||||
else if (arg == Py_None) {
|
||||
/* Read until EOF is reached, by default. */
|
||||
size = -1;
|
||||
}
|
||||
else {
|
||||
PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
|
||||
Py_TYPE(arg)->tp_name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* adjust invalid sizes */
|
||||
n = self->string_size - self->pos;
|
||||
if (size < 0 || size > n) {
|
||||
size = n;
|
||||
if (size < 0)
|
||||
size = 0;
|
||||
}
|
||||
|
||||
output = self->buf + self->pos;
|
||||
self->pos += size;
|
||||
return PyUnicode_FromUnicode(output, size);
|
||||
}
|
||||
|
||||
/* Internal helper, used by stringio_readline and stringio_iternext */
|
||||
static PyObject *
|
||||
_stringio_readline(stringio *self, Py_ssize_t limit)
|
||||
{
|
||||
Py_UNICODE *start, *end, old_char;
|
||||
Py_ssize_t len, consumed;
|
||||
|
||||
/* In case of overseek, return the empty string */
|
||||
if (self->pos >= self->string_size)
|
||||
return PyUnicode_FromString("");
|
||||
|
||||
start = self->buf + self->pos;
|
||||
if (limit < 0 || limit > self->string_size - self->pos)
|
||||
limit = self->string_size - self->pos;
|
||||
|
||||
end = start + limit;
|
||||
old_char = *end;
|
||||
*end = '\0';
|
||||
len = _PyIO_find_line_ending(
|
||||
self->readtranslate, self->readuniversal, self->readnl,
|
||||
start, end, &consumed);
|
||||
*end = old_char;
|
||||
/* If we haven't found any line ending, we just return everything
|
||||
(`consumed` is ignored). */
|
||||
if (len < 0)
|
||||
len = limit;
|
||||
self->pos += len;
|
||||
return PyUnicode_FromUnicode(start, len);
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(stringio_readline_doc,
|
||||
"Read until newline or EOF.\n"
|
||||
"\n"
|
||||
"Returns an empty string if EOF is hit immediately.\n");
|
||||
|
||||
static PyObject *
|
||||
stringio_readline(stringio *self, PyObject *args)
|
||||
{
|
||||
PyObject *arg = Py_None;
|
||||
Py_ssize_t limit = -1;
|
||||
|
||||
CHECK_INITIALIZED(self);
|
||||
if (!PyArg_ParseTuple(args, "|O:readline", &arg))
|
||||
return NULL;
|
||||
CHECK_CLOSED(self);
|
||||
|
||||
if (PyNumber_Check(arg)) {
|
||||
limit = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
|
||||
if (limit == -1 && PyErr_Occurred())
|
||||
return NULL;
|
||||
}
|
||||
else if (arg != Py_None) {
|
||||
PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
|
||||
Py_TYPE(arg)->tp_name);
|
||||
return NULL;
|
||||
}
|
||||
return _stringio_readline(self, limit);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
stringio_iternext(stringio *self)
|
||||
{
|
||||
PyObject *line;
|
||||
|
||||
CHECK_INITIALIZED(self);
|
||||
CHECK_CLOSED(self);
|
||||
|
||||
if (Py_TYPE(self) == &PyStringIO_Type) {
|
||||
/* Skip method call overhead for speed */
|
||||
line = _stringio_readline(self, -1);
|
||||
}
|
||||
else {
|
||||
/* XXX is subclassing StringIO really supported? */
|
||||
line = PyObject_CallMethodObjArgs((PyObject *)self,
|
||||
_PyIO_str_readline, NULL);
|
||||
if (line && !PyUnicode_Check(line)) {
|
||||
PyErr_Format(PyExc_IOError,
|
||||
"readline() should have returned an str object, "
|
||||
"not '%.200s'", Py_TYPE(line)->tp_name);
|
||||
Py_DECREF(line);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (line == NULL)
|
||||
return NULL;
|
||||
|
||||
if (PyUnicode_GET_SIZE(line) == 0) {
|
||||
/* Reached EOF */
|
||||
Py_DECREF(line);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return line;
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(stringio_truncate_doc,
|
||||
"Truncate size to pos.\n"
|
||||
"\n"
|
||||
"The pos argument defaults to the current file position, as\n"
|
||||
"returned by tell(). The current file position is unchanged.\n"
|
||||
"Returns the new absolute position.\n");
|
||||
|
||||
static PyObject *
|
||||
stringio_truncate(stringio *self, PyObject *args)
|
||||
{
|
||||
Py_ssize_t size;
|
||||
PyObject *arg = Py_None;
|
||||
|
||||
CHECK_INITIALIZED(self);
|
||||
if (!PyArg_ParseTuple(args, "|O:truncate", &arg))
|
||||
return NULL;
|
||||
CHECK_CLOSED(self);
|
||||
|
||||
if (PyNumber_Check(arg)) {
|
||||
size = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
|
||||
if (size == -1 && PyErr_Occurred())
|
||||
return NULL;
|
||||
}
|
||||
else if (arg == Py_None) {
|
||||
/* Truncate to current position if no argument is passed. */
|
||||
size = self->pos;
|
||||
}
|
||||
else {
|
||||
PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
|
||||
Py_TYPE(arg)->tp_name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (size < 0) {
|
||||
PyErr_Format(PyExc_ValueError,
|
||||
"Negative size value %zd", size);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (size < self->string_size) {
|
||||
if (resize_buffer(self, size) < 0)
|
||||
return NULL;
|
||||
self->string_size = size;
|
||||
}
|
||||
|
||||
return PyLong_FromSsize_t(size);
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(stringio_seek_doc,
|
||||
"Change stream position.\n"
|
||||
"\n"
|
||||
"Seek to character offset pos relative to position indicated by whence:\n"
|
||||
" 0 Start of stream (the default). pos should be >= 0;\n"
|
||||
" 1 Current position - pos must be 0;\n"
|
||||
" 2 End of stream - pos must be 0.\n"
|
||||
"Returns the new absolute position.\n");
|
||||
|
||||
static PyObject *
|
||||
stringio_seek(stringio *self, PyObject *args)
|
||||
{
|
||||
PyObject *posobj;
|
||||
Py_ssize_t pos;
|
||||
int mode = 0;
|
||||
|
||||
CHECK_INITIALIZED(self);
|
||||
if (!PyArg_ParseTuple(args, "O|i:seek", &posobj, &mode))
|
||||
return NULL;
|
||||
|
||||
pos = PyNumber_AsSsize_t(posobj, PyExc_OverflowError);
|
||||
if (pos == -1 && PyErr_Occurred())
|
||||
return NULL;
|
||||
|
||||
CHECK_CLOSED(self);
|
||||
|
||||
if (mode != 0 && mode != 1 && mode != 2) {
|
||||
PyErr_Format(PyExc_ValueError,
|
||||
"Invalid whence (%i, should be 0, 1 or 2)", mode);
|
||||
return NULL;
|
||||
}
|
||||
else if (pos < 0 && mode == 0) {
|
||||
PyErr_Format(PyExc_ValueError,
|
||||
"Negative seek position %zd", pos);
|
||||
return NULL;
|
||||
}
|
||||
else if (mode != 0 && pos != 0) {
|
||||
PyErr_SetString(PyExc_IOError,
|
||||
"Can't do nonzero cur-relative seeks");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* mode 0: offset relative to beginning of the string.
|
||||
mode 1: no change to current position.
|
||||
mode 2: change position to end of file. */
|
||||
if (mode == 1) {
|
||||
pos = self->pos;
|
||||
}
|
||||
else if (mode == 2) {
|
||||
pos = self->string_size;
|
||||
}
|
||||
|
||||
self->pos = pos;
|
||||
|
||||
return PyLong_FromSsize_t(self->pos);
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(stringio_write_doc,
|
||||
"Write string to file.\n"
|
||||
"\n"
|
||||
"Returns the number of characters written, which is always equal to\n"
|
||||
"the length of the string.\n");
|
||||
|
||||
static PyObject *
|
||||
stringio_write(stringio *self, PyObject *obj)
|
||||
{
|
||||
Py_ssize_t size;
|
||||
|
||||
CHECK_INITIALIZED(self);
|
||||
if (!PyUnicode_Check(obj)) {
|
||||
PyErr_Format(PyExc_TypeError, "unicode argument expected, got '%s'",
|
||||
Py_TYPE(obj)->tp_name);
|
||||
return NULL;
|
||||
}
|
||||
CHECK_CLOSED(self);
|
||||
size = PyUnicode_GET_SIZE(obj);
|
||||
|
||||
if (size > 0 && write_str(self, obj) < 0)
|
||||
return NULL;
|
||||
|
||||
return PyLong_FromSsize_t(size);
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(stringio_close_doc,
|
||||
"Close the IO object. Attempting any further operation after the\n"
|
||||
"object is closed will raise a ValueError.\n"
|
||||
"\n"
|
||||
"This method has no effect if the file is already closed.\n");
|
||||
|
||||
static PyObject *
|
||||
stringio_close(stringio *self)
|
||||
{
|
||||
self->closed = 1;
|
||||
/* Free up some memory */
|
||||
if (resize_buffer(self, 0) < 0)
|
||||
return NULL;
|
||||
Py_CLEAR(self->readnl);
|
||||
Py_CLEAR(self->writenl);
|
||||
Py_CLEAR(self->decoder);
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static int
|
||||
stringio_traverse(stringio *self, visitproc visit, void *arg)
|
||||
{
|
||||
Py_VISIT(self->dict);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
stringio_clear(stringio *self)
|
||||
{
|
||||
Py_CLEAR(self->dict);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
stringio_dealloc(stringio *self)
|
||||
{
|
||||
_PyObject_GC_UNTRACK(self);
|
||||
self->ok = 0;
|
||||
if (self->buf) {
|
||||
PyMem_Free(self->buf);
|
||||
self->buf = NULL;
|
||||
}
|
||||
Py_CLEAR(self->readnl);
|
||||
Py_CLEAR(self->writenl);
|
||||
Py_CLEAR(self->decoder);
|
||||
Py_CLEAR(self->dict);
|
||||
if (self->weakreflist != NULL)
|
||||
PyObject_ClearWeakRefs((PyObject *) self);
|
||||
Py_TYPE(self)->tp_free(self);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
stringio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||
{
|
||||
stringio *self;
|
||||
|
||||
assert(type != NULL && type->tp_alloc != NULL);
|
||||
self = (stringio *)type->tp_alloc(type, 0);
|
||||
if (self == NULL)
|
||||
return NULL;
|
||||
|
||||
/* tp_alloc initializes all the fields to zero. So we don't have to
|
||||
initialize them here. */
|
||||
|
||||
self->buf = (Py_UNICODE *)PyMem_Malloc(0);
|
||||
if (self->buf == NULL) {
|
||||
Py_DECREF(self);
|
||||
return PyErr_NoMemory();
|
||||
}
|
||||
|
||||
return (PyObject *)self;
|
||||
}
|
||||
|
||||
static int
|
||||
stringio_init(stringio *self, PyObject *args, PyObject *kwds)
|
||||
{
|
||||
char *kwlist[] = {"initial_value", "newline", NULL};
|
||||
PyObject *value = NULL;
|
||||
char *newline = "\n";
|
||||
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oz:__init__", kwlist,
|
||||
&value, &newline))
|
||||
return -1;
|
||||
|
||||
if (newline && newline[0] != '\0'
|
||||
&& !(newline[0] == '\n' && newline[1] == '\0')
|
||||
&& !(newline[0] == '\r' && newline[1] == '\0')
|
||||
&& !(newline[0] == '\r' && newline[1] == '\n' && newline[2] == '\0')) {
|
||||
PyErr_Format(PyExc_ValueError,
|
||||
"illegal newline value: %s", newline);
|
||||
return -1;
|
||||
}
|
||||
if (value && value != Py_None && !PyUnicode_Check(value)) {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"initial_value must be unicode or None, not %.200s",
|
||||
Py_TYPE(value)->tp_name);
|
||||
return -1;
|
||||
}
|
||||
|
||||
self->ok = 0;
|
||||
|
||||
Py_CLEAR(self->readnl);
|
||||
Py_CLEAR(self->writenl);
|
||||
Py_CLEAR(self->decoder);
|
||||
|
||||
if (newline) {
|
||||
self->readnl = PyString_FromString(newline);
|
||||
if (self->readnl == NULL)
|
||||
return -1;
|
||||
}
|
||||
self->readuniversal = (newline == NULL || newline[0] == '\0');
|
||||
self->readtranslate = (newline == NULL);
|
||||
/* If newline == "", we don't translate anything.
|
||||
If newline == "\n" or newline == None, we translate to "\n", which is
|
||||
a no-op.
|
||||
(for newline == None, TextIOWrapper translates to os.sepline, but it
|
||||
is pointless for StringIO)
|
||||
*/
|
||||
if (newline != NULL && newline[0] == '\r') {
|
||||
self->writenl = PyUnicode_FromString(newline);
|
||||
}
|
||||
|
||||
if (self->readuniversal) {
|
||||
self->decoder = PyObject_CallFunction(
|
||||
(PyObject *)&PyIncrementalNewlineDecoder_Type,
|
||||
"Oi", Py_None, (int) self->readtranslate);
|
||||
if (self->decoder == NULL)
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Now everything is set up, resize buffer to size of initial value,
|
||||
and copy it */
|
||||
self->string_size = 0;
|
||||
if (value && value != Py_None) {
|
||||
Py_ssize_t len = PyUnicode_GetSize(value);
|
||||
/* This is a heuristic, for newline translation might change
|
||||
the string length. */
|
||||
if (resize_buffer(self, len) < 0)
|
||||
return -1;
|
||||
self->pos = 0;
|
||||
if (write_str(self, value) < 0)
|
||||
return -1;
|
||||
}
|
||||
else {
|
||||
if (resize_buffer(self, 0) < 0)
|
||||
return -1;
|
||||
}
|
||||
self->pos = 0;
|
||||
|
||||
self->closed = 0;
|
||||
self->ok = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Properties and pseudo-properties */
|
||||
|
||||
PyDoc_STRVAR(stringio_readable_doc,
|
||||
"readable() -> bool. Returns True if the IO object can be read.");
|
||||
|
||||
PyDoc_STRVAR(stringio_writable_doc,
|
||||
"writable() -> bool. Returns True if the IO object can be written.");
|
||||
|
||||
PyDoc_STRVAR(stringio_seekable_doc,
|
||||
"seekable() -> bool. Returns True if the IO object can be seeked.");
|
||||
|
||||
static PyObject *
|
||||
stringio_seekable(stringio *self, PyObject *args)
|
||||
{
|
||||
CHECK_INITIALIZED(self);
|
||||
CHECK_CLOSED(self);
|
||||
Py_RETURN_TRUE;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
stringio_readable(stringio *self, PyObject *args)
|
||||
{
|
||||
CHECK_INITIALIZED(self);
|
||||
CHECK_CLOSED(self);
|
||||
Py_RETURN_TRUE;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
stringio_writable(stringio *self, PyObject *args)
|
||||
{
|
||||
CHECK_INITIALIZED(self);
|
||||
CHECK_CLOSED(self);
|
||||
Py_RETURN_TRUE;
|
||||
}
|
||||
|
||||
/* Pickling support.
|
||||
|
||||
The implementation of __getstate__ is similar to the one for BytesIO,
|
||||
except that we also save the newline parameter. For __setstate__ and unlike
|
||||
BytesIO, we call __init__ to restore the object's state. Doing so allows us
|
||||
to avoid decoding the complex newline state while keeping the object
|
||||
representation compact.
|
||||
|
||||
See comment in bytesio.c regarding why only pickle protocols and onward are
|
||||
supported.
|
||||
*/
|
||||
|
||||
static PyObject *
|
||||
stringio_getstate(stringio *self)
|
||||
{
|
||||
PyObject *initvalue = stringio_getvalue(self);
|
||||
PyObject *dict;
|
||||
PyObject *state;
|
||||
|
||||
if (initvalue == NULL)
|
||||
return NULL;
|
||||
if (self->dict == NULL) {
|
||||
Py_INCREF(Py_None);
|
||||
dict = Py_None;
|
||||
}
|
||||
else {
|
||||
dict = PyDict_Copy(self->dict);
|
||||
if (dict == NULL)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
state = Py_BuildValue("(OOnN)", initvalue,
|
||||
self->readnl ? self->readnl : Py_None,
|
||||
self->pos, dict);
|
||||
Py_DECREF(initvalue);
|
||||
return state;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
stringio_setstate(stringio *self, PyObject *state)
|
||||
{
|
||||
PyObject *initarg;
|
||||
PyObject *position_obj;
|
||||
PyObject *dict;
|
||||
Py_ssize_t pos;
|
||||
|
||||
assert(state != NULL);
|
||||
CHECK_CLOSED(self);
|
||||
|
||||
/* We allow the state tuple to be longer than 4, because we may need
|
||||
someday to extend the object's state without breaking
|
||||
backward-compatibility. */
|
||||
if (!PyTuple_Check(state) || Py_SIZE(state) < 4) {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"%.200s.__setstate__ argument should be 4-tuple, got %.200s",
|
||||
Py_TYPE(self)->tp_name, Py_TYPE(state)->tp_name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Initialize the object's state. */
|
||||
initarg = PyTuple_GetSlice(state, 0, 2);
|
||||
if (initarg == NULL)
|
||||
return NULL;
|
||||
if (stringio_init(self, initarg, NULL) < 0) {
|
||||
Py_DECREF(initarg);
|
||||
return NULL;
|
||||
}
|
||||
Py_DECREF(initarg);
|
||||
|
||||
/* Restore the buffer state. Even if __init__ did initialize the buffer,
|
||||
we have to initialize it again since __init__ may translates the
|
||||
newlines in the inital_value string. We clearly do not want that
|
||||
because the string value in the state tuple has already been translated
|
||||
once by __init__. So we do not take any chance and replace object's
|
||||
buffer completely. */
|
||||
{
|
||||
Py_UNICODE *buf = PyUnicode_AS_UNICODE(PyTuple_GET_ITEM(state, 0));
|
||||
Py_ssize_t bufsize = PyUnicode_GET_SIZE(PyTuple_GET_ITEM(state, 0));
|
||||
if (resize_buffer(self, bufsize) < 0)
|
||||
return NULL;
|
||||
memcpy(self->buf, buf, bufsize * sizeof(Py_UNICODE));
|
||||
self->string_size = bufsize;
|
||||
}
|
||||
|
||||
/* Set carefully the position value. Alternatively, we could use the seek
|
||||
method instead of modifying self->pos directly to better protect the
|
||||
object internal state against errneous (or malicious) inputs. */
|
||||
position_obj = PyTuple_GET_ITEM(state, 2);
|
||||
if (!PyIndex_Check(position_obj)) {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"third item of state must be an integer, got %.200s",
|
||||
Py_TYPE(position_obj)->tp_name);
|
||||
return NULL;
|
||||
}
|
||||
pos = PyNumber_AsSsize_t(position_obj, PyExc_OverflowError);
|
||||
if (pos == -1 && PyErr_Occurred())
|
||||
return NULL;
|
||||
if (pos < 0) {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"position value cannot be negative");
|
||||
return NULL;
|
||||
}
|
||||
self->pos = pos;
|
||||
|
||||
/* Set the dictionary of the instance variables. */
|
||||
dict = PyTuple_GET_ITEM(state, 3);
|
||||
if (dict != Py_None) {
|
||||
if (!PyDict_Check(dict)) {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"fourth item of state should be a dict, got a %.200s",
|
||||
Py_TYPE(dict)->tp_name);
|
||||
return NULL;
|
||||
}
|
||||
if (self->dict) {
|
||||
/* Alternatively, we could replace the internal dictionary
|
||||
completely. However, it seems more practical to just update it. */
|
||||
if (PyDict_Update(self->dict, dict) < 0)
|
||||
return NULL;
|
||||
}
|
||||
else {
|
||||
Py_INCREF(dict);
|
||||
self->dict = dict;
|
||||
}
|
||||
}
|
||||
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
|
||||
static PyObject *
|
||||
stringio_closed(stringio *self, void *context)
|
||||
{
|
||||
CHECK_INITIALIZED(self);
|
||||
return PyBool_FromLong(self->closed);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
stringio_line_buffering(stringio *self, void *context)
|
||||
{
|
||||
CHECK_INITIALIZED(self);
|
||||
CHECK_CLOSED(self);
|
||||
Py_RETURN_FALSE;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
stringio_newlines(stringio *self, void *context)
|
||||
{
|
||||
CHECK_INITIALIZED(self);
|
||||
CHECK_CLOSED(self);
|
||||
if (self->decoder == NULL)
|
||||
Py_RETURN_NONE;
|
||||
return PyObject_GetAttr(self->decoder, _PyIO_str_newlines);
|
||||
}
|
||||
|
||||
static struct PyMethodDef stringio_methods[] = {
|
||||
{"close", (PyCFunction)stringio_close, METH_NOARGS, stringio_close_doc},
|
||||
{"getvalue", (PyCFunction)stringio_getvalue, METH_NOARGS, stringio_getvalue_doc},
|
||||
{"read", (PyCFunction)stringio_read, METH_VARARGS, stringio_read_doc},
|
||||
{"readline", (PyCFunction)stringio_readline, METH_VARARGS, stringio_readline_doc},
|
||||
{"tell", (PyCFunction)stringio_tell, METH_NOARGS, stringio_tell_doc},
|
||||
{"truncate", (PyCFunction)stringio_truncate, METH_VARARGS, stringio_truncate_doc},
|
||||
{"seek", (PyCFunction)stringio_seek, METH_VARARGS, stringio_seek_doc},
|
||||
{"write", (PyCFunction)stringio_write, METH_O, stringio_write_doc},
|
||||
|
||||
{"seekable", (PyCFunction)stringio_seekable, METH_NOARGS, stringio_seekable_doc},
|
||||
{"readable", (PyCFunction)stringio_readable, METH_NOARGS, stringio_readable_doc},
|
||||
{"writable", (PyCFunction)stringio_writable, METH_NOARGS, stringio_writable_doc},
|
||||
|
||||
{"__getstate__", (PyCFunction)stringio_getstate, METH_NOARGS},
|
||||
{"__setstate__", (PyCFunction)stringio_setstate, METH_O},
|
||||
{NULL, NULL} /* sentinel */
|
||||
};
|
||||
|
||||
static PyGetSetDef stringio_getset[] = {
|
||||
{"closed", (getter)stringio_closed, NULL, NULL},
|
||||
{"newlines", (getter)stringio_newlines, NULL, NULL},
|
||||
/* (following comments straight off of the original Python wrapper:)
|
||||
XXX Cruft to support the TextIOWrapper API. This would only
|
||||
be meaningful if StringIO supported the buffer attribute.
|
||||
Hopefully, a better solution, than adding these pseudo-attributes,
|
||||
will be found.
|
||||
*/
|
||||
{"line_buffering", (getter)stringio_line_buffering, NULL, NULL},
|
||||
{NULL}
|
||||
};
|
||||
|
||||
PyTypeObject PyStringIO_Type = {
|
||||
PyVarObject_HEAD_INIT(NULL, 0)
|
||||
"_io.StringIO", /*tp_name*/
|
||||
sizeof(stringio), /*tp_basicsize*/
|
||||
0, /*tp_itemsize*/
|
||||
(destructor)stringio_dealloc, /*tp_dealloc*/
|
||||
0, /*tp_print*/
|
||||
0, /*tp_getattr*/
|
||||
0, /*tp_setattr*/
|
||||
0, /*tp_reserved*/
|
||||
0, /*tp_repr*/
|
||||
0, /*tp_as_number*/
|
||||
0, /*tp_as_sequence*/
|
||||
0, /*tp_as_mapping*/
|
||||
0, /*tp_hash*/
|
||||
0, /*tp_call*/
|
||||
0, /*tp_str*/
|
||||
0, /*tp_getattro*/
|
||||
0, /*tp_setattro*/
|
||||
0, /*tp_as_buffer*/
|
||||
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
|
||||
| Py_TPFLAGS_HAVE_GC, /*tp_flags*/
|
||||
stringio_doc, /*tp_doc*/
|
||||
(traverseproc)stringio_traverse, /*tp_traverse*/
|
||||
(inquiry)stringio_clear, /*tp_clear*/
|
||||
0, /*tp_richcompare*/
|
||||
offsetof(stringio, weakreflist), /*tp_weaklistoffset*/
|
||||
0, /*tp_iter*/
|
||||
(iternextfunc)stringio_iternext, /*tp_iternext*/
|
||||
stringio_methods, /*tp_methods*/
|
||||
0, /*tp_members*/
|
||||
stringio_getset, /*tp_getset*/
|
||||
0, /*tp_base*/
|
||||
0, /*tp_dict*/
|
||||
0, /*tp_descr_get*/
|
||||
0, /*tp_descr_set*/
|
||||
offsetof(stringio, dict), /*tp_dictoffset*/
|
||||
(initproc)stringio_init, /*tp_init*/
|
||||
0, /*tp_alloc*/
|
||||
stringio_new, /*tp_new*/
|
||||
};
|
2670
AppPkg/Applications/Python/Python-2.7.10/Modules/_io/textio.c
Normal file
2670
AppPkg/Applications/Python/Python-2.7.10/Modules/_io/textio.c
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user