~ubuntu-branches/ubuntu/trusty/python3.4/trusty-proposed

« back to all changes in this revision

Viewing changes to Modules/_io/_iomodule.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-11-25 09:44:27 UTC
  • Revision ID: package-import@ubuntu.com-20131125094427-lzxj8ap5w01lmo7f
Tags: upstream-3.4~b1
ImportĀ upstreamĀ versionĀ 3.4~b1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    An implementation of the new I/O lib as defined by PEP 3116 - "New I/O"
 
3
 
 
4
    Classes defined here: UnsupportedOperation, BlockingIOError.
 
5
    Functions defined here: open().
 
6
 
 
7
    Mostly written by Amaury Forgeot d'Arc
 
8
*/
 
9
 
 
10
#define PY_SSIZE_T_CLEAN
 
11
#include "Python.h"
 
12
#include "structmember.h"
 
13
#include "_iomodule.h"
 
14
 
 
15
#ifdef HAVE_SYS_TYPES_H
 
16
#include <sys/types.h>
 
17
#endif /* HAVE_SYS_TYPES_H */
 
18
 
 
19
#ifdef HAVE_SYS_STAT_H
 
20
#include <sys/stat.h>
 
21
#endif /* HAVE_SYS_STAT_H */
 
22
 
 
23
 
 
24
/* Various interned strings */
 
25
 
 
26
PyObject *_PyIO_str_close;
 
27
PyObject *_PyIO_str_closed;
 
28
PyObject *_PyIO_str_decode;
 
29
PyObject *_PyIO_str_encode;
 
30
PyObject *_PyIO_str_fileno;
 
31
PyObject *_PyIO_str_flush;
 
32
PyObject *_PyIO_str_getstate;
 
33
PyObject *_PyIO_str_isatty;
 
34
PyObject *_PyIO_str_newlines;
 
35
PyObject *_PyIO_str_nl;
 
36
PyObject *_PyIO_str_read;
 
37
PyObject *_PyIO_str_read1;
 
38
PyObject *_PyIO_str_readable;
 
39
PyObject *_PyIO_str_readall;
 
40
PyObject *_PyIO_str_readinto;
 
41
PyObject *_PyIO_str_readline;
 
42
PyObject *_PyIO_str_reset;
 
43
PyObject *_PyIO_str_seek;
 
44
PyObject *_PyIO_str_seekable;
 
45
PyObject *_PyIO_str_setstate;
 
46
PyObject *_PyIO_str_tell;
 
47
PyObject *_PyIO_str_truncate;
 
48
PyObject *_PyIO_str_writable;
 
49
PyObject *_PyIO_str_write;
 
50
 
 
51
PyObject *_PyIO_empty_str;
 
52
PyObject *_PyIO_empty_bytes;
 
53
PyObject *_PyIO_zero;
 
54
 
 
55
 
 
56
PyDoc_STRVAR(module_doc,
 
57
"The io module provides the Python interfaces to stream handling. The\n"
 
58
"builtin open function is defined in this module.\n"
 
59
"\n"
 
60
"At the top of the I/O hierarchy is the abstract base class IOBase. It\n"
 
61
"defines the basic interface to a stream. Note, however, that there is no\n"
 
62
"separation between reading and writing to streams; implementations are\n"
 
63
"allowed to raise an IOError if they do not support a given operation.\n"
 
64
"\n"
 
65
"Extending IOBase is RawIOBase which deals simply with the reading and\n"
 
66
"writing of raw bytes to a stream. FileIO subclasses RawIOBase to provide\n"
 
67
"an interface to OS files.\n"
 
68
"\n"
 
69
"BufferedIOBase deals with buffering on a raw byte stream (RawIOBase). Its\n"
 
70
"subclasses, BufferedWriter, BufferedReader, and BufferedRWPair buffer\n"
 
71
"streams that are readable, writable, and both respectively.\n"
 
72
"BufferedRandom provides a buffered interface to random access\n"
 
73
"streams. BytesIO is a simple stream of in-memory bytes.\n"
 
74
"\n"
 
75
"Another IOBase subclass, TextIOBase, deals with the encoding and decoding\n"
 
76
"of streams into text. TextIOWrapper, which extends it, is a buffered text\n"
 
77
"interface to a buffered raw stream (`BufferedIOBase`). Finally, StringIO\n"
 
78
"is a in-memory stream for text.\n"
 
79
"\n"
 
80
"Argument names are not part of the specification, and only the arguments\n"
 
81
"of open() are intended to be used as keyword arguments.\n"
 
82
"\n"
 
83
"data:\n"
 
84
"\n"
 
85
"DEFAULT_BUFFER_SIZE\n"
 
86
"\n"
 
87
"   An int containing the default buffer size used by the module's buffered\n"
 
88
"   I/O classes. open() uses the file's blksize (as obtained by os.stat) if\n"
 
89
"   possible.\n"
 
90
    );
 
91
 
 
92
 
 
93
/*
 
94
 * The main open() function
 
95
 */
 
96
PyDoc_STRVAR(open_doc,
 
97
"open(file, mode='r', buffering=-1, encoding=None,\n"
 
98
"     errors=None, newline=None, closefd=True, opener=None) -> file object\n"
 
99
"\n"
 
100
"Open file and return a stream.  Raise IOError upon failure.\n"
 
101
"\n"
 
102
"file is either a text or byte string giving the name (and the path\n"
 
103
"if the file isn't in the current working directory) of the file to\n"
 
104
"be opened or an integer file descriptor of the file to be\n"
 
105
"wrapped. (If a file descriptor is given, it is closed when the\n"
 
106
"returned I/O object is closed, unless closefd is set to False.)\n"
 
107
"\n"
 
108
"mode is an optional string that specifies the mode in which the file\n"
 
109
"is opened. It defaults to 'r' which means open for reading in text\n"
 
110
"mode.  Other common values are 'w' for writing (truncating the file if\n"
 
111
"it already exists), 'x' for creating and writing to a new file, and\n"
 
112
"'a' for appending (which on some Unix systems, means that all writes\n"
 
113
"append to the end of the file regardless of the current seek position).\n"
 
114
"In text mode, if encoding is not specified the encoding used is platform\n"
 
115
"dependent: locale.getpreferredencoding(False) is called to get the\n"
 
116
"current locale encoding. (For reading and writing raw bytes use binary\n"
 
117
"mode and leave encoding unspecified.) The available modes are:\n"
 
118
"\n"
 
119
"========= ===============================================================\n"
 
120
"Character Meaning\n"
 
121
"--------- ---------------------------------------------------------------\n"
 
122
"'r'       open for reading (default)\n"
 
123
"'w'       open for writing, truncating the file first\n"
 
124
"'x'       create a new file and open it for writing\n"
 
125
"'a'       open for writing, appending to the end of the file if it exists\n"
 
126
"'b'       binary mode\n"
 
127
"'t'       text mode (default)\n"
 
128
"'+'       open a disk file for updating (reading and writing)\n"
 
129
"'U'       universal newline mode (deprecated)\n"
 
130
"========= ===============================================================\n"
 
131
"\n"
 
132
"The default mode is 'rt' (open for reading text). For binary random\n"
 
133
"access, the mode 'w+b' opens and truncates the file to 0 bytes, while\n"
 
134
"'r+b' opens the file without truncation. The 'x' mode implies 'w' and\n"
 
135
"raises an `FileExistsError` if the file already exists.\n"
 
136
"\n"
 
137
"Python distinguishes between files opened in binary and text modes,\n"
 
138
"even when the underlying operating system doesn't. Files opened in\n"
 
139
"binary mode (appending 'b' to the mode argument) return contents as\n"
 
140
"bytes objects without any decoding. In text mode (the default, or when\n"
 
141
"'t' is appended to the mode argument), the contents of the file are\n"
 
142
"returned as strings, the bytes having been first decoded using a\n"
 
143
"platform-dependent encoding or using the specified encoding if given.\n"
 
144
"\n"
 
145
"'U' mode is deprecated and will raise an exception in future versions\n"
 
146
"of Python.  It has no effect in Python 3.  Use newline to control\n"
 
147
"universal newlines mode.\n"
 
148
"\n"
 
149
"buffering is an optional integer used to set the buffering policy.\n"
 
150
"Pass 0 to switch buffering off (only allowed in binary mode), 1 to select\n"
 
151
"line buffering (only usable in text mode), and an integer > 1 to indicate\n"
 
152
"the size of a fixed-size chunk buffer.  When no buffering argument is\n"
 
153
"given, the default buffering policy works as follows:\n"
 
154
"\n"
 
155
"* Binary files are buffered in fixed-size chunks; the size of the buffer\n"
 
156
"  is chosen using a heuristic trying to determine the underlying device's\n"
 
157
"  \"block size\" and falling back on `io.DEFAULT_BUFFER_SIZE`.\n"
 
158
"  On many systems, the buffer will typically be 4096 or 8192 bytes long.\n"
 
159
"\n"
 
160
"* \"Interactive\" text files (files for which isatty() returns True)\n"
 
161
"  use line buffering.  Other text files use the policy described above\n"
 
162
"  for binary files.\n"
 
163
"\n"
 
164
"encoding is the name of the encoding used to decode or encode the\n"
 
165
"file. This should only be used in text mode. The default encoding is\n"
 
166
"platform dependent, but any encoding supported by Python can be\n"
 
167
"passed.  See the codecs module for the list of supported encodings.\n"
 
168
"\n"
 
169
"errors is an optional string that specifies how encoding errors are to\n"
 
170
"be handled---this argument should not be used in binary mode. Pass\n"
 
171
"'strict' to raise a ValueError exception if there is an encoding error\n"
 
172
"(the default of None has the same effect), or pass 'ignore' to ignore\n"
 
173
"errors. (Note that ignoring encoding errors can lead to data loss.)\n"
 
174
"See the documentation for codecs.register or run 'help(codecs.Codec)'\n"
 
175
"for a list of the permitted encoding error strings.\n"
 
176
"\n"
 
177
"newline controls how universal newlines works (it only applies to text\n"
 
178
"mode). It can be None, '', '\\n', '\\r', and '\\r\\n'.  It works as\n"
 
179
"follows:\n"
 
180
"\n"
 
181
"* On input, if newline is None, universal newlines mode is\n"
 
182
"  enabled. Lines in the input can end in '\\n', '\\r', or '\\r\\n', and\n"
 
183
"  these are translated into '\\n' before being returned to the\n"
 
184
"  caller. If it is '', universal newline mode is enabled, but line\n"
 
185
"  endings are returned to the caller untranslated. If it has any of\n"
 
186
"  the other legal values, input lines are only terminated by the given\n"
 
187
"  string, and the line ending is returned to the caller untranslated.\n"
 
188
"\n"
 
189
"* On output, if newline is None, any '\\n' characters written are\n"
 
190
"  translated to the system default line separator, os.linesep. If\n"
 
191
"  newline is '' or '\\n', no translation takes place. If newline is any\n"
 
192
"  of the other legal values, any '\\n' characters written are translated\n"
 
193
"  to the given string.\n"
 
194
"\n"
 
195
"If closefd is False, the underlying file descriptor will be kept open\n"
 
196
"when the file is closed. This does not work when a file name is given\n"
 
197
"and must be True in that case.\n"
 
198
"\n"
 
199
"A custom opener can be used by passing a callable as *opener*. The\n"
 
200
"underlying file descriptor for the file object is then obtained by\n"
 
201
"calling *opener* with (*file*, *flags*). *opener* must return an open\n"
 
202
"file descriptor (passing os.open as *opener* results in functionality\n"
 
203
"similar to passing None).\n"
 
204
"\n"
 
205
"open() returns a file object whose type depends on the mode, and\n"
 
206
"through which the standard file operations such as reading and writing\n"
 
207
"are performed. When open() is used to open a file in a text mode ('w',\n"
 
208
"'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open\n"
 
209
"a file in a binary mode, the returned class varies: in read binary\n"
 
210
"mode, it returns a BufferedReader; in write binary and append binary\n"
 
211
"modes, it returns a BufferedWriter, and in read/write mode, it returns\n"
 
212
"a BufferedRandom.\n"
 
213
"\n"
 
214
"It is also possible to use a string or bytearray as a file for both\n"
 
215
"reading and writing. For strings StringIO can be used like a file\n"
 
216
"opened in a text mode, and for bytes a BytesIO can be used like a file\n"
 
217
"opened in a binary mode.\n"
 
218
    );
 
219
 
 
220
static PyObject *
 
221
io_open(PyObject *self, PyObject *args, PyObject *kwds)
 
222
{
 
223
    char *kwlist[] = {"file", "mode", "buffering",
 
224
                      "encoding", "errors", "newline",
 
225
                      "closefd", "opener", NULL};
 
226
    PyObject *file, *opener = Py_None;
 
227
    char *mode = "r";
 
228
    int buffering = -1, closefd = 1;
 
229
    char *encoding = NULL, *errors = NULL, *newline = NULL;
 
230
    unsigned i;
 
231
 
 
232
    int creating = 0, reading = 0, writing = 0, appending = 0, updating = 0;
 
233
    int text = 0, binary = 0, universal = 0;
 
234
 
 
235
    char rawmode[6], *m;
 
236
    int line_buffering, isatty;
 
237
 
 
238
    PyObject *raw, *modeobj = NULL, *buffer = NULL, *wrapper = NULL;
 
239
 
 
240
    _Py_IDENTIFIER(isatty);
 
241
    _Py_IDENTIFIER(fileno);
 
242
    _Py_IDENTIFIER(mode);
 
243
 
 
244
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|sizzziO:open", kwlist,
 
245
                                     &file, &mode, &buffering,
 
246
                                     &encoding, &errors, &newline,
 
247
                                     &closefd, &opener)) {
 
248
        return NULL;
 
249
    }
 
250
 
 
251
    if (!PyUnicode_Check(file) &&
 
252
        !PyBytes_Check(file) &&
 
253
        !PyNumber_Check(file)) {
 
254
        PyErr_Format(PyExc_TypeError, "invalid file: %R", file);
 
255
        return NULL;
 
256
    }
 
257
 
 
258
    /* Decode mode */
 
259
    for (i = 0; i < strlen(mode); i++) {
 
260
        char c = mode[i];
 
261
 
 
262
        switch (c) {
 
263
        case 'x':
 
264
            creating = 1;
 
265
            break;
 
266
        case 'r':
 
267
            reading = 1;
 
268
            break;
 
269
        case 'w':
 
270
            writing = 1;
 
271
            break;
 
272
        case 'a':
 
273
            appending = 1;
 
274
            break;
 
275
        case '+':
 
276
            updating = 1;
 
277
            break;
 
278
        case 't':
 
279
            text = 1;
 
280
            break;
 
281
        case 'b':
 
282
            binary = 1;
 
283
            break;
 
284
        case 'U':
 
285
            universal = 1;
 
286
            reading = 1;
 
287
            break;
 
288
        default:
 
289
            goto invalid_mode;
 
290
        }
 
291
 
 
292
        /* c must not be duplicated */
 
293
        if (strchr(mode+i+1, c)) {
 
294
          invalid_mode:
 
295
            PyErr_Format(PyExc_ValueError, "invalid mode: '%s'", mode);
 
296
            return NULL;
 
297
        }
 
298
 
 
299
    }
 
300
 
 
301
    m = rawmode;
 
302
    if (creating)  *(m++) = 'x';
 
303
    if (reading)   *(m++) = 'r';
 
304
    if (writing)   *(m++) = 'w';
 
305
    if (appending) *(m++) = 'a';
 
306
    if (updating)  *(m++) = '+';
 
307
    *m = '\0';
 
308
 
 
309
    /* Parameters validation */
 
310
    if (universal) {
 
311
        if (writing || appending) {
 
312
            PyErr_SetString(PyExc_ValueError,
 
313
                            "can't use U and writing mode at once");
 
314
            return NULL;
 
315
        }
 
316
        if (PyErr_WarnEx(PyExc_DeprecationWarning,
 
317
                         "'U' mode is deprecated", 1) < 0)
 
318
            return NULL;
 
319
        reading = 1;
 
320
    }
 
321
 
 
322
    if (text && binary) {
 
323
        PyErr_SetString(PyExc_ValueError,
 
324
                        "can't have text and binary mode at once");
 
325
        return NULL;
 
326
    }
 
327
 
 
328
    if (creating + reading + writing + appending > 1) {
 
329
        PyErr_SetString(PyExc_ValueError,
 
330
                        "must have exactly one of create/read/write/append mode");
 
331
        return NULL;
 
332
    }
 
333
 
 
334
    if (binary && encoding != NULL) {
 
335
        PyErr_SetString(PyExc_ValueError,
 
336
                        "binary mode doesn't take an encoding argument");
 
337
        return NULL;
 
338
    }
 
339
 
 
340
    if (binary && errors != NULL) {
 
341
        PyErr_SetString(PyExc_ValueError,
 
342
                        "binary mode doesn't take an errors argument");
 
343
        return NULL;
 
344
    }
 
345
 
 
346
    if (binary && newline != NULL) {
 
347
        PyErr_SetString(PyExc_ValueError,
 
348
                        "binary mode doesn't take a newline argument");
 
349
        return NULL;
 
350
    }
 
351
 
 
352
    /* Create the Raw file stream */
 
353
    raw = PyObject_CallFunction((PyObject *)&PyFileIO_Type,
 
354
                                "OsiO", file, rawmode, closefd, opener);
 
355
    if (raw == NULL)
 
356
        return NULL;
 
357
 
 
358
    modeobj = PyUnicode_FromString(mode);
 
359
    if (modeobj == NULL)
 
360
        goto error;
 
361
 
 
362
    /* buffering */
 
363
    {
 
364
        PyObject *res = _PyObject_CallMethodId(raw, &PyId_isatty, NULL);
 
365
        if (res == NULL)
 
366
            goto error;
 
367
        isatty = PyLong_AsLong(res);
 
368
        Py_DECREF(res);
 
369
        if (isatty == -1 && PyErr_Occurred())
 
370
            goto error;
 
371
    }
 
372
 
 
373
    if (buffering == 1 || (buffering < 0 && isatty)) {
 
374
        buffering = -1;
 
375
        line_buffering = 1;
 
376
    }
 
377
    else
 
378
        line_buffering = 0;
 
379
 
 
380
    if (buffering < 0) {
 
381
        buffering = DEFAULT_BUFFER_SIZE;
 
382
#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
 
383
        {
 
384
            struct stat st;
 
385
            long fileno;
 
386
            PyObject *res = _PyObject_CallMethodId(raw, &PyId_fileno, NULL);
 
387
            if (res == NULL)
 
388
                goto error;
 
389
 
 
390
            fileno = PyLong_AsLong(res);
 
391
            Py_DECREF(res);
 
392
            if (fileno == -1 && PyErr_Occurred())
 
393
                goto error;
 
394
 
 
395
            if (fstat(fileno, &st) >= 0 && st.st_blksize > 1)
 
396
                buffering = st.st_blksize;
 
397
        }
 
398
#endif
 
399
    }
 
400
    if (buffering < 0) {
 
401
        PyErr_SetString(PyExc_ValueError,
 
402
                        "invalid buffering size");
 
403
        goto error;
 
404
    }
 
405
 
 
406
    /* if not buffering, returns the raw file object */
 
407
    if (buffering == 0) {
 
408
        if (!binary) {
 
409
            PyErr_SetString(PyExc_ValueError,
 
410
                            "can't have unbuffered text I/O");
 
411
            goto error;
 
412
        }
 
413
 
 
414
        Py_DECREF(modeobj);
 
415
        return raw;
 
416
    }
 
417
 
 
418
    /* wraps into a buffered file */
 
419
    {
 
420
        PyObject *Buffered_class;
 
421
 
 
422
        if (updating)
 
423
            Buffered_class = (PyObject *)&PyBufferedRandom_Type;
 
424
        else if (creating || writing || appending)
 
425
            Buffered_class = (PyObject *)&PyBufferedWriter_Type;
 
426
        else if (reading)
 
427
            Buffered_class = (PyObject *)&PyBufferedReader_Type;
 
428
        else {
 
429
            PyErr_Format(PyExc_ValueError,
 
430
                         "unknown mode: '%s'", mode);
 
431
            goto error;
 
432
        }
 
433
 
 
434
        buffer = PyObject_CallFunction(Buffered_class, "Oi", raw, buffering);
 
435
    }
 
436
    Py_CLEAR(raw);
 
437
    if (buffer == NULL)
 
438
        goto error;
 
439
 
 
440
 
 
441
    /* if binary, returns the buffered file */
 
442
    if (binary) {
 
443
        Py_DECREF(modeobj);
 
444
        return buffer;
 
445
    }
 
446
 
 
447
    /* wraps into a TextIOWrapper */
 
448
    wrapper = PyObject_CallFunction((PyObject *)&PyTextIOWrapper_Type,
 
449
                                    "Osssi",
 
450
                                    buffer,
 
451
                                    encoding, errors, newline,
 
452
                                    line_buffering);
 
453
    Py_CLEAR(buffer);
 
454
    if (wrapper == NULL)
 
455
        goto error;
 
456
 
 
457
    if (_PyObject_SetAttrId(wrapper, &PyId_mode, modeobj) < 0)
 
458
        goto error;
 
459
    Py_DECREF(modeobj);
 
460
    return wrapper;
 
461
 
 
462
  error:
 
463
    Py_XDECREF(raw);
 
464
    Py_XDECREF(modeobj);
 
465
    Py_XDECREF(buffer);
 
466
    Py_XDECREF(wrapper);
 
467
    return NULL;
 
468
}
 
469
 
 
470
/*
 
471
 * Private helpers for the io module.
 
472
 */
 
473
 
 
474
Py_off_t
 
475
PyNumber_AsOff_t(PyObject *item, PyObject *err)
 
476
{
 
477
    Py_off_t result;
 
478
    PyObject *runerr;
 
479
    PyObject *value = PyNumber_Index(item);
 
480
    if (value == NULL)
 
481
        return -1;
 
482
 
 
483
    /* We're done if PyLong_AsSsize_t() returns without error. */
 
484
    result = PyLong_AsOff_t(value);
 
485
    if (result != -1 || !(runerr = PyErr_Occurred()))
 
486
        goto finish;
 
487
 
 
488
    /* Error handling code -- only manage OverflowError differently */
 
489
    if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError))
 
490
        goto finish;
 
491
 
 
492
    PyErr_Clear();
 
493
    /* If no error-handling desired then the default clipping
 
494
       is sufficient.
 
495
     */
 
496
    if (!err) {
 
497
        assert(PyLong_Check(value));
 
498
        /* Whether or not it is less than or equal to
 
499
           zero is determined by the sign of ob_size
 
500
        */
 
501
        if (_PyLong_Sign(value) < 0)
 
502
            result = PY_OFF_T_MIN;
 
503
        else
 
504
            result = PY_OFF_T_MAX;
 
505
    }
 
506
    else {
 
507
        /* Otherwise replace the error with caller's error object. */
 
508
        PyErr_Format(err,
 
509
                     "cannot fit '%.200s' into an offset-sized integer",
 
510
                     item->ob_type->tp_name);
 
511
    }
 
512
 
 
513
 finish:
 
514
    Py_DECREF(value);
 
515
    return result;
 
516
}
 
517
 
 
518
 
 
519
/* Basically the "n" format code with the ability to turn None into -1. */
 
520
int
 
521
_PyIO_ConvertSsize_t(PyObject *obj, void *result) {
 
522
    Py_ssize_t limit;
 
523
    if (obj == Py_None) {
 
524
        limit = -1;
 
525
    }
 
526
    else if (PyNumber_Check(obj)) {
 
527
        limit = PyNumber_AsSsize_t(obj, PyExc_OverflowError);
 
528
        if (limit == -1 && PyErr_Occurred())
 
529
            return 0;
 
530
    }
 
531
    else {
 
532
        PyErr_Format(PyExc_TypeError,
 
533
                     "integer argument expected, got '%.200s'",
 
534
                     Py_TYPE(obj)->tp_name);
 
535
        return 0;
 
536
    }
 
537
    *((Py_ssize_t *)result) = limit;
 
538
    return 1;
 
539
}
 
540
 
 
541
 
 
542
PyObject *
 
543
_PyIO_get_locale_module(_PyIO_State *state)
 
544
{
 
545
    PyObject *mod;
 
546
    if (state->locale_module != NULL) {
 
547
        assert(PyWeakref_CheckRef(state->locale_module));
 
548
        mod = PyWeakref_GET_OBJECT(state->locale_module);
 
549
        if (mod != Py_None) {
 
550
            Py_INCREF(mod);
 
551
            return mod;
 
552
        }
 
553
        Py_CLEAR(state->locale_module);
 
554
    }
 
555
    mod = PyImport_ImportModule("_bootlocale");
 
556
    if (mod == NULL)
 
557
        return NULL;
 
558
    state->locale_module = PyWeakref_NewRef(mod, NULL);
 
559
    if (state->locale_module == NULL) {
 
560
        Py_DECREF(mod);
 
561
        return NULL;
 
562
    }
 
563
    return mod;
 
564
}
 
565
 
 
566
 
 
567
static int
 
568
iomodule_traverse(PyObject *mod, visitproc visit, void *arg) {
 
569
    _PyIO_State *state = IO_MOD_STATE(mod);
 
570
    if (!state->initialized)
 
571
        return 0;
 
572
    if (state->locale_module != NULL) {
 
573
        Py_VISIT(state->locale_module);
 
574
    }
 
575
    Py_VISIT(state->unsupported_operation);
 
576
    return 0;
 
577
}
 
578
 
 
579
 
 
580
static int
 
581
iomodule_clear(PyObject *mod) {
 
582
    _PyIO_State *state = IO_MOD_STATE(mod);
 
583
    if (!state->initialized)
 
584
        return 0;
 
585
    if (state->locale_module != NULL)
 
586
        Py_CLEAR(state->locale_module);
 
587
    Py_CLEAR(state->unsupported_operation);
 
588
    return 0;
 
589
}
 
590
 
 
591
static void
 
592
iomodule_free(PyObject *mod) {
 
593
    iomodule_clear(mod);
 
594
}
 
595
 
 
596
 
 
597
/*
 
598
 * Module definition
 
599
 */
 
600
 
 
601
static PyMethodDef module_methods[] = {
 
602
    {"open", (PyCFunction)io_open, METH_VARARGS|METH_KEYWORDS, open_doc},
 
603
    {NULL, NULL}
 
604
};
 
605
 
 
606
struct PyModuleDef _PyIO_Module = {
 
607
    PyModuleDef_HEAD_INIT,
 
608
    "io",
 
609
    module_doc,
 
610
    sizeof(_PyIO_State),
 
611
    module_methods,
 
612
    NULL,
 
613
    iomodule_traverse,
 
614
    iomodule_clear,
 
615
    (freefunc)iomodule_free,
 
616
};
 
617
 
 
618
PyMODINIT_FUNC
 
619
PyInit__io(void)
 
620
{
 
621
    PyObject *m = PyModule_Create(&_PyIO_Module);
 
622
    _PyIO_State *state = NULL;
 
623
    if (m == NULL)
 
624
        return NULL;
 
625
    state = IO_MOD_STATE(m);
 
626
    state->initialized = 0;
 
627
 
 
628
#define ADD_TYPE(type, name) \
 
629
    if (PyType_Ready(type) < 0) \
 
630
        goto fail; \
 
631
    Py_INCREF(type); \
 
632
    if (PyModule_AddObject(m, name, (PyObject *)type) < 0) {  \
 
633
        Py_DECREF(type); \
 
634
        goto fail; \
 
635
    }
 
636
 
 
637
    /* DEFAULT_BUFFER_SIZE */
 
638
    if (PyModule_AddIntMacro(m, DEFAULT_BUFFER_SIZE) < 0)
 
639
        goto fail;
 
640
 
 
641
    /* UnsupportedOperation inherits from ValueError and IOError */
 
642
    state->unsupported_operation = PyObject_CallFunction(
 
643
        (PyObject *)&PyType_Type, "s(OO){}",
 
644
        "UnsupportedOperation", PyExc_ValueError, PyExc_IOError);
 
645
    if (state->unsupported_operation == NULL)
 
646
        goto fail;
 
647
    Py_INCREF(state->unsupported_operation);
 
648
    if (PyModule_AddObject(m, "UnsupportedOperation",
 
649
                           state->unsupported_operation) < 0)
 
650
        goto fail;
 
651
 
 
652
    /* BlockingIOError, for compatibility */
 
653
    Py_INCREF(PyExc_BlockingIOError);
 
654
    if (PyModule_AddObject(m, "BlockingIOError",
 
655
                           (PyObject *) PyExc_BlockingIOError) < 0)
 
656
        goto fail;
 
657
 
 
658
    /* Concrete base types of the IO ABCs.
 
659
       (the ABCs themselves are declared through inheritance in io.py)
 
660
    */
 
661
    ADD_TYPE(&PyIOBase_Type, "_IOBase");
 
662
    ADD_TYPE(&PyRawIOBase_Type, "_RawIOBase");
 
663
    ADD_TYPE(&PyBufferedIOBase_Type, "_BufferedIOBase");
 
664
    ADD_TYPE(&PyTextIOBase_Type, "_TextIOBase");
 
665
 
 
666
    /* Implementation of concrete IO objects. */
 
667
    /* FileIO */
 
668
    PyFileIO_Type.tp_base = &PyRawIOBase_Type;
 
669
    ADD_TYPE(&PyFileIO_Type, "FileIO");
 
670
 
 
671
    /* BytesIO */
 
672
    PyBytesIO_Type.tp_base = &PyBufferedIOBase_Type;
 
673
    ADD_TYPE(&PyBytesIO_Type, "BytesIO");
 
674
    if (PyType_Ready(&_PyBytesIOBuffer_Type) < 0)
 
675
        goto fail;
 
676
 
 
677
    /* StringIO */
 
678
    PyStringIO_Type.tp_base = &PyTextIOBase_Type;
 
679
    ADD_TYPE(&PyStringIO_Type, "StringIO");
 
680
 
 
681
    /* BufferedReader */
 
682
    PyBufferedReader_Type.tp_base = &PyBufferedIOBase_Type;
 
683
    ADD_TYPE(&PyBufferedReader_Type, "BufferedReader");
 
684
 
 
685
    /* BufferedWriter */
 
686
    PyBufferedWriter_Type.tp_base = &PyBufferedIOBase_Type;
 
687
    ADD_TYPE(&PyBufferedWriter_Type, "BufferedWriter");
 
688
 
 
689
    /* BufferedRWPair */
 
690
    PyBufferedRWPair_Type.tp_base = &PyBufferedIOBase_Type;
 
691
    ADD_TYPE(&PyBufferedRWPair_Type, "BufferedRWPair");
 
692
 
 
693
    /* BufferedRandom */
 
694
    PyBufferedRandom_Type.tp_base = &PyBufferedIOBase_Type;
 
695
    ADD_TYPE(&PyBufferedRandom_Type, "BufferedRandom");
 
696
 
 
697
    /* TextIOWrapper */
 
698
    PyTextIOWrapper_Type.tp_base = &PyTextIOBase_Type;
 
699
    ADD_TYPE(&PyTextIOWrapper_Type, "TextIOWrapper");
 
700
 
 
701
    /* IncrementalNewlineDecoder */
 
702
    ADD_TYPE(&PyIncrementalNewlineDecoder_Type, "IncrementalNewlineDecoder");
 
703
 
 
704
    /* Interned strings */
 
705
#define ADD_INTERNED(name) \
 
706
    if (!_PyIO_str_ ## name && \
 
707
        !(_PyIO_str_ ## name = PyUnicode_InternFromString(# name))) \
 
708
        goto fail;
 
709
 
 
710
    ADD_INTERNED(close)
 
711
    ADD_INTERNED(closed)
 
712
    ADD_INTERNED(decode)
 
713
    ADD_INTERNED(encode)
 
714
    ADD_INTERNED(fileno)
 
715
    ADD_INTERNED(flush)
 
716
    ADD_INTERNED(getstate)
 
717
    ADD_INTERNED(isatty)
 
718
    ADD_INTERNED(newlines)
 
719
    ADD_INTERNED(read)
 
720
    ADD_INTERNED(read1)
 
721
    ADD_INTERNED(readable)
 
722
    ADD_INTERNED(readall)
 
723
    ADD_INTERNED(readinto)
 
724
    ADD_INTERNED(readline)
 
725
    ADD_INTERNED(reset)
 
726
    ADD_INTERNED(seek)
 
727
    ADD_INTERNED(seekable)
 
728
    ADD_INTERNED(setstate)
 
729
    ADD_INTERNED(tell)
 
730
    ADD_INTERNED(truncate)
 
731
    ADD_INTERNED(write)
 
732
    ADD_INTERNED(writable)
 
733
 
 
734
    if (!_PyIO_str_nl &&
 
735
        !(_PyIO_str_nl = PyUnicode_InternFromString("\n")))
 
736
        goto fail;
 
737
 
 
738
    if (!_PyIO_empty_str &&
 
739
        !(_PyIO_empty_str = PyUnicode_FromStringAndSize(NULL, 0)))
 
740
        goto fail;
 
741
    if (!_PyIO_empty_bytes &&
 
742
        !(_PyIO_empty_bytes = PyBytes_FromStringAndSize(NULL, 0)))
 
743
        goto fail;
 
744
    if (!_PyIO_zero &&
 
745
        !(_PyIO_zero = PyLong_FromLong(0L)))
 
746
        goto fail;
 
747
 
 
748
    state->initialized = 1;
 
749
 
 
750
    return m;
 
751
 
 
752
  fail:
 
753
    Py_XDECREF(state->unsupported_operation);
 
754
    Py_DECREF(m);
 
755
    return NULL;
 
756
}