blob: e9c902b7f9b6ff119ab199f96b598cdd699b5309 [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 Stinner4e314432010-10-07 21:45:39 +0000522/* In principle, this should use HAVE__WSTAT, and _wstat
523 should be detected by autoconf. However, no current
524 POSIX system provides that function, so testing for
525 it is pointless.
526 Not sure whether the MS_WINDOWS guards are necessary:
527 perhaps for cygwin/mingw builds?
528*/
Victor Stinnerb306d752010-10-07 22:09:40 +0000529#if defined(HAVE_STAT) && !defined(MS_WINDOWS)
Victor Stinner6672d0c2010-10-07 22:53:43 +0000530
531/* Get file status. Encode the path to the locale encoding. */
532
Victor Stinnerb306d752010-10-07 22:09:40 +0000533int
534_Py_wstat(const wchar_t* path, struct stat *buf)
535{
Victor Stinner4e314432010-10-07 21:45:39 +0000536 int err;
537 char *fname;
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200538 fname = Py_EncodeLocale(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +0000539 if (fname == NULL) {
540 errno = EINVAL;
541 return -1;
542 }
543 err = stat(fname, buf);
544 PyMem_Free(fname);
545 return err;
Victor Stinner4e314432010-10-07 21:45:39 +0000546}
547#endif
548
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100549
Steve Dowerf2f373f2015-02-21 08:44:05 -0800550#if defined(HAVE_FSTAT) || defined(MS_WINDOWS)
551
552#ifdef MS_WINDOWS
553static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
554
555static void
556FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
557{
558 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
559 /* Cannot simply cast and dereference in_ptr,
560 since it might not be aligned properly */
561 __int64 in;
562 memcpy(&in, in_ptr, sizeof(in));
563 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
564 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
565}
566
567void
Steve Dowerbf1f3762015-02-21 15:26:02 -0800568_Py_time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800569{
570 /* XXX endianness */
571 __int64 out;
572 out = time_in + secs_between_epochs;
573 out = out * 10000000 + nsec_in / 100;
574 memcpy(out_ptr, &out, sizeof(out));
575}
576
577/* Below, we *know* that ugo+r is 0444 */
578#if _S_IREAD != 0400
579#error Unsupported C library
580#endif
581static int
582attributes_to_mode(DWORD attr)
583{
584 int m = 0;
585 if (attr & FILE_ATTRIBUTE_DIRECTORY)
586 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
587 else
588 m |= _S_IFREG;
589 if (attr & FILE_ATTRIBUTE_READONLY)
590 m |= 0444;
591 else
592 m |= 0666;
593 return m;
594}
595
Steve Dowerbf1f3762015-02-21 15:26:02 -0800596void
Steve Dowera2af1a52015-02-21 10:04:10 -0800597_Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct _Py_stat_struct *result)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800598{
599 memset(result, 0, sizeof(*result));
600 result->st_mode = attributes_to_mode(info->dwFileAttributes);
601 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
602 result->st_dev = info->dwVolumeSerialNumber;
603 result->st_rdev = result->st_dev;
604 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
605 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
606 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
607 result->st_nlink = info->nNumberOfLinks;
608 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
609 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
610 /* first clear the S_IFMT bits */
611 result->st_mode ^= (result->st_mode & S_IFMT);
612 /* now set the bits that make this a symlink */
613 result->st_mode |= S_IFLNK;
614 }
615 result->st_file_attributes = info->dwFileAttributes;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800616}
617#endif
618
619/* Return information about a file.
620
621 On POSIX, use fstat().
622
623 On Windows, use GetFileType() and GetFileInformationByHandle() which support
624 files larger than 2 GB. fstat() may fail with EOVERFLOW on files larger
625 than 2 GB because the file size type is an signed 32-bit integer: see issue
626 #23152.
627 */
628int
629_Py_fstat(int fd, struct _Py_stat_struct *result)
630{
631#ifdef MS_WINDOWS
632 BY_HANDLE_FILE_INFORMATION info;
633 HANDLE h;
634 int type;
635
636 if (!_PyVerify_fd(fd))
637 h = INVALID_HANDLE_VALUE;
638 else
639 h = (HANDLE)_get_osfhandle(fd);
640
Steve Dower8acde7d2015-03-07 18:14:07 -0800641 /* Protocol violation: we explicitly clear errno, instead of
642 setting it to a POSIX error. Callers should use GetLastError. */
Steve Dowerf2f373f2015-02-21 08:44:05 -0800643 errno = 0;
644
645 if (h == INVALID_HANDLE_VALUE) {
Steve Dower8acde7d2015-03-07 18:14:07 -0800646 /* This is really a C library error (invalid file handle).
647 We set the Win32 error to the closes one matching. */
648 SetLastError(ERROR_INVALID_HANDLE);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800649 return -1;
650 }
651 memset(result, 0, sizeof(*result));
652
653 type = GetFileType(h);
654 if (type == FILE_TYPE_UNKNOWN) {
655 DWORD error = GetLastError();
656 if (error != 0) {
657 return -1;
658 }
659 /* else: valid but unknown file */
660 }
661
662 if (type != FILE_TYPE_DISK) {
663 if (type == FILE_TYPE_CHAR)
664 result->st_mode = _S_IFCHR;
665 else if (type == FILE_TYPE_PIPE)
666 result->st_mode = _S_IFIFO;
667 return 0;
668 }
669
670 if (!GetFileInformationByHandle(h, &info)) {
671 return -1;
672 }
673
Steve Dowera2af1a52015-02-21 10:04:10 -0800674 _Py_attribute_data_to_stat(&info, 0, result);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800675 /* specific to fstat() */
676 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
677 return 0;
678#else
679 return fstat(fd, result);
680#endif
681}
682#endif /* HAVE_FSTAT || MS_WINDOWS */
683
684
685#ifdef HAVE_STAT
Victor Stinner6672d0c2010-10-07 22:53:43 +0000686/* Call _wstat() on Windows, or encode the path to the filesystem encoding and
687 call stat() otherwise. Only fill st_mode attribute on Windows.
688
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100689 Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception was
690 raised. */
Victor Stinner4e314432010-10-07 21:45:39 +0000691
692int
Victor Stinnera4a75952010-10-07 22:23:10 +0000693_Py_stat(PyObject *path, struct stat *statbuf)
Victor Stinner4e314432010-10-07 21:45:39 +0000694{
695#ifdef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +0000696 int err;
697 struct _stat wstatbuf;
Victor Stinneree587ea2011-11-17 00:51:38 +0100698 wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +0000699
Victor Stinneree587ea2011-11-17 00:51:38 +0100700 wpath = PyUnicode_AsUnicode(path);
701 if (wpath == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100702 return -2;
Victor Stinneree587ea2011-11-17 00:51:38 +0100703 err = _wstat(wpath, &wstatbuf);
Victor Stinner4e314432010-10-07 21:45:39 +0000704 if (!err)
705 statbuf->st_mode = wstatbuf.st_mode;
706 return err;
707#else
708 int ret;
Victor Stinnera4a75952010-10-07 22:23:10 +0000709 PyObject *bytes = PyUnicode_EncodeFSDefault(path);
Victor Stinner4e314432010-10-07 21:45:39 +0000710 if (bytes == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100711 return -2;
Victor Stinner4e314432010-10-07 21:45:39 +0000712 ret = stat(PyBytes_AS_STRING(bytes), statbuf);
713 Py_DECREF(bytes);
714 return ret;
715#endif
716}
717
Steve Dowerf2f373f2015-02-21 08:44:05 -0800718#endif /* HAVE_STAT */
719
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100720
Antoine Pitrou409b5382013-10-12 22:41:17 +0200721static int
Victor Stinnerdaf45552013-08-28 00:53:59 +0200722get_inheritable(int fd, int raise)
723{
724#ifdef MS_WINDOWS
725 HANDLE handle;
726 DWORD flags;
Victor Stinner6672d0c2010-10-07 22:53:43 +0000727
Victor Stinnerdaf45552013-08-28 00:53:59 +0200728 if (!_PyVerify_fd(fd)) {
729 if (raise)
730 PyErr_SetFromErrno(PyExc_OSError);
731 return -1;
732 }
733
734 handle = (HANDLE)_get_osfhandle(fd);
735 if (handle == INVALID_HANDLE_VALUE) {
736 if (raise)
Steve Dower41e72442015-03-14 11:38:27 -0700737 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200738 return -1;
739 }
740
741 if (!GetHandleInformation(handle, &flags)) {
742 if (raise)
743 PyErr_SetFromWindowsErr(0);
744 return -1;
745 }
746
747 return (flags & HANDLE_FLAG_INHERIT);
748#else
749 int flags;
750
751 flags = fcntl(fd, F_GETFD, 0);
752 if (flags == -1) {
753 if (raise)
754 PyErr_SetFromErrno(PyExc_OSError);
755 return -1;
756 }
757 return !(flags & FD_CLOEXEC);
758#endif
759}
760
761/* Get the inheritable flag of the specified file descriptor.
Victor Stinnerb034eee2013-09-07 10:36:04 +0200762 Return 1 if the file descriptor can be inherited, 0 if it cannot,
Victor Stinnerdaf45552013-08-28 00:53:59 +0200763 raise an exception and return -1 on error. */
764int
765_Py_get_inheritable(int fd)
766{
767 return get_inheritable(fd, 1);
768}
769
770static int
771set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
772{
773#ifdef MS_WINDOWS
774 HANDLE handle;
775 DWORD flags;
Victor Stinner282124b2014-09-02 11:41:04 +0200776#else
777#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
778 static int ioctl_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200779 int request;
780 int err;
Victor Stinner282124b2014-09-02 11:41:04 +0200781#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +0200782 int flags;
783 int res;
784#endif
785
786 /* atomic_flag_works can only be used to make the file descriptor
787 non-inheritable */
788 assert(!(atomic_flag_works != NULL && inheritable));
789
790 if (atomic_flag_works != NULL && !inheritable) {
791 if (*atomic_flag_works == -1) {
Steve Dower41e72442015-03-14 11:38:27 -0700792 int isInheritable = get_inheritable(fd, raise);
793 if (isInheritable == -1)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200794 return -1;
Steve Dower41e72442015-03-14 11:38:27 -0700795 *atomic_flag_works = !isInheritable;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200796 }
797
798 if (*atomic_flag_works)
799 return 0;
800 }
801
802#ifdef MS_WINDOWS
803 if (!_PyVerify_fd(fd)) {
804 if (raise)
805 PyErr_SetFromErrno(PyExc_OSError);
806 return -1;
807 }
808
809 handle = (HANDLE)_get_osfhandle(fd);
810 if (handle == INVALID_HANDLE_VALUE) {
811 if (raise)
Steve Dower41e72442015-03-14 11:38:27 -0700812 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200813 return -1;
814 }
815
816 if (inheritable)
817 flags = HANDLE_FLAG_INHERIT;
818 else
819 flags = 0;
820 if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) {
821 if (raise)
822 PyErr_SetFromWindowsErr(0);
823 return -1;
824 }
825 return 0;
826
Victor Stinnerdaf45552013-08-28 00:53:59 +0200827#else
Victor Stinner282124b2014-09-02 11:41:04 +0200828
829#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
830 if (ioctl_works != 0) {
831 /* fast-path: ioctl() only requires one syscall */
832 if (inheritable)
833 request = FIONCLEX;
834 else
835 request = FIOCLEX;
836 err = ioctl(fd, request, NULL);
837 if (!err) {
838 ioctl_works = 1;
839 return 0;
840 }
841
842 if (errno != ENOTTY) {
843 if (raise)
844 PyErr_SetFromErrno(PyExc_OSError);
845 return -1;
846 }
847 else {
848 /* Issue #22258: Here, ENOTTY means "Inappropriate ioctl for
849 device". The ioctl is declared but not supported by the kernel.
850 Remember that ioctl() doesn't work. It is the case on
851 Illumos-based OS for example. */
852 ioctl_works = 0;
853 }
854 /* fallback to fcntl() if ioctl() does not work */
855 }
856#endif
857
858 /* slow-path: fcntl() requires two syscalls */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200859 flags = fcntl(fd, F_GETFD);
860 if (flags < 0) {
861 if (raise)
862 PyErr_SetFromErrno(PyExc_OSError);
863 return -1;
864 }
865
866 if (inheritable)
867 flags &= ~FD_CLOEXEC;
868 else
869 flags |= FD_CLOEXEC;
870 res = fcntl(fd, F_SETFD, flags);
871 if (res < 0) {
872 if (raise)
873 PyErr_SetFromErrno(PyExc_OSError);
874 return -1;
875 }
876 return 0;
877#endif
878}
879
880/* Make the file descriptor non-inheritable.
Victor Stinnerb034eee2013-09-07 10:36:04 +0200881 Return 0 on success, set errno and return -1 on error. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200882static int
883make_non_inheritable(int fd)
884{
885 return set_inheritable(fd, 0, 0, NULL);
886}
887
888/* Set the inheritable flag of the specified file descriptor.
889 On success: return 0, on error: raise an exception if raise is nonzero
890 and return -1.
891
892 If atomic_flag_works is not NULL:
893
894 * if *atomic_flag_works==-1, check if the inheritable is set on the file
895 descriptor: if yes, set *atomic_flag_works to 1, otherwise set to 0 and
896 set the inheritable flag
897 * if *atomic_flag_works==1: do nothing
898 * if *atomic_flag_works==0: set inheritable flag to False
899
900 Set atomic_flag_works to NULL if no atomic flag was used to create the
901 file descriptor.
902
903 atomic_flag_works can only be used to make a file descriptor
904 non-inheritable: atomic_flag_works must be NULL if inheritable=1. */
905int
906_Py_set_inheritable(int fd, int inheritable, int *atomic_flag_works)
907{
908 return set_inheritable(fd, inheritable, 1, atomic_flag_works);
909}
910
Victor Stinnera555cfc2015-03-18 00:22:14 +0100911static int
912_Py_open_impl(const char *pathname, int flags, int gil_held)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200913{
914 int fd;
Victor Stinnera47fc5c2015-03-18 09:52:54 +0100915 int async_err = 0;
Victor Stinnera555cfc2015-03-18 00:22:14 +0100916#ifndef MS_WINDOWS
Victor Stinnerdaf45552013-08-28 00:53:59 +0200917 int *atomic_flag_works;
Victor Stinnera555cfc2015-03-18 00:22:14 +0100918#endif
919
920#ifdef MS_WINDOWS
921 flags |= O_NOINHERIT;
922#elif defined(O_CLOEXEC)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200923 atomic_flag_works = &_Py_open_cloexec_works;
924 flags |= O_CLOEXEC;
925#else
926 atomic_flag_works = NULL;
927#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +0200928
Victor Stinnera555cfc2015-03-18 00:22:14 +0100929 if (gil_held) {
Victor Stinnera47fc5c2015-03-18 09:52:54 +0100930 do {
931 Py_BEGIN_ALLOW_THREADS
932 fd = open(pathname, flags);
933 Py_END_ALLOW_THREADS
934 } while (fd < 0
935 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
936 if (async_err)
937 return -1;
Victor Stinnera555cfc2015-03-18 00:22:14 +0100938 if (fd < 0) {
939 PyErr_SetFromErrnoWithFilename(PyExc_OSError, pathname);
940 return -1;
941 }
942 }
943 else {
944 fd = open(pathname, flags);
945 if (fd < 0)
946 return -1;
947 }
948
949#ifndef MS_WINDOWS
950 if (set_inheritable(fd, 0, gil_held, atomic_flag_works) < 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +0200951 close(fd);
952 return -1;
953 }
Victor Stinnera555cfc2015-03-18 00:22:14 +0100954#endif
955
Victor Stinnerdaf45552013-08-28 00:53:59 +0200956 return fd;
957}
958
Victor Stinnera555cfc2015-03-18 00:22:14 +0100959/* Open a file with the specified flags (wrapper to open() function).
960 Return a file descriptor on success. Raise an exception and return -1 on
961 error.
962
963 The file descriptor is created non-inheritable.
964
Victor Stinnera47fc5c2015-03-18 09:52:54 +0100965 When interrupted by a signal (open() fails with EINTR), retry the syscall,
966 except if the Python signal handler raises an exception.
967
Victor Stinnere42ccd22015-03-18 01:39:23 +0100968 The GIL must be held. */
Victor Stinnera555cfc2015-03-18 00:22:14 +0100969int
970_Py_open(const char *pathname, int flags)
971{
972 /* _Py_open() must be called with the GIL held. */
973 assert(PyGILState_Check());
974 return _Py_open_impl(pathname, flags, 1);
975}
976
977/* Open a file with the specified flags (wrapper to open() function).
978 Return a file descriptor on success. Set errno and return -1 on error.
979
Victor Stinnera47fc5c2015-03-18 09:52:54 +0100980 The file descriptor is created non-inheritable.
981
982 If interrupted by a signal, fail with EINTR. */
Victor Stinnera555cfc2015-03-18 00:22:14 +0100983int
984_Py_open_noraise(const char *pathname, int flags)
985{
986 return _Py_open_impl(pathname, flags, 0);
987}
988
Victor Stinnerdaf45552013-08-28 00:53:59 +0200989/* Open a file. Use _wfopen() on Windows, encode the path to the locale
Victor Stinnere42ccd22015-03-18 01:39:23 +0100990 encoding and use fopen() otherwise.
991
Victor Stinnera47fc5c2015-03-18 09:52:54 +0100992 The file descriptor is created non-inheritable.
993
994 If interrupted by a signal, fail with EINTR. */
Victor Stinner4e314432010-10-07 21:45:39 +0000995FILE *
996_Py_wfopen(const wchar_t *path, const wchar_t *mode)
997{
Victor Stinner4e314432010-10-07 21:45:39 +0000998 FILE *f;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200999#ifndef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +00001000 char *cpath;
1001 char cmode[10];
1002 size_t r;
1003 r = wcstombs(cmode, mode, 10);
1004 if (r == (size_t)-1 || r >= 10) {
1005 errno = EINVAL;
1006 return NULL;
1007 }
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001008 cpath = Py_EncodeLocale(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001009 if (cpath == NULL)
1010 return NULL;
1011 f = fopen(cpath, cmode);
1012 PyMem_Free(cpath);
Victor Stinner4e314432010-10-07 21:45:39 +00001013#else
Victor Stinnerdaf45552013-08-28 00:53:59 +02001014 f = _wfopen(path, mode);
Victor Stinner4e314432010-10-07 21:45:39 +00001015#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +02001016 if (f == NULL)
1017 return NULL;
1018 if (make_non_inheritable(fileno(f)) < 0) {
1019 fclose(f);
1020 return NULL;
1021 }
1022 return f;
Victor Stinner4e314432010-10-07 21:45:39 +00001023}
1024
Victor Stinnere42ccd22015-03-18 01:39:23 +01001025/* Wrapper to fopen().
1026
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001027 The file descriptor is created non-inheritable.
1028
1029 If interrupted by a signal, fail with EINTR. */
Victor Stinnerdaf45552013-08-28 00:53:59 +02001030FILE*
1031_Py_fopen(const char *pathname, const char *mode)
1032{
1033 FILE *f = fopen(pathname, mode);
1034 if (f == NULL)
1035 return NULL;
1036 if (make_non_inheritable(fileno(f)) < 0) {
1037 fclose(f);
1038 return NULL;
1039 }
1040 return f;
1041}
1042
1043/* Open a file. Call _wfopen() on Windows, or encode the path to the filesystem
Victor Stinnere42ccd22015-03-18 01:39:23 +01001044 encoding and call fopen() otherwise.
Victor Stinner6672d0c2010-10-07 22:53:43 +00001045
Victor Stinnere42ccd22015-03-18 01:39:23 +01001046 Return the new file object on success. Raise an exception and return NULL
1047 on error.
1048
1049 The file descriptor is created non-inheritable.
1050
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001051 When interrupted by a signal (open() fails with EINTR), retry the syscall,
1052 except if the Python signal handler raises an exception.
1053
Victor Stinnere42ccd22015-03-18 01:39:23 +01001054 The GIL must be held. */
Victor Stinner4e314432010-10-07 21:45:39 +00001055FILE*
Victor Stinnerdaf45552013-08-28 00:53:59 +02001056_Py_fopen_obj(PyObject *path, const char *mode)
Victor Stinner4e314432010-10-07 21:45:39 +00001057{
Victor Stinnerdaf45552013-08-28 00:53:59 +02001058 FILE *f;
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001059 int async_err = 0;
Victor Stinner4e314432010-10-07 21:45:39 +00001060#ifdef MS_WINDOWS
Victor Stinneree587ea2011-11-17 00:51:38 +01001061 wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +00001062 wchar_t wmode[10];
1063 int usize;
Victor Stinner4e314432010-10-07 21:45:39 +00001064
Victor Stinnere42ccd22015-03-18 01:39:23 +01001065 assert(PyGILState_Check());
1066
Antoine Pitrou0e576f12011-12-22 10:03:38 +01001067 if (!PyUnicode_Check(path)) {
1068 PyErr_Format(PyExc_TypeError,
1069 "str file path expected under Windows, got %R",
1070 Py_TYPE(path));
1071 return NULL;
1072 }
Victor Stinneree587ea2011-11-17 00:51:38 +01001073 wpath = PyUnicode_AsUnicode(path);
1074 if (wpath == NULL)
1075 return NULL;
1076
Victor Stinner4e314432010-10-07 21:45:39 +00001077 usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode));
Victor Stinnere42ccd22015-03-18 01:39:23 +01001078 if (usize == 0) {
1079 PyErr_SetFromWindowsErr(0);
Victor Stinner4e314432010-10-07 21:45:39 +00001080 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001081 }
Victor Stinner4e314432010-10-07 21:45:39 +00001082
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001083 do {
1084 Py_BEGIN_ALLOW_THREADS
1085 f = _wfopen(wpath, wmode);
1086 Py_END_ALLOW_THREADS
1087 } while (f == NULL
1088 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner4e314432010-10-07 21:45:39 +00001089#else
Antoine Pitrou2b1cc892011-12-19 18:19:06 +01001090 PyObject *bytes;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001091 char *path_bytes;
1092
1093 assert(PyGILState_Check());
1094
Antoine Pitrou2b1cc892011-12-19 18:19:06 +01001095 if (!PyUnicode_FSConverter(path, &bytes))
Victor Stinner4e314432010-10-07 21:45:39 +00001096 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001097 path_bytes = PyBytes_AS_STRING(bytes);
1098
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001099 do {
1100 Py_BEGIN_ALLOW_THREADS
1101 f = fopen(path_bytes, mode);
1102 Py_END_ALLOW_THREADS
1103 } while (f == NULL
1104 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnere42ccd22015-03-18 01:39:23 +01001105
Victor Stinner4e314432010-10-07 21:45:39 +00001106 Py_DECREF(bytes);
Victor Stinner4e314432010-10-07 21:45:39 +00001107#endif
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001108 if (async_err)
1109 return NULL;
1110
Victor Stinnere42ccd22015-03-18 01:39:23 +01001111 if (f == NULL) {
1112 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001113 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001114 }
1115
1116 if (set_inheritable(fileno(f), 0, 1, NULL) < 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +02001117 fclose(f);
1118 return NULL;
1119 }
1120 return f;
Victor Stinner4e314432010-10-07 21:45:39 +00001121}
1122
Victor Stinner66aab0c2015-03-19 22:53:20 +01001123/* Read count bytes from fd into buf.
1124 *
1125 * On success, return the number of read bytes, it can be lower than count.
1126 * If the current file offset is at or past the end of file, no bytes are read,
1127 * and read() returns zero.
1128 *
1129 * On error, raise an exception, set errno and return -1.
1130 *
1131 * When interrupted by a signal (read() fails with EINTR), retry the syscall.
1132 * If the Python signal handler raises an exception, the function returns -1
1133 * (the syscall is not retried).
1134 *
1135 * The GIL must be held. */
1136Py_ssize_t
1137_Py_read(int fd, void *buf, size_t count)
1138{
1139 Py_ssize_t n;
Victor Stinnera3c02022015-03-20 11:58:18 +01001140 int err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001141 int async_err = 0;
1142
1143 /* _Py_read() must not be called with an exception set, otherwise the
1144 * caller may think that read() was interrupted by a signal and the signal
1145 * handler raised an exception. */
1146 assert(!PyErr_Occurred());
1147
Victor Stinnerc1cf4f72015-03-19 23:53:04 +01001148 if (!_PyVerify_fd(fd)) {
Victor Stinnera3c02022015-03-20 11:58:18 +01001149 /* save/restore errno because PyErr_SetFromErrno() can modify it */
1150 err = errno;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001151 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera3c02022015-03-20 11:58:18 +01001152 errno = err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001153 return -1;
1154 }
1155
1156#ifdef MS_WINDOWS
1157 if (count > INT_MAX) {
1158 /* On Windows, the count parameter of read() is an int */
1159 count = INT_MAX;
1160 }
1161#else
1162 if (count > PY_SSIZE_T_MAX) {
1163 /* if count is greater than PY_SSIZE_T_MAX,
1164 * read() result is undefined */
1165 count = PY_SSIZE_T_MAX;
1166 }
1167#endif
1168
1169 do {
1170 Py_BEGIN_ALLOW_THREADS
1171 errno = 0;
1172#ifdef MS_WINDOWS
1173 n = read(fd, buf, (int)count);
1174#else
1175 n = read(fd, buf, count);
1176#endif
Victor Stinnera3c02022015-03-20 11:58:18 +01001177 /* save/restore errno because PyErr_CheckSignals()
1178 * and PyErr_SetFromErrno() can modify it */
1179 err = errno;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001180 Py_END_ALLOW_THREADS
Victor Stinnera3c02022015-03-20 11:58:18 +01001181 } while (n < 0 && err == EINTR &&
Victor Stinner66aab0c2015-03-19 22:53:20 +01001182 !(async_err = PyErr_CheckSignals()));
1183
1184 if (async_err) {
1185 /* read() was interrupted by a signal (failed with EINTR)
1186 * and the Python signal handler raised an exception */
Victor Stinnera3c02022015-03-20 11:58:18 +01001187 errno = err;
1188 assert(errno == EINTR && PyErr_Occurred());
Victor Stinner66aab0c2015-03-19 22:53:20 +01001189 return -1;
1190 }
1191 if (n < 0) {
Victor Stinner66aab0c2015-03-19 22:53:20 +01001192 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera3c02022015-03-20 11:58:18 +01001193 errno = err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001194 return -1;
1195 }
1196
1197 return n;
1198}
1199
1200/* Write count bytes of buf into fd.
1201 *
1202 * -On success, return the number of written bytes, it can be lower than count
1203 * including 0
1204 * - On error, raise an exception, set errno and return -1.
1205 *
1206 * When interrupted by a signal (write() fails with EINTR), retry the syscall.
1207 * If the Python signal handler raises an exception, the function returns -1
1208 * (the syscall is not retried).
1209 *
1210 * The GIL must be held. */
1211Py_ssize_t
1212_Py_write(int fd, const void *buf, size_t count)
1213{
1214 Py_ssize_t n;
Victor Stinnera3c02022015-03-20 11:58:18 +01001215 int err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001216 int async_err = 0;
1217
1218 /* _Py_write() must not be called with an exception set, otherwise the
1219 * caller may think that write() was interrupted by a signal and the signal
1220 * handler raised an exception. */
1221 assert(!PyErr_Occurred());
1222
1223 if (!_PyVerify_fd(fd)) {
Victor Stinnera3c02022015-03-20 11:58:18 +01001224 /* save/restore errno because PyErr_SetFromErrno() can modify it */
1225 err = errno;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001226 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera3c02022015-03-20 11:58:18 +01001227 errno = err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001228 return -1;
1229 }
1230
1231#ifdef MS_WINDOWS
1232 if (count > 32767 && isatty(fd)) {
1233 /* Issue #11395: the Windows console returns an error (12: not
1234 enough space error) on writing into stdout if stdout mode is
1235 binary and the length is greater than 66,000 bytes (or less,
1236 depending on heap usage). */
1237 count = 32767;
1238 }
1239 else if (count > INT_MAX)
1240 count = INT_MAX;
1241#else
1242 if (count > PY_SSIZE_T_MAX) {
1243 /* write() should truncate count to PY_SSIZE_T_MAX, but it's safer
1244 * to do it ourself to have a portable behaviour. */
1245 count = PY_SSIZE_T_MAX;
1246 }
1247#endif
1248
1249 do {
1250 Py_BEGIN_ALLOW_THREADS
1251 errno = 0;
1252#ifdef MS_WINDOWS
1253 n = write(fd, buf, (int)count);
1254#else
1255 n = write(fd, buf, count);
1256#endif
Victor Stinnera3c02022015-03-20 11:58:18 +01001257 /* save/restore errno because PyErr_CheckSignals()
1258 * and PyErr_SetFromErrno() can modify it */
1259 err = errno;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001260 Py_END_ALLOW_THREADS
1261 } while (n < 0 && errno == EINTR &&
1262 !(async_err = PyErr_CheckSignals()));
1263
1264 if (async_err) {
1265 /* write() was interrupted by a signal (failed with EINTR)
1266 * and the Python signal handler raised an exception */
Victor Stinnera3c02022015-03-20 11:58:18 +01001267 errno = err;
1268 assert(errno == EINTR && PyErr_Occurred());
Victor Stinner66aab0c2015-03-19 22:53:20 +01001269 return -1;
1270 }
1271 if (n < 0) {
Victor Stinner66aab0c2015-03-19 22:53:20 +01001272 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera3c02022015-03-20 11:58:18 +01001273 errno = err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001274 return -1;
1275 }
1276
1277 return n;
1278}
1279
Victor Stinner4e314432010-10-07 21:45:39 +00001280#ifdef HAVE_READLINK
Victor Stinner6672d0c2010-10-07 22:53:43 +00001281
1282/* Read value of symbolic link. Encode the path to the locale encoding, decode
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001283 the result from the locale encoding. Return -1 on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001284
Victor Stinner4e314432010-10-07 21:45:39 +00001285int
1286_Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
1287{
1288 char *cpath;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001289 char cbuf[MAXPATHLEN];
Victor Stinner3f711f42010-10-16 22:47:37 +00001290 wchar_t *wbuf;
Victor Stinner4e314432010-10-07 21:45:39 +00001291 int res;
1292 size_t r1;
1293
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001294 cpath = Py_EncodeLocale(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001295 if (cpath == NULL) {
1296 errno = EINVAL;
1297 return -1;
1298 }
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001299 res = (int)readlink(cpath, cbuf, Py_ARRAY_LENGTH(cbuf));
Victor Stinner4e314432010-10-07 21:45:39 +00001300 PyMem_Free(cpath);
1301 if (res == -1)
1302 return -1;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001303 if (res == Py_ARRAY_LENGTH(cbuf)) {
Victor Stinner4e314432010-10-07 21:45:39 +00001304 errno = EINVAL;
1305 return -1;
1306 }
1307 cbuf[res] = '\0'; /* buf will be null terminated */
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001308 wbuf = Py_DecodeLocale(cbuf, &r1);
Victor Stinner350147b2010-10-16 22:52:09 +00001309 if (wbuf == NULL) {
1310 errno = EINVAL;
1311 return -1;
1312 }
Victor Stinner3f711f42010-10-16 22:47:37 +00001313 if (bufsiz <= r1) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001314 PyMem_RawFree(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001315 errno = EINVAL;
1316 return -1;
1317 }
Victor Stinner3f711f42010-10-16 22:47:37 +00001318 wcsncpy(buf, wbuf, bufsiz);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001319 PyMem_RawFree(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001320 return (int)r1;
1321}
1322#endif
1323
1324#ifdef HAVE_REALPATH
Victor Stinner6672d0c2010-10-07 22:53:43 +00001325
1326/* Return the canonicalized absolute pathname. Encode path to the locale
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001327 encoding, decode the result from the locale encoding.
1328 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001329
Victor Stinner4e314432010-10-07 21:45:39 +00001330wchar_t*
Victor Stinner015f4d82010-10-07 22:29:53 +00001331_Py_wrealpath(const wchar_t *path,
1332 wchar_t *resolved_path, size_t resolved_path_size)
Victor Stinner4e314432010-10-07 21:45:39 +00001333{
1334 char *cpath;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001335 char cresolved_path[MAXPATHLEN];
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001336 wchar_t *wresolved_path;
Victor Stinner4e314432010-10-07 21:45:39 +00001337 char *res;
1338 size_t r;
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001339 cpath = Py_EncodeLocale(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001340 if (cpath == NULL) {
1341 errno = EINVAL;
1342 return NULL;
1343 }
1344 res = realpath(cpath, cresolved_path);
1345 PyMem_Free(cpath);
1346 if (res == NULL)
1347 return NULL;
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001348
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001349 wresolved_path = Py_DecodeLocale(cresolved_path, &r);
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001350 if (wresolved_path == NULL) {
Victor Stinner4e314432010-10-07 21:45:39 +00001351 errno = EINVAL;
1352 return NULL;
1353 }
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001354 if (resolved_path_size <= r) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001355 PyMem_RawFree(wresolved_path);
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001356 errno = EINVAL;
1357 return NULL;
1358 }
1359 wcsncpy(resolved_path, wresolved_path, resolved_path_size);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001360 PyMem_RawFree(wresolved_path);
Victor Stinner4e314432010-10-07 21:45:39 +00001361 return resolved_path;
1362}
1363#endif
1364
Victor Stinnerf4061da2010-10-14 12:37:19 +00001365/* Get the current directory. size is the buffer size in wide characters
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001366 including the null character. Decode the path from the locale encoding.
1367 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001368
Victor Stinner4e314432010-10-07 21:45:39 +00001369wchar_t*
1370_Py_wgetcwd(wchar_t *buf, size_t size)
1371{
1372#ifdef MS_WINDOWS
Victor Stinner56785ea2013-06-05 00:46:29 +02001373 int isize = (int)Py_MIN(size, INT_MAX);
1374 return _wgetcwd(buf, isize);
Victor Stinner4e314432010-10-07 21:45:39 +00001375#else
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001376 char fname[MAXPATHLEN];
Victor Stinnerf4061da2010-10-14 12:37:19 +00001377 wchar_t *wname;
Victor Stinner168e1172010-10-16 23:16:16 +00001378 size_t len;
Victor Stinnerf4061da2010-10-14 12:37:19 +00001379
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001380 if (getcwd(fname, Py_ARRAY_LENGTH(fname)) == NULL)
Victor Stinner4e314432010-10-07 21:45:39 +00001381 return NULL;
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001382 wname = Py_DecodeLocale(fname, &len);
Victor Stinnerf4061da2010-10-14 12:37:19 +00001383 if (wname == NULL)
1384 return NULL;
Victor Stinner168e1172010-10-16 23:16:16 +00001385 if (size <= len) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001386 PyMem_RawFree(wname);
Victor Stinner4e314432010-10-07 21:45:39 +00001387 return NULL;
1388 }
Victor Stinnerf4061da2010-10-14 12:37:19 +00001389 wcsncpy(buf, wname, size);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001390 PyMem_RawFree(wname);
Victor Stinner4e314432010-10-07 21:45:39 +00001391 return buf;
1392#endif
1393}
1394
Victor Stinnerdaf45552013-08-28 00:53:59 +02001395/* Duplicate a file descriptor. The new file descriptor is created as
1396 non-inheritable. Return a new file descriptor on success, raise an OSError
1397 exception and return -1 on error.
1398
1399 The GIL is released to call dup(). The caller must hold the GIL. */
1400int
1401_Py_dup(int fd)
1402{
1403#ifdef MS_WINDOWS
1404 HANDLE handle;
1405 DWORD ftype;
1406#endif
1407
1408 if (!_PyVerify_fd(fd)) {
1409 PyErr_SetFromErrno(PyExc_OSError);
1410 return -1;
1411 }
1412
1413#ifdef MS_WINDOWS
1414 handle = (HANDLE)_get_osfhandle(fd);
1415 if (handle == INVALID_HANDLE_VALUE) {
Steve Dower41e72442015-03-14 11:38:27 -07001416 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001417 return -1;
1418 }
1419
1420 /* get the file type, ignore the error if it failed */
1421 ftype = GetFileType(handle);
1422
1423 Py_BEGIN_ALLOW_THREADS
1424 fd = dup(fd);
1425 Py_END_ALLOW_THREADS
1426 if (fd < 0) {
1427 PyErr_SetFromErrno(PyExc_OSError);
1428 return -1;
1429 }
1430
1431 /* Character files like console cannot be make non-inheritable */
1432 if (ftype != FILE_TYPE_CHAR) {
1433 if (_Py_set_inheritable(fd, 0, NULL) < 0) {
1434 close(fd);
1435 return -1;
1436 }
1437 }
1438#elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC)
1439 Py_BEGIN_ALLOW_THREADS
1440 fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1441 Py_END_ALLOW_THREADS
1442 if (fd < 0) {
1443 PyErr_SetFromErrno(PyExc_OSError);
1444 return -1;
1445 }
1446
1447#else
1448 Py_BEGIN_ALLOW_THREADS
1449 fd = dup(fd);
1450 Py_END_ALLOW_THREADS
1451 if (fd < 0) {
1452 PyErr_SetFromErrno(PyExc_OSError);
1453 return -1;
1454 }
1455
1456 if (_Py_set_inheritable(fd, 0, NULL) < 0) {
1457 close(fd);
1458 return -1;
1459 }
1460#endif
1461 return fd;
1462}
1463
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001464#ifndef MS_WINDOWS
1465/* Get the blocking mode of the file descriptor.
1466 Return 0 if the O_NONBLOCK flag is set, 1 if the flag is cleared,
1467 raise an exception and return -1 on error. */
1468int
1469_Py_get_blocking(int fd)
1470{
1471 int flags = fcntl(fd, F_GETFL, 0);
1472 if (flags < 0) {
1473 PyErr_SetFromErrno(PyExc_OSError);
1474 return -1;
1475 }
1476
1477 return !(flags & O_NONBLOCK);
1478}
1479
1480/* Set the blocking mode of the specified file descriptor.
1481
1482 Set the O_NONBLOCK flag if blocking is False, clear the O_NONBLOCK flag
1483 otherwise.
1484
1485 Return 0 on success, raise an exception and return -1 on error. */
1486int
1487_Py_set_blocking(int fd, int blocking)
1488{
1489#if defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)
1490 int arg = !blocking;
1491 if (ioctl(fd, FIONBIO, &arg) < 0)
1492 goto error;
1493#else
1494 int flags, res;
1495
1496 flags = fcntl(fd, F_GETFL, 0);
1497 if (flags < 0)
1498 goto error;
1499
1500 if (blocking)
1501 flags = flags & (~O_NONBLOCK);
1502 else
1503 flags = flags | O_NONBLOCK;
1504
1505 res = fcntl(fd, F_SETFL, flags);
1506 if (res < 0)
1507 goto error;
1508#endif
1509 return 0;
1510
1511error:
1512 PyErr_SetFromErrno(PyExc_OSError);
1513 return -1;
1514}
1515#endif
1516
Steve Dowerd81431f2015-03-06 14:47:02 -08001517#ifdef _MSC_VER
1518#if _MSC_VER >= 1900
1519
1520/* This function lets the Windows CRT validate the file handle without
1521 terminating the process if it's invalid. */
1522int
1523_PyVerify_fd(int fd)
1524{
1525 intptr_t osh;
1526 /* Fast check for the only condition we know */
1527 if (fd < 0) {
1528 _set_errno(EBADF);
1529 return 0;
1530 }
1531 osh = _get_osfhandle(fd);
1532 return osh != (intptr_t)-1;
1533}
1534
1535#elif _MSC_VER >= 1400
1536/* Legacy implementation of _PyVerify_fd while transitioning to
1537 * MSVC 14.0. This should eventually be removed. (issue23524)
1538 */
1539
1540/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
1541 * valid and raise an assertion if it isn't.
1542 * Normally, an invalid fd is likely to be a C program error and therefore
1543 * an assertion can be useful, but it does contradict the POSIX standard
1544 * which for write(2) states:
1545 * "Otherwise, -1 shall be returned and errno set to indicate the error."
1546 * "[EBADF] The fildes argument is not a valid file descriptor open for
1547 * writing."
1548 * Furthermore, python allows the user to enter any old integer
1549 * as a fd and should merely raise a python exception on error.
1550 * The Microsoft CRT doesn't provide an official way to check for the
1551 * validity of a file descriptor, but we can emulate its internal behaviour
1552 * by using the exported __pinfo data member and knowledge of the
1553 * internal structures involved.
1554 * The structures below must be updated for each version of visual studio
1555 * according to the file internal.h in the CRT source, until MS comes
1556 * up with a less hacky way to do this.
1557 * (all of this is to avoid globally modifying the CRT behaviour using
1558 * _set_invalid_parameter_handler() and _CrtSetReportMode())
1559 */
1560/* The actual size of the structure is determined at runtime.
1561 * Only the first items must be present.
1562 */
1563typedef struct {
1564 intptr_t osfhnd;
1565 char osfile;
1566} my_ioinfo;
1567
1568extern __declspec(dllimport) char * __pioinfo[];
1569#define IOINFO_L2E 5
1570#define IOINFO_ARRAYS 64
1571#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
1572#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
1573#define FOPEN 0x01
1574#define _NO_CONSOLE_FILENO (intptr_t)-2
1575
1576/* This function emulates what the windows CRT does to validate file handles */
1577int
1578_PyVerify_fd(int fd)
1579{
1580 const int i1 = fd >> IOINFO_L2E;
1581 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
1582
1583 static size_t sizeof_ioinfo = 0;
1584
1585 /* Determine the actual size of the ioinfo structure,
1586 * as used by the CRT loaded in memory
1587 */
1588 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
1589 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
1590 }
1591 if (sizeof_ioinfo == 0) {
1592 /* This should not happen... */
1593 goto fail;
1594 }
1595
1596 /* See that it isn't a special CLEAR fileno */
1597 if (fd != _NO_CONSOLE_FILENO) {
1598 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
1599 * we check pointer validity and other info
1600 */
1601 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
1602 /* finally, check that the file is open */
1603 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
1604 if (info->osfile & FOPEN) {
1605 return 1;
1606 }
1607 }
1608 }
1609 fail:
1610 errno = EBADF;
1611 return 0;
1612}
1613
1614#endif /* _MSC_VER >= 1900 || _MSC_VER >= 1400 */
1615#endif /* defined _MSC_VER */