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 */ |
| 9 | |
| 10 | /* |
| 11 | * Known likely problems: |
| 12 | * |
| 13 | * - Files larger then 2**32-1 |
| 14 | * - Files with unicode filenames |
| 15 | * - Passing numbers greater than 2**32-1 when an integer is expected |
| 16 | * - Making it work on Windows and other oddball platforms |
| 17 | * |
| 18 | * To Do: |
| 19 | * |
| 20 | * - autoconfify header file inclusion |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #ifdef MS_WINDOWS |
| 24 | /* can simulate truncate with Win32 API functions; see file_truncate */ |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 25 | #define HAVE_FTRUNCATE |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 26 | #define WIN32_LEAN_AND_MEAN |
| 27 | #include <windows.h> |
| 28 | #endif |
| 29 | |
| 30 | typedef struct { |
| 31 | PyObject_HEAD |
| 32 | int fd; |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 33 | unsigned readable : 1; |
| 34 | unsigned writable : 1; |
| 35 | int seekable : 2; /* -1 means unknown */ |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 36 | int closefd : 1; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 37 | PyObject *weakreflist; |
| 38 | } PyFileIOObject; |
| 39 | |
Collin Winter | af33438 | 2007-03-08 21:46:15 +0000 | [diff] [blame] | 40 | PyTypeObject PyFileIO_Type; |
| 41 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 42 | #define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type)) |
| 43 | |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 44 | /* Returns 0 on success, errno (which is < 0) on failure. */ |
| 45 | static int |
| 46 | internal_close(PyFileIOObject *self) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 47 | { |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 48 | int save_errno = 0; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 49 | if (self->fd >= 0) { |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 50 | int fd = self->fd; |
| 51 | self->fd = -1; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 52 | Py_BEGIN_ALLOW_THREADS |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 53 | if (close(fd) < 0) |
| 54 | save_errno = errno; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 55 | Py_END_ALLOW_THREADS |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 56 | } |
| 57 | return save_errno; |
| 58 | } |
| 59 | |
| 60 | static PyObject * |
| 61 | fileio_close(PyFileIOObject *self) |
| 62 | { |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 63 | if (!self->closefd) { |
| 64 | if (PyErr_WarnEx(PyExc_RuntimeWarning, |
| 65 | "Trying to close unclosable fd!", 3) < 0) { |
| 66 | return NULL; |
| 67 | } |
| 68 | Py_RETURN_NONE; |
| 69 | } |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 70 | errno = internal_close(self); |
| 71 | if (errno < 0) { |
| 72 | PyErr_SetFromErrno(PyExc_IOError); |
| 73 | return NULL; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | Py_RETURN_NONE; |
| 77 | } |
| 78 | |
| 79 | static PyObject * |
| 80 | fileio_new(PyTypeObject *type, PyObject *args, PyObject *kews) |
| 81 | { |
| 82 | PyFileIOObject *self; |
| 83 | |
| 84 | assert(type != NULL && type->tp_alloc != NULL); |
| 85 | |
| 86 | self = (PyFileIOObject *) type->tp_alloc(type, 0); |
| 87 | if (self != NULL) { |
| 88 | self->fd = -1; |
| 89 | self->weakreflist = NULL; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | return (PyObject *) self; |
| 93 | } |
| 94 | |
| 95 | /* On Unix, open will succeed for directories. |
| 96 | In Python, there should be no file objects referring to |
| 97 | directories, so we need a check. */ |
| 98 | |
| 99 | static int |
| 100 | dircheck(PyFileIOObject* self) |
| 101 | { |
| 102 | #if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR) |
| 103 | struct stat buf; |
| 104 | if (self->fd < 0) |
| 105 | return 0; |
| 106 | if (fstat(self->fd, &buf) == 0 && S_ISDIR(buf.st_mode)) { |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 107 | char *msg = strerror(EISDIR); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 108 | PyObject *exc; |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 109 | internal_close(self); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 110 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 111 | exc = PyObject_CallFunction(PyExc_IOError, "(is)", |
| 112 | EISDIR, msg); |
| 113 | PyErr_SetObject(PyExc_IOError, exc); |
| 114 | Py_XDECREF(exc); |
| 115 | return -1; |
| 116 | } |
| 117 | #endif |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | static int |
| 123 | fileio_init(PyObject *oself, PyObject *args, PyObject *kwds) |
| 124 | { |
| 125 | PyFileIOObject *self = (PyFileIOObject *) oself; |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 126 | static char *kwlist[] = {"file", "mode", "closefd", NULL}; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 127 | char *name = NULL; |
| 128 | char *mode = "r"; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 129 | char *s; |
Thomas Heller | af2be26 | 2007-07-12 11:03:13 +0000 | [diff] [blame] | 130 | #ifdef MS_WINDOWS |
| 131 | Py_UNICODE *widename = NULL; |
| 132 | #endif |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 133 | int ret = 0; |
| 134 | int rwa = 0, plus = 0, append = 0; |
| 135 | int flags = 0; |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 136 | int fd = -1; |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 137 | int closefd = 1; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 138 | |
| 139 | assert(PyFileIO_Check(oself)); |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 140 | if (self->fd >= 0) { |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 141 | /* Have to close the existing file first. */ |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 142 | if (internal_close(self) < 0) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 143 | return -1; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 146 | if (PyArg_ParseTupleAndKeywords(args, kwds, "i|si:fileio", |
| 147 | kwlist, &fd, &mode, &closefd)) { |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 148 | if (fd < 0) { |
| 149 | PyErr_SetString(PyExc_ValueError, |
| 150 | "Negative filedescriptor"); |
| 151 | return -1; |
| 152 | } |
| 153 | } |
| 154 | else { |
| 155 | PyErr_Clear(); |
| 156 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 157 | #ifdef Py_WIN_WIDE_FILENAMES |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 158 | if (GetVersion() < 0x80000000) { |
| 159 | /* On NT, so wide API available */ |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 160 | PyObject *po; |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 161 | if (PyArg_ParseTupleAndKeywords(args, kwds, "U|si:fileio", |
| 162 | kwlist, &po, &mode, &closefd) |
| 163 | ) { |
Thomas Heller | af2be26 | 2007-07-12 11:03:13 +0000 | [diff] [blame] | 164 | widename = PyUnicode_AS_UNICODE(po); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 165 | } else { |
| 166 | /* Drop the argument parsing error as narrow |
| 167 | strings are also valid. */ |
| 168 | PyErr_Clear(); |
| 169 | } |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 170 | } |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 171 | if (widename == NULL) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 172 | #endif |
Thomas Heller | af2be26 | 2007-07-12 11:03:13 +0000 | [diff] [blame] | 173 | { |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 174 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:fileio", |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 175 | kwlist, |
| 176 | Py_FileSystemDefaultEncoding, |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 177 | &name, &mode, &closefd)) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 178 | goto error; |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 179 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | self->readable = self->writable = 0; |
Thomas Heller | af2be26 | 2007-07-12 11:03:13 +0000 | [diff] [blame] | 183 | self->seekable = -1; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 184 | s = mode; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 185 | while (*s) { |
| 186 | switch (*s++) { |
| 187 | case 'r': |
| 188 | if (rwa) { |
| 189 | bad_mode: |
| 190 | PyErr_SetString(PyExc_ValueError, |
| 191 | "Must have exactly one of read/write/append mode"); |
| 192 | goto error; |
| 193 | } |
| 194 | rwa = 1; |
| 195 | self->readable = 1; |
| 196 | break; |
| 197 | case 'w': |
| 198 | if (rwa) |
| 199 | goto bad_mode; |
| 200 | rwa = 1; |
| 201 | self->writable = 1; |
| 202 | flags |= O_CREAT | O_TRUNC; |
| 203 | break; |
| 204 | case 'a': |
| 205 | if (rwa) |
| 206 | goto bad_mode; |
| 207 | rwa = 1; |
| 208 | self->writable = 1; |
| 209 | flags |= O_CREAT; |
| 210 | append = 1; |
| 211 | break; |
| 212 | case '+': |
| 213 | if (plus) |
| 214 | goto bad_mode; |
| 215 | self->readable = self->writable = 1; |
| 216 | plus = 1; |
| 217 | break; |
| 218 | default: |
| 219 | PyErr_Format(PyExc_ValueError, |
| 220 | "invalid mode: %.200s", mode); |
| 221 | goto error; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | if (!rwa) |
| 226 | goto bad_mode; |
| 227 | |
| 228 | if (self->readable && self->writable) |
| 229 | flags |= O_RDWR; |
| 230 | else if (self->readable) |
| 231 | flags |= O_RDONLY; |
| 232 | else |
| 233 | flags |= O_WRONLY; |
| 234 | |
| 235 | #ifdef O_BINARY |
| 236 | flags |= O_BINARY; |
| 237 | #endif |
| 238 | |
Walter Dörwald | 0e41148 | 2007-06-06 16:55:38 +0000 | [diff] [blame] | 239 | #ifdef O_APPEND |
| 240 | if (append) |
| 241 | flags |= O_APPEND; |
| 242 | #endif |
| 243 | |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 244 | if (fd >= 0) { |
| 245 | self->fd = fd; |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 246 | self->closefd = closefd; |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 247 | } |
| 248 | else { |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 249 | self->closefd = 1; |
| 250 | if (!closefd) { |
| 251 | PyErr_SetString(PyExc_ValueError, |
| 252 | "Cannot use closefd=True with file name"); |
| 253 | goto error; |
| 254 | } |
| 255 | |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 256 | Py_BEGIN_ALLOW_THREADS |
| 257 | errno = 0; |
Thomas Heller | af2be26 | 2007-07-12 11:03:13 +0000 | [diff] [blame] | 258 | #ifdef MS_WINDOWS |
| 259 | if (widename != NULL) |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 260 | self->fd = _wopen(widename, flags, 0666); |
Thomas Heller | af2be26 | 2007-07-12 11:03:13 +0000 | [diff] [blame] | 261 | else |
| 262 | #endif |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 263 | self->fd = open(name, flags, 0666); |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 264 | Py_END_ALLOW_THREADS |
| 265 | if (self->fd < 0 || dircheck(self) < 0) { |
Christian Heimes | 0b48954 | 2007-10-31 19:20:48 +0000 | [diff] [blame] | 266 | #ifdef MS_WINDOWS |
| 267 | PyErr_SetFromErrnoWithUnicodeFilename(PyExc_IOError, widename); |
| 268 | #else |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 269 | PyErr_SetFromErrnoWithFilename(PyExc_IOError, name); |
Christian Heimes | 0b48954 | 2007-10-31 19:20:48 +0000 | [diff] [blame] | 270 | #endif |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 271 | goto error; |
| 272 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | goto done; |
| 276 | |
| 277 | error: |
| 278 | ret = -1; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 279 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 280 | done: |
| 281 | PyMem_Free(name); |
| 282 | return ret; |
| 283 | } |
| 284 | |
| 285 | static void |
| 286 | fileio_dealloc(PyFileIOObject *self) |
| 287 | { |
| 288 | if (self->weakreflist != NULL) |
| 289 | PyObject_ClearWeakRefs((PyObject *) self); |
| 290 | |
Guido van Rossum | 2dced8b | 2007-10-30 17:27:30 +0000 | [diff] [blame] | 291 | if (self->fd >= 0 && self->closefd) { |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 292 | errno = internal_close(self); |
| 293 | if (errno < 0) { |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 294 | PySys_WriteStderr("close failed: [Errno %d] %s\n", |
| 295 | errno, strerror(errno)); |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 296 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 299 | Py_TYPE(self)->tp_free((PyObject *)self); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | static PyObject * |
| 303 | err_closed(void) |
| 304 | { |
| 305 | PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); |
| 306 | return NULL; |
| 307 | } |
| 308 | |
| 309 | static PyObject * |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 310 | err_mode(char *action) |
| 311 | { |
| 312 | PyErr_Format(PyExc_ValueError, "File not open for %s", action); |
| 313 | return NULL; |
| 314 | } |
| 315 | |
| 316 | static PyObject * |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 317 | fileio_fileno(PyFileIOObject *self) |
| 318 | { |
| 319 | if (self->fd < 0) |
| 320 | return err_closed(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 321 | return PyLong_FromLong((long) self->fd); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | static PyObject * |
| 325 | fileio_readable(PyFileIOObject *self) |
| 326 | { |
| 327 | if (self->fd < 0) |
| 328 | return err_closed(); |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 329 | return PyBool_FromLong((long) self->readable); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | static PyObject * |
| 333 | fileio_writable(PyFileIOObject *self) |
| 334 | { |
| 335 | if (self->fd < 0) |
| 336 | return err_closed(); |
Neal Norwitz | 88b44da | 2007-08-12 17:23:54 +0000 | [diff] [blame] | 337 | return PyBool_FromLong((long) self->writable); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | static PyObject * |
| 341 | fileio_seekable(PyFileIOObject *self) |
| 342 | { |
| 343 | if (self->fd < 0) |
| 344 | return err_closed(); |
| 345 | if (self->seekable < 0) { |
| 346 | int ret; |
| 347 | Py_BEGIN_ALLOW_THREADS |
| 348 | ret = lseek(self->fd, 0, SEEK_CUR); |
| 349 | Py_END_ALLOW_THREADS |
| 350 | if (ret < 0) |
| 351 | self->seekable = 0; |
| 352 | else |
| 353 | self->seekable = 1; |
| 354 | } |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 355 | return PyBool_FromLong((long) self->seekable); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | static PyObject * |
| 359 | fileio_readinto(PyFileIOObject *self, PyObject *args) |
| 360 | { |
| 361 | char *ptr; |
| 362 | Py_ssize_t n; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 363 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 364 | if (self->fd < 0) |
| 365 | return err_closed(); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 366 | if (!self->readable) |
| 367 | return err_mode("reading"); |
| 368 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 369 | if (!PyArg_ParseTuple(args, "w#", &ptr, &n)) |
| 370 | return NULL; |
| 371 | |
| 372 | Py_BEGIN_ALLOW_THREADS |
| 373 | errno = 0; |
| 374 | n = read(self->fd, ptr, n); |
| 375 | Py_END_ALLOW_THREADS |
| 376 | if (n < 0) { |
| 377 | if (errno == EAGAIN) |
| 378 | Py_RETURN_NONE; |
| 379 | PyErr_SetFromErrno(PyExc_IOError); |
| 380 | return NULL; |
| 381 | } |
| 382 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 383 | return PyLong_FromSsize_t(n); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 386 | #define DEFAULT_BUFFER_SIZE (8*1024) |
| 387 | |
| 388 | static PyObject * |
| 389 | fileio_readall(PyFileIOObject *self) |
| 390 | { |
| 391 | PyObject *result; |
| 392 | Py_ssize_t total = 0; |
| 393 | int n; |
| 394 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 395 | result = PyString_FromStringAndSize(NULL, DEFAULT_BUFFER_SIZE); |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 396 | if (result == NULL) |
| 397 | return NULL; |
| 398 | |
| 399 | while (1) { |
| 400 | Py_ssize_t newsize = total + DEFAULT_BUFFER_SIZE; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 401 | if (PyString_GET_SIZE(result) < newsize) { |
| 402 | if (_PyString_Resize(&result, newsize) < 0) { |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 403 | if (total == 0) { |
| 404 | Py_DECREF(result); |
| 405 | return NULL; |
| 406 | } |
| 407 | PyErr_Clear(); |
| 408 | break; |
| 409 | } |
| 410 | } |
| 411 | Py_BEGIN_ALLOW_THREADS |
| 412 | errno = 0; |
| 413 | n = read(self->fd, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 414 | PyString_AS_STRING(result) + total, |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 415 | newsize - total); |
| 416 | Py_END_ALLOW_THREADS |
| 417 | if (n == 0) |
| 418 | break; |
| 419 | if (n < 0) { |
| 420 | if (total > 0) |
| 421 | break; |
| 422 | if (errno == EAGAIN) { |
| 423 | Py_DECREF(result); |
| 424 | Py_RETURN_NONE; |
| 425 | } |
| 426 | Py_DECREF(result); |
| 427 | PyErr_SetFromErrno(PyExc_IOError); |
| 428 | return NULL; |
| 429 | } |
| 430 | total += n; |
| 431 | } |
| 432 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 433 | if (PyString_GET_SIZE(result) > total) { |
| 434 | if (_PyString_Resize(&result, total) < 0) { |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 435 | /* This should never happen, but just in case */ |
| 436 | Py_DECREF(result); |
| 437 | return NULL; |
| 438 | } |
| 439 | } |
| 440 | return result; |
| 441 | } |
| 442 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 443 | static PyObject * |
| 444 | fileio_read(PyFileIOObject *self, PyObject *args) |
| 445 | { |
| 446 | char *ptr; |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 447 | Py_ssize_t n; |
| 448 | Py_ssize_t size = -1; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 449 | PyObject *bytes; |
| 450 | |
| 451 | if (self->fd < 0) |
| 452 | return err_closed(); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 453 | if (!self->readable) |
| 454 | return err_mode("reading"); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 455 | |
Neal Norwitz | 3c8ba93 | 2007-08-08 04:36:17 +0000 | [diff] [blame] | 456 | if (!PyArg_ParseTuple(args, "|n", &size)) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 457 | return NULL; |
| 458 | |
Guido van Rossum | c2f93dc | 2007-05-24 00:50:02 +0000 | [diff] [blame] | 459 | if (size < 0) { |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 460 | return fileio_readall(self); |
Guido van Rossum | c2f93dc | 2007-05-24 00:50:02 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 463 | bytes = PyString_FromStringAndSize(NULL, size); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 464 | if (bytes == NULL) |
| 465 | return NULL; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 466 | ptr = PyString_AS_STRING(bytes); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 467 | |
| 468 | Py_BEGIN_ALLOW_THREADS |
| 469 | errno = 0; |
| 470 | n = read(self->fd, ptr, size); |
| 471 | Py_END_ALLOW_THREADS |
| 472 | |
| 473 | if (n < 0) { |
| 474 | if (errno == EAGAIN) |
| 475 | Py_RETURN_NONE; |
| 476 | PyErr_SetFromErrno(PyExc_IOError); |
| 477 | return NULL; |
| 478 | } |
| 479 | |
| 480 | if (n != size) { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 481 | if (_PyString_Resize(&bytes, n) < 0) { |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 482 | Py_DECREF(bytes); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 483 | return NULL; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 484 | } |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 485 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 486 | |
| 487 | return (PyObject *) bytes; |
| 488 | } |
| 489 | |
| 490 | static PyObject * |
| 491 | fileio_write(PyFileIOObject *self, PyObject *args) |
| 492 | { |
| 493 | Py_ssize_t n; |
| 494 | char *ptr; |
| 495 | |
| 496 | if (self->fd < 0) |
| 497 | return err_closed(); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 498 | if (!self->writable) |
| 499 | return err_mode("writing"); |
| 500 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 501 | if (!PyArg_ParseTuple(args, "s#", &ptr, &n)) |
| 502 | return NULL; |
| 503 | |
| 504 | Py_BEGIN_ALLOW_THREADS |
| 505 | errno = 0; |
| 506 | n = write(self->fd, ptr, n); |
| 507 | Py_END_ALLOW_THREADS |
| 508 | |
| 509 | if (n < 0) { |
| 510 | if (errno == EAGAIN) |
| 511 | Py_RETURN_NONE; |
| 512 | PyErr_SetFromErrno(PyExc_IOError); |
| 513 | return NULL; |
| 514 | } |
| 515 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 516 | return PyLong_FromSsize_t(n); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 519 | /* XXX Windows support below is likely incomplete */ |
| 520 | |
| 521 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
| 522 | typedef PY_LONG_LONG Py_off_t; |
| 523 | #else |
| 524 | typedef off_t Py_off_t; |
| 525 | #endif |
| 526 | |
| 527 | /* Cribbed from posix_lseek() */ |
| 528 | static PyObject * |
| 529 | portable_lseek(int fd, PyObject *posobj, int whence) |
| 530 | { |
| 531 | Py_off_t pos, res; |
| 532 | |
| 533 | #ifdef SEEK_SET |
| 534 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 535 | switch (whence) { |
| 536 | #if SEEK_SET != 0 |
| 537 | case 0: whence = SEEK_SET; break; |
| 538 | #endif |
| 539 | #if SEEK_CUR != 1 |
| 540 | case 1: whence = SEEK_CUR; break; |
| 541 | #endif |
| 542 | #if SEEL_END != 2 |
| 543 | case 2: whence = SEEK_END; break; |
| 544 | #endif |
| 545 | } |
| 546 | #endif /* SEEK_SET */ |
| 547 | |
| 548 | if (posobj == NULL) |
| 549 | pos = 0; |
| 550 | else { |
Christian Heimes | 8e42a0a | 2007-11-08 18:04:45 +0000 | [diff] [blame] | 551 | if(PyFloat_Check(posobj)) { |
| 552 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 553 | return NULL; |
| 554 | } |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 555 | #if defined(HAVE_LARGEFILE_SUPPORT) |
| 556 | pos = PyLong_AsLongLong(posobj); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 557 | #else |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 558 | pos = PyLong_AsLong(posobj); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 559 | #endif |
| 560 | if (PyErr_Occurred()) |
| 561 | return NULL; |
| 562 | } |
| 563 | |
| 564 | Py_BEGIN_ALLOW_THREADS |
| 565 | #if defined(MS_WIN64) || defined(MS_WINDOWS) |
| 566 | res = _lseeki64(fd, pos, whence); |
| 567 | #else |
| 568 | res = lseek(fd, pos, whence); |
| 569 | #endif |
| 570 | Py_END_ALLOW_THREADS |
| 571 | if (res < 0) |
| 572 | return PyErr_SetFromErrno(PyExc_IOError); |
| 573 | |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 574 | #if defined(HAVE_LARGEFILE_SUPPORT) |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 575 | return PyLong_FromLongLong(res); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 576 | #else |
| 577 | return PyLong_FromLong(res); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 578 | #endif |
| 579 | } |
| 580 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 581 | static PyObject * |
| 582 | fileio_seek(PyFileIOObject *self, PyObject *args) |
| 583 | { |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 584 | PyObject *posobj; |
| 585 | int whence = 0; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 586 | |
| 587 | if (self->fd < 0) |
| 588 | return err_closed(); |
| 589 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 590 | if (!PyArg_ParseTuple(args, "O|i", &posobj, &whence)) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 591 | return NULL; |
| 592 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 593 | return portable_lseek(self->fd, posobj, whence); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | static PyObject * |
| 597 | fileio_tell(PyFileIOObject *self, PyObject *args) |
| 598 | { |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 599 | if (self->fd < 0) |
| 600 | return err_closed(); |
| 601 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 602 | return portable_lseek(self->fd, NULL, 1); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 605 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 606 | static PyObject * |
| 607 | fileio_truncate(PyFileIOObject *self, PyObject *args) |
| 608 | { |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 609 | PyObject *posobj = NULL; |
| 610 | Py_off_t pos; |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 611 | int ret; |
Guido van Rossum | dc0b1a1 | 2007-04-12 22:55:07 +0000 | [diff] [blame] | 612 | int fd; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 613 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 614 | fd = self->fd; |
| 615 | if (fd < 0) |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 616 | return err_closed(); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 617 | if (!self->writable) |
| 618 | return err_mode("writing"); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 619 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 620 | if (!PyArg_ParseTuple(args, "|O", &posobj)) |
| 621 | return NULL; |
| 622 | |
Guido van Rossum | dc0b1a1 | 2007-04-12 22:55:07 +0000 | [diff] [blame] | 623 | if (posobj == Py_None || posobj == NULL) { |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 624 | /* Get the current position. */ |
Guido van Rossum | dc0b1a1 | 2007-04-12 22:55:07 +0000 | [diff] [blame] | 625 | posobj = portable_lseek(fd, NULL, 1); |
| 626 | if (posobj == NULL) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 627 | return NULL; |
Guido van Rossum | dc0b1a1 | 2007-04-12 22:55:07 +0000 | [diff] [blame] | 628 | } |
| 629 | else { |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 630 | /* Move to the position to be truncated. */ |
| 631 | posobj = portable_lseek(fd, posobj, 0); |
Guido van Rossum | dc0b1a1 | 2007-04-12 22:55:07 +0000 | [diff] [blame] | 632 | } |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 633 | |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 634 | #if defined(HAVE_LARGEFILE_SUPPORT) |
| 635 | pos = PyLong_AsLongLong(posobj); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 636 | #else |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 637 | pos = PyLong_AsLong(posobj); |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 638 | #endif |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 639 | if (PyErr_Occurred()) |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 640 | return NULL; |
| 641 | |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 642 | #ifdef MS_WINDOWS |
| 643 | /* MS _chsize doesn't work if newsize doesn't fit in 32 bits, |
| 644 | so don't even try using it. */ |
| 645 | { |
| 646 | HANDLE hFile; |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 647 | |
| 648 | /* Truncate. Note that this may grow the file! */ |
| 649 | Py_BEGIN_ALLOW_THREADS |
| 650 | errno = 0; |
| 651 | hFile = (HANDLE)_get_osfhandle(fd); |
| 652 | ret = hFile == (HANDLE)-1; |
| 653 | if (ret == 0) { |
| 654 | ret = SetEndOfFile(hFile) == 0; |
| 655 | if (ret) |
| 656 | errno = EACCES; |
| 657 | } |
| 658 | Py_END_ALLOW_THREADS |
| 659 | } |
| 660 | #else |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 661 | Py_BEGIN_ALLOW_THREADS |
| 662 | errno = 0; |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 663 | ret = ftruncate(fd, pos); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 664 | Py_END_ALLOW_THREADS |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 665 | #endif /* !MS_WINDOWS */ |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 666 | |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 667 | if (ret != 0) { |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 668 | PyErr_SetFromErrno(PyExc_IOError); |
Thomas Heller | fdeee3a | 2007-07-12 11:21:36 +0000 | [diff] [blame] | 669 | return NULL; |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 670 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 671 | |
Guido van Rossum | 8742977 | 2007-04-10 21:06:59 +0000 | [diff] [blame] | 672 | return posobj; |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 673 | } |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 674 | #endif |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 675 | |
| 676 | static char * |
| 677 | mode_string(PyFileIOObject *self) |
| 678 | { |
| 679 | if (self->readable) { |
| 680 | if (self->writable) |
| 681 | return "r+"; |
| 682 | else |
| 683 | return "r"; |
| 684 | } |
| 685 | else |
| 686 | return "w"; |
| 687 | } |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 688 | |
| 689 | static PyObject * |
| 690 | fileio_repr(PyFileIOObject *self) |
| 691 | { |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 692 | if (self->fd < 0) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 693 | return PyUnicode_FromFormat("_fileio._FileIO(-1)"); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 694 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 695 | return PyUnicode_FromFormat("_fileio._FileIO(%d, '%s')", |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 696 | self->fd, mode_string(self)); |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | static PyObject * |
| 700 | fileio_isatty(PyFileIOObject *self) |
| 701 | { |
| 702 | long res; |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 703 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 704 | if (self->fd < 0) |
| 705 | return err_closed(); |
| 706 | Py_BEGIN_ALLOW_THREADS |
| 707 | res = isatty(self->fd); |
| 708 | Py_END_ALLOW_THREADS |
| 709 | return PyBool_FromLong(res); |
| 710 | } |
| 711 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 712 | |
| 713 | PyDoc_STRVAR(fileio_doc, |
| 714 | "file(name: str[, mode: str]) -> file IO object\n" |
| 715 | "\n" |
| 716 | "Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n" |
| 717 | "writing or appending. The file will be created if it doesn't exist\n" |
| 718 | "when opened for writing or appending; it will be truncated when\n" |
| 719 | "opened for writing. Add a '+' to the mode to allow simultaneous\n" |
| 720 | "reading and writing."); |
| 721 | |
| 722 | PyDoc_STRVAR(read_doc, |
| 723 | "read(size: int) -> bytes. read at most size bytes, returned as bytes.\n" |
| 724 | "\n" |
| 725 | "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] | 726 | "In non-blocking mode, returns None if no data is available.\n" |
| 727 | "On end-of-file, returns ''."); |
| 728 | |
| 729 | PyDoc_STRVAR(readall_doc, |
| 730 | "readall() -> bytes. read all data from the file, returned as bytes.\n" |
| 731 | "\n" |
| 732 | "In non-blocking mode, returns as much as is immediately available,\n" |
| 733 | "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] | 734 | |
| 735 | PyDoc_STRVAR(write_doc, |
| 736 | "write(b: bytes) -> int. Write bytes b to file, return number written.\n" |
| 737 | "\n" |
| 738 | "Only makes one system call, so not all of the data may be written.\n" |
| 739 | "The number of bytes actually written is returned."); |
| 740 | |
| 741 | PyDoc_STRVAR(fileno_doc, |
| 742 | "fileno() -> int. \"file descriptor\".\n" |
| 743 | "\n" |
| 744 | "This is needed for lower-level file interfaces, such the fcntl module."); |
| 745 | |
| 746 | PyDoc_STRVAR(seek_doc, |
| 747 | "seek(offset: int[, whence: int]) -> None. Move to new file position.\n" |
| 748 | "\n" |
| 749 | "Argument offset is a byte count. Optional argument whence defaults to\n" |
| 750 | "0 (offset from start of file, offset should be >= 0); other values are 1\n" |
| 751 | "(move relative to current position, positive or negative), and 2 (move\n" |
| 752 | "relative to end of file, usually negative, although many platforms allow\n" |
| 753 | "seeking beyond the end of a file)." |
| 754 | "\n" |
| 755 | "Note that not all file objects are seekable."); |
| 756 | |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 757 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 758 | PyDoc_STRVAR(truncate_doc, |
| 759 | "truncate([size: int]) -> None. Truncate the file to at most size bytes.\n" |
| 760 | "\n" |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 761 | "Size defaults to the current file position, as returned by tell()." |
| 762 | "The current file position is changed to the value of size."); |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 763 | #endif |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 764 | |
| 765 | PyDoc_STRVAR(tell_doc, |
| 766 | "tell() -> int. Current file position"); |
| 767 | |
| 768 | PyDoc_STRVAR(readinto_doc, |
| 769 | "readinto() -> Undocumented. Don't use this; it may go away."); |
| 770 | |
| 771 | PyDoc_STRVAR(close_doc, |
| 772 | "close() -> None. Close the file.\n" |
| 773 | "\n" |
| 774 | "A closed file cannot be used for further I/O operations. close() may be\n" |
| 775 | "called more than once without error. Changes the fileno to -1."); |
| 776 | |
| 777 | PyDoc_STRVAR(isatty_doc, |
| 778 | "isatty() -> bool. True if the file is connected to a tty device."); |
| 779 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 780 | PyDoc_STRVAR(seekable_doc, |
| 781 | "seekable() -> bool. True if file supports random-access."); |
| 782 | |
| 783 | PyDoc_STRVAR(readable_doc, |
| 784 | "readable() -> bool. True if file was opened in a read mode."); |
| 785 | |
| 786 | PyDoc_STRVAR(writable_doc, |
| 787 | "writable() -> bool. True if file was opened in a write mode."); |
| 788 | |
| 789 | static PyMethodDef fileio_methods[] = { |
| 790 | {"read", (PyCFunction)fileio_read, METH_VARARGS, read_doc}, |
Guido van Rossum | 7165cb1 | 2007-07-10 06:54:34 +0000 | [diff] [blame] | 791 | {"readall", (PyCFunction)fileio_readall, METH_NOARGS, readall_doc}, |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 792 | {"readinto", (PyCFunction)fileio_readinto, METH_VARARGS, readinto_doc}, |
| 793 | {"write", (PyCFunction)fileio_write, METH_VARARGS, write_doc}, |
| 794 | {"seek", (PyCFunction)fileio_seek, METH_VARARGS, seek_doc}, |
| 795 | {"tell", (PyCFunction)fileio_tell, METH_VARARGS, tell_doc}, |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 796 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 797 | {"truncate", (PyCFunction)fileio_truncate, METH_VARARGS, truncate_doc}, |
Thomas Heller | c6a55ee | 2007-07-11 12:45:46 +0000 | [diff] [blame] | 798 | #endif |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 799 | {"close", (PyCFunction)fileio_close, METH_NOARGS, close_doc}, |
| 800 | {"seekable", (PyCFunction)fileio_seekable, METH_NOARGS, seekable_doc}, |
| 801 | {"readable", (PyCFunction)fileio_readable, METH_NOARGS, readable_doc}, |
| 802 | {"writable", (PyCFunction)fileio_writable, METH_NOARGS, writable_doc}, |
| 803 | {"fileno", (PyCFunction)fileio_fileno, METH_NOARGS, fileno_doc}, |
| 804 | {"isatty", (PyCFunction)fileio_isatty, METH_NOARGS, isatty_doc}, |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 805 | {NULL, NULL} /* sentinel */ |
| 806 | }; |
| 807 | |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 808 | /* 'closed' and 'mode' are attributes for backwards compatibility reasons. */ |
| 809 | |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 810 | static PyObject * |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 811 | get_closed(PyFileIOObject *self, void *closure) |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 812 | { |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 813 | return PyBool_FromLong((long)(self->fd < 0)); |
| 814 | } |
| 815 | |
| 816 | static PyObject * |
| 817 | get_mode(PyFileIOObject *self, void *closure) |
| 818 | { |
Guido van Rossum | c43e79f | 2007-06-18 18:26:36 +0000 | [diff] [blame] | 819 | return PyUnicode_FromString(mode_string(self)); |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | static PyGetSetDef fileio_getsetlist[] = { |
| 823 | {"closed", (getter)get_closed, NULL, "True if the file is closed"}, |
Guido van Rossum | 53807da | 2007-04-10 19:01:47 +0000 | [diff] [blame] | 824 | {"mode", (getter)get_mode, NULL, "String giving the file mode"}, |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 825 | {0}, |
| 826 | }; |
| 827 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 828 | PyTypeObject PyFileIO_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 829 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Amaury Forgeot d'Arc | 1ff9910 | 2007-11-19 20:34:10 +0000 | [diff] [blame] | 830 | "_FileIO", |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 831 | sizeof(PyFileIOObject), |
| 832 | 0, |
| 833 | (destructor)fileio_dealloc, /* tp_dealloc */ |
| 834 | 0, /* tp_print */ |
| 835 | 0, /* tp_getattr */ |
| 836 | 0, /* tp_setattr */ |
| 837 | 0, /* tp_compare */ |
| 838 | (reprfunc)fileio_repr, /* tp_repr */ |
| 839 | 0, /* tp_as_number */ |
| 840 | 0, /* tp_as_sequence */ |
| 841 | 0, /* tp_as_mapping */ |
| 842 | 0, /* tp_hash */ |
| 843 | 0, /* tp_call */ |
| 844 | 0, /* tp_str */ |
| 845 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 846 | 0, /* tp_setattro */ |
| 847 | 0, /* tp_as_buffer */ |
| 848 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 849 | fileio_doc, /* tp_doc */ |
| 850 | 0, /* tp_traverse */ |
| 851 | 0, /* tp_clear */ |
| 852 | 0, /* tp_richcompare */ |
| 853 | offsetof(PyFileIOObject, weakreflist), /* tp_weaklistoffset */ |
| 854 | 0, /* tp_iter */ |
| 855 | 0, /* tp_iternext */ |
| 856 | fileio_methods, /* tp_methods */ |
| 857 | 0, /* tp_members */ |
Guido van Rossum | b042815 | 2007-04-08 17:44:42 +0000 | [diff] [blame] | 858 | fileio_getsetlist, /* tp_getset */ |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 859 | 0, /* tp_base */ |
| 860 | 0, /* tp_dict */ |
| 861 | 0, /* tp_descr_get */ |
| 862 | 0, /* tp_descr_set */ |
| 863 | 0, /* tp_dictoffset */ |
| 864 | fileio_init, /* tp_init */ |
| 865 | PyType_GenericAlloc, /* tp_alloc */ |
| 866 | fileio_new, /* tp_new */ |
| 867 | PyObject_Del, /* tp_free */ |
| 868 | }; |
| 869 | |
| 870 | static PyMethodDef module_methods[] = { |
| 871 | {NULL, NULL} |
| 872 | }; |
| 873 | |
| 874 | PyMODINIT_FUNC |
| 875 | init_fileio(void) |
| 876 | { |
| 877 | PyObject *m; /* a module object */ |
| 878 | |
| 879 | m = Py_InitModule3("_fileio", module_methods, |
| 880 | "Fast implementation of io.FileIO."); |
| 881 | if (m == NULL) |
| 882 | return; |
| 883 | if (PyType_Ready(&PyFileIO_Type) < 0) |
| 884 | return; |
| 885 | Py_INCREF(&PyFileIO_Type); |
| 886 | PyModule_AddObject(m, "_FileIO", (PyObject *) &PyFileIO_Type); |
| 887 | } |