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 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 539 | if (!_PyVerify_fd(self->fd)) |
| 540 | return PyErr_SetFromErrno(PyExc_IOError); |
Kristján Valur Jónsson | a8abe86 | 2009-03-24 15:27:42 +0000 | [diff] [blame] | 541 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 542 | result = PyBytes_FromStringAndSize(NULL, SMALLCHUNK); |
| 543 | if (result == NULL) |
| 544 | return NULL; |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 545 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 546 | while (1) { |
| 547 | size_t newsize = new_buffersize(self, total); |
| 548 | if (newsize > PY_SSIZE_T_MAX || newsize <= 0) { |
| 549 | PyErr_SetString(PyExc_OverflowError, |
| 550 | "unbounded read returned more bytes " |
| 551 | "than a Python string can hold "); |
| 552 | Py_DECREF(result); |
| 553 | return NULL; |
| 554 | } |
Christian Heimes | a872de5 | 2008-12-05 08:26:55 +0000 | [diff] [blame] | 555 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 556 | if (PyBytes_GET_SIZE(result) < (Py_ssize_t)newsize) { |
| 557 | if (_PyBytes_Resize(&result, newsize) < 0) { |
| 558 | if (total == 0) { |
| 559 | Py_DECREF(result); |
| 560 | return NULL; |
| 561 | } |
| 562 | PyErr_Clear(); |
| 563 | break; |
| 564 | } |
| 565 | } |
| 566 | Py_BEGIN_ALLOW_THREADS |
| 567 | errno = 0; |
| 568 | n = read(self->fd, |
| 569 | PyBytes_AS_STRING(result) + total, |
| 570 | newsize - total); |
| 571 | Py_END_ALLOW_THREADS |
| 572 | if (n == 0) |
| 573 | break; |
| 574 | if (n < 0) { |
| 575 | if (total > 0) |
| 576 | break; |
| 577 | if (errno == EAGAIN) { |
| 578 | Py_DECREF(result); |
| 579 | Py_RETURN_NONE; |
| 580 | } |
| 581 | Py_DECREF(result); |
| 582 | PyErr_SetFromErrno(PyExc_IOError); |
| 583 | return NULL; |
| 584 | } |
| 585 | total += n; |
| 586 | } |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 587 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 588 | if (PyBytes_GET_SIZE(result) > total) { |
| 589 | if (_PyBytes_Resize(&result, total) < 0) { |
| 590 | /* This should never happen, but just in case */ |
| 591 | Py_DECREF(result); |
| 592 | return NULL; |
| 593 | } |
| 594 | } |
| 595 | return result; |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 598 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 599 | fileio_read(fileio *self, PyObject *args) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 600 | { |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 601 | char *ptr; |
| 602 | Py_ssize_t n; |
| 603 | Py_ssize_t size = -1; |
| 604 | PyObject *bytes; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 605 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 606 | if (self->fd < 0) |
| 607 | return err_closed(); |
| 608 | if (!self->readable) |
| 609 | return err_mode("reading"); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 610 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 611 | if (!PyArg_ParseTuple(args, "|O&", &_PyIO_ConvertSsize_t, &size)) |
| 612 | return NULL; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 613 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 614 | if (size < 0) { |
| 615 | return fileio_readall(self); |
| 616 | } |
Guido van Rossum | c2f93dc | 2007-05-24 00:50:02 +0000 | [diff] [blame] | 617 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 618 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 619 | if (bytes == NULL) |
| 620 | return NULL; |
| 621 | ptr = PyBytes_AS_STRING(bytes); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 622 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 623 | if (_PyVerify_fd(self->fd)) { |
| 624 | Py_BEGIN_ALLOW_THREADS |
| 625 | errno = 0; |
| 626 | n = read(self->fd, ptr, size); |
| 627 | Py_END_ALLOW_THREADS |
| 628 | } else |
| 629 | n = -1; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 630 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 631 | if (n < 0) { |
| 632 | Py_DECREF(bytes); |
| 633 | if (errno == EAGAIN) |
| 634 | Py_RETURN_NONE; |
| 635 | PyErr_SetFromErrno(PyExc_IOError); |
| 636 | return NULL; |
| 637 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 638 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 639 | if (n != size) { |
| 640 | if (_PyBytes_Resize(&bytes, n) < 0) { |
| 641 | Py_DECREF(bytes); |
| 642 | return NULL; |
| 643 | } |
| 644 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 645 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 646 | return (PyObject *) bytes; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 650 | fileio_write(fileio *self, PyObject *args) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 651 | { |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 652 | Py_buffer pbuf; |
| 653 | Py_ssize_t n; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 654 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 655 | if (self->fd < 0) |
| 656 | return err_closed(); |
| 657 | if (!self->writable) |
| 658 | return err_mode("writing"); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 659 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 660 | if (!PyArg_ParseTuple(args, "y*", &pbuf)) |
| 661 | return NULL; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 662 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 663 | if (_PyVerify_fd(self->fd)) { |
| 664 | Py_BEGIN_ALLOW_THREADS |
| 665 | errno = 0; |
| 666 | n = write(self->fd, pbuf.buf, pbuf.len); |
| 667 | Py_END_ALLOW_THREADS |
| 668 | } else |
| 669 | n = -1; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 670 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 671 | PyBuffer_Release(&pbuf); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 672 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 673 | if (n < 0) { |
| 674 | if (errno == EAGAIN) |
| 675 | Py_RETURN_NONE; |
| 676 | PyErr_SetFromErrno(PyExc_IOError); |
| 677 | return NULL; |
| 678 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 679 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 680 | return PyLong_FromSsize_t(n); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 683 | /* XXX Windows support below is likely incomplete */ |
| 684 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 685 | /* Cribbed from posix_lseek() */ |
| 686 | static PyObject * |
| 687 | portable_lseek(int fd, PyObject *posobj, int whence) |
| 688 | { |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 689 | Py_off_t pos, res; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 690 | |
| 691 | #ifdef SEEK_SET |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 692 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 693 | switch (whence) { |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 694 | #if SEEK_SET != 0 |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 695 | case 0: whence = SEEK_SET; break; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 696 | #endif |
| 697 | #if SEEK_CUR != 1 |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 698 | case 1: whence = SEEK_CUR; break; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 699 | #endif |
Antoine Pitrou | 4f7945f | 2009-01-20 11:42:11 +0000 | [diff] [blame] | 700 | #if SEEK_END != 2 |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 701 | case 2: whence = SEEK_END; break; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 702 | #endif |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 703 | } |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 704 | #endif /* SEEK_SET */ |
| 705 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 706 | if (posobj == NULL) |
| 707 | pos = 0; |
| 708 | else { |
| 709 | if(PyFloat_Check(posobj)) { |
| 710 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 711 | return NULL; |
| 712 | } |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 713 | #if defined(HAVE_LARGEFILE_SUPPORT) |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 714 | pos = PyLong_AsLongLong(posobj); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 715 | #else |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 716 | pos = PyLong_AsLong(posobj); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 717 | #endif |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 718 | if (PyErr_Occurred()) |
| 719 | return NULL; |
| 720 | } |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 721 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 722 | if (_PyVerify_fd(fd)) { |
| 723 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 724 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 725 | res = _lseeki64(fd, pos, whence); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 726 | #else |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 727 | res = lseek(fd, pos, whence); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 728 | #endif |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 729 | Py_END_ALLOW_THREADS |
| 730 | } else |
| 731 | res = -1; |
| 732 | if (res < 0) |
| 733 | return PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 734 | |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 735 | #if defined(HAVE_LARGEFILE_SUPPORT) |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 736 | return PyLong_FromLongLong(res); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 737 | #else |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 738 | return PyLong_FromLong(res); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 739 | #endif |
| 740 | } |
| 741 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 742 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 743 | fileio_seek(fileio *self, PyObject *args) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 744 | { |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 745 | PyObject *posobj; |
| 746 | int whence = 0; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 747 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 748 | if (self->fd < 0) |
| 749 | return err_closed(); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 750 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 751 | if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence)) |
| 752 | return NULL; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 753 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 754 | return portable_lseek(self->fd, posobj, whence); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 758 | fileio_tell(fileio *self, PyObject *args) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 759 | { |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 760 | if (self->fd < 0) |
| 761 | return err_closed(); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 762 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 763 | return portable_lseek(self->fd, NULL, 1); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 766 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 767 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 768 | fileio_truncate(fileio *self, PyObject *args) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 769 | { |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 770 | PyObject *posobj = NULL; /* the new size wanted by the user */ |
Antoine Pitrou | 66f9fea | 2010-01-31 23:20:26 +0000 | [diff] [blame] | 771 | #ifndef MS_WINDOWS |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 772 | Py_off_t pos; |
Antoine Pitrou | 66f9fea | 2010-01-31 23:20:26 +0000 | [diff] [blame] | 773 | #endif |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 774 | int ret; |
| 775 | int fd; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 776 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 777 | fd = self->fd; |
| 778 | if (fd < 0) |
| 779 | return err_closed(); |
| 780 | if (!self->writable) |
| 781 | return err_mode("writing"); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 782 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 783 | if (!PyArg_ParseTuple(args, "|O", &posobj)) |
| 784 | return NULL; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 785 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 786 | if (posobj == Py_None || posobj == NULL) { |
| 787 | /* Get the current position. */ |
| 788 | posobj = portable_lseek(fd, NULL, 1); |
| 789 | if (posobj == NULL) |
| 790 | return NULL; |
| 791 | } |
| 792 | else { |
| 793 | Py_INCREF(posobj); |
| 794 | } |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 795 | |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 796 | #ifdef MS_WINDOWS |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 797 | /* MS _chsize doesn't work if newsize doesn't fit in 32 bits, |
| 798 | so don't even try using it. */ |
| 799 | { |
| 800 | PyObject *oldposobj, *tempposobj; |
| 801 | HANDLE hFile; |
Antoine Pitrou | 66f9fea | 2010-01-31 23:20:26 +0000 | [diff] [blame] | 802 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 803 | /* we save the file pointer position */ |
| 804 | oldposobj = portable_lseek(fd, NULL, 1); |
| 805 | if (oldposobj == NULL) { |
| 806 | Py_DECREF(posobj); |
| 807 | return NULL; |
| 808 | } |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 809 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 810 | /* we then move to the truncation position */ |
| 811 | tempposobj = portable_lseek(fd, posobj, 0); |
| 812 | if (tempposobj == NULL) { |
| 813 | Py_DECREF(oldposobj); |
| 814 | Py_DECREF(posobj); |
| 815 | return NULL; |
| 816 | } |
| 817 | Py_DECREF(tempposobj); |
Antoine Pitrou | 66f9fea | 2010-01-31 23:20:26 +0000 | [diff] [blame] | 818 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 819 | /* Truncate. Note that this may grow the file! */ |
| 820 | Py_BEGIN_ALLOW_THREADS |
| 821 | errno = 0; |
| 822 | hFile = (HANDLE)_get_osfhandle(fd); |
| 823 | ret = hFile == (HANDLE)-1; /* testing for INVALID_HANDLE value */ |
| 824 | if (ret == 0) { |
| 825 | ret = SetEndOfFile(hFile) == 0; |
| 826 | if (ret) |
| 827 | errno = EACCES; |
| 828 | } |
| 829 | Py_END_ALLOW_THREADS |
| 830 | |
| 831 | /* we restore the file pointer position in any case */ |
| 832 | tempposobj = portable_lseek(fd, oldposobj, 0); |
| 833 | Py_DECREF(oldposobj); |
| 834 | if (tempposobj == NULL) { |
| 835 | Py_DECREF(posobj); |
| 836 | return NULL; |
| 837 | } |
| 838 | Py_DECREF(tempposobj); |
| 839 | } |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 840 | #else |
Antoine Pitrou | 66f9fea | 2010-01-31 23:20:26 +0000 | [diff] [blame] | 841 | |
| 842 | #if defined(HAVE_LARGEFILE_SUPPORT) |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 843 | pos = PyLong_AsLongLong(posobj); |
Antoine Pitrou | 66f9fea | 2010-01-31 23:20:26 +0000 | [diff] [blame] | 844 | #else |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 845 | pos = PyLong_AsLong(posobj); |
Antoine Pitrou | 66f9fea | 2010-01-31 23:20:26 +0000 | [diff] [blame] | 846 | #endif |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 847 | if (PyErr_Occurred()){ |
| 848 | Py_DECREF(posobj); |
| 849 | return NULL; |
| 850 | } |
Antoine Pitrou | 66f9fea | 2010-01-31 23:20:26 +0000 | [diff] [blame] | 851 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 852 | Py_BEGIN_ALLOW_THREADS |
| 853 | errno = 0; |
| 854 | ret = ftruncate(fd, pos); |
| 855 | Py_END_ALLOW_THREADS |
Antoine Pitrou | 66f9fea | 2010-01-31 23:20:26 +0000 | [diff] [blame] | 856 | |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 857 | #endif /* !MS_WINDOWS */ |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 858 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 859 | if (ret != 0) { |
| 860 | Py_DECREF(posobj); |
| 861 | PyErr_SetFromErrno(PyExc_IOError); |
| 862 | return NULL; |
| 863 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 864 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 865 | return posobj; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 866 | } |
Antoine Pitrou | 66f9fea | 2010-01-31 23:20:26 +0000 | [diff] [blame] | 867 | #endif /* HAVE_FTRUNCATE */ |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 868 | |
| 869 | static char * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 870 | mode_string(fileio *self) |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 871 | { |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 872 | if (self->readable) { |
| 873 | if (self->writable) |
| 874 | return "rb+"; |
| 875 | else |
| 876 | return "rb"; |
| 877 | } |
| 878 | else |
| 879 | return "wb"; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 880 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 881 | |
| 882 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 883 | fileio_repr(fileio *self) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 884 | { |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 885 | PyObject *nameobj, *res; |
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 | if (self->fd < 0) |
| 888 | return PyUnicode_FromFormat("<_io.FileIO [closed]>"); |
Antoine Pitrou | 716c444 | 2009-05-23 19:04:03 +0000 | [diff] [blame] | 889 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 890 | nameobj = PyObject_GetAttrString((PyObject *) self, "name"); |
| 891 | if (nameobj == NULL) { |
| 892 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 893 | PyErr_Clear(); |
| 894 | else |
| 895 | return NULL; |
| 896 | res = PyUnicode_FromFormat("<_io.FileIO fd=%d mode='%s'>", |
| 897 | self->fd, mode_string(self)); |
| 898 | } |
| 899 | else { |
| 900 | res = PyUnicode_FromFormat("<_io.FileIO name=%R mode='%s'>", |
| 901 | nameobj, mode_string(self)); |
| 902 | Py_DECREF(nameobj); |
| 903 | } |
| 904 | return res; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 908 | fileio_isatty(fileio *self) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 909 | { |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 910 | long res; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 911 | |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 912 | if (self->fd < 0) |
| 913 | return err_closed(); |
| 914 | Py_BEGIN_ALLOW_THREADS |
| 915 | res = isatty(self->fd); |
| 916 | Py_END_ALLOW_THREADS |
| 917 | return PyBool_FromLong(res); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 918 | } |
| 919 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 920 | |
| 921 | PyDoc_STRVAR(fileio_doc, |
| 922 | "file(name: str[, mode: str]) -> file IO object\n" |
| 923 | "\n" |
| 924 | "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] | 925 | "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] | 926 | "when opened for writing or appending; it will be truncated when\n" |
| 927 | "opened for writing. Add a '+' to the mode to allow simultaneous\n" |
| 928 | "reading and writing."); |
| 929 | |
| 930 | PyDoc_STRVAR(read_doc, |
| 931 | "read(size: int) -> bytes. read at most size bytes, returned as bytes.\n" |
| 932 | "\n" |
| 933 | "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] | 934 | "In non-blocking mode, returns None if no data is available.\n" |
| 935 | "On end-of-file, returns ''."); |
| 936 | |
| 937 | PyDoc_STRVAR(readall_doc, |
| 938 | "readall() -> bytes. read all data from the file, returned as bytes.\n" |
| 939 | "\n" |
| 940 | "In non-blocking mode, returns as much as is immediately available,\n" |
| 941 | "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] | 942 | |
| 943 | PyDoc_STRVAR(write_doc, |
| 944 | "write(b: bytes) -> int. Write bytes b to file, return number written.\n" |
| 945 | "\n" |
| 946 | "Only makes one system call, so not all of the data may be written.\n" |
| 947 | "The number of bytes actually written is returned."); |
| 948 | |
| 949 | PyDoc_STRVAR(fileno_doc, |
| 950 | "fileno() -> int. \"file descriptor\".\n" |
| 951 | "\n" |
| 952 | "This is needed for lower-level file interfaces, such the fcntl module."); |
| 953 | |
| 954 | PyDoc_STRVAR(seek_doc, |
| 955 | "seek(offset: int[, whence: int]) -> None. Move to new file position.\n" |
| 956 | "\n" |
| 957 | "Argument offset is a byte count. Optional argument whence defaults to\n" |
| 958 | "0 (offset from start of file, offset should be >= 0); other values are 1\n" |
| 959 | "(move relative to current position, positive or negative), and 2 (move\n" |
| 960 | "relative to end of file, usually negative, although many platforms allow\n" |
| 961 | "seeking beyond the end of a file)." |
| 962 | "\n" |
| 963 | "Note that not all file objects are seekable."); |
| 964 | |
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 | PyDoc_STRVAR(truncate_doc, |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 967 | "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] | 968 | "\n" |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 969 | "Size defaults to the current file position, as returned by tell()." |
| 970 | "The current file position is changed to the value of size."); |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 971 | #endif |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 972 | |
| 973 | PyDoc_STRVAR(tell_doc, |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 974 | "tell() -> int. Current file position"); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 975 | |
| 976 | PyDoc_STRVAR(readinto_doc, |
Benjamin Peterson | 9a8082f | 2009-03-05 00:55:56 +0000 | [diff] [blame] | 977 | "readinto() -> Same as RawIOBase.readinto()."); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 978 | |
| 979 | PyDoc_STRVAR(close_doc, |
| 980 | "close() -> None. Close the file.\n" |
| 981 | "\n" |
| 982 | "A closed file cannot be used for further I/O operations. close() may be\n" |
| 983 | "called more than once without error. Changes the fileno to -1."); |
| 984 | |
| 985 | PyDoc_STRVAR(isatty_doc, |
| 986 | "isatty() -> bool. True if the file is connected to a tty device."); |
| 987 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 988 | PyDoc_STRVAR(seekable_doc, |
| 989 | "seekable() -> bool. True if file supports random-access."); |
| 990 | |
| 991 | PyDoc_STRVAR(readable_doc, |
| 992 | "readable() -> bool. True if file was opened in a read mode."); |
| 993 | |
| 994 | PyDoc_STRVAR(writable_doc, |
| 995 | "writable() -> bool. True if file was opened in a write mode."); |
| 996 | |
| 997 | static PyMethodDef fileio_methods[] = { |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 998 | {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc}, |
| 999 | {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc}, |
| 1000 | {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc}, |
| 1001 | {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc}, |
| 1002 | {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc}, |
| 1003 | {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc}, |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 1004 | #ifdef HAVE_FTRUNCATE |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 1005 | {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc}, |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 1006 | #endif |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 1007 | {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc}, |
| 1008 | {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc}, |
| 1009 | {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc}, |
| 1010 | {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc}, |
| 1011 | {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc}, |
| 1012 | {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc}, |
| 1013 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 1014 | }; |
| 1015 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 1016 | /* 'closed' and 'mode' are attributes for backwards compatibility reasons. */ |
| 1017 | |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 1018 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1019 | get_closed(fileio *self, void *closure) |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 1020 | { |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 1021 | return PyBool_FromLong((long)(self->fd < 0)); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1025 | get_closefd(fileio *self, void *closure) |
Christian Heimes | ecc42a2 | 2008-11-05 19:30:32 +0000 | [diff] [blame] | 1026 | { |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 1027 | return PyBool_FromLong((long)(self->closefd)); |
Christian Heimes | ecc42a2 | 2008-11-05 19:30:32 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1031 | get_mode(fileio *self, void *closure) |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 1032 | { |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 1033 | return PyUnicode_FromString(mode_string(self)); |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | static PyGetSetDef fileio_getsetlist[] = { |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 1037 | {"closed", (getter)get_closed, NULL, "True if the file is closed"}, |
| 1038 | {"closefd", (getter)get_closefd, NULL, |
| 1039 | "True if the file descriptor will be closed"}, |
| 1040 | {"mode", (getter)get_mode, NULL, "String giving the file mode"}, |
| 1041 | {NULL}, |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 1042 | }; |
| 1043 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 1044 | PyTypeObject PyFileIO_Type = { |
Antoine Pitrou | f7ec1fd | 2010-05-05 16:34:37 +0000 | [diff] [blame] | 1045 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1046 | "_io.FileIO", |
| 1047 | sizeof(fileio), |
| 1048 | 0, |
| 1049 | (destructor)fileio_dealloc, /* tp_dealloc */ |
| 1050 | 0, /* tp_print */ |
| 1051 | 0, /* tp_getattr */ |
| 1052 | 0, /* tp_setattr */ |
| 1053 | 0, /* tp_reserved */ |
| 1054 | (reprfunc)fileio_repr, /* tp_repr */ |
| 1055 | 0, /* tp_as_number */ |
| 1056 | 0, /* tp_as_sequence */ |
| 1057 | 0, /* tp_as_mapping */ |
| 1058 | 0, /* tp_hash */ |
| 1059 | 0, /* tp_call */ |
| 1060 | 0, /* tp_str */ |
| 1061 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1062 | 0, /* tp_setattro */ |
| 1063 | 0, /* tp_as_buffer */ |
| 1064 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
| 1065 | | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 1066 | fileio_doc, /* tp_doc */ |
| 1067 | (traverseproc)fileio_traverse, /* tp_traverse */ |
| 1068 | (inquiry)fileio_clear, /* tp_clear */ |
| 1069 | 0, /* tp_richcompare */ |
| 1070 | offsetof(fileio, weakreflist), /* tp_weaklistoffset */ |
| 1071 | 0, /* tp_iter */ |
| 1072 | 0, /* tp_iternext */ |
| 1073 | fileio_methods, /* tp_methods */ |
| 1074 | 0, /* tp_members */ |
| 1075 | fileio_getsetlist, /* tp_getset */ |
| 1076 | 0, /* tp_base */ |
| 1077 | 0, /* tp_dict */ |
| 1078 | 0, /* tp_descr_get */ |
| 1079 | 0, /* tp_descr_set */ |
| 1080 | offsetof(fileio, dict), /* tp_dictoffset */ |
| 1081 | fileio_init, /* tp_init */ |
| 1082 | PyType_GenericAlloc, /* tp_alloc */ |
| 1083 | fileio_new, /* tp_new */ |
| 1084 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 1085 | }; |