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