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