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