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