blob: 64368f356f3bee793216804cae087325b5a9c09a [file] [log] [blame]
Victor Stinner4e314432010-10-07 21:45:39 +00001#include "Python.h"
Stefan Krah6df5cae2012-11-12 20:14:36 +01002#include "osdefs.h"
Stefan Krah6c01e382014-01-20 15:31:08 +01003#include <locale.h>
4
Victor Stinnerb306d752010-10-07 22:09:40 +00005#ifdef MS_WINDOWS
Steve Dowerd81431f2015-03-06 14:47:02 -08006# include <malloc.h>
Victor Stinnerb306d752010-10-07 22:09:40 +00007# include <windows.h>
8#endif
Victor Stinner4e314432010-10-07 21:45:39 +00009
Brett Cannonefb00c02012-02-29 18:31:31 -050010#ifdef HAVE_LANGINFO_H
11#include <langinfo.h>
12#endif
13
Victor Stinnerdaf45552013-08-28 00:53:59 +020014#ifdef HAVE_SYS_IOCTL_H
15#include <sys/ioctl.h>
16#endif
17
18#ifdef HAVE_FCNTL_H
19#include <fcntl.h>
20#endif /* HAVE_FCNTL_H */
21
Victor Stinnere2623772012-11-12 23:04:02 +010022#ifdef __APPLE__
23extern wchar_t* _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size);
24#endif
25
Victor Stinnerdaf45552013-08-28 00:53:59 +020026#ifdef O_CLOEXEC
Victor Stinnerb034eee2013-09-07 10:36:04 +020027/* Does open() support the O_CLOEXEC flag? Possible values:
Victor Stinnerdaf45552013-08-28 00:53:59 +020028
29 -1: unknown
30 0: open() ignores O_CLOEXEC flag, ex: Linux kernel older than 2.6.23
31 1: open() supports O_CLOEXEC flag, close-on-exec is set
32
Victor Stinnera555cfc2015-03-18 00:22:14 +010033 The flag is used by _Py_open(), _Py_open_noraise(), io.FileIO
34 and os.open(). */
Victor Stinnerdaf45552013-08-28 00:53:59 +020035int _Py_open_cloexec_works = -1;
36#endif
37
Brett Cannonefb00c02012-02-29 18:31:31 -050038PyObject *
39_Py_device_encoding(int fd)
40{
Victor Stinner14b9b112013-06-25 00:37:25 +020041#if defined(MS_WINDOWS)
Brett Cannonefb00c02012-02-29 18:31:31 -050042 UINT cp;
43#endif
44 if (!_PyVerify_fd(fd) || !isatty(fd)) {
45 Py_RETURN_NONE;
46 }
Victor Stinner14b9b112013-06-25 00:37:25 +020047#if defined(MS_WINDOWS)
Brett Cannonefb00c02012-02-29 18:31:31 -050048 if (fd == 0)
49 cp = GetConsoleCP();
50 else if (fd == 1 || fd == 2)
51 cp = GetConsoleOutputCP();
52 else
53 cp = 0;
54 /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
55 has no console */
56 if (cp != 0)
57 return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
58#elif defined(CODESET)
59 {
60 char *codeset = nl_langinfo(CODESET);
61 if (codeset != NULL && codeset[0] != 0)
62 return PyUnicode_FromString(codeset);
63 }
64#endif
65 Py_RETURN_NONE;
66}
67
Victor Stinnerd45c7f82012-12-04 01:34:47 +010068#if !defined(__APPLE__) && !defined(MS_WINDOWS)
69extern int _Py_normalize_encoding(const char *, char *, size_t);
70
71/* Workaround FreeBSD and OpenIndiana locale encoding issue with the C locale.
72 On these operating systems, nl_langinfo(CODESET) announces an alias of the
73 ASCII encoding, whereas mbstowcs() and wcstombs() functions use the
74 ISO-8859-1 encoding. The problem is that os.fsencode() and os.fsdecode() use
75 locale.getpreferredencoding() codec. For example, if command line arguments
76 are decoded by mbstowcs() and encoded back by os.fsencode(), we get a
77 UnicodeEncodeError instead of retrieving the original byte string.
78
79 The workaround is enabled if setlocale(LC_CTYPE, NULL) returns "C",
80 nl_langinfo(CODESET) announces "ascii" (or an alias to ASCII), and at least
81 one byte in range 0x80-0xff can be decoded from the locale encoding. The
82 workaround is also enabled on error, for example if getting the locale
83 failed.
84
Philip Jenvey215c49a2013-01-15 13:24:12 -080085 Values of force_ascii:
Victor Stinnerd45c7f82012-12-04 01:34:47 +010086
Victor Stinnerf6a271a2014-08-01 12:28:48 +020087 1: the workaround is used: Py_EncodeLocale() uses
88 encode_ascii_surrogateescape() and Py_DecodeLocale() uses
Victor Stinnerd45c7f82012-12-04 01:34:47 +010089 decode_ascii_surrogateescape()
Victor Stinnerf6a271a2014-08-01 12:28:48 +020090 0: the workaround is not used: Py_EncodeLocale() uses wcstombs() and
91 Py_DecodeLocale() uses mbstowcs()
Victor Stinnerd45c7f82012-12-04 01:34:47 +010092 -1: unknown, need to call check_force_ascii() to get the value
93*/
94static int force_ascii = -1;
95
96static int
97check_force_ascii(void)
98{
99 char *loc;
100#if defined(HAVE_LANGINFO_H) && defined(CODESET)
101 char *codeset, **alias;
102 char encoding[100];
103 int is_ascii;
104 unsigned int i;
105 char* ascii_aliases[] = {
106 "ascii",
107 "646",
108 "ansi-x3.4-1968",
109 "ansi-x3-4-1968",
110 "ansi-x3.4-1986",
111 "cp367",
112 "csascii",
113 "ibm367",
114 "iso646-us",
115 "iso-646.irv-1991",
116 "iso-ir-6",
117 "us",
118 "us-ascii",
119 NULL
120 };
121#endif
122
123 loc = setlocale(LC_CTYPE, NULL);
124 if (loc == NULL)
125 goto error;
126 if (strcmp(loc, "C") != 0) {
127 /* the LC_CTYPE locale is different than C */
128 return 0;
129 }
130
131#if defined(HAVE_LANGINFO_H) && defined(CODESET)
132 codeset = nl_langinfo(CODESET);
133 if (!codeset || codeset[0] == '\0') {
134 /* CODESET is not set or empty */
135 goto error;
136 }
137 if (!_Py_normalize_encoding(codeset, encoding, sizeof(encoding)))
138 goto error;
139
140 is_ascii = 0;
141 for (alias=ascii_aliases; *alias != NULL; alias++) {
142 if (strcmp(encoding, *alias) == 0) {
143 is_ascii = 1;
144 break;
145 }
146 }
147 if (!is_ascii) {
148 /* nl_langinfo(CODESET) is not "ascii" or an alias of ASCII */
149 return 0;
150 }
151
152 for (i=0x80; i<0xff; i++) {
153 unsigned char ch;
154 wchar_t wch;
155 size_t res;
156
157 ch = (unsigned char)i;
158 res = mbstowcs(&wch, (char*)&ch, 1);
159 if (res != (size_t)-1) {
160 /* decoding a non-ASCII character from the locale encoding succeed:
161 the locale encoding is not ASCII, force ASCII */
162 return 1;
163 }
164 }
165 /* None of the bytes in the range 0x80-0xff can be decoded from the locale
166 encoding: the locale encoding is really ASCII */
167 return 0;
168#else
169 /* nl_langinfo(CODESET) is not available: always force ASCII */
170 return 1;
171#endif
172
173error:
174 /* if an error occured, force the ASCII encoding */
175 return 1;
176}
177
178static char*
179encode_ascii_surrogateescape(const wchar_t *text, size_t *error_pos)
180{
181 char *result = NULL, *out;
182 size_t len, i;
183 wchar_t ch;
184
185 if (error_pos != NULL)
186 *error_pos = (size_t)-1;
187
188 len = wcslen(text);
189
190 result = PyMem_Malloc(len + 1); /* +1 for NUL byte */
191 if (result == NULL)
192 return NULL;
193
194 out = result;
195 for (i=0; i<len; i++) {
196 ch = text[i];
197
198 if (ch <= 0x7f) {
199 /* ASCII character */
200 *out++ = (char)ch;
201 }
202 else if (0xdc80 <= ch && ch <= 0xdcff) {
203 /* UTF-8b surrogate */
204 *out++ = (char)(ch - 0xdc00);
205 }
206 else {
207 if (error_pos != NULL)
208 *error_pos = i;
209 PyMem_Free(result);
210 return NULL;
211 }
212 }
213 *out = '\0';
214 return result;
215}
216#endif /* !defined(__APPLE__) && !defined(MS_WINDOWS) */
217
218#if !defined(__APPLE__) && (!defined(MS_WINDOWS) || !defined(HAVE_MBRTOWC))
219static wchar_t*
220decode_ascii_surrogateescape(const char *arg, size_t *size)
221{
222 wchar_t *res;
223 unsigned char *in;
224 wchar_t *out;
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600225 size_t argsize = strlen(arg) + 1;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100226
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600227 if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
228 return NULL;
Benjamin Peterson10ecaa22015-01-04 16:05:39 -0600229 res = PyMem_RawMalloc(argsize*sizeof(wchar_t));
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100230 if (!res)
231 return NULL;
232
233 in = (unsigned char*)arg;
234 out = res;
235 while(*in)
236 if(*in < 128)
237 *out++ = *in++;
238 else
239 *out++ = 0xdc00 + *in++;
240 *out = 0;
241 if (size != NULL)
242 *size = out - res;
243 return res;
244}
245#endif
246
Victor Stinner4e314432010-10-07 21:45:39 +0000247
248/* Decode a byte string from the locale encoding with the
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200249 surrogateescape error handler: undecodable bytes are decoded as characters
250 in range U+DC80..U+DCFF. If a byte sequence can be decoded as a surrogate
Victor Stinner4e314432010-10-07 21:45:39 +0000251 character, escape the bytes using the surrogateescape error handler instead
252 of decoding them.
253
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200254 Return a pointer to a newly allocated wide character string, use
255 PyMem_RawFree() to free the memory. If size is not NULL, write the number of
256 wide characters excluding the null character into *size
Victor Stinner4e314432010-10-07 21:45:39 +0000257
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200258 Return NULL on decoding error or memory allocation error. If *size* is not
259 NULL, *size is set to (size_t)-1 on memory error or set to (size_t)-2 on
260 decoding error.
Victor Stinner19de4c32010-11-08 23:30:46 +0000261
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200262 Decoding errors should never happen, unless there is a bug in the C
263 library.
264
265 Use the Py_EncodeLocale() function to encode the character string back to a
266 byte string. */
Victor Stinner4e314432010-10-07 21:45:39 +0000267wchar_t*
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200268Py_DecodeLocale(const char* arg, size_t *size)
Victor Stinner4e314432010-10-07 21:45:39 +0000269{
Victor Stinnere2623772012-11-12 23:04:02 +0100270#ifdef __APPLE__
271 wchar_t *wstr;
272 wstr = _Py_DecodeUTF8_surrogateescape(arg, strlen(arg));
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100273 if (size != NULL) {
274 if (wstr != NULL)
275 *size = wcslen(wstr);
276 else
277 *size = (size_t)-1;
278 }
Victor Stinnere2623772012-11-12 23:04:02 +0100279 return wstr;
280#else
Victor Stinner4e314432010-10-07 21:45:39 +0000281 wchar_t *res;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100282 size_t argsize;
Victor Stinner4e314432010-10-07 21:45:39 +0000283 size_t count;
Victor Stinner313f10c2013-05-07 23:48:56 +0200284#ifdef HAVE_MBRTOWC
Victor Stinner4e314432010-10-07 21:45:39 +0000285 unsigned char *in;
286 wchar_t *out;
Victor Stinner4e314432010-10-07 21:45:39 +0000287 mbstate_t mbs;
288#endif
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100289
290#ifndef MS_WINDOWS
291 if (force_ascii == -1)
292 force_ascii = check_force_ascii();
293
294 if (force_ascii) {
295 /* force ASCII encoding to workaround mbstowcs() issue */
296 res = decode_ascii_surrogateescape(arg, size);
297 if (res == NULL)
298 goto oom;
299 return res;
300 }
301#endif
302
303#ifdef HAVE_BROKEN_MBSTOWCS
304 /* Some platforms have a broken implementation of
305 * mbstowcs which does not count the characters that
306 * would result from conversion. Use an upper bound.
307 */
308 argsize = strlen(arg);
309#else
310 argsize = mbstowcs(NULL, arg, 0);
311#endif
Victor Stinner4e314432010-10-07 21:45:39 +0000312 if (argsize != (size_t)-1) {
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600313 if (argsize == PY_SSIZE_T_MAX)
314 goto oom;
315 argsize += 1;
316 if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
317 goto oom;
Benjamin Peterson10ecaa22015-01-04 16:05:39 -0600318 res = (wchar_t *)PyMem_RawMalloc(argsize*sizeof(wchar_t));
Victor Stinner4e314432010-10-07 21:45:39 +0000319 if (!res)
320 goto oom;
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600321 count = mbstowcs(res, arg, argsize);
Victor Stinner4e314432010-10-07 21:45:39 +0000322 if (count != (size_t)-1) {
323 wchar_t *tmp;
324 /* Only use the result if it contains no
325 surrogate characters. */
326 for (tmp = res; *tmp != 0 &&
Victor Stinner76df43d2012-10-30 01:42:39 +0100327 !Py_UNICODE_IS_SURROGATE(*tmp); tmp++)
Victor Stinner4e314432010-10-07 21:45:39 +0000328 ;
Victor Stinner168e1172010-10-16 23:16:16 +0000329 if (*tmp == 0) {
330 if (size != NULL)
331 *size = count;
Victor Stinner4e314432010-10-07 21:45:39 +0000332 return res;
Victor Stinner168e1172010-10-16 23:16:16 +0000333 }
Victor Stinner4e314432010-10-07 21:45:39 +0000334 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200335 PyMem_RawFree(res);
Victor Stinner4e314432010-10-07 21:45:39 +0000336 }
337 /* Conversion failed. Fall back to escaping with surrogateescape. */
338#ifdef HAVE_MBRTOWC
339 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
340
341 /* Overallocate; as multi-byte characters are in the argument, the
342 actual output could use less memory. */
343 argsize = strlen(arg) + 1;
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600344 if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
345 goto oom;
Victor Stinner1a7425f2013-07-07 16:25:15 +0200346 res = (wchar_t*)PyMem_RawMalloc(argsize*sizeof(wchar_t));
Victor Stinner19de4c32010-11-08 23:30:46 +0000347 if (!res)
348 goto oom;
Victor Stinner4e314432010-10-07 21:45:39 +0000349 in = (unsigned char*)arg;
350 out = res;
351 memset(&mbs, 0, sizeof mbs);
352 while (argsize) {
353 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
354 if (converted == 0)
355 /* Reached end of string; null char stored. */
356 break;
357 if (converted == (size_t)-2) {
358 /* Incomplete character. This should never happen,
359 since we provide everything that we have -
360 unless there is a bug in the C library, or I
361 misunderstood how mbrtowc works. */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200362 PyMem_RawFree(res);
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100363 if (size != NULL)
364 *size = (size_t)-2;
Victor Stinner4e314432010-10-07 21:45:39 +0000365 return NULL;
366 }
367 if (converted == (size_t)-1) {
368 /* Conversion error. Escape as UTF-8b, and start over
369 in the initial shift state. */
370 *out++ = 0xdc00 + *in++;
371 argsize--;
372 memset(&mbs, 0, sizeof mbs);
373 continue;
374 }
Victor Stinner76df43d2012-10-30 01:42:39 +0100375 if (Py_UNICODE_IS_SURROGATE(*out)) {
Victor Stinner4e314432010-10-07 21:45:39 +0000376 /* Surrogate character. Escape the original
377 byte sequence with surrogateescape. */
378 argsize -= converted;
379 while (converted--)
380 *out++ = 0xdc00 + *in++;
381 continue;
382 }
383 /* successfully converted some bytes */
384 in += converted;
385 argsize -= converted;
386 out++;
387 }
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100388 if (size != NULL)
389 *size = out - res;
Victor Stinnere2623772012-11-12 23:04:02 +0100390#else /* HAVE_MBRTOWC */
Victor Stinner4e314432010-10-07 21:45:39 +0000391 /* Cannot use C locale for escaping; manually escape as if charset
392 is ASCII (i.e. escape all bytes > 128. This will still roundtrip
393 correctly in the locale's charset, which must be an ASCII superset. */
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100394 res = decode_ascii_surrogateescape(arg, size);
395 if (res == NULL)
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100396 goto oom;
Victor Stinnere2623772012-11-12 23:04:02 +0100397#endif /* HAVE_MBRTOWC */
Victor Stinner4e314432010-10-07 21:45:39 +0000398 return res;
399oom:
Victor Stinneraf02e1c2011-12-16 23:56:01 +0100400 if (size != NULL)
401 *size = (size_t)-1;
Victor Stinner4e314432010-10-07 21:45:39 +0000402 return NULL;
Victor Stinnere2623772012-11-12 23:04:02 +0100403#endif /* __APPLE__ */
Victor Stinner4e314432010-10-07 21:45:39 +0000404}
405
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200406/* Encode a wide character string to the locale encoding with the
407 surrogateescape error handler: surrogate characters in the range
408 U+DC80..U+DCFF are converted to bytes 0x80..0xFF.
Victor Stinner4e314432010-10-07 21:45:39 +0000409
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200410 Return a pointer to a newly allocated byte string, use PyMem_Free() to free
411 the memory. Return NULL on encoding or memory allocation error.
Victor Stinner4e314432010-10-07 21:45:39 +0000412
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200413 If error_pos is not NULL, *error_pos is set to the index of the invalid
414 character on encoding error, or set to (size_t)-1 otherwise.
Victor Stinner2f02a512010-11-08 22:43:46 +0000415
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200416 Use the Py_DecodeLocale() function to decode the bytes string back to a wide
417 character string. */
Victor Stinner4e314432010-10-07 21:45:39 +0000418char*
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200419Py_EncodeLocale(const wchar_t *text, size_t *error_pos)
Victor Stinner4e314432010-10-07 21:45:39 +0000420{
Victor Stinnere2623772012-11-12 23:04:02 +0100421#ifdef __APPLE__
422 Py_ssize_t len;
423 PyObject *unicode, *bytes = NULL;
424 char *cpath;
425
426 unicode = PyUnicode_FromWideChar(text, wcslen(text));
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100427 if (unicode == NULL)
Victor Stinnere2623772012-11-12 23:04:02 +0100428 return NULL;
Victor Stinnere2623772012-11-12 23:04:02 +0100429
430 bytes = _PyUnicode_AsUTF8String(unicode, "surrogateescape");
431 Py_DECREF(unicode);
432 if (bytes == NULL) {
433 PyErr_Clear();
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100434 if (error_pos != NULL)
435 *error_pos = (size_t)-1;
Victor Stinnere2623772012-11-12 23:04:02 +0100436 return NULL;
437 }
438
439 len = PyBytes_GET_SIZE(bytes);
440 cpath = PyMem_Malloc(len+1);
441 if (cpath == NULL) {
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100442 PyErr_Clear();
Victor Stinnere2623772012-11-12 23:04:02 +0100443 Py_DECREF(bytes);
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100444 if (error_pos != NULL)
445 *error_pos = (size_t)-1;
Victor Stinnere2623772012-11-12 23:04:02 +0100446 return NULL;
447 }
448 memcpy(cpath, PyBytes_AsString(bytes), len + 1);
449 Py_DECREF(bytes);
450 return cpath;
451#else /* __APPLE__ */
Victor Stinner4e314432010-10-07 21:45:39 +0000452 const size_t len = wcslen(text);
453 char *result = NULL, *bytes = NULL;
454 size_t i, size, converted;
455 wchar_t c, buf[2];
456
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100457#ifndef MS_WINDOWS
458 if (force_ascii == -1)
459 force_ascii = check_force_ascii();
460
461 if (force_ascii)
462 return encode_ascii_surrogateescape(text, error_pos);
463#endif
464
Victor Stinner4e314432010-10-07 21:45:39 +0000465 /* The function works in two steps:
466 1. compute the length of the output buffer in bytes (size)
467 2. outputs the bytes */
468 size = 0;
469 buf[1] = 0;
470 while (1) {
471 for (i=0; i < len; i++) {
472 c = text[i];
473 if (c >= 0xdc80 && c <= 0xdcff) {
474 /* UTF-8b surrogate */
475 if (bytes != NULL) {
476 *bytes++ = c - 0xdc00;
477 size--;
478 }
479 else
480 size++;
481 continue;
482 }
483 else {
484 buf[0] = c;
485 if (bytes != NULL)
486 converted = wcstombs(bytes, buf, size);
487 else
488 converted = wcstombs(NULL, buf, 0);
489 if (converted == (size_t)-1) {
490 if (result != NULL)
491 PyMem_Free(result);
Victor Stinner2f02a512010-11-08 22:43:46 +0000492 if (error_pos != NULL)
493 *error_pos = i;
Victor Stinner4e314432010-10-07 21:45:39 +0000494 return NULL;
495 }
496 if (bytes != NULL) {
497 bytes += converted;
498 size -= converted;
499 }
500 else
501 size += converted;
502 }
503 }
504 if (result != NULL) {
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100505 *bytes = '\0';
Victor Stinner4e314432010-10-07 21:45:39 +0000506 break;
507 }
508
509 size += 1; /* nul byte at the end */
510 result = PyMem_Malloc(size);
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100511 if (result == NULL) {
512 if (error_pos != NULL)
513 *error_pos = (size_t)-1;
Victor Stinner4e314432010-10-07 21:45:39 +0000514 return NULL;
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100515 }
Victor Stinner4e314432010-10-07 21:45:39 +0000516 bytes = result;
517 }
518 return result;
Victor Stinnere2623772012-11-12 23:04:02 +0100519#endif /* __APPLE__ */
Victor Stinner4e314432010-10-07 21:45:39 +0000520}
521
Victor Stinner6672d0c2010-10-07 22:53:43 +0000522
Steve Dowerf2f373f2015-02-21 08:44:05 -0800523#ifdef MS_WINDOWS
524static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
525
526static void
527FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
528{
529 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
530 /* Cannot simply cast and dereference in_ptr,
531 since it might not be aligned properly */
532 __int64 in;
533 memcpy(&in, in_ptr, sizeof(in));
534 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
535 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
536}
537
538void
Steve Dowerbf1f3762015-02-21 15:26:02 -0800539_Py_time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800540{
541 /* XXX endianness */
542 __int64 out;
543 out = time_in + secs_between_epochs;
544 out = out * 10000000 + nsec_in / 100;
545 memcpy(out_ptr, &out, sizeof(out));
546}
547
548/* Below, we *know* that ugo+r is 0444 */
549#if _S_IREAD != 0400
550#error Unsupported C library
551#endif
552static int
553attributes_to_mode(DWORD attr)
554{
555 int m = 0;
556 if (attr & FILE_ATTRIBUTE_DIRECTORY)
557 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
558 else
559 m |= _S_IFREG;
560 if (attr & FILE_ATTRIBUTE_READONLY)
561 m |= 0444;
562 else
563 m |= 0666;
564 return m;
565}
566
Steve Dowerbf1f3762015-02-21 15:26:02 -0800567void
Victor Stinnere134a7f2015-03-30 10:09:31 +0200568_Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag,
569 struct _Py_stat_struct *result)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800570{
571 memset(result, 0, sizeof(*result));
572 result->st_mode = attributes_to_mode(info->dwFileAttributes);
573 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
574 result->st_dev = info->dwVolumeSerialNumber;
575 result->st_rdev = result->st_dev;
576 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
577 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
578 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
579 result->st_nlink = info->nNumberOfLinks;
580 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
581 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
582 /* first clear the S_IFMT bits */
583 result->st_mode ^= (result->st_mode & S_IFMT);
584 /* now set the bits that make this a symlink */
585 result->st_mode |= S_IFLNK;
586 }
587 result->st_file_attributes = info->dwFileAttributes;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800588}
589#endif
590
591/* Return information about a file.
592
593 On POSIX, use fstat().
594
595 On Windows, use GetFileType() and GetFileInformationByHandle() which support
596 files larger than 2 GB. fstat() may fail with EOVERFLOW on files larger
597 than 2 GB because the file size type is an signed 32-bit integer: see issue
598 #23152.
Victor Stinnere134a7f2015-03-30 10:09:31 +0200599
600 On Windows, set the last Windows error and return nonzero on error. On
601 POSIX, set errno and return nonzero on error. Fill status and return 0 on
602 success. */
Steve Dowerf2f373f2015-02-21 08:44:05 -0800603int
Victor Stinnere134a7f2015-03-30 10:09:31 +0200604_Py_fstat_noraise(int fd, struct _Py_stat_struct *status)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800605{
606#ifdef MS_WINDOWS
607 BY_HANDLE_FILE_INFORMATION info;
608 HANDLE h;
609 int type;
610
611 if (!_PyVerify_fd(fd))
612 h = INVALID_HANDLE_VALUE;
613 else
614 h = (HANDLE)_get_osfhandle(fd);
615
Steve Dower8acde7d2015-03-07 18:14:07 -0800616 /* Protocol violation: we explicitly clear errno, instead of
617 setting it to a POSIX error. Callers should use GetLastError. */
Steve Dowerf2f373f2015-02-21 08:44:05 -0800618 errno = 0;
619
620 if (h == INVALID_HANDLE_VALUE) {
Steve Dower8acde7d2015-03-07 18:14:07 -0800621 /* This is really a C library error (invalid file handle).
622 We set the Win32 error to the closes one matching. */
623 SetLastError(ERROR_INVALID_HANDLE);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800624 return -1;
625 }
Victor Stinnere134a7f2015-03-30 10:09:31 +0200626 memset(status, 0, sizeof(*status));
Steve Dowerf2f373f2015-02-21 08:44:05 -0800627
628 type = GetFileType(h);
629 if (type == FILE_TYPE_UNKNOWN) {
630 DWORD error = GetLastError();
Victor Stinnere134a7f2015-03-30 10:09:31 +0200631 if (error != 0)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800632 return -1;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800633 /* else: valid but unknown file */
634 }
635
636 if (type != FILE_TYPE_DISK) {
637 if (type == FILE_TYPE_CHAR)
Victor Stinnere134a7f2015-03-30 10:09:31 +0200638 status->st_mode = _S_IFCHR;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800639 else if (type == FILE_TYPE_PIPE)
Victor Stinnere134a7f2015-03-30 10:09:31 +0200640 status->st_mode = _S_IFIFO;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800641 return 0;
642 }
643
644 if (!GetFileInformationByHandle(h, &info)) {
645 return -1;
646 }
647
Victor Stinnere134a7f2015-03-30 10:09:31 +0200648 _Py_attribute_data_to_stat(&info, 0, status);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800649 /* specific to fstat() */
Victor Stinnere134a7f2015-03-30 10:09:31 +0200650 status->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800651 return 0;
652#else
Victor Stinnere134a7f2015-03-30 10:09:31 +0200653 return fstat(fd, status);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800654#endif
655}
Steve Dowerf2f373f2015-02-21 08:44:05 -0800656
Victor Stinnere134a7f2015-03-30 10:09:31 +0200657/* Return information about a file.
658
659 On POSIX, use fstat().
660
661 On Windows, use GetFileType() and GetFileInformationByHandle() which support
662 files larger than 2 GB. fstat() may fail with EOVERFLOW on files larger
663 than 2 GB because the file size type is an signed 32-bit integer: see issue
664 #23152.
665
666 Raise an exception and return -1 on error. On Windows, set the last Windows
667 error on error. On POSIX, set errno on error. Fill status and return 0 on
668 success.
669
Victor Stinner6f4fae82015-04-01 18:34:32 +0200670 Release the GIL to call GetFileType() and GetFileInformationByHandle(), or
671 to call fstat(). The caller must hold the GIL. */
Victor Stinnere134a7f2015-03-30 10:09:31 +0200672int
673_Py_fstat(int fd, struct _Py_stat_struct *status)
674{
675 int res;
676
677 Py_BEGIN_ALLOW_THREADS
678 res = _Py_fstat_noraise(fd, status);
679 Py_END_ALLOW_THREADS
680
681 if (res != 0) {
682#ifdef MS_WINDOWS
683 PyErr_SetFromWindowsErr(0);
684#else
685 PyErr_SetFromErrno(PyExc_OSError);
686#endif
687 return -1;
688 }
689 return 0;
690}
Steve Dowerf2f373f2015-02-21 08:44:05 -0800691
Victor Stinner6672d0c2010-10-07 22:53:43 +0000692/* Call _wstat() on Windows, or encode the path to the filesystem encoding and
693 call stat() otherwise. Only fill st_mode attribute on Windows.
694
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100695 Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception was
696 raised. */
Victor Stinner4e314432010-10-07 21:45:39 +0000697
698int
Victor Stinnera4a75952010-10-07 22:23:10 +0000699_Py_stat(PyObject *path, struct stat *statbuf)
Victor Stinner4e314432010-10-07 21:45:39 +0000700{
701#ifdef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +0000702 int err;
703 struct _stat wstatbuf;
Victor Stinneree587ea2011-11-17 00:51:38 +0100704 wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +0000705
Victor Stinneree587ea2011-11-17 00:51:38 +0100706 wpath = PyUnicode_AsUnicode(path);
707 if (wpath == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100708 return -2;
Victor Stinneree587ea2011-11-17 00:51:38 +0100709 err = _wstat(wpath, &wstatbuf);
Victor Stinner4e314432010-10-07 21:45:39 +0000710 if (!err)
711 statbuf->st_mode = wstatbuf.st_mode;
712 return err;
713#else
714 int ret;
Victor Stinnera4a75952010-10-07 22:23:10 +0000715 PyObject *bytes = PyUnicode_EncodeFSDefault(path);
Victor Stinner4e314432010-10-07 21:45:39 +0000716 if (bytes == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100717 return -2;
Victor Stinner4e314432010-10-07 21:45:39 +0000718 ret = stat(PyBytes_AS_STRING(bytes), statbuf);
719 Py_DECREF(bytes);
720 return ret;
721#endif
722}
723
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100724
Antoine Pitrou409b5382013-10-12 22:41:17 +0200725static int
Victor Stinnerdaf45552013-08-28 00:53:59 +0200726get_inheritable(int fd, int raise)
727{
728#ifdef MS_WINDOWS
729 HANDLE handle;
730 DWORD flags;
Victor Stinner6672d0c2010-10-07 22:53:43 +0000731
Victor Stinnerdaf45552013-08-28 00:53:59 +0200732 if (!_PyVerify_fd(fd)) {
733 if (raise)
734 PyErr_SetFromErrno(PyExc_OSError);
735 return -1;
736 }
737
738 handle = (HANDLE)_get_osfhandle(fd);
739 if (handle == INVALID_HANDLE_VALUE) {
740 if (raise)
Steve Dower41e72442015-03-14 11:38:27 -0700741 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200742 return -1;
743 }
744
745 if (!GetHandleInformation(handle, &flags)) {
746 if (raise)
747 PyErr_SetFromWindowsErr(0);
748 return -1;
749 }
750
751 return (flags & HANDLE_FLAG_INHERIT);
752#else
753 int flags;
754
755 flags = fcntl(fd, F_GETFD, 0);
756 if (flags == -1) {
757 if (raise)
758 PyErr_SetFromErrno(PyExc_OSError);
759 return -1;
760 }
761 return !(flags & FD_CLOEXEC);
762#endif
763}
764
765/* Get the inheritable flag of the specified file descriptor.
Victor Stinnerb034eee2013-09-07 10:36:04 +0200766 Return 1 if the file descriptor can be inherited, 0 if it cannot,
Victor Stinnerdaf45552013-08-28 00:53:59 +0200767 raise an exception and return -1 on error. */
768int
769_Py_get_inheritable(int fd)
770{
771 return get_inheritable(fd, 1);
772}
773
774static int
775set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
776{
777#ifdef MS_WINDOWS
778 HANDLE handle;
779 DWORD flags;
Victor Stinner282124b2014-09-02 11:41:04 +0200780#else
781#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
782 static int ioctl_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200783 int request;
784 int err;
Victor Stinner282124b2014-09-02 11:41:04 +0200785#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +0200786 int flags;
787 int res;
788#endif
789
790 /* atomic_flag_works can only be used to make the file descriptor
791 non-inheritable */
792 assert(!(atomic_flag_works != NULL && inheritable));
793
794 if (atomic_flag_works != NULL && !inheritable) {
795 if (*atomic_flag_works == -1) {
Steve Dower41e72442015-03-14 11:38:27 -0700796 int isInheritable = get_inheritable(fd, raise);
797 if (isInheritable == -1)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200798 return -1;
Steve Dower41e72442015-03-14 11:38:27 -0700799 *atomic_flag_works = !isInheritable;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200800 }
801
802 if (*atomic_flag_works)
803 return 0;
804 }
805
806#ifdef MS_WINDOWS
807 if (!_PyVerify_fd(fd)) {
808 if (raise)
809 PyErr_SetFromErrno(PyExc_OSError);
810 return -1;
811 }
812
813 handle = (HANDLE)_get_osfhandle(fd);
814 if (handle == INVALID_HANDLE_VALUE) {
815 if (raise)
Steve Dower41e72442015-03-14 11:38:27 -0700816 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200817 return -1;
818 }
819
820 if (inheritable)
821 flags = HANDLE_FLAG_INHERIT;
822 else
823 flags = 0;
824 if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) {
825 if (raise)
826 PyErr_SetFromWindowsErr(0);
827 return -1;
828 }
829 return 0;
830
Victor Stinnerdaf45552013-08-28 00:53:59 +0200831#else
Victor Stinner282124b2014-09-02 11:41:04 +0200832
833#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
834 if (ioctl_works != 0) {
835 /* fast-path: ioctl() only requires one syscall */
836 if (inheritable)
837 request = FIONCLEX;
838 else
839 request = FIOCLEX;
840 err = ioctl(fd, request, NULL);
841 if (!err) {
842 ioctl_works = 1;
843 return 0;
844 }
845
846 if (errno != ENOTTY) {
847 if (raise)
848 PyErr_SetFromErrno(PyExc_OSError);
849 return -1;
850 }
851 else {
852 /* Issue #22258: Here, ENOTTY means "Inappropriate ioctl for
853 device". The ioctl is declared but not supported by the kernel.
854 Remember that ioctl() doesn't work. It is the case on
855 Illumos-based OS for example. */
856 ioctl_works = 0;
857 }
858 /* fallback to fcntl() if ioctl() does not work */
859 }
860#endif
861
862 /* slow-path: fcntl() requires two syscalls */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200863 flags = fcntl(fd, F_GETFD);
864 if (flags < 0) {
865 if (raise)
866 PyErr_SetFromErrno(PyExc_OSError);
867 return -1;
868 }
869
870 if (inheritable)
871 flags &= ~FD_CLOEXEC;
872 else
873 flags |= FD_CLOEXEC;
874 res = fcntl(fd, F_SETFD, flags);
875 if (res < 0) {
876 if (raise)
877 PyErr_SetFromErrno(PyExc_OSError);
878 return -1;
879 }
880 return 0;
881#endif
882}
883
884/* Make the file descriptor non-inheritable.
Victor Stinnerb034eee2013-09-07 10:36:04 +0200885 Return 0 on success, set errno and return -1 on error. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200886static int
887make_non_inheritable(int fd)
888{
889 return set_inheritable(fd, 0, 0, NULL);
890}
891
892/* Set the inheritable flag of the specified file descriptor.
893 On success: return 0, on error: raise an exception if raise is nonzero
894 and return -1.
895
896 If atomic_flag_works is not NULL:
897
898 * if *atomic_flag_works==-1, check if the inheritable is set on the file
899 descriptor: if yes, set *atomic_flag_works to 1, otherwise set to 0 and
900 set the inheritable flag
901 * if *atomic_flag_works==1: do nothing
902 * if *atomic_flag_works==0: set inheritable flag to False
903
904 Set atomic_flag_works to NULL if no atomic flag was used to create the
905 file descriptor.
906
907 atomic_flag_works can only be used to make a file descriptor
908 non-inheritable: atomic_flag_works must be NULL if inheritable=1. */
909int
910_Py_set_inheritable(int fd, int inheritable, int *atomic_flag_works)
911{
912 return set_inheritable(fd, inheritable, 1, atomic_flag_works);
913}
914
Victor Stinnera555cfc2015-03-18 00:22:14 +0100915static int
916_Py_open_impl(const char *pathname, int flags, int gil_held)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200917{
918 int fd;
Victor Stinnera47fc5c2015-03-18 09:52:54 +0100919 int async_err = 0;
Victor Stinnera555cfc2015-03-18 00:22:14 +0100920#ifndef MS_WINDOWS
Victor Stinnerdaf45552013-08-28 00:53:59 +0200921 int *atomic_flag_works;
Victor Stinnera555cfc2015-03-18 00:22:14 +0100922#endif
923
924#ifdef MS_WINDOWS
925 flags |= O_NOINHERIT;
926#elif defined(O_CLOEXEC)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200927 atomic_flag_works = &_Py_open_cloexec_works;
928 flags |= O_CLOEXEC;
929#else
930 atomic_flag_works = NULL;
931#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +0200932
Victor Stinnera555cfc2015-03-18 00:22:14 +0100933 if (gil_held) {
Victor Stinnera47fc5c2015-03-18 09:52:54 +0100934 do {
935 Py_BEGIN_ALLOW_THREADS
936 fd = open(pathname, flags);
937 Py_END_ALLOW_THREADS
938 } while (fd < 0
939 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
940 if (async_err)
941 return -1;
Victor Stinnera555cfc2015-03-18 00:22:14 +0100942 if (fd < 0) {
943 PyErr_SetFromErrnoWithFilename(PyExc_OSError, pathname);
944 return -1;
945 }
946 }
947 else {
948 fd = open(pathname, flags);
949 if (fd < 0)
950 return -1;
951 }
952
953#ifndef MS_WINDOWS
954 if (set_inheritable(fd, 0, gil_held, atomic_flag_works) < 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +0200955 close(fd);
956 return -1;
957 }
Victor Stinnera555cfc2015-03-18 00:22:14 +0100958#endif
959
Victor Stinnerdaf45552013-08-28 00:53:59 +0200960 return fd;
961}
962
Victor Stinnera555cfc2015-03-18 00:22:14 +0100963/* Open a file with the specified flags (wrapper to open() function).
964 Return a file descriptor on success. Raise an exception and return -1 on
965 error.
966
967 The file descriptor is created non-inheritable.
968
Victor Stinnera47fc5c2015-03-18 09:52:54 +0100969 When interrupted by a signal (open() fails with EINTR), retry the syscall,
970 except if the Python signal handler raises an exception.
971
Victor Stinner6f4fae82015-04-01 18:34:32 +0200972 Release the GIL to call open(). The caller must hold the GIL. */
Victor Stinnera555cfc2015-03-18 00:22:14 +0100973int
974_Py_open(const char *pathname, int flags)
975{
976 /* _Py_open() must be called with the GIL held. */
977 assert(PyGILState_Check());
978 return _Py_open_impl(pathname, flags, 1);
979}
980
981/* Open a file with the specified flags (wrapper to open() function).
982 Return a file descriptor on success. Set errno and return -1 on error.
983
Victor Stinnera47fc5c2015-03-18 09:52:54 +0100984 The file descriptor is created non-inheritable.
985
986 If interrupted by a signal, fail with EINTR. */
Victor Stinnera555cfc2015-03-18 00:22:14 +0100987int
988_Py_open_noraise(const char *pathname, int flags)
989{
990 return _Py_open_impl(pathname, flags, 0);
991}
992
Victor Stinnerdaf45552013-08-28 00:53:59 +0200993/* Open a file. Use _wfopen() on Windows, encode the path to the locale
Victor Stinnere42ccd22015-03-18 01:39:23 +0100994 encoding and use fopen() otherwise.
995
Victor Stinnera47fc5c2015-03-18 09:52:54 +0100996 The file descriptor is created non-inheritable.
997
998 If interrupted by a signal, fail with EINTR. */
Victor Stinner4e314432010-10-07 21:45:39 +0000999FILE *
1000_Py_wfopen(const wchar_t *path, const wchar_t *mode)
1001{
Victor Stinner4e314432010-10-07 21:45:39 +00001002 FILE *f;
Victor Stinnerdaf45552013-08-28 00:53:59 +02001003#ifndef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +00001004 char *cpath;
1005 char cmode[10];
1006 size_t r;
1007 r = wcstombs(cmode, mode, 10);
1008 if (r == (size_t)-1 || r >= 10) {
1009 errno = EINVAL;
1010 return NULL;
1011 }
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001012 cpath = Py_EncodeLocale(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001013 if (cpath == NULL)
1014 return NULL;
1015 f = fopen(cpath, cmode);
1016 PyMem_Free(cpath);
Victor Stinner4e314432010-10-07 21:45:39 +00001017#else
Victor Stinnerdaf45552013-08-28 00:53:59 +02001018 f = _wfopen(path, mode);
Victor Stinner4e314432010-10-07 21:45:39 +00001019#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +02001020 if (f == NULL)
1021 return NULL;
1022 if (make_non_inheritable(fileno(f)) < 0) {
1023 fclose(f);
1024 return NULL;
1025 }
1026 return f;
Victor Stinner4e314432010-10-07 21:45:39 +00001027}
1028
Victor Stinnere42ccd22015-03-18 01:39:23 +01001029/* Wrapper to fopen().
1030
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001031 The file descriptor is created non-inheritable.
1032
1033 If interrupted by a signal, fail with EINTR. */
Victor Stinnerdaf45552013-08-28 00:53:59 +02001034FILE*
1035_Py_fopen(const char *pathname, const char *mode)
1036{
1037 FILE *f = fopen(pathname, mode);
1038 if (f == NULL)
1039 return NULL;
1040 if (make_non_inheritable(fileno(f)) < 0) {
1041 fclose(f);
1042 return NULL;
1043 }
1044 return f;
1045}
1046
1047/* Open a file. Call _wfopen() on Windows, or encode the path to the filesystem
Victor Stinnere42ccd22015-03-18 01:39:23 +01001048 encoding and call fopen() otherwise.
Victor Stinner6672d0c2010-10-07 22:53:43 +00001049
Victor Stinnere42ccd22015-03-18 01:39:23 +01001050 Return the new file object on success. Raise an exception and return NULL
1051 on error.
1052
1053 The file descriptor is created non-inheritable.
1054
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001055 When interrupted by a signal (open() fails with EINTR), retry the syscall,
1056 except if the Python signal handler raises an exception.
1057
Victor Stinner6f4fae82015-04-01 18:34:32 +02001058 Release the GIL to call _wfopen() or fopen(). The caller must hold
1059 the GIL. */
Victor Stinner4e314432010-10-07 21:45:39 +00001060FILE*
Victor Stinnerdaf45552013-08-28 00:53:59 +02001061_Py_fopen_obj(PyObject *path, const char *mode)
Victor Stinner4e314432010-10-07 21:45:39 +00001062{
Victor Stinnerdaf45552013-08-28 00:53:59 +02001063 FILE *f;
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001064 int async_err = 0;
Victor Stinner4e314432010-10-07 21:45:39 +00001065#ifdef MS_WINDOWS
Victor Stinneree587ea2011-11-17 00:51:38 +01001066 wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +00001067 wchar_t wmode[10];
1068 int usize;
Victor Stinner4e314432010-10-07 21:45:39 +00001069
Victor Stinnere42ccd22015-03-18 01:39:23 +01001070 assert(PyGILState_Check());
1071
Antoine Pitrou0e576f12011-12-22 10:03:38 +01001072 if (!PyUnicode_Check(path)) {
1073 PyErr_Format(PyExc_TypeError,
1074 "str file path expected under Windows, got %R",
1075 Py_TYPE(path));
1076 return NULL;
1077 }
Victor Stinneree587ea2011-11-17 00:51:38 +01001078 wpath = PyUnicode_AsUnicode(path);
1079 if (wpath == NULL)
1080 return NULL;
1081
Victor Stinner4e314432010-10-07 21:45:39 +00001082 usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode));
Victor Stinnere42ccd22015-03-18 01:39:23 +01001083 if (usize == 0) {
1084 PyErr_SetFromWindowsErr(0);
Victor Stinner4e314432010-10-07 21:45:39 +00001085 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001086 }
Victor Stinner4e314432010-10-07 21:45:39 +00001087
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001088 do {
1089 Py_BEGIN_ALLOW_THREADS
1090 f = _wfopen(wpath, wmode);
1091 Py_END_ALLOW_THREADS
1092 } while (f == NULL
1093 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner4e314432010-10-07 21:45:39 +00001094#else
Antoine Pitrou2b1cc892011-12-19 18:19:06 +01001095 PyObject *bytes;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001096 char *path_bytes;
1097
1098 assert(PyGILState_Check());
1099
Antoine Pitrou2b1cc892011-12-19 18:19:06 +01001100 if (!PyUnicode_FSConverter(path, &bytes))
Victor Stinner4e314432010-10-07 21:45:39 +00001101 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001102 path_bytes = PyBytes_AS_STRING(bytes);
1103
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001104 do {
1105 Py_BEGIN_ALLOW_THREADS
1106 f = fopen(path_bytes, mode);
1107 Py_END_ALLOW_THREADS
1108 } while (f == NULL
1109 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnere42ccd22015-03-18 01:39:23 +01001110
Victor Stinner4e314432010-10-07 21:45:39 +00001111 Py_DECREF(bytes);
Victor Stinner4e314432010-10-07 21:45:39 +00001112#endif
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001113 if (async_err)
1114 return NULL;
1115
Victor Stinnere42ccd22015-03-18 01:39:23 +01001116 if (f == NULL) {
1117 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001118 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001119 }
1120
1121 if (set_inheritable(fileno(f), 0, 1, NULL) < 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +02001122 fclose(f);
1123 return NULL;
1124 }
1125 return f;
Victor Stinner4e314432010-10-07 21:45:39 +00001126}
1127
Victor Stinner66aab0c2015-03-19 22:53:20 +01001128/* Read count bytes from fd into buf.
Victor Stinner82c3e452015-04-01 18:34:45 +02001129
1130 On success, return the number of read bytes, it can be lower than count.
1131 If the current file offset is at or past the end of file, no bytes are read,
1132 and read() returns zero.
1133
1134 On error, raise an exception, set errno and return -1.
1135
1136 When interrupted by a signal (read() fails with EINTR), retry the syscall.
1137 If the Python signal handler raises an exception, the function returns -1
1138 (the syscall is not retried).
1139
1140 Release the GIL to call read(). The caller must hold the GIL. */
Victor Stinner66aab0c2015-03-19 22:53:20 +01001141Py_ssize_t
1142_Py_read(int fd, void *buf, size_t count)
1143{
1144 Py_ssize_t n;
Victor Stinnera3c02022015-03-20 11:58:18 +01001145 int err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001146 int async_err = 0;
1147
1148 /* _Py_read() must not be called with an exception set, otherwise the
1149 * caller may think that read() was interrupted by a signal and the signal
1150 * handler raised an exception. */
1151 assert(!PyErr_Occurred());
1152
Victor Stinnerc1cf4f72015-03-19 23:53:04 +01001153 if (!_PyVerify_fd(fd)) {
Victor Stinnera3c02022015-03-20 11:58:18 +01001154 /* save/restore errno because PyErr_SetFromErrno() can modify it */
1155 err = errno;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001156 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera3c02022015-03-20 11:58:18 +01001157 errno = err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001158 return -1;
1159 }
1160
1161#ifdef MS_WINDOWS
1162 if (count > INT_MAX) {
1163 /* On Windows, the count parameter of read() is an int */
1164 count = INT_MAX;
1165 }
1166#else
1167 if (count > PY_SSIZE_T_MAX) {
1168 /* if count is greater than PY_SSIZE_T_MAX,
1169 * read() result is undefined */
1170 count = PY_SSIZE_T_MAX;
1171 }
1172#endif
1173
1174 do {
1175 Py_BEGIN_ALLOW_THREADS
1176 errno = 0;
1177#ifdef MS_WINDOWS
1178 n = read(fd, buf, (int)count);
1179#else
1180 n = read(fd, buf, count);
1181#endif
Victor Stinnera3c02022015-03-20 11:58:18 +01001182 /* save/restore errno because PyErr_CheckSignals()
1183 * and PyErr_SetFromErrno() can modify it */
1184 err = errno;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001185 Py_END_ALLOW_THREADS
Victor Stinnera3c02022015-03-20 11:58:18 +01001186 } while (n < 0 && err == EINTR &&
Victor Stinner66aab0c2015-03-19 22:53:20 +01001187 !(async_err = PyErr_CheckSignals()));
1188
1189 if (async_err) {
1190 /* read() was interrupted by a signal (failed with EINTR)
1191 * and the Python signal handler raised an exception */
Victor Stinnera3c02022015-03-20 11:58:18 +01001192 errno = err;
1193 assert(errno == EINTR && PyErr_Occurred());
Victor Stinner66aab0c2015-03-19 22:53:20 +01001194 return -1;
1195 }
1196 if (n < 0) {
Victor Stinner66aab0c2015-03-19 22:53:20 +01001197 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera3c02022015-03-20 11:58:18 +01001198 errno = err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001199 return -1;
1200 }
1201
1202 return n;
1203}
1204
Victor Stinner82c3e452015-04-01 18:34:45 +02001205static Py_ssize_t
1206_Py_write_impl(int fd, const void *buf, size_t count, int gil_held)
Victor Stinner66aab0c2015-03-19 22:53:20 +01001207{
1208 Py_ssize_t n;
Victor Stinnera3c02022015-03-20 11:58:18 +01001209 int err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001210 int async_err = 0;
1211
Victor Stinner66aab0c2015-03-19 22:53:20 +01001212 if (!_PyVerify_fd(fd)) {
Victor Stinner82c3e452015-04-01 18:34:45 +02001213 if (gil_held) {
1214 /* save/restore errno because PyErr_SetFromErrno() can modify it */
1215 err = errno;
1216 PyErr_SetFromErrno(PyExc_OSError);
1217 errno = err;
1218 }
Victor Stinner66aab0c2015-03-19 22:53:20 +01001219 return -1;
1220 }
1221
1222#ifdef MS_WINDOWS
1223 if (count > 32767 && isatty(fd)) {
1224 /* Issue #11395: the Windows console returns an error (12: not
1225 enough space error) on writing into stdout if stdout mode is
1226 binary and the length is greater than 66,000 bytes (or less,
1227 depending on heap usage). */
1228 count = 32767;
1229 }
1230 else if (count > INT_MAX)
1231 count = INT_MAX;
1232#else
1233 if (count > PY_SSIZE_T_MAX) {
1234 /* write() should truncate count to PY_SSIZE_T_MAX, but it's safer
1235 * to do it ourself to have a portable behaviour. */
1236 count = PY_SSIZE_T_MAX;
1237 }
1238#endif
1239
Victor Stinner82c3e452015-04-01 18:34:45 +02001240 if (gil_held) {
1241 do {
1242 Py_BEGIN_ALLOW_THREADS
1243 errno = 0;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001244#ifdef MS_WINDOWS
Victor Stinner82c3e452015-04-01 18:34:45 +02001245 n = write(fd, buf, (int)count);
Victor Stinner66aab0c2015-03-19 22:53:20 +01001246#else
Victor Stinner82c3e452015-04-01 18:34:45 +02001247 n = write(fd, buf, count);
Victor Stinner66aab0c2015-03-19 22:53:20 +01001248#endif
Victor Stinner82c3e452015-04-01 18:34:45 +02001249 /* save/restore errno because PyErr_CheckSignals()
1250 * and PyErr_SetFromErrno() can modify it */
1251 err = errno;
1252 Py_END_ALLOW_THREADS
1253 } while (n < 0 && err == EINTR &&
1254 !(async_err = PyErr_CheckSignals()));
1255 }
1256 else {
1257 do {
1258 errno = 0;
1259#ifdef MS_WINDOWS
1260 n = write(fd, buf, (int)count);
1261#else
1262 n = write(fd, buf, count);
1263#endif
1264 err = errno;
1265 } while (n < 0 && err == EINTR);
1266 }
Victor Stinner66aab0c2015-03-19 22:53:20 +01001267
1268 if (async_err) {
1269 /* write() was interrupted by a signal (failed with EINTR)
Victor Stinner82c3e452015-04-01 18:34:45 +02001270 and the Python signal handler raised an exception (if gil_held is
1271 nonzero). */
Victor Stinnera3c02022015-03-20 11:58:18 +01001272 errno = err;
Victor Stinner82c3e452015-04-01 18:34:45 +02001273 assert(errno == EINTR && (!gil_held || PyErr_Occurred()));
Victor Stinner66aab0c2015-03-19 22:53:20 +01001274 return -1;
1275 }
1276 if (n < 0) {
Victor Stinner82c3e452015-04-01 18:34:45 +02001277 if (gil_held)
1278 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera3c02022015-03-20 11:58:18 +01001279 errno = err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001280 return -1;
1281 }
1282
1283 return n;
1284}
1285
Victor Stinner82c3e452015-04-01 18:34:45 +02001286/* Write count bytes of buf into fd.
1287
1288 On success, return the number of written bytes, it can be lower than count
1289 including 0. On error, raise an exception, set errno and return -1.
1290
1291 When interrupted by a signal (write() fails with EINTR), retry the syscall.
1292 If the Python signal handler raises an exception, the function returns -1
1293 (the syscall is not retried).
1294
1295 Release the GIL to call write(). The caller must hold the GIL. */
1296Py_ssize_t
1297_Py_write(int fd, const void *buf, size_t count)
1298{
1299 /* _Py_write() must not be called with an exception set, otherwise the
1300 * caller may think that write() was interrupted by a signal and the signal
1301 * handler raised an exception. */
1302 assert(!PyErr_Occurred());
1303
1304 return _Py_write_impl(fd, buf, count, 1);
1305}
1306
1307/* Write count bytes of buf into fd.
1308 *
1309 * On success, return the number of written bytes, it can be lower than count
1310 * including 0. On error, set errno and return -1.
1311 *
1312 * When interrupted by a signal (write() fails with EINTR), retry the syscall
1313 * without calling the Python signal handler. */
1314Py_ssize_t
1315_Py_write_noraise(int fd, const void *buf, size_t count)
1316{
1317 return _Py_write_impl(fd, buf, count, 0);
1318}
1319
Victor Stinner4e314432010-10-07 21:45:39 +00001320#ifdef HAVE_READLINK
Victor Stinner6672d0c2010-10-07 22:53:43 +00001321
1322/* Read value of symbolic link. Encode the path to the locale encoding, decode
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001323 the result from the locale encoding. Return -1 on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001324
Victor Stinner4e314432010-10-07 21:45:39 +00001325int
1326_Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
1327{
1328 char *cpath;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001329 char cbuf[MAXPATHLEN];
Victor Stinner3f711f42010-10-16 22:47:37 +00001330 wchar_t *wbuf;
Victor Stinner4e314432010-10-07 21:45:39 +00001331 int res;
1332 size_t r1;
1333
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001334 cpath = Py_EncodeLocale(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001335 if (cpath == NULL) {
1336 errno = EINVAL;
1337 return -1;
1338 }
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001339 res = (int)readlink(cpath, cbuf, Py_ARRAY_LENGTH(cbuf));
Victor Stinner4e314432010-10-07 21:45:39 +00001340 PyMem_Free(cpath);
1341 if (res == -1)
1342 return -1;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001343 if (res == Py_ARRAY_LENGTH(cbuf)) {
Victor Stinner4e314432010-10-07 21:45:39 +00001344 errno = EINVAL;
1345 return -1;
1346 }
1347 cbuf[res] = '\0'; /* buf will be null terminated */
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001348 wbuf = Py_DecodeLocale(cbuf, &r1);
Victor Stinner350147b2010-10-16 22:52:09 +00001349 if (wbuf == NULL) {
1350 errno = EINVAL;
1351 return -1;
1352 }
Victor Stinner3f711f42010-10-16 22:47:37 +00001353 if (bufsiz <= r1) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001354 PyMem_RawFree(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001355 errno = EINVAL;
1356 return -1;
1357 }
Victor Stinner3f711f42010-10-16 22:47:37 +00001358 wcsncpy(buf, wbuf, bufsiz);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001359 PyMem_RawFree(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001360 return (int)r1;
1361}
1362#endif
1363
1364#ifdef HAVE_REALPATH
Victor Stinner6672d0c2010-10-07 22:53:43 +00001365
1366/* Return the canonicalized absolute pathname. Encode path to the locale
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001367 encoding, decode the result from the locale encoding.
1368 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001369
Victor Stinner4e314432010-10-07 21:45:39 +00001370wchar_t*
Victor Stinner015f4d82010-10-07 22:29:53 +00001371_Py_wrealpath(const wchar_t *path,
1372 wchar_t *resolved_path, size_t resolved_path_size)
Victor Stinner4e314432010-10-07 21:45:39 +00001373{
1374 char *cpath;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001375 char cresolved_path[MAXPATHLEN];
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001376 wchar_t *wresolved_path;
Victor Stinner4e314432010-10-07 21:45:39 +00001377 char *res;
1378 size_t r;
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001379 cpath = Py_EncodeLocale(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001380 if (cpath == NULL) {
1381 errno = EINVAL;
1382 return NULL;
1383 }
1384 res = realpath(cpath, cresolved_path);
1385 PyMem_Free(cpath);
1386 if (res == NULL)
1387 return NULL;
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001388
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001389 wresolved_path = Py_DecodeLocale(cresolved_path, &r);
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001390 if (wresolved_path == NULL) {
Victor Stinner4e314432010-10-07 21:45:39 +00001391 errno = EINVAL;
1392 return NULL;
1393 }
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001394 if (resolved_path_size <= r) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001395 PyMem_RawFree(wresolved_path);
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001396 errno = EINVAL;
1397 return NULL;
1398 }
1399 wcsncpy(resolved_path, wresolved_path, resolved_path_size);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001400 PyMem_RawFree(wresolved_path);
Victor Stinner4e314432010-10-07 21:45:39 +00001401 return resolved_path;
1402}
1403#endif
1404
Victor Stinnerf4061da2010-10-14 12:37:19 +00001405/* Get the current directory. size is the buffer size in wide characters
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001406 including the null character. Decode the path from the locale encoding.
1407 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001408
Victor Stinner4e314432010-10-07 21:45:39 +00001409wchar_t*
1410_Py_wgetcwd(wchar_t *buf, size_t size)
1411{
1412#ifdef MS_WINDOWS
Victor Stinner56785ea2013-06-05 00:46:29 +02001413 int isize = (int)Py_MIN(size, INT_MAX);
1414 return _wgetcwd(buf, isize);
Victor Stinner4e314432010-10-07 21:45:39 +00001415#else
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001416 char fname[MAXPATHLEN];
Victor Stinnerf4061da2010-10-14 12:37:19 +00001417 wchar_t *wname;
Victor Stinner168e1172010-10-16 23:16:16 +00001418 size_t len;
Victor Stinnerf4061da2010-10-14 12:37:19 +00001419
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001420 if (getcwd(fname, Py_ARRAY_LENGTH(fname)) == NULL)
Victor Stinner4e314432010-10-07 21:45:39 +00001421 return NULL;
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001422 wname = Py_DecodeLocale(fname, &len);
Victor Stinnerf4061da2010-10-14 12:37:19 +00001423 if (wname == NULL)
1424 return NULL;
Victor Stinner168e1172010-10-16 23:16:16 +00001425 if (size <= len) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001426 PyMem_RawFree(wname);
Victor Stinner4e314432010-10-07 21:45:39 +00001427 return NULL;
1428 }
Victor Stinnerf4061da2010-10-14 12:37:19 +00001429 wcsncpy(buf, wname, size);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001430 PyMem_RawFree(wname);
Victor Stinner4e314432010-10-07 21:45:39 +00001431 return buf;
1432#endif
1433}
1434
Victor Stinnerdaf45552013-08-28 00:53:59 +02001435/* Duplicate a file descriptor. The new file descriptor is created as
1436 non-inheritable. Return a new file descriptor on success, raise an OSError
1437 exception and return -1 on error.
1438
1439 The GIL is released to call dup(). The caller must hold the GIL. */
1440int
1441_Py_dup(int fd)
1442{
1443#ifdef MS_WINDOWS
1444 HANDLE handle;
1445 DWORD ftype;
1446#endif
1447
1448 if (!_PyVerify_fd(fd)) {
1449 PyErr_SetFromErrno(PyExc_OSError);
1450 return -1;
1451 }
1452
1453#ifdef MS_WINDOWS
1454 handle = (HANDLE)_get_osfhandle(fd);
1455 if (handle == INVALID_HANDLE_VALUE) {
Steve Dower41e72442015-03-14 11:38:27 -07001456 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001457 return -1;
1458 }
1459
1460 /* get the file type, ignore the error if it failed */
1461 ftype = GetFileType(handle);
1462
1463 Py_BEGIN_ALLOW_THREADS
1464 fd = dup(fd);
1465 Py_END_ALLOW_THREADS
1466 if (fd < 0) {
1467 PyErr_SetFromErrno(PyExc_OSError);
1468 return -1;
1469 }
1470
1471 /* Character files like console cannot be make non-inheritable */
1472 if (ftype != FILE_TYPE_CHAR) {
1473 if (_Py_set_inheritable(fd, 0, NULL) < 0) {
1474 close(fd);
1475 return -1;
1476 }
1477 }
1478#elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC)
1479 Py_BEGIN_ALLOW_THREADS
1480 fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1481 Py_END_ALLOW_THREADS
1482 if (fd < 0) {
1483 PyErr_SetFromErrno(PyExc_OSError);
1484 return -1;
1485 }
1486
1487#else
1488 Py_BEGIN_ALLOW_THREADS
1489 fd = dup(fd);
1490 Py_END_ALLOW_THREADS
1491 if (fd < 0) {
1492 PyErr_SetFromErrno(PyExc_OSError);
1493 return -1;
1494 }
1495
1496 if (_Py_set_inheritable(fd, 0, NULL) < 0) {
1497 close(fd);
1498 return -1;
1499 }
1500#endif
1501 return fd;
1502}
1503
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001504#ifndef MS_WINDOWS
1505/* Get the blocking mode of the file descriptor.
1506 Return 0 if the O_NONBLOCK flag is set, 1 if the flag is cleared,
1507 raise an exception and return -1 on error. */
1508int
1509_Py_get_blocking(int fd)
1510{
1511 int flags = fcntl(fd, F_GETFL, 0);
1512 if (flags < 0) {
1513 PyErr_SetFromErrno(PyExc_OSError);
1514 return -1;
1515 }
1516
1517 return !(flags & O_NONBLOCK);
1518}
1519
1520/* Set the blocking mode of the specified file descriptor.
1521
1522 Set the O_NONBLOCK flag if blocking is False, clear the O_NONBLOCK flag
1523 otherwise.
1524
1525 Return 0 on success, raise an exception and return -1 on error. */
1526int
1527_Py_set_blocking(int fd, int blocking)
1528{
1529#if defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)
1530 int arg = !blocking;
1531 if (ioctl(fd, FIONBIO, &arg) < 0)
1532 goto error;
1533#else
1534 int flags, res;
1535
1536 flags = fcntl(fd, F_GETFL, 0);
1537 if (flags < 0)
1538 goto error;
1539
1540 if (blocking)
1541 flags = flags & (~O_NONBLOCK);
1542 else
1543 flags = flags | O_NONBLOCK;
1544
1545 res = fcntl(fd, F_SETFL, flags);
1546 if (res < 0)
1547 goto error;
1548#endif
1549 return 0;
1550
1551error:
1552 PyErr_SetFromErrno(PyExc_OSError);
1553 return -1;
1554}
1555#endif
1556
Steve Dowerd81431f2015-03-06 14:47:02 -08001557#ifdef _MSC_VER
1558#if _MSC_VER >= 1900
1559
1560/* This function lets the Windows CRT validate the file handle without
1561 terminating the process if it's invalid. */
1562int
1563_PyVerify_fd(int fd)
1564{
1565 intptr_t osh;
1566 /* Fast check for the only condition we know */
1567 if (fd < 0) {
1568 _set_errno(EBADF);
1569 return 0;
1570 }
1571 osh = _get_osfhandle(fd);
1572 return osh != (intptr_t)-1;
1573}
1574
1575#elif _MSC_VER >= 1400
1576/* Legacy implementation of _PyVerify_fd while transitioning to
1577 * MSVC 14.0. This should eventually be removed. (issue23524)
1578 */
1579
1580/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
1581 * valid and raise an assertion if it isn't.
1582 * Normally, an invalid fd is likely to be a C program error and therefore
1583 * an assertion can be useful, but it does contradict the POSIX standard
1584 * which for write(2) states:
1585 * "Otherwise, -1 shall be returned and errno set to indicate the error."
1586 * "[EBADF] The fildes argument is not a valid file descriptor open for
1587 * writing."
1588 * Furthermore, python allows the user to enter any old integer
1589 * as a fd and should merely raise a python exception on error.
1590 * The Microsoft CRT doesn't provide an official way to check for the
1591 * validity of a file descriptor, but we can emulate its internal behaviour
1592 * by using the exported __pinfo data member and knowledge of the
1593 * internal structures involved.
1594 * The structures below must be updated for each version of visual studio
1595 * according to the file internal.h in the CRT source, until MS comes
1596 * up with a less hacky way to do this.
1597 * (all of this is to avoid globally modifying the CRT behaviour using
1598 * _set_invalid_parameter_handler() and _CrtSetReportMode())
1599 */
1600/* The actual size of the structure is determined at runtime.
1601 * Only the first items must be present.
1602 */
1603typedef struct {
1604 intptr_t osfhnd;
1605 char osfile;
1606} my_ioinfo;
1607
1608extern __declspec(dllimport) char * __pioinfo[];
1609#define IOINFO_L2E 5
1610#define IOINFO_ARRAYS 64
1611#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
1612#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
1613#define FOPEN 0x01
1614#define _NO_CONSOLE_FILENO (intptr_t)-2
1615
1616/* This function emulates what the windows CRT does to validate file handles */
1617int
1618_PyVerify_fd(int fd)
1619{
1620 const int i1 = fd >> IOINFO_L2E;
1621 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
1622
1623 static size_t sizeof_ioinfo = 0;
1624
1625 /* Determine the actual size of the ioinfo structure,
1626 * as used by the CRT loaded in memory
1627 */
1628 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
1629 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
1630 }
1631 if (sizeof_ioinfo == 0) {
1632 /* This should not happen... */
1633 goto fail;
1634 }
1635
1636 /* See that it isn't a special CLEAR fileno */
1637 if (fd != _NO_CONSOLE_FILENO) {
1638 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
1639 * we check pointer validity and other info
1640 */
1641 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
1642 /* finally, check that the file is open */
1643 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
1644 if (info->osfile & FOPEN) {
1645 return 1;
1646 }
1647 }
1648 }
1649 fail:
1650 errno = EBADF;
1651 return 0;
1652}
1653
1654#endif /* _MSC_VER >= 1900 || _MSC_VER >= 1400 */
1655#endif /* defined _MSC_VER */