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