Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 1 | /* Author: Daniel Stutzbach */ |
| 2 | |
| 3 | #define PY_SSIZE_T_CLEAN |
| 4 | #include "Python.h" |
| 5 | #include <sys/types.h> |
| 6 | #include <sys/stat.h> |
| 7 | #include <fcntl.h> |
| 8 | #include <stddef.h> /* For offsetof */ |
| 9 | |
| 10 | /* |
| 11 | * Known likely problems: |
| 12 | * |
| 13 | * - Files larger then 2**32-1 |
| 14 | * - Files with unicode filenames |
| 15 | * - Passing numbers greater than 2**32-1 when an integer is expected |
| 16 | * - Making it work on Windows and other oddball platforms |
| 17 | * |
| 18 | * To Do: |
| 19 | * |
| 20 | * - autoconfify header file inclusion |
| 21 | * - Make the ABC RawIO type and inherit from it. |
| 22 | * |
| 23 | * Unanswered questions: |
| 24 | * |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 25 | * - Add mode and name properties a la Python 2 file objects? |
| 26 | * - Check for readable/writable before attempting to read/write? |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 27 | */ |
| 28 | |
| 29 | #ifdef MS_WINDOWS |
| 30 | /* can simulate truncate with Win32 API functions; see file_truncate */ |
| 31 | #define HAVE_FTRUNCATE |
| 32 | #define WIN32_LEAN_AND_MEAN |
| 33 | #include <windows.h> |
| 34 | #endif |
| 35 | |
| 36 | typedef struct { |
| 37 | PyObject_HEAD |
| 38 | int fd; |
| 39 | int own_fd; /* 1 means we should close fd */ |
| 40 | int readable; |
| 41 | int writable; |
| 42 | int seekable; /* -1 means unknown */ |
| 43 | PyObject *weakreflist; |
| 44 | } PyFileIOObject; |
| 45 | |
Collin Winter | af33438 | 2007-03-08 21:46:15 +0000 | [diff] [blame] | 46 | PyTypeObject PyFileIO_Type; |
| 47 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 48 | #define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type)) |
| 49 | |
| 50 | /* Note: if this function is changed so that it can return a true value, |
| 51 | * then we need a separate function for __exit__ |
| 52 | */ |
| 53 | static PyObject * |
| 54 | fileio_close(PyFileIOObject *self) |
| 55 | { |
| 56 | if (self->fd >= 0) { |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 57 | int fd = self->fd; |
| 58 | self->fd = -1; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 59 | Py_BEGIN_ALLOW_THREADS |
| 60 | errno = 0; |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 61 | close(fd); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 62 | Py_END_ALLOW_THREADS |
| 63 | if (errno < 0) { |
| 64 | PyErr_SetFromErrno(PyExc_IOError); |
| 65 | return NULL; |
| 66 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | Py_RETURN_NONE; |
| 70 | } |
| 71 | |
| 72 | static PyObject * |
| 73 | fileio_new(PyTypeObject *type, PyObject *args, PyObject *kews) |
| 74 | { |
| 75 | PyFileIOObject *self; |
| 76 | |
| 77 | assert(type != NULL && type->tp_alloc != NULL); |
| 78 | |
| 79 | self = (PyFileIOObject *) type->tp_alloc(type, 0); |
| 80 | if (self != NULL) { |
| 81 | self->fd = -1; |
| 82 | self->weakreflist = NULL; |
| 83 | self->own_fd = 1; |
| 84 | self->seekable = -1; |
| 85 | } |
| 86 | |
| 87 | return (PyObject *) self; |
| 88 | } |
| 89 | |
| 90 | /* On Unix, open will succeed for directories. |
| 91 | In Python, there should be no file objects referring to |
| 92 | directories, so we need a check. */ |
| 93 | |
| 94 | static int |
| 95 | dircheck(PyFileIOObject* self) |
| 96 | { |
| 97 | #if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR) |
| 98 | struct stat buf; |
| 99 | if (self->fd < 0) |
| 100 | return 0; |
| 101 | if (fstat(self->fd, &buf) == 0 && S_ISDIR(buf.st_mode)) { |
| 102 | #ifdef HAVE_STRERROR |
| 103 | char *msg = strerror(EISDIR); |
| 104 | #else |
| 105 | char *msg = "Is a directory"; |
| 106 | #endif |
| 107 | PyObject *exc; |
| 108 | PyObject *closeresult = fileio_close(self); |
| 109 | Py_DECREF(closeresult); |
| 110 | |
| 111 | exc = PyObject_CallFunction(PyExc_IOError, "(is)", |
| 112 | EISDIR, msg); |
| 113 | PyErr_SetObject(PyExc_IOError, exc); |
| 114 | Py_XDECREF(exc); |
| 115 | return -1; |
| 116 | } |
| 117 | #endif |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | static int |
| 123 | fileio_init(PyObject *oself, PyObject *args, PyObject *kwds) |
| 124 | { |
| 125 | PyFileIOObject *self = (PyFileIOObject *) oself; |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 126 | static char *kwlist[] = {"file", "mode", NULL}; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 127 | char *name = NULL; |
| 128 | char *mode = "r"; |
| 129 | char *s; |
| 130 | int wideargument = 0; |
| 131 | int ret = 0; |
| 132 | int rwa = 0, plus = 0, append = 0; |
| 133 | int flags = 0; |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 134 | int fd = -1; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 135 | |
| 136 | assert(PyFileIO_Check(oself)); |
| 137 | if (self->fd >= 0) |
| 138 | { |
| 139 | /* Have to close the existing file first. */ |
| 140 | PyObject *closeresult = fileio_close(self); |
| 141 | if (closeresult == NULL) |
| 142 | return -1; |
| 143 | Py_DECREF(closeresult); |
| 144 | } |
| 145 | |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 146 | if (PyArg_ParseTupleAndKeywords(args, kwds, "i|s:fileio", |
| 147 | kwlist, &fd, &mode)) { |
| 148 | if (fd < 0) { |
| 149 | PyErr_SetString(PyExc_ValueError, |
| 150 | "Negative filedescriptor"); |
| 151 | return -1; |
| 152 | } |
| 153 | } |
| 154 | else { |
| 155 | PyErr_Clear(); |
| 156 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 157 | #ifdef Py_WIN_WIDE_FILENAMES |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 158 | if (GetVersion() < 0x80000000) { |
| 159 | /* On NT, so wide API available */ |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 160 | PyObject *po; |
| 161 | if (PyArg_ParseTupleAndKeywords(args, kwds, "U|s:fileio", |
| 162 | kwlist, &po, &mode)) { |
| 163 | wideargument = 1; |
| 164 | } else { |
| 165 | /* Drop the argument parsing error as narrow |
| 166 | strings are also valid. */ |
| 167 | PyErr_Clear(); |
| 168 | } |
| 169 | |
| 170 | PyErr_SetString(PyExc_NotImplementedError, |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 171 | "Windows wide filenames are not yet supported"); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 172 | goto error; |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 173 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 174 | #endif |
| 175 | |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 176 | if (!wideargument) { |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 177 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|s:fileio", |
| 178 | kwlist, |
| 179 | Py_FileSystemDefaultEncoding, |
| 180 | &name, &mode)) |
| 181 | goto error; |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 182 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | self->readable = self->writable = 0; |
| 186 | s = mode; |
| 187 | while (*s) { |
| 188 | switch (*s++) { |
| 189 | case 'r': |
| 190 | if (rwa) { |
| 191 | bad_mode: |
| 192 | PyErr_SetString(PyExc_ValueError, |
| 193 | "Must have exactly one of read/write/append mode"); |
| 194 | goto error; |
| 195 | } |
| 196 | rwa = 1; |
| 197 | self->readable = 1; |
| 198 | break; |
| 199 | case 'w': |
| 200 | if (rwa) |
| 201 | goto bad_mode; |
| 202 | rwa = 1; |
| 203 | self->writable = 1; |
| 204 | flags |= O_CREAT | O_TRUNC; |
| 205 | break; |
| 206 | case 'a': |
| 207 | if (rwa) |
| 208 | goto bad_mode; |
| 209 | rwa = 1; |
| 210 | self->writable = 1; |
| 211 | flags |= O_CREAT; |
| 212 | append = 1; |
| 213 | break; |
| 214 | case '+': |
| 215 | if (plus) |
| 216 | goto bad_mode; |
| 217 | self->readable = self->writable = 1; |
| 218 | plus = 1; |
| 219 | break; |
| 220 | default: |
| 221 | PyErr_Format(PyExc_ValueError, |
| 222 | "invalid mode: %.200s", mode); |
| 223 | goto error; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | if (!rwa) |
| 228 | goto bad_mode; |
| 229 | |
| 230 | if (self->readable && self->writable) |
| 231 | flags |= O_RDWR; |
| 232 | else if (self->readable) |
| 233 | flags |= O_RDONLY; |
| 234 | else |
| 235 | flags |= O_WRONLY; |
| 236 | |
| 237 | #ifdef O_BINARY |
| 238 | flags |= O_BINARY; |
| 239 | #endif |
| 240 | |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 241 | if (fd >= 0) { |
| 242 | self->fd = fd; |
| 243 | /* XXX Should we set self->own_fd = 0 ??? */ |
| 244 | } |
| 245 | else { |
| 246 | Py_BEGIN_ALLOW_THREADS |
| 247 | errno = 0; |
| 248 | self->fd = open(name, flags, 0666); |
| 249 | Py_END_ALLOW_THREADS |
| 250 | if (self->fd < 0 || dircheck(self) < 0) { |
| 251 | PyErr_SetFromErrnoWithFilename(PyExc_IOError, name); |
| 252 | goto error; |
| 253 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | goto done; |
| 257 | |
| 258 | error: |
| 259 | ret = -1; |
| 260 | |
| 261 | done: |
| 262 | PyMem_Free(name); |
| 263 | return ret; |
| 264 | } |
| 265 | |
| 266 | static void |
| 267 | fileio_dealloc(PyFileIOObject *self) |
| 268 | { |
| 269 | if (self->weakreflist != NULL) |
| 270 | PyObject_ClearWeakRefs((PyObject *) self); |
| 271 | |
| 272 | if (self->fd >= 0 && self->own_fd) { |
| 273 | PyObject *closeresult = fileio_close(self); |
| 274 | if (closeresult == NULL) { |
| 275 | #ifdef HAVE_STRERROR |
| 276 | PySys_WriteStderr("close failed: [Errno %d] %s\n", errno, strerror(errno)); |
| 277 | #else |
| 278 | PySys_WriteStderr("close failed: [Errno %d]\n", errno); |
| 279 | #endif |
| 280 | } else |
| 281 | Py_DECREF(closeresult); |
| 282 | } |
| 283 | |
| 284 | self->ob_type->tp_free((PyObject *)self); |
| 285 | } |
| 286 | |
| 287 | static PyObject * |
| 288 | err_closed(void) |
| 289 | { |
| 290 | PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); |
| 291 | return NULL; |
| 292 | } |
| 293 | |
| 294 | static PyObject * |
| 295 | fileio_fileno(PyFileIOObject *self) |
| 296 | { |
| 297 | if (self->fd < 0) |
| 298 | return err_closed(); |
| 299 | return PyInt_FromLong((long) self->fd); |
| 300 | } |
| 301 | |
| 302 | static PyObject * |
| 303 | fileio_readable(PyFileIOObject *self) |
| 304 | { |
| 305 | if (self->fd < 0) |
| 306 | return err_closed(); |
| 307 | return PyInt_FromLong((long) self->readable); |
| 308 | } |
| 309 | |
| 310 | static PyObject * |
| 311 | fileio_writable(PyFileIOObject *self) |
| 312 | { |
| 313 | if (self->fd < 0) |
| 314 | return err_closed(); |
| 315 | return PyInt_FromLong((long) self->writable); |
| 316 | } |
| 317 | |
| 318 | static PyObject * |
| 319 | fileio_seekable(PyFileIOObject *self) |
| 320 | { |
| 321 | if (self->fd < 0) |
| 322 | return err_closed(); |
| 323 | if (self->seekable < 0) { |
| 324 | int ret; |
| 325 | Py_BEGIN_ALLOW_THREADS |
| 326 | ret = lseek(self->fd, 0, SEEK_CUR); |
| 327 | Py_END_ALLOW_THREADS |
| 328 | if (ret < 0) |
| 329 | self->seekable = 0; |
| 330 | else |
| 331 | self->seekable = 1; |
| 332 | } |
| 333 | return PyInt_FromLong((long) self->seekable); |
| 334 | } |
| 335 | |
| 336 | static PyObject * |
| 337 | fileio_readinto(PyFileIOObject *self, PyObject *args) |
| 338 | { |
| 339 | char *ptr; |
| 340 | Py_ssize_t n; |
| 341 | |
| 342 | if (self->fd < 0) |
| 343 | return err_closed(); |
| 344 | if (!PyArg_ParseTuple(args, "w#", &ptr, &n)) |
| 345 | return NULL; |
| 346 | |
| 347 | Py_BEGIN_ALLOW_THREADS |
| 348 | errno = 0; |
| 349 | n = read(self->fd, ptr, n); |
| 350 | Py_END_ALLOW_THREADS |
| 351 | if (n < 0) { |
| 352 | if (errno == EAGAIN) |
| 353 | Py_RETURN_NONE; |
| 354 | PyErr_SetFromErrno(PyExc_IOError); |
| 355 | return NULL; |
| 356 | } |
| 357 | |
| 358 | return PyInt_FromLong(n); |
| 359 | } |
| 360 | |
| 361 | static PyObject * |
| 362 | fileio_read(PyFileIOObject *self, PyObject *args) |
| 363 | { |
| 364 | char *ptr; |
| 365 | Py_ssize_t n, size; |
| 366 | PyObject *bytes; |
| 367 | |
| 368 | if (self->fd < 0) |
| 369 | return err_closed(); |
| 370 | |
| 371 | if (!PyArg_ParseTuple(args, "i", &size)) |
| 372 | return NULL; |
| 373 | |
| 374 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 375 | if (bytes == NULL) |
| 376 | return NULL; |
| 377 | ptr = PyBytes_AsString(bytes); |
| 378 | |
| 379 | Py_BEGIN_ALLOW_THREADS |
| 380 | errno = 0; |
| 381 | n = read(self->fd, ptr, size); |
| 382 | Py_END_ALLOW_THREADS |
| 383 | |
| 384 | if (n < 0) { |
| 385 | if (errno == EAGAIN) |
| 386 | Py_RETURN_NONE; |
| 387 | PyErr_SetFromErrno(PyExc_IOError); |
| 388 | return NULL; |
| 389 | } |
| 390 | |
| 391 | if (n != size) { |
| 392 | if (PyBytes_Resize(bytes, n) < 0) { |
| 393 | Py_DECREF(bytes); |
| 394 | return NULL; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | return (PyObject *) bytes; |
| 399 | } |
| 400 | |
| 401 | static PyObject * |
| 402 | fileio_write(PyFileIOObject *self, PyObject *args) |
| 403 | { |
| 404 | Py_ssize_t n; |
| 405 | char *ptr; |
| 406 | |
| 407 | if (self->fd < 0) |
| 408 | return err_closed(); |
| 409 | if (!PyArg_ParseTuple(args, "s#", &ptr, &n)) |
| 410 | return NULL; |
| 411 | |
| 412 | Py_BEGIN_ALLOW_THREADS |
| 413 | errno = 0; |
| 414 | n = write(self->fd, ptr, n); |
| 415 | Py_END_ALLOW_THREADS |
| 416 | |
| 417 | if (n < 0) { |
| 418 | if (errno == EAGAIN) |
| 419 | Py_RETURN_NONE; |
| 420 | PyErr_SetFromErrno(PyExc_IOError); |
| 421 | return NULL; |
| 422 | } |
| 423 | |
| 424 | return PyInt_FromLong(n); |
| 425 | } |
| 426 | |
| 427 | static PyObject * |
| 428 | fileio_seek(PyFileIOObject *self, PyObject *args) |
| 429 | { |
| 430 | Py_ssize_t offset; |
| 431 | Py_ssize_t whence = 0; |
| 432 | |
| 433 | if (self->fd < 0) |
| 434 | return err_closed(); |
| 435 | |
| 436 | if (!PyArg_ParseTuple(args, "i|i", &offset, &whence)) |
| 437 | return NULL; |
| 438 | |
| 439 | Py_BEGIN_ALLOW_THREADS |
| 440 | errno = 0; |
| 441 | offset = lseek(self->fd, offset, whence); |
| 442 | Py_END_ALLOW_THREADS |
| 443 | |
| 444 | if (offset < 0) { |
| 445 | PyErr_SetFromErrno(PyExc_IOError); |
| 446 | return NULL; |
| 447 | } |
| 448 | |
| 449 | Py_RETURN_NONE; |
| 450 | } |
| 451 | |
| 452 | static PyObject * |
| 453 | fileio_tell(PyFileIOObject *self, PyObject *args) |
| 454 | { |
| 455 | Py_ssize_t offset; |
| 456 | |
| 457 | if (self->fd < 0) |
| 458 | return err_closed(); |
| 459 | |
| 460 | Py_BEGIN_ALLOW_THREADS |
| 461 | errno = 0; |
| 462 | offset = lseek(self->fd, 0, SEEK_CUR); |
| 463 | Py_END_ALLOW_THREADS |
| 464 | |
| 465 | if (offset < 0) { |
| 466 | PyErr_SetFromErrno(PyExc_IOError); |
| 467 | return NULL; |
| 468 | } |
| 469 | |
| 470 | return PyInt_FromLong(offset); |
| 471 | } |
| 472 | |
| 473 | #ifdef HAVE_FTRUNCATE |
| 474 | static PyObject * |
| 475 | fileio_truncate(PyFileIOObject *self, PyObject *args) |
| 476 | { |
| 477 | Py_ssize_t length; |
| 478 | int ret; |
| 479 | |
| 480 | if (self->fd < 0) |
| 481 | return err_closed(); |
| 482 | |
| 483 | /* Setup default value */ |
| 484 | Py_BEGIN_ALLOW_THREADS |
| 485 | errno = 0; |
| 486 | length = lseek(self->fd, 0, SEEK_CUR); |
| 487 | Py_END_ALLOW_THREADS |
| 488 | |
| 489 | if (length < 0) { |
| 490 | PyErr_SetFromErrno(PyExc_IOError); |
| 491 | return NULL; |
| 492 | } |
| 493 | |
| 494 | if (!PyArg_ParseTuple(args, "|i", &length)) |
| 495 | return NULL; |
| 496 | |
| 497 | #ifdef MS_WINDOWS |
| 498 | /* MS _chsize doesn't work if newsize doesn't fit in 32 bits, |
| 499 | so don't even try using it. */ |
| 500 | { |
| 501 | HANDLE hFile; |
| 502 | Py_ssize_t initialpos; |
| 503 | |
| 504 | /* Have to move current pos to desired endpoint on Windows. */ |
| 505 | Py_BEGIN_ALLOW_THREADS |
| 506 | errno = 0; |
| 507 | ret = _portable_fseek(f->f_fp, newsize, SEEK_SET) != 0; |
| 508 | Py_END_ALLOW_THREADS |
| 509 | if (ret) |
| 510 | goto onioerror; |
| 511 | |
| 512 | /* Truncate. Note that this may grow the file! */ |
| 513 | Py_BEGIN_ALLOW_THREADS |
| 514 | errno = 0; |
| 515 | hFile = (HANDLE)_get_osfhandle(fileno(f->f_fp)); |
| 516 | ret = hFile == (HANDLE)-1; |
| 517 | if (ret == 0) { |
| 518 | ret = SetEndOfFile(hFile) == 0; |
| 519 | if (ret) |
| 520 | errno = EACCES; |
| 521 | } |
| 522 | Py_END_ALLOW_THREADS |
| 523 | if (ret) |
| 524 | goto onioerror; |
| 525 | } |
| 526 | #else |
| 527 | Py_BEGIN_ALLOW_THREADS |
| 528 | errno = 0; |
| 529 | ret = ftruncate(self->fd, length); |
| 530 | Py_END_ALLOW_THREADS |
| 531 | #endif /* !MS_WINDOWS */ |
| 532 | |
| 533 | if (ret < 0) { |
| 534 | onioerror: |
| 535 | PyErr_SetFromErrno(PyExc_IOError); |
| 536 | return NULL; |
| 537 | } |
| 538 | |
| 539 | /* Return to initial position */ |
| 540 | Py_BEGIN_ALLOW_THREADS |
| 541 | errno = 0; |
| 542 | ret = lseek(self->fd, length, SEEK_SET); |
| 543 | Py_END_ALLOW_THREADS |
| 544 | if (ret < 0) |
| 545 | goto onioerror; |
| 546 | |
| 547 | Py_RETURN_NONE; |
| 548 | } |
| 549 | #endif |
| 550 | |
| 551 | static PyObject * |
| 552 | fileio_repr(PyFileIOObject *self) |
| 553 | { |
| 554 | PyObject *ret = NULL; |
| 555 | |
| 556 | ret = PyString_FromFormat("<%s file at %p>", |
| 557 | self->fd < 0 ? "closed" : "open", |
| 558 | self); |
| 559 | return ret; |
| 560 | } |
| 561 | |
| 562 | static PyObject * |
| 563 | fileio_isatty(PyFileIOObject *self) |
| 564 | { |
| 565 | long res; |
| 566 | |
| 567 | if (self->fd < 0) |
| 568 | return err_closed(); |
| 569 | Py_BEGIN_ALLOW_THREADS |
| 570 | res = isatty(self->fd); |
| 571 | Py_END_ALLOW_THREADS |
| 572 | return PyBool_FromLong(res); |
| 573 | } |
| 574 | |
| 575 | static PyObject * |
| 576 | fileio_self(PyFileIOObject *self) |
| 577 | { |
| 578 | if (self->fd < 0) |
| 579 | return err_closed(); |
| 580 | Py_INCREF(self); |
| 581 | return (PyObject *)self; |
| 582 | } |
| 583 | |
| 584 | PyDoc_STRVAR(fileio_doc, |
| 585 | "file(name: str[, mode: str]) -> file IO object\n" |
| 586 | "\n" |
| 587 | "Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n" |
| 588 | "writing or appending. The file will be created if it doesn't exist\n" |
| 589 | "when opened for writing or appending; it will be truncated when\n" |
| 590 | "opened for writing. Add a '+' to the mode to allow simultaneous\n" |
| 591 | "reading and writing."); |
| 592 | |
| 593 | PyDoc_STRVAR(read_doc, |
| 594 | "read(size: int) -> bytes. read at most size bytes, returned as bytes.\n" |
| 595 | "\n" |
| 596 | "Only makes one system call, so less data may be returned than requested\n" |
| 597 | "In non-blocking mode, returns None if no data is available. On\n" |
| 598 | "end-of-file, returns 0."); |
| 599 | |
| 600 | PyDoc_STRVAR(write_doc, |
| 601 | "write(b: bytes) -> int. Write bytes b to file, return number written.\n" |
| 602 | "\n" |
| 603 | "Only makes one system call, so not all of the data may be written.\n" |
| 604 | "The number of bytes actually written is returned."); |
| 605 | |
| 606 | PyDoc_STRVAR(fileno_doc, |
| 607 | "fileno() -> int. \"file descriptor\".\n" |
| 608 | "\n" |
| 609 | "This is needed for lower-level file interfaces, such the fcntl module."); |
| 610 | |
| 611 | PyDoc_STRVAR(seek_doc, |
| 612 | "seek(offset: int[, whence: int]) -> None. Move to new file position.\n" |
| 613 | "\n" |
| 614 | "Argument offset is a byte count. Optional argument whence defaults to\n" |
| 615 | "0 (offset from start of file, offset should be >= 0); other values are 1\n" |
| 616 | "(move relative to current position, positive or negative), and 2 (move\n" |
| 617 | "relative to end of file, usually negative, although many platforms allow\n" |
| 618 | "seeking beyond the end of a file)." |
| 619 | "\n" |
| 620 | "Note that not all file objects are seekable."); |
| 621 | |
| 622 | PyDoc_STRVAR(truncate_doc, |
| 623 | "truncate([size: int]) -> None. Truncate the file to at most size bytes.\n" |
| 624 | "\n" |
| 625 | "Size defaults to the current file position, as returned by tell()."); |
| 626 | |
| 627 | PyDoc_STRVAR(tell_doc, |
| 628 | "tell() -> int. Current file position"); |
| 629 | |
| 630 | PyDoc_STRVAR(readinto_doc, |
| 631 | "readinto() -> Undocumented. Don't use this; it may go away."); |
| 632 | |
| 633 | PyDoc_STRVAR(close_doc, |
| 634 | "close() -> None. Close the file.\n" |
| 635 | "\n" |
| 636 | "A closed file cannot be used for further I/O operations. close() may be\n" |
| 637 | "called more than once without error. Changes the fileno to -1."); |
| 638 | |
| 639 | PyDoc_STRVAR(isatty_doc, |
| 640 | "isatty() -> bool. True if the file is connected to a tty device."); |
| 641 | |
| 642 | PyDoc_STRVAR(enter_doc, |
| 643 | "__enter__() -> self."); |
| 644 | |
| 645 | PyDoc_STRVAR(exit_doc, |
| 646 | "__exit__(*excinfo) -> None. Closes the file."); |
| 647 | |
| 648 | PyDoc_STRVAR(seekable_doc, |
| 649 | "seekable() -> bool. True if file supports random-access."); |
| 650 | |
| 651 | PyDoc_STRVAR(readable_doc, |
| 652 | "readable() -> bool. True if file was opened in a read mode."); |
| 653 | |
| 654 | PyDoc_STRVAR(writable_doc, |
| 655 | "writable() -> bool. True if file was opened in a write mode."); |
| 656 | |
| 657 | static PyMethodDef fileio_methods[] = { |
| 658 | {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc}, |
| 659 | {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc}, |
| 660 | {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc}, |
| 661 | {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc}, |
| 662 | {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc}, |
| 663 | {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc}, |
| 664 | {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc}, |
| 665 | {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc}, |
| 666 | {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc}, |
| 667 | {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc}, |
| 668 | {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc}, |
| 669 | {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc}, |
| 670 | {"__enter__",(PyCFunction)fileio_self, METH_NOARGS, enter_doc}, |
| 671 | {"__exit__", (PyCFunction)fileio_close, METH_VARARGS, exit_doc}, |
| 672 | {NULL, NULL} /* sentinel */ |
| 673 | }; |
| 674 | |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 675 | /* 'closed' is an attribute for backwards compatibility reasons. */ |
| 676 | static PyObject * |
| 677 | get_closed(PyFileIOObject *f, void *closure) |
| 678 | { |
| 679 | return PyBool_FromLong((long)(f->fd < 0)); |
| 680 | } |
| 681 | |
| 682 | static PyGetSetDef fileio_getsetlist[] = { |
| 683 | {"closed", (getter)get_closed, NULL, "True if the file is closed"}, |
| 684 | {0}, |
| 685 | }; |
| 686 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 687 | PyTypeObject PyFileIO_Type = { |
| 688 | PyObject_HEAD_INIT(&PyType_Type) |
| 689 | 0, |
| 690 | "FileIO", |
| 691 | sizeof(PyFileIOObject), |
| 692 | 0, |
| 693 | (destructor)fileio_dealloc, /* tp_dealloc */ |
| 694 | 0, /* tp_print */ |
| 695 | 0, /* tp_getattr */ |
| 696 | 0, /* tp_setattr */ |
| 697 | 0, /* tp_compare */ |
| 698 | (reprfunc)fileio_repr, /* tp_repr */ |
| 699 | 0, /* tp_as_number */ |
| 700 | 0, /* tp_as_sequence */ |
| 701 | 0, /* tp_as_mapping */ |
| 702 | 0, /* tp_hash */ |
| 703 | 0, /* tp_call */ |
| 704 | 0, /* tp_str */ |
| 705 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 706 | 0, /* tp_setattro */ |
| 707 | 0, /* tp_as_buffer */ |
| 708 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 709 | fileio_doc, /* tp_doc */ |
| 710 | 0, /* tp_traverse */ |
| 711 | 0, /* tp_clear */ |
| 712 | 0, /* tp_richcompare */ |
| 713 | offsetof(PyFileIOObject, weakreflist), /* tp_weaklistoffset */ |
| 714 | 0, /* tp_iter */ |
| 715 | 0, /* tp_iternext */ |
| 716 | fileio_methods, /* tp_methods */ |
| 717 | 0, /* tp_members */ |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 718 | fileio_getsetlist, /* tp_getset */ |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 719 | 0, /* tp_base */ |
| 720 | 0, /* tp_dict */ |
| 721 | 0, /* tp_descr_get */ |
| 722 | 0, /* tp_descr_set */ |
| 723 | 0, /* tp_dictoffset */ |
| 724 | fileio_init, /* tp_init */ |
| 725 | PyType_GenericAlloc, /* tp_alloc */ |
| 726 | fileio_new, /* tp_new */ |
| 727 | PyObject_Del, /* tp_free */ |
| 728 | }; |
| 729 | |
| 730 | static PyMethodDef module_methods[] = { |
| 731 | {NULL, NULL} |
| 732 | }; |
| 733 | |
| 734 | PyMODINIT_FUNC |
| 735 | init_fileio(void) |
| 736 | { |
| 737 | PyObject *m; /* a module object */ |
| 738 | |
| 739 | m = Py_InitModule3("_fileio", module_methods, |
| 740 | "Fast implementation of io.FileIO."); |
| 741 | if (m == NULL) |
| 742 | return; |
| 743 | if (PyType_Ready(&PyFileIO_Type) < 0) |
| 744 | return; |
| 745 | Py_INCREF(&PyFileIO_Type); |
| 746 | PyModule_AddObject(m, "_FileIO", (PyObject *) &PyFileIO_Type); |
| 747 | } |