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