2007-06-12 20:30:11 -03:00
|
|
|
/* File object implementation (what's left of it -- see io.py) */
|
1990-10-14 09:07:46 -03:00
|
|
|
|
2006-02-15 13:27:45 -04:00
|
|
|
#define PY_SSIZE_T_CLEAN
|
1997-05-02 00:12:38 -03:00
|
|
|
#include "Python.h"
|
1990-10-14 09:07:46 -03:00
|
|
|
|
2002-04-14 17:12:41 -03:00
|
|
|
#ifdef HAVE_GETC_UNLOCKED
|
|
|
|
#define GETC(f) getc_unlocked(f)
|
|
|
|
#define FLOCKFILE(f) flockfile(f)
|
|
|
|
#define FUNLOCKFILE(f) funlockfile(f)
|
|
|
|
#else
|
|
|
|
#define GETC(f) getc(f)
|
|
|
|
#define FLOCKFILE(f)
|
2007-06-12 20:30:11 -03:00
|
|
|
#define FUNLOCKFILE(f)
|
2001-01-07 17:19:34 -04:00
|
|
|
#endif
|
|
|
|
|
2007-06-12 20:30:11 -03:00
|
|
|
/* Newline flags */
|
|
|
|
#define NEWLINE_UNKNOWN 0 /* No newline seen, yet */
|
|
|
|
#define NEWLINE_CR 1 /* \r newline seen */
|
|
|
|
#define NEWLINE_LF 2 /* \n newline seen */
|
|
|
|
#define NEWLINE_CRLF 4 /* \r\n newline seen */
|
2001-01-07 17:19:34 -04:00
|
|
|
|
2007-06-12 20:30:11 -03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
1997-05-05 19:15:02 -03:00
|
|
|
|
2007-06-12 20:30:11 -03:00
|
|
|
/* External C interface */
|
1991-03-06 09:06:18 -04:00
|
|
|
|
2007-06-12 20:30:11 -03:00
|
|
|
PyObject *
|
2007-10-21 21:09:51 -03:00
|
|
|
PyFile_FromFd(int fd, char *name, char *mode, int buffering, char *encoding,
|
2007-12-03 18:54:21 -04:00
|
|
|
char *errors, char *newline, int closefd)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2007-10-21 21:09:51 -03:00
|
|
|
PyObject *io, *stream, *nameobj = NULL;
|
1991-04-04 11:21:57 -04:00
|
|
|
|
2007-06-12 20:30:11 -03:00
|
|
|
io = PyImport_ImportModule("io");
|
|
|
|
if (io == NULL)
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
2007-12-03 18:54:21 -04:00
|
|
|
stream = PyObject_CallMethod(io, "open", "isisssi", fd, mode,
|
|
|
|
buffering, encoding, errors,
|
|
|
|
newline, closefd);
|
2007-10-19 20:16:50 -03:00
|
|
|
Py_DECREF(io);
|
2007-06-12 20:30:11 -03:00
|
|
|
if (stream == NULL)
|
|
|
|
return NULL;
|
2007-10-19 20:16:50 -03:00
|
|
|
if (name != NULL) {
|
|
|
|
nameobj = PyUnicode_FromString(name);
|
|
|
|
if (nameobj == NULL)
|
2007-06-12 20:30:11 -03:00
|
|
|
PyErr_Clear();
|
2007-10-19 20:16:50 -03:00
|
|
|
else {
|
|
|
|
if (PyObject_SetAttrString(stream, "name", nameobj) < 0)
|
|
|
|
PyErr_Clear();
|
|
|
|
Py_DECREF(nameobj);
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
2007-06-12 20:30:11 -03:00
|
|
|
return stream;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2000-07-09 02:02:18 -03:00
|
|
|
PyFile_GetLine(PyObject *f, int n)
|
1991-04-04 11:21:57 -04:00
|
|
|
{
|
2001-01-07 16:51:39 -04:00
|
|
|
PyObject *result;
|
|
|
|
|
1992-09-25 18:59:05 -03:00
|
|
|
if (f == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_BadInternalCall();
|
1991-04-04 11:21:57 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-01-07 16:51:39 -04:00
|
|
|
|
2007-06-12 20:30:11 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *reader;
|
|
|
|
PyObject *args;
|
2001-01-07 16:51:39 -04:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
reader = PyObject_GetAttrString(f, "readline");
|
1992-09-25 18:59:05 -03:00
|
|
|
if (reader == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (n <= 0)
|
2003-10-12 16:09:37 -03:00
|
|
|
args = PyTuple_New(0);
|
1992-09-25 18:59:05 -03:00
|
|
|
else
|
1997-05-02 00:12:38 -03:00
|
|
|
args = Py_BuildValue("(i)", n);
|
1992-09-25 18:59:05 -03:00
|
|
|
if (args == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(reader);
|
1992-09-25 18:59:05 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
result = PyEval_CallObject(reader, args);
|
|
|
|
Py_DECREF(reader);
|
|
|
|
Py_DECREF(args);
|
2003-01-03 15:16:14 -04:00
|
|
|
if (result != NULL && !PyString_Check(result) &&
|
|
|
|
!PyUnicode_Check(result)) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(result);
|
1992-09-25 18:59:05 -03:00
|
|
|
result = NULL;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1992-09-25 18:59:05 -03:00
|
|
|
"object.readline() returned non-string");
|
|
|
|
}
|
2001-01-07 16:51:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (n < 0 && result != NULL && PyString_Check(result)) {
|
|
|
|
char *s = PyString_AS_STRING(result);
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t len = PyString_GET_SIZE(result);
|
2001-01-07 16:51:39 -04:00
|
|
|
if (len == 0) {
|
|
|
|
Py_DECREF(result);
|
|
|
|
result = NULL;
|
|
|
|
PyErr_SetString(PyExc_EOFError,
|
|
|
|
"EOF when reading a line");
|
|
|
|
}
|
|
|
|
else if (s[len-1] == '\n') {
|
|
|
|
if (result->ob_refcnt == 1)
|
|
|
|
_PyString_Resize(&result, len-1);
|
|
|
|
else {
|
|
|
|
PyObject *v;
|
|
|
|
v = PyString_FromStringAndSize(s, len-1);
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(result);
|
2001-01-07 16:51:39 -04:00
|
|
|
result = v;
|
1992-09-25 18:59:05 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-01-03 15:16:14 -04:00
|
|
|
if (n < 0 && result != NULL && PyUnicode_Check(result)) {
|
|
|
|
Py_UNICODE *s = PyUnicode_AS_UNICODE(result);
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t len = PyUnicode_GET_SIZE(result);
|
2003-01-03 15:16:14 -04:00
|
|
|
if (len == 0) {
|
|
|
|
Py_DECREF(result);
|
|
|
|
result = NULL;
|
|
|
|
PyErr_SetString(PyExc_EOFError,
|
|
|
|
"EOF when reading a line");
|
|
|
|
}
|
|
|
|
else if (s[len-1] == '\n') {
|
|
|
|
if (result->ob_refcnt == 1)
|
|
|
|
PyUnicode_Resize(&result, len-1);
|
|
|
|
else {
|
|
|
|
PyObject *v;
|
|
|
|
v = PyUnicode_FromUnicode(s, len-1);
|
|
|
|
Py_DECREF(result);
|
|
|
|
result = v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-01-07 16:51:39 -04:00
|
|
|
return result;
|
1991-04-04 11:21:57 -04:00
|
|
|
}
|
|
|
|
|
1992-09-25 18:59:05 -03:00
|
|
|
/* Interfaces to write objects/strings to file-like objects */
|
|
|
|
|
|
|
|
int
|
2000-07-09 02:02:18 -03:00
|
|
|
PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
|
1992-09-25 18:59:05 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *writer, *value, *args, *result;
|
1992-09-25 18:59:05 -03:00
|
|
|
if (f == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
|
1992-09-25 18:59:05 -03:00
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
writer = PyObject_GetAttrString(f, "write");
|
1992-09-25 18:59:05 -03:00
|
|
|
if (writer == NULL)
|
|
|
|
return -1;
|
2001-09-19 10:47:32 -03:00
|
|
|
if (flags & Py_PRINT_RAW) {
|
2007-11-06 17:34:58 -04:00
|
|
|
value = PyObject_Str(v);
|
2001-09-19 10:47:32 -03:00
|
|
|
}
|
2007-07-11 06:28:11 -03:00
|
|
|
else
|
2007-10-09 18:55:58 -03:00
|
|
|
value = PyObject_Repr(v);
|
1993-11-05 06:22:19 -04:00
|
|
|
if (value == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(writer);
|
1993-11-05 06:22:19 -04:00
|
|
|
return -1;
|
1992-09-25 18:59:05 -03:00
|
|
|
}
|
2003-10-12 16:09:37 -03:00
|
|
|
args = PyTuple_Pack(1, value);
|
1997-05-22 11:02:25 -03:00
|
|
|
if (args == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(value);
|
|
|
|
Py_DECREF(writer);
|
1995-07-10 20:32:26 -03:00
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
result = PyEval_CallObject(writer, args);
|
|
|
|
Py_DECREF(args);
|
|
|
|
Py_DECREF(value);
|
|
|
|
Py_DECREF(writer);
|
1992-09-25 18:59:05 -03:00
|
|
|
if (result == NULL)
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(result);
|
1992-09-25 18:59:05 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-05-22 19:25:11 -03:00
|
|
|
int
|
2001-11-28 18:13:25 -04:00
|
|
|
PyFile_WriteString(const char *s, PyObject *f)
|
1992-09-25 18:59:05 -03:00
|
|
|
{
|
|
|
|
if (f == NULL) {
|
1997-05-22 19:25:11 -03:00
|
|
|
/* Should be caused by a pre-existing error */
|
2000-07-09 02:02:18 -03:00
|
|
|
if (!PyErr_Occurred())
|
1997-05-22 19:25:11 -03:00
|
|
|
PyErr_SetString(PyExc_SystemError,
|
|
|
|
"null file for PyFile_WriteString");
|
|
|
|
return -1;
|
1992-09-25 18:59:05 -03:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
else if (!PyErr_Occurred()) {
|
2007-08-09 19:58:05 -03:00
|
|
|
PyObject *v = PyUnicode_FromString(s);
|
1997-05-22 19:25:11 -03:00
|
|
|
int err;
|
|
|
|
if (v == NULL)
|
|
|
|
return -1;
|
|
|
|
err = PyFile_WriteObject(v, f, Py_PRINT_RAW);
|
|
|
|
Py_DECREF(v);
|
|
|
|
return err;
|
1992-09-25 18:59:05 -03:00
|
|
|
}
|
1997-07-13 00:56:50 -03:00
|
|
|
else
|
|
|
|
return -1;
|
1992-09-25 18:59:05 -03:00
|
|
|
}
|
2000-07-13 20:56:54 -03:00
|
|
|
|
|
|
|
/* Try to get a file-descriptor from a Python object. If the object
|
|
|
|
is an integer or long integer, its value is returned. If not, the
|
|
|
|
object's fileno() method is called if it exists; the method must return
|
|
|
|
an integer or long integer, which is returned as the file descriptor value.
|
|
|
|
-1 is returned on failure.
|
|
|
|
*/
|
|
|
|
|
2007-06-12 20:30:11 -03:00
|
|
|
int
|
|
|
|
PyObject_AsFileDescriptor(PyObject *o)
|
2000-07-13 20:56:54 -03:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
PyObject *meth;
|
|
|
|
|
2007-10-23 15:25:20 -03:00
|
|
|
if (PyLong_Check(o)) {
|
2000-07-13 20:56:54 -03:00
|
|
|
fd = PyLong_AsLong(o);
|
|
|
|
}
|
|
|
|
else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
|
|
|
|
{
|
|
|
|
PyObject *fno = PyEval_CallObject(meth, NULL);
|
|
|
|
Py_DECREF(meth);
|
|
|
|
if (fno == NULL)
|
|
|
|
return -1;
|
2001-01-07 17:19:34 -04:00
|
|
|
|
2007-10-23 15:25:20 -03:00
|
|
|
if (PyLong_Check(fno)) {
|
2000-07-13 20:56:54 -03:00
|
|
|
fd = PyLong_AsLong(fno);
|
|
|
|
Py_DECREF(fno);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"fileno() returned a non-integer");
|
|
|
|
Py_DECREF(fno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"argument must be an int, or have a fileno() method.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-01-13 23:31:43 -04:00
|
|
|
if (fd == -1 && PyErr_Occurred())
|
|
|
|
return -1;
|
2000-07-13 20:56:54 -03:00
|
|
|
if (fd < 0) {
|
|
|
|
PyErr_Format(PyExc_ValueError,
|
|
|
|
"file descriptor cannot be a negative integer (%i)",
|
|
|
|
fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
2002-04-14 17:12:41 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Py_UniversalNewlineFgets is an fgets variation that understands
|
|
|
|
** all of \r, \n and \r\n conventions.
|
|
|
|
** The stream should be opened in binary mode.
|
|
|
|
** If fobj is NULL the routine always does newline conversion, and
|
|
|
|
** it may peek one char ahead to gobble the second char in \r\n.
|
|
|
|
** If fobj is non-NULL it must be a PyFileObject. In this case there
|
|
|
|
** is no readahead but in stead a flag is used to skip a following
|
|
|
|
** \n on the next read. Also, if the file is open in binary mode
|
|
|
|
** the whole conversion is skipped. Finally, the routine keeps track of
|
|
|
|
** the different types of newlines seen.
|
|
|
|
** Note that we need no error handling: fgets() treats error and eof
|
|
|
|
** identically.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj)
|
|
|
|
{
|
|
|
|
char *p = buf;
|
|
|
|
int c;
|
|
|
|
int newlinetypes = 0;
|
|
|
|
int skipnextlf = 0;
|
2002-04-21 04:29:14 -03:00
|
|
|
|
2002-04-14 17:12:41 -03:00
|
|
|
if (fobj) {
|
2007-06-12 20:30:11 -03:00
|
|
|
errno = ENXIO; /* What can you do... */
|
|
|
|
return NULL;
|
2002-04-14 17:12:41 -03:00
|
|
|
}
|
|
|
|
FLOCKFILE(stream);
|
|
|
|
c = 'x'; /* Shut up gcc warning */
|
|
|
|
while (--n > 0 && (c = GETC(stream)) != EOF ) {
|
|
|
|
if (skipnextlf ) {
|
|
|
|
skipnextlf = 0;
|
|
|
|
if (c == '\n') {
|
|
|
|
/* Seeing a \n here with skipnextlf true
|
|
|
|
** means we saw a \r before.
|
|
|
|
*/
|
|
|
|
newlinetypes |= NEWLINE_CRLF;
|
|
|
|
c = GETC(stream);
|
|
|
|
if (c == EOF) break;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
** Note that c == EOF also brings us here,
|
|
|
|
** so we're okay if the last char in the file
|
|
|
|
** is a CR.
|
|
|
|
*/
|
|
|
|
newlinetypes |= NEWLINE_CR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (c == '\r') {
|
|
|
|
/* A \r is translated into a \n, and we skip
|
|
|
|
** an adjacent \n, if any. We don't set the
|
|
|
|
** newlinetypes flag until we've seen the next char.
|
|
|
|
*/
|
|
|
|
skipnextlf = 1;
|
|
|
|
c = '\n';
|
|
|
|
} else if ( c == '\n') {
|
|
|
|
newlinetypes |= NEWLINE_LF;
|
|
|
|
}
|
|
|
|
*p++ = c;
|
|
|
|
if (c == '\n') break;
|
|
|
|
}
|
|
|
|
if ( c == EOF && skipnextlf )
|
|
|
|
newlinetypes |= NEWLINE_CR;
|
|
|
|
FUNLOCKFILE(stream);
|
|
|
|
*p = '\0';
|
2007-06-12 20:30:11 -03:00
|
|
|
if ( skipnextlf ) {
|
2002-04-14 17:12:41 -03:00
|
|
|
/* If we have no file object we cannot save the
|
|
|
|
** skipnextlf flag. We have to readahead, which
|
|
|
|
** will cause a pause if we're reading from an
|
|
|
|
** interactive stream, but that is very unlikely
|
|
|
|
** unless we're doing something silly like
|
2007-08-11 21:43:29 -03:00
|
|
|
** exec(open("/dev/tty").read()).
|
2002-04-14 17:12:41 -03:00
|
|
|
*/
|
|
|
|
c = GETC(stream);
|
|
|
|
if ( c != '\n' )
|
|
|
|
ungetc(c, stream);
|
|
|
|
}
|
|
|
|
if (p == buf)
|
|
|
|
return NULL;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2007-11-12 12:05:45 -04:00
|
|
|
/* **************************** std printer ****************************
|
|
|
|
* The stdprinter is used during the boot strapping phase as a preliminary
|
|
|
|
* file like object for sys.stderr.
|
|
|
|
*/
|
2007-10-30 15:34:07 -03:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
|
|
|
int fd;
|
|
|
|
} PyStdPrinter_Object;
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
stdprinter_new(PyTypeObject *type, PyObject *args, PyObject *kews)
|
|
|
|
{
|
|
|
|
PyStdPrinter_Object *self;
|
|
|
|
|
|
|
|
assert(type != NULL && type->tp_alloc != NULL);
|
|
|
|
|
|
|
|
self = (PyStdPrinter_Object *) type->tp_alloc(type, 0);
|
|
|
|
if (self != NULL) {
|
|
|
|
self->fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (PyObject *) self;
|
|
|
|
}
|
|
|
|
|
2007-11-12 12:05:45 -04:00
|
|
|
static int
|
|
|
|
fileio_init(PyObject *self, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"cannot create 'stderrprinter' instances");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-10-30 15:34:07 -03:00
|
|
|
PyObject *
|
|
|
|
PyFile_NewStdPrinter(int fd)
|
|
|
|
{
|
|
|
|
PyStdPrinter_Object *self;
|
|
|
|
|
2007-11-12 22:19:40 -04:00
|
|
|
if (fd != fileno(stdout) && fd != fileno(stderr)) {
|
2007-11-09 20:30:14 -04:00
|
|
|
/* not enough infrastructure for PyErr_BadInternalCall() */
|
2007-10-30 15:34:07 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
self = PyObject_New(PyStdPrinter_Object,
|
|
|
|
&PyStdPrinter_Type);
|
2007-10-30 15:36:44 -03:00
|
|
|
if (self != NULL) {
|
|
|
|
self->fd = fd;
|
|
|
|
}
|
2007-10-30 15:34:07 -03:00
|
|
|
return (PyObject*)self;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
stdprinter_write(PyStdPrinter_Object *self, PyObject *args)
|
|
|
|
{
|
|
|
|
char *c;
|
|
|
|
Py_ssize_t n;
|
|
|
|
|
|
|
|
if (self->fd < 0) {
|
2007-11-12 12:05:45 -04:00
|
|
|
/* fd might be invalid on Windows
|
|
|
|
* I can't raise an exception here. It may lead to an
|
|
|
|
* unlimited recursion in the case stderr is invalid.
|
|
|
|
*/
|
2007-11-12 13:02:51 -04:00
|
|
|
Py_RETURN_NONE;
|
2007-10-30 15:34:07 -03:00
|
|
|
}
|
|
|
|
|
2007-11-12 12:05:45 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "s", &c)) {
|
2007-10-30 15:34:07 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2007-11-12 12:05:45 -04:00
|
|
|
n = strlen(c);
|
2007-10-30 15:34:07 -03:00
|
|
|
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
errno = 0;
|
|
|
|
n = write(self->fd, c, n);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
|
|
|
if (n < 0) {
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-11-12 12:05:45 -04:00
|
|
|
return PyLong_FromSsize_t(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
stdprinter_fileno(PyStdPrinter_Object *self)
|
|
|
|
{
|
2007-12-02 10:31:20 -04:00
|
|
|
return PyLong_FromLong((long) self->fd);
|
2007-11-12 12:05:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
stdprinter_repr(PyStdPrinter_Object *self)
|
|
|
|
{
|
|
|
|
return PyUnicode_FromFormat("<stdprinter(fd=%d) object at 0x%x>",
|
|
|
|
self->fd, self);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
stdprinter_noop(PyStdPrinter_Object *self)
|
|
|
|
{
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
stdprinter_isatty(PyStdPrinter_Object *self)
|
|
|
|
{
|
|
|
|
long res;
|
|
|
|
if (self->fd < 0) {
|
2007-11-12 13:02:51 -04:00
|
|
|
Py_RETURN_FALSE;
|
2007-11-12 12:05:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
res = isatty(self->fd);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
|
|
|
return PyBool_FromLong(res);
|
2007-10-30 15:34:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef stdprinter_methods[] = {
|
2007-11-12 12:05:45 -04:00
|
|
|
{"close", (PyCFunction)stdprinter_noop, METH_NOARGS, ""},
|
|
|
|
{"flush", (PyCFunction)stdprinter_noop, METH_NOARGS, ""},
|
|
|
|
{"fileno", (PyCFunction)stdprinter_fileno, METH_NOARGS, ""},
|
|
|
|
{"isatty", (PyCFunction)stdprinter_isatty, METH_NOARGS, ""},
|
|
|
|
{"write", (PyCFunction)stdprinter_write, METH_VARARGS, ""},
|
|
|
|
{NULL, NULL} /*sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
get_closed(PyStdPrinter_Object *self, void *closure)
|
|
|
|
{
|
|
|
|
Py_INCREF(Py_False);
|
|
|
|
return Py_False;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
get_mode(PyStdPrinter_Object *self, void *closure)
|
|
|
|
{
|
|
|
|
return PyUnicode_FromString("w");
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
get_encoding(PyStdPrinter_Object *self, void *closure)
|
|
|
|
{
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyGetSetDef stdprinter_getsetlist[] = {
|
|
|
|
{"closed", (getter)get_closed, NULL, "True if the file is closed"},
|
|
|
|
{"encoding", (getter)get_encoding, NULL, "Encoding of the file"},
|
|
|
|
{"mode", (getter)get_mode, NULL, "String giving the file mode"},
|
|
|
|
{0},
|
2007-10-30 15:34:07 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
PyTypeObject PyStdPrinter_Type = {
|
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
|
|
"stderrprinter", /* tp_name */
|
|
|
|
sizeof(PyStdPrinter_Object), /* tp_basicsize */
|
|
|
|
0, /* tp_itemsize */
|
|
|
|
/* methods */
|
|
|
|
0, /* tp_dealloc */
|
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
0, /* tp_compare */
|
2007-11-12 12:05:45 -04:00
|
|
|
(reprfunc)stdprinter_repr, /* tp_repr */
|
2007-10-30 15:34:07 -03:00
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT, /* tp_flags */
|
|
|
|
0, /* tp_doc */
|
|
|
|
0, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
stdprinter_methods, /* tp_methods */
|
|
|
|
0, /* tp_members */
|
2007-11-12 12:05:45 -04:00
|
|
|
stdprinter_getsetlist, /* tp_getset */
|
2007-10-30 15:34:07 -03:00
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
0, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
0, /* tp_dictoffset */
|
2007-11-12 12:05:45 -04:00
|
|
|
fileio_init, /* tp_init */
|
2007-10-30 15:34:07 -03:00
|
|
|
PyType_GenericAlloc, /* tp_alloc */
|
|
|
|
stdprinter_new, /* tp_new */
|
|
|
|
PyObject_Del, /* tp_free */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2006-04-21 07:40:58 -03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|