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