• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*[clinic input]
2 preserve
3 [clinic start generated code]*/
4 
5 PyDoc_STRVAR(_io_open__doc__,
6 "open($module, /, file, mode=\'r\', buffering=-1, encoding=None,\n"
7 "     errors=None, newline=None, closefd=True, opener=None)\n"
8 "--\n"
9 "\n"
10 "Open file and return a stream.  Raise OSError upon failure.\n"
11 "\n"
12 "file is either a text or byte string giving the name (and the path\n"
13 "if the file isn\'t in the current working directory) of the file to\n"
14 "be opened or an integer file descriptor of the file to be\n"
15 "wrapped. (If a file descriptor is given, it is closed when the\n"
16 "returned I/O object is closed, unless closefd is set to False.)\n"
17 "\n"
18 "mode is an optional string that specifies the mode in which the file\n"
19 "is opened. It defaults to \'r\' which means open for reading in text\n"
20 "mode.  Other common values are \'w\' for writing (truncating the file if\n"
21 "it already exists), \'x\' for creating and writing to a new file, and\n"
22 "\'a\' for appending (which on some Unix systems, means that all writes\n"
23 "append to the end of the file regardless of the current seek position).\n"
24 "In text mode, if encoding is not specified the encoding used is platform\n"
25 "dependent: locale.getpreferredencoding(False) is called to get the\n"
26 "current locale encoding. (For reading and writing raw bytes use binary\n"
27 "mode and leave encoding unspecified.) The available modes are:\n"
28 "\n"
29 "========= ===============================================================\n"
30 "Character Meaning\n"
31 "--------- ---------------------------------------------------------------\n"
32 "\'r\'       open for reading (default)\n"
33 "\'w\'       open for writing, truncating the file first\n"
34 "\'x\'       create a new file and open it for writing\n"
35 "\'a\'       open for writing, appending to the end of the file if it exists\n"
36 "\'b\'       binary mode\n"
37 "\'t\'       text mode (default)\n"
38 "\'+\'       open a disk file for updating (reading and writing)\n"
39 "\'U\'       universal newline mode (deprecated)\n"
40 "========= ===============================================================\n"
41 "\n"
42 "The default mode is \'rt\' (open for reading text). For binary random\n"
43 "access, the mode \'w+b\' opens and truncates the file to 0 bytes, while\n"
44 "\'r+b\' opens the file without truncation. The \'x\' mode implies \'w\' and\n"
45 "raises an `FileExistsError` if the file already exists.\n"
46 "\n"
47 "Python distinguishes between files opened in binary and text modes,\n"
48 "even when the underlying operating system doesn\'t. Files opened in\n"
49 "binary mode (appending \'b\' to the mode argument) return contents as\n"
50 "bytes objects without any decoding. In text mode (the default, or when\n"
51 "\'t\' is appended to the mode argument), the contents of the file are\n"
52 "returned as strings, the bytes having been first decoded using a\n"
53 "platform-dependent encoding or using the specified encoding if given.\n"
54 "\n"
55 "\'U\' mode is deprecated and will raise an exception in future versions\n"
56 "of Python.  It has no effect in Python 3.  Use newline to control\n"
57 "universal newlines mode.\n"
58 "\n"
59 "buffering is an optional integer used to set the buffering policy.\n"
60 "Pass 0 to switch buffering off (only allowed in binary mode), 1 to select\n"
61 "line buffering (only usable in text mode), and an integer > 1 to indicate\n"
62 "the size of a fixed-size chunk buffer.  When no buffering argument is\n"
63 "given, the default buffering policy works as follows:\n"
64 "\n"
65 "* Binary files are buffered in fixed-size chunks; the size of the buffer\n"
66 "  is chosen using a heuristic trying to determine the underlying device\'s\n"
67 "  \"block size\" and falling back on `io.DEFAULT_BUFFER_SIZE`.\n"
68 "  On many systems, the buffer will typically be 4096 or 8192 bytes long.\n"
69 "\n"
70 "* \"Interactive\" text files (files for which isatty() returns True)\n"
71 "  use line buffering.  Other text files use the policy described above\n"
72 "  for binary files.\n"
73 "\n"
74 "encoding is the name of the encoding used to decode or encode the\n"
75 "file. This should only be used in text mode. The default encoding is\n"
76 "platform dependent, but any encoding supported by Python can be\n"
77 "passed.  See the codecs module for the list of supported encodings.\n"
78 "\n"
79 "errors is an optional string that specifies how encoding errors are to\n"
80 "be handled---this argument should not be used in binary mode. Pass\n"
81 "\'strict\' to raise a ValueError exception if there is an encoding error\n"
82 "(the default of None has the same effect), or pass \'ignore\' to ignore\n"
83 "errors. (Note that ignoring encoding errors can lead to data loss.)\n"
84 "See the documentation for codecs.register or run \'help(codecs.Codec)\'\n"
85 "for a list of the permitted encoding error strings.\n"
86 "\n"
87 "newline controls how universal newlines works (it only applies to text\n"
88 "mode). It can be None, \'\', \'\\n\', \'\\r\', and \'\\r\\n\'.  It works as\n"
89 "follows:\n"
90 "\n"
91 "* On input, if newline is None, universal newlines mode is\n"
92 "  enabled. Lines in the input can end in \'\\n\', \'\\r\', or \'\\r\\n\', and\n"
93 "  these are translated into \'\\n\' before being returned to the\n"
94 "  caller. If it is \'\', universal newline mode is enabled, but line\n"
95 "  endings are returned to the caller untranslated. If it has any of\n"
96 "  the other legal values, input lines are only terminated by the given\n"
97 "  string, and the line ending is returned to the caller untranslated.\n"
98 "\n"
99 "* On output, if newline is None, any \'\\n\' characters written are\n"
100 "  translated to the system default line separator, os.linesep. If\n"
101 "  newline is \'\' or \'\\n\', no translation takes place. If newline is any\n"
102 "  of the other legal values, any \'\\n\' characters written are translated\n"
103 "  to the given string.\n"
104 "\n"
105 "If closefd is False, the underlying file descriptor will be kept open\n"
106 "when the file is closed. This does not work when a file name is given\n"
107 "and must be True in that case.\n"
108 "\n"
109 "A custom opener can be used by passing a callable as *opener*. The\n"
110 "underlying file descriptor for the file object is then obtained by\n"
111 "calling *opener* with (*file*, *flags*). *opener* must return an open\n"
112 "file descriptor (passing os.open as *opener* results in functionality\n"
113 "similar to passing None).\n"
114 "\n"
115 "open() returns a file object whose type depends on the mode, and\n"
116 "through which the standard file operations such as reading and writing\n"
117 "are performed. When open() is used to open a file in a text mode (\'w\',\n"
118 "\'r\', \'wt\', \'rt\', etc.), it returns a TextIOWrapper. When used to open\n"
119 "a file in a binary mode, the returned class varies: in read binary\n"
120 "mode, it returns a BufferedReader; in write binary and append binary\n"
121 "modes, it returns a BufferedWriter, and in read/write mode, it returns\n"
122 "a BufferedRandom.\n"
123 "\n"
124 "It is also possible to use a string or bytearray as a file for both\n"
125 "reading and writing. For strings StringIO can be used like a file\n"
126 "opened in a text mode, and for bytes a BytesIO can be used like a file\n"
127 "opened in a binary mode.");
128 
129 #define _IO_OPEN_METHODDEF    \
130     {"open", (PyCFunction)_io_open, METH_FASTCALL|METH_KEYWORDS, _io_open__doc__},
131 
132 static PyObject *
133 _io_open_impl(PyObject *module, PyObject *file, const char *mode,
134               int buffering, const char *encoding, const char *errors,
135               const char *newline, int closefd, PyObject *opener);
136 
137 static PyObject *
_io_open(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)138 _io_open(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
139 {
140     PyObject *return_value = NULL;
141     static const char * const _keywords[] = {"file", "mode", "buffering", "encoding", "errors", "newline", "closefd", "opener", NULL};
142     static _PyArg_Parser _parser = {"O|sizzziO:open", _keywords, 0};
143     PyObject *file;
144     const char *mode = "r";
145     int buffering = -1;
146     const char *encoding = NULL;
147     const char *errors = NULL;
148     const char *newline = NULL;
149     int closefd = 1;
150     PyObject *opener = Py_None;
151 
152     if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
153         &file, &mode, &buffering, &encoding, &errors, &newline, &closefd, &opener)) {
154         goto exit;
155     }
156     return_value = _io_open_impl(module, file, mode, buffering, encoding, errors, newline, closefd, opener);
157 
158 exit:
159     return return_value;
160 }
161 /*[clinic end generated code: output=a9de1ae79c960e81 input=a9049054013a1b77]*/
162