blob: 033c2ff71b913362bce2227d7a103bb5ac2b9c1a [file] [log] [blame]
Victor Stinner4e314432010-10-07 21:45:39 +00001#include "Python.h"
Victor Stinner9fc57a32018-11-07 00:44:03 +01002#include "pycore_fileutils.h"
Stefan Krah6df5cae2012-11-12 20:14:36 +01003#include "osdefs.h"
Stefan Krah6c01e382014-01-20 15:31:08 +01004#include <locale.h>
5
Victor Stinnerb306d752010-10-07 22:09:40 +00006#ifdef MS_WINDOWS
Steve Dowerd81431f2015-03-06 14:47:02 -08007# include <malloc.h>
Victor Stinnerb306d752010-10-07 22:09:40 +00008# include <windows.h>
Steve Dower8fc89802015-04-12 00:26:27 -04009extern int winerror_to_errno(int);
Victor Stinnerb306d752010-10-07 22:09:40 +000010#endif
Victor Stinner4e314432010-10-07 21:45:39 +000011
Brett Cannonefb00c02012-02-29 18:31:31 -050012#ifdef HAVE_LANGINFO_H
13#include <langinfo.h>
14#endif
15
Victor Stinnerdaf45552013-08-28 00:53:59 +020016#ifdef HAVE_SYS_IOCTL_H
17#include <sys/ioctl.h>
18#endif
19
20#ifdef HAVE_FCNTL_H
21#include <fcntl.h>
22#endif /* HAVE_FCNTL_H */
23
Victor Stinnerdaf45552013-08-28 00:53:59 +020024#ifdef O_CLOEXEC
Victor Stinnerb034eee2013-09-07 10:36:04 +020025/* Does open() support the O_CLOEXEC flag? Possible values:
Victor Stinnerdaf45552013-08-28 00:53:59 +020026
27 -1: unknown
28 0: open() ignores O_CLOEXEC flag, ex: Linux kernel older than 2.6.23
29 1: open() supports O_CLOEXEC flag, close-on-exec is set
30
Victor Stinnera555cfc2015-03-18 00:22:14 +010031 The flag is used by _Py_open(), _Py_open_noraise(), io.FileIO
32 and os.open(). */
Victor Stinnerdaf45552013-08-28 00:53:59 +020033int _Py_open_cloexec_works = -1;
34#endif
35
Victor Stinner3d4226a2018-08-29 22:21:32 +020036
37static int
38get_surrogateescape(_Py_error_handler errors, int *surrogateescape)
39{
40 switch (errors)
41 {
42 case _Py_ERROR_STRICT:
43 *surrogateescape = 0;
44 return 0;
45 case _Py_ERROR_SURROGATEESCAPE:
46 *surrogateescape = 1;
47 return 0;
48 default:
49 return -1;
50 }
51}
52
53
Brett Cannonefb00c02012-02-29 18:31:31 -050054PyObject *
55_Py_device_encoding(int fd)
56{
Victor Stinner14b9b112013-06-25 00:37:25 +020057#if defined(MS_WINDOWS)
Brett Cannonefb00c02012-02-29 18:31:31 -050058 UINT cp;
59#endif
Steve Dower8fc89802015-04-12 00:26:27 -040060 int valid;
61 _Py_BEGIN_SUPPRESS_IPH
Steve Dower940f33a2016-09-08 11:21:54 -070062 valid = isatty(fd);
Steve Dower8fc89802015-04-12 00:26:27 -040063 _Py_END_SUPPRESS_IPH
64 if (!valid)
Brett Cannonefb00c02012-02-29 18:31:31 -050065 Py_RETURN_NONE;
Steve Dower8fc89802015-04-12 00:26:27 -040066
Victor Stinner14b9b112013-06-25 00:37:25 +020067#if defined(MS_WINDOWS)
Brett Cannonefb00c02012-02-29 18:31:31 -050068 if (fd == 0)
69 cp = GetConsoleCP();
70 else if (fd == 1 || fd == 2)
71 cp = GetConsoleOutputCP();
72 else
73 cp = 0;
74 /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
75 has no console */
76 if (cp != 0)
77 return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
78#elif defined(CODESET)
79 {
80 char *codeset = nl_langinfo(CODESET);
81 if (codeset != NULL && codeset[0] != 0)
82 return PyUnicode_FromString(codeset);
83 }
84#endif
85 Py_RETURN_NONE;
86}
87
Victor Stinner7ed7aea2018-01-15 10:45:49 +010088#if !defined(__APPLE__) && !defined(__ANDROID__) && !defined(MS_WINDOWS)
89
90#define USE_FORCE_ASCII
91
Victor Stinnerd45c7f82012-12-04 01:34:47 +010092extern int _Py_normalize_encoding(const char *, char *, size_t);
93
Victor Stinnerd500e532018-08-28 17:27:36 +020094/* Workaround FreeBSD and OpenIndiana locale encoding issue with the C locale
95 and POSIX locale. nl_langinfo(CODESET) announces an alias of the
Victor Stinnerd45c7f82012-12-04 01:34:47 +010096 ASCII encoding, whereas mbstowcs() and wcstombs() functions use the
97 ISO-8859-1 encoding. The problem is that os.fsencode() and os.fsdecode() use
98 locale.getpreferredencoding() codec. For example, if command line arguments
99 are decoded by mbstowcs() and encoded back by os.fsencode(), we get a
100 UnicodeEncodeError instead of retrieving the original byte string.
101
102 The workaround is enabled if setlocale(LC_CTYPE, NULL) returns "C",
103 nl_langinfo(CODESET) announces "ascii" (or an alias to ASCII), and at least
104 one byte in range 0x80-0xff can be decoded from the locale encoding. The
105 workaround is also enabled on error, for example if getting the locale
106 failed.
107
Victor Stinnerd500e532018-08-28 17:27:36 +0200108 On HP-UX with the C locale or the POSIX locale, nl_langinfo(CODESET)
109 announces "roman8" but mbstowcs() uses Latin1 in practice. Force also the
110 ASCII encoding in this case.
111
Philip Jenvey215c49a2013-01-15 13:24:12 -0800112 Values of force_ascii:
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100113
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200114 1: the workaround is used: Py_EncodeLocale() uses
115 encode_ascii_surrogateescape() and Py_DecodeLocale() uses
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100116 decode_ascii()
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200117 0: the workaround is not used: Py_EncodeLocale() uses wcstombs() and
118 Py_DecodeLocale() uses mbstowcs()
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100119 -1: unknown, need to call check_force_ascii() to get the value
120*/
121static int force_ascii = -1;
122
123static int
124check_force_ascii(void)
125{
Victor Stinnerd500e532018-08-28 17:27:36 +0200126 char *loc = setlocale(LC_CTYPE, NULL);
127 if (loc == NULL) {
128 goto error;
129 }
130 if (strcmp(loc, "C") != 0 && strcmp(loc, "POSIX") != 0) {
131 /* the LC_CTYPE locale is different than C and POSIX */
132 return 0;
133 }
134
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100135#if defined(HAVE_LANGINFO_H) && defined(CODESET)
Victor Stinnerd500e532018-08-28 17:27:36 +0200136 const char *codeset = nl_langinfo(CODESET);
137 if (!codeset || codeset[0] == '\0') {
138 /* CODESET is not set or empty */
139 goto error;
140 }
141
Victor Stinner54de2b12016-09-09 23:11:52 -0700142 char encoding[20]; /* longest name: "iso_646.irv_1991\0" */
Victor Stinnerd500e532018-08-28 17:27:36 +0200143 if (!_Py_normalize_encoding(codeset, encoding, sizeof(encoding))) {
144 goto error;
145 }
146
147#ifdef __hpux
148 if (strcmp(encoding, "roman8") == 0) {
149 unsigned char ch;
150 wchar_t wch;
151 size_t res;
152
153 ch = (unsigned char)0xA7;
154 res = mbstowcs(&wch, (char*)&ch, 1);
155 if (res != (size_t)-1 && wch == L'\xA7') {
156 /* On HP-UX withe C locale or the POSIX locale,
157 nl_langinfo(CODESET) announces "roman8", whereas mbstowcs() uses
158 Latin1 encoding in practice. Force ASCII in this case.
159
160 Roman8 decodes 0xA7 to U+00CF. Latin1 decodes 0xA7 to U+00A7. */
161 return 1;
162 }
163 }
164#else
165 const char* ascii_aliases[] = {
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100166 "ascii",
Victor Stinner54de2b12016-09-09 23:11:52 -0700167 /* Aliases from Lib/encodings/aliases.py */
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100168 "646",
Victor Stinner54de2b12016-09-09 23:11:52 -0700169 "ansi_x3.4_1968",
170 "ansi_x3.4_1986",
171 "ansi_x3_4_1968",
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100172 "cp367",
173 "csascii",
174 "ibm367",
Victor Stinner54de2b12016-09-09 23:11:52 -0700175 "iso646_us",
176 "iso_646.irv_1991",
177 "iso_ir_6",
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100178 "us",
Victor Stinner54de2b12016-09-09 23:11:52 -0700179 "us_ascii",
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100180 NULL
181 };
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100182
Victor Stinnerd500e532018-08-28 17:27:36 +0200183 int is_ascii = 0;
184 for (const char **alias=ascii_aliases; *alias != NULL; alias++) {
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100185 if (strcmp(encoding, *alias) == 0) {
186 is_ascii = 1;
187 break;
188 }
189 }
190 if (!is_ascii) {
191 /* nl_langinfo(CODESET) is not "ascii" or an alias of ASCII */
192 return 0;
193 }
194
Victor Stinnerd500e532018-08-28 17:27:36 +0200195 for (unsigned int i=0x80; i<=0xff; i++) {
196 char ch[1];
197 wchar_t wch[1];
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100198 size_t res;
199
Victor Stinnerd500e532018-08-28 17:27:36 +0200200 unsigned uch = (unsigned char)i;
201 ch[0] = (char)uch;
202 res = mbstowcs(wch, ch, 1);
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100203 if (res != (size_t)-1) {
204 /* decoding a non-ASCII character from the locale encoding succeed:
205 the locale encoding is not ASCII, force ASCII */
206 return 1;
207 }
208 }
209 /* None of the bytes in the range 0x80-0xff can be decoded from the locale
210 encoding: the locale encoding is really ASCII */
Victor Stinnerd500e532018-08-28 17:27:36 +0200211#endif /* !defined(__hpux) */
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100212 return 0;
213#else
214 /* nl_langinfo(CODESET) is not available: always force ASCII */
215 return 1;
Victor Stinnerd500e532018-08-28 17:27:36 +0200216#endif /* defined(HAVE_LANGINFO_H) && defined(CODESET) */
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100217
218error:
Martin Panter46f50722016-05-26 05:35:26 +0000219 /* if an error occurred, force the ASCII encoding */
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100220 return 1;
221}
222
Victor Stinnerd500e532018-08-28 17:27:36 +0200223
224int
225_Py_GetForceASCII(void)
226{
227 if (force_ascii == -1) {
228 force_ascii = check_force_ascii();
229 }
230 return force_ascii;
231}
232
233
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100234static int
235encode_ascii(const wchar_t *text, char **str,
236 size_t *error_pos, const char **reason,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200237 int raw_malloc, _Py_error_handler errors)
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100238{
239 char *result = NULL, *out;
240 size_t len, i;
241 wchar_t ch;
242
Victor Stinner3d4226a2018-08-29 22:21:32 +0200243 int surrogateescape;
244 if (get_surrogateescape(errors, &surrogateescape) < 0) {
245 return -3;
246 }
247
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100248 len = wcslen(text);
249
Victor Stinner9bee3292017-12-21 16:49:13 +0100250 /* +1 for NULL byte */
Victor Stinner9dd76202017-12-21 16:20:32 +0100251 if (raw_malloc) {
252 result = PyMem_RawMalloc(len + 1);
253 }
254 else {
255 result = PyMem_Malloc(len + 1);
256 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100257 if (result == NULL) {
258 return -1;
259 }
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100260
261 out = result;
262 for (i=0; i<len; i++) {
263 ch = text[i];
264
265 if (ch <= 0x7f) {
266 /* ASCII character */
267 *out++ = (char)ch;
268 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100269 else if (surrogateescape && 0xdc80 <= ch && ch <= 0xdcff) {
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100270 /* UTF-8b surrogate */
271 *out++ = (char)(ch - 0xdc00);
272 }
273 else {
Victor Stinner9dd76202017-12-21 16:20:32 +0100274 if (raw_malloc) {
275 PyMem_RawFree(result);
276 }
277 else {
278 PyMem_Free(result);
279 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100280 if (error_pos != NULL) {
281 *error_pos = i;
282 }
283 if (reason) {
284 *reason = "encoding error";
285 }
286 return -2;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100287 }
288 }
289 *out = '\0';
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100290 *str = result;
291 return 0;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100292}
Victor Stinnerd500e532018-08-28 17:27:36 +0200293#else
294int
295_Py_GetForceASCII(void)
296{
297 return 0;
298}
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100299#endif /* !defined(__APPLE__) && !defined(__ANDROID__) && !defined(MS_WINDOWS) */
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100300
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100301
302#if !defined(HAVE_MBRTOWC) || defined(USE_FORCE_ASCII)
303static int
304decode_ascii(const char *arg, wchar_t **wstr, size_t *wlen,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200305 const char **reason, _Py_error_handler errors)
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100306{
307 wchar_t *res;
308 unsigned char *in;
309 wchar_t *out;
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600310 size_t argsize = strlen(arg) + 1;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100311
Victor Stinner3d4226a2018-08-29 22:21:32 +0200312 int surrogateescape;
313 if (get_surrogateescape(errors, &surrogateescape) < 0) {
314 return -3;
315 }
316
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100317 if (argsize > PY_SSIZE_T_MAX / sizeof(wchar_t)) {
318 return -1;
319 }
320 res = PyMem_RawMalloc(argsize * sizeof(wchar_t));
321 if (!res) {
322 return -1;
323 }
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100324
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100325 out = res;
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100326 for (in = (unsigned char*)arg; *in; in++) {
327 unsigned char ch = *in;
328 if (ch < 128) {
329 *out++ = ch;
330 }
331 else {
332 if (!surrogateescape) {
333 PyMem_RawFree(res);
334 if (wlen) {
335 *wlen = in - (unsigned char*)arg;
336 }
337 if (reason) {
338 *reason = "decoding error";
339 }
340 return -2;
341 }
342 *out++ = 0xdc00 + ch;
343 }
344 }
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100345 *out = 0;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100346
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100347 if (wlen != NULL) {
348 *wlen = out - res;
349 }
350 *wstr = res;
351 return 0;
352}
353#endif /* !HAVE_MBRTOWC */
354
355static int
356decode_current_locale(const char* arg, wchar_t **wstr, size_t *wlen,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200357 const char **reason, _Py_error_handler errors)
Victor Stinner4e314432010-10-07 21:45:39 +0000358{
359 wchar_t *res;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100360 size_t argsize;
Victor Stinner4e314432010-10-07 21:45:39 +0000361 size_t count;
Victor Stinner313f10c2013-05-07 23:48:56 +0200362#ifdef HAVE_MBRTOWC
Victor Stinner4e314432010-10-07 21:45:39 +0000363 unsigned char *in;
364 wchar_t *out;
Victor Stinner4e314432010-10-07 21:45:39 +0000365 mbstate_t mbs;
366#endif
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100367
Victor Stinner3d4226a2018-08-29 22:21:32 +0200368 int surrogateescape;
369 if (get_surrogateescape(errors, &surrogateescape) < 0) {
370 return -3;
371 }
372
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100373#ifdef HAVE_BROKEN_MBSTOWCS
374 /* Some platforms have a broken implementation of
375 * mbstowcs which does not count the characters that
376 * would result from conversion. Use an upper bound.
377 */
378 argsize = strlen(arg);
379#else
380 argsize = mbstowcs(NULL, arg, 0);
381#endif
Victor Stinner4e314432010-10-07 21:45:39 +0000382 if (argsize != (size_t)-1) {
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100383 if (argsize > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) {
384 return -1;
385 }
386 res = (wchar_t *)PyMem_RawMalloc((argsize + 1) * sizeof(wchar_t));
387 if (!res) {
388 return -1;
389 }
390
391 count = mbstowcs(res, arg, argsize + 1);
Victor Stinner4e314432010-10-07 21:45:39 +0000392 if (count != (size_t)-1) {
393 wchar_t *tmp;
394 /* Only use the result if it contains no
395 surrogate characters. */
396 for (tmp = res; *tmp != 0 &&
Victor Stinner76df43d2012-10-30 01:42:39 +0100397 !Py_UNICODE_IS_SURROGATE(*tmp); tmp++)
Victor Stinner4e314432010-10-07 21:45:39 +0000398 ;
Victor Stinner168e1172010-10-16 23:16:16 +0000399 if (*tmp == 0) {
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100400 if (wlen != NULL) {
401 *wlen = count;
402 }
403 *wstr = res;
404 return 0;
Victor Stinner168e1172010-10-16 23:16:16 +0000405 }
Victor Stinner4e314432010-10-07 21:45:39 +0000406 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200407 PyMem_RawFree(res);
Victor Stinner4e314432010-10-07 21:45:39 +0000408 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100409
Victor Stinner4e314432010-10-07 21:45:39 +0000410 /* Conversion failed. Fall back to escaping with surrogateescape. */
411#ifdef HAVE_MBRTOWC
412 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
413
414 /* Overallocate; as multi-byte characters are in the argument, the
415 actual output could use less memory. */
416 argsize = strlen(arg) + 1;
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100417 if (argsize > PY_SSIZE_T_MAX / sizeof(wchar_t)) {
418 return -1;
419 }
420 res = (wchar_t*)PyMem_RawMalloc(argsize * sizeof(wchar_t));
421 if (!res) {
422 return -1;
423 }
424
Victor Stinner4e314432010-10-07 21:45:39 +0000425 in = (unsigned char*)arg;
426 out = res;
427 memset(&mbs, 0, sizeof mbs);
428 while (argsize) {
429 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100430 if (converted == 0) {
Victor Stinner4e314432010-10-07 21:45:39 +0000431 /* Reached end of string; null char stored. */
432 break;
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100433 }
434
Victor Stinner4e314432010-10-07 21:45:39 +0000435 if (converted == (size_t)-2) {
436 /* Incomplete character. This should never happen,
437 since we provide everything that we have -
438 unless there is a bug in the C library, or I
439 misunderstood how mbrtowc works. */
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100440 goto decode_error;
Victor Stinner4e314432010-10-07 21:45:39 +0000441 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100442
Victor Stinner4e314432010-10-07 21:45:39 +0000443 if (converted == (size_t)-1) {
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100444 if (!surrogateescape) {
445 goto decode_error;
446 }
447
Victor Stinner4e314432010-10-07 21:45:39 +0000448 /* Conversion error. Escape as UTF-8b, and start over
449 in the initial shift state. */
450 *out++ = 0xdc00 + *in++;
451 argsize--;
452 memset(&mbs, 0, sizeof mbs);
453 continue;
454 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100455
Victor Stinner76df43d2012-10-30 01:42:39 +0100456 if (Py_UNICODE_IS_SURROGATE(*out)) {
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100457 if (!surrogateescape) {
458 goto decode_error;
459 }
460
Victor Stinner4e314432010-10-07 21:45:39 +0000461 /* Surrogate character. Escape the original
462 byte sequence with surrogateescape. */
463 argsize -= converted;
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100464 while (converted--) {
Victor Stinner4e314432010-10-07 21:45:39 +0000465 *out++ = 0xdc00 + *in++;
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100466 }
Victor Stinner4e314432010-10-07 21:45:39 +0000467 continue;
468 }
469 /* successfully converted some bytes */
470 in += converted;
471 argsize -= converted;
472 out++;
473 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100474 if (wlen != NULL) {
475 *wlen = out - res;
476 }
477 *wstr = res;
478 return 0;
479
480decode_error:
481 PyMem_RawFree(res);
482 if (wlen) {
483 *wlen = in - (unsigned char*)arg;
484 }
485 if (reason) {
486 *reason = "decoding error";
487 }
488 return -2;
Victor Stinnere2623772012-11-12 23:04:02 +0100489#else /* HAVE_MBRTOWC */
Victor Stinner4e314432010-10-07 21:45:39 +0000490 /* Cannot use C locale for escaping; manually escape as if charset
491 is ASCII (i.e. escape all bytes > 128. This will still roundtrip
492 correctly in the locale's charset, which must be an ASCII superset. */
Victor Stinner3d4226a2018-08-29 22:21:32 +0200493 return decode_ascii(arg, wstr, wlen, reason, errors);
Victor Stinnere2623772012-11-12 23:04:02 +0100494#endif /* HAVE_MBRTOWC */
Victor Stinner91106cd2017-12-13 12:29:09 +0100495}
496
497
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100498/* Decode a byte string from the locale encoding.
499
500 Use the strict error handler if 'surrogateescape' is zero. Use the
501 surrogateescape error handler if 'surrogateescape' is non-zero: undecodable
502 bytes are decoded as characters in range U+DC80..U+DCFF. If a byte sequence
503 can be decoded as a surrogate character, escape the bytes using the
504 surrogateescape error handler instead of decoding them.
505
Ville Skyttä61f82e02018-04-20 23:08:45 +0300506 On success, return 0 and write the newly allocated wide character string into
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100507 *wstr (use PyMem_RawFree() to free the memory). If wlen is not NULL, write
508 the number of wide characters excluding the null character into *wlen.
509
510 On memory allocation failure, return -1.
511
512 On decoding error, return -2. If wlen is not NULL, write the start of
513 invalid byte sequence in the input string into *wlen. If reason is not NULL,
514 write the decoding error message into *reason.
515
Victor Stinner3d4226a2018-08-29 22:21:32 +0200516 Return -3 if the error handler 'errors' is not supported.
517
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100518 Use the Py_EncodeLocaleEx() function to encode the character string back to
519 a byte string. */
520int
521_Py_DecodeLocaleEx(const char* arg, wchar_t **wstr, size_t *wlen,
522 const char **reason,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200523 int current_locale, _Py_error_handler errors)
Victor Stinner2cba6b82018-01-10 22:46:15 +0100524{
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100525 if (current_locale) {
Victor Stinner9089a262018-01-22 19:07:32 +0100526#ifdef __ANDROID__
527 return _Py_DecodeUTF8Ex(arg, strlen(arg), wstr, wlen, reason,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200528 errors);
Victor Stinner9089a262018-01-22 19:07:32 +0100529#else
Victor Stinner3d4226a2018-08-29 22:21:32 +0200530 return decode_current_locale(arg, wstr, wlen, reason, errors);
Victor Stinner9089a262018-01-22 19:07:32 +0100531#endif
Victor Stinner2cba6b82018-01-10 22:46:15 +0100532 }
533
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100534#if defined(__APPLE__) || defined(__ANDROID__)
535 return _Py_DecodeUTF8Ex(arg, strlen(arg), wstr, wlen, reason,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200536 errors);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100537#else
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200538 int use_utf8 = (Py_UTF8Mode == 1);
539#ifdef MS_WINDOWS
540 use_utf8 |= !Py_LegacyWindowsFSEncodingFlag;
541#endif
542 if (use_utf8) {
Victor Stinner3d4226a2018-08-29 22:21:32 +0200543 return _Py_DecodeUTF8Ex(arg, strlen(arg), wstr, wlen, reason,
544 errors);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100545 }
546
547#ifdef USE_FORCE_ASCII
548 if (force_ascii == -1) {
Victor Stinner2cba6b82018-01-10 22:46:15 +0100549 force_ascii = check_force_ascii();
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100550 }
Victor Stinner2cba6b82018-01-10 22:46:15 +0100551
552 if (force_ascii) {
553 /* force ASCII encoding to workaround mbstowcs() issue */
Victor Stinner3d4226a2018-08-29 22:21:32 +0200554 return decode_ascii(arg, wstr, wlen, reason, errors);
Victor Stinner2cba6b82018-01-10 22:46:15 +0100555 }
556#endif
557
Victor Stinner3d4226a2018-08-29 22:21:32 +0200558 return decode_current_locale(arg, wstr, wlen, reason, errors);
Victor Stinner2cba6b82018-01-10 22:46:15 +0100559#endif /* __APPLE__ or __ANDROID__ */
560}
561
562
Victor Stinner91106cd2017-12-13 12:29:09 +0100563/* Decode a byte string from the locale encoding with the
564 surrogateescape error handler: undecodable bytes are decoded as characters
565 in range U+DC80..U+DCFF. If a byte sequence can be decoded as a surrogate
566 character, escape the bytes using the surrogateescape error handler instead
567 of decoding them.
568
569 Return a pointer to a newly allocated wide character string, use
570 PyMem_RawFree() to free the memory. If size is not NULL, write the number of
571 wide characters excluding the null character into *size
572
573 Return NULL on decoding error or memory allocation error. If *size* is not
574 NULL, *size is set to (size_t)-1 on memory error or set to (size_t)-2 on
575 decoding error.
576
577 Decoding errors should never happen, unless there is a bug in the C
578 library.
579
580 Use the Py_EncodeLocale() function to encode the character string back to a
581 byte string. */
582wchar_t*
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100583Py_DecodeLocale(const char* arg, size_t *wlen)
Victor Stinner91106cd2017-12-13 12:29:09 +0100584{
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100585 wchar_t *wstr;
Victor Stinner3d4226a2018-08-29 22:21:32 +0200586 int res = _Py_DecodeLocaleEx(arg, &wstr, wlen,
587 NULL, 0,
588 _Py_ERROR_SURROGATEESCAPE);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100589 if (res != 0) {
Victor Stinner3d4226a2018-08-29 22:21:32 +0200590 assert(res != -3);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100591 if (wlen != NULL) {
592 *wlen = (size_t)res;
593 }
594 return NULL;
595 }
596 return wstr;
Victor Stinner2cba6b82018-01-10 22:46:15 +0100597}
Victor Stinner91106cd2017-12-13 12:29:09 +0100598
Victor Stinner91106cd2017-12-13 12:29:09 +0100599
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100600static int
601encode_current_locale(const wchar_t *text, char **str,
602 size_t *error_pos, const char **reason,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200603 int raw_malloc, _Py_error_handler errors)
Victor Stinner91106cd2017-12-13 12:29:09 +0100604{
Victor Stinner4e314432010-10-07 21:45:39 +0000605 const size_t len = wcslen(text);
606 char *result = NULL, *bytes = NULL;
607 size_t i, size, converted;
608 wchar_t c, buf[2];
609
Victor Stinner3d4226a2018-08-29 22:21:32 +0200610 int surrogateescape;
611 if (get_surrogateescape(errors, &surrogateescape) < 0) {
612 return -3;
613 }
614
Victor Stinner4e314432010-10-07 21:45:39 +0000615 /* The function works in two steps:
616 1. compute the length of the output buffer in bytes (size)
617 2. outputs the bytes */
618 size = 0;
619 buf[1] = 0;
620 while (1) {
621 for (i=0; i < len; i++) {
622 c = text[i];
623 if (c >= 0xdc80 && c <= 0xdcff) {
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100624 if (!surrogateescape) {
625 goto encode_error;
626 }
Victor Stinner4e314432010-10-07 21:45:39 +0000627 /* UTF-8b surrogate */
628 if (bytes != NULL) {
629 *bytes++ = c - 0xdc00;
630 size--;
631 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100632 else {
Victor Stinner4e314432010-10-07 21:45:39 +0000633 size++;
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100634 }
Victor Stinner4e314432010-10-07 21:45:39 +0000635 continue;
636 }
637 else {
638 buf[0] = c;
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100639 if (bytes != NULL) {
Victor Stinner4e314432010-10-07 21:45:39 +0000640 converted = wcstombs(bytes, buf, size);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100641 }
642 else {
Victor Stinner4e314432010-10-07 21:45:39 +0000643 converted = wcstombs(NULL, buf, 0);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100644 }
Victor Stinner4e314432010-10-07 21:45:39 +0000645 if (converted == (size_t)-1) {
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100646 goto encode_error;
Victor Stinner4e314432010-10-07 21:45:39 +0000647 }
648 if (bytes != NULL) {
649 bytes += converted;
650 size -= converted;
651 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100652 else {
Victor Stinner4e314432010-10-07 21:45:39 +0000653 size += converted;
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100654 }
Victor Stinner4e314432010-10-07 21:45:39 +0000655 }
656 }
657 if (result != NULL) {
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100658 *bytes = '\0';
Victor Stinner4e314432010-10-07 21:45:39 +0000659 break;
660 }
661
662 size += 1; /* nul byte at the end */
Victor Stinner9dd76202017-12-21 16:20:32 +0100663 if (raw_malloc) {
664 result = PyMem_RawMalloc(size);
665 }
666 else {
667 result = PyMem_Malloc(size);
668 }
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100669 if (result == NULL) {
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100670 return -1;
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100671 }
Victor Stinner4e314432010-10-07 21:45:39 +0000672 bytes = result;
673 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100674 *str = result;
675 return 0;
676
677encode_error:
678 if (raw_malloc) {
679 PyMem_RawFree(result);
680 }
681 else {
682 PyMem_Free(result);
683 }
684 if (error_pos != NULL) {
685 *error_pos = i;
686 }
687 if (reason) {
688 *reason = "encoding error";
689 }
690 return -2;
Victor Stinner91106cd2017-12-13 12:29:09 +0100691}
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100692
Victor Stinner3d4226a2018-08-29 22:21:32 +0200693
694/* Encode a string to the locale encoding.
695
696 Parameters:
697
698 * raw_malloc: if non-zero, allocate memory using PyMem_RawMalloc() instead
699 of PyMem_Malloc().
700 * current_locale: if non-zero, use the current LC_CTYPE, otherwise use
701 Python filesystem encoding.
702 * errors: error handler like "strict" or "surrogateescape".
703
704 Return value:
705
706 0: success, *str is set to a newly allocated decoded string.
707 -1: memory allocation failure
708 -2: encoding error, set *error_pos and *reason (if set).
709 -3: the error handler 'errors' is not supported.
710 */
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100711static int
712encode_locale_ex(const wchar_t *text, char **str, size_t *error_pos,
713 const char **reason,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200714 int raw_malloc, int current_locale, _Py_error_handler errors)
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100715{
716 if (current_locale) {
Victor Stinner9089a262018-01-22 19:07:32 +0100717#ifdef __ANDROID__
718 return _Py_EncodeUTF8Ex(text, str, error_pos, reason,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200719 raw_malloc, errors);
Victor Stinner9089a262018-01-22 19:07:32 +0100720#else
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100721 return encode_current_locale(text, str, error_pos, reason,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200722 raw_malloc, errors);
Victor Stinner9089a262018-01-22 19:07:32 +0100723#endif
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100724 }
725
726#if defined(__APPLE__) || defined(__ANDROID__)
727 return _Py_EncodeUTF8Ex(text, str, error_pos, reason,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200728 raw_malloc, errors);
729#else
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200730 int use_utf8 = (Py_UTF8Mode == 1);
731#ifdef MS_WINDOWS
732 use_utf8 |= !Py_LegacyWindowsFSEncodingFlag;
733#endif
734 if (use_utf8) {
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100735 return _Py_EncodeUTF8Ex(text, str, error_pos, reason,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200736 raw_malloc, errors);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100737 }
738
739#ifdef USE_FORCE_ASCII
740 if (force_ascii == -1) {
741 force_ascii = check_force_ascii();
742 }
743
744 if (force_ascii) {
745 return encode_ascii(text, str, error_pos, reason,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200746 raw_malloc, errors);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100747 }
Victor Stinnerd2b02312017-12-15 23:06:17 +0100748#endif
Victor Stinner91106cd2017-12-13 12:29:09 +0100749
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100750 return encode_current_locale(text, str, error_pos, reason,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200751 raw_malloc, errors);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100752#endif /* __APPLE__ or __ANDROID__ */
753}
754
Victor Stinner9dd76202017-12-21 16:20:32 +0100755static char*
Victor Stinner2cba6b82018-01-10 22:46:15 +0100756encode_locale(const wchar_t *text, size_t *error_pos,
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100757 int raw_malloc, int current_locale)
Victor Stinner9dd76202017-12-21 16:20:32 +0100758{
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100759 char *str;
760 int res = encode_locale_ex(text, &str, error_pos, NULL,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200761 raw_malloc, current_locale,
762 _Py_ERROR_SURROGATEESCAPE);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100763 if (res != -2 && error_pos) {
764 *error_pos = (size_t)-1;
Victor Stinner9dd76202017-12-21 16:20:32 +0100765 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100766 if (res != 0) {
767 return NULL;
768 }
769 return str;
Victor Stinner9dd76202017-12-21 16:20:32 +0100770}
771
Victor Stinner91106cd2017-12-13 12:29:09 +0100772/* Encode a wide character string to the locale encoding with the
773 surrogateescape error handler: surrogate characters in the range
774 U+DC80..U+DCFF are converted to bytes 0x80..0xFF.
775
776 Return a pointer to a newly allocated byte string, use PyMem_Free() to free
777 the memory. Return NULL on encoding or memory allocation error.
778
779 If error_pos is not NULL, *error_pos is set to (size_t)-1 on success, or set
780 to the index of the invalid character on encoding error.
781
782 Use the Py_DecodeLocale() function to decode the bytes string back to a wide
783 character string. */
784char*
785Py_EncodeLocale(const wchar_t *text, size_t *error_pos)
786{
Victor Stinner2cba6b82018-01-10 22:46:15 +0100787 return encode_locale(text, error_pos, 0, 0);
Victor Stinner9dd76202017-12-21 16:20:32 +0100788}
Victor Stinner91106cd2017-12-13 12:29:09 +0100789
Victor Stinner91106cd2017-12-13 12:29:09 +0100790
Victor Stinner9dd76202017-12-21 16:20:32 +0100791/* Similar to Py_EncodeLocale(), but result must be freed by PyMem_RawFree()
792 instead of PyMem_Free(). */
793char*
794_Py_EncodeLocaleRaw(const wchar_t *text, size_t *error_pos)
795{
Victor Stinner2cba6b82018-01-10 22:46:15 +0100796 return encode_locale(text, error_pos, 1, 0);
797}
798
799
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100800int
801_Py_EncodeLocaleEx(const wchar_t *text, char **str,
802 size_t *error_pos, const char **reason,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200803 int current_locale, _Py_error_handler errors)
Victor Stinner2cba6b82018-01-10 22:46:15 +0100804{
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100805 return encode_locale_ex(text, str, error_pos, reason, 1,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200806 current_locale, errors);
Victor Stinner4e314432010-10-07 21:45:39 +0000807}
808
Victor Stinner6672d0c2010-10-07 22:53:43 +0000809
Steve Dowerf2f373f2015-02-21 08:44:05 -0800810#ifdef MS_WINDOWS
811static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
812
813static void
814FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
815{
816 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
817 /* Cannot simply cast and dereference in_ptr,
818 since it might not be aligned properly */
819 __int64 in;
820 memcpy(&in, in_ptr, sizeof(in));
821 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
822 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
823}
824
825void
Steve Dowerbf1f3762015-02-21 15:26:02 -0800826_Py_time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800827{
828 /* XXX endianness */
829 __int64 out;
830 out = time_in + secs_between_epochs;
831 out = out * 10000000 + nsec_in / 100;
832 memcpy(out_ptr, &out, sizeof(out));
833}
834
835/* Below, we *know* that ugo+r is 0444 */
836#if _S_IREAD != 0400
837#error Unsupported C library
838#endif
839static int
840attributes_to_mode(DWORD attr)
841{
842 int m = 0;
843 if (attr & FILE_ATTRIBUTE_DIRECTORY)
844 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
845 else
846 m |= _S_IFREG;
847 if (attr & FILE_ATTRIBUTE_READONLY)
848 m |= 0444;
849 else
850 m |= 0666;
851 return m;
852}
853
Steve Dowerbf1f3762015-02-21 15:26:02 -0800854void
Victor Stinnere134a7f2015-03-30 10:09:31 +0200855_Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag,
856 struct _Py_stat_struct *result)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800857{
858 memset(result, 0, sizeof(*result));
859 result->st_mode = attributes_to_mode(info->dwFileAttributes);
860 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
861 result->st_dev = info->dwVolumeSerialNumber;
862 result->st_rdev = result->st_dev;
863 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
864 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
865 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
866 result->st_nlink = info->nNumberOfLinks;
Victor Stinner0f6d7332017-03-09 17:34:28 +0100867 result->st_ino = (((uint64_t)info->nFileIndexHigh) << 32) + info->nFileIndexLow;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800868 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
869 /* first clear the S_IFMT bits */
870 result->st_mode ^= (result->st_mode & S_IFMT);
871 /* now set the bits that make this a symlink */
872 result->st_mode |= S_IFLNK;
873 }
874 result->st_file_attributes = info->dwFileAttributes;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800875}
876#endif
877
878/* Return information about a file.
879
880 On POSIX, use fstat().
881
882 On Windows, use GetFileType() and GetFileInformationByHandle() which support
Victor Stinner8c663fd2017-11-08 14:44:44 -0800883 files larger than 2 GiB. fstat() may fail with EOVERFLOW on files larger
884 than 2 GiB because the file size type is a signed 32-bit integer: see issue
Steve Dowerf2f373f2015-02-21 08:44:05 -0800885 #23152.
Victor Stinnere134a7f2015-03-30 10:09:31 +0200886
887 On Windows, set the last Windows error and return nonzero on error. On
888 POSIX, set errno and return nonzero on error. Fill status and return 0 on
889 success. */
Steve Dowerf2f373f2015-02-21 08:44:05 -0800890int
Victor Stinnere134a7f2015-03-30 10:09:31 +0200891_Py_fstat_noraise(int fd, struct _Py_stat_struct *status)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800892{
893#ifdef MS_WINDOWS
894 BY_HANDLE_FILE_INFORMATION info;
895 HANDLE h;
896 int type;
897
Steve Dower940f33a2016-09-08 11:21:54 -0700898 _Py_BEGIN_SUPPRESS_IPH
899 h = (HANDLE)_get_osfhandle(fd);
900 _Py_END_SUPPRESS_IPH
Steve Dowerf2f373f2015-02-21 08:44:05 -0800901
902 if (h == INVALID_HANDLE_VALUE) {
Steve Dower8fc89802015-04-12 00:26:27 -0400903 /* errno is already set by _get_osfhandle, but we also set
904 the Win32 error for callers who expect that */
Steve Dower8acde7d2015-03-07 18:14:07 -0800905 SetLastError(ERROR_INVALID_HANDLE);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800906 return -1;
907 }
Victor Stinnere134a7f2015-03-30 10:09:31 +0200908 memset(status, 0, sizeof(*status));
Steve Dowerf2f373f2015-02-21 08:44:05 -0800909
910 type = GetFileType(h);
911 if (type == FILE_TYPE_UNKNOWN) {
912 DWORD error = GetLastError();
Steve Dower8fc89802015-04-12 00:26:27 -0400913 if (error != 0) {
914 errno = winerror_to_errno(error);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800915 return -1;
Steve Dower8fc89802015-04-12 00:26:27 -0400916 }
Steve Dowerf2f373f2015-02-21 08:44:05 -0800917 /* else: valid but unknown file */
918 }
919
920 if (type != FILE_TYPE_DISK) {
921 if (type == FILE_TYPE_CHAR)
Victor Stinnere134a7f2015-03-30 10:09:31 +0200922 status->st_mode = _S_IFCHR;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800923 else if (type == FILE_TYPE_PIPE)
Victor Stinnere134a7f2015-03-30 10:09:31 +0200924 status->st_mode = _S_IFIFO;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800925 return 0;
926 }
927
928 if (!GetFileInformationByHandle(h, &info)) {
Steve Dower8fc89802015-04-12 00:26:27 -0400929 /* The Win32 error is already set, but we also set errno for
930 callers who expect it */
931 errno = winerror_to_errno(GetLastError());
Steve Dowerf2f373f2015-02-21 08:44:05 -0800932 return -1;
933 }
934
Victor Stinnere134a7f2015-03-30 10:09:31 +0200935 _Py_attribute_data_to_stat(&info, 0, status);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800936 /* specific to fstat() */
Victor Stinner0f6d7332017-03-09 17:34:28 +0100937 status->st_ino = (((uint64_t)info.nFileIndexHigh) << 32) + info.nFileIndexLow;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800938 return 0;
939#else
Victor Stinnere134a7f2015-03-30 10:09:31 +0200940 return fstat(fd, status);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800941#endif
942}
Steve Dowerf2f373f2015-02-21 08:44:05 -0800943
Victor Stinnere134a7f2015-03-30 10:09:31 +0200944/* Return information about a file.
945
946 On POSIX, use fstat().
947
948 On Windows, use GetFileType() and GetFileInformationByHandle() which support
Victor Stinner8c663fd2017-11-08 14:44:44 -0800949 files larger than 2 GiB. fstat() may fail with EOVERFLOW on files larger
950 than 2 GiB because the file size type is a signed 32-bit integer: see issue
Victor Stinnere134a7f2015-03-30 10:09:31 +0200951 #23152.
952
953 Raise an exception and return -1 on error. On Windows, set the last Windows
954 error on error. On POSIX, set errno on error. Fill status and return 0 on
955 success.
956
Victor Stinner6f4fae82015-04-01 18:34:32 +0200957 Release the GIL to call GetFileType() and GetFileInformationByHandle(), or
958 to call fstat(). The caller must hold the GIL. */
Victor Stinnere134a7f2015-03-30 10:09:31 +0200959int
960_Py_fstat(int fd, struct _Py_stat_struct *status)
961{
962 int res;
963
Victor Stinner8a1be612016-03-14 22:07:55 +0100964 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +0100965
Victor Stinnere134a7f2015-03-30 10:09:31 +0200966 Py_BEGIN_ALLOW_THREADS
967 res = _Py_fstat_noraise(fd, status);
968 Py_END_ALLOW_THREADS
969
970 if (res != 0) {
971#ifdef MS_WINDOWS
972 PyErr_SetFromWindowsErr(0);
973#else
974 PyErr_SetFromErrno(PyExc_OSError);
975#endif
976 return -1;
977 }
978 return 0;
979}
Steve Dowerf2f373f2015-02-21 08:44:05 -0800980
Victor Stinner6672d0c2010-10-07 22:53:43 +0000981/* Call _wstat() on Windows, or encode the path to the filesystem encoding and
982 call stat() otherwise. Only fill st_mode attribute on Windows.
983
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100984 Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception was
985 raised. */
Victor Stinner4e314432010-10-07 21:45:39 +0000986
987int
Victor Stinnera4a75952010-10-07 22:23:10 +0000988_Py_stat(PyObject *path, struct stat *statbuf)
Victor Stinner4e314432010-10-07 21:45:39 +0000989{
990#ifdef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +0000991 int err;
992 struct _stat wstatbuf;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300993 const wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +0000994
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300995 wpath = _PyUnicode_AsUnicode(path);
Victor Stinneree587ea2011-11-17 00:51:38 +0100996 if (wpath == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100997 return -2;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300998
Victor Stinneree587ea2011-11-17 00:51:38 +0100999 err = _wstat(wpath, &wstatbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001000 if (!err)
1001 statbuf->st_mode = wstatbuf.st_mode;
1002 return err;
1003#else
1004 int ret;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03001005 PyObject *bytes;
1006 char *cpath;
1007
1008 bytes = PyUnicode_EncodeFSDefault(path);
Victor Stinner4e314432010-10-07 21:45:39 +00001009 if (bytes == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +01001010 return -2;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03001011
1012 /* check for embedded null bytes */
1013 if (PyBytes_AsStringAndSize(bytes, &cpath, NULL) == -1) {
1014 Py_DECREF(bytes);
1015 return -2;
1016 }
1017
1018 ret = stat(cpath, statbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001019 Py_DECREF(bytes);
1020 return ret;
1021#endif
1022}
1023
Victor Stinnerd45c7f82012-12-04 01:34:47 +01001024
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +03001025/* This function MUST be kept async-signal-safe on POSIX when raise=0. */
Antoine Pitrou409b5382013-10-12 22:41:17 +02001026static int
Victor Stinnerdaf45552013-08-28 00:53:59 +02001027get_inheritable(int fd, int raise)
1028{
1029#ifdef MS_WINDOWS
1030 HANDLE handle;
1031 DWORD flags;
Victor Stinner6672d0c2010-10-07 22:53:43 +00001032
Steve Dower8fc89802015-04-12 00:26:27 -04001033 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001034 handle = (HANDLE)_get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001035 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001036 if (handle == INVALID_HANDLE_VALUE) {
1037 if (raise)
Steve Dower41e72442015-03-14 11:38:27 -07001038 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001039 return -1;
1040 }
1041
1042 if (!GetHandleInformation(handle, &flags)) {
1043 if (raise)
1044 PyErr_SetFromWindowsErr(0);
1045 return -1;
1046 }
1047
1048 return (flags & HANDLE_FLAG_INHERIT);
1049#else
1050 int flags;
1051
1052 flags = fcntl(fd, F_GETFD, 0);
1053 if (flags == -1) {
1054 if (raise)
1055 PyErr_SetFromErrno(PyExc_OSError);
1056 return -1;
1057 }
1058 return !(flags & FD_CLOEXEC);
1059#endif
1060}
1061
1062/* Get the inheritable flag of the specified file descriptor.
Victor Stinnerb034eee2013-09-07 10:36:04 +02001063 Return 1 if the file descriptor can be inherited, 0 if it cannot,
Victor Stinnerdaf45552013-08-28 00:53:59 +02001064 raise an exception and return -1 on error. */
1065int
1066_Py_get_inheritable(int fd)
1067{
1068 return get_inheritable(fd, 1);
1069}
1070
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +03001071
1072/* This function MUST be kept async-signal-safe on POSIX when raise=0. */
Victor Stinnerdaf45552013-08-28 00:53:59 +02001073static int
1074set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
1075{
1076#ifdef MS_WINDOWS
1077 HANDLE handle;
1078 DWORD flags;
Victor Stinner282124b2014-09-02 11:41:04 +02001079#else
1080#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
1081 static int ioctl_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02001082 int request;
1083 int err;
Victor Stinner282124b2014-09-02 11:41:04 +02001084#endif
Victor Stinnera858bbd2016-04-17 16:51:52 +02001085 int flags, new_flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +02001086 int res;
1087#endif
1088
1089 /* atomic_flag_works can only be used to make the file descriptor
1090 non-inheritable */
1091 assert(!(atomic_flag_works != NULL && inheritable));
1092
1093 if (atomic_flag_works != NULL && !inheritable) {
1094 if (*atomic_flag_works == -1) {
Steve Dower41e72442015-03-14 11:38:27 -07001095 int isInheritable = get_inheritable(fd, raise);
1096 if (isInheritable == -1)
Victor Stinnerdaf45552013-08-28 00:53:59 +02001097 return -1;
Steve Dower41e72442015-03-14 11:38:27 -07001098 *atomic_flag_works = !isInheritable;
Victor Stinnerdaf45552013-08-28 00:53:59 +02001099 }
1100
1101 if (*atomic_flag_works)
1102 return 0;
1103 }
1104
1105#ifdef MS_WINDOWS
Steve Dower8fc89802015-04-12 00:26:27 -04001106 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001107 handle = (HANDLE)_get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001108 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001109 if (handle == INVALID_HANDLE_VALUE) {
1110 if (raise)
Steve Dower41e72442015-03-14 11:38:27 -07001111 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001112 return -1;
1113 }
1114
1115 if (inheritable)
1116 flags = HANDLE_FLAG_INHERIT;
1117 else
1118 flags = 0;
1119 if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) {
1120 if (raise)
1121 PyErr_SetFromWindowsErr(0);
1122 return -1;
1123 }
1124 return 0;
1125
Victor Stinnerdaf45552013-08-28 00:53:59 +02001126#else
Victor Stinner282124b2014-09-02 11:41:04 +02001127
1128#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +03001129 if (ioctl_works != 0 && raise != 0) {
Victor Stinner282124b2014-09-02 11:41:04 +02001130 /* fast-path: ioctl() only requires one syscall */
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +03001131 /* caveat: raise=0 is an indicator that we must be async-signal-safe
1132 * thus avoid using ioctl() so we skip the fast-path. */
Victor Stinner282124b2014-09-02 11:41:04 +02001133 if (inheritable)
1134 request = FIONCLEX;
1135 else
1136 request = FIOCLEX;
1137 err = ioctl(fd, request, NULL);
1138 if (!err) {
1139 ioctl_works = 1;
1140 return 0;
1141 }
1142
Victor Stinner3116cc42016-05-19 16:46:18 +02001143 if (errno != ENOTTY && errno != EACCES) {
Victor Stinner282124b2014-09-02 11:41:04 +02001144 if (raise)
1145 PyErr_SetFromErrno(PyExc_OSError);
1146 return -1;
1147 }
1148 else {
1149 /* Issue #22258: Here, ENOTTY means "Inappropriate ioctl for
1150 device". The ioctl is declared but not supported by the kernel.
1151 Remember that ioctl() doesn't work. It is the case on
Victor Stinner3116cc42016-05-19 16:46:18 +02001152 Illumos-based OS for example.
1153
1154 Issue #27057: When SELinux policy disallows ioctl it will fail
1155 with EACCES. While FIOCLEX is safe operation it may be
1156 unavailable because ioctl was denied altogether.
1157 This can be the case on Android. */
Victor Stinner282124b2014-09-02 11:41:04 +02001158 ioctl_works = 0;
1159 }
1160 /* fallback to fcntl() if ioctl() does not work */
1161 }
1162#endif
1163
1164 /* slow-path: fcntl() requires two syscalls */
Victor Stinnerdaf45552013-08-28 00:53:59 +02001165 flags = fcntl(fd, F_GETFD);
1166 if (flags < 0) {
1167 if (raise)
1168 PyErr_SetFromErrno(PyExc_OSError);
1169 return -1;
1170 }
1171
Victor Stinnera858bbd2016-04-17 16:51:52 +02001172 if (inheritable) {
1173 new_flags = flags & ~FD_CLOEXEC;
1174 }
1175 else {
1176 new_flags = flags | FD_CLOEXEC;
1177 }
1178
1179 if (new_flags == flags) {
1180 /* FD_CLOEXEC flag already set/cleared: nothing to do */
1181 return 0;
1182 }
1183
Xavier de Gayeec5d3cd2016-11-19 16:19:29 +01001184 res = fcntl(fd, F_SETFD, new_flags);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001185 if (res < 0) {
1186 if (raise)
1187 PyErr_SetFromErrno(PyExc_OSError);
1188 return -1;
1189 }
1190 return 0;
1191#endif
1192}
1193
1194/* Make the file descriptor non-inheritable.
Victor Stinnerb034eee2013-09-07 10:36:04 +02001195 Return 0 on success, set errno and return -1 on error. */
Victor Stinnerdaf45552013-08-28 00:53:59 +02001196static int
1197make_non_inheritable(int fd)
1198{
1199 return set_inheritable(fd, 0, 0, NULL);
1200}
1201
1202/* Set the inheritable flag of the specified file descriptor.
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +03001203 On success: return 0, on error: raise an exception and return -1.
Victor Stinnerdaf45552013-08-28 00:53:59 +02001204
1205 If atomic_flag_works is not NULL:
1206
1207 * if *atomic_flag_works==-1, check if the inheritable is set on the file
1208 descriptor: if yes, set *atomic_flag_works to 1, otherwise set to 0 and
1209 set the inheritable flag
1210 * if *atomic_flag_works==1: do nothing
1211 * if *atomic_flag_works==0: set inheritable flag to False
1212
1213 Set atomic_flag_works to NULL if no atomic flag was used to create the
1214 file descriptor.
1215
1216 atomic_flag_works can only be used to make a file descriptor
1217 non-inheritable: atomic_flag_works must be NULL if inheritable=1. */
1218int
1219_Py_set_inheritable(int fd, int inheritable, int *atomic_flag_works)
1220{
1221 return set_inheritable(fd, inheritable, 1, atomic_flag_works);
1222}
1223
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +03001224/* Same as _Py_set_inheritable() but on error, set errno and
1225 don't raise an exception.
1226 This function is async-signal-safe. */
1227int
1228_Py_set_inheritable_async_safe(int fd, int inheritable, int *atomic_flag_works)
1229{
1230 return set_inheritable(fd, inheritable, 0, atomic_flag_works);
1231}
1232
Victor Stinnera555cfc2015-03-18 00:22:14 +01001233static int
1234_Py_open_impl(const char *pathname, int flags, int gil_held)
Victor Stinnerdaf45552013-08-28 00:53:59 +02001235{
1236 int fd;
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001237 int async_err = 0;
Victor Stinnera555cfc2015-03-18 00:22:14 +01001238#ifndef MS_WINDOWS
Victor Stinnerdaf45552013-08-28 00:53:59 +02001239 int *atomic_flag_works;
Victor Stinnera555cfc2015-03-18 00:22:14 +01001240#endif
1241
1242#ifdef MS_WINDOWS
1243 flags |= O_NOINHERIT;
1244#elif defined(O_CLOEXEC)
Victor Stinnerdaf45552013-08-28 00:53:59 +02001245 atomic_flag_works = &_Py_open_cloexec_works;
1246 flags |= O_CLOEXEC;
1247#else
1248 atomic_flag_works = NULL;
1249#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +02001250
Victor Stinnera555cfc2015-03-18 00:22:14 +01001251 if (gil_held) {
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001252 do {
1253 Py_BEGIN_ALLOW_THREADS
1254 fd = open(pathname, flags);
1255 Py_END_ALLOW_THREADS
1256 } while (fd < 0
1257 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1258 if (async_err)
1259 return -1;
Victor Stinnera555cfc2015-03-18 00:22:14 +01001260 if (fd < 0) {
1261 PyErr_SetFromErrnoWithFilename(PyExc_OSError, pathname);
1262 return -1;
1263 }
1264 }
1265 else {
1266 fd = open(pathname, flags);
1267 if (fd < 0)
1268 return -1;
1269 }
1270
1271#ifndef MS_WINDOWS
1272 if (set_inheritable(fd, 0, gil_held, atomic_flag_works) < 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +02001273 close(fd);
1274 return -1;
1275 }
Victor Stinnera555cfc2015-03-18 00:22:14 +01001276#endif
1277
Victor Stinnerdaf45552013-08-28 00:53:59 +02001278 return fd;
1279}
1280
Victor Stinnera555cfc2015-03-18 00:22:14 +01001281/* Open a file with the specified flags (wrapper to open() function).
1282 Return a file descriptor on success. Raise an exception and return -1 on
1283 error.
1284
1285 The file descriptor is created non-inheritable.
1286
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001287 When interrupted by a signal (open() fails with EINTR), retry the syscall,
1288 except if the Python signal handler raises an exception.
1289
Victor Stinner6f4fae82015-04-01 18:34:32 +02001290 Release the GIL to call open(). The caller must hold the GIL. */
Victor Stinnera555cfc2015-03-18 00:22:14 +01001291int
1292_Py_open(const char *pathname, int flags)
1293{
1294 /* _Py_open() must be called with the GIL held. */
1295 assert(PyGILState_Check());
1296 return _Py_open_impl(pathname, flags, 1);
1297}
1298
1299/* Open a file with the specified flags (wrapper to open() function).
1300 Return a file descriptor on success. Set errno and return -1 on error.
1301
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001302 The file descriptor is created non-inheritable.
1303
1304 If interrupted by a signal, fail with EINTR. */
Victor Stinnera555cfc2015-03-18 00:22:14 +01001305int
1306_Py_open_noraise(const char *pathname, int flags)
1307{
1308 return _Py_open_impl(pathname, flags, 0);
1309}
1310
Victor Stinnerdaf45552013-08-28 00:53:59 +02001311/* Open a file. Use _wfopen() on Windows, encode the path to the locale
Victor Stinnere42ccd22015-03-18 01:39:23 +01001312 encoding and use fopen() otherwise.
1313
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001314 The file descriptor is created non-inheritable.
1315
1316 If interrupted by a signal, fail with EINTR. */
Victor Stinner4e314432010-10-07 21:45:39 +00001317FILE *
1318_Py_wfopen(const wchar_t *path, const wchar_t *mode)
1319{
Victor Stinner4e314432010-10-07 21:45:39 +00001320 FILE *f;
Victor Stinnerdaf45552013-08-28 00:53:59 +02001321#ifndef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +00001322 char *cpath;
1323 char cmode[10];
1324 size_t r;
1325 r = wcstombs(cmode, mode, 10);
1326 if (r == (size_t)-1 || r >= 10) {
1327 errno = EINVAL;
1328 return NULL;
1329 }
Victor Stinner9dd76202017-12-21 16:20:32 +01001330 cpath = _Py_EncodeLocaleRaw(path, NULL);
1331 if (cpath == NULL) {
Victor Stinner4e314432010-10-07 21:45:39 +00001332 return NULL;
Victor Stinner9dd76202017-12-21 16:20:32 +01001333 }
Victor Stinner4e314432010-10-07 21:45:39 +00001334 f = fopen(cpath, cmode);
Victor Stinner9dd76202017-12-21 16:20:32 +01001335 PyMem_RawFree(cpath);
Victor Stinner4e314432010-10-07 21:45:39 +00001336#else
Victor Stinnerdaf45552013-08-28 00:53:59 +02001337 f = _wfopen(path, mode);
Victor Stinner4e314432010-10-07 21:45:39 +00001338#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +02001339 if (f == NULL)
1340 return NULL;
1341 if (make_non_inheritable(fileno(f)) < 0) {
1342 fclose(f);
1343 return NULL;
1344 }
1345 return f;
Victor Stinner4e314432010-10-07 21:45:39 +00001346}
1347
Victor Stinnere42ccd22015-03-18 01:39:23 +01001348/* Wrapper to fopen().
1349
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001350 The file descriptor is created non-inheritable.
1351
1352 If interrupted by a signal, fail with EINTR. */
Victor Stinnerdaf45552013-08-28 00:53:59 +02001353FILE*
1354_Py_fopen(const char *pathname, const char *mode)
1355{
1356 FILE *f = fopen(pathname, mode);
1357 if (f == NULL)
1358 return NULL;
1359 if (make_non_inheritable(fileno(f)) < 0) {
1360 fclose(f);
1361 return NULL;
1362 }
1363 return f;
1364}
1365
1366/* Open a file. Call _wfopen() on Windows, or encode the path to the filesystem
Victor Stinnere42ccd22015-03-18 01:39:23 +01001367 encoding and call fopen() otherwise.
Victor Stinner6672d0c2010-10-07 22:53:43 +00001368
Victor Stinnere42ccd22015-03-18 01:39:23 +01001369 Return the new file object on success. Raise an exception and return NULL
1370 on error.
1371
1372 The file descriptor is created non-inheritable.
1373
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001374 When interrupted by a signal (open() fails with EINTR), retry the syscall,
1375 except if the Python signal handler raises an exception.
1376
Victor Stinner6f4fae82015-04-01 18:34:32 +02001377 Release the GIL to call _wfopen() or fopen(). The caller must hold
1378 the GIL. */
Victor Stinner4e314432010-10-07 21:45:39 +00001379FILE*
Victor Stinnerdaf45552013-08-28 00:53:59 +02001380_Py_fopen_obj(PyObject *path, const char *mode)
Victor Stinner4e314432010-10-07 21:45:39 +00001381{
Victor Stinnerdaf45552013-08-28 00:53:59 +02001382 FILE *f;
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001383 int async_err = 0;
Victor Stinner4e314432010-10-07 21:45:39 +00001384#ifdef MS_WINDOWS
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03001385 const wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +00001386 wchar_t wmode[10];
1387 int usize;
Victor Stinner4e314432010-10-07 21:45:39 +00001388
Victor Stinnere42ccd22015-03-18 01:39:23 +01001389 assert(PyGILState_Check());
1390
Antoine Pitrou0e576f12011-12-22 10:03:38 +01001391 if (!PyUnicode_Check(path)) {
1392 PyErr_Format(PyExc_TypeError,
1393 "str file path expected under Windows, got %R",
1394 Py_TYPE(path));
1395 return NULL;
1396 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03001397 wpath = _PyUnicode_AsUnicode(path);
Victor Stinneree587ea2011-11-17 00:51:38 +01001398 if (wpath == NULL)
1399 return NULL;
1400
Alexey Izbyshevb3b4a9d2018-02-18 20:57:24 +03001401 usize = MultiByteToWideChar(CP_ACP, 0, mode, -1,
1402 wmode, Py_ARRAY_LENGTH(wmode));
Victor Stinnere42ccd22015-03-18 01:39:23 +01001403 if (usize == 0) {
1404 PyErr_SetFromWindowsErr(0);
Victor Stinner4e314432010-10-07 21:45:39 +00001405 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001406 }
Victor Stinner4e314432010-10-07 21:45:39 +00001407
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001408 do {
1409 Py_BEGIN_ALLOW_THREADS
1410 f = _wfopen(wpath, wmode);
1411 Py_END_ALLOW_THREADS
1412 } while (f == NULL
1413 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner4e314432010-10-07 21:45:39 +00001414#else
Antoine Pitrou2b1cc892011-12-19 18:19:06 +01001415 PyObject *bytes;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001416 char *path_bytes;
1417
1418 assert(PyGILState_Check());
1419
Antoine Pitrou2b1cc892011-12-19 18:19:06 +01001420 if (!PyUnicode_FSConverter(path, &bytes))
Victor Stinner4e314432010-10-07 21:45:39 +00001421 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001422 path_bytes = PyBytes_AS_STRING(bytes);
1423
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001424 do {
1425 Py_BEGIN_ALLOW_THREADS
1426 f = fopen(path_bytes, mode);
1427 Py_END_ALLOW_THREADS
1428 } while (f == NULL
1429 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnere42ccd22015-03-18 01:39:23 +01001430
Victor Stinner4e314432010-10-07 21:45:39 +00001431 Py_DECREF(bytes);
Victor Stinner4e314432010-10-07 21:45:39 +00001432#endif
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001433 if (async_err)
1434 return NULL;
1435
Victor Stinnere42ccd22015-03-18 01:39:23 +01001436 if (f == NULL) {
1437 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001438 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001439 }
1440
1441 if (set_inheritable(fileno(f), 0, 1, NULL) < 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +02001442 fclose(f);
1443 return NULL;
1444 }
1445 return f;
Victor Stinner4e314432010-10-07 21:45:39 +00001446}
1447
Victor Stinner66aab0c2015-03-19 22:53:20 +01001448/* Read count bytes from fd into buf.
Victor Stinner82c3e452015-04-01 18:34:45 +02001449
1450 On success, return the number of read bytes, it can be lower than count.
1451 If the current file offset is at or past the end of file, no bytes are read,
1452 and read() returns zero.
1453
1454 On error, raise an exception, set errno and return -1.
1455
1456 When interrupted by a signal (read() fails with EINTR), retry the syscall.
1457 If the Python signal handler raises an exception, the function returns -1
1458 (the syscall is not retried).
1459
1460 Release the GIL to call read(). The caller must hold the GIL. */
Victor Stinner66aab0c2015-03-19 22:53:20 +01001461Py_ssize_t
1462_Py_read(int fd, void *buf, size_t count)
1463{
1464 Py_ssize_t n;
Victor Stinnera3c02022015-03-20 11:58:18 +01001465 int err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001466 int async_err = 0;
1467
Victor Stinner8a1be612016-03-14 22:07:55 +01001468 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +01001469
Victor Stinner66aab0c2015-03-19 22:53:20 +01001470 /* _Py_read() must not be called with an exception set, otherwise the
1471 * caller may think that read() was interrupted by a signal and the signal
1472 * handler raised an exception. */
1473 assert(!PyErr_Occurred());
1474
Stéphane Wirtel74a8b6e2018-10-18 01:05:04 +02001475 if (count > _PY_READ_MAX) {
1476 count = _PY_READ_MAX;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001477 }
Victor Stinner66aab0c2015-03-19 22:53:20 +01001478
Steve Dower8fc89802015-04-12 00:26:27 -04001479 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001480 do {
1481 Py_BEGIN_ALLOW_THREADS
1482 errno = 0;
1483#ifdef MS_WINDOWS
1484 n = read(fd, buf, (int)count);
1485#else
1486 n = read(fd, buf, count);
1487#endif
Victor Stinnera3c02022015-03-20 11:58:18 +01001488 /* save/restore errno because PyErr_CheckSignals()
1489 * and PyErr_SetFromErrno() can modify it */
1490 err = errno;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001491 Py_END_ALLOW_THREADS
Victor Stinnera3c02022015-03-20 11:58:18 +01001492 } while (n < 0 && err == EINTR &&
Victor Stinner66aab0c2015-03-19 22:53:20 +01001493 !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04001494 _Py_END_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001495
1496 if (async_err) {
1497 /* read() was interrupted by a signal (failed with EINTR)
1498 * and the Python signal handler raised an exception */
Victor Stinnera3c02022015-03-20 11:58:18 +01001499 errno = err;
1500 assert(errno == EINTR && PyErr_Occurred());
Victor Stinner66aab0c2015-03-19 22:53:20 +01001501 return -1;
1502 }
1503 if (n < 0) {
Victor Stinner66aab0c2015-03-19 22:53:20 +01001504 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera3c02022015-03-20 11:58:18 +01001505 errno = err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001506 return -1;
1507 }
1508
1509 return n;
1510}
1511
Victor Stinner82c3e452015-04-01 18:34:45 +02001512static Py_ssize_t
1513_Py_write_impl(int fd, const void *buf, size_t count, int gil_held)
Victor Stinner66aab0c2015-03-19 22:53:20 +01001514{
1515 Py_ssize_t n;
Victor Stinnera3c02022015-03-20 11:58:18 +01001516 int err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001517 int async_err = 0;
1518
Steve Dower8fc89802015-04-12 00:26:27 -04001519 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001520#ifdef MS_WINDOWS
1521 if (count > 32767 && isatty(fd)) {
1522 /* Issue #11395: the Windows console returns an error (12: not
1523 enough space error) on writing into stdout if stdout mode is
1524 binary and the length is greater than 66,000 bytes (or less,
1525 depending on heap usage). */
1526 count = 32767;
1527 }
Victor Stinner66aab0c2015-03-19 22:53:20 +01001528#endif
Stéphane Wirtel74a8b6e2018-10-18 01:05:04 +02001529 if (count > _PY_WRITE_MAX) {
1530 count = _PY_WRITE_MAX;
1531 }
Victor Stinner66aab0c2015-03-19 22:53:20 +01001532
Victor Stinner82c3e452015-04-01 18:34:45 +02001533 if (gil_held) {
1534 do {
1535 Py_BEGIN_ALLOW_THREADS
1536 errno = 0;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001537#ifdef MS_WINDOWS
Victor Stinner82c3e452015-04-01 18:34:45 +02001538 n = write(fd, buf, (int)count);
Victor Stinner66aab0c2015-03-19 22:53:20 +01001539#else
Victor Stinner82c3e452015-04-01 18:34:45 +02001540 n = write(fd, buf, count);
Victor Stinner66aab0c2015-03-19 22:53:20 +01001541#endif
Victor Stinner82c3e452015-04-01 18:34:45 +02001542 /* save/restore errno because PyErr_CheckSignals()
1543 * and PyErr_SetFromErrno() can modify it */
1544 err = errno;
1545 Py_END_ALLOW_THREADS
1546 } while (n < 0 && err == EINTR &&
1547 !(async_err = PyErr_CheckSignals()));
1548 }
1549 else {
1550 do {
1551 errno = 0;
1552#ifdef MS_WINDOWS
1553 n = write(fd, buf, (int)count);
1554#else
1555 n = write(fd, buf, count);
1556#endif
1557 err = errno;
1558 } while (n < 0 && err == EINTR);
1559 }
Steve Dower8fc89802015-04-12 00:26:27 -04001560 _Py_END_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001561
1562 if (async_err) {
1563 /* write() was interrupted by a signal (failed with EINTR)
Victor Stinner82c3e452015-04-01 18:34:45 +02001564 and the Python signal handler raised an exception (if gil_held is
1565 nonzero). */
Victor Stinnera3c02022015-03-20 11:58:18 +01001566 errno = err;
Victor Stinner82c3e452015-04-01 18:34:45 +02001567 assert(errno == EINTR && (!gil_held || PyErr_Occurred()));
Victor Stinner66aab0c2015-03-19 22:53:20 +01001568 return -1;
1569 }
1570 if (n < 0) {
Victor Stinner82c3e452015-04-01 18:34:45 +02001571 if (gil_held)
1572 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera3c02022015-03-20 11:58:18 +01001573 errno = err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001574 return -1;
1575 }
1576
1577 return n;
1578}
1579
Victor Stinner82c3e452015-04-01 18:34:45 +02001580/* Write count bytes of buf into fd.
1581
1582 On success, return the number of written bytes, it can be lower than count
1583 including 0. On error, raise an exception, set errno and return -1.
1584
1585 When interrupted by a signal (write() fails with EINTR), retry the syscall.
1586 If the Python signal handler raises an exception, the function returns -1
1587 (the syscall is not retried).
1588
1589 Release the GIL to call write(). The caller must hold the GIL. */
1590Py_ssize_t
1591_Py_write(int fd, const void *buf, size_t count)
1592{
Victor Stinner8a1be612016-03-14 22:07:55 +01001593 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +01001594
Victor Stinner82c3e452015-04-01 18:34:45 +02001595 /* _Py_write() must not be called with an exception set, otherwise the
1596 * caller may think that write() was interrupted by a signal and the signal
1597 * handler raised an exception. */
1598 assert(!PyErr_Occurred());
1599
1600 return _Py_write_impl(fd, buf, count, 1);
1601}
1602
1603/* Write count bytes of buf into fd.
1604 *
1605 * On success, return the number of written bytes, it can be lower than count
1606 * including 0. On error, set errno and return -1.
1607 *
1608 * When interrupted by a signal (write() fails with EINTR), retry the syscall
1609 * without calling the Python signal handler. */
1610Py_ssize_t
1611_Py_write_noraise(int fd, const void *buf, size_t count)
1612{
1613 return _Py_write_impl(fd, buf, count, 0);
1614}
1615
Victor Stinner4e314432010-10-07 21:45:39 +00001616#ifdef HAVE_READLINK
Victor Stinner6672d0c2010-10-07 22:53:43 +00001617
1618/* Read value of symbolic link. Encode the path to the locale encoding, decode
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001619 the result from the locale encoding. Return -1 on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001620
Victor Stinner4e314432010-10-07 21:45:39 +00001621int
1622_Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
1623{
1624 char *cpath;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001625 char cbuf[MAXPATHLEN];
Victor Stinner3f711f42010-10-16 22:47:37 +00001626 wchar_t *wbuf;
Victor Stinner4e314432010-10-07 21:45:39 +00001627 int res;
1628 size_t r1;
1629
Victor Stinner9dd76202017-12-21 16:20:32 +01001630 cpath = _Py_EncodeLocaleRaw(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001631 if (cpath == NULL) {
1632 errno = EINVAL;
1633 return -1;
1634 }
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001635 res = (int)readlink(cpath, cbuf, Py_ARRAY_LENGTH(cbuf));
Victor Stinner9dd76202017-12-21 16:20:32 +01001636 PyMem_RawFree(cpath);
Victor Stinner4e314432010-10-07 21:45:39 +00001637 if (res == -1)
1638 return -1;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001639 if (res == Py_ARRAY_LENGTH(cbuf)) {
Victor Stinner4e314432010-10-07 21:45:39 +00001640 errno = EINVAL;
1641 return -1;
1642 }
1643 cbuf[res] = '\0'; /* buf will be null terminated */
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001644 wbuf = Py_DecodeLocale(cbuf, &r1);
Victor Stinner350147b2010-10-16 22:52:09 +00001645 if (wbuf == NULL) {
1646 errno = EINVAL;
1647 return -1;
1648 }
Victor Stinner3f711f42010-10-16 22:47:37 +00001649 if (bufsiz <= r1) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001650 PyMem_RawFree(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001651 errno = EINVAL;
1652 return -1;
1653 }
Victor Stinner3f711f42010-10-16 22:47:37 +00001654 wcsncpy(buf, wbuf, bufsiz);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001655 PyMem_RawFree(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001656 return (int)r1;
1657}
1658#endif
1659
1660#ifdef HAVE_REALPATH
Victor Stinner6672d0c2010-10-07 22:53:43 +00001661
1662/* Return the canonicalized absolute pathname. Encode path to the locale
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001663 encoding, decode the result from the locale encoding.
1664 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001665
Victor Stinner4e314432010-10-07 21:45:39 +00001666wchar_t*
Victor Stinner015f4d82010-10-07 22:29:53 +00001667_Py_wrealpath(const wchar_t *path,
1668 wchar_t *resolved_path, size_t resolved_path_size)
Victor Stinner4e314432010-10-07 21:45:39 +00001669{
1670 char *cpath;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001671 char cresolved_path[MAXPATHLEN];
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001672 wchar_t *wresolved_path;
Victor Stinner4e314432010-10-07 21:45:39 +00001673 char *res;
1674 size_t r;
Victor Stinner9dd76202017-12-21 16:20:32 +01001675 cpath = _Py_EncodeLocaleRaw(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001676 if (cpath == NULL) {
1677 errno = EINVAL;
1678 return NULL;
1679 }
1680 res = realpath(cpath, cresolved_path);
Victor Stinner9dd76202017-12-21 16:20:32 +01001681 PyMem_RawFree(cpath);
Victor Stinner4e314432010-10-07 21:45:39 +00001682 if (res == NULL)
1683 return NULL;
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001684
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001685 wresolved_path = Py_DecodeLocale(cresolved_path, &r);
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001686 if (wresolved_path == NULL) {
Victor Stinner4e314432010-10-07 21:45:39 +00001687 errno = EINVAL;
1688 return NULL;
1689 }
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001690 if (resolved_path_size <= r) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001691 PyMem_RawFree(wresolved_path);
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001692 errno = EINVAL;
1693 return NULL;
1694 }
1695 wcsncpy(resolved_path, wresolved_path, resolved_path_size);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001696 PyMem_RawFree(wresolved_path);
Victor Stinner4e314432010-10-07 21:45:39 +00001697 return resolved_path;
1698}
1699#endif
1700
Victor Stinnerf4061da2010-10-14 12:37:19 +00001701/* Get the current directory. size is the buffer size in wide characters
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001702 including the null character. Decode the path from the locale encoding.
1703 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001704
Victor Stinner4e314432010-10-07 21:45:39 +00001705wchar_t*
1706_Py_wgetcwd(wchar_t *buf, size_t size)
1707{
1708#ifdef MS_WINDOWS
Victor Stinner56785ea2013-06-05 00:46:29 +02001709 int isize = (int)Py_MIN(size, INT_MAX);
1710 return _wgetcwd(buf, isize);
Victor Stinner4e314432010-10-07 21:45:39 +00001711#else
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001712 char fname[MAXPATHLEN];
Victor Stinnerf4061da2010-10-14 12:37:19 +00001713 wchar_t *wname;
Victor Stinner168e1172010-10-16 23:16:16 +00001714 size_t len;
Victor Stinnerf4061da2010-10-14 12:37:19 +00001715
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001716 if (getcwd(fname, Py_ARRAY_LENGTH(fname)) == NULL)
Victor Stinner4e314432010-10-07 21:45:39 +00001717 return NULL;
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001718 wname = Py_DecodeLocale(fname, &len);
Victor Stinnerf4061da2010-10-14 12:37:19 +00001719 if (wname == NULL)
1720 return NULL;
Victor Stinner168e1172010-10-16 23:16:16 +00001721 if (size <= len) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001722 PyMem_RawFree(wname);
Victor Stinner4e314432010-10-07 21:45:39 +00001723 return NULL;
1724 }
Victor Stinnerf4061da2010-10-14 12:37:19 +00001725 wcsncpy(buf, wname, size);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001726 PyMem_RawFree(wname);
Victor Stinner4e314432010-10-07 21:45:39 +00001727 return buf;
1728#endif
1729}
1730
Victor Stinnerdaf45552013-08-28 00:53:59 +02001731/* Duplicate a file descriptor. The new file descriptor is created as
1732 non-inheritable. Return a new file descriptor on success, raise an OSError
1733 exception and return -1 on error.
1734
1735 The GIL is released to call dup(). The caller must hold the GIL. */
1736int
1737_Py_dup(int fd)
1738{
1739#ifdef MS_WINDOWS
1740 HANDLE handle;
1741 DWORD ftype;
1742#endif
1743
Victor Stinner8a1be612016-03-14 22:07:55 +01001744 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +01001745
Victor Stinnerdaf45552013-08-28 00:53:59 +02001746#ifdef MS_WINDOWS
Steve Dower8fc89802015-04-12 00:26:27 -04001747 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001748 handle = (HANDLE)_get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001749 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001750 if (handle == INVALID_HANDLE_VALUE) {
Steve Dower41e72442015-03-14 11:38:27 -07001751 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001752 return -1;
1753 }
1754
1755 /* get the file type, ignore the error if it failed */
1756 ftype = GetFileType(handle);
1757
1758 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001759 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001760 fd = dup(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001761 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001762 Py_END_ALLOW_THREADS
1763 if (fd < 0) {
1764 PyErr_SetFromErrno(PyExc_OSError);
1765 return -1;
1766 }
1767
1768 /* Character files like console cannot be make non-inheritable */
1769 if (ftype != FILE_TYPE_CHAR) {
1770 if (_Py_set_inheritable(fd, 0, NULL) < 0) {
Steve Dower8fc89802015-04-12 00:26:27 -04001771 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001772 close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001773 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001774 return -1;
1775 }
1776 }
1777#elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC)
1778 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001779 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001780 fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Steve Dower8fc89802015-04-12 00:26:27 -04001781 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001782 Py_END_ALLOW_THREADS
1783 if (fd < 0) {
1784 PyErr_SetFromErrno(PyExc_OSError);
1785 return -1;
1786 }
1787
1788#else
1789 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001790 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001791 fd = dup(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001792 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001793 Py_END_ALLOW_THREADS
1794 if (fd < 0) {
1795 PyErr_SetFromErrno(PyExc_OSError);
1796 return -1;
1797 }
1798
1799 if (_Py_set_inheritable(fd, 0, NULL) < 0) {
Steve Dower8fc89802015-04-12 00:26:27 -04001800 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001801 close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001802 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001803 return -1;
1804 }
1805#endif
1806 return fd;
1807}
1808
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001809#ifndef MS_WINDOWS
1810/* Get the blocking mode of the file descriptor.
1811 Return 0 if the O_NONBLOCK flag is set, 1 if the flag is cleared,
1812 raise an exception and return -1 on error. */
1813int
1814_Py_get_blocking(int fd)
1815{
Steve Dower8fc89802015-04-12 00:26:27 -04001816 int flags;
1817 _Py_BEGIN_SUPPRESS_IPH
1818 flags = fcntl(fd, F_GETFL, 0);
1819 _Py_END_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001820 if (flags < 0) {
1821 PyErr_SetFromErrno(PyExc_OSError);
1822 return -1;
1823 }
1824
1825 return !(flags & O_NONBLOCK);
1826}
1827
1828/* Set the blocking mode of the specified file descriptor.
1829
1830 Set the O_NONBLOCK flag if blocking is False, clear the O_NONBLOCK flag
1831 otherwise.
1832
1833 Return 0 on success, raise an exception and return -1 on error. */
1834int
1835_Py_set_blocking(int fd, int blocking)
1836{
1837#if defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)
1838 int arg = !blocking;
1839 if (ioctl(fd, FIONBIO, &arg) < 0)
1840 goto error;
1841#else
1842 int flags, res;
1843
Steve Dower8fc89802015-04-12 00:26:27 -04001844 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001845 flags = fcntl(fd, F_GETFL, 0);
Steve Dower8fc89802015-04-12 00:26:27 -04001846 if (flags >= 0) {
1847 if (blocking)
1848 flags = flags & (~O_NONBLOCK);
1849 else
1850 flags = flags | O_NONBLOCK;
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001851
Steve Dower8fc89802015-04-12 00:26:27 -04001852 res = fcntl(fd, F_SETFL, flags);
1853 } else {
1854 res = -1;
1855 }
1856 _Py_END_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001857
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001858 if (res < 0)
1859 goto error;
1860#endif
1861 return 0;
1862
1863error:
1864 PyErr_SetFromErrno(PyExc_OSError);
1865 return -1;
1866}
1867#endif
Victor Stinnercb064fc2018-01-15 15:58:02 +01001868
1869
1870int
Victor Stinner02e6bf72018-11-20 16:20:16 +01001871_Py_GetLocaleconvNumeric(struct lconv *lc,
1872 PyObject **decimal_point, PyObject **thousands_sep)
Victor Stinnercb064fc2018-01-15 15:58:02 +01001873{
Victor Stinner02e6bf72018-11-20 16:20:16 +01001874 assert(decimal_point != NULL);
1875 assert(thousands_sep != NULL);
Victor Stinnercb064fc2018-01-15 15:58:02 +01001876
1877 int change_locale = 0;
Victor Stinner02e6bf72018-11-20 16:20:16 +01001878 if ((strlen(lc->decimal_point) > 1 || ((unsigned char)lc->decimal_point[0]) > 127)) {
Victor Stinnercb064fc2018-01-15 15:58:02 +01001879 change_locale = 1;
1880 }
Victor Stinner02e6bf72018-11-20 16:20:16 +01001881 if ((strlen(lc->thousands_sep) > 1 || ((unsigned char)lc->thousands_sep[0]) > 127)) {
Victor Stinnercb064fc2018-01-15 15:58:02 +01001882 change_locale = 1;
1883 }
1884
1885 /* Keep a copy of the LC_CTYPE locale */
1886 char *oldloc = NULL, *loc = NULL;
1887 if (change_locale) {
1888 oldloc = setlocale(LC_CTYPE, NULL);
1889 if (!oldloc) {
Victor Stinner02e6bf72018-11-20 16:20:16 +01001890 PyErr_SetString(PyExc_RuntimeWarning,
1891 "failed to get LC_CTYPE locale");
Victor Stinnercb064fc2018-01-15 15:58:02 +01001892 return -1;
1893 }
1894
1895 oldloc = _PyMem_Strdup(oldloc);
1896 if (!oldloc) {
1897 PyErr_NoMemory();
1898 return -1;
1899 }
1900
1901 loc = setlocale(LC_NUMERIC, NULL);
1902 if (loc != NULL && strcmp(loc, oldloc) == 0) {
1903 loc = NULL;
1904 }
1905
1906 if (loc != NULL) {
Victor Stinner02e6bf72018-11-20 16:20:16 +01001907 /* Only set the locale temporarily the LC_CTYPE locale
Victor Stinnercb064fc2018-01-15 15:58:02 +01001908 if LC_NUMERIC locale is different than LC_CTYPE locale and
1909 decimal_point and/or thousands_sep are non-ASCII or longer than
1910 1 byte */
1911 setlocale(LC_CTYPE, loc);
1912 }
1913 }
1914
Victor Stinner02e6bf72018-11-20 16:20:16 +01001915 int res = -1;
1916
1917 *decimal_point = PyUnicode_DecodeLocale(lc->decimal_point, NULL);
1918 if (*decimal_point == NULL) {
1919 goto done;
Victor Stinnercb064fc2018-01-15 15:58:02 +01001920 }
1921
Victor Stinner02e6bf72018-11-20 16:20:16 +01001922 *thousands_sep = PyUnicode_DecodeLocale(lc->thousands_sep, NULL);
1923 if (*thousands_sep == NULL) {
1924 goto done;
Victor Stinnercb064fc2018-01-15 15:58:02 +01001925 }
1926
1927 res = 0;
1928
Victor Stinner02e6bf72018-11-20 16:20:16 +01001929done:
Victor Stinnercb064fc2018-01-15 15:58:02 +01001930 if (loc != NULL) {
1931 setlocale(LC_CTYPE, oldloc);
1932 }
1933 PyMem_Free(oldloc);
1934 return res;
1935}