blob: e756c260cdcc4cdf6f360be461c0fd391fb5bd9c [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
Brett Cannonefb00c02012-02-29 18:31:31 -050035PyObject *
36_Py_device_encoding(int fd)
37{
Victor Stinner14b9b112013-06-25 00:37:25 +020038#if defined(MS_WINDOWS)
Brett Cannonefb00c02012-02-29 18:31:31 -050039 UINT cp;
40#endif
Steve Dower8fc89802015-04-12 00:26:27 -040041 int valid;
42 _Py_BEGIN_SUPPRESS_IPH
Steve Dower940f33a2016-09-08 11:21:54 -070043 valid = isatty(fd);
Steve Dower8fc89802015-04-12 00:26:27 -040044 _Py_END_SUPPRESS_IPH
45 if (!valid)
Brett Cannonefb00c02012-02-29 18:31:31 -050046 Py_RETURN_NONE;
Steve Dower8fc89802015-04-12 00:26:27 -040047
Victor Stinner14b9b112013-06-25 00:37:25 +020048#if defined(MS_WINDOWS)
Brett Cannonefb00c02012-02-29 18:31:31 -050049 if (fd == 0)
50 cp = GetConsoleCP();
51 else if (fd == 1 || fd == 2)
52 cp = GetConsoleOutputCP();
53 else
54 cp = 0;
55 /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
56 has no console */
57 if (cp != 0)
58 return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
59#elif defined(CODESET)
60 {
61 char *codeset = nl_langinfo(CODESET);
62 if (codeset != NULL && codeset[0] != 0)
63 return PyUnicode_FromString(codeset);
64 }
65#endif
66 Py_RETURN_NONE;
67}
68
Victor Stinner7ed7aea2018-01-15 10:45:49 +010069#if !defined(__APPLE__) && !defined(__ANDROID__) && !defined(MS_WINDOWS)
70
71#define USE_FORCE_ASCII
72
Victor Stinnerd45c7f82012-12-04 01:34:47 +010073extern int _Py_normalize_encoding(const char *, char *, size_t);
74
Victor Stinnerd500e532018-08-28 17:27:36 +020075/* Workaround FreeBSD and OpenIndiana locale encoding issue with the C locale
76 and POSIX locale. nl_langinfo(CODESET) announces an alias of the
Victor Stinnerd45c7f82012-12-04 01:34:47 +010077 ASCII encoding, whereas mbstowcs() and wcstombs() functions use the
78 ISO-8859-1 encoding. The problem is that os.fsencode() and os.fsdecode() use
79 locale.getpreferredencoding() codec. For example, if command line arguments
80 are decoded by mbstowcs() and encoded back by os.fsencode(), we get a
81 UnicodeEncodeError instead of retrieving the original byte string.
82
83 The workaround is enabled if setlocale(LC_CTYPE, NULL) returns "C",
84 nl_langinfo(CODESET) announces "ascii" (or an alias to ASCII), and at least
85 one byte in range 0x80-0xff can be decoded from the locale encoding. The
86 workaround is also enabled on error, for example if getting the locale
87 failed.
88
Victor Stinnerd500e532018-08-28 17:27:36 +020089 On HP-UX with the C locale or the POSIX locale, nl_langinfo(CODESET)
90 announces "roman8" but mbstowcs() uses Latin1 in practice. Force also the
91 ASCII encoding in this case.
92
Philip Jenvey215c49a2013-01-15 13:24:12 -080093 Values of force_ascii:
Victor Stinnerd45c7f82012-12-04 01:34:47 +010094
Victor Stinnerf6a271a2014-08-01 12:28:48 +020095 1: the workaround is used: Py_EncodeLocale() uses
96 encode_ascii_surrogateescape() and Py_DecodeLocale() uses
Victor Stinner7ed7aea2018-01-15 10:45:49 +010097 decode_ascii()
Victor Stinnerf6a271a2014-08-01 12:28:48 +020098 0: the workaround is not used: Py_EncodeLocale() uses wcstombs() and
99 Py_DecodeLocale() uses mbstowcs()
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100100 -1: unknown, need to call check_force_ascii() to get the value
101*/
102static int force_ascii = -1;
103
104static int
105check_force_ascii(void)
106{
Victor Stinnerd500e532018-08-28 17:27:36 +0200107 char *loc = setlocale(LC_CTYPE, NULL);
108 if (loc == NULL) {
109 goto error;
110 }
111 if (strcmp(loc, "C") != 0 && strcmp(loc, "POSIX") != 0) {
112 /* the LC_CTYPE locale is different than C and POSIX */
113 return 0;
114 }
115
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100116#if defined(HAVE_LANGINFO_H) && defined(CODESET)
Victor Stinnerd500e532018-08-28 17:27:36 +0200117 const char *codeset = nl_langinfo(CODESET);
118 if (!codeset || codeset[0] == '\0') {
119 /* CODESET is not set or empty */
120 goto error;
121 }
122
Victor Stinner54de2b12016-09-09 23:11:52 -0700123 char encoding[20]; /* longest name: "iso_646.irv_1991\0" */
Victor Stinnerd500e532018-08-28 17:27:36 +0200124 if (!_Py_normalize_encoding(codeset, encoding, sizeof(encoding))) {
125 goto error;
126 }
127
128#ifdef __hpux
129 if (strcmp(encoding, "roman8") == 0) {
130 unsigned char ch;
131 wchar_t wch;
132 size_t res;
133
134 ch = (unsigned char)0xA7;
135 res = mbstowcs(&wch, (char*)&ch, 1);
136 if (res != (size_t)-1 && wch == L'\xA7') {
137 /* On HP-UX withe C locale or the POSIX locale,
138 nl_langinfo(CODESET) announces "roman8", whereas mbstowcs() uses
139 Latin1 encoding in practice. Force ASCII in this case.
140
141 Roman8 decodes 0xA7 to U+00CF. Latin1 decodes 0xA7 to U+00A7. */
142 return 1;
143 }
144 }
145#else
146 const char* ascii_aliases[] = {
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100147 "ascii",
Victor Stinner54de2b12016-09-09 23:11:52 -0700148 /* Aliases from Lib/encodings/aliases.py */
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100149 "646",
Victor Stinner54de2b12016-09-09 23:11:52 -0700150 "ansi_x3.4_1968",
151 "ansi_x3.4_1986",
152 "ansi_x3_4_1968",
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100153 "cp367",
154 "csascii",
155 "ibm367",
Victor Stinner54de2b12016-09-09 23:11:52 -0700156 "iso646_us",
157 "iso_646.irv_1991",
158 "iso_ir_6",
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100159 "us",
Victor Stinner54de2b12016-09-09 23:11:52 -0700160 "us_ascii",
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100161 NULL
162 };
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100163
Victor Stinnerd500e532018-08-28 17:27:36 +0200164 int is_ascii = 0;
165 for (const char **alias=ascii_aliases; *alias != NULL; alias++) {
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100166 if (strcmp(encoding, *alias) == 0) {
167 is_ascii = 1;
168 break;
169 }
170 }
171 if (!is_ascii) {
172 /* nl_langinfo(CODESET) is not "ascii" or an alias of ASCII */
173 return 0;
174 }
175
Victor Stinnerd500e532018-08-28 17:27:36 +0200176 for (unsigned int i=0x80; i<=0xff; i++) {
177 char ch[1];
178 wchar_t wch[1];
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100179 size_t res;
180
Victor Stinnerd500e532018-08-28 17:27:36 +0200181 unsigned uch = (unsigned char)i;
182 ch[0] = (char)uch;
183 res = mbstowcs(wch, ch, 1);
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100184 if (res != (size_t)-1) {
185 /* decoding a non-ASCII character from the locale encoding succeed:
186 the locale encoding is not ASCII, force ASCII */
187 return 1;
188 }
189 }
190 /* None of the bytes in the range 0x80-0xff can be decoded from the locale
191 encoding: the locale encoding is really ASCII */
Victor Stinnerd500e532018-08-28 17:27:36 +0200192#endif /* !defined(__hpux) */
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100193 return 0;
194#else
195 /* nl_langinfo(CODESET) is not available: always force ASCII */
196 return 1;
Victor Stinnerd500e532018-08-28 17:27:36 +0200197#endif /* defined(HAVE_LANGINFO_H) && defined(CODESET) */
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100198
199error:
Martin Panter46f50722016-05-26 05:35:26 +0000200 /* if an error occurred, force the ASCII encoding */
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100201 return 1;
202}
203
Victor Stinnerd500e532018-08-28 17:27:36 +0200204
205int
206_Py_GetForceASCII(void)
207{
208 if (force_ascii == -1) {
209 force_ascii = check_force_ascii();
210 }
211 return force_ascii;
212}
213
214
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100215static int
216encode_ascii(const wchar_t *text, char **str,
217 size_t *error_pos, const char **reason,
218 int raw_malloc, int surrogateescape)
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100219{
220 char *result = NULL, *out;
221 size_t len, i;
222 wchar_t ch;
223
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100224 len = wcslen(text);
225
Victor Stinner9bee3292017-12-21 16:49:13 +0100226 /* +1 for NULL byte */
Victor Stinner9dd76202017-12-21 16:20:32 +0100227 if (raw_malloc) {
228 result = PyMem_RawMalloc(len + 1);
229 }
230 else {
231 result = PyMem_Malloc(len + 1);
232 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100233 if (result == NULL) {
234 return -1;
235 }
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100236
237 out = result;
238 for (i=0; i<len; i++) {
239 ch = text[i];
240
241 if (ch <= 0x7f) {
242 /* ASCII character */
243 *out++ = (char)ch;
244 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100245 else if (surrogateescape && 0xdc80 <= ch && ch <= 0xdcff) {
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100246 /* UTF-8b surrogate */
247 *out++ = (char)(ch - 0xdc00);
248 }
249 else {
Victor Stinner9dd76202017-12-21 16:20:32 +0100250 if (raw_malloc) {
251 PyMem_RawFree(result);
252 }
253 else {
254 PyMem_Free(result);
255 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100256 if (error_pos != NULL) {
257 *error_pos = i;
258 }
259 if (reason) {
260 *reason = "encoding error";
261 }
262 return -2;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100263 }
264 }
265 *out = '\0';
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100266 *str = result;
267 return 0;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100268}
Victor Stinnerd500e532018-08-28 17:27:36 +0200269#else
270int
271_Py_GetForceASCII(void)
272{
273 return 0;
274}
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100275#endif /* !defined(__APPLE__) && !defined(__ANDROID__) && !defined(MS_WINDOWS) */
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100276
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100277
278#if !defined(HAVE_MBRTOWC) || defined(USE_FORCE_ASCII)
279static int
280decode_ascii(const char *arg, wchar_t **wstr, size_t *wlen,
281 const char **reason, int surrogateescape)
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100282{
283 wchar_t *res;
284 unsigned char *in;
285 wchar_t *out;
Benjamin Petersonf18bf6f2015-01-04 16:03:17 -0600286 size_t argsize = strlen(arg) + 1;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100287
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100288 if (argsize > PY_SSIZE_T_MAX / sizeof(wchar_t)) {
289 return -1;
290 }
291 res = PyMem_RawMalloc(argsize * sizeof(wchar_t));
292 if (!res) {
293 return -1;
294 }
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100295
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100296 out = res;
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100297 for (in = (unsigned char*)arg; *in; in++) {
298 unsigned char ch = *in;
299 if (ch < 128) {
300 *out++ = ch;
301 }
302 else {
303 if (!surrogateescape) {
304 PyMem_RawFree(res);
305 if (wlen) {
306 *wlen = in - (unsigned char*)arg;
307 }
308 if (reason) {
309 *reason = "decoding error";
310 }
311 return -2;
312 }
313 *out++ = 0xdc00 + ch;
314 }
315 }
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100316 *out = 0;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100317
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100318 if (wlen != NULL) {
319 *wlen = out - res;
320 }
321 *wstr = res;
322 return 0;
323}
324#endif /* !HAVE_MBRTOWC */
325
326static int
327decode_current_locale(const char* arg, wchar_t **wstr, size_t *wlen,
328 const char **reason, int surrogateescape)
Victor Stinner4e314432010-10-07 21:45:39 +0000329{
330 wchar_t *res;
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100331 size_t argsize;
Victor Stinner4e314432010-10-07 21:45:39 +0000332 size_t count;
Victor Stinner313f10c2013-05-07 23:48:56 +0200333#ifdef HAVE_MBRTOWC
Victor Stinner4e314432010-10-07 21:45:39 +0000334 unsigned char *in;
335 wchar_t *out;
Victor Stinner4e314432010-10-07 21:45:39 +0000336 mbstate_t mbs;
337#endif
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100338
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100339#ifdef HAVE_BROKEN_MBSTOWCS
340 /* Some platforms have a broken implementation of
341 * mbstowcs which does not count the characters that
342 * would result from conversion. Use an upper bound.
343 */
344 argsize = strlen(arg);
345#else
346 argsize = mbstowcs(NULL, arg, 0);
347#endif
Victor Stinner4e314432010-10-07 21:45:39 +0000348 if (argsize != (size_t)-1) {
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100349 if (argsize > PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) {
350 return -1;
351 }
352 res = (wchar_t *)PyMem_RawMalloc((argsize + 1) * sizeof(wchar_t));
353 if (!res) {
354 return -1;
355 }
356
357 count = mbstowcs(res, arg, argsize + 1);
Victor Stinner4e314432010-10-07 21:45:39 +0000358 if (count != (size_t)-1) {
359 wchar_t *tmp;
360 /* Only use the result if it contains no
361 surrogate characters. */
362 for (tmp = res; *tmp != 0 &&
Victor Stinner76df43d2012-10-30 01:42:39 +0100363 !Py_UNICODE_IS_SURROGATE(*tmp); tmp++)
Victor Stinner4e314432010-10-07 21:45:39 +0000364 ;
Victor Stinner168e1172010-10-16 23:16:16 +0000365 if (*tmp == 0) {
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100366 if (wlen != NULL) {
367 *wlen = count;
368 }
369 *wstr = res;
370 return 0;
Victor Stinner168e1172010-10-16 23:16:16 +0000371 }
Victor Stinner4e314432010-10-07 21:45:39 +0000372 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200373 PyMem_RawFree(res);
Victor Stinner4e314432010-10-07 21:45:39 +0000374 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100375
Victor Stinner4e314432010-10-07 21:45:39 +0000376 /* Conversion failed. Fall back to escaping with surrogateescape. */
377#ifdef HAVE_MBRTOWC
378 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
379
380 /* Overallocate; as multi-byte characters are in the argument, the
381 actual output could use less memory. */
382 argsize = strlen(arg) + 1;
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100383 if (argsize > PY_SSIZE_T_MAX / sizeof(wchar_t)) {
384 return -1;
385 }
386 res = (wchar_t*)PyMem_RawMalloc(argsize * sizeof(wchar_t));
387 if (!res) {
388 return -1;
389 }
390
Victor Stinner4e314432010-10-07 21:45:39 +0000391 in = (unsigned char*)arg;
392 out = res;
393 memset(&mbs, 0, sizeof mbs);
394 while (argsize) {
395 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100396 if (converted == 0) {
Victor Stinner4e314432010-10-07 21:45:39 +0000397 /* Reached end of string; null char stored. */
398 break;
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100399 }
400
Victor Stinner4e314432010-10-07 21:45:39 +0000401 if (converted == (size_t)-2) {
402 /* Incomplete character. This should never happen,
403 since we provide everything that we have -
404 unless there is a bug in the C library, or I
405 misunderstood how mbrtowc works. */
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100406 goto decode_error;
Victor Stinner4e314432010-10-07 21:45:39 +0000407 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100408
Victor Stinner4e314432010-10-07 21:45:39 +0000409 if (converted == (size_t)-1) {
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100410 if (!surrogateescape) {
411 goto decode_error;
412 }
413
Victor Stinner4e314432010-10-07 21:45:39 +0000414 /* Conversion error. Escape as UTF-8b, and start over
415 in the initial shift state. */
416 *out++ = 0xdc00 + *in++;
417 argsize--;
418 memset(&mbs, 0, sizeof mbs);
419 continue;
420 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100421
Victor Stinner76df43d2012-10-30 01:42:39 +0100422 if (Py_UNICODE_IS_SURROGATE(*out)) {
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100423 if (!surrogateescape) {
424 goto decode_error;
425 }
426
Victor Stinner4e314432010-10-07 21:45:39 +0000427 /* Surrogate character. Escape the original
428 byte sequence with surrogateescape. */
429 argsize -= converted;
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100430 while (converted--) {
Victor Stinner4e314432010-10-07 21:45:39 +0000431 *out++ = 0xdc00 + *in++;
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100432 }
Victor Stinner4e314432010-10-07 21:45:39 +0000433 continue;
434 }
435 /* successfully converted some bytes */
436 in += converted;
437 argsize -= converted;
438 out++;
439 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100440 if (wlen != NULL) {
441 *wlen = out - res;
442 }
443 *wstr = res;
444 return 0;
445
446decode_error:
447 PyMem_RawFree(res);
448 if (wlen) {
449 *wlen = in - (unsigned char*)arg;
450 }
451 if (reason) {
452 *reason = "decoding error";
453 }
454 return -2;
Victor Stinnere2623772012-11-12 23:04:02 +0100455#else /* HAVE_MBRTOWC */
Victor Stinner4e314432010-10-07 21:45:39 +0000456 /* Cannot use C locale for escaping; manually escape as if charset
457 is ASCII (i.e. escape all bytes > 128. This will still roundtrip
458 correctly in the locale's charset, which must be an ASCII superset. */
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100459 return decode_ascii(arg, wstr, wlen, reason, surrogateescape);
Victor Stinnere2623772012-11-12 23:04:02 +0100460#endif /* HAVE_MBRTOWC */
Victor Stinner91106cd2017-12-13 12:29:09 +0100461}
462
463
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100464/* Decode a byte string from the locale encoding.
465
466 Use the strict error handler if 'surrogateescape' is zero. Use the
467 surrogateescape error handler if 'surrogateescape' is non-zero: undecodable
468 bytes are decoded as characters in range U+DC80..U+DCFF. If a byte sequence
469 can be decoded as a surrogate character, escape the bytes using the
470 surrogateescape error handler instead of decoding them.
471
Ville Skyttä61f82e02018-04-20 23:08:45 +0300472 On success, return 0 and write the newly allocated wide character string into
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100473 *wstr (use PyMem_RawFree() to free the memory). If wlen is not NULL, write
474 the number of wide characters excluding the null character into *wlen.
475
476 On memory allocation failure, return -1.
477
478 On decoding error, return -2. If wlen is not NULL, write the start of
479 invalid byte sequence in the input string into *wlen. If reason is not NULL,
480 write the decoding error message into *reason.
481
482 Use the Py_EncodeLocaleEx() function to encode the character string back to
483 a byte string. */
484int
485_Py_DecodeLocaleEx(const char* arg, wchar_t **wstr, size_t *wlen,
486 const char **reason,
487 int current_locale, int surrogateescape)
Victor Stinner2cba6b82018-01-10 22:46:15 +0100488{
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100489 if (current_locale) {
Victor Stinner9089a262018-01-22 19:07:32 +0100490#ifdef __ANDROID__
491 return _Py_DecodeUTF8Ex(arg, strlen(arg), wstr, wlen, reason,
492 surrogateescape);
493#else
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100494 return decode_current_locale(arg, wstr, wlen, reason, surrogateescape);
Victor Stinner9089a262018-01-22 19:07:32 +0100495#endif
Victor Stinner2cba6b82018-01-10 22:46:15 +0100496 }
497
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100498#if defined(__APPLE__) || defined(__ANDROID__)
499 return _Py_DecodeUTF8Ex(arg, strlen(arg), wstr, wlen, reason,
500 surrogateescape);
501#else
502 if (Py_UTF8Mode == 1) {
503 return _Py_DecodeUTF8Ex(arg, strlen(arg), wstr, wlen, reason,
504 surrogateescape);
505 }
506
507#ifdef USE_FORCE_ASCII
508 if (force_ascii == -1) {
Victor Stinner2cba6b82018-01-10 22:46:15 +0100509 force_ascii = check_force_ascii();
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100510 }
Victor Stinner2cba6b82018-01-10 22:46:15 +0100511
512 if (force_ascii) {
513 /* force ASCII encoding to workaround mbstowcs() issue */
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100514 return decode_ascii(arg, wstr, wlen, reason, surrogateescape);
Victor Stinner2cba6b82018-01-10 22:46:15 +0100515 }
516#endif
517
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100518 return decode_current_locale(arg, wstr, wlen, reason, surrogateescape);
Victor Stinner2cba6b82018-01-10 22:46:15 +0100519#endif /* __APPLE__ or __ANDROID__ */
520}
521
522
Victor Stinner91106cd2017-12-13 12:29:09 +0100523/* Decode a byte string from the locale encoding with the
524 surrogateescape error handler: undecodable bytes are decoded as characters
525 in range U+DC80..U+DCFF. If a byte sequence can be decoded as a surrogate
526 character, escape the bytes using the surrogateescape error handler instead
527 of decoding them.
528
529 Return a pointer to a newly allocated wide character string, use
530 PyMem_RawFree() to free the memory. If size is not NULL, write the number of
531 wide characters excluding the null character into *size
532
533 Return NULL on decoding error or memory allocation error. If *size* is not
534 NULL, *size is set to (size_t)-1 on memory error or set to (size_t)-2 on
535 decoding error.
536
537 Decoding errors should never happen, unless there is a bug in the C
538 library.
539
540 Use the Py_EncodeLocale() function to encode the character string back to a
541 byte string. */
542wchar_t*
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100543Py_DecodeLocale(const char* arg, size_t *wlen)
Victor Stinner91106cd2017-12-13 12:29:09 +0100544{
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100545 wchar_t *wstr;
546 int res = _Py_DecodeLocaleEx(arg, &wstr, wlen, NULL, 0, 1);
547 if (res != 0) {
548 if (wlen != NULL) {
549 *wlen = (size_t)res;
550 }
551 return NULL;
552 }
553 return wstr;
Victor Stinner2cba6b82018-01-10 22:46:15 +0100554}
Victor Stinner91106cd2017-12-13 12:29:09 +0100555
Victor Stinner91106cd2017-12-13 12:29:09 +0100556
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100557static int
558encode_current_locale(const wchar_t *text, char **str,
559 size_t *error_pos, const char **reason,
560 int raw_malloc, int surrogateescape)
Victor Stinner91106cd2017-12-13 12:29:09 +0100561{
Victor Stinner4e314432010-10-07 21:45:39 +0000562 const size_t len = wcslen(text);
563 char *result = NULL, *bytes = NULL;
564 size_t i, size, converted;
565 wchar_t c, buf[2];
566
567 /* The function works in two steps:
568 1. compute the length of the output buffer in bytes (size)
569 2. outputs the bytes */
570 size = 0;
571 buf[1] = 0;
572 while (1) {
573 for (i=0; i < len; i++) {
574 c = text[i];
575 if (c >= 0xdc80 && c <= 0xdcff) {
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100576 if (!surrogateescape) {
577 goto encode_error;
578 }
Victor Stinner4e314432010-10-07 21:45:39 +0000579 /* UTF-8b surrogate */
580 if (bytes != NULL) {
581 *bytes++ = c - 0xdc00;
582 size--;
583 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100584 else {
Victor Stinner4e314432010-10-07 21:45:39 +0000585 size++;
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100586 }
Victor Stinner4e314432010-10-07 21:45:39 +0000587 continue;
588 }
589 else {
590 buf[0] = c;
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100591 if (bytes != NULL) {
Victor Stinner4e314432010-10-07 21:45:39 +0000592 converted = wcstombs(bytes, buf, size);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100593 }
594 else {
Victor Stinner4e314432010-10-07 21:45:39 +0000595 converted = wcstombs(NULL, buf, 0);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100596 }
Victor Stinner4e314432010-10-07 21:45:39 +0000597 if (converted == (size_t)-1) {
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100598 goto encode_error;
Victor Stinner4e314432010-10-07 21:45:39 +0000599 }
600 if (bytes != NULL) {
601 bytes += converted;
602 size -= converted;
603 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100604 else {
Victor Stinner4e314432010-10-07 21:45:39 +0000605 size += converted;
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100606 }
Victor Stinner4e314432010-10-07 21:45:39 +0000607 }
608 }
609 if (result != NULL) {
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100610 *bytes = '\0';
Victor Stinner4e314432010-10-07 21:45:39 +0000611 break;
612 }
613
614 size += 1; /* nul byte at the end */
Victor Stinner9dd76202017-12-21 16:20:32 +0100615 if (raw_malloc) {
616 result = PyMem_RawMalloc(size);
617 }
618 else {
619 result = PyMem_Malloc(size);
620 }
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100621 if (result == NULL) {
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100622 return -1;
Victor Stinner0d92c4f2012-11-12 23:32:21 +0100623 }
Victor Stinner4e314432010-10-07 21:45:39 +0000624 bytes = result;
625 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100626 *str = result;
627 return 0;
628
629encode_error:
630 if (raw_malloc) {
631 PyMem_RawFree(result);
632 }
633 else {
634 PyMem_Free(result);
635 }
636 if (error_pos != NULL) {
637 *error_pos = i;
638 }
639 if (reason) {
640 *reason = "encoding error";
641 }
642 return -2;
Victor Stinner91106cd2017-12-13 12:29:09 +0100643}
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100644
645static int
646encode_locale_ex(const wchar_t *text, char **str, size_t *error_pos,
647 const char **reason,
648 int raw_malloc, int current_locale, int surrogateescape)
649{
650 if (current_locale) {
Victor Stinner9089a262018-01-22 19:07:32 +0100651#ifdef __ANDROID__
652 return _Py_EncodeUTF8Ex(text, str, error_pos, reason,
653 raw_malloc, surrogateescape);
654#else
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100655 return encode_current_locale(text, str, error_pos, reason,
656 raw_malloc, surrogateescape);
Victor Stinner9089a262018-01-22 19:07:32 +0100657#endif
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100658 }
659
660#if defined(__APPLE__) || defined(__ANDROID__)
661 return _Py_EncodeUTF8Ex(text, str, error_pos, reason,
662 raw_malloc, surrogateescape);
663#else /* __APPLE__ */
664 if (Py_UTF8Mode == 1) {
665 return _Py_EncodeUTF8Ex(text, str, error_pos, reason,
666 raw_malloc, surrogateescape);
667 }
668
669#ifdef USE_FORCE_ASCII
670 if (force_ascii == -1) {
671 force_ascii = check_force_ascii();
672 }
673
674 if (force_ascii) {
675 return encode_ascii(text, str, error_pos, reason,
676 raw_malloc, surrogateescape);
677 }
Victor Stinnerd2b02312017-12-15 23:06:17 +0100678#endif
Victor Stinner91106cd2017-12-13 12:29:09 +0100679
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100680 return encode_current_locale(text, str, error_pos, reason,
681 raw_malloc, surrogateescape);
682#endif /* __APPLE__ or __ANDROID__ */
683}
684
Victor Stinner9dd76202017-12-21 16:20:32 +0100685static char*
Victor Stinner2cba6b82018-01-10 22:46:15 +0100686encode_locale(const wchar_t *text, size_t *error_pos,
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100687 int raw_malloc, int current_locale)
Victor Stinner9dd76202017-12-21 16:20:32 +0100688{
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100689 char *str;
690 int res = encode_locale_ex(text, &str, error_pos, NULL,
691 raw_malloc, current_locale, 1);
692 if (res != -2 && error_pos) {
693 *error_pos = (size_t)-1;
Victor Stinner9dd76202017-12-21 16:20:32 +0100694 }
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100695 if (res != 0) {
696 return NULL;
697 }
698 return str;
Victor Stinner9dd76202017-12-21 16:20:32 +0100699}
700
Victor Stinner91106cd2017-12-13 12:29:09 +0100701/* Encode a wide character string to the locale encoding with the
702 surrogateescape error handler: surrogate characters in the range
703 U+DC80..U+DCFF are converted to bytes 0x80..0xFF.
704
705 Return a pointer to a newly allocated byte string, use PyMem_Free() to free
706 the memory. Return NULL on encoding or memory allocation error.
707
708 If error_pos is not NULL, *error_pos is set to (size_t)-1 on success, or set
709 to the index of the invalid character on encoding error.
710
711 Use the Py_DecodeLocale() function to decode the bytes string back to a wide
712 character string. */
713char*
714Py_EncodeLocale(const wchar_t *text, size_t *error_pos)
715{
Victor Stinner2cba6b82018-01-10 22:46:15 +0100716 return encode_locale(text, error_pos, 0, 0);
Victor Stinner9dd76202017-12-21 16:20:32 +0100717}
Victor Stinner91106cd2017-12-13 12:29:09 +0100718
Victor Stinner91106cd2017-12-13 12:29:09 +0100719
Victor Stinner9dd76202017-12-21 16:20:32 +0100720/* Similar to Py_EncodeLocale(), but result must be freed by PyMem_RawFree()
721 instead of PyMem_Free(). */
722char*
723_Py_EncodeLocaleRaw(const wchar_t *text, size_t *error_pos)
724{
Victor Stinner2cba6b82018-01-10 22:46:15 +0100725 return encode_locale(text, error_pos, 1, 0);
726}
727
728
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100729int
730_Py_EncodeLocaleEx(const wchar_t *text, char **str,
731 size_t *error_pos, const char **reason,
732 int current_locale, int surrogateescape)
Victor Stinner2cba6b82018-01-10 22:46:15 +0100733{
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100734 return encode_locale_ex(text, str, error_pos, reason, 1,
735 current_locale, surrogateescape);
Victor Stinner4e314432010-10-07 21:45:39 +0000736}
737
Victor Stinner6672d0c2010-10-07 22:53:43 +0000738
Steve Dowerf2f373f2015-02-21 08:44:05 -0800739#ifdef MS_WINDOWS
740static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
741
742static void
743FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
744{
745 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
746 /* Cannot simply cast and dereference in_ptr,
747 since it might not be aligned properly */
748 __int64 in;
749 memcpy(&in, in_ptr, sizeof(in));
750 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
751 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
752}
753
754void
Steve Dowerbf1f3762015-02-21 15:26:02 -0800755_Py_time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800756{
757 /* XXX endianness */
758 __int64 out;
759 out = time_in + secs_between_epochs;
760 out = out * 10000000 + nsec_in / 100;
761 memcpy(out_ptr, &out, sizeof(out));
762}
763
764/* Below, we *know* that ugo+r is 0444 */
765#if _S_IREAD != 0400
766#error Unsupported C library
767#endif
768static int
769attributes_to_mode(DWORD attr)
770{
771 int m = 0;
772 if (attr & FILE_ATTRIBUTE_DIRECTORY)
773 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
774 else
775 m |= _S_IFREG;
776 if (attr & FILE_ATTRIBUTE_READONLY)
777 m |= 0444;
778 else
779 m |= 0666;
780 return m;
781}
782
Steve Dowerbf1f3762015-02-21 15:26:02 -0800783void
Victor Stinnere134a7f2015-03-30 10:09:31 +0200784_Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag,
785 struct _Py_stat_struct *result)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800786{
787 memset(result, 0, sizeof(*result));
788 result->st_mode = attributes_to_mode(info->dwFileAttributes);
789 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
790 result->st_dev = info->dwVolumeSerialNumber;
791 result->st_rdev = result->st_dev;
792 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
793 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
794 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
795 result->st_nlink = info->nNumberOfLinks;
Victor Stinner0f6d7332017-03-09 17:34:28 +0100796 result->st_ino = (((uint64_t)info->nFileIndexHigh) << 32) + info->nFileIndexLow;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800797 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
798 /* first clear the S_IFMT bits */
799 result->st_mode ^= (result->st_mode & S_IFMT);
800 /* now set the bits that make this a symlink */
801 result->st_mode |= S_IFLNK;
802 }
803 result->st_file_attributes = info->dwFileAttributes;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800804}
805#endif
806
807/* Return information about a file.
808
809 On POSIX, use fstat().
810
811 On Windows, use GetFileType() and GetFileInformationByHandle() which support
Victor Stinner8c663fd2017-11-08 14:44:44 -0800812 files larger than 2 GiB. fstat() may fail with EOVERFLOW on files larger
813 than 2 GiB because the file size type is a signed 32-bit integer: see issue
Steve Dowerf2f373f2015-02-21 08:44:05 -0800814 #23152.
Victor Stinnere134a7f2015-03-30 10:09:31 +0200815
816 On Windows, set the last Windows error and return nonzero on error. On
817 POSIX, set errno and return nonzero on error. Fill status and return 0 on
818 success. */
Steve Dowerf2f373f2015-02-21 08:44:05 -0800819int
Victor Stinnere134a7f2015-03-30 10:09:31 +0200820_Py_fstat_noraise(int fd, struct _Py_stat_struct *status)
Steve Dowerf2f373f2015-02-21 08:44:05 -0800821{
822#ifdef MS_WINDOWS
823 BY_HANDLE_FILE_INFORMATION info;
824 HANDLE h;
825 int type;
826
Steve Dower940f33a2016-09-08 11:21:54 -0700827 _Py_BEGIN_SUPPRESS_IPH
828 h = (HANDLE)_get_osfhandle(fd);
829 _Py_END_SUPPRESS_IPH
Steve Dowerf2f373f2015-02-21 08:44:05 -0800830
831 if (h == INVALID_HANDLE_VALUE) {
Steve Dower8fc89802015-04-12 00:26:27 -0400832 /* errno is already set by _get_osfhandle, but we also set
833 the Win32 error for callers who expect that */
Steve Dower8acde7d2015-03-07 18:14:07 -0800834 SetLastError(ERROR_INVALID_HANDLE);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800835 return -1;
836 }
Victor Stinnere134a7f2015-03-30 10:09:31 +0200837 memset(status, 0, sizeof(*status));
Steve Dowerf2f373f2015-02-21 08:44:05 -0800838
839 type = GetFileType(h);
840 if (type == FILE_TYPE_UNKNOWN) {
841 DWORD error = GetLastError();
Steve Dower8fc89802015-04-12 00:26:27 -0400842 if (error != 0) {
843 errno = winerror_to_errno(error);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800844 return -1;
Steve Dower8fc89802015-04-12 00:26:27 -0400845 }
Steve Dowerf2f373f2015-02-21 08:44:05 -0800846 /* else: valid but unknown file */
847 }
848
849 if (type != FILE_TYPE_DISK) {
850 if (type == FILE_TYPE_CHAR)
Victor Stinnere134a7f2015-03-30 10:09:31 +0200851 status->st_mode = _S_IFCHR;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800852 else if (type == FILE_TYPE_PIPE)
Victor Stinnere134a7f2015-03-30 10:09:31 +0200853 status->st_mode = _S_IFIFO;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800854 return 0;
855 }
856
857 if (!GetFileInformationByHandle(h, &info)) {
Steve Dower8fc89802015-04-12 00:26:27 -0400858 /* The Win32 error is already set, but we also set errno for
859 callers who expect it */
860 errno = winerror_to_errno(GetLastError());
Steve Dowerf2f373f2015-02-21 08:44:05 -0800861 return -1;
862 }
863
Victor Stinnere134a7f2015-03-30 10:09:31 +0200864 _Py_attribute_data_to_stat(&info, 0, status);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800865 /* specific to fstat() */
Victor Stinner0f6d7332017-03-09 17:34:28 +0100866 status->st_ino = (((uint64_t)info.nFileIndexHigh) << 32) + info.nFileIndexLow;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800867 return 0;
868#else
Victor Stinnere134a7f2015-03-30 10:09:31 +0200869 return fstat(fd, status);
Steve Dowerf2f373f2015-02-21 08:44:05 -0800870#endif
871}
Steve Dowerf2f373f2015-02-21 08:44:05 -0800872
Victor Stinnere134a7f2015-03-30 10:09:31 +0200873/* Return information about a file.
874
875 On POSIX, use fstat().
876
877 On Windows, use GetFileType() and GetFileInformationByHandle() which support
Victor Stinner8c663fd2017-11-08 14:44:44 -0800878 files larger than 2 GiB. fstat() may fail with EOVERFLOW on files larger
879 than 2 GiB because the file size type is a signed 32-bit integer: see issue
Victor Stinnere134a7f2015-03-30 10:09:31 +0200880 #23152.
881
882 Raise an exception and return -1 on error. On Windows, set the last Windows
883 error on error. On POSIX, set errno on error. Fill status and return 0 on
884 success.
885
Victor Stinner6f4fae82015-04-01 18:34:32 +0200886 Release the GIL to call GetFileType() and GetFileInformationByHandle(), or
887 to call fstat(). The caller must hold the GIL. */
Victor Stinnere134a7f2015-03-30 10:09:31 +0200888int
889_Py_fstat(int fd, struct _Py_stat_struct *status)
890{
891 int res;
892
Victor Stinner8a1be612016-03-14 22:07:55 +0100893 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +0100894
Victor Stinnere134a7f2015-03-30 10:09:31 +0200895 Py_BEGIN_ALLOW_THREADS
896 res = _Py_fstat_noraise(fd, status);
897 Py_END_ALLOW_THREADS
898
899 if (res != 0) {
900#ifdef MS_WINDOWS
901 PyErr_SetFromWindowsErr(0);
902#else
903 PyErr_SetFromErrno(PyExc_OSError);
904#endif
905 return -1;
906 }
907 return 0;
908}
Steve Dowerf2f373f2015-02-21 08:44:05 -0800909
Victor Stinner6672d0c2010-10-07 22:53:43 +0000910/* Call _wstat() on Windows, or encode the path to the filesystem encoding and
911 call stat() otherwise. Only fill st_mode attribute on Windows.
912
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100913 Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception was
914 raised. */
Victor Stinner4e314432010-10-07 21:45:39 +0000915
916int
Victor Stinnera4a75952010-10-07 22:23:10 +0000917_Py_stat(PyObject *path, struct stat *statbuf)
Victor Stinner4e314432010-10-07 21:45:39 +0000918{
919#ifdef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +0000920 int err;
921 struct _stat wstatbuf;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300922 const wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +0000923
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300924 wpath = _PyUnicode_AsUnicode(path);
Victor Stinneree587ea2011-11-17 00:51:38 +0100925 if (wpath == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100926 return -2;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300927
Victor Stinneree587ea2011-11-17 00:51:38 +0100928 err = _wstat(wpath, &wstatbuf);
Victor Stinner4e314432010-10-07 21:45:39 +0000929 if (!err)
930 statbuf->st_mode = wstatbuf.st_mode;
931 return err;
932#else
933 int ret;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300934 PyObject *bytes;
935 char *cpath;
936
937 bytes = PyUnicode_EncodeFSDefault(path);
Victor Stinner4e314432010-10-07 21:45:39 +0000938 if (bytes == NULL)
Victor Stinnerbd0850b2011-12-18 20:47:30 +0100939 return -2;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300940
941 /* check for embedded null bytes */
942 if (PyBytes_AsStringAndSize(bytes, &cpath, NULL) == -1) {
943 Py_DECREF(bytes);
944 return -2;
945 }
946
947 ret = stat(cpath, statbuf);
Victor Stinner4e314432010-10-07 21:45:39 +0000948 Py_DECREF(bytes);
949 return ret;
950#endif
951}
952
Victor Stinnerd45c7f82012-12-04 01:34:47 +0100953
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +0300954/* This function MUST be kept async-signal-safe on POSIX when raise=0. */
Antoine Pitrou409b5382013-10-12 22:41:17 +0200955static int
Victor Stinnerdaf45552013-08-28 00:53:59 +0200956get_inheritable(int fd, int raise)
957{
958#ifdef MS_WINDOWS
959 HANDLE handle;
960 DWORD flags;
Victor Stinner6672d0c2010-10-07 22:53:43 +0000961
Steve Dower8fc89802015-04-12 00:26:27 -0400962 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +0200963 handle = (HANDLE)_get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -0400964 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +0200965 if (handle == INVALID_HANDLE_VALUE) {
966 if (raise)
Steve Dower41e72442015-03-14 11:38:27 -0700967 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200968 return -1;
969 }
970
971 if (!GetHandleInformation(handle, &flags)) {
972 if (raise)
973 PyErr_SetFromWindowsErr(0);
974 return -1;
975 }
976
977 return (flags & HANDLE_FLAG_INHERIT);
978#else
979 int flags;
980
981 flags = fcntl(fd, F_GETFD, 0);
982 if (flags == -1) {
983 if (raise)
984 PyErr_SetFromErrno(PyExc_OSError);
985 return -1;
986 }
987 return !(flags & FD_CLOEXEC);
988#endif
989}
990
991/* Get the inheritable flag of the specified file descriptor.
Victor Stinnerb034eee2013-09-07 10:36:04 +0200992 Return 1 if the file descriptor can be inherited, 0 if it cannot,
Victor Stinnerdaf45552013-08-28 00:53:59 +0200993 raise an exception and return -1 on error. */
994int
995_Py_get_inheritable(int fd)
996{
997 return get_inheritable(fd, 1);
998}
999
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +03001000
1001/* This function MUST be kept async-signal-safe on POSIX when raise=0. */
Victor Stinnerdaf45552013-08-28 00:53:59 +02001002static int
1003set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
1004{
1005#ifdef MS_WINDOWS
1006 HANDLE handle;
1007 DWORD flags;
Victor Stinner282124b2014-09-02 11:41:04 +02001008#else
1009#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
1010 static int ioctl_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02001011 int request;
1012 int err;
Victor Stinner282124b2014-09-02 11:41:04 +02001013#endif
Victor Stinnera858bbd2016-04-17 16:51:52 +02001014 int flags, new_flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +02001015 int res;
1016#endif
1017
1018 /* atomic_flag_works can only be used to make the file descriptor
1019 non-inheritable */
1020 assert(!(atomic_flag_works != NULL && inheritable));
1021
1022 if (atomic_flag_works != NULL && !inheritable) {
1023 if (*atomic_flag_works == -1) {
Steve Dower41e72442015-03-14 11:38:27 -07001024 int isInheritable = get_inheritable(fd, raise);
1025 if (isInheritable == -1)
Victor Stinnerdaf45552013-08-28 00:53:59 +02001026 return -1;
Steve Dower41e72442015-03-14 11:38:27 -07001027 *atomic_flag_works = !isInheritable;
Victor Stinnerdaf45552013-08-28 00:53:59 +02001028 }
1029
1030 if (*atomic_flag_works)
1031 return 0;
1032 }
1033
1034#ifdef MS_WINDOWS
Steve Dower8fc89802015-04-12 00:26:27 -04001035 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001036 handle = (HANDLE)_get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001037 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001038 if (handle == INVALID_HANDLE_VALUE) {
1039 if (raise)
Steve Dower41e72442015-03-14 11:38:27 -07001040 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001041 return -1;
1042 }
1043
1044 if (inheritable)
1045 flags = HANDLE_FLAG_INHERIT;
1046 else
1047 flags = 0;
1048 if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) {
1049 if (raise)
1050 PyErr_SetFromWindowsErr(0);
1051 return -1;
1052 }
1053 return 0;
1054
Victor Stinnerdaf45552013-08-28 00:53:59 +02001055#else
Victor Stinner282124b2014-09-02 11:41:04 +02001056
1057#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +03001058 if (ioctl_works != 0 && raise != 0) {
Victor Stinner282124b2014-09-02 11:41:04 +02001059 /* fast-path: ioctl() only requires one syscall */
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +03001060 /* caveat: raise=0 is an indicator that we must be async-signal-safe
1061 * thus avoid using ioctl() so we skip the fast-path. */
Victor Stinner282124b2014-09-02 11:41:04 +02001062 if (inheritable)
1063 request = FIONCLEX;
1064 else
1065 request = FIOCLEX;
1066 err = ioctl(fd, request, NULL);
1067 if (!err) {
1068 ioctl_works = 1;
1069 return 0;
1070 }
1071
Victor Stinner3116cc42016-05-19 16:46:18 +02001072 if (errno != ENOTTY && errno != EACCES) {
Victor Stinner282124b2014-09-02 11:41:04 +02001073 if (raise)
1074 PyErr_SetFromErrno(PyExc_OSError);
1075 return -1;
1076 }
1077 else {
1078 /* Issue #22258: Here, ENOTTY means "Inappropriate ioctl for
1079 device". The ioctl is declared but not supported by the kernel.
1080 Remember that ioctl() doesn't work. It is the case on
Victor Stinner3116cc42016-05-19 16:46:18 +02001081 Illumos-based OS for example.
1082
1083 Issue #27057: When SELinux policy disallows ioctl it will fail
1084 with EACCES. While FIOCLEX is safe operation it may be
1085 unavailable because ioctl was denied altogether.
1086 This can be the case on Android. */
Victor Stinner282124b2014-09-02 11:41:04 +02001087 ioctl_works = 0;
1088 }
1089 /* fallback to fcntl() if ioctl() does not work */
1090 }
1091#endif
1092
1093 /* slow-path: fcntl() requires two syscalls */
Victor Stinnerdaf45552013-08-28 00:53:59 +02001094 flags = fcntl(fd, F_GETFD);
1095 if (flags < 0) {
1096 if (raise)
1097 PyErr_SetFromErrno(PyExc_OSError);
1098 return -1;
1099 }
1100
Victor Stinnera858bbd2016-04-17 16:51:52 +02001101 if (inheritable) {
1102 new_flags = flags & ~FD_CLOEXEC;
1103 }
1104 else {
1105 new_flags = flags | FD_CLOEXEC;
1106 }
1107
1108 if (new_flags == flags) {
1109 /* FD_CLOEXEC flag already set/cleared: nothing to do */
1110 return 0;
1111 }
1112
Xavier de Gayeec5d3cd2016-11-19 16:19:29 +01001113 res = fcntl(fd, F_SETFD, new_flags);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001114 if (res < 0) {
1115 if (raise)
1116 PyErr_SetFromErrno(PyExc_OSError);
1117 return -1;
1118 }
1119 return 0;
1120#endif
1121}
1122
1123/* Make the file descriptor non-inheritable.
Victor Stinnerb034eee2013-09-07 10:36:04 +02001124 Return 0 on success, set errno and return -1 on error. */
Victor Stinnerdaf45552013-08-28 00:53:59 +02001125static int
1126make_non_inheritable(int fd)
1127{
1128 return set_inheritable(fd, 0, 0, NULL);
1129}
1130
1131/* Set the inheritable flag of the specified file descriptor.
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +03001132 On success: return 0, on error: raise an exception and return -1.
Victor Stinnerdaf45552013-08-28 00:53:59 +02001133
1134 If atomic_flag_works is not NULL:
1135
1136 * if *atomic_flag_works==-1, check if the inheritable is set on the file
1137 descriptor: if yes, set *atomic_flag_works to 1, otherwise set to 0 and
1138 set the inheritable flag
1139 * if *atomic_flag_works==1: do nothing
1140 * if *atomic_flag_works==0: set inheritable flag to False
1141
1142 Set atomic_flag_works to NULL if no atomic flag was used to create the
1143 file descriptor.
1144
1145 atomic_flag_works can only be used to make a file descriptor
1146 non-inheritable: atomic_flag_works must be NULL if inheritable=1. */
1147int
1148_Py_set_inheritable(int fd, int inheritable, int *atomic_flag_works)
1149{
1150 return set_inheritable(fd, inheritable, 1, atomic_flag_works);
1151}
1152
Alexey Izbyshevc1e46e92018-02-06 09:09:34 +03001153/* Same as _Py_set_inheritable() but on error, set errno and
1154 don't raise an exception.
1155 This function is async-signal-safe. */
1156int
1157_Py_set_inheritable_async_safe(int fd, int inheritable, int *atomic_flag_works)
1158{
1159 return set_inheritable(fd, inheritable, 0, atomic_flag_works);
1160}
1161
Victor Stinnera555cfc2015-03-18 00:22:14 +01001162static int
1163_Py_open_impl(const char *pathname, int flags, int gil_held)
Victor Stinnerdaf45552013-08-28 00:53:59 +02001164{
1165 int fd;
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001166 int async_err = 0;
Victor Stinnera555cfc2015-03-18 00:22:14 +01001167#ifndef MS_WINDOWS
Victor Stinnerdaf45552013-08-28 00:53:59 +02001168 int *atomic_flag_works;
Victor Stinnera555cfc2015-03-18 00:22:14 +01001169#endif
1170
1171#ifdef MS_WINDOWS
1172 flags |= O_NOINHERIT;
1173#elif defined(O_CLOEXEC)
Victor Stinnerdaf45552013-08-28 00:53:59 +02001174 atomic_flag_works = &_Py_open_cloexec_works;
1175 flags |= O_CLOEXEC;
1176#else
1177 atomic_flag_works = NULL;
1178#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +02001179
Victor Stinnera555cfc2015-03-18 00:22:14 +01001180 if (gil_held) {
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001181 do {
1182 Py_BEGIN_ALLOW_THREADS
1183 fd = open(pathname, flags);
1184 Py_END_ALLOW_THREADS
1185 } while (fd < 0
1186 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1187 if (async_err)
1188 return -1;
Victor Stinnera555cfc2015-03-18 00:22:14 +01001189 if (fd < 0) {
1190 PyErr_SetFromErrnoWithFilename(PyExc_OSError, pathname);
1191 return -1;
1192 }
1193 }
1194 else {
1195 fd = open(pathname, flags);
1196 if (fd < 0)
1197 return -1;
1198 }
1199
1200#ifndef MS_WINDOWS
1201 if (set_inheritable(fd, 0, gil_held, atomic_flag_works) < 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +02001202 close(fd);
1203 return -1;
1204 }
Victor Stinnera555cfc2015-03-18 00:22:14 +01001205#endif
1206
Victor Stinnerdaf45552013-08-28 00:53:59 +02001207 return fd;
1208}
1209
Victor Stinnera555cfc2015-03-18 00:22:14 +01001210/* Open a file with the specified flags (wrapper to open() function).
1211 Return a file descriptor on success. Raise an exception and return -1 on
1212 error.
1213
1214 The file descriptor is created non-inheritable.
1215
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001216 When interrupted by a signal (open() fails with EINTR), retry the syscall,
1217 except if the Python signal handler raises an exception.
1218
Victor Stinner6f4fae82015-04-01 18:34:32 +02001219 Release the GIL to call open(). The caller must hold the GIL. */
Victor Stinnera555cfc2015-03-18 00:22:14 +01001220int
1221_Py_open(const char *pathname, int flags)
1222{
1223 /* _Py_open() must be called with the GIL held. */
1224 assert(PyGILState_Check());
1225 return _Py_open_impl(pathname, flags, 1);
1226}
1227
1228/* Open a file with the specified flags (wrapper to open() function).
1229 Return a file descriptor on success. Set errno and return -1 on error.
1230
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001231 The file descriptor is created non-inheritable.
1232
1233 If interrupted by a signal, fail with EINTR. */
Victor Stinnera555cfc2015-03-18 00:22:14 +01001234int
1235_Py_open_noraise(const char *pathname, int flags)
1236{
1237 return _Py_open_impl(pathname, flags, 0);
1238}
1239
Victor Stinnerdaf45552013-08-28 00:53:59 +02001240/* Open a file. Use _wfopen() on Windows, encode the path to the locale
Victor Stinnere42ccd22015-03-18 01:39:23 +01001241 encoding and use fopen() otherwise.
1242
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001243 The file descriptor is created non-inheritable.
1244
1245 If interrupted by a signal, fail with EINTR. */
Victor Stinner4e314432010-10-07 21:45:39 +00001246FILE *
1247_Py_wfopen(const wchar_t *path, const wchar_t *mode)
1248{
Victor Stinner4e314432010-10-07 21:45:39 +00001249 FILE *f;
Victor Stinnerdaf45552013-08-28 00:53:59 +02001250#ifndef MS_WINDOWS
Victor Stinner4e314432010-10-07 21:45:39 +00001251 char *cpath;
1252 char cmode[10];
1253 size_t r;
1254 r = wcstombs(cmode, mode, 10);
1255 if (r == (size_t)-1 || r >= 10) {
1256 errno = EINVAL;
1257 return NULL;
1258 }
Victor Stinner9dd76202017-12-21 16:20:32 +01001259 cpath = _Py_EncodeLocaleRaw(path, NULL);
1260 if (cpath == NULL) {
Victor Stinner4e314432010-10-07 21:45:39 +00001261 return NULL;
Victor Stinner9dd76202017-12-21 16:20:32 +01001262 }
Victor Stinner4e314432010-10-07 21:45:39 +00001263 f = fopen(cpath, cmode);
Victor Stinner9dd76202017-12-21 16:20:32 +01001264 PyMem_RawFree(cpath);
Victor Stinner4e314432010-10-07 21:45:39 +00001265#else
Victor Stinnerdaf45552013-08-28 00:53:59 +02001266 f = _wfopen(path, mode);
Victor Stinner4e314432010-10-07 21:45:39 +00001267#endif
Victor Stinnerdaf45552013-08-28 00:53:59 +02001268 if (f == NULL)
1269 return NULL;
1270 if (make_non_inheritable(fileno(f)) < 0) {
1271 fclose(f);
1272 return NULL;
1273 }
1274 return f;
Victor Stinner4e314432010-10-07 21:45:39 +00001275}
1276
Victor Stinnere42ccd22015-03-18 01:39:23 +01001277/* Wrapper to fopen().
1278
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001279 The file descriptor is created non-inheritable.
1280
1281 If interrupted by a signal, fail with EINTR. */
Victor Stinnerdaf45552013-08-28 00:53:59 +02001282FILE*
1283_Py_fopen(const char *pathname, const char *mode)
1284{
1285 FILE *f = fopen(pathname, mode);
1286 if (f == NULL)
1287 return NULL;
1288 if (make_non_inheritable(fileno(f)) < 0) {
1289 fclose(f);
1290 return NULL;
1291 }
1292 return f;
1293}
1294
1295/* Open a file. Call _wfopen() on Windows, or encode the path to the filesystem
Victor Stinnere42ccd22015-03-18 01:39:23 +01001296 encoding and call fopen() otherwise.
Victor Stinner6672d0c2010-10-07 22:53:43 +00001297
Victor Stinnere42ccd22015-03-18 01:39:23 +01001298 Return the new file object on success. Raise an exception and return NULL
1299 on error.
1300
1301 The file descriptor is created non-inheritable.
1302
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001303 When interrupted by a signal (open() fails with EINTR), retry the syscall,
1304 except if the Python signal handler raises an exception.
1305
Victor Stinner6f4fae82015-04-01 18:34:32 +02001306 Release the GIL to call _wfopen() or fopen(). The caller must hold
1307 the GIL. */
Victor Stinner4e314432010-10-07 21:45:39 +00001308FILE*
Victor Stinnerdaf45552013-08-28 00:53:59 +02001309_Py_fopen_obj(PyObject *path, const char *mode)
Victor Stinner4e314432010-10-07 21:45:39 +00001310{
Victor Stinnerdaf45552013-08-28 00:53:59 +02001311 FILE *f;
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001312 int async_err = 0;
Victor Stinner4e314432010-10-07 21:45:39 +00001313#ifdef MS_WINDOWS
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03001314 const wchar_t *wpath;
Victor Stinner4e314432010-10-07 21:45:39 +00001315 wchar_t wmode[10];
1316 int usize;
Victor Stinner4e314432010-10-07 21:45:39 +00001317
Victor Stinnere42ccd22015-03-18 01:39:23 +01001318 assert(PyGILState_Check());
1319
Antoine Pitrou0e576f12011-12-22 10:03:38 +01001320 if (!PyUnicode_Check(path)) {
1321 PyErr_Format(PyExc_TypeError,
1322 "str file path expected under Windows, got %R",
1323 Py_TYPE(path));
1324 return NULL;
1325 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03001326 wpath = _PyUnicode_AsUnicode(path);
Victor Stinneree587ea2011-11-17 00:51:38 +01001327 if (wpath == NULL)
1328 return NULL;
1329
Alexey Izbyshevb3b4a9d2018-02-18 20:57:24 +03001330 usize = MultiByteToWideChar(CP_ACP, 0, mode, -1,
1331 wmode, Py_ARRAY_LENGTH(wmode));
Victor Stinnere42ccd22015-03-18 01:39:23 +01001332 if (usize == 0) {
1333 PyErr_SetFromWindowsErr(0);
Victor Stinner4e314432010-10-07 21:45:39 +00001334 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001335 }
Victor Stinner4e314432010-10-07 21:45:39 +00001336
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001337 do {
1338 Py_BEGIN_ALLOW_THREADS
1339 f = _wfopen(wpath, wmode);
1340 Py_END_ALLOW_THREADS
1341 } while (f == NULL
1342 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner4e314432010-10-07 21:45:39 +00001343#else
Antoine Pitrou2b1cc892011-12-19 18:19:06 +01001344 PyObject *bytes;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001345 char *path_bytes;
1346
1347 assert(PyGILState_Check());
1348
Antoine Pitrou2b1cc892011-12-19 18:19:06 +01001349 if (!PyUnicode_FSConverter(path, &bytes))
Victor Stinner4e314432010-10-07 21:45:39 +00001350 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001351 path_bytes = PyBytes_AS_STRING(bytes);
1352
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001353 do {
1354 Py_BEGIN_ALLOW_THREADS
1355 f = fopen(path_bytes, mode);
1356 Py_END_ALLOW_THREADS
1357 } while (f == NULL
1358 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnere42ccd22015-03-18 01:39:23 +01001359
Victor Stinner4e314432010-10-07 21:45:39 +00001360 Py_DECREF(bytes);
Victor Stinner4e314432010-10-07 21:45:39 +00001361#endif
Victor Stinnera47fc5c2015-03-18 09:52:54 +01001362 if (async_err)
1363 return NULL;
1364
Victor Stinnere42ccd22015-03-18 01:39:23 +01001365 if (f == NULL) {
1366 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001367 return NULL;
Victor Stinnere42ccd22015-03-18 01:39:23 +01001368 }
1369
1370 if (set_inheritable(fileno(f), 0, 1, NULL) < 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +02001371 fclose(f);
1372 return NULL;
1373 }
1374 return f;
Victor Stinner4e314432010-10-07 21:45:39 +00001375}
1376
Victor Stinner66aab0c2015-03-19 22:53:20 +01001377/* Read count bytes from fd into buf.
Victor Stinner82c3e452015-04-01 18:34:45 +02001378
1379 On success, return the number of read bytes, it can be lower than count.
1380 If the current file offset is at or past the end of file, no bytes are read,
1381 and read() returns zero.
1382
1383 On error, raise an exception, set errno and return -1.
1384
1385 When interrupted by a signal (read() fails with EINTR), retry the syscall.
1386 If the Python signal handler raises an exception, the function returns -1
1387 (the syscall is not retried).
1388
1389 Release the GIL to call read(). The caller must hold the GIL. */
Victor Stinner66aab0c2015-03-19 22:53:20 +01001390Py_ssize_t
1391_Py_read(int fd, void *buf, size_t count)
1392{
1393 Py_ssize_t n;
Victor Stinnera3c02022015-03-20 11:58:18 +01001394 int err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001395 int async_err = 0;
1396
Victor Stinner8a1be612016-03-14 22:07:55 +01001397 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +01001398
Victor Stinner66aab0c2015-03-19 22:53:20 +01001399 /* _Py_read() must not be called with an exception set, otherwise the
1400 * caller may think that read() was interrupted by a signal and the signal
1401 * handler raised an exception. */
1402 assert(!PyErr_Occurred());
1403
Victor Stinner66aab0c2015-03-19 22:53:20 +01001404#ifdef MS_WINDOWS
1405 if (count > INT_MAX) {
1406 /* On Windows, the count parameter of read() is an int */
1407 count = INT_MAX;
1408 }
1409#else
1410 if (count > PY_SSIZE_T_MAX) {
1411 /* if count is greater than PY_SSIZE_T_MAX,
1412 * read() result is undefined */
1413 count = PY_SSIZE_T_MAX;
1414 }
1415#endif
1416
Steve Dower8fc89802015-04-12 00:26:27 -04001417 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001418 do {
1419 Py_BEGIN_ALLOW_THREADS
1420 errno = 0;
1421#ifdef MS_WINDOWS
1422 n = read(fd, buf, (int)count);
1423#else
1424 n = read(fd, buf, count);
1425#endif
Victor Stinnera3c02022015-03-20 11:58:18 +01001426 /* save/restore errno because PyErr_CheckSignals()
1427 * and PyErr_SetFromErrno() can modify it */
1428 err = errno;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001429 Py_END_ALLOW_THREADS
Victor Stinnera3c02022015-03-20 11:58:18 +01001430 } while (n < 0 && err == EINTR &&
Victor Stinner66aab0c2015-03-19 22:53:20 +01001431 !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04001432 _Py_END_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001433
1434 if (async_err) {
1435 /* read() was interrupted by a signal (failed with EINTR)
1436 * and the Python signal handler raised an exception */
Victor Stinnera3c02022015-03-20 11:58:18 +01001437 errno = err;
1438 assert(errno == EINTR && PyErr_Occurred());
Victor Stinner66aab0c2015-03-19 22:53:20 +01001439 return -1;
1440 }
1441 if (n < 0) {
Victor Stinner66aab0c2015-03-19 22:53:20 +01001442 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera3c02022015-03-20 11:58:18 +01001443 errno = err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001444 return -1;
1445 }
1446
1447 return n;
1448}
1449
Victor Stinner82c3e452015-04-01 18:34:45 +02001450static Py_ssize_t
1451_Py_write_impl(int fd, const void *buf, size_t count, int gil_held)
Victor Stinner66aab0c2015-03-19 22:53:20 +01001452{
1453 Py_ssize_t n;
Victor Stinnera3c02022015-03-20 11:58:18 +01001454 int err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001455 int async_err = 0;
1456
Steve Dower8fc89802015-04-12 00:26:27 -04001457 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001458#ifdef MS_WINDOWS
1459 if (count > 32767 && isatty(fd)) {
1460 /* Issue #11395: the Windows console returns an error (12: not
1461 enough space error) on writing into stdout if stdout mode is
1462 binary and the length is greater than 66,000 bytes (or less,
1463 depending on heap usage). */
1464 count = 32767;
1465 }
1466 else if (count > INT_MAX)
1467 count = INT_MAX;
1468#else
1469 if (count > PY_SSIZE_T_MAX) {
1470 /* write() should truncate count to PY_SSIZE_T_MAX, but it's safer
1471 * to do it ourself to have a portable behaviour. */
1472 count = PY_SSIZE_T_MAX;
1473 }
1474#endif
1475
Victor Stinner82c3e452015-04-01 18:34:45 +02001476 if (gil_held) {
1477 do {
1478 Py_BEGIN_ALLOW_THREADS
1479 errno = 0;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001480#ifdef MS_WINDOWS
Victor Stinner82c3e452015-04-01 18:34:45 +02001481 n = write(fd, buf, (int)count);
Victor Stinner66aab0c2015-03-19 22:53:20 +01001482#else
Victor Stinner82c3e452015-04-01 18:34:45 +02001483 n = write(fd, buf, count);
Victor Stinner66aab0c2015-03-19 22:53:20 +01001484#endif
Victor Stinner82c3e452015-04-01 18:34:45 +02001485 /* save/restore errno because PyErr_CheckSignals()
1486 * and PyErr_SetFromErrno() can modify it */
1487 err = errno;
1488 Py_END_ALLOW_THREADS
1489 } while (n < 0 && err == EINTR &&
1490 !(async_err = PyErr_CheckSignals()));
1491 }
1492 else {
1493 do {
1494 errno = 0;
1495#ifdef MS_WINDOWS
1496 n = write(fd, buf, (int)count);
1497#else
1498 n = write(fd, buf, count);
1499#endif
1500 err = errno;
1501 } while (n < 0 && err == EINTR);
1502 }
Steve Dower8fc89802015-04-12 00:26:27 -04001503 _Py_END_SUPPRESS_IPH
Victor Stinner66aab0c2015-03-19 22:53:20 +01001504
1505 if (async_err) {
1506 /* write() was interrupted by a signal (failed with EINTR)
Victor Stinner82c3e452015-04-01 18:34:45 +02001507 and the Python signal handler raised an exception (if gil_held is
1508 nonzero). */
Victor Stinnera3c02022015-03-20 11:58:18 +01001509 errno = err;
Victor Stinner82c3e452015-04-01 18:34:45 +02001510 assert(errno == EINTR && (!gil_held || PyErr_Occurred()));
Victor Stinner66aab0c2015-03-19 22:53:20 +01001511 return -1;
1512 }
1513 if (n < 0) {
Victor Stinner82c3e452015-04-01 18:34:45 +02001514 if (gil_held)
1515 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera3c02022015-03-20 11:58:18 +01001516 errno = err;
Victor Stinner66aab0c2015-03-19 22:53:20 +01001517 return -1;
1518 }
1519
1520 return n;
1521}
1522
Victor Stinner82c3e452015-04-01 18:34:45 +02001523/* Write count bytes of buf into fd.
1524
1525 On success, return the number of written bytes, it can be lower than count
1526 including 0. On error, raise an exception, set errno and return -1.
1527
1528 When interrupted by a signal (write() fails with EINTR), retry the syscall.
1529 If the Python signal handler raises an exception, the function returns -1
1530 (the syscall is not retried).
1531
1532 Release the GIL to call write(). The caller must hold the GIL. */
1533Py_ssize_t
1534_Py_write(int fd, const void *buf, size_t count)
1535{
Victor Stinner8a1be612016-03-14 22:07:55 +01001536 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +01001537
Victor Stinner82c3e452015-04-01 18:34:45 +02001538 /* _Py_write() must not be called with an exception set, otherwise the
1539 * caller may think that write() was interrupted by a signal and the signal
1540 * handler raised an exception. */
1541 assert(!PyErr_Occurred());
1542
1543 return _Py_write_impl(fd, buf, count, 1);
1544}
1545
1546/* Write count bytes of buf into fd.
1547 *
1548 * On success, return the number of written bytes, it can be lower than count
1549 * including 0. On error, set errno and return -1.
1550 *
1551 * When interrupted by a signal (write() fails with EINTR), retry the syscall
1552 * without calling the Python signal handler. */
1553Py_ssize_t
1554_Py_write_noraise(int fd, const void *buf, size_t count)
1555{
1556 return _Py_write_impl(fd, buf, count, 0);
1557}
1558
Victor Stinner4e314432010-10-07 21:45:39 +00001559#ifdef HAVE_READLINK
Victor Stinner6672d0c2010-10-07 22:53:43 +00001560
1561/* Read value of symbolic link. Encode the path to the locale encoding, decode
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001562 the result from the locale encoding. Return -1 on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001563
Victor Stinner4e314432010-10-07 21:45:39 +00001564int
1565_Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
1566{
1567 char *cpath;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001568 char cbuf[MAXPATHLEN];
Victor Stinner3f711f42010-10-16 22:47:37 +00001569 wchar_t *wbuf;
Victor Stinner4e314432010-10-07 21:45:39 +00001570 int res;
1571 size_t r1;
1572
Victor Stinner9dd76202017-12-21 16:20:32 +01001573 cpath = _Py_EncodeLocaleRaw(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001574 if (cpath == NULL) {
1575 errno = EINVAL;
1576 return -1;
1577 }
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001578 res = (int)readlink(cpath, cbuf, Py_ARRAY_LENGTH(cbuf));
Victor Stinner9dd76202017-12-21 16:20:32 +01001579 PyMem_RawFree(cpath);
Victor Stinner4e314432010-10-07 21:45:39 +00001580 if (res == -1)
1581 return -1;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001582 if (res == Py_ARRAY_LENGTH(cbuf)) {
Victor Stinner4e314432010-10-07 21:45:39 +00001583 errno = EINVAL;
1584 return -1;
1585 }
1586 cbuf[res] = '\0'; /* buf will be null terminated */
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001587 wbuf = Py_DecodeLocale(cbuf, &r1);
Victor Stinner350147b2010-10-16 22:52:09 +00001588 if (wbuf == NULL) {
1589 errno = EINVAL;
1590 return -1;
1591 }
Victor Stinner3f711f42010-10-16 22:47:37 +00001592 if (bufsiz <= r1) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001593 PyMem_RawFree(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001594 errno = EINVAL;
1595 return -1;
1596 }
Victor Stinner3f711f42010-10-16 22:47:37 +00001597 wcsncpy(buf, wbuf, bufsiz);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001598 PyMem_RawFree(wbuf);
Victor Stinner4e314432010-10-07 21:45:39 +00001599 return (int)r1;
1600}
1601#endif
1602
1603#ifdef HAVE_REALPATH
Victor Stinner6672d0c2010-10-07 22:53:43 +00001604
1605/* Return the canonicalized absolute pathname. Encode path to the locale
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001606 encoding, decode the result from the locale encoding.
1607 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001608
Victor Stinner4e314432010-10-07 21:45:39 +00001609wchar_t*
Victor Stinner015f4d82010-10-07 22:29:53 +00001610_Py_wrealpath(const wchar_t *path,
1611 wchar_t *resolved_path, size_t resolved_path_size)
Victor Stinner4e314432010-10-07 21:45:39 +00001612{
1613 char *cpath;
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001614 char cresolved_path[MAXPATHLEN];
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001615 wchar_t *wresolved_path;
Victor Stinner4e314432010-10-07 21:45:39 +00001616 char *res;
1617 size_t r;
Victor Stinner9dd76202017-12-21 16:20:32 +01001618 cpath = _Py_EncodeLocaleRaw(path, NULL);
Victor Stinner4e314432010-10-07 21:45:39 +00001619 if (cpath == NULL) {
1620 errno = EINVAL;
1621 return NULL;
1622 }
1623 res = realpath(cpath, cresolved_path);
Victor Stinner9dd76202017-12-21 16:20:32 +01001624 PyMem_RawFree(cpath);
Victor Stinner4e314432010-10-07 21:45:39 +00001625 if (res == NULL)
1626 return NULL;
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001627
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001628 wresolved_path = Py_DecodeLocale(cresolved_path, &r);
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001629 if (wresolved_path == NULL) {
Victor Stinner4e314432010-10-07 21:45:39 +00001630 errno = EINVAL;
1631 return NULL;
1632 }
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001633 if (resolved_path_size <= r) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001634 PyMem_RawFree(wresolved_path);
Victor Stinner0a1b8cb2010-10-16 22:55:47 +00001635 errno = EINVAL;
1636 return NULL;
1637 }
1638 wcsncpy(resolved_path, wresolved_path, resolved_path_size);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001639 PyMem_RawFree(wresolved_path);
Victor Stinner4e314432010-10-07 21:45:39 +00001640 return resolved_path;
1641}
1642#endif
1643
Victor Stinnerf4061da2010-10-14 12:37:19 +00001644/* Get the current directory. size is the buffer size in wide characters
Victor Stinneraf02e1c2011-12-16 23:56:01 +01001645 including the null character. Decode the path from the locale encoding.
1646 Return NULL on error. */
Victor Stinner6672d0c2010-10-07 22:53:43 +00001647
Victor Stinner4e314432010-10-07 21:45:39 +00001648wchar_t*
1649_Py_wgetcwd(wchar_t *buf, size_t size)
1650{
1651#ifdef MS_WINDOWS
Victor Stinner56785ea2013-06-05 00:46:29 +02001652 int isize = (int)Py_MIN(size, INT_MAX);
1653 return _wgetcwd(buf, isize);
Victor Stinner4e314432010-10-07 21:45:39 +00001654#else
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001655 char fname[MAXPATHLEN];
Victor Stinnerf4061da2010-10-14 12:37:19 +00001656 wchar_t *wname;
Victor Stinner168e1172010-10-16 23:16:16 +00001657 size_t len;
Victor Stinnerf4061da2010-10-14 12:37:19 +00001658
Victor Stinnerb11d6cb2013-11-15 18:14:11 +01001659 if (getcwd(fname, Py_ARRAY_LENGTH(fname)) == NULL)
Victor Stinner4e314432010-10-07 21:45:39 +00001660 return NULL;
Victor Stinnerf6a271a2014-08-01 12:28:48 +02001661 wname = Py_DecodeLocale(fname, &len);
Victor Stinnerf4061da2010-10-14 12:37:19 +00001662 if (wname == NULL)
1663 return NULL;
Victor Stinner168e1172010-10-16 23:16:16 +00001664 if (size <= len) {
Victor Stinner1a7425f2013-07-07 16:25:15 +02001665 PyMem_RawFree(wname);
Victor Stinner4e314432010-10-07 21:45:39 +00001666 return NULL;
1667 }
Victor Stinnerf4061da2010-10-14 12:37:19 +00001668 wcsncpy(buf, wname, size);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001669 PyMem_RawFree(wname);
Victor Stinner4e314432010-10-07 21:45:39 +00001670 return buf;
1671#endif
1672}
1673
Victor Stinnerdaf45552013-08-28 00:53:59 +02001674/* Duplicate a file descriptor. The new file descriptor is created as
1675 non-inheritable. Return a new file descriptor on success, raise an OSError
1676 exception and return -1 on error.
1677
1678 The GIL is released to call dup(). The caller must hold the GIL. */
1679int
1680_Py_dup(int fd)
1681{
1682#ifdef MS_WINDOWS
1683 HANDLE handle;
1684 DWORD ftype;
1685#endif
1686
Victor Stinner8a1be612016-03-14 22:07:55 +01001687 assert(PyGILState_Check());
Victor Stinner8a1be612016-03-14 22:07:55 +01001688
Victor Stinnerdaf45552013-08-28 00:53:59 +02001689#ifdef MS_WINDOWS
Steve Dower8fc89802015-04-12 00:26:27 -04001690 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001691 handle = (HANDLE)_get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001692 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001693 if (handle == INVALID_HANDLE_VALUE) {
Steve Dower41e72442015-03-14 11:38:27 -07001694 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001695 return -1;
1696 }
1697
1698 /* get the file type, ignore the error if it failed */
1699 ftype = GetFileType(handle);
1700
1701 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001702 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001703 fd = dup(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001704 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001705 Py_END_ALLOW_THREADS
1706 if (fd < 0) {
1707 PyErr_SetFromErrno(PyExc_OSError);
1708 return -1;
1709 }
1710
1711 /* Character files like console cannot be make non-inheritable */
1712 if (ftype != FILE_TYPE_CHAR) {
1713 if (_Py_set_inheritable(fd, 0, NULL) < 0) {
Steve Dower8fc89802015-04-12 00:26:27 -04001714 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001715 close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001716 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001717 return -1;
1718 }
1719 }
1720#elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC)
1721 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001722 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001723 fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Steve Dower8fc89802015-04-12 00:26:27 -04001724 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001725 Py_END_ALLOW_THREADS
1726 if (fd < 0) {
1727 PyErr_SetFromErrno(PyExc_OSError);
1728 return -1;
1729 }
1730
1731#else
1732 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001733 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001734 fd = dup(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001735 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001736 Py_END_ALLOW_THREADS
1737 if (fd < 0) {
1738 PyErr_SetFromErrno(PyExc_OSError);
1739 return -1;
1740 }
1741
1742 if (_Py_set_inheritable(fd, 0, NULL) < 0) {
Steve Dower8fc89802015-04-12 00:26:27 -04001743 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001744 close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001745 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02001746 return -1;
1747 }
1748#endif
1749 return fd;
1750}
1751
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001752#ifndef MS_WINDOWS
1753/* Get the blocking mode of the file descriptor.
1754 Return 0 if the O_NONBLOCK flag is set, 1 if the flag is cleared,
1755 raise an exception and return -1 on error. */
1756int
1757_Py_get_blocking(int fd)
1758{
Steve Dower8fc89802015-04-12 00:26:27 -04001759 int flags;
1760 _Py_BEGIN_SUPPRESS_IPH
1761 flags = fcntl(fd, F_GETFL, 0);
1762 _Py_END_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001763 if (flags < 0) {
1764 PyErr_SetFromErrno(PyExc_OSError);
1765 return -1;
1766 }
1767
1768 return !(flags & O_NONBLOCK);
1769}
1770
1771/* Set the blocking mode of the specified file descriptor.
1772
1773 Set the O_NONBLOCK flag if blocking is False, clear the O_NONBLOCK flag
1774 otherwise.
1775
1776 Return 0 on success, raise an exception and return -1 on error. */
1777int
1778_Py_set_blocking(int fd, int blocking)
1779{
1780#if defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)
1781 int arg = !blocking;
1782 if (ioctl(fd, FIONBIO, &arg) < 0)
1783 goto error;
1784#else
1785 int flags, res;
1786
Steve Dower8fc89802015-04-12 00:26:27 -04001787 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001788 flags = fcntl(fd, F_GETFL, 0);
Steve Dower8fc89802015-04-12 00:26:27 -04001789 if (flags >= 0) {
1790 if (blocking)
1791 flags = flags & (~O_NONBLOCK);
1792 else
1793 flags = flags | O_NONBLOCK;
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001794
Steve Dower8fc89802015-04-12 00:26:27 -04001795 res = fcntl(fd, F_SETFL, flags);
1796 } else {
1797 res = -1;
1798 }
1799 _Py_END_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001800
Victor Stinner1db9e7b2014-07-29 22:32:47 +02001801 if (res < 0)
1802 goto error;
1803#endif
1804 return 0;
1805
1806error:
1807 PyErr_SetFromErrno(PyExc_OSError);
1808 return -1;
1809}
1810#endif
Victor Stinnercb064fc2018-01-15 15:58:02 +01001811
1812
1813int
1814_Py_GetLocaleconvNumeric(PyObject **decimal_point, PyObject **thousands_sep,
1815 const char **grouping)
1816{
1817 int res = -1;
1818
1819 struct lconv *lc = localeconv();
1820
1821 int change_locale = 0;
1822 if (decimal_point != NULL &&
1823 (strlen(lc->decimal_point) > 1 || ((unsigned char)lc->decimal_point[0]) > 127))
1824 {
1825 change_locale = 1;
1826 }
1827 if (thousands_sep != NULL &&
1828 (strlen(lc->thousands_sep) > 1 || ((unsigned char)lc->thousands_sep[0]) > 127))
1829 {
1830 change_locale = 1;
1831 }
1832
1833 /* Keep a copy of the LC_CTYPE locale */
1834 char *oldloc = NULL, *loc = NULL;
1835 if (change_locale) {
1836 oldloc = setlocale(LC_CTYPE, NULL);
1837 if (!oldloc) {
1838 PyErr_SetString(PyExc_RuntimeWarning, "faild to get LC_CTYPE locale");
1839 return -1;
1840 }
1841
1842 oldloc = _PyMem_Strdup(oldloc);
1843 if (!oldloc) {
1844 PyErr_NoMemory();
1845 return -1;
1846 }
1847
1848 loc = setlocale(LC_NUMERIC, NULL);
1849 if (loc != NULL && strcmp(loc, oldloc) == 0) {
1850 loc = NULL;
1851 }
1852
1853 if (loc != NULL) {
1854 /* Only set the locale temporarilty the LC_CTYPE locale
1855 if LC_NUMERIC locale is different than LC_CTYPE locale and
1856 decimal_point and/or thousands_sep are non-ASCII or longer than
1857 1 byte */
1858 setlocale(LC_CTYPE, loc);
1859 }
1860 }
1861
1862 if (decimal_point != NULL) {
1863 *decimal_point = PyUnicode_DecodeLocale(lc->decimal_point, NULL);
1864 if (*decimal_point == NULL) {
1865 goto error;
1866 }
1867 }
1868 if (thousands_sep != NULL) {
1869 *thousands_sep = PyUnicode_DecodeLocale(lc->thousands_sep, NULL);
1870 if (*thousands_sep == NULL) {
1871 goto error;
1872 }
1873 }
1874
1875 if (grouping != NULL) {
1876 *grouping = lc->grouping;
1877 }
1878
1879 res = 0;
1880
1881error:
1882 if (loc != NULL) {
1883 setlocale(LC_CTYPE, oldloc);
1884 }
1885 PyMem_Free(oldloc);
1886 return res;
1887}