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 */ |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 25 | #define HAVE_FTRUNCATE |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 26 | #define WIN32_LEAN_AND_MEAN |
| 27 | #include <windows.h> |
| 28 | #endif |
| 29 | |
Christian Heimes | a872de5 | 2008-12-05 08:26:55 +0000 | [diff] [blame] | 30 | #if BUFSIZ < (8*1024) |
| 31 | #define SMALLCHUNK (8*1024) |
| 32 | #elif (BUFSIZ >= (2 << 25)) |
| 33 | #error "unreasonable BUFSIZ > 64MB defined" |
| 34 | #else |
| 35 | #define SMALLCHUNK BUFSIZ |
| 36 | #endif |
| 37 | |
| 38 | #if SIZEOF_INT < 4 |
| 39 | #define BIGCHUNK (512 * 32) |
| 40 | #else |
| 41 | #define BIGCHUNK (512 * 1024) |
| 42 | #endif |
| 43 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 44 | typedef struct { |
| 45 | PyObject_HEAD |
| 46 | int fd; |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 47 | unsigned readable : 1; |
| 48 | unsigned writable : 1; |
| 49 | int seekable : 2; /* -1 means unknown */ |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 50 | int closefd : 1; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 51 | PyObject *weakreflist; |
| 52 | } PyFileIOObject; |
| 53 | |
Collin Winter | af33438 | 2007-03-08 21:46:15 +0000 | [diff] [blame] | 54 | PyTypeObject PyFileIO_Type; |
| 55 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 56 | #define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type)) |
| 57 | |
Antoine Pitrou | 08838b6 | 2009-01-21 00:55:13 +0000 | [diff] [blame^] | 58 | static PyObject * |
| 59 | portable_lseek(int fd, PyObject *posobj, int whence); |
| 60 | |
Kristján Valur Jónsson | 19288c2 | 2008-12-18 17:15:54 +0000 | [diff] [blame] | 61 | /* Returns 0 on success, -1 with exception set on failure. */ |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 62 | static int |
| 63 | internal_close(PyFileIOObject *self) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 64 | { |
Kristján Valur Jónsson | 19288c2 | 2008-12-18 17:15:54 +0000 | [diff] [blame] | 65 | int err = 0; |
Benjamin Peterson | 7aedf11 | 2009-01-19 15:19:46 +0000 | [diff] [blame] | 66 | int save_errno = 0; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 67 | if (self->fd >= 0) { |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 68 | int fd = self->fd; |
| 69 | self->fd = -1; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 70 | Py_BEGIN_ALLOW_THREADS |
Kristján Valur Jónsson | 19288c2 | 2008-12-18 17:15:54 +0000 | [diff] [blame] | 71 | err = close(fd); |
| 72 | if (err < 0) |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 73 | save_errno = errno; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 74 | Py_END_ALLOW_THREADS |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 75 | } |
Kristján Valur Jónsson | 19288c2 | 2008-12-18 17:15:54 +0000 | [diff] [blame] | 76 | if (err < 0) { |
| 77 | errno = save_errno; |
| 78 | PyErr_SetFromErrno(PyExc_IOError); |
| 79 | return -1; |
| 80 | } |
| 81 | return 0; |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static PyObject * |
| 85 | fileio_close(PyFileIOObject *self) |
| 86 | { |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 87 | if (!self->closefd) { |
Christian Heimes | ecc42a2 | 2008-11-05 19:30:32 +0000 | [diff] [blame] | 88 | self->fd = -1; |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 89 | Py_RETURN_NONE; |
| 90 | } |
Kristján Valur Jónsson | 19288c2 | 2008-12-18 17:15:54 +0000 | [diff] [blame] | 91 | if (internal_close(self)) |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 92 | return NULL; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 93 | |
| 94 | Py_RETURN_NONE; |
| 95 | } |
| 96 | |
| 97 | static PyObject * |
| 98 | fileio_new(PyTypeObject *type, PyObject *args, PyObject *kews) |
| 99 | { |
| 100 | PyFileIOObject *self; |
| 101 | |
| 102 | assert(type != NULL && type->tp_alloc != NULL); |
| 103 | |
| 104 | self = (PyFileIOObject *) type->tp_alloc(type, 0); |
| 105 | if (self != NULL) { |
| 106 | self->fd = -1; |
Christian Heimes | df32b39 | 2008-10-30 21:23:35 +0000 | [diff] [blame] | 107 | self->readable = 0; |
| 108 | self->writable = 0; |
| 109 | self->seekable = -1; |
| 110 | self->closefd = 1; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 111 | self->weakreflist = NULL; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | return (PyObject *) self; |
| 115 | } |
| 116 | |
| 117 | /* On Unix, open will succeed for directories. |
| 118 | In Python, there should be no file objects referring to |
| 119 | directories, so we need a check. */ |
| 120 | |
| 121 | static int |
Benjamin Peterson | 1efc23c | 2008-12-29 18:02:28 +0000 | [diff] [blame] | 122 | dircheck(PyFileIOObject* self, char *name) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 123 | { |
| 124 | #if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR) |
| 125 | struct stat buf; |
| 126 | if (self->fd < 0) |
| 127 | return 0; |
| 128 | if (fstat(self->fd, &buf) == 0 && S_ISDIR(buf.st_mode)) { |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 129 | char *msg = strerror(EISDIR); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 130 | PyObject *exc; |
Kristján Valur Jónsson | 19288c2 | 2008-12-18 17:15:54 +0000 | [diff] [blame] | 131 | if (internal_close(self)) |
| 132 | return -1; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 133 | |
Benjamin Peterson | 1efc23c | 2008-12-29 18:02:28 +0000 | [diff] [blame] | 134 | exc = PyObject_CallFunction(PyExc_IOError, "(iss)", |
| 135 | EISDIR, msg, name); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 136 | PyErr_SetObject(PyExc_IOError, exc); |
| 137 | Py_XDECREF(exc); |
| 138 | return -1; |
| 139 | } |
| 140 | #endif |
| 141 | return 0; |
| 142 | } |
| 143 | |
Benjamin Peterson | 806d402 | 2009-01-19 15:11:51 +0000 | [diff] [blame] | 144 | static int |
| 145 | check_fd(int fd) |
| 146 | { |
| 147 | #if defined(HAVE_FSTAT) |
| 148 | struct stat buf; |
| 149 | if (fstat(fd, &buf) < 0 && errno == EBADF) { |
| 150 | PyObject *exc; |
| 151 | char *msg = strerror(EBADF); |
| 152 | exc = PyObject_CallFunction(PyExc_OSError, "(is)", |
| 153 | EBADF, msg); |
| 154 | PyErr_SetObject(PyExc_OSError, exc); |
| 155 | Py_XDECREF(exc); |
| 156 | return -1; |
| 157 | } |
| 158 | #endif |
| 159 | return 0; |
| 160 | } |
| 161 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 162 | |
| 163 | static int |
| 164 | fileio_init(PyObject *oself, PyObject *args, PyObject *kwds) |
| 165 | { |
| 166 | PyFileIOObject *self = (PyFileIOObject *) oself; |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 167 | static char *kwlist[] = {"file", "mode", "closefd", NULL}; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 168 | char *name = NULL; |
| 169 | char *mode = "r"; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 170 | char *s; |
Thomas Heller | af2be26 | 2007-07-12 11:03:13 +0000 | [diff] [blame] | 171 | #ifdef MS_WINDOWS |
| 172 | Py_UNICODE *widename = NULL; |
| 173 | #endif |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 174 | int ret = 0; |
| 175 | int rwa = 0, plus = 0, append = 0; |
| 176 | int flags = 0; |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 177 | int fd = -1; |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 178 | int closefd = 1; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 179 | |
| 180 | assert(PyFileIO_Check(oself)); |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 181 | if (self->fd >= 0) { |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 182 | /* Have to close the existing file first. */ |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 183 | if (internal_close(self) < 0) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 184 | return -1; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 187 | if (PyArg_ParseTupleAndKeywords(args, kwds, "i|si:fileio", |
| 188 | kwlist, &fd, &mode, &closefd)) { |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 189 | if (fd < 0) { |
| 190 | PyErr_SetString(PyExc_ValueError, |
| 191 | "Negative filedescriptor"); |
| 192 | return -1; |
| 193 | } |
Benjamin Peterson | 806d402 | 2009-01-19 15:11:51 +0000 | [diff] [blame] | 194 | if (check_fd(fd)) |
| 195 | return -1; |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 196 | } |
| 197 | else { |
| 198 | PyErr_Clear(); |
| 199 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 200 | #ifdef Py_WIN_WIDE_FILENAMES |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 201 | if (GetVersion() < 0x80000000) { |
| 202 | /* On NT, so wide API available */ |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 203 | PyObject *po; |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 204 | if (PyArg_ParseTupleAndKeywords(args, kwds, "U|si:fileio", |
| 205 | kwlist, &po, &mode, &closefd) |
| 206 | ) { |
Thomas Heller | af2be26 | 2007-07-12 11:03:13 +0000 | [diff] [blame] | 207 | widename = PyUnicode_AS_UNICODE(po); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 208 | } else { |
| 209 | /* Drop the argument parsing error as narrow |
| 210 | strings are also valid. */ |
| 211 | PyErr_Clear(); |
| 212 | } |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 213 | } |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 214 | if (widename == NULL) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 215 | #endif |
Thomas Heller | af2be26 | 2007-07-12 11:03:13 +0000 | [diff] [blame] | 216 | { |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 217 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:fileio", |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 218 | kwlist, |
| 219 | Py_FileSystemDefaultEncoding, |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 220 | &name, &mode, &closefd)) |
Neal Norwitz | 6e0e0e6 | 2008-08-24 22:07:28 +0000 | [diff] [blame] | 221 | return -1; |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 222 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 225 | s = mode; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 226 | while (*s) { |
| 227 | switch (*s++) { |
| 228 | case 'r': |
| 229 | if (rwa) { |
| 230 | bad_mode: |
| 231 | PyErr_SetString(PyExc_ValueError, |
| 232 | "Must have exactly one of read/write/append mode"); |
| 233 | goto error; |
| 234 | } |
| 235 | rwa = 1; |
| 236 | self->readable = 1; |
| 237 | break; |
| 238 | case 'w': |
| 239 | if (rwa) |
| 240 | goto bad_mode; |
| 241 | rwa = 1; |
| 242 | self->writable = 1; |
| 243 | flags |= O_CREAT | O_TRUNC; |
| 244 | break; |
| 245 | case 'a': |
| 246 | if (rwa) |
| 247 | goto bad_mode; |
| 248 | rwa = 1; |
| 249 | self->writable = 1; |
| 250 | flags |= O_CREAT; |
| 251 | append = 1; |
| 252 | break; |
Benjamin Peterson | 44309e6 | 2008-11-22 00:41:45 +0000 | [diff] [blame] | 253 | case 'b': |
| 254 | break; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 255 | case '+': |
| 256 | if (plus) |
| 257 | goto bad_mode; |
| 258 | self->readable = self->writable = 1; |
| 259 | plus = 1; |
| 260 | break; |
| 261 | default: |
| 262 | PyErr_Format(PyExc_ValueError, |
| 263 | "invalid mode: %.200s", mode); |
| 264 | goto error; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | if (!rwa) |
| 269 | goto bad_mode; |
| 270 | |
| 271 | if (self->readable && self->writable) |
| 272 | flags |= O_RDWR; |
| 273 | else if (self->readable) |
| 274 | flags |= O_RDONLY; |
| 275 | else |
| 276 | flags |= O_WRONLY; |
| 277 | |
| 278 | #ifdef O_BINARY |
| 279 | flags |= O_BINARY; |
| 280 | #endif |
| 281 | |
Walter Dörwald | 0e41148 | 2007-06-06 16:55:38 +0000 | [diff] [blame] | 282 | #ifdef O_APPEND |
| 283 | if (append) |
| 284 | flags |= O_APPEND; |
| 285 | #endif |
| 286 | |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 287 | if (fd >= 0) { |
| 288 | self->fd = fd; |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 289 | self->closefd = closefd; |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 290 | } |
| 291 | else { |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 292 | self->closefd = 1; |
| 293 | if (!closefd) { |
| 294 | PyErr_SetString(PyExc_ValueError, |
Benjamin Peterson | c0747cf | 2008-11-03 20:31:38 +0000 | [diff] [blame] | 295 | "Cannot use closefd=False with file name"); |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 296 | goto error; |
| 297 | } |
| 298 | |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 299 | Py_BEGIN_ALLOW_THREADS |
| 300 | errno = 0; |
Thomas Heller | af2be26 | 2007-07-12 11:03:13 +0000 | [diff] [blame] | 301 | #ifdef MS_WINDOWS |
| 302 | if (widename != NULL) |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 303 | self->fd = _wopen(widename, flags, 0666); |
Thomas Heller | af2be26 | 2007-07-12 11:03:13 +0000 | [diff] [blame] | 304 | else |
| 305 | #endif |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 306 | self->fd = open(name, flags, 0666); |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 307 | Py_END_ALLOW_THREADS |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 308 | if (self->fd < 0) { |
Christian Heimes | 0b48954 | 2007-10-31 19:20:48 +0000 | [diff] [blame] | 309 | #ifdef MS_WINDOWS |
Hirokazu Yamamoto | 0f22d69 | 2009-01-01 16:03:45 +0000 | [diff] [blame] | 310 | if (widename != NULL) |
| 311 | PyErr_SetFromErrnoWithUnicodeFilename(PyExc_IOError, widename); |
| 312 | else |
Christian Heimes | 0b48954 | 2007-10-31 19:20:48 +0000 | [diff] [blame] | 313 | #endif |
Hirokazu Yamamoto | 0f22d69 | 2009-01-01 16:03:45 +0000 | [diff] [blame] | 314 | PyErr_SetFromErrnoWithFilename(PyExc_IOError, name); |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 315 | goto error; |
| 316 | } |
Benjamin Peterson | 1efc23c | 2008-12-29 18:02:28 +0000 | [diff] [blame] | 317 | if(dircheck(self, name) < 0) |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 318 | goto error; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Antoine Pitrou | 08838b6 | 2009-01-21 00:55:13 +0000 | [diff] [blame^] | 321 | if (append) { |
| 322 | /* For consistent behaviour, we explicitly seek to the |
| 323 | end of file (otherwise, it might be done only on the |
| 324 | first write()). */ |
| 325 | PyObject *pos = portable_lseek(self->fd, NULL, 2); |
| 326 | if (pos == NULL) |
| 327 | goto error; |
| 328 | Py_DECREF(pos); |
| 329 | } |
| 330 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 331 | goto done; |
| 332 | |
| 333 | error: |
| 334 | ret = -1; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 335 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 336 | done: |
Neal Norwitz | 2f99b24 | 2008-08-24 05:48:10 +0000 | [diff] [blame] | 337 | PyMem_Free(name); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 338 | return ret; |
| 339 | } |
| 340 | |
| 341 | static void |
| 342 | fileio_dealloc(PyFileIOObject *self) |
| 343 | { |
| 344 | if (self->weakreflist != NULL) |
| 345 | PyObject_ClearWeakRefs((PyObject *) self); |
| 346 | |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 347 | if (self->fd >= 0 && self->closefd) { |
Kristján Valur Jónsson | 19288c2 | 2008-12-18 17:15:54 +0000 | [diff] [blame] | 348 | if(internal_close(self)) |
| 349 | PyErr_WriteUnraisable((PyObject*)self); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 352 | Py_TYPE(self)->tp_free((PyObject *)self); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | static PyObject * |
| 356 | err_closed(void) |
| 357 | { |
| 358 | PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); |
| 359 | return NULL; |
| 360 | } |
| 361 | |
| 362 | static PyObject * |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 363 | err_mode(char *action) |
| 364 | { |
| 365 | PyErr_Format(PyExc_ValueError, "File not open for %s", action); |
| 366 | return NULL; |
| 367 | } |
| 368 | |
| 369 | static PyObject * |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 370 | fileio_fileno(PyFileIOObject *self) |
| 371 | { |
| 372 | if (self->fd < 0) |
| 373 | return err_closed(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 374 | return PyLong_FromLong((long) self->fd); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | static PyObject * |
| 378 | fileio_readable(PyFileIOObject *self) |
| 379 | { |
| 380 | if (self->fd < 0) |
| 381 | return err_closed(); |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 382 | return PyBool_FromLong((long) self->readable); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | static PyObject * |
| 386 | fileio_writable(PyFileIOObject *self) |
| 387 | { |
| 388 | if (self->fd < 0) |
| 389 | return err_closed(); |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 390 | return PyBool_FromLong((long) self->writable); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | static PyObject * |
| 394 | fileio_seekable(PyFileIOObject *self) |
| 395 | { |
| 396 | if (self->fd < 0) |
| 397 | return err_closed(); |
| 398 | if (self->seekable < 0) { |
| 399 | int ret; |
| 400 | Py_BEGIN_ALLOW_THREADS |
| 401 | ret = lseek(self->fd, 0, SEEK_CUR); |
| 402 | Py_END_ALLOW_THREADS |
| 403 | if (ret < 0) |
| 404 | self->seekable = 0; |
| 405 | else |
| 406 | self->seekable = 1; |
| 407 | } |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 408 | return PyBool_FromLong((long) self->seekable); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | static PyObject * |
| 412 | fileio_readinto(PyFileIOObject *self, PyObject *args) |
| 413 | { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 414 | Py_buffer pbuf; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 415 | Py_ssize_t n; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 416 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 417 | if (self->fd < 0) |
| 418 | return err_closed(); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 419 | if (!self->readable) |
| 420 | return err_mode("reading"); |
| 421 | |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 422 | if (!PyArg_ParseTuple(args, "w*", &pbuf)) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 423 | return NULL; |
| 424 | |
| 425 | Py_BEGIN_ALLOW_THREADS |
| 426 | errno = 0; |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 427 | n = read(self->fd, pbuf.buf, pbuf.len); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 428 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 429 | PyBuffer_Release(&pbuf); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 430 | if (n < 0) { |
| 431 | if (errno == EAGAIN) |
| 432 | Py_RETURN_NONE; |
| 433 | PyErr_SetFromErrno(PyExc_IOError); |
| 434 | return NULL; |
| 435 | } |
| 436 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 437 | return PyLong_FromSsize_t(n); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 440 | static PyObject * |
| 441 | fileio_readall(PyFileIOObject *self) |
| 442 | { |
| 443 | PyObject *result; |
| 444 | Py_ssize_t total = 0; |
| 445 | int n; |
| 446 | |
Christian Heimes | a872de5 | 2008-12-05 08:26:55 +0000 | [diff] [blame] | 447 | result = PyBytes_FromStringAndSize(NULL, SMALLCHUNK); |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 448 | if (result == NULL) |
| 449 | return NULL; |
| 450 | |
| 451 | while (1) { |
Christian Heimes | a872de5 | 2008-12-05 08:26:55 +0000 | [diff] [blame] | 452 | Py_ssize_t newsize = (total < SMALLCHUNK) ? SMALLCHUNK : total; |
| 453 | |
| 454 | /* Keep doubling until we reach BIGCHUNK; |
| 455 | then keep adding BIGCHUNK. */ |
| 456 | if (newsize <= BIGCHUNK) { |
| 457 | newsize += newsize; |
| 458 | } |
| 459 | else { |
| 460 | /* NOTE: overflow impossible due to limits on BUFSIZ */ |
| 461 | newsize += BIGCHUNK; |
| 462 | } |
| 463 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 464 | if (PyBytes_GET_SIZE(result) < newsize) { |
| 465 | if (_PyBytes_Resize(&result, newsize) < 0) { |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 466 | if (total == 0) { |
| 467 | Py_DECREF(result); |
| 468 | return NULL; |
| 469 | } |
| 470 | PyErr_Clear(); |
| 471 | break; |
| 472 | } |
| 473 | } |
| 474 | Py_BEGIN_ALLOW_THREADS |
| 475 | errno = 0; |
| 476 | n = read(self->fd, |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 477 | PyBytes_AS_STRING(result) + total, |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 478 | newsize - total); |
| 479 | Py_END_ALLOW_THREADS |
| 480 | if (n == 0) |
| 481 | break; |
| 482 | if (n < 0) { |
| 483 | if (total > 0) |
| 484 | break; |
| 485 | if (errno == EAGAIN) { |
| 486 | Py_DECREF(result); |
| 487 | Py_RETURN_NONE; |
| 488 | } |
| 489 | Py_DECREF(result); |
| 490 | PyErr_SetFromErrno(PyExc_IOError); |
| 491 | return NULL; |
| 492 | } |
| 493 | total += n; |
| 494 | } |
| 495 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 496 | if (PyBytes_GET_SIZE(result) > total) { |
| 497 | if (_PyBytes_Resize(&result, total) < 0) { |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 498 | /* This should never happen, but just in case */ |
| 499 | Py_DECREF(result); |
| 500 | return NULL; |
| 501 | } |
| 502 | } |
| 503 | return result; |
| 504 | } |
| 505 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 506 | static PyObject * |
| 507 | fileio_read(PyFileIOObject *self, PyObject *args) |
| 508 | { |
| 509 | char *ptr; |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 510 | Py_ssize_t n; |
| 511 | Py_ssize_t size = -1; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 512 | PyObject *bytes; |
| 513 | |
| 514 | if (self->fd < 0) |
| 515 | return err_closed(); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 516 | if (!self->readable) |
| 517 | return err_mode("reading"); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 518 | |
Neal Norwitz | 3c8ba93 | 2007-08-08 04:36:17 +0000 | [diff] [blame] | 519 | if (!PyArg_ParseTuple(args, "|n", &size)) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 520 | return NULL; |
| 521 | |
Guido van Rossum | c2f93dc | 2007-05-24 00:50:02 +0000 | [diff] [blame] | 522 | if (size < 0) { |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 523 | return fileio_readall(self); |
Guido van Rossum | c2f93dc | 2007-05-24 00:50:02 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 526 | bytes = PyBytes_FromStringAndSize(NULL, size); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 527 | if (bytes == NULL) |
| 528 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 529 | ptr = PyBytes_AS_STRING(bytes); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 530 | |
| 531 | Py_BEGIN_ALLOW_THREADS |
| 532 | errno = 0; |
| 533 | n = read(self->fd, ptr, size); |
| 534 | Py_END_ALLOW_THREADS |
| 535 | |
| 536 | if (n < 0) { |
| 537 | if (errno == EAGAIN) |
| 538 | Py_RETURN_NONE; |
| 539 | PyErr_SetFromErrno(PyExc_IOError); |
| 540 | return NULL; |
| 541 | } |
| 542 | |
| 543 | if (n != size) { |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 544 | if (_PyBytes_Resize(&bytes, n) < 0) { |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 545 | Py_DECREF(bytes); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 546 | return NULL; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 547 | } |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 548 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 549 | |
| 550 | return (PyObject *) bytes; |
| 551 | } |
| 552 | |
| 553 | static PyObject * |
| 554 | fileio_write(PyFileIOObject *self, PyObject *args) |
| 555 | { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 556 | Py_buffer pbuf; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 557 | Py_ssize_t n; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 558 | |
| 559 | if (self->fd < 0) |
| 560 | return err_closed(); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 561 | if (!self->writable) |
| 562 | return err_mode("writing"); |
| 563 | |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 564 | if (!PyArg_ParseTuple(args, "s*", &pbuf)) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 565 | return NULL; |
| 566 | |
| 567 | Py_BEGIN_ALLOW_THREADS |
| 568 | errno = 0; |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 569 | n = write(self->fd, pbuf.buf, pbuf.len); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 570 | Py_END_ALLOW_THREADS |
| 571 | |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 572 | PyBuffer_Release(&pbuf); |
| 573 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 574 | if (n < 0) { |
| 575 | if (errno == EAGAIN) |
| 576 | Py_RETURN_NONE; |
| 577 | PyErr_SetFromErrno(PyExc_IOError); |
| 578 | return NULL; |
| 579 | } |
| 580 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 581 | return PyLong_FromSsize_t(n); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 584 | /* XXX Windows support below is likely incomplete */ |
| 585 | |
| 586 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
| 587 | typedef PY_LONG_LONG Py_off_t; |
| 588 | #else |
| 589 | typedef off_t Py_off_t; |
| 590 | #endif |
| 591 | |
| 592 | /* Cribbed from posix_lseek() */ |
| 593 | static PyObject * |
| 594 | portable_lseek(int fd, PyObject *posobj, int whence) |
| 595 | { |
| 596 | Py_off_t pos, res; |
| 597 | |
| 598 | #ifdef SEEK_SET |
| 599 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 600 | switch (whence) { |
| 601 | #if SEEK_SET != 0 |
| 602 | case 0: whence = SEEK_SET; break; |
| 603 | #endif |
| 604 | #if SEEK_CUR != 1 |
| 605 | case 1: whence = SEEK_CUR; break; |
| 606 | #endif |
Antoine Pitrou | 4f7945f | 2009-01-20 11:42:11 +0000 | [diff] [blame] | 607 | #if SEEK_END != 2 |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 608 | case 2: whence = SEEK_END; break; |
| 609 | #endif |
| 610 | } |
| 611 | #endif /* SEEK_SET */ |
| 612 | |
| 613 | if (posobj == NULL) |
| 614 | pos = 0; |
| 615 | else { |
Christian Heimes | 8e42a0a | 2007-11-08 18:04:45 +0000 | [diff] [blame] | 616 | if(PyFloat_Check(posobj)) { |
| 617 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 618 | return NULL; |
| 619 | } |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 620 | #if defined(HAVE_LARGEFILE_SUPPORT) |
| 621 | pos = PyLong_AsLongLong(posobj); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 622 | #else |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 623 | pos = PyLong_AsLong(posobj); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 624 | #endif |
| 625 | if (PyErr_Occurred()) |
| 626 | return NULL; |
| 627 | } |
| 628 | |
| 629 | Py_BEGIN_ALLOW_THREADS |
| 630 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
| 631 | res = _lseeki64(fd, pos, whence); |
| 632 | #else |
| 633 | res = lseek(fd, pos, whence); |
| 634 | #endif |
| 635 | Py_END_ALLOW_THREADS |
| 636 | if (res < 0) |
| 637 | return PyErr_SetFromErrno(PyExc_IOError); |
| 638 | |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 639 | #if defined(HAVE_LARGEFILE_SUPPORT) |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 640 | return PyLong_FromLongLong(res); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 641 | #else |
| 642 | return PyLong_FromLong(res); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 643 | #endif |
| 644 | } |
| 645 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 646 | static PyObject * |
| 647 | fileio_seek(PyFileIOObject *self, PyObject *args) |
| 648 | { |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 649 | PyObject *posobj; |
| 650 | int whence = 0; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 651 | |
| 652 | if (self->fd < 0) |
| 653 | return err_closed(); |
| 654 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 655 | if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence)) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 656 | return NULL; |
| 657 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 658 | return portable_lseek(self->fd, posobj, whence); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | static PyObject * |
| 662 | fileio_tell(PyFileIOObject *self, PyObject *args) |
| 663 | { |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 664 | if (self->fd < 0) |
| 665 | return err_closed(); |
| 666 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 667 | return portable_lseek(self->fd, NULL, 1); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 670 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 671 | static PyObject * |
| 672 | fileio_truncate(PyFileIOObject *self, PyObject *args) |
| 673 | { |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 674 | PyObject *posobj = NULL; |
| 675 | Py_off_t pos; |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 676 | int ret; |
Guido van Rossum | dc0b1a1 | 2007-04-12 22:55:07 +0000 | [diff] [blame] | 677 | int fd; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 678 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 679 | fd = self->fd; |
| 680 | if (fd < 0) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 681 | return err_closed(); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 682 | if (!self->writable) |
| 683 | return err_mode("writing"); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 684 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 685 | if (!PyArg_ParseTuple(args, "|O", &posobj)) |
| 686 | return NULL; |
| 687 | |
Guido van Rossum | dc0b1a1 | 2007-04-12 22:55:07 +0000 | [diff] [blame] | 688 | if (posobj == Py_None || posobj == NULL) { |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 689 | /* Get the current position. */ |
Guido van Rossum | dc0b1a1 | 2007-04-12 22:55:07 +0000 | [diff] [blame] | 690 | posobj = portable_lseek(fd, NULL, 1); |
| 691 | if (posobj == NULL) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 692 | return NULL; |
Guido van Rossum | dc0b1a1 | 2007-04-12 22:55:07 +0000 | [diff] [blame] | 693 | } |
| 694 | else { |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 695 | /* Move to the position to be truncated. */ |
| 696 | posobj = portable_lseek(fd, posobj, 0); |
Guido van Rossum | dc0b1a1 | 2007-04-12 22:55:07 +0000 | [diff] [blame] | 697 | } |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 698 | |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 699 | #if defined(HAVE_LARGEFILE_SUPPORT) |
| 700 | pos = PyLong_AsLongLong(posobj); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 701 | #else |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 702 | pos = PyLong_AsLong(posobj); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 703 | #endif |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 704 | if (PyErr_Occurred()) |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 705 | return NULL; |
| 706 | |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 707 | #ifdef MS_WINDOWS |
| 708 | /* MS _chsize doesn't work if newsize doesn't fit in 32 bits, |
| 709 | so don't even try using it. */ |
| 710 | { |
| 711 | HANDLE hFile; |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 712 | |
| 713 | /* Truncate. Note that this may grow the file! */ |
| 714 | Py_BEGIN_ALLOW_THREADS |
| 715 | errno = 0; |
| 716 | hFile = (HANDLE)_get_osfhandle(fd); |
| 717 | ret = hFile == (HANDLE)-1; |
| 718 | if (ret == 0) { |
| 719 | ret = SetEndOfFile(hFile) == 0; |
| 720 | if (ret) |
| 721 | errno = EACCES; |
| 722 | } |
| 723 | Py_END_ALLOW_THREADS |
| 724 | } |
| 725 | #else |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 726 | Py_BEGIN_ALLOW_THREADS |
| 727 | errno = 0; |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 728 | ret = ftruncate(fd, pos); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 729 | Py_END_ALLOW_THREADS |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 730 | #endif /* !MS_WINDOWS */ |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 731 | |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 732 | if (ret != 0) { |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 733 | PyErr_SetFromErrno(PyExc_IOError); |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 734 | return NULL; |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 735 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 736 | |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 737 | return posobj; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 738 | } |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 739 | #endif |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 740 | |
| 741 | static char * |
| 742 | mode_string(PyFileIOObject *self) |
| 743 | { |
| 744 | if (self->readable) { |
| 745 | if (self->writable) |
Benjamin Peterson | 44309e6 | 2008-11-22 00:41:45 +0000 | [diff] [blame] | 746 | return "rb+"; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 747 | else |
Benjamin Peterson | 44309e6 | 2008-11-22 00:41:45 +0000 | [diff] [blame] | 748 | return "rb"; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 749 | } |
| 750 | else |
Benjamin Peterson | 44309e6 | 2008-11-22 00:41:45 +0000 | [diff] [blame] | 751 | return "wb"; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 752 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 753 | |
| 754 | static PyObject * |
| 755 | fileio_repr(PyFileIOObject *self) |
| 756 | { |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 757 | if (self->fd < 0) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 758 | return PyUnicode_FromFormat("_fileio._FileIO(-1)"); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 759 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 760 | return PyUnicode_FromFormat("_fileio._FileIO(%d, '%s')", |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 761 | self->fd, mode_string(self)); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | static PyObject * |
| 765 | fileio_isatty(PyFileIOObject *self) |
| 766 | { |
| 767 | long res; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 768 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 769 | if (self->fd < 0) |
| 770 | return err_closed(); |
| 771 | Py_BEGIN_ALLOW_THREADS |
| 772 | res = isatty(self->fd); |
| 773 | Py_END_ALLOW_THREADS |
| 774 | return PyBool_FromLong(res); |
| 775 | } |
| 776 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 777 | |
| 778 | PyDoc_STRVAR(fileio_doc, |
| 779 | "file(name: str[, mode: str]) -> file IO object\n" |
| 780 | "\n" |
| 781 | "Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n" |
| 782 | "writing or appending. The file will be created if it doesn't exist\n" |
| 783 | "when opened for writing or appending; it will be truncated when\n" |
| 784 | "opened for writing. Add a '+' to the mode to allow simultaneous\n" |
| 785 | "reading and writing."); |
| 786 | |
| 787 | PyDoc_STRVAR(read_doc, |
| 788 | "read(size: int) -> bytes. read at most size bytes, returned as bytes.\n" |
| 789 | "\n" |
| 790 | "Only makes one system call, so less data may be returned than requested\n" |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 791 | "In non-blocking mode, returns None if no data is available.\n" |
| 792 | "On end-of-file, returns ''."); |
| 793 | |
| 794 | PyDoc_STRVAR(readall_doc, |
| 795 | "readall() -> bytes. read all data from the file, returned as bytes.\n" |
| 796 | "\n" |
| 797 | "In non-blocking mode, returns as much as is immediately available,\n" |
| 798 | "or None if no data is available. On end-of-file, returns ''."); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 799 | |
| 800 | PyDoc_STRVAR(write_doc, |
| 801 | "write(b: bytes) -> int. Write bytes b to file, return number written.\n" |
| 802 | "\n" |
| 803 | "Only makes one system call, so not all of the data may be written.\n" |
| 804 | "The number of bytes actually written is returned."); |
| 805 | |
| 806 | PyDoc_STRVAR(fileno_doc, |
| 807 | "fileno() -> int. \"file descriptor\".\n" |
| 808 | "\n" |
| 809 | "This is needed for lower-level file interfaces, such the fcntl module."); |
| 810 | |
| 811 | PyDoc_STRVAR(seek_doc, |
| 812 | "seek(offset: int[, whence: int]) -> None. Move to new file position.\n" |
| 813 | "\n" |
| 814 | "Argument offset is a byte count. Optional argument whence defaults to\n" |
| 815 | "0 (offset from start of file, offset should be >= 0); other values are 1\n" |
| 816 | "(move relative to current position, positive or negative), and 2 (move\n" |
| 817 | "relative to end of file, usually negative, although many platforms allow\n" |
| 818 | "seeking beyond the end of a file)." |
| 819 | "\n" |
| 820 | "Note that not all file objects are seekable."); |
| 821 | |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 822 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 823 | PyDoc_STRVAR(truncate_doc, |
| 824 | "truncate([size: int]) -> None. Truncate the file to at most size bytes.\n" |
| 825 | "\n" |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 826 | "Size defaults to the current file position, as returned by tell()." |
| 827 | "The current file position is changed to the value of size."); |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 828 | #endif |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 829 | |
| 830 | PyDoc_STRVAR(tell_doc, |
| 831 | "tell() -> int. Current file position"); |
| 832 | |
| 833 | PyDoc_STRVAR(readinto_doc, |
| 834 | "readinto() -> Undocumented. Don't use this; it may go away."); |
| 835 | |
| 836 | PyDoc_STRVAR(close_doc, |
| 837 | "close() -> None. Close the file.\n" |
| 838 | "\n" |
| 839 | "A closed file cannot be used for further I/O operations. close() may be\n" |
| 840 | "called more than once without error. Changes the fileno to -1."); |
| 841 | |
| 842 | PyDoc_STRVAR(isatty_doc, |
| 843 | "isatty() -> bool. True if the file is connected to a tty device."); |
| 844 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 845 | PyDoc_STRVAR(seekable_doc, |
| 846 | "seekable() -> bool. True if file supports random-access."); |
| 847 | |
| 848 | PyDoc_STRVAR(readable_doc, |
| 849 | "readable() -> bool. True if file was opened in a read mode."); |
| 850 | |
| 851 | PyDoc_STRVAR(writable_doc, |
| 852 | "writable() -> bool. True if file was opened in a write mode."); |
| 853 | |
| 854 | static PyMethodDef fileio_methods[] = { |
| 855 | {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc}, |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 856 | {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc}, |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 857 | {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc}, |
| 858 | {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc}, |
| 859 | {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc}, |
| 860 | {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc}, |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 861 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 862 | {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc}, |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 863 | #endif |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 864 | {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc}, |
| 865 | {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc}, |
| 866 | {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc}, |
| 867 | {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc}, |
| 868 | {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc}, |
| 869 | {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc}, |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 870 | {NULL, NULL} /* sentinel */ |
| 871 | }; |
| 872 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 873 | /* 'closed' and 'mode' are attributes for backwards compatibility reasons. */ |
| 874 | |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 875 | static PyObject * |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 876 | get_closed(PyFileIOObject *self, void *closure) |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 877 | { |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 878 | return PyBool_FromLong((long)(self->fd < 0)); |
| 879 | } |
| 880 | |
| 881 | static PyObject * |
Christian Heimes | ecc42a2 | 2008-11-05 19:30:32 +0000 | [diff] [blame] | 882 | get_closefd(PyFileIOObject *self, void *closure) |
| 883 | { |
| 884 | return PyBool_FromLong((long)(self->closefd)); |
| 885 | } |
| 886 | |
| 887 | static PyObject * |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 888 | get_mode(PyFileIOObject *self, void *closure) |
| 889 | { |
Guido van Rossum | c43e79f | 2007-06-18 18:26:36 +0000 | [diff] [blame] | 890 | return PyUnicode_FromString(mode_string(self)); |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | static PyGetSetDef fileio_getsetlist[] = { |
| 894 | {"closed", (getter)get_closed, NULL, "True if the file is closed"}, |
Christian Heimes | ecc42a2 | 2008-11-05 19:30:32 +0000 | [diff] [blame] | 895 | {"closefd", (getter)get_closefd, NULL, |
| 896 | "True if the file descriptor will be closed"}, |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 897 | {"mode", (getter)get_mode, NULL, "String giving the file mode"}, |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 898 | {0}, |
| 899 | }; |
| 900 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 901 | PyTypeObject PyFileIO_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 902 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 903 | "_FileIO", |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 904 | sizeof(PyFileIOObject), |
| 905 | 0, |
| 906 | (destructor)fileio_dealloc, /* tp_dealloc */ |
| 907 | 0, /* tp_print */ |
| 908 | 0, /* tp_getattr */ |
| 909 | 0, /* tp_setattr */ |
| 910 | 0, /* tp_compare */ |
| 911 | (reprfunc)fileio_repr, /* tp_repr */ |
| 912 | 0, /* tp_as_number */ |
| 913 | 0, /* tp_as_sequence */ |
| 914 | 0, /* tp_as_mapping */ |
| 915 | 0, /* tp_hash */ |
| 916 | 0, /* tp_call */ |
| 917 | 0, /* tp_str */ |
| 918 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 919 | 0, /* tp_setattro */ |
| 920 | 0, /* tp_as_buffer */ |
| 921 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 922 | fileio_doc, /* tp_doc */ |
| 923 | 0, /* tp_traverse */ |
| 924 | 0, /* tp_clear */ |
| 925 | 0, /* tp_richcompare */ |
| 926 | offsetof(PyFileIOObject, weakreflist), /* tp_weaklistoffset */ |
| 927 | 0, /* tp_iter */ |
| 928 | 0, /* tp_iternext */ |
| 929 | fileio_methods, /* tp_methods */ |
| 930 | 0, /* tp_members */ |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 931 | fileio_getsetlist, /* tp_getset */ |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 932 | 0, /* tp_base */ |
| 933 | 0, /* tp_dict */ |
| 934 | 0, /* tp_descr_get */ |
| 935 | 0, /* tp_descr_set */ |
| 936 | 0, /* tp_dictoffset */ |
| 937 | fileio_init, /* tp_init */ |
| 938 | PyType_GenericAlloc, /* tp_alloc */ |
| 939 | fileio_new, /* tp_new */ |
| 940 | PyObject_Del, /* tp_free */ |
| 941 | }; |
| 942 | |
| 943 | static PyMethodDef module_methods[] = { |
| 944 | {NULL, NULL} |
| 945 | }; |
| 946 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 947 | static struct PyModuleDef fileiomodule = { |
| 948 | PyModuleDef_HEAD_INIT, |
| 949 | "_fileio", |
| 950 | "Fast implementation of io.FileIO.", |
| 951 | -1, |
| 952 | module_methods, |
| 953 | NULL, |
| 954 | NULL, |
| 955 | NULL, |
| 956 | NULL |
| 957 | }; |
| 958 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 959 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 960 | PyInit__fileio(void) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 961 | { |
| 962 | PyObject *m; /* a module object */ |
| 963 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 964 | m = PyModule_Create(&fileiomodule); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 965 | if (m == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 966 | return NULL; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 967 | if (PyType_Ready(&PyFileIO_Type) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 968 | return NULL; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 969 | Py_INCREF(&PyFileIO_Type); |
| 970 | PyModule_AddObject(m, "_FileIO", (PyObject *) &PyFileIO_Type); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 971 | return m; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 972 | } |