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