| 1 | n/a | /* File object implementation (what's left of it -- see io.py) */ |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | #define PY_SSIZE_T_CLEAN |
|---|
| 4 | n/a | #include "Python.h" |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | #ifdef HAVE_GETC_UNLOCKED |
|---|
| 7 | n/a | #define GETC(f) getc_unlocked(f) |
|---|
| 8 | n/a | #define FLOCKFILE(f) flockfile(f) |
|---|
| 9 | n/a | #define FUNLOCKFILE(f) funlockfile(f) |
|---|
| 10 | n/a | #else |
|---|
| 11 | n/a | #define GETC(f) getc(f) |
|---|
| 12 | n/a | #define FLOCKFILE(f) |
|---|
| 13 | n/a | #define FUNLOCKFILE(f) |
|---|
| 14 | n/a | #endif |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | /* Newline flags */ |
|---|
| 17 | n/a | #define NEWLINE_UNKNOWN 0 /* No newline seen, yet */ |
|---|
| 18 | n/a | #define NEWLINE_CR 1 /* \r newline seen */ |
|---|
| 19 | n/a | #define NEWLINE_LF 2 /* \n newline seen */ |
|---|
| 20 | n/a | #define NEWLINE_CRLF 4 /* \r\n newline seen */ |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | #ifdef __cplusplus |
|---|
| 23 | n/a | extern "C" { |
|---|
| 24 | n/a | #endif |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | /* External C interface */ |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | PyObject * |
|---|
| 29 | n/a | PyFile_FromFd(int fd, const char *name, const char *mode, int buffering, const char *encoding, |
|---|
| 30 | n/a | const char *errors, const char *newline, int closefd) |
|---|
| 31 | n/a | { |
|---|
| 32 | n/a | PyObject *io, *stream; |
|---|
| 33 | n/a | _Py_IDENTIFIER(open); |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | io = PyImport_ImportModule("io"); |
|---|
| 36 | n/a | if (io == NULL) |
|---|
| 37 | n/a | return NULL; |
|---|
| 38 | n/a | stream = _PyObject_CallMethodId(io, &PyId_open, "isisssi", fd, mode, |
|---|
| 39 | n/a | buffering, encoding, errors, |
|---|
| 40 | n/a | newline, closefd); |
|---|
| 41 | n/a | Py_DECREF(io); |
|---|
| 42 | n/a | if (stream == NULL) |
|---|
| 43 | n/a | return NULL; |
|---|
| 44 | n/a | /* ignore name attribute because the name attribute of _BufferedIOMixin |
|---|
| 45 | n/a | and TextIOWrapper is read only */ |
|---|
| 46 | n/a | return stream; |
|---|
| 47 | n/a | } |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | PyObject * |
|---|
| 50 | n/a | PyFile_GetLine(PyObject *f, int n) |
|---|
| 51 | n/a | { |
|---|
| 52 | n/a | PyObject *result; |
|---|
| 53 | n/a | |
|---|
| 54 | n/a | if (f == NULL) { |
|---|
| 55 | n/a | PyErr_BadInternalCall(); |
|---|
| 56 | n/a | return NULL; |
|---|
| 57 | n/a | } |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | { |
|---|
| 60 | n/a | PyObject *reader; |
|---|
| 61 | n/a | PyObject *args; |
|---|
| 62 | n/a | _Py_IDENTIFIER(readline); |
|---|
| 63 | n/a | |
|---|
| 64 | n/a | reader = _PyObject_GetAttrId(f, &PyId_readline); |
|---|
| 65 | n/a | if (reader == NULL) |
|---|
| 66 | n/a | return NULL; |
|---|
| 67 | n/a | if (n <= 0) |
|---|
| 68 | n/a | args = PyTuple_New(0); |
|---|
| 69 | n/a | else |
|---|
| 70 | n/a | args = Py_BuildValue("(i)", n); |
|---|
| 71 | n/a | if (args == NULL) { |
|---|
| 72 | n/a | Py_DECREF(reader); |
|---|
| 73 | n/a | return NULL; |
|---|
| 74 | n/a | } |
|---|
| 75 | n/a | result = PyEval_CallObject(reader, args); |
|---|
| 76 | n/a | Py_DECREF(reader); |
|---|
| 77 | n/a | Py_DECREF(args); |
|---|
| 78 | n/a | if (result != NULL && !PyBytes_Check(result) && |
|---|
| 79 | n/a | !PyUnicode_Check(result)) { |
|---|
| 80 | n/a | Py_DECREF(result); |
|---|
| 81 | n/a | result = NULL; |
|---|
| 82 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 83 | n/a | "object.readline() returned non-string"); |
|---|
| 84 | n/a | } |
|---|
| 85 | n/a | } |
|---|
| 86 | n/a | |
|---|
| 87 | n/a | if (n < 0 && result != NULL && PyBytes_Check(result)) { |
|---|
| 88 | n/a | char *s = PyBytes_AS_STRING(result); |
|---|
| 89 | n/a | Py_ssize_t len = PyBytes_GET_SIZE(result); |
|---|
| 90 | n/a | if (len == 0) { |
|---|
| 91 | n/a | Py_DECREF(result); |
|---|
| 92 | n/a | result = NULL; |
|---|
| 93 | n/a | PyErr_SetString(PyExc_EOFError, |
|---|
| 94 | n/a | "EOF when reading a line"); |
|---|
| 95 | n/a | } |
|---|
| 96 | n/a | else if (s[len-1] == '\n') { |
|---|
| 97 | n/a | if (result->ob_refcnt == 1) |
|---|
| 98 | n/a | _PyBytes_Resize(&result, len-1); |
|---|
| 99 | n/a | else { |
|---|
| 100 | n/a | PyObject *v; |
|---|
| 101 | n/a | v = PyBytes_FromStringAndSize(s, len-1); |
|---|
| 102 | n/a | Py_DECREF(result); |
|---|
| 103 | n/a | result = v; |
|---|
| 104 | n/a | } |
|---|
| 105 | n/a | } |
|---|
| 106 | n/a | } |
|---|
| 107 | n/a | if (n < 0 && result != NULL && PyUnicode_Check(result)) { |
|---|
| 108 | n/a | Py_ssize_t len = PyUnicode_GET_LENGTH(result); |
|---|
| 109 | n/a | if (len == 0) { |
|---|
| 110 | n/a | Py_DECREF(result); |
|---|
| 111 | n/a | result = NULL; |
|---|
| 112 | n/a | PyErr_SetString(PyExc_EOFError, |
|---|
| 113 | n/a | "EOF when reading a line"); |
|---|
| 114 | n/a | } |
|---|
| 115 | n/a | else if (PyUnicode_READ_CHAR(result, len-1) == '\n') { |
|---|
| 116 | n/a | PyObject *v; |
|---|
| 117 | n/a | v = PyUnicode_Substring(result, 0, len-1); |
|---|
| 118 | n/a | Py_DECREF(result); |
|---|
| 119 | n/a | result = v; |
|---|
| 120 | n/a | } |
|---|
| 121 | n/a | } |
|---|
| 122 | n/a | return result; |
|---|
| 123 | n/a | } |
|---|
| 124 | n/a | |
|---|
| 125 | n/a | /* Interfaces to write objects/strings to file-like objects */ |
|---|
| 126 | n/a | |
|---|
| 127 | n/a | int |
|---|
| 128 | n/a | PyFile_WriteObject(PyObject *v, PyObject *f, int flags) |
|---|
| 129 | n/a | { |
|---|
| 130 | n/a | PyObject *writer, *value, *result; |
|---|
| 131 | n/a | _Py_IDENTIFIER(write); |
|---|
| 132 | n/a | |
|---|
| 133 | n/a | if (f == NULL) { |
|---|
| 134 | n/a | PyErr_SetString(PyExc_TypeError, "writeobject with NULL file"); |
|---|
| 135 | n/a | return -1; |
|---|
| 136 | n/a | } |
|---|
| 137 | n/a | writer = _PyObject_GetAttrId(f, &PyId_write); |
|---|
| 138 | n/a | if (writer == NULL) |
|---|
| 139 | n/a | return -1; |
|---|
| 140 | n/a | if (flags & Py_PRINT_RAW) { |
|---|
| 141 | n/a | value = PyObject_Str(v); |
|---|
| 142 | n/a | } |
|---|
| 143 | n/a | else |
|---|
| 144 | n/a | value = PyObject_Repr(v); |
|---|
| 145 | n/a | if (value == NULL) { |
|---|
| 146 | n/a | Py_DECREF(writer); |
|---|
| 147 | n/a | return -1; |
|---|
| 148 | n/a | } |
|---|
| 149 | n/a | result = PyObject_CallFunctionObjArgs(writer, value, NULL); |
|---|
| 150 | n/a | Py_DECREF(value); |
|---|
| 151 | n/a | Py_DECREF(writer); |
|---|
| 152 | n/a | if (result == NULL) |
|---|
| 153 | n/a | return -1; |
|---|
| 154 | n/a | Py_DECREF(result); |
|---|
| 155 | n/a | return 0; |
|---|
| 156 | n/a | } |
|---|
| 157 | n/a | |
|---|
| 158 | n/a | int |
|---|
| 159 | n/a | PyFile_WriteString(const char *s, PyObject *f) |
|---|
| 160 | n/a | { |
|---|
| 161 | n/a | if (f == NULL) { |
|---|
| 162 | n/a | /* Should be caused by a pre-existing error */ |
|---|
| 163 | n/a | if (!PyErr_Occurred()) |
|---|
| 164 | n/a | PyErr_SetString(PyExc_SystemError, |
|---|
| 165 | n/a | "null file for PyFile_WriteString"); |
|---|
| 166 | n/a | return -1; |
|---|
| 167 | n/a | } |
|---|
| 168 | n/a | else if (!PyErr_Occurred()) { |
|---|
| 169 | n/a | PyObject *v = PyUnicode_FromString(s); |
|---|
| 170 | n/a | int err; |
|---|
| 171 | n/a | if (v == NULL) |
|---|
| 172 | n/a | return -1; |
|---|
| 173 | n/a | err = PyFile_WriteObject(v, f, Py_PRINT_RAW); |
|---|
| 174 | n/a | Py_DECREF(v); |
|---|
| 175 | n/a | return err; |
|---|
| 176 | n/a | } |
|---|
| 177 | n/a | else |
|---|
| 178 | n/a | return -1; |
|---|
| 179 | n/a | } |
|---|
| 180 | n/a | |
|---|
| 181 | n/a | /* Try to get a file-descriptor from a Python object. If the object |
|---|
| 182 | n/a | is an integer, its value is returned. If not, the |
|---|
| 183 | n/a | object's fileno() method is called if it exists; the method must return |
|---|
| 184 | n/a | an integer, which is returned as the file descriptor value. |
|---|
| 185 | n/a | -1 is returned on failure. |
|---|
| 186 | n/a | */ |
|---|
| 187 | n/a | |
|---|
| 188 | n/a | int |
|---|
| 189 | n/a | PyObject_AsFileDescriptor(PyObject *o) |
|---|
| 190 | n/a | { |
|---|
| 191 | n/a | int fd; |
|---|
| 192 | n/a | PyObject *meth; |
|---|
| 193 | n/a | _Py_IDENTIFIER(fileno); |
|---|
| 194 | n/a | |
|---|
| 195 | n/a | if (PyLong_Check(o)) { |
|---|
| 196 | n/a | fd = _PyLong_AsInt(o); |
|---|
| 197 | n/a | } |
|---|
| 198 | n/a | else if ((meth = _PyObject_GetAttrId(o, &PyId_fileno)) != NULL) |
|---|
| 199 | n/a | { |
|---|
| 200 | n/a | PyObject *fno = PyEval_CallObject(meth, NULL); |
|---|
| 201 | n/a | Py_DECREF(meth); |
|---|
| 202 | n/a | if (fno == NULL) |
|---|
| 203 | n/a | return -1; |
|---|
| 204 | n/a | |
|---|
| 205 | n/a | if (PyLong_Check(fno)) { |
|---|
| 206 | n/a | fd = _PyLong_AsInt(fno); |
|---|
| 207 | n/a | Py_DECREF(fno); |
|---|
| 208 | n/a | } |
|---|
| 209 | n/a | else { |
|---|
| 210 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 211 | n/a | "fileno() returned a non-integer"); |
|---|
| 212 | n/a | Py_DECREF(fno); |
|---|
| 213 | n/a | return -1; |
|---|
| 214 | n/a | } |
|---|
| 215 | n/a | } |
|---|
| 216 | n/a | else { |
|---|
| 217 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 218 | n/a | "argument must be an int, or have a fileno() method."); |
|---|
| 219 | n/a | return -1; |
|---|
| 220 | n/a | } |
|---|
| 221 | n/a | |
|---|
| 222 | n/a | if (fd == -1 && PyErr_Occurred()) |
|---|
| 223 | n/a | return -1; |
|---|
| 224 | n/a | if (fd < 0) { |
|---|
| 225 | n/a | PyErr_Format(PyExc_ValueError, |
|---|
| 226 | n/a | "file descriptor cannot be a negative integer (%i)", |
|---|
| 227 | n/a | fd); |
|---|
| 228 | n/a | return -1; |
|---|
| 229 | n/a | } |
|---|
| 230 | n/a | return fd; |
|---|
| 231 | n/a | } |
|---|
| 232 | n/a | |
|---|
| 233 | n/a | /* |
|---|
| 234 | n/a | ** Py_UniversalNewlineFgets is an fgets variation that understands |
|---|
| 235 | n/a | ** all of \r, \n and \r\n conventions. |
|---|
| 236 | n/a | ** The stream should be opened in binary mode. |
|---|
| 237 | n/a | ** If fobj is NULL the routine always does newline conversion, and |
|---|
| 238 | n/a | ** it may peek one char ahead to gobble the second char in \r\n. |
|---|
| 239 | n/a | ** If fobj is non-NULL it must be a PyFileObject. In this case there |
|---|
| 240 | n/a | ** is no readahead but in stead a flag is used to skip a following |
|---|
| 241 | n/a | ** \n on the next read. Also, if the file is open in binary mode |
|---|
| 242 | n/a | ** the whole conversion is skipped. Finally, the routine keeps track of |
|---|
| 243 | n/a | ** the different types of newlines seen. |
|---|
| 244 | n/a | ** Note that we need no error handling: fgets() treats error and eof |
|---|
| 245 | n/a | ** identically. |
|---|
| 246 | n/a | */ |
|---|
| 247 | n/a | char * |
|---|
| 248 | n/a | Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj) |
|---|
| 249 | n/a | { |
|---|
| 250 | n/a | char *p = buf; |
|---|
| 251 | n/a | int c; |
|---|
| 252 | n/a | int newlinetypes = 0; |
|---|
| 253 | n/a | int skipnextlf = 0; |
|---|
| 254 | n/a | |
|---|
| 255 | n/a | if (fobj) { |
|---|
| 256 | n/a | errno = ENXIO; /* What can you do... */ |
|---|
| 257 | n/a | return NULL; |
|---|
| 258 | n/a | } |
|---|
| 259 | n/a | FLOCKFILE(stream); |
|---|
| 260 | n/a | c = 'x'; /* Shut up gcc warning */ |
|---|
| 261 | n/a | while (--n > 0 && (c = GETC(stream)) != EOF ) { |
|---|
| 262 | n/a | if (skipnextlf ) { |
|---|
| 263 | n/a | skipnextlf = 0; |
|---|
| 264 | n/a | if (c == '\n') { |
|---|
| 265 | n/a | /* Seeing a \n here with skipnextlf true |
|---|
| 266 | n/a | ** means we saw a \r before. |
|---|
| 267 | n/a | */ |
|---|
| 268 | n/a | newlinetypes |= NEWLINE_CRLF; |
|---|
| 269 | n/a | c = GETC(stream); |
|---|
| 270 | n/a | if (c == EOF) break; |
|---|
| 271 | n/a | } else { |
|---|
| 272 | n/a | /* |
|---|
| 273 | n/a | ** Note that c == EOF also brings us here, |
|---|
| 274 | n/a | ** so we're okay if the last char in the file |
|---|
| 275 | n/a | ** is a CR. |
|---|
| 276 | n/a | */ |
|---|
| 277 | n/a | newlinetypes |= NEWLINE_CR; |
|---|
| 278 | n/a | } |
|---|
| 279 | n/a | } |
|---|
| 280 | n/a | if (c == '\r') { |
|---|
| 281 | n/a | /* A \r is translated into a \n, and we skip |
|---|
| 282 | n/a | ** an adjacent \n, if any. We don't set the |
|---|
| 283 | n/a | ** newlinetypes flag until we've seen the next char. |
|---|
| 284 | n/a | */ |
|---|
| 285 | n/a | skipnextlf = 1; |
|---|
| 286 | n/a | c = '\n'; |
|---|
| 287 | n/a | } else if ( c == '\n') { |
|---|
| 288 | n/a | newlinetypes |= NEWLINE_LF; |
|---|
| 289 | n/a | } |
|---|
| 290 | n/a | *p++ = c; |
|---|
| 291 | n/a | if (c == '\n') break; |
|---|
| 292 | n/a | } |
|---|
| 293 | n/a | /* if ( c == EOF && skipnextlf ) |
|---|
| 294 | n/a | newlinetypes |= NEWLINE_CR; */ |
|---|
| 295 | n/a | FUNLOCKFILE(stream); |
|---|
| 296 | n/a | *p = '\0'; |
|---|
| 297 | n/a | if ( skipnextlf ) { |
|---|
| 298 | n/a | /* If we have no file object we cannot save the |
|---|
| 299 | n/a | ** skipnextlf flag. We have to readahead, which |
|---|
| 300 | n/a | ** will cause a pause if we're reading from an |
|---|
| 301 | n/a | ** interactive stream, but that is very unlikely |
|---|
| 302 | n/a | ** unless we're doing something silly like |
|---|
| 303 | n/a | ** exec(open("/dev/tty").read()). |
|---|
| 304 | n/a | */ |
|---|
| 305 | n/a | c = GETC(stream); |
|---|
| 306 | n/a | if ( c != '\n' ) |
|---|
| 307 | n/a | ungetc(c, stream); |
|---|
| 308 | n/a | } |
|---|
| 309 | n/a | if (p == buf) |
|---|
| 310 | n/a | return NULL; |
|---|
| 311 | n/a | return buf; |
|---|
| 312 | n/a | } |
|---|
| 313 | n/a | |
|---|
| 314 | n/a | /* **************************** std printer **************************** |
|---|
| 315 | n/a | * The stdprinter is used during the boot strapping phase as a preliminary |
|---|
| 316 | n/a | * file like object for sys.stderr. |
|---|
| 317 | n/a | */ |
|---|
| 318 | n/a | |
|---|
| 319 | n/a | typedef struct { |
|---|
| 320 | n/a | PyObject_HEAD |
|---|
| 321 | n/a | int fd; |
|---|
| 322 | n/a | } PyStdPrinter_Object; |
|---|
| 323 | n/a | |
|---|
| 324 | n/a | static PyObject * |
|---|
| 325 | n/a | stdprinter_new(PyTypeObject *type, PyObject *args, PyObject *kews) |
|---|
| 326 | n/a | { |
|---|
| 327 | n/a | PyStdPrinter_Object *self; |
|---|
| 328 | n/a | |
|---|
| 329 | n/a | assert(type != NULL && type->tp_alloc != NULL); |
|---|
| 330 | n/a | |
|---|
| 331 | n/a | self = (PyStdPrinter_Object *) type->tp_alloc(type, 0); |
|---|
| 332 | n/a | if (self != NULL) { |
|---|
| 333 | n/a | self->fd = -1; |
|---|
| 334 | n/a | } |
|---|
| 335 | n/a | |
|---|
| 336 | n/a | return (PyObject *) self; |
|---|
| 337 | n/a | } |
|---|
| 338 | n/a | |
|---|
| 339 | n/a | static int |
|---|
| 340 | n/a | stdprinter_init(PyObject *self, PyObject *args, PyObject *kwds) |
|---|
| 341 | n/a | { |
|---|
| 342 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 343 | n/a | "cannot create 'stderrprinter' instances"); |
|---|
| 344 | n/a | return -1; |
|---|
| 345 | n/a | } |
|---|
| 346 | n/a | |
|---|
| 347 | n/a | PyObject * |
|---|
| 348 | n/a | PyFile_NewStdPrinter(int fd) |
|---|
| 349 | n/a | { |
|---|
| 350 | n/a | PyStdPrinter_Object *self; |
|---|
| 351 | n/a | |
|---|
| 352 | n/a | if (fd != fileno(stdout) && fd != fileno(stderr)) { |
|---|
| 353 | n/a | /* not enough infrastructure for PyErr_BadInternalCall() */ |
|---|
| 354 | n/a | return NULL; |
|---|
| 355 | n/a | } |
|---|
| 356 | n/a | |
|---|
| 357 | n/a | self = PyObject_New(PyStdPrinter_Object, |
|---|
| 358 | n/a | &PyStdPrinter_Type); |
|---|
| 359 | n/a | if (self != NULL) { |
|---|
| 360 | n/a | self->fd = fd; |
|---|
| 361 | n/a | } |
|---|
| 362 | n/a | return (PyObject*)self; |
|---|
| 363 | n/a | } |
|---|
| 364 | n/a | |
|---|
| 365 | n/a | static PyObject * |
|---|
| 366 | n/a | stdprinter_write(PyStdPrinter_Object *self, PyObject *args) |
|---|
| 367 | n/a | { |
|---|
| 368 | n/a | PyObject *unicode; |
|---|
| 369 | n/a | PyObject *bytes = NULL; |
|---|
| 370 | n/a | const char *str; |
|---|
| 371 | n/a | Py_ssize_t n; |
|---|
| 372 | n/a | int err; |
|---|
| 373 | n/a | |
|---|
| 374 | n/a | if (self->fd < 0) { |
|---|
| 375 | n/a | /* fd might be invalid on Windows |
|---|
| 376 | n/a | * I can't raise an exception here. It may lead to an |
|---|
| 377 | n/a | * unlimited recursion in the case stderr is invalid. |
|---|
| 378 | n/a | */ |
|---|
| 379 | n/a | Py_RETURN_NONE; |
|---|
| 380 | n/a | } |
|---|
| 381 | n/a | |
|---|
| 382 | n/a | if (!PyArg_ParseTuple(args, "U", &unicode)) |
|---|
| 383 | n/a | return NULL; |
|---|
| 384 | n/a | |
|---|
| 385 | n/a | /* encode Unicode to UTF-8 */ |
|---|
| 386 | n/a | str = PyUnicode_AsUTF8AndSize(unicode, &n); |
|---|
| 387 | n/a | if (str == NULL) { |
|---|
| 388 | n/a | PyErr_Clear(); |
|---|
| 389 | n/a | bytes = _PyUnicode_AsUTF8String(unicode, "backslashreplace"); |
|---|
| 390 | n/a | if (bytes == NULL) |
|---|
| 391 | n/a | return NULL; |
|---|
| 392 | n/a | str = PyBytes_AS_STRING(bytes); |
|---|
| 393 | n/a | n = PyBytes_GET_SIZE(bytes); |
|---|
| 394 | n/a | } |
|---|
| 395 | n/a | |
|---|
| 396 | n/a | n = _Py_write(self->fd, str, n); |
|---|
| 397 | n/a | /* save errno, it can be modified indirectly by Py_XDECREF() */ |
|---|
| 398 | n/a | err = errno; |
|---|
| 399 | n/a | |
|---|
| 400 | n/a | Py_XDECREF(bytes); |
|---|
| 401 | n/a | |
|---|
| 402 | n/a | if (n == -1) { |
|---|
| 403 | n/a | if (err == EAGAIN) { |
|---|
| 404 | n/a | PyErr_Clear(); |
|---|
| 405 | n/a | Py_RETURN_NONE; |
|---|
| 406 | n/a | } |
|---|
| 407 | n/a | return NULL; |
|---|
| 408 | n/a | } |
|---|
| 409 | n/a | |
|---|
| 410 | n/a | return PyLong_FromSsize_t(n); |
|---|
| 411 | n/a | } |
|---|
| 412 | n/a | |
|---|
| 413 | n/a | static PyObject * |
|---|
| 414 | n/a | stdprinter_fileno(PyStdPrinter_Object *self) |
|---|
| 415 | n/a | { |
|---|
| 416 | n/a | return PyLong_FromLong((long) self->fd); |
|---|
| 417 | n/a | } |
|---|
| 418 | n/a | |
|---|
| 419 | n/a | static PyObject * |
|---|
| 420 | n/a | stdprinter_repr(PyStdPrinter_Object *self) |
|---|
| 421 | n/a | { |
|---|
| 422 | n/a | return PyUnicode_FromFormat("<stdprinter(fd=%d) object at 0x%x>", |
|---|
| 423 | n/a | self->fd, self); |
|---|
| 424 | n/a | } |
|---|
| 425 | n/a | |
|---|
| 426 | n/a | static PyObject * |
|---|
| 427 | n/a | stdprinter_noop(PyStdPrinter_Object *self) |
|---|
| 428 | n/a | { |
|---|
| 429 | n/a | Py_RETURN_NONE; |
|---|
| 430 | n/a | } |
|---|
| 431 | n/a | |
|---|
| 432 | n/a | static PyObject * |
|---|
| 433 | n/a | stdprinter_isatty(PyStdPrinter_Object *self) |
|---|
| 434 | n/a | { |
|---|
| 435 | n/a | long res; |
|---|
| 436 | n/a | if (self->fd < 0) { |
|---|
| 437 | n/a | Py_RETURN_FALSE; |
|---|
| 438 | n/a | } |
|---|
| 439 | n/a | |
|---|
| 440 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 441 | n/a | res = isatty(self->fd); |
|---|
| 442 | n/a | Py_END_ALLOW_THREADS |
|---|
| 443 | n/a | |
|---|
| 444 | n/a | return PyBool_FromLong(res); |
|---|
| 445 | n/a | } |
|---|
| 446 | n/a | |
|---|
| 447 | n/a | static PyMethodDef stdprinter_methods[] = { |
|---|
| 448 | n/a | {"close", (PyCFunction)stdprinter_noop, METH_NOARGS, ""}, |
|---|
| 449 | n/a | {"flush", (PyCFunction)stdprinter_noop, METH_NOARGS, ""}, |
|---|
| 450 | n/a | {"fileno", (PyCFunction)stdprinter_fileno, METH_NOARGS, ""}, |
|---|
| 451 | n/a | {"isatty", (PyCFunction)stdprinter_isatty, METH_NOARGS, ""}, |
|---|
| 452 | n/a | {"write", (PyCFunction)stdprinter_write, METH_VARARGS, ""}, |
|---|
| 453 | n/a | {NULL, NULL} /*sentinel */ |
|---|
| 454 | n/a | }; |
|---|
| 455 | n/a | |
|---|
| 456 | n/a | static PyObject * |
|---|
| 457 | n/a | get_closed(PyStdPrinter_Object *self, void *closure) |
|---|
| 458 | n/a | { |
|---|
| 459 | n/a | Py_RETURN_FALSE; |
|---|
| 460 | n/a | } |
|---|
| 461 | n/a | |
|---|
| 462 | n/a | static PyObject * |
|---|
| 463 | n/a | get_mode(PyStdPrinter_Object *self, void *closure) |
|---|
| 464 | n/a | { |
|---|
| 465 | n/a | return PyUnicode_FromString("w"); |
|---|
| 466 | n/a | } |
|---|
| 467 | n/a | |
|---|
| 468 | n/a | static PyObject * |
|---|
| 469 | n/a | get_encoding(PyStdPrinter_Object *self, void *closure) |
|---|
| 470 | n/a | { |
|---|
| 471 | n/a | Py_RETURN_NONE; |
|---|
| 472 | n/a | } |
|---|
| 473 | n/a | |
|---|
| 474 | n/a | static PyGetSetDef stdprinter_getsetlist[] = { |
|---|
| 475 | n/a | {"closed", (getter)get_closed, NULL, "True if the file is closed"}, |
|---|
| 476 | n/a | {"encoding", (getter)get_encoding, NULL, "Encoding of the file"}, |
|---|
| 477 | n/a | {"mode", (getter)get_mode, NULL, "String giving the file mode"}, |
|---|
| 478 | n/a | {0}, |
|---|
| 479 | n/a | }; |
|---|
| 480 | n/a | |
|---|
| 481 | n/a | PyTypeObject PyStdPrinter_Type = { |
|---|
| 482 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|---|
| 483 | n/a | "stderrprinter", /* tp_name */ |
|---|
| 484 | n/a | sizeof(PyStdPrinter_Object), /* tp_basicsize */ |
|---|
| 485 | n/a | 0, /* tp_itemsize */ |
|---|
| 486 | n/a | /* methods */ |
|---|
| 487 | n/a | 0, /* tp_dealloc */ |
|---|
| 488 | n/a | 0, /* tp_print */ |
|---|
| 489 | n/a | 0, /* tp_getattr */ |
|---|
| 490 | n/a | 0, /* tp_setattr */ |
|---|
| 491 | n/a | 0, /* tp_reserved */ |
|---|
| 492 | n/a | (reprfunc)stdprinter_repr, /* tp_repr */ |
|---|
| 493 | n/a | 0, /* tp_as_number */ |
|---|
| 494 | n/a | 0, /* tp_as_sequence */ |
|---|
| 495 | n/a | 0, /* tp_as_mapping */ |
|---|
| 496 | n/a | 0, /* tp_hash */ |
|---|
| 497 | n/a | 0, /* tp_call */ |
|---|
| 498 | n/a | 0, /* tp_str */ |
|---|
| 499 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
|---|
| 500 | n/a | 0, /* tp_setattro */ |
|---|
| 501 | n/a | 0, /* tp_as_buffer */ |
|---|
| 502 | n/a | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|---|
| 503 | n/a | 0, /* tp_doc */ |
|---|
| 504 | n/a | 0, /* tp_traverse */ |
|---|
| 505 | n/a | 0, /* tp_clear */ |
|---|
| 506 | n/a | 0, /* tp_richcompare */ |
|---|
| 507 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 508 | n/a | 0, /* tp_iter */ |
|---|
| 509 | n/a | 0, /* tp_iternext */ |
|---|
| 510 | n/a | stdprinter_methods, /* tp_methods */ |
|---|
| 511 | n/a | 0, /* tp_members */ |
|---|
| 512 | n/a | stdprinter_getsetlist, /* tp_getset */ |
|---|
| 513 | n/a | 0, /* tp_base */ |
|---|
| 514 | n/a | 0, /* tp_dict */ |
|---|
| 515 | n/a | 0, /* tp_descr_get */ |
|---|
| 516 | n/a | 0, /* tp_descr_set */ |
|---|
| 517 | n/a | 0, /* tp_dictoffset */ |
|---|
| 518 | n/a | stdprinter_init, /* tp_init */ |
|---|
| 519 | n/a | PyType_GenericAlloc, /* tp_alloc */ |
|---|
| 520 | n/a | stdprinter_new, /* tp_new */ |
|---|
| 521 | n/a | PyObject_Del, /* tp_free */ |
|---|
| 522 | n/a | }; |
|---|
| 523 | n/a | |
|---|
| 524 | n/a | |
|---|
| 525 | n/a | #ifdef __cplusplus |
|---|
| 526 | n/a | } |
|---|
| 527 | n/a | #endif |
|---|