blob: 0486f865924a499e6bd8fdce00c86279437c9f60 [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>
Steve Dower8fc89802015-04-12 00:26:27 -04008extern int winerror_to_errno(int);
Victor Stinnerb306d752010-10-07 22:09:40 +00009#endif
Victor Stinner4e314432010-10-07 21:45:39 +000010
Brett Cannonefb00c02012-02-29 18:31:31 -050011#ifdef HAVE_LANGINFO_H
12#include <langinfo.h>
13#endif
14
Victor Stinnerdaf45552013-08-28 00:53:59 +020015#ifdef HAVE_SYS_IOCTL_H
16#include <sys/ioctl.h>
17#endif
18
19#ifdef HAVE_FCNTL_H
20#include <fcntl.h>
21#endif /* HAVE_FCNTL_H */
22
Victor Stinnerdaf45552013-08-28 00:53:59 +020023#ifdef O_CLOEXEC
Victor Stinnerb034eee2013-09-07 10:36:04 +020024/* Does open() support the O_CLOEXEC flag? Possible values:
Victor Stinnerdaf45552013-08-28 00:53:59 +020025
26 -1: unknown
27 0: open() ignores O_CLOEXEC flag, ex: Linux kernel older than 2.6.23
28 1: open() supports O_CLOEXEC flag, close-on-exec is set
29
Victor Stinnera555cfc2015-03-18 00:22:14 +010030 The flag is used by _Py_open(), _Py_open_noraise(), io.FileIO
31 and os.open(). */
Victor Stinnerdaf45552013-08-28 00:53:59 +020032int _Py_open_cloexec_works = -1;
33#endif
34
Victor Stinner3d4226a2018-08-29 22:21:32 +020035
36static int
37get_surrogateescape(_Py_error_handler errors, int *surrogateescape)
38{
39 switch (errors)
40 {
41 case _Py_ERROR_STRICT:
42 *surrogateescape = 0;
43 return 0;
44 case _Py_ERROR_SURROGATEESCAPE:
45 *surrogateescape = 1;
46 return 0;
47 default:
48 return -1;
49 }
50}
51
52
Brett Cannonefb00c02012-02-29 18:31:31 -050053PyObject *
54_Py_device_encoding(int fd)
55{
Victor Stinner14b9b112013-06-25 00:37:25 +020056#if defined(MS_WINDOWS)
Brett Cannonefb00c02012-02-29 18:31:31 -050057 UINT cp;
58#endif
Steve Dower8fc89802015-04-12 00:26:27 -040059 int valid;
60 _Py_BEGIN_SUPPRESS_IPH
Steve Dower940f33a2016-09-08 11:21:54 -070061 valid = isatty(fd);
Steve Dower8fc89802015-04-12 00:26:27 -040062 _Py_END_SUPPRESS_IPH
63 if (!valid)
Brett Cannonefb00c02012-02-29 18:31:31 -050064 Py_RETURN_NONE;
Steve Dower8fc89802015-04-12 00:26:27 -040065
Victor Stinner14b9b112013-06-25 00:37:25 +020066#if defined(MS_WINDOWS)
Brett Cannonefb00c02012-02-29 18:31:31 -050067 if (fd == 0)
68 cp = GetConsoleCP();
69 else if (fd == 1 || fd == 2)
70 cp = GetConsoleOutputCP();
71 else
72 cp = 0;
73 /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
74 has no console */
75 if (cp != 0)
76 return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
77#elif defined(CODESET)
78 {
79 char *codeset = nl_langinfo(CODESET);
80 if (codeset != NULL && codeset[0] != 0)
81 return PyUnicode_FromString(codeset);
82 }
83#endif
84 Py_RETURN_NONE;
85}
86
Victor Stinner7ed7aea2018-01-15 10:45:49 +010087#if !defined(__APPLE__) && !defined(__ANDROID__) && !defined(MS_WINDOWS)
88
89#define USE_FORCE_ASCII
90
Victor Stinnerd45c7f82012-12-04 01:34:47 +010091extern int _Py_normalize_encoding(const char *, char *, size_t);
92
Victor Stinnerd500e532018-08-28 17:27:36 +020093/* Workaround FreeBSD and OpenIndiana locale encoding issue with the C locale
94 and POSIX locale. nl_langinfo(CODESET) announces an alias of the
Victor Stinnerd45c7f82012-12-04 01:34:47 +010095 ASCII encoding, whereas mbstowcs() and wcstombs() functions use the
96 ISO-8859-1 encoding. The problem is that os.fsencode() and os.fsdecode() use
97 locale.getpreferredencoding() codec. For example, if command line arguments
98 are decoded by mbstowcs() and encoded back by os.fsencode(), we get a
99 UnicodeEncodeError instead of retrieving the original byte string.
100
101 The workaround is enabled if setlocale(LC_CTYPE, NULL) returns "C",
102 nl_langinfo(CODESET) announces "ascii" (or an alias to ASCII), and at least
103 one byte in range 0x80-0xff can be decoded from the locale encoding. The
104 workaround is also enabled on error, for example if getting the locale
105 failed.
106
Victor Stinnerd500e532018-08-28 17:27:36 +0200107 On HP-UX with the C locale or the POSIX locale, nl_langinfo(CODESET)
108 announces "roman8" but mbstowcs() uses Latin1 in practice. Force also the
109 ASCII encoding in this case.
110
Philip Jenvey215c49a2013-01-15 13:24:12 -0800111 Values of force_ascii:
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100112
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200113 1: the workaround is used: Py_EncodeLocale() uses
114 encode_ascii_surrogateescape() and Py_DecodeLocale() uses
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100115 decode_ascii()
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200116 0: the workaround is not used: Py_EncodeLocale() uses wcstombs() and
117 Py_DecodeLocale() uses mbstowcs()
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100118 -1: unknown, need to call check_force_ascii() to get the value
119*/
120static int force_ascii = -1;
121
122static int
123check_force_ascii(void)
124{
Victor Stinnerd500e532018-08-28 17:27:36 +0200125 char *loc = setlocale(LC_CTYPE, NULL);
126 if (loc == NULL) {
127 goto error;
128 }
129 if (strcmp(loc, "C") != 0 && strcmp(loc, "POSIX") != 0) {
130 /* the LC_CTYPE locale is different than C and POSIX */
131 return 0;
132 }
133
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100134#if defined(HAVE_LANGINFO_H) && defined(CODESET)
Victor Stinnerd500e532018-08-28 17:27:36 +0200135 const char *codeset = nl_langinfo(CODESET);
136 if (!codeset || codeset[0] == '\0') {
137 /* CODESET is not set or empty */
138 goto error;
139 }
140
Victor Stinner54de2b12016-09-09 23:11:52 -0700141 char encoding[20]; /* longest name: "iso_646.irv_1991\0" */
Victor Stinnerd500e532018-08-28 17:27:36 +0200142 if (!_Py_normalize_encoding(codeset, encoding, sizeof(encoding))) {
143 goto error;
144 }
145
146#ifdef __hpux
147 if (strcmp(encoding, "roman8") == 0) {
148 unsigned char ch;
149 wchar_t wch;
150 size_t res;
151
152 ch = (unsigned char)0xA7;
153 res = mbstowcs(&wch, (char*)&ch, 1);
154 if (res != (size_t)-1 && wch == L'\xA7') {
155 /* On HP-UX withe C locale or the POSIX locale,
156 nl_langinfo(CODESET) announces "roman8", whereas mbstowcs() uses
157 Latin1 encoding in practice. Force ASCII in this case.
158
159 Roman8 decodes 0xA7 to U+00CF. Latin1 decodes 0xA7 to U+00A7. */
160 return 1;
161 }
162 }
163#else
164 const char* ascii_aliases[] = {
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100165 "ascii",
Victor Stinner54de2b12016-09-09 23:11:52 -0700166 /* Aliases from Lib/encodings/aliases.py */
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100167 "646",
Victor Stinner54de2b12016-09-09 23:11:52 -0700168 "ansi_x3.4_1968",
169 "ansi_x3.4_1986",
170 "ansi_x3_4_1968",
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100171 "cp367",
172 "csascii",
173 "ibm367",
Victor Stinner54de2b12016-09-09 23:11:52 -0700174 "iso646_us",
175 "iso_646.irv_1991",
176 "iso_ir_6",
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100177 "us",
Victor Stinner54de2b12016-09-09 23:11:52 -0700178 "us_ascii",
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100179 NULL
180 };
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100181
Victor Stinnerd500e532018-08-28 17:27:36 +0200182 int is_ascii = 0;
183 for (const char **alias=ascii_aliases; *alias != NULL; alias++) {
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100184 if (strcmp(encoding, *alias) == 0) {
185 is_ascii = 1;
186 break;
187 }
188 }
189 if (!is_ascii) {
190 /* nl_langinfo(CODESET) is not "ascii" or an alias of ASCII */
191 return 0;
192 }
193
Victor Stinnerd500e532018-08-28 17:27:36 +0200194 for (unsigned int i=0x80; i<=0xff; i++) {
195 char ch[1];
196 wchar_t wch[1];
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100197 size_t res;
198
Victor Stinnerd500e532018-08-28 17:27:36 +0200199 unsigned uch = (unsigned char)i;
200 ch[0] = (char)uch;
201 res = mbstowcs(wch, ch, 1);
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100202 if (res != (size_t)-1) {
203 /* decoding a non-ASCII character from the locale encoding succeed:
204 the locale encoding is not ASCII, force ASCII */
205 return 1;
206 }
207 }
208 /* None of the bytes in the range 0x80-0xff can be decoded from the locale
209 encoding: the locale encoding is really ASCII */
Victor Stinnerd500e532018-08-28 17:27:36 +0200210#endif /* !defined(__hpux) */
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100211 return 0;
212#else
213 /* nl_langinfo(CODESET) is not available: always force ASCII */
214 return 1;
Victor Stinnerd500e532018-08-28 17:27:36 +0200215#endif /* defined(HAVE_LANGINFO_H) && defined(CODESET) */
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100216
217error:
Martin Panter46f50722016-05-26 05:35:26 +0000218 /* if an error occurred, force the ASCII encoding */
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100219 return 1;
220}
221
Victor Stinnerd500e532018-08-28 17:27:36 +0200222
223int
224_Py_GetForceASCII(void)
225{
226 if (force_ascii == -1) {
227 force_ascii = check_force_ascii();
228 }
229 return force_ascii;
230}
231
232
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100233static int
234encode_ascii(const wchar_t *text, char **str,
235 size_t *error_pos, const char **reason,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200236 int raw_malloc, _Py_error_handler errors)
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100237{
238 char *result = NULL, *out;
239 size_t len, i;
240 wchar_t ch;
241
Victor Stinner3d4226a2018-08-29 22:21:32 +0200242 int surrogateescape;
243 if (get_surrogateescape(errors, &surrogateescape) < 0) {
244 return -3;
245 }
246
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100247 len = wcslen(text);
248
Victor Stinner9bee3292017-12-21 16:49:13 +0100249 /* +1 for NULL byte */
Victor Stinner9dd76202017-12-21 16:20:32 +0100250 if (raw_malloc) {
251 result = PyMem_RawMalloc(len + 1);
252 }
253 else {
254 result = PyMem_Malloc(len + 1);
255 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100256 if (result == NULL) {
257 return -1;
258 }
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100259
260 out = result;
261 for (i=0; i<len; i++) {
262 ch = text[i];
263
264 if (ch <= 0x7f) {
265 /* ASCII character */
266 *out++ = (char)ch;
267 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100268 else if (surrogateescape && 0xdc80 <= ch && ch <= 0xdcff) {
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100269 /* UTF-8b surrogate */
270 *out++ = (char)(ch - 0xdc00);
271 }
272 else {
Victor Stinner9dd76202017-12-21 16:20:32 +0100273 if (raw_malloc) {
274 PyMem_RawFree(result);
275 }
276 else {
277 PyMem_Free(result);
278 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100279 if (error_pos != NULL) {
280 *error_pos = i;
281 }
282 if (reason) {
283 *reason = "encoding error";
284 }
285 return -2;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100286 }
287 }
288 *out = '\0';
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100289 *str = result;
290 return 0;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100291}
Victor Stinnerd500e532018-08-28 17:27:36 +0200292#else
293int
294_Py_GetForceASCII(void)
295{
296 return 0;
297}
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100298#endif /* !defined(__APPLE__) && !defined(__ANDROID__) && !defined(MS_WINDOWS) */
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100299
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100300
301#if !defined(HAVE_MBRTOWC) || defined(USE_FORCE_ASCII)
302static int
303decode_ascii(const char *arg, wchar_t **wstr, size_t *wlen,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200304 const char **reason, _Py_error_handler errors)
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100305{
306 wchar_t *res;
307 unsigned char *in;
308 wchar_t *out;
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600309 size_t argsize = strlen(arg) + 1;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100310
Victor Stinner3d4226a2018-08-29 22:21:32 +0200311 int surrogateescape;
312 if (get_surrogateescape(errors, &surrogateescape) < 0) {
313 return -3;
314 }
315
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100316 if (argsize > PY_SSIZE_T_MAX / sizeof(wchar_t)) {
317 return -1;
318 }
319 res = PyMem_RawMalloc(argsize * sizeof(wchar_t));
320 if (!res) {
321 return -1;
322 }
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100323
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100324 out = res;
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100325 for (in = (unsigned char*)arg; *in; in++) {
326 unsigned char ch = *in;
327 if (ch < 128) {
328 *out++ = ch;
329 }
330 else {
331 if (!surrogateescape) {
332 PyMem_RawFree(res);
333 if (wlen) {
334 *wlen = in - (unsigned char*)arg;
335 }
336 if (reason) {
337 *reason = "decoding error";
338 }
339 return -2;
340 }
341 *out++ = 0xdc00 + ch;
342 }
343 }
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100344 *out = 0;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100345
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100346 if (wlen != NULL) {
347 *wlen = out - res;
348 }
349 *wstr = res;
350 return 0;
351}
352#endif /* !HAVE_MBRTOWC */
353
354static int
355decode_current_locale(const char* arg, wchar_t **wstr, size_t *wlen,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200356 const char **reason, _Py_error_handler errors)
Victor Stinner4e314432010-10-07 21:45:39 +0000357{
358 wchar_t *res;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100359 size_t argsize;
Victor Stinner4e314432010-10-07 21:45:39 +0000360 size_t count;
Victor Stinner313f10c2013-05-07 23:48:56 +0200361#ifdef HAVE_MBRTOWC
Victor Stinner4e314432010-10-07 21:45:39 +0000362 unsigned char *in;
363 wchar_t *out;
Victor Stinner4e314432010-10-07 21:45:39 +0000364 mbstate_t mbs;
365#endif
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100366
Victor Stinner3d4226a2018-08-29 22:21:32 +0200367 int surrogateescape;
368 if (get_surrogateescape(errors, &surrogateescape) < 0) {
369 return -3;
370 }
371
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100372#ifdef HAVE_BROKEN_MBSTOWCS
373 /* Some platforms have a broken implementation of
374 * mbstowcs which does not count the characters that
375 * would result from conversion. Use an upper bound.
376 */
377 argsize = strlen(arg);
378#else
379 argsize = mbstowcs(NULL, arg, 0);
380#endif
Victor Stinner4e314432010-10-07 21:45:39 +0000381 if (argsize != (size_t)-1) {
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100382 if (argsize > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) {
383 return -1;
384 }
385 res = (wchar_t *)PyMem_RawMalloc((argsize + 1) * sizeof(wchar_t));
386 if (!res) {
387 return -1;
388 }
389
390 count = mbstowcs(res, arg, argsize + 1);
Victor Stinner4e314432010-10-07 21:45:39 +0000391 if (count != (size_t)-1) {
392 wchar_t *tmp;
393 /* Only use the result if it contains no
394 surrogate characters. */
395 for (tmp = res; *tmp != 0 &&
Victor Stinner76df43d2012-10-30 01:42:39 +0100396 !Py_UNICODE_IS_SURROGATE(*tmp); tmp++)
Victor Stinner4e314432010-10-07 21:45:39 +0000397 ;
Victor Stinner168e1172010-10-16 23:16:16 +0000398 if (*tmp == 0) {
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100399 if (wlen != NULL) {
400 *wlen = count;
401 }
402 *wstr = res;
403 return 0;
Victor Stinner168e1172010-10-16 23:16:16 +0000404 }
Victor Stinner4e314432010-10-07 21:45:39 +0000405 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200406 PyMem_RawFree(res);
Victor Stinner4e314432010-10-07 21:45:39 +0000407 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100408
Victor Stinner4e314432010-10-07 21:45:39 +0000409 /* Conversion failed. Fall back to escaping with surrogateescape. */
410#ifdef HAVE_MBRTOWC
411 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
412
413 /* Overallocate; as multi-byte characters are in the argument, the
414 actual output could use less memory. */
415 argsize = strlen(arg) + 1;
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100416 if (argsize > PY_SSIZE_T_MAX / sizeof(wchar_t)) {
417 return -1;
418 }
419 res = (wchar_t*)PyMem_RawMalloc(argsize * sizeof(wchar_t));
420 if (!res) {
421 return -1;
422 }
423
Victor Stinner4e314432010-10-07 21:45:39 +0000424 in = (unsigned char*)arg;
425 out = res;
426 memset(&mbs, 0, sizeof mbs);
427 while (argsize) {
428 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100429 if (converted == 0) {
Victor Stinner4e314432010-10-07 21:45:39 +0000430 /* Reached end of string; null char stored. */
431 break;
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100432 }
433
Victor Stinner4e314432010-10-07 21:45:39 +0000434 if (converted == (size_t)-2) {
435 /* Incomplete character. This should never happen,
436 since we provide everything that we have -
437 unless there is a bug in the C library, or I
438 misunderstood how mbrtowc works. */
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100439 goto decode_error;
Victor Stinner4e314432010-10-07 21:45:39 +0000440 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100441
Victor Stinner4e314432010-10-07 21:45:39 +0000442 if (converted == (size_t)-1) {
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100443 if (!surrogateescape) {
444 goto decode_error;
445 }
446
Victor Stinner4e314432010-10-07 21:45:39 +0000447 /* Conversion error. Escape as UTF-8b, and start over
448 in the initial shift state. */
449 *out++ = 0xdc00 + *in++;
450 argsize--;
451 memset(&mbs, 0, sizeof mbs);
452 continue;
453 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100454
Victor Stinner76df43d2012-10-30 01:42:39 +0100455 if (Py_UNICODE_IS_SURROGATE(*out)) {
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100456 if (!surrogateescape) {
457 goto decode_error;
458 }
459
Victor Stinner4e314432010-10-07 21:45:39 +0000460 /* Surrogate character. Escape the original
461 byte sequence with surrogateescape. */
462 argsize -= converted;
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100463 while (converted--) {
Victor Stinner4e314432010-10-07 21:45:39 +0000464 *out++ = 0xdc00 + *in++;
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100465 }
Victor Stinner4e314432010-10-07 21:45:39 +0000466 continue;
467 }
468 /* successfully converted some bytes */
469 in += converted;
470 argsize -= converted;
471 out++;
472 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100473 if (wlen != NULL) {
474 *wlen = out - res;
475 }
476 *wstr = res;
477 return 0;
478
479decode_error:
480 PyMem_RawFree(res);
481 if (wlen) {
482 *wlen = in - (unsigned char*)arg;
483 }
484 if (reason) {
485 *reason = "decoding error";
486 }
487 return -2;
Victor Stinnere2623772012-11-12 23:04:02 +0100488#else /* HAVE_MBRTOWC */
Victor Stinner4e314432010-10-07 21:45:39 +0000489 /* Cannot use C locale for escaping; manually escape as if charset
490 is ASCII (i.e. escape all bytes > 128. This will still roundtrip
491 correctly in the locale's charset, which must be an ASCII superset. */
Victor Stinner3d4226a2018-08-29 22:21:32 +0200492 return decode_ascii(arg, wstr, wlen, reason, errors);
Victor Stinnere2623772012-11-12 23:04:02 +0100493#endif /* HAVE_MBRTOWC */
Victor Stinner91106cd2017-12-13 12:29:09 +0100494}
495
496
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100497/* Decode a byte string from the locale encoding.
498
499 Use the strict error handler if 'surrogateescape' is zero. Use the
500 surrogateescape error handler if 'surrogateescape' is non-zero: undecodable
501 bytes are decoded as characters in range U+DC80..U+DCFF. If a byte sequence
502 can be decoded as a surrogate character, escape the bytes using the
503 surrogateescape error handler instead of decoding them.
504
Ville Skyttä61f82e02018-04-20 23:08:45 +0300505 On success, return 0 and write the newly allocated wide character string into
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100506 *wstr (use PyMem_RawFree() to free the memory). If wlen is not NULL, write
507 the number of wide characters excluding the null character into *wlen.
508
509 On memory allocation failure, return -1.
510
511 On decoding error, return -2. If wlen is not NULL, write the start of
512 invalid byte sequence in the input string into *wlen. If reason is not NULL,
513 write the decoding error message into *reason.
514
Victor Stinner3d4226a2018-08-29 22:21:32 +0200515 Return -3 if the error handler 'errors' is not supported.
516
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100517 Use the Py_EncodeLocaleEx() function to encode the character string back to
518 a byte string. */
519int
520_Py_DecodeLocaleEx(const char* arg, wchar_t **wstr, size_t *wlen,
521 const char **reason,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200522 int current_locale, _Py_error_handler errors)
Victor Stinner2cba6b82018-01-10 22:46:15 +0100523{
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100524 if (current_locale) {
Victor Stinner9089a262018-01-22 19:07:32 +0100525#ifdef __ANDROID__
526 return _Py_DecodeUTF8Ex(arg, strlen(arg), wstr, wlen, reason,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200527 errors);
Victor Stinner9089a262018-01-22 19:07:32 +0100528#else
Victor Stinner3d4226a2018-08-29 22:21:32 +0200529 return decode_current_locale(arg, wstr, wlen, reason, errors);
Victor Stinner9089a262018-01-22 19:07:32 +0100530#endif
Victor Stinner2cba6b82018-01-10 22:46:15 +0100531 }
532
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100533#if defined(__APPLE__) || defined(__ANDROID__)
534 return _Py_DecodeUTF8Ex(arg, strlen(arg), wstr, wlen, reason,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200535 errors);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100536#else
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200537 int use_utf8 = (Py_UTF8Mode == 1);
538#ifdef MS_WINDOWS
539 use_utf8 |= !Py_LegacyWindowsFSEncodingFlag;
540#endif
541 if (use_utf8) {
Victor Stinner3d4226a2018-08-29 22:21:32 +0200542 return _Py_DecodeUTF8Ex(arg, strlen(arg), wstr, wlen, reason,
543 errors);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100544 }
545
546#ifdef USE_FORCE_ASCII
547 if (force_ascii == -1) {
Victor Stinner2cba6b82018-01-10 22:46:15 +0100548 force_ascii = check_force_ascii();
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100549 }
Victor Stinner2cba6b82018-01-10 22:46:15 +0100550
551 if (force_ascii) {
552 /* force ASCII encoding to workaround mbstowcs() issue */
Victor Stinner3d4226a2018-08-29 22:21:32 +0200553 return decode_ascii(arg, wstr, wlen, reason, errors);
Victor Stinner2cba6b82018-01-10 22:46:15 +0100554 }
555#endif
556
Victor Stinner3d4226a2018-08-29 22:21:32 +0200557 return decode_current_locale(arg, wstr, wlen, reason, errors);
Victor Stinner2cba6b82018-01-10 22:46:15 +0100558#endif /* __APPLE__ or __ANDROID__ */
559}
560
561
Victor Stinner91106cd2017-12-13 12:29:09 +0100562/* Decode a byte string from the locale encoding with the
563 surrogateescape error handler: undecodable bytes are decoded as characters
564 in range U+DC80..U+DCFF. If a byte sequence can be decoded as a surrogate
565 character, escape the bytes using the surrogateescape error handler instead
566 of decoding them.
567
568 Return a pointer to a newly allocated wide character string, use
569 PyMem_RawFree() to free the memory. If size is not NULL, write the number of
570 wide characters excluding the null character into *size
571
572 Return NULL on decoding error or memory allocation error. If *size* is not
573 NULL, *size is set to (size_t)-1 on memory error or set to (size_t)-2 on
574 decoding error.
575
576 Decoding errors should never happen, unless there is a bug in the C
577 library.
578
579 Use the Py_EncodeLocale() function to encode the character string back to a
580 byte string. */
581wchar_t*
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100582Py_DecodeLocale(const char* arg, size_t *wlen)
Victor Stinner91106cd2017-12-13 12:29:09 +0100583{
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100584 wchar_t *wstr;
Victor Stinner3d4226a2018-08-29 22:21:32 +0200585 int res = _Py_DecodeLocaleEx(arg, &wstr, wlen,
586 NULL, 0,
587 _Py_ERROR_SURROGATEESCAPE);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100588 if (res != 0) {
Victor Stinner3d4226a2018-08-29 22:21:32 +0200589 assert(res != -3);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100590 if (wlen != NULL) {
591 *wlen = (size_t)res;
592 }
593 return NULL;
594 }
595 return wstr;
Victor Stinner2cba6b82018-01-10 22:46:15 +0100596}
Victor Stinner91106cd2017-12-13 12:29:09 +0100597
Victor Stinner91106cd2017-12-13 12:29:09 +0100598
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100599static int
600encode_current_locale(const wchar_t *text, char **str,
601 size_t *error_pos, const char **reason,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200602 int raw_malloc, _Py_error_handler errors)
Victor Stinner91106cd2017-12-13 12:29:09 +0100603{
Victor Stinner4e314432010-10-07 21:45:39 +0000604 const size_t len = wcslen(text);
605 char *result = NULL, *bytes = NULL;
606 size_t i, size, converted;
607 wchar_t c, buf[2];
608
Victor Stinner3d4226a2018-08-29 22:21:32 +0200609 int surrogateescape;
610 if (get_surrogateescape(errors, &surrogateescape) < 0) {
611 return -3;
612 }
613
Victor Stinner4e314432010-10-07 21:45:39 +0000614 /* The function works in two steps:
615 1. compute the length of the output buffer in bytes (size)
616 2. outputs the bytes */
617 size = 0;
618 buf[1] = 0;
619 while (1) {
620 for (i=0; i < len; i++) {
621 c = text[i];
622 if (c >= 0xdc80 && c <= 0xdcff) {
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100623 if (!surrogateescape) {
624 goto encode_error;
625 }
Victor Stinner4e314432010-10-07 21:45:39 +0000626 /* UTF-8b surrogate */
627 if (bytes != NULL) {
628 *bytes++ = c - 0xdc00;
629 size--;
630 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100631 else {
Victor Stinner4e314432010-10-07 21:45:39 +0000632 size++;
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100633 }
Victor Stinner4e314432010-10-07 21:45:39 +0000634 continue;
635 }
636 else {
637 buf[0] = c;
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100638 if (bytes != NULL) {
Victor Stinner4e314432010-10-07 21:45:39 +0000639 converted = wcstombs(bytes, buf, size);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100640 }
641 else {
Victor Stinner4e314432010-10-07 21:45:39 +0000642 converted = wcstombs(NULL, buf, 0);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100643 }
Victor Stinner4e314432010-10-07 21:45:39 +0000644 if (converted == (size_t)-1) {
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100645 goto encode_error;
Victor Stinner4e314432010-10-07 21:45:39 +0000646 }
647 if (bytes != NULL) {
648 bytes += converted;
649 size -= converted;
650 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100651 else {
Victor Stinner4e314432010-10-07 21:45:39 +0000652 size += converted;
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100653 }
Victor Stinner4e314432010-10-07 21:45:39 +0000654 }
655 }
656 if (result != NULL) {
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100657 *bytes = '\0';
Victor Stinner4e314432010-10-07 21:45:39 +0000658 break;
659 }
660
661 size += 1; /* nul byte at the end */
Victor Stinner9dd76202017-12-21 16:20:32 +0100662 if (raw_malloc) {
663 result = PyMem_RawMalloc(size);
664 }
665 else {
666 result = PyMem_Malloc(size);
667 }
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100668 if (result == NULL) {
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100669 return -1;
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100670 }
Victor Stinner4e314432010-10-07 21:45:39 +0000671 bytes = result;
672 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100673 *str = result;
674 return 0;
675
676encode_error:
677 if (raw_malloc) {
678 PyMem_RawFree(result);
679 }
680 else {
681 PyMem_Free(result);
682 }
683 if (error_pos != NULL) {
684 *error_pos = i;
685 }
686 if (reason) {
687 *reason = "encoding error";
688 }
689 return -2;
Victor Stinner91106cd2017-12-13 12:29:09 +0100690}
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100691
Victor Stinner3d4226a2018-08-29 22:21:32 +0200692
693/* Encode a string to the locale encoding.
694
695 Parameters:
696
697 * raw_malloc: if non-zero, allocate memory using PyMem_RawMalloc() instead
698 of PyMem_Malloc().
699 * current_locale: if non-zero, use the current LC_CTYPE, otherwise use
700 Python filesystem encoding.
701 * errors: error handler like "strict" or "surrogateescape".
702
703 Return value:
704
705 0: success, *str is set to a newly allocated decoded string.
706 -1: memory allocation failure
707 -2: encoding error, set *error_pos and *reason (if set).
708 -3: the error handler 'errors' is not supported.
709 */
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100710static int
711encode_locale_ex(const wchar_t *text, char **str, size_t *error_pos,
712 const char **reason,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200713 int raw_malloc, int current_locale, _Py_error_handler errors)
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100714{
715 if (current_locale) {
Victor Stinner9089a262018-01-22 19:07:32 +0100716#ifdef __ANDROID__
717 return _Py_EncodeUTF8Ex(text, str, error_pos, reason,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200718 raw_malloc, errors);
Victor Stinner9089a262018-01-22 19:07:32 +0100719#else
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100720 return encode_current_locale(text, str, error_pos, reason,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200721 raw_malloc, errors);
Victor Stinner9089a262018-01-22 19:07:32 +0100722#endif
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100723 }
724
725#if defined(__APPLE__) || defined(__ANDROID__)
726 return _Py_EncodeUTF8Ex(text, str, error_pos, reason,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200727 raw_malloc, errors);
728#else
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200729 int use_utf8 = (Py_UTF8Mode == 1);
730#ifdef MS_WINDOWS
731 use_utf8 |= !Py_LegacyWindowsFSEncodingFlag;
732#endif
733 if (use_utf8) {
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100734 return _Py_EncodeUTF8Ex(text, str, error_pos, reason,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200735 raw_malloc, errors);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100736 }
737
738#ifdef USE_FORCE_ASCII
739 if (force_ascii == -1) {
740 force_ascii = check_force_ascii();
741 }
742
743 if (force_ascii) {
744 return encode_ascii(text, str, error_pos, reason,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200745 raw_malloc, errors);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100746 }
Victor Stinnerd2b02312017-12-15 23:06:17 +0100747#endif
Victor Stinner91106cd2017-12-13 12:29:09 +0100748
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100749 return encode_current_locale(text, str, error_pos, reason,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200750 raw_malloc, errors);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100751#endif /* __APPLE__ or __ANDROID__ */
752}
753
Victor Stinner9dd76202017-12-21 16:20:32 +0100754static char*
Victor Stinner2cba6b82018-01-10 22:46:15 +0100755encode_locale(const wchar_t *text, size_t *error_pos,
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100756 int raw_malloc, int current_locale)
Victor Stinner9dd76202017-12-21 16:20:32 +0100757{
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100758 char *str;
759 int res = encode_locale_ex(text, &str, error_pos, NULL,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200760 raw_malloc, current_locale,
761 _Py_ERROR_SURROGATEESCAPE);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100762 if (res != -2 && error_pos) {
763 *error_pos = (size_t)-1;
Victor Stinner9dd76202017-12-21 16:20:32 +0100764 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100765 if (res != 0) {
766 return NULL;
767 }
768 return str;
Victor Stinner9dd76202017-12-21 16:20:32 +0100769}
770
Victor Stinner91106cd2017-12-13 12:29:09 +0100771/* Encode a wide character string to the locale encoding with the
772 surrogateescape error handler: surrogate characters in the range
773 U+DC80..U+DCFF are converted to bytes 0x80..0xFF.
774
775 Return a pointer to a newly allocated byte string, use PyMem_Free() to free
776 the memory. Return NULL on encoding or memory allocation error.
777
778 If error_pos is not NULL, *error_pos is set to (size_t)-1 on success, or set
779 to the index of the invalid character on encoding error.
780
781 Use the Py_DecodeLocale() function to decode the bytes string back to a wide
782 character string. */
783char*
784Py_EncodeLocale(const wchar_t *text, size_t *error_pos)
785{
Victor Stinner2cba6b82018-01-10 22:46:15 +0100786 return encode_locale(text, error_pos, 0, 0);
Victor Stinner9dd76202017-12-21 16:20:32 +0100787}
Victor Stinner91106cd2017-12-13 12:29:09 +0100788
Victor Stinner91106cd2017-12-13 12:29:09 +0100789
Victor Stinner9dd76202017-12-21 16:20:32 +0100790/* Similar to Py_EncodeLocale(), but result must be freed by PyMem_RawFree()
791 instead of PyMem_Free(). */
792char*
793_Py_EncodeLocaleRaw(const wchar_t *text, size_t *error_pos)
794{
Victor Stinner2cba6b82018-01-10 22:46:15 +0100795 return encode_locale(text, error_pos, 1, 0);
796}
797
798
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100799int
800_Py_EncodeLocaleEx(const wchar_t *text, char **str,
801 size_t *error_pos, const char **reason,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200802 int current_locale, _Py_error_handler errors)
Victor Stinner2cba6b82018-01-10 22:46:15 +0100803{
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100804 return encode_locale_ex(text, str, error_pos, reason, 1,
Victor Stinner3d4226a2018-08-29 22:21:32 +0200805 current_locale, errors);
Victor Stinner4e314432010-10-07 21:45:39 +0000806}
807
Victor Stinner6672d0c2010-10-07 22:53:43 +0000808
Steve Dowerf2f373f2015-02-21 08:44:05 -0800809#ifdef MS_WINDOWS
810static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
811
812static void
813FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
814{
815 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
816 /* Cannot simply cast and dereference in_ptr,
817 since it might not be aligned properly */
818 __int64 in;
819 memcpy(&in, in_ptr, sizeof(in));
820 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
821 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
822}
823
824void
Steve Dowerbf1f3762015-02-21 15:26:02 -0800825_Py_time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800826{
827 /* XXX endianness */
828 __int64 out;
829 out = time_in + secs_between_epochs;
830 out = out * 10000000 + nsec_in / 100;
831 memcpy(out_ptr, &out, sizeof(out));
832}
833
834/* Below, we *know* that ugo+r is 0444 */
835#if _S_IREAD != 0400
836#error Unsupported C library
837#endif
838static int
839attributes_to_mode(DWORD attr)
840{
841 int m = 0;
842 if (attr & FILE_ATTRIBUTE_DIRECTORY)
843 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
844 else
845 m |= _S_IFREG;
846 if (attr & FILE_ATTRIBUTE_READONLY)
847 m |= 0444;
848 else
849 m |= 0666;
850 return m;
851}
852
Steve Dowerbf1f3762015-02-21 15:26:02 -0800853void
Victor Stinnere134a7f2015-03-30 10:09:31 +0200854_Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag,
855 struct _Py_stat_struct *result)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800856{
857 memset(result, 0, sizeof(*result));
858 result->st_mode = attributes_to_mode(info->dwFileAttributes);
859 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
860 result->st_dev = info->dwVolumeSerialNumber;
861 result->st_rdev = result->st_dev;
862 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
863 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
864 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
865 result->st_nlink = info->nNumberOfLinks;
Victor Stinner0f6d7332017-03-09 17:34:28 +0100866 result->st_ino = (((uint64_t)info->nFileIndexHigh) << 32) + info->nFileIndexLow;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800867 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
868 /* first clear the S_IFMT bits */
869 result->st_mode ^= (result->st_mode & S_IFMT);
870 /* now set the bits that make this a symlink */
871 result->st_mode |= S_IFLNK;
872 }
873 result->st_file_attributes = info->dwFileAttributes;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800874}
875#endif
876
877/* Return information about a file.
878
879 On POSIX, use fstat().
880
881 On Windows, use GetFileType() and GetFileInformationByHandle() which support
Victor Stinner8c663fd2017-11-08 14:44:44 -0800882 files larger than 2 GiB. fstat() may fail with EOVERFLOW on files larger
883 than 2 GiB because the file size type is a signed 32-bit integer: see issue
Steve Dowerf2f373f2015-02-21 08:44:05 -0800884 #23152.
Victor Stinnere134a7f2015-03-30 10:09:31 +0200885
886 On Windows, set the last Windows error and return nonzero on error. On
887 POSIX, set errno and return nonzero on error. Fill status and return 0 on
888 success. */
Steve Dowerf2f373f2015-02-21 08:44:05 -0800889int
Victor Stinnere134a7f2015-03-30 10:09:31 +0200890_Py_fstat_noraise(int fd, struct _Py_stat_struct *status)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800891{
892#ifdef MS_WINDOWS
893 BY_HANDLE_FILE_INFORMATION info;
894 HANDLE h;
895 int type;
896
Steve Dower940f33a2016-09-08 11:21:54 -0700897 _Py_BEGIN_SUPPRESS_IPH
898 h = (HANDLE)_get_osfhandle(fd);
899 _Py_END_SUPPRESS_IPH
Steve Dowerf2f373f2015-02-21 08:44:05 -0800900
901 if (h == INVALID_HANDLE_VALUE) {
Steve Dower8fc89802015-04-12 00:26:27 -0400902 /* errno is already set by _get_osfhandle, but we also set
903 the Win32 error for callers who expect that */
Steve Dower8acde7d2015-03-07 18:14:07 -0800904 SetLastError(ERROR_INVALID_HANDLE);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800905 return -1;
906 }
Victor Stinnere134a7f2015-03-30 10:09:31 +0200907 memset(status, 0, sizeof(*status));
Steve Dowerf2f373f2015-02-21 08:44:05 -0800908
909 type = GetFileType(h);
910 if (type == FILE_TYPE_UNKNOWN) {
911 DWORD error = GetLastError();
Steve Dower8fc89802015-04-12 00:26:27 -0400912 if (error != 0) {
913 errno = winerror_to_errno(error);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800914 return -1;
Steve Dower8fc89802015-04-12 00:26:27 -0400915 }
Steve Dowerf2f373f2015-02-21 08:44:05 -0800916 /* else: valid but unknown file */
917 }
918
919 if (type != FILE_TYPE_DISK) {
920 if (type == FILE_TYPE_CHAR)
Victor Stinnere134a7f2015-03-30 10:09:31 +0200921 status->st_mode = _S_IFCHR;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800922 else if (type == FILE_TYPE_PIPE)
Victor Stinnere134a7f2015-03-30 10:09:31 +0200923 status->st_mode = _S_IFIFO;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800924 return 0;
925 }
926
927 if (!GetFileInformationByHandle(h, &info)) {
Steve Dower8fc89802015-04-12 00:26:27 -0400928 /* The Win32 error is already set, but we also set errno for
929 callers who expect it */
930 errno = winerror_to_errno(GetLastError());
Steve Dowerf2f373f2015-02-21 08:44:05 -0800931 return -1;
932 }
933
Victor Stinnere134a7f2015-03-30 10:09:31 +0200934 _Py_attribute_data_to_stat(&info, 0, status);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800935 /* specific to fstat() */
Victor Stinner0f6d7332017-03-09 17:34:28 +0100936 status->st_ino = (((uint64_t)info.nFileIndexHigh) << 32) + info.nFileIndexLow;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800937 return 0;
938#else
Victor Stinnere134a7f2015-03-30 10:09:31 +0200939 return fstat(fd, status);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800940#endif
941}
Steve Dowerf2f373f2015-02-21 08:44:05 -0800942
Victor Stinnere134a7f2015-03-30 10:09:31 +0200943/* Return information about a file.
944
945 On POSIX, use fstat().
946
947 On Windows, use GetFileType() and GetFileInformationByHandle() which support
Victor Stinner8c663fd2017-11-08 14:44:44 -0800948 files larger than 2 GiB. fstat() may fail with EOVERFLOW on files larger
949 than 2 GiB because the file size type is a signed 32-bit integer: see issue
Victor Stinnere134a7f2015-03-30 10:09:31 +0200950 #23152.
951
952 Raise an exception and return -1 on error. On Windows, set the last Windows
953 error on error. On POSIX, set errno on error. Fill status and return 0 on
954 success.
955
Victor Stinner6f4fae82015-04-01 18:34:32 +0200956 Release the GIL to call GetFileType() and GetFileInformationByHandle(), or
957 to call fstat(). The caller must hold the GIL. */
Victor Stinnere134a7f2015-03-30 10:09:31 +0200958int
959_Py_fstat(int fd, struct _Py_stat_struct *status)
960{
961 int res;
962
Victor Stinner8a1be612016-03-14 22:07:55 +0100963 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +0100964
Victor Stinnere134a7f2015-03-30 10:09:31 +0200965 Py_BEGIN_ALLOW_THREADS
966 res = _Py_fstat_noraise(fd, status);
967 Py_END_ALLOW_THREADS
968
969 if (res != 0) {
970#ifdef MS_WINDOWS
971 PyErr_SetFromWindowsErr(0);
972#else
973 PyErr_SetFromErrno(PyExc_OSError);
974#endif
975 return -1;
976 }
977 return 0;
978}
Steve Dowerf2f373f2015-02-21 08:44:05 -0800979
Victor Stinner6672d0c2010-10-07 22:53:43 +0000980/* Call _wstat() on Windows, or encode the path to the filesystem encoding and
981 call stat() otherwise. Only fill st_mode attribute on Windows.
982
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100983 Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception was
984 raised. */
Victor Stinner4e314432010-10-07 21:45:39 +0000985
986int
Victor Stinnera4a75952010-10-07 22:23:10 +0000987_Py_stat(PyObject *path, struct stat *statbuf)
Victor Stinner4e314432010-10-07 21:45:39 +0000988{
989#ifdef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +0000990 int err;
991 struct _stat wstatbuf;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300992 const wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +0000993
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300994 wpath = _PyUnicode_AsUnicode(path);
Victor Stinneree587ea2011-11-17 00:51:38 +0100995 if (wpath == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100996 return -2;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300997
Victor Stinneree587ea2011-11-17 00:51:38 +0100998 err = _wstat(wpath, &wstatbuf);
Victor Stinner4e314432010-10-07 21:45:39 +0000999 if (!err)
1000 statbuf->st_mode = wstatbuf.st_mode;
1001 return err;
1002#else
1003 int ret;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03001004 PyObject *bytes;
1005 char *cpath;
1006
1007 bytes = PyUnicode_EncodeFSDefault(path);
Victor Stinner4e314432010-10-07 21:45:39 +00001008 if (bytes == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +01001009 return -2;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03001010
1011 /* check for embedded null bytes */
1012 if (PyBytes_AsStringAndSize(bytes, &cpath, NULL) == -1) {
1013 Py_DECREF(bytes);
1014 return -2;
1015 }
1016
1017 ret = stat(cpath, statbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001018 Py_DECREF(bytes);
1019 return ret;
1020#endif
1021}
1022
Victor Stinnerd45c7f82012-12-04 01:34:47 +01001023
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +03001024/* This function MUST be kept async-signal-safe on POSIX when raise=0. */
Antoine Pitrou409b5382013-10-12 22:41:17 +02001025static int
Victor Stinnerdaf45552013-08-28 00:53:59 +02001026get_inheritable(int fd, int raise)
1027{
1028#ifdef MS_WINDOWS
1029 HANDLE handle;
1030 DWORD flags;
Victor Stinner6672d0c2010-10-07 22:53:43 +00001031
Steve Dower8fc89802015-04-12 00:26:27 -04001032 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001033 handle = (HANDLE)_get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001034 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001035 if (handle == INVALID_HANDLE_VALUE) {
1036 if (raise)
Steve Dower41e72442015-03-14 11:38:27 -07001037 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001038 return -1;
1039 }
1040
1041 if (!GetHandleInformation(handle, &flags)) {
1042 if (raise)
1043 PyErr_SetFromWindowsErr(0);
1044 return -1;
1045 }
1046
1047 return (flags & HANDLE_FLAG_INHERIT);
1048#else
1049 int flags;
1050
1051 flags = fcntl(fd, F_GETFD, 0);
1052 if (flags == -1) {
1053 if (raise)
1054 PyErr_SetFromErrno(PyExc_OSError);
1055 return -1;
1056 }
1057 return !(flags & FD_CLOEXEC);
1058#endif
1059}
1060
1061/* Get the inheritable flag of the specified file descriptor.
Victor Stinnerb034eee2013-09-07 10:36:04 +02001062 Return 1 if the file descriptor can be inherited, 0 if it cannot,
Victor Stinnerdaf45552013-08-28 00:53:59 +02001063 raise an exception and return -1 on error. */
1064int
1065_Py_get_inheritable(int fd)
1066{
1067 return get_inheritable(fd, 1);
1068}
1069
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +03001070
1071/* This function MUST be kept async-signal-safe on POSIX when raise=0. */
Victor Stinnerdaf45552013-08-28 00:53:59 +02001072static int
1073set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
1074{
1075#ifdef MS_WINDOWS
1076 HANDLE handle;
1077 DWORD flags;
Victor Stinner282124b2014-09-02 11:41:04 +02001078#else
1079#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
1080 static int ioctl_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02001081 int request;
1082 int err;
Victor Stinner282124b2014-09-02 11:41:04 +02001083#endif
Victor Stinnera858bbd2016-04-17 16:51:52 +02001084 int flags, new_flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +02001085 int res;
1086#endif
1087
1088 /* atomic_flag_works can only be used to make the file descriptor
1089 non-inheritable */
1090 assert(!(atomic_flag_works != NULL && inheritable));
1091
1092 if (atomic_flag_works != NULL && !inheritable) {
1093 if (*atomic_flag_works == -1) {
Steve Dower41e72442015-03-14 11:38:27 -07001094 int isInheritable = get_inheritable(fd, raise);
1095 if (isInheritable == -1)
Victor Stinnerdaf45552013-08-28 00:53:59 +02001096 return -1;
Steve Dower41e72442015-03-14 11:38:27 -07001097 *atomic_flag_works = !isInheritable;
Victor Stinnerdaf45552013-08-28 00:53:59 +02001098 }
1099
1100 if (*atomic_flag_works)
1101 return 0;
1102 }
1103
1104#ifdef MS_WINDOWS
Steve Dower8fc89802015-04-12 00:26:27 -04001105 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001106 handle = (HANDLE)_get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001107 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001108 if (handle == INVALID_HANDLE_VALUE) {
1109 if (raise)
Steve Dower41e72442015-03-14 11:38:27 -07001110 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001111 return -1;
1112 }
1113
1114 if (inheritable)
1115 flags = HANDLE_FLAG_INHERIT;
1116 else
1117 flags = 0;
1118 if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) {
1119 if (raise)
1120 PyErr_SetFromWindowsErr(0);
1121 return -1;
1122 }
1123 return 0;
1124
Victor Stinnerdaf45552013-08-28 00:53:59 +02001125#else
Victor Stinner282124b2014-09-02 11:41:04 +02001126
1127#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +03001128 if (ioctl_works != 0 && raise != 0) {
Victor Stinner282124b2014-09-02 11:41:04 +02001129 /* fast-path: ioctl() only requires one syscall */
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +03001130 /* caveat: raise=0 is an indicator that we must be async-signal-safe
1131 * thus avoid using ioctl() so we skip the fast-path. */
Victor Stinner282124b2014-09-02 11:41:04 +02001132 if (inheritable)
1133 request = FIONCLEX;
1134 else
1135 request = FIOCLEX;
1136 err = ioctl(fd, request, NULL);
1137 if (!err) {
1138 ioctl_works = 1;
1139 return 0;
1140 }
1141
Victor Stinner3116cc42016-05-19 16:46:18 +02001142 if (errno != ENOTTY && errno != EACCES) {
Victor Stinner282124b2014-09-02 11:41:04 +02001143 if (raise)
1144 PyErr_SetFromErrno(PyExc_OSError);
1145 return -1;
1146 }
1147 else {
1148 /* Issue #22258: Here, ENOTTY means "Inappropriate ioctl for
1149 device". The ioctl is declared but not supported by the kernel.
1150 Remember that ioctl() doesn't work. It is the case on
Victor Stinner3116cc42016-05-19 16:46:18 +02001151 Illumos-based OS for example.
1152
1153 Issue #27057: When SELinux policy disallows ioctl it will fail
1154 with EACCES. While FIOCLEX is safe operation it may be
1155 unavailable because ioctl was denied altogether.
1156 This can be the case on Android. */
Victor Stinner282124b2014-09-02 11:41:04 +02001157 ioctl_works = 0;
1158 }
1159 /* fallback to fcntl() if ioctl() does not work */
1160 }
1161#endif
1162
1163 /* slow-path: fcntl() requires two syscalls */
Victor Stinnerdaf45552013-08-28 00:53:59 +02001164 flags = fcntl(fd, F_GETFD);
1165 if (flags < 0) {
1166 if (raise)
1167 PyErr_SetFromErrno(PyExc_OSError);
1168 return -1;
1169 }
1170
Victor Stinnera858bbd2016-04-17 16:51:52 +02001171 if (inheritable) {
1172 new_flags = flags & ~FD_CLOEXEC;
1173 }
1174 else {
1175 new_flags = flags | FD_CLOEXEC;
1176 }
1177
1178 if (new_flags == flags) {
1179 /* FD_CLOEXEC flag already set/cleared: nothing to do */
1180 return 0;
1181 }
1182
Xavier de Gayeec5d3cd2016-11-19 16:19:29 +01001183 res = fcntl(fd, F_SETFD, new_flags);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001184 if (res < 0) {
1185 if (raise)
1186 PyErr_SetFromErrno(PyExc_OSError);
1187 return -1;
1188 }
1189 return 0;
1190#endif
1191}
1192
1193/* Make the file descriptor non-inheritable.
Victor Stinnerb034eee2013-09-07 10:36:04 +02001194 Return 0 on success, set errno and return -1 on error. */
Victor Stinnerdaf45552013-08-28 00:53:59 +02001195static int
1196make_non_inheritable(int fd)
1197{
1198 return set_inheritable(fd, 0, 0, NULL);
1199}
1200
1201/* Set the inheritable flag of the specified file descriptor.
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +03001202 On success: return 0, on error: raise an exception and return -1.
Victor Stinnerdaf45552013-08-28 00:53:59 +02001203
1204 If atomic_flag_works is not NULL:
1205
1206 * if *atomic_flag_works==-1, check if the inheritable is set on the file
1207 descriptor: if yes, set *atomic_flag_works to 1, otherwise set to 0 and
1208 set the inheritable flag
1209 * if *atomic_flag_works==1: do nothing
1210 * if *atomic_flag_works==0: set inheritable flag to False
1211
1212 Set atomic_flag_works to NULL if no atomic flag was used to create the
1213 file descriptor.
1214
1215 atomic_flag_works can only be used to make a file descriptor
1216 non-inheritable: atomic_flag_works must be NULL if inheritable=1. */
1217int
1218_Py_set_inheritable(int fd, int inheritable, int *atomic_flag_works)
1219{
1220 return set_inheritable(fd, inheritable, 1, atomic_flag_works);
1221}
1222
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +03001223/* Same as _Py_set_inheritable() but on error, set errno and
1224 don't raise an exception.
1225 This function is async-signal-safe. */
1226int
1227_Py_set_inheritable_async_safe(int fd, int inheritable, int *atomic_flag_works)
1228{
1229 return set_inheritable(fd, inheritable, 0, atomic_flag_works);
1230}
1231
Victor Stinnera555cfc2015-03-18 00:22:14 +01001232static int
1233_Py_open_impl(const char *pathname, int flags, int gil_held)
Victor Stinnerdaf45552013-08-28 00:53:59 +02001234{
1235 int fd;
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001236 int async_err = 0;
Victor Stinnera555cfc2015-03-18 00:22:14 +01001237#ifndef MS_WINDOWS
Victor Stinnerdaf45552013-08-28 00:53:59 +02001238 int *atomic_flag_works;
Victor Stinnera555cfc2015-03-18 00:22:14 +01001239#endif
1240
1241#ifdef MS_WINDOWS
1242 flags |= O_NOINHERIT;
1243#elif defined(O_CLOEXEC)
Victor Stinnerdaf45552013-08-28 00:53:59 +02001244 atomic_flag_works = &_Py_open_cloexec_works;
1245 flags |= O_CLOEXEC;
1246#else
1247 atomic_flag_works = NULL;
1248#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +02001249
Victor Stinnera555cfc2015-03-18 00:22:14 +01001250 if (gil_held) {
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001251 do {
1252 Py_BEGIN_ALLOW_THREADS
1253 fd = open(pathname, flags);
1254 Py_END_ALLOW_THREADS
1255 } while (fd < 0
1256 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1257 if (async_err)
1258 return -1;
Victor Stinnera555cfc2015-03-18 00:22:14 +01001259 if (fd < 0) {
1260 PyErr_SetFromErrnoWithFilename(PyExc_OSError, pathname);
1261 return -1;
1262 }
1263 }
1264 else {
1265 fd = open(pathname, flags);
1266 if (fd < 0)
1267 return -1;
1268 }
1269
1270#ifndef MS_WINDOWS
1271 if (set_inheritable(fd, 0, gil_held, atomic_flag_works) < 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +02001272 close(fd);
1273 return -1;
1274 }
Victor Stinnera555cfc2015-03-18 00:22:14 +01001275#endif
1276
Victor Stinnerdaf45552013-08-28 00:53:59 +02001277 return fd;
1278}
1279
Victor Stinnera555cfc2015-03-18 00:22:14 +01001280/* Open a file with the specified flags (wrapper to open() function).
1281 Return a file descriptor on success. Raise an exception and return -1 on
1282 error.
1283
1284 The file descriptor is created non-inheritable.
1285
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001286 When interrupted by a signal (open() fails with EINTR), retry the syscall,
1287 except if the Python signal handler raises an exception.
1288
Victor Stinner6f4fae82015-04-01 18:34:32 +02001289 Release the GIL to call open(). The caller must hold the GIL. */
Victor Stinnera555cfc2015-03-18 00:22:14 +01001290int
1291_Py_open(const char *pathname, int flags)
1292{
1293 /* _Py_open() must be called with the GIL held. */
1294 assert(PyGILState_Check());
1295 return _Py_open_impl(pathname, flags, 1);
1296}
1297
1298/* Open a file with the specified flags (wrapper to open() function).
1299 Return a file descriptor on success. Set errno and return -1 on error.
1300
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001301 The file descriptor is created non-inheritable.
1302
1303 If interrupted by a signal, fail with EINTR. */
Victor Stinnera555cfc2015-03-18 00:22:14 +01001304int
1305_Py_open_noraise(const char *pathname, int flags)
1306{
1307 return _Py_open_impl(pathname, flags, 0);
1308}
1309
Victor Stinnerdaf45552013-08-28 00:53:59 +02001310/* Open a file. Use _wfopen() on Windows, encode the path to the locale
Victor Stinnere42ccd22015-03-18 01:39:23 +01001311 encoding and use fopen() otherwise.
1312
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001313 The file descriptor is created non-inheritable.
1314
1315 If interrupted by a signal, fail with EINTR. */
Victor Stinner4e314432010-10-07 21:45:39 +00001316FILE *
1317_Py_wfopen(const wchar_t *path, const wchar_t *mode)
1318{
Victor Stinner4e314432010-10-07 21:45:39 +00001319 FILE *f;
Victor Stinnerdaf45552013-08-28 00:53:59 +02001320#ifndef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +00001321 char *cpath;
1322 char cmode[10];
1323 size_t r;
1324 r = wcstombs(cmode, mode, 10);
1325 if (r == (size_t)-1 || r >= 10) {
1326 errno = EINVAL;
1327 return NULL;
1328 }
Victor Stinner9dd76202017-12-21 16:20:32 +01001329 cpath = _Py_EncodeLocaleRaw(path, NULL);
1330 if (cpath == NULL) {
Victor Stinner4e314432010-10-07 21:45:39 +00001331 return NULL;
Victor Stinner9dd76202017-12-21 16:20:32 +01001332 }
Victor Stinner4e314432010-10-07 21:45:39 +00001333 f = fopen(cpath, cmode);
Victor Stinner9dd76202017-12-21 16:20:32 +01001334 PyMem_RawFree(cpath);
Victor Stinner4e314432010-10-07 21:45:39 +00001335#else
Victor Stinnerdaf45552013-08-28 00:53:59 +02001336 f = _wfopen(path, mode);
Victor Stinner4e314432010-10-07 21:45:39 +00001337#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +02001338 if (f == NULL)
1339 return NULL;
1340 if (make_non_inheritable(fileno(f)) < 0) {
1341 fclose(f);
1342 return NULL;
1343 }
1344 return f;
Victor Stinner4e314432010-10-07 21:45:39 +00001345}
1346
Victor Stinnere42ccd22015-03-18 01:39:23 +01001347/* Wrapper to fopen().
1348
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001349 The file descriptor is created non-inheritable.
1350
1351 If interrupted by a signal, fail with EINTR. */
Victor Stinnerdaf45552013-08-28 00:53:59 +02001352FILE*
1353_Py_fopen(const char *pathname, const char *mode)
1354{
1355 FILE *f = fopen(pathname, mode);
1356 if (f == NULL)
1357 return NULL;
1358 if (make_non_inheritable(fileno(f)) < 0) {
1359 fclose(f);
1360 return NULL;
1361 }
1362 return f;
1363}
1364
1365/* Open a file. Call _wfopen() on Windows, or encode the path to the filesystem
Victor Stinnere42ccd22015-03-18 01:39:23 +01001366 encoding and call fopen() otherwise.
Victor Stinner6672d0c2010-10-07 22:53:43 +00001367
Victor Stinnere42ccd22015-03-18 01:39:23 +01001368 Return the new file object on success. Raise an exception and return NULL
1369 on error.
1370
1371 The file descriptor is created non-inheritable.
1372
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001373 When interrupted by a signal (open() fails with EINTR), retry the syscall,
1374 except if the Python signal handler raises an exception.
1375
Victor Stinner6f4fae82015-04-01 18:34:32 +02001376 Release the GIL to call _wfopen() or fopen(). The caller must hold
1377 the GIL. */
Victor Stinner4e314432010-10-07 21:45:39 +00001378FILE*
Victor Stinnerdaf45552013-08-28 00:53:59 +02001379_Py_fopen_obj(PyObject *path, const char *mode)
Victor Stinner4e314432010-10-07 21:45:39 +00001380{
Victor Stinnerdaf45552013-08-28 00:53:59 +02001381 FILE *f;
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001382 int async_err = 0;
Victor Stinner4e314432010-10-07 21:45:39 +00001383#ifdef MS_WINDOWS
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03001384 const wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +00001385 wchar_t wmode[10];
1386 int usize;
Victor Stinner4e314432010-10-07 21:45:39 +00001387
Victor Stinnere42ccd22015-03-18 01:39:23 +01001388 assert(PyGILState_Check());
1389
Antoine Pitrou0e576f12011-12-22 10:03:38 +01001390 if (!PyUnicode_Check(path)) {
1391 PyErr_Format(PyExc_TypeError,
1392 "str file path expected under Windows, got %R",
1393 Py_TYPE(path));
1394 return NULL;
1395 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03001396 wpath = _PyUnicode_AsUnicode(path);
Victor Stinneree587ea2011-11-17 00:51:38 +01001397 if (wpath == NULL)
1398 return NULL;
1399
Alexey Izbyshevb3b4a9d2018-02-18 20:57:24 +03001400 usize = MultiByteToWideChar(CP_ACP, 0, mode, -1,
1401 wmode, Py_ARRAY_LENGTH(wmode));
Victor Stinnere42ccd22015-03-18 01:39:23 +01001402 if (usize == 0) {
1403 PyErr_SetFromWindowsErr(0);
Victor Stinner4e314432010-10-07 21:45:39 +00001404 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001405 }
Victor Stinner4e314432010-10-07 21:45:39 +00001406
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001407 do {
1408 Py_BEGIN_ALLOW_THREADS
1409 f = _wfopen(wpath, wmode);
1410 Py_END_ALLOW_THREADS
1411 } while (f == NULL
1412 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner4e314432010-10-07 21:45:39 +00001413#else
Antoine Pitrou2b1cc892011-12-19 18:19:06 +01001414 PyObject *bytes;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001415 char *path_bytes;
1416
1417 assert(PyGILState_Check());
1418
Antoine Pitrou2b1cc892011-12-19 18:19:06 +01001419 if (!PyUnicode_FSConverter(path, &bytes))
Victor Stinner4e314432010-10-07 21:45:39 +00001420 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001421 path_bytes = PyBytes_AS_STRING(bytes);
1422
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001423 do {
1424 Py_BEGIN_ALLOW_THREADS
1425 f = fopen(path_bytes, mode);
1426 Py_END_ALLOW_THREADS
1427 } while (f == NULL
1428 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnere42ccd22015-03-18 01:39:23 +01001429
Victor Stinner4e314432010-10-07 21:45:39 +00001430 Py_DECREF(bytes);
Victor Stinner4e314432010-10-07 21:45:39 +00001431#endif
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001432 if (async_err)
1433 return NULL;
1434
Victor Stinnere42ccd22015-03-18 01:39:23 +01001435 if (f == NULL) {
1436 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001437 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001438 }
1439
1440 if (set_inheritable(fileno(f), 0, 1, NULL) < 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +02001441 fclose(f);
1442 return NULL;
1443 }
1444 return f;
Victor Stinner4e314432010-10-07 21:45:39 +00001445}
1446
Victor Stinner66aab0c2015-03-19 22:53:20 +01001447/* Read count bytes from fd into buf.
Victor Stinner82c3e452015-04-01 18:34:45 +02001448
1449 On success, return the number of read bytes, it can be lower than count.
1450 If the current file offset is at or past the end of file, no bytes are read,
1451 and read() returns zero.
1452
1453 On error, raise an exception, set errno and return -1.
1454
1455 When interrupted by a signal (read() fails with EINTR), retry the syscall.
1456 If the Python signal handler raises an exception, the function returns -1
1457 (the syscall is not retried).
1458
1459 Release the GIL to call read(). The caller must hold the GIL. */
Victor Stinner66aab0c2015-03-19 22:53:20 +01001460Py_ssize_t
1461_Py_read(int fd, void *buf, size_t count)
1462{
1463 Py_ssize_t n;
Victor Stinnera3c02022015-03-20 11:58:18 +01001464 int err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001465 int async_err = 0;
1466
Victor Stinner8a1be612016-03-14 22:07:55 +01001467 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +01001468
Victor Stinner66aab0c2015-03-19 22:53:20 +01001469 /* _Py_read() must not be called with an exception set, otherwise the
1470 * caller may think that read() was interrupted by a signal and the signal
1471 * handler raised an exception. */
1472 assert(!PyErr_Occurred());
1473
Victor Stinner66aab0c2015-03-19 22:53:20 +01001474#ifdef MS_WINDOWS
1475 if (count > INT_MAX) {
1476 /* On Windows, the count parameter of read() is an int */
1477 count = INT_MAX;
1478 }
1479#else
1480 if (count > PY_SSIZE_T_MAX) {
1481 /* if count is greater than PY_SSIZE_T_MAX,
1482 * read() result is undefined */
1483 count = PY_SSIZE_T_MAX;
1484 }
1485#endif
1486
Steve Dower8fc89802015-04-12 00:26:27 -04001487 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001488 do {
1489 Py_BEGIN_ALLOW_THREADS
1490 errno = 0;
1491#ifdef MS_WINDOWS
1492 n = read(fd, buf, (int)count);
1493#else
1494 n = read(fd, buf, count);
1495#endif
Victor Stinnera3c02022015-03-20 11:58:18 +01001496 /* save/restore errno because PyErr_CheckSignals()
1497 * and PyErr_SetFromErrno() can modify it */
1498 err = errno;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001499 Py_END_ALLOW_THREADS
Victor Stinnera3c02022015-03-20 11:58:18 +01001500 } while (n < 0 && err == EINTR &&
Victor Stinner66aab0c2015-03-19 22:53:20 +01001501 !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04001502 _Py_END_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001503
1504 if (async_err) {
1505 /* read() was interrupted by a signal (failed with EINTR)
1506 * and the Python signal handler raised an exception */
Victor Stinnera3c02022015-03-20 11:58:18 +01001507 errno = err;
1508 assert(errno == EINTR && PyErr_Occurred());
Victor Stinner66aab0c2015-03-19 22:53:20 +01001509 return -1;
1510 }
1511 if (n < 0) {
Victor Stinner66aab0c2015-03-19 22:53:20 +01001512 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera3c02022015-03-20 11:58:18 +01001513 errno = err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001514 return -1;
1515 }
1516
1517 return n;
1518}
1519
Victor Stinner82c3e452015-04-01 18:34:45 +02001520static Py_ssize_t
1521_Py_write_impl(int fd, const void *buf, size_t count, int gil_held)
Victor Stinner66aab0c2015-03-19 22:53:20 +01001522{
1523 Py_ssize_t n;
Victor Stinnera3c02022015-03-20 11:58:18 +01001524 int err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001525 int async_err = 0;
1526
Steve Dower8fc89802015-04-12 00:26:27 -04001527 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001528#ifdef MS_WINDOWS
1529 if (count > 32767 && isatty(fd)) {
1530 /* Issue #11395: the Windows console returns an error (12: not
1531 enough space error) on writing into stdout if stdout mode is
1532 binary and the length is greater than 66,000 bytes (or less,
1533 depending on heap usage). */
1534 count = 32767;
1535 }
1536 else if (count > INT_MAX)
1537 count = INT_MAX;
1538#else
1539 if (count > PY_SSIZE_T_MAX) {
1540 /* write() should truncate count to PY_SSIZE_T_MAX, but it's safer
1541 * to do it ourself to have a portable behaviour. */
1542 count = PY_SSIZE_T_MAX;
1543 }
1544#endif
1545
Victor Stinner82c3e452015-04-01 18:34:45 +02001546 if (gil_held) {
1547 do {
1548 Py_BEGIN_ALLOW_THREADS
1549 errno = 0;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001550#ifdef MS_WINDOWS
Victor Stinner82c3e452015-04-01 18:34:45 +02001551 n = write(fd, buf, (int)count);
Victor Stinner66aab0c2015-03-19 22:53:20 +01001552#else
Victor Stinner82c3e452015-04-01 18:34:45 +02001553 n = write(fd, buf, count);
Victor Stinner66aab0c2015-03-19 22:53:20 +01001554#endif
Victor Stinner82c3e452015-04-01 18:34:45 +02001555 /* save/restore errno because PyErr_CheckSignals()
1556 * and PyErr_SetFromErrno() can modify it */
1557 err = errno;
1558 Py_END_ALLOW_THREADS
1559 } while (n < 0 && err == EINTR &&
1560 !(async_err = PyErr_CheckSignals()));
1561 }
1562 else {
1563 do {
1564 errno = 0;
1565#ifdef MS_WINDOWS
1566 n = write(fd, buf, (int)count);
1567#else
1568 n = write(fd, buf, count);
1569#endif
1570 err = errno;
1571 } while (n < 0 && err == EINTR);
1572 }
Steve Dower8fc89802015-04-12 00:26:27 -04001573 _Py_END_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001574
1575 if (async_err) {
1576 /* write() was interrupted by a signal (failed with EINTR)
Victor Stinner82c3e452015-04-01 18:34:45 +02001577 and the Python signal handler raised an exception (if gil_held is
1578 nonzero). */
Victor Stinnera3c02022015-03-20 11:58:18 +01001579 errno = err;
Victor Stinner82c3e452015-04-01 18:34:45 +02001580 assert(errno == EINTR && (!gil_held || PyErr_Occurred()));
Victor Stinner66aab0c2015-03-19 22:53:20 +01001581 return -1;
1582 }
1583 if (n < 0) {
Victor Stinner82c3e452015-04-01 18:34:45 +02001584 if (gil_held)
1585 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera3c02022015-03-20 11:58:18 +01001586 errno = err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001587 return -1;
1588 }
1589
1590 return n;
1591}
1592
Victor Stinner82c3e452015-04-01 18:34:45 +02001593/* Write count bytes of buf into fd.
1594
1595 On success, return the number of written bytes, it can be lower than count
1596 including 0. On error, raise an exception, set errno and return -1.
1597
1598 When interrupted by a signal (write() fails with EINTR), retry the syscall.
1599 If the Python signal handler raises an exception, the function returns -1
1600 (the syscall is not retried).
1601
1602 Release the GIL to call write(). The caller must hold the GIL. */
1603Py_ssize_t
1604_Py_write(int fd, const void *buf, size_t count)
1605{
Victor Stinner8a1be612016-03-14 22:07:55 +01001606 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +01001607
Victor Stinner82c3e452015-04-01 18:34:45 +02001608 /* _Py_write() must not be called with an exception set, otherwise the
1609 * caller may think that write() was interrupted by a signal and the signal
1610 * handler raised an exception. */
1611 assert(!PyErr_Occurred());
1612
1613 return _Py_write_impl(fd, buf, count, 1);
1614}
1615
1616/* Write count bytes of buf into fd.
1617 *
1618 * On success, return the number of written bytes, it can be lower than count
1619 * including 0. On error, set errno and return -1.
1620 *
1621 * When interrupted by a signal (write() fails with EINTR), retry the syscall
1622 * without calling the Python signal handler. */
1623Py_ssize_t
1624_Py_write_noraise(int fd, const void *buf, size_t count)
1625{
1626 return _Py_write_impl(fd, buf, count, 0);
1627}
1628
Victor Stinner4e314432010-10-07 21:45:39 +00001629#ifdef HAVE_READLINK
Victor Stinner6672d0c2010-10-07 22:53:43 +00001630
1631/* Read value of symbolic link. Encode the path to the locale encoding, decode
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001632 the result from the locale encoding. Return -1 on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001633
Victor Stinner4e314432010-10-07 21:45:39 +00001634int
1635_Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
1636{
1637 char *cpath;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001638 char cbuf[MAXPATHLEN];
Victor Stinner3f711f42010-10-16 22:47:37 +00001639 wchar_t *wbuf;
Victor Stinner4e314432010-10-07 21:45:39 +00001640 int res;
1641 size_t r1;
1642
Victor Stinner9dd76202017-12-21 16:20:32 +01001643 cpath = _Py_EncodeLocaleRaw(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001644 if (cpath == NULL) {
1645 errno = EINVAL;
1646 return -1;
1647 }
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001648 res = (int)readlink(cpath, cbuf, Py_ARRAY_LENGTH(cbuf));
Victor Stinner9dd76202017-12-21 16:20:32 +01001649 PyMem_RawFree(cpath);
Victor Stinner4e314432010-10-07 21:45:39 +00001650 if (res == -1)
1651 return -1;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001652 if (res == Py_ARRAY_LENGTH(cbuf)) {
Victor Stinner4e314432010-10-07 21:45:39 +00001653 errno = EINVAL;
1654 return -1;
1655 }
1656 cbuf[res] = '\0'; /* buf will be null terminated */
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001657 wbuf = Py_DecodeLocale(cbuf, &r1);
Victor Stinner350147b2010-10-16 22:52:09 +00001658 if (wbuf == NULL) {
1659 errno = EINVAL;
1660 return -1;
1661 }
Victor Stinner3f711f42010-10-16 22:47:37 +00001662 if (bufsiz <= r1) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001663 PyMem_RawFree(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001664 errno = EINVAL;
1665 return -1;
1666 }
Victor Stinner3f711f42010-10-16 22:47:37 +00001667 wcsncpy(buf, wbuf, bufsiz);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001668 PyMem_RawFree(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001669 return (int)r1;
1670}
1671#endif
1672
1673#ifdef HAVE_REALPATH
Victor Stinner6672d0c2010-10-07 22:53:43 +00001674
1675/* Return the canonicalized absolute pathname. Encode path to the locale
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001676 encoding, decode the result from the locale encoding.
1677 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001678
Victor Stinner4e314432010-10-07 21:45:39 +00001679wchar_t*
Victor Stinner015f4d82010-10-07 22:29:53 +00001680_Py_wrealpath(const wchar_t *path,
1681 wchar_t *resolved_path, size_t resolved_path_size)
Victor Stinner4e314432010-10-07 21:45:39 +00001682{
1683 char *cpath;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001684 char cresolved_path[MAXPATHLEN];
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001685 wchar_t *wresolved_path;
Victor Stinner4e314432010-10-07 21:45:39 +00001686 char *res;
1687 size_t r;
Victor Stinner9dd76202017-12-21 16:20:32 +01001688 cpath = _Py_EncodeLocaleRaw(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001689 if (cpath == NULL) {
1690 errno = EINVAL;
1691 return NULL;
1692 }
1693 res = realpath(cpath, cresolved_path);
Victor Stinner9dd76202017-12-21 16:20:32 +01001694 PyMem_RawFree(cpath);
Victor Stinner4e314432010-10-07 21:45:39 +00001695 if (res == NULL)
1696 return NULL;
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001697
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001698 wresolved_path = Py_DecodeLocale(cresolved_path, &r);
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001699 if (wresolved_path == NULL) {
Victor Stinner4e314432010-10-07 21:45:39 +00001700 errno = EINVAL;
1701 return NULL;
1702 }
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001703 if (resolved_path_size <= r) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001704 PyMem_RawFree(wresolved_path);
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001705 errno = EINVAL;
1706 return NULL;
1707 }
1708 wcsncpy(resolved_path, wresolved_path, resolved_path_size);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001709 PyMem_RawFree(wresolved_path);
Victor Stinner4e314432010-10-07 21:45:39 +00001710 return resolved_path;
1711}
1712#endif
1713
Victor Stinnerf4061da2010-10-14 12:37:19 +00001714/* Get the current directory. size is the buffer size in wide characters
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001715 including the null character. Decode the path from the locale encoding.
1716 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001717
Victor Stinner4e314432010-10-07 21:45:39 +00001718wchar_t*
1719_Py_wgetcwd(wchar_t *buf, size_t size)
1720{
1721#ifdef MS_WINDOWS
Victor Stinner56785ea2013-06-05 00:46:29 +02001722 int isize = (int)Py_MIN(size, INT_MAX);
1723 return _wgetcwd(buf, isize);
Victor Stinner4e314432010-10-07 21:45:39 +00001724#else
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001725 char fname[MAXPATHLEN];
Victor Stinnerf4061da2010-10-14 12:37:19 +00001726 wchar_t *wname;
Victor Stinner168e1172010-10-16 23:16:16 +00001727 size_t len;
Victor Stinnerf4061da2010-10-14 12:37:19 +00001728
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001729 if (getcwd(fname, Py_ARRAY_LENGTH(fname)) == NULL)
Victor Stinner4e314432010-10-07 21:45:39 +00001730 return NULL;
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001731 wname = Py_DecodeLocale(fname, &len);
Victor Stinnerf4061da2010-10-14 12:37:19 +00001732 if (wname == NULL)
1733 return NULL;
Victor Stinner168e1172010-10-16 23:16:16 +00001734 if (size <= len) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001735 PyMem_RawFree(wname);
Victor Stinner4e314432010-10-07 21:45:39 +00001736 return NULL;
1737 }
Victor Stinnerf4061da2010-10-14 12:37:19 +00001738 wcsncpy(buf, wname, size);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001739 PyMem_RawFree(wname);
Victor Stinner4e314432010-10-07 21:45:39 +00001740 return buf;
1741#endif
1742}
1743
Victor Stinnerdaf45552013-08-28 00:53:59 +02001744/* Duplicate a file descriptor. The new file descriptor is created as
1745 non-inheritable. Return a new file descriptor on success, raise an OSError
1746 exception and return -1 on error.
1747
1748 The GIL is released to call dup(). The caller must hold the GIL. */
1749int
1750_Py_dup(int fd)
1751{
1752#ifdef MS_WINDOWS
1753 HANDLE handle;
1754 DWORD ftype;
1755#endif
1756
Victor Stinner8a1be612016-03-14 22:07:55 +01001757 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +01001758
Victor Stinnerdaf45552013-08-28 00:53:59 +02001759#ifdef MS_WINDOWS
Steve Dower8fc89802015-04-12 00:26:27 -04001760 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001761 handle = (HANDLE)_get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001762 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001763 if (handle == INVALID_HANDLE_VALUE) {
Steve Dower41e72442015-03-14 11:38:27 -07001764 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001765 return -1;
1766 }
1767
1768 /* get the file type, ignore the error if it failed */
1769 ftype = GetFileType(handle);
1770
1771 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001772 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001773 fd = dup(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001774 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001775 Py_END_ALLOW_THREADS
1776 if (fd < 0) {
1777 PyErr_SetFromErrno(PyExc_OSError);
1778 return -1;
1779 }
1780
1781 /* Character files like console cannot be make non-inheritable */
1782 if (ftype != FILE_TYPE_CHAR) {
1783 if (_Py_set_inheritable(fd, 0, NULL) < 0) {
Steve Dower8fc89802015-04-12 00:26:27 -04001784 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001785 close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001786 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001787 return -1;
1788 }
1789 }
1790#elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC)
1791 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001792 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001793 fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Steve Dower8fc89802015-04-12 00:26:27 -04001794 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001795 Py_END_ALLOW_THREADS
1796 if (fd < 0) {
1797 PyErr_SetFromErrno(PyExc_OSError);
1798 return -1;
1799 }
1800
1801#else
1802 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001803 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001804 fd = dup(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001805 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001806 Py_END_ALLOW_THREADS
1807 if (fd < 0) {
1808 PyErr_SetFromErrno(PyExc_OSError);
1809 return -1;
1810 }
1811
1812 if (_Py_set_inheritable(fd, 0, NULL) < 0) {
Steve Dower8fc89802015-04-12 00:26:27 -04001813 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001814 close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001815 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001816 return -1;
1817 }
1818#endif
1819 return fd;
1820}
1821
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001822#ifndef MS_WINDOWS
1823/* Get the blocking mode of the file descriptor.
1824 Return 0 if the O_NONBLOCK flag is set, 1 if the flag is cleared,
1825 raise an exception and return -1 on error. */
1826int
1827_Py_get_blocking(int fd)
1828{
Steve Dower8fc89802015-04-12 00:26:27 -04001829 int flags;
1830 _Py_BEGIN_SUPPRESS_IPH
1831 flags = fcntl(fd, F_GETFL, 0);
1832 _Py_END_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001833 if (flags < 0) {
1834 PyErr_SetFromErrno(PyExc_OSError);
1835 return -1;
1836 }
1837
1838 return !(flags & O_NONBLOCK);
1839}
1840
1841/* Set the blocking mode of the specified file descriptor.
1842
1843 Set the O_NONBLOCK flag if blocking is False, clear the O_NONBLOCK flag
1844 otherwise.
1845
1846 Return 0 on success, raise an exception and return -1 on error. */
1847int
1848_Py_set_blocking(int fd, int blocking)
1849{
1850#if defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)
1851 int arg = !blocking;
1852 if (ioctl(fd, FIONBIO, &arg) < 0)
1853 goto error;
1854#else
1855 int flags, res;
1856
Steve Dower8fc89802015-04-12 00:26:27 -04001857 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001858 flags = fcntl(fd, F_GETFL, 0);
Steve Dower8fc89802015-04-12 00:26:27 -04001859 if (flags >= 0) {
1860 if (blocking)
1861 flags = flags & (~O_NONBLOCK);
1862 else
1863 flags = flags | O_NONBLOCK;
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001864
Steve Dower8fc89802015-04-12 00:26:27 -04001865 res = fcntl(fd, F_SETFL, flags);
1866 } else {
1867 res = -1;
1868 }
1869 _Py_END_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001870
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001871 if (res < 0)
1872 goto error;
1873#endif
1874 return 0;
1875
1876error:
1877 PyErr_SetFromErrno(PyExc_OSError);
1878 return -1;
1879}
1880#endif
Victor Stinnercb064fc2018-01-15 15:58:02 +01001881
1882
1883int
1884_Py_GetLocaleconvNumeric(PyObject **decimal_point, PyObject **thousands_sep,
1885 const char **grouping)
1886{
1887 int res = -1;
1888
1889 struct lconv *lc = localeconv();
1890
1891 int change_locale = 0;
1892 if (decimal_point != NULL &&
1893 (strlen(lc->decimal_point) > 1 || ((unsigned char)lc->decimal_point[0]) > 127))
1894 {
1895 change_locale = 1;
1896 }
1897 if (thousands_sep != NULL &&
1898 (strlen(lc->thousands_sep) > 1 || ((unsigned char)lc->thousands_sep[0]) > 127))
1899 {
1900 change_locale = 1;
1901 }
1902
1903 /* Keep a copy of the LC_CTYPE locale */
1904 char *oldloc = NULL, *loc = NULL;
1905 if (change_locale) {
1906 oldloc = setlocale(LC_CTYPE, NULL);
1907 if (!oldloc) {
1908 PyErr_SetString(PyExc_RuntimeWarning, "faild to get LC_CTYPE locale");
1909 return -1;
1910 }
1911
1912 oldloc = _PyMem_Strdup(oldloc);
1913 if (!oldloc) {
1914 PyErr_NoMemory();
1915 return -1;
1916 }
1917
1918 loc = setlocale(LC_NUMERIC, NULL);
1919 if (loc != NULL && strcmp(loc, oldloc) == 0) {
1920 loc = NULL;
1921 }
1922
1923 if (loc != NULL) {
1924 /* Only set the locale temporarilty the LC_CTYPE locale
1925 if LC_NUMERIC locale is different than LC_CTYPE locale and
1926 decimal_point and/or thousands_sep are non-ASCII or longer than
1927 1 byte */
1928 setlocale(LC_CTYPE, loc);
1929 }
1930 }
1931
1932 if (decimal_point != NULL) {
1933 *decimal_point = PyUnicode_DecodeLocale(lc->decimal_point, NULL);
1934 if (*decimal_point == NULL) {
1935 goto error;
1936 }
1937 }
1938 if (thousands_sep != NULL) {
1939 *thousands_sep = PyUnicode_DecodeLocale(lc->thousands_sep, NULL);
1940 if (*thousands_sep == NULL) {
1941 goto error;
1942 }
1943 }
1944
1945 if (grouping != NULL) {
1946 *grouping = lc->grouping;
1947 }
1948
1949 res = 0;
1950
1951error:
1952 if (loc != NULL) {
1953 setlocale(LC_CTYPE, oldloc);
1954 }
1955 PyMem_Free(oldloc);
1956 return res;
1957}