1 | n/a | #include "Python.h" |
---|
2 | n/a | #include "osdefs.h" |
---|
3 | n/a | #include <locale.h> |
---|
4 | n/a | |
---|
5 | n/a | #ifdef MS_WINDOWS |
---|
6 | n/a | # include <malloc.h> |
---|
7 | n/a | # include <windows.h> |
---|
8 | n/a | extern int winerror_to_errno(int); |
---|
9 | n/a | #endif |
---|
10 | n/a | |
---|
11 | n/a | #ifdef HAVE_LANGINFO_H |
---|
12 | n/a | #include <langinfo.h> |
---|
13 | n/a | #endif |
---|
14 | n/a | |
---|
15 | n/a | #ifdef HAVE_SYS_IOCTL_H |
---|
16 | n/a | #include <sys/ioctl.h> |
---|
17 | n/a | #endif |
---|
18 | n/a | |
---|
19 | n/a | #ifdef HAVE_FCNTL_H |
---|
20 | n/a | #include <fcntl.h> |
---|
21 | n/a | #endif /* HAVE_FCNTL_H */ |
---|
22 | n/a | |
---|
23 | n/a | #if defined(__APPLE__) || defined(__ANDROID__) |
---|
24 | n/a | extern wchar_t* _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size); |
---|
25 | n/a | #endif |
---|
26 | n/a | |
---|
27 | n/a | #ifdef O_CLOEXEC |
---|
28 | n/a | /* Does open() support the O_CLOEXEC flag? Possible values: |
---|
29 | n/a | |
---|
30 | n/a | -1: unknown |
---|
31 | n/a | 0: open() ignores O_CLOEXEC flag, ex: Linux kernel older than 2.6.23 |
---|
32 | n/a | 1: open() supports O_CLOEXEC flag, close-on-exec is set |
---|
33 | n/a | |
---|
34 | n/a | The flag is used by _Py_open(), _Py_open_noraise(), io.FileIO |
---|
35 | n/a | and os.open(). */ |
---|
36 | n/a | int _Py_open_cloexec_works = -1; |
---|
37 | n/a | #endif |
---|
38 | n/a | |
---|
39 | n/a | PyObject * |
---|
40 | n/a | _Py_device_encoding(int fd) |
---|
41 | n/a | { |
---|
42 | n/a | #if defined(MS_WINDOWS) |
---|
43 | n/a | UINT cp; |
---|
44 | n/a | #endif |
---|
45 | n/a | int valid; |
---|
46 | n/a | _Py_BEGIN_SUPPRESS_IPH |
---|
47 | n/a | valid = isatty(fd); |
---|
48 | n/a | _Py_END_SUPPRESS_IPH |
---|
49 | n/a | if (!valid) |
---|
50 | n/a | Py_RETURN_NONE; |
---|
51 | n/a | |
---|
52 | n/a | #if defined(MS_WINDOWS) |
---|
53 | n/a | if (fd == 0) |
---|
54 | n/a | cp = GetConsoleCP(); |
---|
55 | n/a | else if (fd == 1 || fd == 2) |
---|
56 | n/a | cp = GetConsoleOutputCP(); |
---|
57 | n/a | else |
---|
58 | n/a | cp = 0; |
---|
59 | n/a | /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application |
---|
60 | n/a | has no console */ |
---|
61 | n/a | if (cp != 0) |
---|
62 | n/a | return PyUnicode_FromFormat("cp%u", (unsigned int)cp); |
---|
63 | n/a | #elif defined(CODESET) |
---|
64 | n/a | { |
---|
65 | n/a | char *codeset = nl_langinfo(CODESET); |
---|
66 | n/a | if (codeset != NULL && codeset[0] != 0) |
---|
67 | n/a | return PyUnicode_FromString(codeset); |
---|
68 | n/a | } |
---|
69 | n/a | #endif |
---|
70 | n/a | Py_RETURN_NONE; |
---|
71 | n/a | } |
---|
72 | n/a | |
---|
73 | n/a | #if !defined(__APPLE__) && !defined(MS_WINDOWS) |
---|
74 | n/a | extern int _Py_normalize_encoding(const char *, char *, size_t); |
---|
75 | n/a | |
---|
76 | n/a | /* Workaround FreeBSD and OpenIndiana locale encoding issue with the C locale. |
---|
77 | n/a | On these operating systems, nl_langinfo(CODESET) announces an alias of the |
---|
78 | n/a | ASCII encoding, whereas mbstowcs() and wcstombs() functions use the |
---|
79 | n/a | ISO-8859-1 encoding. The problem is that os.fsencode() and os.fsdecode() use |
---|
80 | n/a | locale.getpreferredencoding() codec. For example, if command line arguments |
---|
81 | n/a | are decoded by mbstowcs() and encoded back by os.fsencode(), we get a |
---|
82 | n/a | UnicodeEncodeError instead of retrieving the original byte string. |
---|
83 | n/a | |
---|
84 | n/a | The workaround is enabled if setlocale(LC_CTYPE, NULL) returns "C", |
---|
85 | n/a | nl_langinfo(CODESET) announces "ascii" (or an alias to ASCII), and at least |
---|
86 | n/a | one byte in range 0x80-0xff can be decoded from the locale encoding. The |
---|
87 | n/a | workaround is also enabled on error, for example if getting the locale |
---|
88 | n/a | failed. |
---|
89 | n/a | |
---|
90 | n/a | Values of force_ascii: |
---|
91 | n/a | |
---|
92 | n/a | 1: the workaround is used: Py_EncodeLocale() uses |
---|
93 | n/a | encode_ascii_surrogateescape() and Py_DecodeLocale() uses |
---|
94 | n/a | decode_ascii_surrogateescape() |
---|
95 | n/a | 0: the workaround is not used: Py_EncodeLocale() uses wcstombs() and |
---|
96 | n/a | Py_DecodeLocale() uses mbstowcs() |
---|
97 | n/a | -1: unknown, need to call check_force_ascii() to get the value |
---|
98 | n/a | */ |
---|
99 | n/a | static int force_ascii = -1; |
---|
100 | n/a | |
---|
101 | n/a | static int |
---|
102 | n/a | check_force_ascii(void) |
---|
103 | n/a | { |
---|
104 | n/a | char *loc; |
---|
105 | n/a | #if defined(HAVE_LANGINFO_H) && defined(CODESET) |
---|
106 | n/a | char *codeset, **alias; |
---|
107 | n/a | char encoding[20]; /* longest name: "iso_646.irv_1991\0" */ |
---|
108 | n/a | int is_ascii; |
---|
109 | n/a | unsigned int i; |
---|
110 | n/a | char* ascii_aliases[] = { |
---|
111 | n/a | "ascii", |
---|
112 | n/a | /* Aliases from Lib/encodings/aliases.py */ |
---|
113 | n/a | "646", |
---|
114 | n/a | "ansi_x3.4_1968", |
---|
115 | n/a | "ansi_x3.4_1986", |
---|
116 | n/a | "ansi_x3_4_1968", |
---|
117 | n/a | "cp367", |
---|
118 | n/a | "csascii", |
---|
119 | n/a | "ibm367", |
---|
120 | n/a | "iso646_us", |
---|
121 | n/a | "iso_646.irv_1991", |
---|
122 | n/a | "iso_ir_6", |
---|
123 | n/a | "us", |
---|
124 | n/a | "us_ascii", |
---|
125 | n/a | NULL |
---|
126 | n/a | }; |
---|
127 | n/a | #endif |
---|
128 | n/a | |
---|
129 | n/a | loc = setlocale(LC_CTYPE, NULL); |
---|
130 | n/a | if (loc == NULL) |
---|
131 | n/a | goto error; |
---|
132 | n/a | if (strcmp(loc, "C") != 0) { |
---|
133 | n/a | /* the LC_CTYPE locale is different than C */ |
---|
134 | n/a | return 0; |
---|
135 | n/a | } |
---|
136 | n/a | |
---|
137 | n/a | #if defined(HAVE_LANGINFO_H) && defined(CODESET) |
---|
138 | n/a | codeset = nl_langinfo(CODESET); |
---|
139 | n/a | if (!codeset || codeset[0] == '\0') { |
---|
140 | n/a | /* CODESET is not set or empty */ |
---|
141 | n/a | goto error; |
---|
142 | n/a | } |
---|
143 | n/a | if (!_Py_normalize_encoding(codeset, encoding, sizeof(encoding))) |
---|
144 | n/a | goto error; |
---|
145 | n/a | |
---|
146 | n/a | is_ascii = 0; |
---|
147 | n/a | for (alias=ascii_aliases; *alias != NULL; alias++) { |
---|
148 | n/a | if (strcmp(encoding, *alias) == 0) { |
---|
149 | n/a | is_ascii = 1; |
---|
150 | n/a | break; |
---|
151 | n/a | } |
---|
152 | n/a | } |
---|
153 | n/a | if (!is_ascii) { |
---|
154 | n/a | /* nl_langinfo(CODESET) is not "ascii" or an alias of ASCII */ |
---|
155 | n/a | return 0; |
---|
156 | n/a | } |
---|
157 | n/a | |
---|
158 | n/a | for (i=0x80; i<0xff; i++) { |
---|
159 | n/a | unsigned char ch; |
---|
160 | n/a | wchar_t wch; |
---|
161 | n/a | size_t res; |
---|
162 | n/a | |
---|
163 | n/a | ch = (unsigned char)i; |
---|
164 | n/a | res = mbstowcs(&wch, (char*)&ch, 1); |
---|
165 | n/a | if (res != (size_t)-1) { |
---|
166 | n/a | /* decoding a non-ASCII character from the locale encoding succeed: |
---|
167 | n/a | the locale encoding is not ASCII, force ASCII */ |
---|
168 | n/a | return 1; |
---|
169 | n/a | } |
---|
170 | n/a | } |
---|
171 | n/a | /* None of the bytes in the range 0x80-0xff can be decoded from the locale |
---|
172 | n/a | encoding: the locale encoding is really ASCII */ |
---|
173 | n/a | return 0; |
---|
174 | n/a | #else |
---|
175 | n/a | /* nl_langinfo(CODESET) is not available: always force ASCII */ |
---|
176 | n/a | return 1; |
---|
177 | n/a | #endif |
---|
178 | n/a | |
---|
179 | n/a | error: |
---|
180 | n/a | /* if an error occurred, force the ASCII encoding */ |
---|
181 | n/a | return 1; |
---|
182 | n/a | } |
---|
183 | n/a | |
---|
184 | n/a | static char* |
---|
185 | n/a | encode_ascii_surrogateescape(const wchar_t *text, size_t *error_pos) |
---|
186 | n/a | { |
---|
187 | n/a | char *result = NULL, *out; |
---|
188 | n/a | size_t len, i; |
---|
189 | n/a | wchar_t ch; |
---|
190 | n/a | |
---|
191 | n/a | if (error_pos != NULL) |
---|
192 | n/a | *error_pos = (size_t)-1; |
---|
193 | n/a | |
---|
194 | n/a | len = wcslen(text); |
---|
195 | n/a | |
---|
196 | n/a | result = PyMem_Malloc(len + 1); /* +1 for NUL byte */ |
---|
197 | n/a | if (result == NULL) |
---|
198 | n/a | return NULL; |
---|
199 | n/a | |
---|
200 | n/a | out = result; |
---|
201 | n/a | for (i=0; i<len; i++) { |
---|
202 | n/a | ch = text[i]; |
---|
203 | n/a | |
---|
204 | n/a | if (ch <= 0x7f) { |
---|
205 | n/a | /* ASCII character */ |
---|
206 | n/a | *out++ = (char)ch; |
---|
207 | n/a | } |
---|
208 | n/a | else if (0xdc80 <= ch && ch <= 0xdcff) { |
---|
209 | n/a | /* UTF-8b surrogate */ |
---|
210 | n/a | *out++ = (char)(ch - 0xdc00); |
---|
211 | n/a | } |
---|
212 | n/a | else { |
---|
213 | n/a | if (error_pos != NULL) |
---|
214 | n/a | *error_pos = i; |
---|
215 | n/a | PyMem_Free(result); |
---|
216 | n/a | return NULL; |
---|
217 | n/a | } |
---|
218 | n/a | } |
---|
219 | n/a | *out = '\0'; |
---|
220 | n/a | return result; |
---|
221 | n/a | } |
---|
222 | n/a | #endif /* !defined(__APPLE__) && !defined(MS_WINDOWS) */ |
---|
223 | n/a | |
---|
224 | n/a | #if !defined(__APPLE__) && (!defined(MS_WINDOWS) || !defined(HAVE_MBRTOWC)) |
---|
225 | n/a | static wchar_t* |
---|
226 | n/a | decode_ascii_surrogateescape(const char *arg, size_t *size) |
---|
227 | n/a | { |
---|
228 | n/a | wchar_t *res; |
---|
229 | n/a | unsigned char *in; |
---|
230 | n/a | wchar_t *out; |
---|
231 | n/a | size_t argsize = strlen(arg) + 1; |
---|
232 | n/a | |
---|
233 | n/a | if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t)) |
---|
234 | n/a | return NULL; |
---|
235 | n/a | res = PyMem_RawMalloc(argsize*sizeof(wchar_t)); |
---|
236 | n/a | if (!res) |
---|
237 | n/a | return NULL; |
---|
238 | n/a | |
---|
239 | n/a | in = (unsigned char*)arg; |
---|
240 | n/a | out = res; |
---|
241 | n/a | while(*in) |
---|
242 | n/a | if(*in < 128) |
---|
243 | n/a | *out++ = *in++; |
---|
244 | n/a | else |
---|
245 | n/a | *out++ = 0xdc00 + *in++; |
---|
246 | n/a | *out = 0; |
---|
247 | n/a | if (size != NULL) |
---|
248 | n/a | *size = out - res; |
---|
249 | n/a | return res; |
---|
250 | n/a | } |
---|
251 | n/a | #endif |
---|
252 | n/a | |
---|
253 | n/a | |
---|
254 | n/a | /* Decode a byte string from the locale encoding with the |
---|
255 | n/a | surrogateescape error handler: undecodable bytes are decoded as characters |
---|
256 | n/a | in range U+DC80..U+DCFF. If a byte sequence can be decoded as a surrogate |
---|
257 | n/a | character, escape the bytes using the surrogateescape error handler instead |
---|
258 | n/a | of decoding them. |
---|
259 | n/a | |
---|
260 | n/a | Return a pointer to a newly allocated wide character string, use |
---|
261 | n/a | PyMem_RawFree() to free the memory. If size is not NULL, write the number of |
---|
262 | n/a | wide characters excluding the null character into *size |
---|
263 | n/a | |
---|
264 | n/a | Return NULL on decoding error or memory allocation error. If *size* is not |
---|
265 | n/a | NULL, *size is set to (size_t)-1 on memory error or set to (size_t)-2 on |
---|
266 | n/a | decoding error. |
---|
267 | n/a | |
---|
268 | n/a | Decoding errors should never happen, unless there is a bug in the C |
---|
269 | n/a | library. |
---|
270 | n/a | |
---|
271 | n/a | Use the Py_EncodeLocale() function to encode the character string back to a |
---|
272 | n/a | byte string. */ |
---|
273 | n/a | wchar_t* |
---|
274 | n/a | Py_DecodeLocale(const char* arg, size_t *size) |
---|
275 | n/a | { |
---|
276 | n/a | #if defined(__APPLE__) || defined(__ANDROID__) |
---|
277 | n/a | wchar_t *wstr; |
---|
278 | n/a | wstr = _Py_DecodeUTF8_surrogateescape(arg, strlen(arg)); |
---|
279 | n/a | if (size != NULL) { |
---|
280 | n/a | if (wstr != NULL) |
---|
281 | n/a | *size = wcslen(wstr); |
---|
282 | n/a | else |
---|
283 | n/a | *size = (size_t)-1; |
---|
284 | n/a | } |
---|
285 | n/a | return wstr; |
---|
286 | n/a | #else |
---|
287 | n/a | wchar_t *res; |
---|
288 | n/a | size_t argsize; |
---|
289 | n/a | size_t count; |
---|
290 | n/a | #ifdef HAVE_MBRTOWC |
---|
291 | n/a | unsigned char *in; |
---|
292 | n/a | wchar_t *out; |
---|
293 | n/a | mbstate_t mbs; |
---|
294 | n/a | #endif |
---|
295 | n/a | |
---|
296 | n/a | #ifndef MS_WINDOWS |
---|
297 | n/a | if (force_ascii == -1) |
---|
298 | n/a | force_ascii = check_force_ascii(); |
---|
299 | n/a | |
---|
300 | n/a | if (force_ascii) { |
---|
301 | n/a | /* force ASCII encoding to workaround mbstowcs() issue */ |
---|
302 | n/a | res = decode_ascii_surrogateescape(arg, size); |
---|
303 | n/a | if (res == NULL) |
---|
304 | n/a | goto oom; |
---|
305 | n/a | return res; |
---|
306 | n/a | } |
---|
307 | n/a | #endif |
---|
308 | n/a | |
---|
309 | n/a | #ifdef HAVE_BROKEN_MBSTOWCS |
---|
310 | n/a | /* Some platforms have a broken implementation of |
---|
311 | n/a | * mbstowcs which does not count the characters that |
---|
312 | n/a | * would result from conversion. Use an upper bound. |
---|
313 | n/a | */ |
---|
314 | n/a | argsize = strlen(arg); |
---|
315 | n/a | #else |
---|
316 | n/a | argsize = mbstowcs(NULL, arg, 0); |
---|
317 | n/a | #endif |
---|
318 | n/a | if (argsize != (size_t)-1) { |
---|
319 | n/a | if (argsize == PY_SSIZE_T_MAX) |
---|
320 | n/a | goto oom; |
---|
321 | n/a | argsize += 1; |
---|
322 | n/a | if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t)) |
---|
323 | n/a | goto oom; |
---|
324 | n/a | res = (wchar_t *)PyMem_RawMalloc(argsize*sizeof(wchar_t)); |
---|
325 | n/a | if (!res) |
---|
326 | n/a | goto oom; |
---|
327 | n/a | count = mbstowcs(res, arg, argsize); |
---|
328 | n/a | if (count != (size_t)-1) { |
---|
329 | n/a | wchar_t *tmp; |
---|
330 | n/a | /* Only use the result if it contains no |
---|
331 | n/a | surrogate characters. */ |
---|
332 | n/a | for (tmp = res; *tmp != 0 && |
---|
333 | n/a | !Py_UNICODE_IS_SURROGATE(*tmp); tmp++) |
---|
334 | n/a | ; |
---|
335 | n/a | if (*tmp == 0) { |
---|
336 | n/a | if (size != NULL) |
---|
337 | n/a | *size = count; |
---|
338 | n/a | return res; |
---|
339 | n/a | } |
---|
340 | n/a | } |
---|
341 | n/a | PyMem_RawFree(res); |
---|
342 | n/a | } |
---|
343 | n/a | /* Conversion failed. Fall back to escaping with surrogateescape. */ |
---|
344 | n/a | #ifdef HAVE_MBRTOWC |
---|
345 | n/a | /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */ |
---|
346 | n/a | |
---|
347 | n/a | /* Overallocate; as multi-byte characters are in the argument, the |
---|
348 | n/a | actual output could use less memory. */ |
---|
349 | n/a | argsize = strlen(arg) + 1; |
---|
350 | n/a | if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t)) |
---|
351 | n/a | goto oom; |
---|
352 | n/a | res = (wchar_t*)PyMem_RawMalloc(argsize*sizeof(wchar_t)); |
---|
353 | n/a | if (!res) |
---|
354 | n/a | goto oom; |
---|
355 | n/a | in = (unsigned char*)arg; |
---|
356 | n/a | out = res; |
---|
357 | n/a | memset(&mbs, 0, sizeof mbs); |
---|
358 | n/a | while (argsize) { |
---|
359 | n/a | size_t converted = mbrtowc(out, (char*)in, argsize, &mbs); |
---|
360 | n/a | if (converted == 0) |
---|
361 | n/a | /* Reached end of string; null char stored. */ |
---|
362 | n/a | break; |
---|
363 | n/a | if (converted == (size_t)-2) { |
---|
364 | n/a | /* Incomplete character. This should never happen, |
---|
365 | n/a | since we provide everything that we have - |
---|
366 | n/a | unless there is a bug in the C library, or I |
---|
367 | n/a | misunderstood how mbrtowc works. */ |
---|
368 | n/a | PyMem_RawFree(res); |
---|
369 | n/a | if (size != NULL) |
---|
370 | n/a | *size = (size_t)-2; |
---|
371 | n/a | return NULL; |
---|
372 | n/a | } |
---|
373 | n/a | if (converted == (size_t)-1) { |
---|
374 | n/a | /* Conversion error. Escape as UTF-8b, and start over |
---|
375 | n/a | in the initial shift state. */ |
---|
376 | n/a | *out++ = 0xdc00 + *in++; |
---|
377 | n/a | argsize--; |
---|
378 | n/a | memset(&mbs, 0, sizeof mbs); |
---|
379 | n/a | continue; |
---|
380 | n/a | } |
---|
381 | n/a | if (Py_UNICODE_IS_SURROGATE(*out)) { |
---|
382 | n/a | /* Surrogate character. Escape the original |
---|
383 | n/a | byte sequence with surrogateescape. */ |
---|
384 | n/a | argsize -= converted; |
---|
385 | n/a | while (converted--) |
---|
386 | n/a | *out++ = 0xdc00 + *in++; |
---|
387 | n/a | continue; |
---|
388 | n/a | } |
---|
389 | n/a | /* successfully converted some bytes */ |
---|
390 | n/a | in += converted; |
---|
391 | n/a | argsize -= converted; |
---|
392 | n/a | out++; |
---|
393 | n/a | } |
---|
394 | n/a | if (size != NULL) |
---|
395 | n/a | *size = out - res; |
---|
396 | n/a | #else /* HAVE_MBRTOWC */ |
---|
397 | n/a | /* Cannot use C locale for escaping; manually escape as if charset |
---|
398 | n/a | is ASCII (i.e. escape all bytes > 128. This will still roundtrip |
---|
399 | n/a | correctly in the locale's charset, which must be an ASCII superset. */ |
---|
400 | n/a | res = decode_ascii_surrogateescape(arg, size); |
---|
401 | n/a | if (res == NULL) |
---|
402 | n/a | goto oom; |
---|
403 | n/a | #endif /* HAVE_MBRTOWC */ |
---|
404 | n/a | return res; |
---|
405 | n/a | oom: |
---|
406 | n/a | if (size != NULL) |
---|
407 | n/a | *size = (size_t)-1; |
---|
408 | n/a | return NULL; |
---|
409 | n/a | #endif /* __APPLE__ or __ANDROID__ */ |
---|
410 | n/a | } |
---|
411 | n/a | |
---|
412 | n/a | /* Encode a wide character string to the locale encoding with the |
---|
413 | n/a | surrogateescape error handler: surrogate characters in the range |
---|
414 | n/a | U+DC80..U+DCFF are converted to bytes 0x80..0xFF. |
---|
415 | n/a | |
---|
416 | n/a | Return a pointer to a newly allocated byte string, use PyMem_Free() to free |
---|
417 | n/a | the memory. Return NULL on encoding or memory allocation error. |
---|
418 | n/a | |
---|
419 | n/a | If error_pos is not NULL, *error_pos is set to the index of the invalid |
---|
420 | n/a | character on encoding error, or set to (size_t)-1 otherwise. |
---|
421 | n/a | |
---|
422 | n/a | Use the Py_DecodeLocale() function to decode the bytes string back to a wide |
---|
423 | n/a | character string. */ |
---|
424 | n/a | char* |
---|
425 | n/a | Py_EncodeLocale(const wchar_t *text, size_t *error_pos) |
---|
426 | n/a | { |
---|
427 | n/a | #if defined(__APPLE__) || defined(__ANDROID__) |
---|
428 | n/a | Py_ssize_t len; |
---|
429 | n/a | PyObject *unicode, *bytes = NULL; |
---|
430 | n/a | char *cpath; |
---|
431 | n/a | |
---|
432 | n/a | unicode = PyUnicode_FromWideChar(text, wcslen(text)); |
---|
433 | n/a | if (unicode == NULL) |
---|
434 | n/a | return NULL; |
---|
435 | n/a | |
---|
436 | n/a | bytes = _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
---|
437 | n/a | Py_DECREF(unicode); |
---|
438 | n/a | if (bytes == NULL) { |
---|
439 | n/a | PyErr_Clear(); |
---|
440 | n/a | if (error_pos != NULL) |
---|
441 | n/a | *error_pos = (size_t)-1; |
---|
442 | n/a | return NULL; |
---|
443 | n/a | } |
---|
444 | n/a | |
---|
445 | n/a | len = PyBytes_GET_SIZE(bytes); |
---|
446 | n/a | cpath = PyMem_Malloc(len+1); |
---|
447 | n/a | if (cpath == NULL) { |
---|
448 | n/a | PyErr_Clear(); |
---|
449 | n/a | Py_DECREF(bytes); |
---|
450 | n/a | if (error_pos != NULL) |
---|
451 | n/a | *error_pos = (size_t)-1; |
---|
452 | n/a | return NULL; |
---|
453 | n/a | } |
---|
454 | n/a | memcpy(cpath, PyBytes_AsString(bytes), len + 1); |
---|
455 | n/a | Py_DECREF(bytes); |
---|
456 | n/a | return cpath; |
---|
457 | n/a | #else /* __APPLE__ */ |
---|
458 | n/a | const size_t len = wcslen(text); |
---|
459 | n/a | char *result = NULL, *bytes = NULL; |
---|
460 | n/a | size_t i, size, converted; |
---|
461 | n/a | wchar_t c, buf[2]; |
---|
462 | n/a | |
---|
463 | n/a | #ifndef MS_WINDOWS |
---|
464 | n/a | if (force_ascii == -1) |
---|
465 | n/a | force_ascii = check_force_ascii(); |
---|
466 | n/a | |
---|
467 | n/a | if (force_ascii) |
---|
468 | n/a | return encode_ascii_surrogateescape(text, error_pos); |
---|
469 | n/a | #endif |
---|
470 | n/a | |
---|
471 | n/a | /* The function works in two steps: |
---|
472 | n/a | 1. compute the length of the output buffer in bytes (size) |
---|
473 | n/a | 2. outputs the bytes */ |
---|
474 | n/a | size = 0; |
---|
475 | n/a | buf[1] = 0; |
---|
476 | n/a | while (1) { |
---|
477 | n/a | for (i=0; i < len; i++) { |
---|
478 | n/a | c = text[i]; |
---|
479 | n/a | if (c >= 0xdc80 && c <= 0xdcff) { |
---|
480 | n/a | /* UTF-8b surrogate */ |
---|
481 | n/a | if (bytes != NULL) { |
---|
482 | n/a | *bytes++ = c - 0xdc00; |
---|
483 | n/a | size--; |
---|
484 | n/a | } |
---|
485 | n/a | else |
---|
486 | n/a | size++; |
---|
487 | n/a | continue; |
---|
488 | n/a | } |
---|
489 | n/a | else { |
---|
490 | n/a | buf[0] = c; |
---|
491 | n/a | if (bytes != NULL) |
---|
492 | n/a | converted = wcstombs(bytes, buf, size); |
---|
493 | n/a | else |
---|
494 | n/a | converted = wcstombs(NULL, buf, 0); |
---|
495 | n/a | if (converted == (size_t)-1) { |
---|
496 | n/a | if (result != NULL) |
---|
497 | n/a | PyMem_Free(result); |
---|
498 | n/a | if (error_pos != NULL) |
---|
499 | n/a | *error_pos = i; |
---|
500 | n/a | return NULL; |
---|
501 | n/a | } |
---|
502 | n/a | if (bytes != NULL) { |
---|
503 | n/a | bytes += converted; |
---|
504 | n/a | size -= converted; |
---|
505 | n/a | } |
---|
506 | n/a | else |
---|
507 | n/a | size += converted; |
---|
508 | n/a | } |
---|
509 | n/a | } |
---|
510 | n/a | if (result != NULL) { |
---|
511 | n/a | *bytes = '\0'; |
---|
512 | n/a | break; |
---|
513 | n/a | } |
---|
514 | n/a | |
---|
515 | n/a | size += 1; /* nul byte at the end */ |
---|
516 | n/a | result = PyMem_Malloc(size); |
---|
517 | n/a | if (result == NULL) { |
---|
518 | n/a | if (error_pos != NULL) |
---|
519 | n/a | *error_pos = (size_t)-1; |
---|
520 | n/a | return NULL; |
---|
521 | n/a | } |
---|
522 | n/a | bytes = result; |
---|
523 | n/a | } |
---|
524 | n/a | return result; |
---|
525 | n/a | #endif /* __APPLE__ or __ANDROID__ */ |
---|
526 | n/a | } |
---|
527 | n/a | |
---|
528 | n/a | |
---|
529 | n/a | #ifdef MS_WINDOWS |
---|
530 | n/a | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
---|
531 | n/a | |
---|
532 | n/a | static void |
---|
533 | n/a | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out) |
---|
534 | n/a | { |
---|
535 | n/a | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
---|
536 | n/a | /* Cannot simply cast and dereference in_ptr, |
---|
537 | n/a | since it might not be aligned properly */ |
---|
538 | n/a | __int64 in; |
---|
539 | n/a | memcpy(&in, in_ptr, sizeof(in)); |
---|
540 | n/a | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
---|
541 | n/a | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t); |
---|
542 | n/a | } |
---|
543 | n/a | |
---|
544 | n/a | void |
---|
545 | n/a | _Py_time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr) |
---|
546 | n/a | { |
---|
547 | n/a | /* XXX endianness */ |
---|
548 | n/a | __int64 out; |
---|
549 | n/a | out = time_in + secs_between_epochs; |
---|
550 | n/a | out = out * 10000000 + nsec_in / 100; |
---|
551 | n/a | memcpy(out_ptr, &out, sizeof(out)); |
---|
552 | n/a | } |
---|
553 | n/a | |
---|
554 | n/a | /* Below, we *know* that ugo+r is 0444 */ |
---|
555 | n/a | #if _S_IREAD != 0400 |
---|
556 | n/a | #error Unsupported C library |
---|
557 | n/a | #endif |
---|
558 | n/a | static int |
---|
559 | n/a | attributes_to_mode(DWORD attr) |
---|
560 | n/a | { |
---|
561 | n/a | int m = 0; |
---|
562 | n/a | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
---|
563 | n/a | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
---|
564 | n/a | else |
---|
565 | n/a | m |= _S_IFREG; |
---|
566 | n/a | if (attr & FILE_ATTRIBUTE_READONLY) |
---|
567 | n/a | m |= 0444; |
---|
568 | n/a | else |
---|
569 | n/a | m |= 0666; |
---|
570 | n/a | return m; |
---|
571 | n/a | } |
---|
572 | n/a | |
---|
573 | n/a | void |
---|
574 | n/a | _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, |
---|
575 | n/a | struct _Py_stat_struct *result) |
---|
576 | n/a | { |
---|
577 | n/a | memset(result, 0, sizeof(*result)); |
---|
578 | n/a | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
---|
579 | n/a | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
---|
580 | n/a | result->st_dev = info->dwVolumeSerialNumber; |
---|
581 | n/a | result->st_rdev = result->st_dev; |
---|
582 | n/a | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
---|
583 | n/a | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
---|
584 | n/a | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
---|
585 | n/a | result->st_nlink = info->nNumberOfLinks; |
---|
586 | n/a | result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow; |
---|
587 | n/a | if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { |
---|
588 | n/a | /* first clear the S_IFMT bits */ |
---|
589 | n/a | result->st_mode ^= (result->st_mode & S_IFMT); |
---|
590 | n/a | /* now set the bits that make this a symlink */ |
---|
591 | n/a | result->st_mode |= S_IFLNK; |
---|
592 | n/a | } |
---|
593 | n/a | result->st_file_attributes = info->dwFileAttributes; |
---|
594 | n/a | } |
---|
595 | n/a | #endif |
---|
596 | n/a | |
---|
597 | n/a | /* Return information about a file. |
---|
598 | n/a | |
---|
599 | n/a | On POSIX, use fstat(). |
---|
600 | n/a | |
---|
601 | n/a | On Windows, use GetFileType() and GetFileInformationByHandle() which support |
---|
602 | n/a | files larger than 2 GB. fstat() may fail with EOVERFLOW on files larger |
---|
603 | n/a | than 2 GB because the file size type is a signed 32-bit integer: see issue |
---|
604 | n/a | #23152. |
---|
605 | n/a | |
---|
606 | n/a | On Windows, set the last Windows error and return nonzero on error. On |
---|
607 | n/a | POSIX, set errno and return nonzero on error. Fill status and return 0 on |
---|
608 | n/a | success. */ |
---|
609 | n/a | int |
---|
610 | n/a | _Py_fstat_noraise(int fd, struct _Py_stat_struct *status) |
---|
611 | n/a | { |
---|
612 | n/a | #ifdef MS_WINDOWS |
---|
613 | n/a | BY_HANDLE_FILE_INFORMATION info; |
---|
614 | n/a | HANDLE h; |
---|
615 | n/a | int type; |
---|
616 | n/a | |
---|
617 | n/a | _Py_BEGIN_SUPPRESS_IPH |
---|
618 | n/a | h = (HANDLE)_get_osfhandle(fd); |
---|
619 | n/a | _Py_END_SUPPRESS_IPH |
---|
620 | n/a | |
---|
621 | n/a | if (h == INVALID_HANDLE_VALUE) { |
---|
622 | n/a | /* errno is already set by _get_osfhandle, but we also set |
---|
623 | n/a | the Win32 error for callers who expect that */ |
---|
624 | n/a | SetLastError(ERROR_INVALID_HANDLE); |
---|
625 | n/a | return -1; |
---|
626 | n/a | } |
---|
627 | n/a | memset(status, 0, sizeof(*status)); |
---|
628 | n/a | |
---|
629 | n/a | type = GetFileType(h); |
---|
630 | n/a | if (type == FILE_TYPE_UNKNOWN) { |
---|
631 | n/a | DWORD error = GetLastError(); |
---|
632 | n/a | if (error != 0) { |
---|
633 | n/a | errno = winerror_to_errno(error); |
---|
634 | n/a | return -1; |
---|
635 | n/a | } |
---|
636 | n/a | /* else: valid but unknown file */ |
---|
637 | n/a | } |
---|
638 | n/a | |
---|
639 | n/a | if (type != FILE_TYPE_DISK) { |
---|
640 | n/a | if (type == FILE_TYPE_CHAR) |
---|
641 | n/a | status->st_mode = _S_IFCHR; |
---|
642 | n/a | else if (type == FILE_TYPE_PIPE) |
---|
643 | n/a | status->st_mode = _S_IFIFO; |
---|
644 | n/a | return 0; |
---|
645 | n/a | } |
---|
646 | n/a | |
---|
647 | n/a | if (!GetFileInformationByHandle(h, &info)) { |
---|
648 | n/a | /* The Win32 error is already set, but we also set errno for |
---|
649 | n/a | callers who expect it */ |
---|
650 | n/a | errno = winerror_to_errno(GetLastError()); |
---|
651 | n/a | return -1; |
---|
652 | n/a | } |
---|
653 | n/a | |
---|
654 | n/a | _Py_attribute_data_to_stat(&info, 0, status); |
---|
655 | n/a | /* specific to fstat() */ |
---|
656 | n/a | status->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
---|
657 | n/a | return 0; |
---|
658 | n/a | #else |
---|
659 | n/a | return fstat(fd, status); |
---|
660 | n/a | #endif |
---|
661 | n/a | } |
---|
662 | n/a | |
---|
663 | n/a | /* Return information about a file. |
---|
664 | n/a | |
---|
665 | n/a | On POSIX, use fstat(). |
---|
666 | n/a | |
---|
667 | n/a | On Windows, use GetFileType() and GetFileInformationByHandle() which support |
---|
668 | n/a | files larger than 2 GB. fstat() may fail with EOVERFLOW on files larger |
---|
669 | n/a | than 2 GB because the file size type is a signed 32-bit integer: see issue |
---|
670 | n/a | #23152. |
---|
671 | n/a | |
---|
672 | n/a | Raise an exception and return -1 on error. On Windows, set the last Windows |
---|
673 | n/a | error on error. On POSIX, set errno on error. Fill status and return 0 on |
---|
674 | n/a | success. |
---|
675 | n/a | |
---|
676 | n/a | Release the GIL to call GetFileType() and GetFileInformationByHandle(), or |
---|
677 | n/a | to call fstat(). The caller must hold the GIL. */ |
---|
678 | n/a | int |
---|
679 | n/a | _Py_fstat(int fd, struct _Py_stat_struct *status) |
---|
680 | n/a | { |
---|
681 | n/a | int res; |
---|
682 | n/a | |
---|
683 | n/a | #ifdef WITH_THREAD |
---|
684 | n/a | assert(PyGILState_Check()); |
---|
685 | n/a | #endif |
---|
686 | n/a | |
---|
687 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
688 | n/a | res = _Py_fstat_noraise(fd, status); |
---|
689 | n/a | Py_END_ALLOW_THREADS |
---|
690 | n/a | |
---|
691 | n/a | if (res != 0) { |
---|
692 | n/a | #ifdef MS_WINDOWS |
---|
693 | n/a | PyErr_SetFromWindowsErr(0); |
---|
694 | n/a | #else |
---|
695 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
696 | n/a | #endif |
---|
697 | n/a | return -1; |
---|
698 | n/a | } |
---|
699 | n/a | return 0; |
---|
700 | n/a | } |
---|
701 | n/a | |
---|
702 | n/a | /* Call _wstat() on Windows, or encode the path to the filesystem encoding and |
---|
703 | n/a | call stat() otherwise. Only fill st_mode attribute on Windows. |
---|
704 | n/a | |
---|
705 | n/a | Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception was |
---|
706 | n/a | raised. */ |
---|
707 | n/a | |
---|
708 | n/a | int |
---|
709 | n/a | _Py_stat(PyObject *path, struct stat *statbuf) |
---|
710 | n/a | { |
---|
711 | n/a | #ifdef MS_WINDOWS |
---|
712 | n/a | int err; |
---|
713 | n/a | struct _stat wstatbuf; |
---|
714 | n/a | wchar_t *wpath; |
---|
715 | n/a | |
---|
716 | n/a | wpath = PyUnicode_AsUnicode(path); |
---|
717 | n/a | if (wpath == NULL) |
---|
718 | n/a | return -2; |
---|
719 | n/a | err = _wstat(wpath, &wstatbuf); |
---|
720 | n/a | if (!err) |
---|
721 | n/a | statbuf->st_mode = wstatbuf.st_mode; |
---|
722 | n/a | return err; |
---|
723 | n/a | #else |
---|
724 | n/a | int ret; |
---|
725 | n/a | PyObject *bytes = PyUnicode_EncodeFSDefault(path); |
---|
726 | n/a | if (bytes == NULL) |
---|
727 | n/a | return -2; |
---|
728 | n/a | ret = stat(PyBytes_AS_STRING(bytes), statbuf); |
---|
729 | n/a | Py_DECREF(bytes); |
---|
730 | n/a | return ret; |
---|
731 | n/a | #endif |
---|
732 | n/a | } |
---|
733 | n/a | |
---|
734 | n/a | |
---|
735 | n/a | static int |
---|
736 | n/a | get_inheritable(int fd, int raise) |
---|
737 | n/a | { |
---|
738 | n/a | #ifdef MS_WINDOWS |
---|
739 | n/a | HANDLE handle; |
---|
740 | n/a | DWORD flags; |
---|
741 | n/a | |
---|
742 | n/a | _Py_BEGIN_SUPPRESS_IPH |
---|
743 | n/a | handle = (HANDLE)_get_osfhandle(fd); |
---|
744 | n/a | _Py_END_SUPPRESS_IPH |
---|
745 | n/a | if (handle == INVALID_HANDLE_VALUE) { |
---|
746 | n/a | if (raise) |
---|
747 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
748 | n/a | return -1; |
---|
749 | n/a | } |
---|
750 | n/a | |
---|
751 | n/a | if (!GetHandleInformation(handle, &flags)) { |
---|
752 | n/a | if (raise) |
---|
753 | n/a | PyErr_SetFromWindowsErr(0); |
---|
754 | n/a | return -1; |
---|
755 | n/a | } |
---|
756 | n/a | |
---|
757 | n/a | return (flags & HANDLE_FLAG_INHERIT); |
---|
758 | n/a | #else |
---|
759 | n/a | int flags; |
---|
760 | n/a | |
---|
761 | n/a | flags = fcntl(fd, F_GETFD, 0); |
---|
762 | n/a | if (flags == -1) { |
---|
763 | n/a | if (raise) |
---|
764 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
765 | n/a | return -1; |
---|
766 | n/a | } |
---|
767 | n/a | return !(flags & FD_CLOEXEC); |
---|
768 | n/a | #endif |
---|
769 | n/a | } |
---|
770 | n/a | |
---|
771 | n/a | /* Get the inheritable flag of the specified file descriptor. |
---|
772 | n/a | Return 1 if the file descriptor can be inherited, 0 if it cannot, |
---|
773 | n/a | raise an exception and return -1 on error. */ |
---|
774 | n/a | int |
---|
775 | n/a | _Py_get_inheritable(int fd) |
---|
776 | n/a | { |
---|
777 | n/a | return get_inheritable(fd, 1); |
---|
778 | n/a | } |
---|
779 | n/a | |
---|
780 | n/a | static int |
---|
781 | n/a | set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works) |
---|
782 | n/a | { |
---|
783 | n/a | #ifdef MS_WINDOWS |
---|
784 | n/a | HANDLE handle; |
---|
785 | n/a | DWORD flags; |
---|
786 | n/a | #else |
---|
787 | n/a | #if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX) |
---|
788 | n/a | static int ioctl_works = -1; |
---|
789 | n/a | int request; |
---|
790 | n/a | int err; |
---|
791 | n/a | #endif |
---|
792 | n/a | int flags, new_flags; |
---|
793 | n/a | int res; |
---|
794 | n/a | #endif |
---|
795 | n/a | |
---|
796 | n/a | /* atomic_flag_works can only be used to make the file descriptor |
---|
797 | n/a | non-inheritable */ |
---|
798 | n/a | assert(!(atomic_flag_works != NULL && inheritable)); |
---|
799 | n/a | |
---|
800 | n/a | if (atomic_flag_works != NULL && !inheritable) { |
---|
801 | n/a | if (*atomic_flag_works == -1) { |
---|
802 | n/a | int isInheritable = get_inheritable(fd, raise); |
---|
803 | n/a | if (isInheritable == -1) |
---|
804 | n/a | return -1; |
---|
805 | n/a | *atomic_flag_works = !isInheritable; |
---|
806 | n/a | } |
---|
807 | n/a | |
---|
808 | n/a | if (*atomic_flag_works) |
---|
809 | n/a | return 0; |
---|
810 | n/a | } |
---|
811 | n/a | |
---|
812 | n/a | #ifdef MS_WINDOWS |
---|
813 | n/a | _Py_BEGIN_SUPPRESS_IPH |
---|
814 | n/a | handle = (HANDLE)_get_osfhandle(fd); |
---|
815 | n/a | _Py_END_SUPPRESS_IPH |
---|
816 | n/a | if (handle == INVALID_HANDLE_VALUE) { |
---|
817 | n/a | if (raise) |
---|
818 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
819 | n/a | return -1; |
---|
820 | n/a | } |
---|
821 | n/a | |
---|
822 | n/a | if (inheritable) |
---|
823 | n/a | flags = HANDLE_FLAG_INHERIT; |
---|
824 | n/a | else |
---|
825 | n/a | flags = 0; |
---|
826 | n/a | if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) { |
---|
827 | n/a | if (raise) |
---|
828 | n/a | PyErr_SetFromWindowsErr(0); |
---|
829 | n/a | return -1; |
---|
830 | n/a | } |
---|
831 | n/a | return 0; |
---|
832 | n/a | |
---|
833 | n/a | #else |
---|
834 | n/a | |
---|
835 | n/a | #if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX) |
---|
836 | n/a | if (ioctl_works != 0) { |
---|
837 | n/a | /* fast-path: ioctl() only requires one syscall */ |
---|
838 | n/a | if (inheritable) |
---|
839 | n/a | request = FIONCLEX; |
---|
840 | n/a | else |
---|
841 | n/a | request = FIOCLEX; |
---|
842 | n/a | err = ioctl(fd, request, NULL); |
---|
843 | n/a | if (!err) { |
---|
844 | n/a | ioctl_works = 1; |
---|
845 | n/a | return 0; |
---|
846 | n/a | } |
---|
847 | n/a | |
---|
848 | n/a | if (errno != ENOTTY && errno != EACCES) { |
---|
849 | n/a | if (raise) |
---|
850 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
851 | n/a | return -1; |
---|
852 | n/a | } |
---|
853 | n/a | else { |
---|
854 | n/a | /* Issue #22258: Here, ENOTTY means "Inappropriate ioctl for |
---|
855 | n/a | device". The ioctl is declared but not supported by the kernel. |
---|
856 | n/a | Remember that ioctl() doesn't work. It is the case on |
---|
857 | n/a | Illumos-based OS for example. |
---|
858 | n/a | |
---|
859 | n/a | Issue #27057: When SELinux policy disallows ioctl it will fail |
---|
860 | n/a | with EACCES. While FIOCLEX is safe operation it may be |
---|
861 | n/a | unavailable because ioctl was denied altogether. |
---|
862 | n/a | This can be the case on Android. */ |
---|
863 | n/a | ioctl_works = 0; |
---|
864 | n/a | } |
---|
865 | n/a | /* fallback to fcntl() if ioctl() does not work */ |
---|
866 | n/a | } |
---|
867 | n/a | #endif |
---|
868 | n/a | |
---|
869 | n/a | /* slow-path: fcntl() requires two syscalls */ |
---|
870 | n/a | flags = fcntl(fd, F_GETFD); |
---|
871 | n/a | if (flags < 0) { |
---|
872 | n/a | if (raise) |
---|
873 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
874 | n/a | return -1; |
---|
875 | n/a | } |
---|
876 | n/a | |
---|
877 | n/a | if (inheritable) { |
---|
878 | n/a | new_flags = flags & ~FD_CLOEXEC; |
---|
879 | n/a | } |
---|
880 | n/a | else { |
---|
881 | n/a | new_flags = flags | FD_CLOEXEC; |
---|
882 | n/a | } |
---|
883 | n/a | |
---|
884 | n/a | if (new_flags == flags) { |
---|
885 | n/a | /* FD_CLOEXEC flag already set/cleared: nothing to do */ |
---|
886 | n/a | return 0; |
---|
887 | n/a | } |
---|
888 | n/a | |
---|
889 | n/a | res = fcntl(fd, F_SETFD, new_flags); |
---|
890 | n/a | if (res < 0) { |
---|
891 | n/a | if (raise) |
---|
892 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
893 | n/a | return -1; |
---|
894 | n/a | } |
---|
895 | n/a | return 0; |
---|
896 | n/a | #endif |
---|
897 | n/a | } |
---|
898 | n/a | |
---|
899 | n/a | /* Make the file descriptor non-inheritable. |
---|
900 | n/a | Return 0 on success, set errno and return -1 on error. */ |
---|
901 | n/a | static int |
---|
902 | n/a | make_non_inheritable(int fd) |
---|
903 | n/a | { |
---|
904 | n/a | return set_inheritable(fd, 0, 0, NULL); |
---|
905 | n/a | } |
---|
906 | n/a | |
---|
907 | n/a | /* Set the inheritable flag of the specified file descriptor. |
---|
908 | n/a | On success: return 0, on error: raise an exception if raise is nonzero |
---|
909 | n/a | and return -1. |
---|
910 | n/a | |
---|
911 | n/a | If atomic_flag_works is not NULL: |
---|
912 | n/a | |
---|
913 | n/a | * if *atomic_flag_works==-1, check if the inheritable is set on the file |
---|
914 | n/a | descriptor: if yes, set *atomic_flag_works to 1, otherwise set to 0 and |
---|
915 | n/a | set the inheritable flag |
---|
916 | n/a | * if *atomic_flag_works==1: do nothing |
---|
917 | n/a | * if *atomic_flag_works==0: set inheritable flag to False |
---|
918 | n/a | |
---|
919 | n/a | Set atomic_flag_works to NULL if no atomic flag was used to create the |
---|
920 | n/a | file descriptor. |
---|
921 | n/a | |
---|
922 | n/a | atomic_flag_works can only be used to make a file descriptor |
---|
923 | n/a | non-inheritable: atomic_flag_works must be NULL if inheritable=1. */ |
---|
924 | n/a | int |
---|
925 | n/a | _Py_set_inheritable(int fd, int inheritable, int *atomic_flag_works) |
---|
926 | n/a | { |
---|
927 | n/a | return set_inheritable(fd, inheritable, 1, atomic_flag_works); |
---|
928 | n/a | } |
---|
929 | n/a | |
---|
930 | n/a | static int |
---|
931 | n/a | _Py_open_impl(const char *pathname, int flags, int gil_held) |
---|
932 | n/a | { |
---|
933 | n/a | int fd; |
---|
934 | n/a | int async_err = 0; |
---|
935 | n/a | #ifndef MS_WINDOWS |
---|
936 | n/a | int *atomic_flag_works; |
---|
937 | n/a | #endif |
---|
938 | n/a | |
---|
939 | n/a | #ifdef MS_WINDOWS |
---|
940 | n/a | flags |= O_NOINHERIT; |
---|
941 | n/a | #elif defined(O_CLOEXEC) |
---|
942 | n/a | atomic_flag_works = &_Py_open_cloexec_works; |
---|
943 | n/a | flags |= O_CLOEXEC; |
---|
944 | n/a | #else |
---|
945 | n/a | atomic_flag_works = NULL; |
---|
946 | n/a | #endif |
---|
947 | n/a | |
---|
948 | n/a | if (gil_held) { |
---|
949 | n/a | do { |
---|
950 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
951 | n/a | fd = open(pathname, flags); |
---|
952 | n/a | Py_END_ALLOW_THREADS |
---|
953 | n/a | } while (fd < 0 |
---|
954 | n/a | && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
---|
955 | n/a | if (async_err) |
---|
956 | n/a | return -1; |
---|
957 | n/a | if (fd < 0) { |
---|
958 | n/a | PyErr_SetFromErrnoWithFilename(PyExc_OSError, pathname); |
---|
959 | n/a | return -1; |
---|
960 | n/a | } |
---|
961 | n/a | } |
---|
962 | n/a | else { |
---|
963 | n/a | fd = open(pathname, flags); |
---|
964 | n/a | if (fd < 0) |
---|
965 | n/a | return -1; |
---|
966 | n/a | } |
---|
967 | n/a | |
---|
968 | n/a | #ifndef MS_WINDOWS |
---|
969 | n/a | if (set_inheritable(fd, 0, gil_held, atomic_flag_works) < 0) { |
---|
970 | n/a | close(fd); |
---|
971 | n/a | return -1; |
---|
972 | n/a | } |
---|
973 | n/a | #endif |
---|
974 | n/a | |
---|
975 | n/a | return fd; |
---|
976 | n/a | } |
---|
977 | n/a | |
---|
978 | n/a | /* Open a file with the specified flags (wrapper to open() function). |
---|
979 | n/a | Return a file descriptor on success. Raise an exception and return -1 on |
---|
980 | n/a | error. |
---|
981 | n/a | |
---|
982 | n/a | The file descriptor is created non-inheritable. |
---|
983 | n/a | |
---|
984 | n/a | When interrupted by a signal (open() fails with EINTR), retry the syscall, |
---|
985 | n/a | except if the Python signal handler raises an exception. |
---|
986 | n/a | |
---|
987 | n/a | Release the GIL to call open(). The caller must hold the GIL. */ |
---|
988 | n/a | int |
---|
989 | n/a | _Py_open(const char *pathname, int flags) |
---|
990 | n/a | { |
---|
991 | n/a | #ifdef WITH_THREAD |
---|
992 | n/a | /* _Py_open() must be called with the GIL held. */ |
---|
993 | n/a | assert(PyGILState_Check()); |
---|
994 | n/a | #endif |
---|
995 | n/a | return _Py_open_impl(pathname, flags, 1); |
---|
996 | n/a | } |
---|
997 | n/a | |
---|
998 | n/a | /* Open a file with the specified flags (wrapper to open() function). |
---|
999 | n/a | Return a file descriptor on success. Set errno and return -1 on error. |
---|
1000 | n/a | |
---|
1001 | n/a | The file descriptor is created non-inheritable. |
---|
1002 | n/a | |
---|
1003 | n/a | If interrupted by a signal, fail with EINTR. */ |
---|
1004 | n/a | int |
---|
1005 | n/a | _Py_open_noraise(const char *pathname, int flags) |
---|
1006 | n/a | { |
---|
1007 | n/a | return _Py_open_impl(pathname, flags, 0); |
---|
1008 | n/a | } |
---|
1009 | n/a | |
---|
1010 | n/a | /* Open a file. Use _wfopen() on Windows, encode the path to the locale |
---|
1011 | n/a | encoding and use fopen() otherwise. |
---|
1012 | n/a | |
---|
1013 | n/a | The file descriptor is created non-inheritable. |
---|
1014 | n/a | |
---|
1015 | n/a | If interrupted by a signal, fail with EINTR. */ |
---|
1016 | n/a | FILE * |
---|
1017 | n/a | _Py_wfopen(const wchar_t *path, const wchar_t *mode) |
---|
1018 | n/a | { |
---|
1019 | n/a | FILE *f; |
---|
1020 | n/a | #ifndef MS_WINDOWS |
---|
1021 | n/a | char *cpath; |
---|
1022 | n/a | char cmode[10]; |
---|
1023 | n/a | size_t r; |
---|
1024 | n/a | r = wcstombs(cmode, mode, 10); |
---|
1025 | n/a | if (r == (size_t)-1 || r >= 10) { |
---|
1026 | n/a | errno = EINVAL; |
---|
1027 | n/a | return NULL; |
---|
1028 | n/a | } |
---|
1029 | n/a | cpath = Py_EncodeLocale(path, NULL); |
---|
1030 | n/a | if (cpath == NULL) |
---|
1031 | n/a | return NULL; |
---|
1032 | n/a | f = fopen(cpath, cmode); |
---|
1033 | n/a | PyMem_Free(cpath); |
---|
1034 | n/a | #else |
---|
1035 | n/a | f = _wfopen(path, mode); |
---|
1036 | n/a | #endif |
---|
1037 | n/a | if (f == NULL) |
---|
1038 | n/a | return NULL; |
---|
1039 | n/a | if (make_non_inheritable(fileno(f)) < 0) { |
---|
1040 | n/a | fclose(f); |
---|
1041 | n/a | return NULL; |
---|
1042 | n/a | } |
---|
1043 | n/a | return f; |
---|
1044 | n/a | } |
---|
1045 | n/a | |
---|
1046 | n/a | /* Wrapper to fopen(). |
---|
1047 | n/a | |
---|
1048 | n/a | The file descriptor is created non-inheritable. |
---|
1049 | n/a | |
---|
1050 | n/a | If interrupted by a signal, fail with EINTR. */ |
---|
1051 | n/a | FILE* |
---|
1052 | n/a | _Py_fopen(const char *pathname, const char *mode) |
---|
1053 | n/a | { |
---|
1054 | n/a | FILE *f = fopen(pathname, mode); |
---|
1055 | n/a | if (f == NULL) |
---|
1056 | n/a | return NULL; |
---|
1057 | n/a | if (make_non_inheritable(fileno(f)) < 0) { |
---|
1058 | n/a | fclose(f); |
---|
1059 | n/a | return NULL; |
---|
1060 | n/a | } |
---|
1061 | n/a | return f; |
---|
1062 | n/a | } |
---|
1063 | n/a | |
---|
1064 | n/a | /* Open a file. Call _wfopen() on Windows, or encode the path to the filesystem |
---|
1065 | n/a | encoding and call fopen() otherwise. |
---|
1066 | n/a | |
---|
1067 | n/a | Return the new file object on success. Raise an exception and return NULL |
---|
1068 | n/a | on error. |
---|
1069 | n/a | |
---|
1070 | n/a | The file descriptor is created non-inheritable. |
---|
1071 | n/a | |
---|
1072 | n/a | When interrupted by a signal (open() fails with EINTR), retry the syscall, |
---|
1073 | n/a | except if the Python signal handler raises an exception. |
---|
1074 | n/a | |
---|
1075 | n/a | Release the GIL to call _wfopen() or fopen(). The caller must hold |
---|
1076 | n/a | the GIL. */ |
---|
1077 | n/a | FILE* |
---|
1078 | n/a | _Py_fopen_obj(PyObject *path, const char *mode) |
---|
1079 | n/a | { |
---|
1080 | n/a | FILE *f; |
---|
1081 | n/a | int async_err = 0; |
---|
1082 | n/a | #ifdef MS_WINDOWS |
---|
1083 | n/a | wchar_t *wpath; |
---|
1084 | n/a | wchar_t wmode[10]; |
---|
1085 | n/a | int usize; |
---|
1086 | n/a | |
---|
1087 | n/a | #ifdef WITH_THREAD |
---|
1088 | n/a | assert(PyGILState_Check()); |
---|
1089 | n/a | #endif |
---|
1090 | n/a | |
---|
1091 | n/a | if (!PyUnicode_Check(path)) { |
---|
1092 | n/a | PyErr_Format(PyExc_TypeError, |
---|
1093 | n/a | "str file path expected under Windows, got %R", |
---|
1094 | n/a | Py_TYPE(path)); |
---|
1095 | n/a | return NULL; |
---|
1096 | n/a | } |
---|
1097 | n/a | wpath = PyUnicode_AsUnicode(path); |
---|
1098 | n/a | if (wpath == NULL) |
---|
1099 | n/a | return NULL; |
---|
1100 | n/a | |
---|
1101 | n/a | usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode)); |
---|
1102 | n/a | if (usize == 0) { |
---|
1103 | n/a | PyErr_SetFromWindowsErr(0); |
---|
1104 | n/a | return NULL; |
---|
1105 | n/a | } |
---|
1106 | n/a | |
---|
1107 | n/a | do { |
---|
1108 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
1109 | n/a | f = _wfopen(wpath, wmode); |
---|
1110 | n/a | Py_END_ALLOW_THREADS |
---|
1111 | n/a | } while (f == NULL |
---|
1112 | n/a | && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
---|
1113 | n/a | #else |
---|
1114 | n/a | PyObject *bytes; |
---|
1115 | n/a | char *path_bytes; |
---|
1116 | n/a | |
---|
1117 | n/a | #ifdef WITH_THREAD |
---|
1118 | n/a | assert(PyGILState_Check()); |
---|
1119 | n/a | #endif |
---|
1120 | n/a | |
---|
1121 | n/a | if (!PyUnicode_FSConverter(path, &bytes)) |
---|
1122 | n/a | return NULL; |
---|
1123 | n/a | path_bytes = PyBytes_AS_STRING(bytes); |
---|
1124 | n/a | |
---|
1125 | n/a | do { |
---|
1126 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
1127 | n/a | f = fopen(path_bytes, mode); |
---|
1128 | n/a | Py_END_ALLOW_THREADS |
---|
1129 | n/a | } while (f == NULL |
---|
1130 | n/a | && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
---|
1131 | n/a | |
---|
1132 | n/a | Py_DECREF(bytes); |
---|
1133 | n/a | #endif |
---|
1134 | n/a | if (async_err) |
---|
1135 | n/a | return NULL; |
---|
1136 | n/a | |
---|
1137 | n/a | if (f == NULL) { |
---|
1138 | n/a | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path); |
---|
1139 | n/a | return NULL; |
---|
1140 | n/a | } |
---|
1141 | n/a | |
---|
1142 | n/a | if (set_inheritable(fileno(f), 0, 1, NULL) < 0) { |
---|
1143 | n/a | fclose(f); |
---|
1144 | n/a | return NULL; |
---|
1145 | n/a | } |
---|
1146 | n/a | return f; |
---|
1147 | n/a | } |
---|
1148 | n/a | |
---|
1149 | n/a | /* Read count bytes from fd into buf. |
---|
1150 | n/a | |
---|
1151 | n/a | On success, return the number of read bytes, it can be lower than count. |
---|
1152 | n/a | If the current file offset is at or past the end of file, no bytes are read, |
---|
1153 | n/a | and read() returns zero. |
---|
1154 | n/a | |
---|
1155 | n/a | On error, raise an exception, set errno and return -1. |
---|
1156 | n/a | |
---|
1157 | n/a | When interrupted by a signal (read() fails with EINTR), retry the syscall. |
---|
1158 | n/a | If the Python signal handler raises an exception, the function returns -1 |
---|
1159 | n/a | (the syscall is not retried). |
---|
1160 | n/a | |
---|
1161 | n/a | Release the GIL to call read(). The caller must hold the GIL. */ |
---|
1162 | n/a | Py_ssize_t |
---|
1163 | n/a | _Py_read(int fd, void *buf, size_t count) |
---|
1164 | n/a | { |
---|
1165 | n/a | Py_ssize_t n; |
---|
1166 | n/a | int err; |
---|
1167 | n/a | int async_err = 0; |
---|
1168 | n/a | |
---|
1169 | n/a | #ifdef WITH_THREAD |
---|
1170 | n/a | assert(PyGILState_Check()); |
---|
1171 | n/a | #endif |
---|
1172 | n/a | |
---|
1173 | n/a | /* _Py_read() must not be called with an exception set, otherwise the |
---|
1174 | n/a | * caller may think that read() was interrupted by a signal and the signal |
---|
1175 | n/a | * handler raised an exception. */ |
---|
1176 | n/a | assert(!PyErr_Occurred()); |
---|
1177 | n/a | |
---|
1178 | n/a | #ifdef MS_WINDOWS |
---|
1179 | n/a | if (count > INT_MAX) { |
---|
1180 | n/a | /* On Windows, the count parameter of read() is an int */ |
---|
1181 | n/a | count = INT_MAX; |
---|
1182 | n/a | } |
---|
1183 | n/a | #else |
---|
1184 | n/a | if (count > PY_SSIZE_T_MAX) { |
---|
1185 | n/a | /* if count is greater than PY_SSIZE_T_MAX, |
---|
1186 | n/a | * read() result is undefined */ |
---|
1187 | n/a | count = PY_SSIZE_T_MAX; |
---|
1188 | n/a | } |
---|
1189 | n/a | #endif |
---|
1190 | n/a | |
---|
1191 | n/a | _Py_BEGIN_SUPPRESS_IPH |
---|
1192 | n/a | do { |
---|
1193 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
1194 | n/a | errno = 0; |
---|
1195 | n/a | #ifdef MS_WINDOWS |
---|
1196 | n/a | n = read(fd, buf, (int)count); |
---|
1197 | n/a | #else |
---|
1198 | n/a | n = read(fd, buf, count); |
---|
1199 | n/a | #endif |
---|
1200 | n/a | /* save/restore errno because PyErr_CheckSignals() |
---|
1201 | n/a | * and PyErr_SetFromErrno() can modify it */ |
---|
1202 | n/a | err = errno; |
---|
1203 | n/a | Py_END_ALLOW_THREADS |
---|
1204 | n/a | } while (n < 0 && err == EINTR && |
---|
1205 | n/a | !(async_err = PyErr_CheckSignals())); |
---|
1206 | n/a | _Py_END_SUPPRESS_IPH |
---|
1207 | n/a | |
---|
1208 | n/a | if (async_err) { |
---|
1209 | n/a | /* read() was interrupted by a signal (failed with EINTR) |
---|
1210 | n/a | * and the Python signal handler raised an exception */ |
---|
1211 | n/a | errno = err; |
---|
1212 | n/a | assert(errno == EINTR && PyErr_Occurred()); |
---|
1213 | n/a | return -1; |
---|
1214 | n/a | } |
---|
1215 | n/a | if (n < 0) { |
---|
1216 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
1217 | n/a | errno = err; |
---|
1218 | n/a | return -1; |
---|
1219 | n/a | } |
---|
1220 | n/a | |
---|
1221 | n/a | return n; |
---|
1222 | n/a | } |
---|
1223 | n/a | |
---|
1224 | n/a | static Py_ssize_t |
---|
1225 | n/a | _Py_write_impl(int fd, const void *buf, size_t count, int gil_held) |
---|
1226 | n/a | { |
---|
1227 | n/a | Py_ssize_t n; |
---|
1228 | n/a | int err; |
---|
1229 | n/a | int async_err = 0; |
---|
1230 | n/a | |
---|
1231 | n/a | _Py_BEGIN_SUPPRESS_IPH |
---|
1232 | n/a | #ifdef MS_WINDOWS |
---|
1233 | n/a | if (count > 32767 && isatty(fd)) { |
---|
1234 | n/a | /* Issue #11395: the Windows console returns an error (12: not |
---|
1235 | n/a | enough space error) on writing into stdout if stdout mode is |
---|
1236 | n/a | binary and the length is greater than 66,000 bytes (or less, |
---|
1237 | n/a | depending on heap usage). */ |
---|
1238 | n/a | count = 32767; |
---|
1239 | n/a | } |
---|
1240 | n/a | else if (count > INT_MAX) |
---|
1241 | n/a | count = INT_MAX; |
---|
1242 | n/a | #else |
---|
1243 | n/a | if (count > PY_SSIZE_T_MAX) { |
---|
1244 | n/a | /* write() should truncate count to PY_SSIZE_T_MAX, but it's safer |
---|
1245 | n/a | * to do it ourself to have a portable behaviour. */ |
---|
1246 | n/a | count = PY_SSIZE_T_MAX; |
---|
1247 | n/a | } |
---|
1248 | n/a | #endif |
---|
1249 | n/a | |
---|
1250 | n/a | if (gil_held) { |
---|
1251 | n/a | do { |
---|
1252 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
1253 | n/a | errno = 0; |
---|
1254 | n/a | #ifdef MS_WINDOWS |
---|
1255 | n/a | n = write(fd, buf, (int)count); |
---|
1256 | n/a | #else |
---|
1257 | n/a | n = write(fd, buf, count); |
---|
1258 | n/a | #endif |
---|
1259 | n/a | /* save/restore errno because PyErr_CheckSignals() |
---|
1260 | n/a | * and PyErr_SetFromErrno() can modify it */ |
---|
1261 | n/a | err = errno; |
---|
1262 | n/a | Py_END_ALLOW_THREADS |
---|
1263 | n/a | } while (n < 0 && err == EINTR && |
---|
1264 | n/a | !(async_err = PyErr_CheckSignals())); |
---|
1265 | n/a | } |
---|
1266 | n/a | else { |
---|
1267 | n/a | do { |
---|
1268 | n/a | errno = 0; |
---|
1269 | n/a | #ifdef MS_WINDOWS |
---|
1270 | n/a | n = write(fd, buf, (int)count); |
---|
1271 | n/a | #else |
---|
1272 | n/a | n = write(fd, buf, count); |
---|
1273 | n/a | #endif |
---|
1274 | n/a | err = errno; |
---|
1275 | n/a | } while (n < 0 && err == EINTR); |
---|
1276 | n/a | } |
---|
1277 | n/a | _Py_END_SUPPRESS_IPH |
---|
1278 | n/a | |
---|
1279 | n/a | if (async_err) { |
---|
1280 | n/a | /* write() was interrupted by a signal (failed with EINTR) |
---|
1281 | n/a | and the Python signal handler raised an exception (if gil_held is |
---|
1282 | n/a | nonzero). */ |
---|
1283 | n/a | errno = err; |
---|
1284 | n/a | assert(errno == EINTR && (!gil_held || PyErr_Occurred())); |
---|
1285 | n/a | return -1; |
---|
1286 | n/a | } |
---|
1287 | n/a | if (n < 0) { |
---|
1288 | n/a | if (gil_held) |
---|
1289 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
1290 | n/a | errno = err; |
---|
1291 | n/a | return -1; |
---|
1292 | n/a | } |
---|
1293 | n/a | |
---|
1294 | n/a | return n; |
---|
1295 | n/a | } |
---|
1296 | n/a | |
---|
1297 | n/a | /* Write count bytes of buf into fd. |
---|
1298 | n/a | |
---|
1299 | n/a | On success, return the number of written bytes, it can be lower than count |
---|
1300 | n/a | including 0. On error, raise an exception, set errno and return -1. |
---|
1301 | n/a | |
---|
1302 | n/a | When interrupted by a signal (write() fails with EINTR), retry the syscall. |
---|
1303 | n/a | If the Python signal handler raises an exception, the function returns -1 |
---|
1304 | n/a | (the syscall is not retried). |
---|
1305 | n/a | |
---|
1306 | n/a | Release the GIL to call write(). The caller must hold the GIL. */ |
---|
1307 | n/a | Py_ssize_t |
---|
1308 | n/a | _Py_write(int fd, const void *buf, size_t count) |
---|
1309 | n/a | { |
---|
1310 | n/a | #ifdef WITH_THREAD |
---|
1311 | n/a | assert(PyGILState_Check()); |
---|
1312 | n/a | #endif |
---|
1313 | n/a | |
---|
1314 | n/a | /* _Py_write() must not be called with an exception set, otherwise the |
---|
1315 | n/a | * caller may think that write() was interrupted by a signal and the signal |
---|
1316 | n/a | * handler raised an exception. */ |
---|
1317 | n/a | assert(!PyErr_Occurred()); |
---|
1318 | n/a | |
---|
1319 | n/a | return _Py_write_impl(fd, buf, count, 1); |
---|
1320 | n/a | } |
---|
1321 | n/a | |
---|
1322 | n/a | /* Write count bytes of buf into fd. |
---|
1323 | n/a | * |
---|
1324 | n/a | * On success, return the number of written bytes, it can be lower than count |
---|
1325 | n/a | * including 0. On error, set errno and return -1. |
---|
1326 | n/a | * |
---|
1327 | n/a | * When interrupted by a signal (write() fails with EINTR), retry the syscall |
---|
1328 | n/a | * without calling the Python signal handler. */ |
---|
1329 | n/a | Py_ssize_t |
---|
1330 | n/a | _Py_write_noraise(int fd, const void *buf, size_t count) |
---|
1331 | n/a | { |
---|
1332 | n/a | return _Py_write_impl(fd, buf, count, 0); |
---|
1333 | n/a | } |
---|
1334 | n/a | |
---|
1335 | n/a | #ifdef HAVE_READLINK |
---|
1336 | n/a | |
---|
1337 | n/a | /* Read value of symbolic link. Encode the path to the locale encoding, decode |
---|
1338 | n/a | the result from the locale encoding. Return -1 on error. */ |
---|
1339 | n/a | |
---|
1340 | n/a | int |
---|
1341 | n/a | _Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz) |
---|
1342 | n/a | { |
---|
1343 | n/a | char *cpath; |
---|
1344 | n/a | char cbuf[MAXPATHLEN]; |
---|
1345 | n/a | wchar_t *wbuf; |
---|
1346 | n/a | int res; |
---|
1347 | n/a | size_t r1; |
---|
1348 | n/a | |
---|
1349 | n/a | cpath = Py_EncodeLocale(path, NULL); |
---|
1350 | n/a | if (cpath == NULL) { |
---|
1351 | n/a | errno = EINVAL; |
---|
1352 | n/a | return -1; |
---|
1353 | n/a | } |
---|
1354 | n/a | res = (int)readlink(cpath, cbuf, Py_ARRAY_LENGTH(cbuf)); |
---|
1355 | n/a | PyMem_Free(cpath); |
---|
1356 | n/a | if (res == -1) |
---|
1357 | n/a | return -1; |
---|
1358 | n/a | if (res == Py_ARRAY_LENGTH(cbuf)) { |
---|
1359 | n/a | errno = EINVAL; |
---|
1360 | n/a | return -1; |
---|
1361 | n/a | } |
---|
1362 | n/a | cbuf[res] = '\0'; /* buf will be null terminated */ |
---|
1363 | n/a | wbuf = Py_DecodeLocale(cbuf, &r1); |
---|
1364 | n/a | if (wbuf == NULL) { |
---|
1365 | n/a | errno = EINVAL; |
---|
1366 | n/a | return -1; |
---|
1367 | n/a | } |
---|
1368 | n/a | if (bufsiz <= r1) { |
---|
1369 | n/a | PyMem_RawFree(wbuf); |
---|
1370 | n/a | errno = EINVAL; |
---|
1371 | n/a | return -1; |
---|
1372 | n/a | } |
---|
1373 | n/a | wcsncpy(buf, wbuf, bufsiz); |
---|
1374 | n/a | PyMem_RawFree(wbuf); |
---|
1375 | n/a | return (int)r1; |
---|
1376 | n/a | } |
---|
1377 | n/a | #endif |
---|
1378 | n/a | |
---|
1379 | n/a | #ifdef HAVE_REALPATH |
---|
1380 | n/a | |
---|
1381 | n/a | /* Return the canonicalized absolute pathname. Encode path to the locale |
---|
1382 | n/a | encoding, decode the result from the locale encoding. |
---|
1383 | n/a | Return NULL on error. */ |
---|
1384 | n/a | |
---|
1385 | n/a | wchar_t* |
---|
1386 | n/a | _Py_wrealpath(const wchar_t *path, |
---|
1387 | n/a | wchar_t *resolved_path, size_t resolved_path_size) |
---|
1388 | n/a | { |
---|
1389 | n/a | char *cpath; |
---|
1390 | n/a | char cresolved_path[MAXPATHLEN]; |
---|
1391 | n/a | wchar_t *wresolved_path; |
---|
1392 | n/a | char *res; |
---|
1393 | n/a | size_t r; |
---|
1394 | n/a | cpath = Py_EncodeLocale(path, NULL); |
---|
1395 | n/a | if (cpath == NULL) { |
---|
1396 | n/a | errno = EINVAL; |
---|
1397 | n/a | return NULL; |
---|
1398 | n/a | } |
---|
1399 | n/a | res = realpath(cpath, cresolved_path); |
---|
1400 | n/a | PyMem_Free(cpath); |
---|
1401 | n/a | if (res == NULL) |
---|
1402 | n/a | return NULL; |
---|
1403 | n/a | |
---|
1404 | n/a | wresolved_path = Py_DecodeLocale(cresolved_path, &r); |
---|
1405 | n/a | if (wresolved_path == NULL) { |
---|
1406 | n/a | errno = EINVAL; |
---|
1407 | n/a | return NULL; |
---|
1408 | n/a | } |
---|
1409 | n/a | if (resolved_path_size <= r) { |
---|
1410 | n/a | PyMem_RawFree(wresolved_path); |
---|
1411 | n/a | errno = EINVAL; |
---|
1412 | n/a | return NULL; |
---|
1413 | n/a | } |
---|
1414 | n/a | wcsncpy(resolved_path, wresolved_path, resolved_path_size); |
---|
1415 | n/a | PyMem_RawFree(wresolved_path); |
---|
1416 | n/a | return resolved_path; |
---|
1417 | n/a | } |
---|
1418 | n/a | #endif |
---|
1419 | n/a | |
---|
1420 | n/a | /* Get the current directory. size is the buffer size in wide characters |
---|
1421 | n/a | including the null character. Decode the path from the locale encoding. |
---|
1422 | n/a | Return NULL on error. */ |
---|
1423 | n/a | |
---|
1424 | n/a | wchar_t* |
---|
1425 | n/a | _Py_wgetcwd(wchar_t *buf, size_t size) |
---|
1426 | n/a | { |
---|
1427 | n/a | #ifdef MS_WINDOWS |
---|
1428 | n/a | int isize = (int)Py_MIN(size, INT_MAX); |
---|
1429 | n/a | return _wgetcwd(buf, isize); |
---|
1430 | n/a | #else |
---|
1431 | n/a | char fname[MAXPATHLEN]; |
---|
1432 | n/a | wchar_t *wname; |
---|
1433 | n/a | size_t len; |
---|
1434 | n/a | |
---|
1435 | n/a | if (getcwd(fname, Py_ARRAY_LENGTH(fname)) == NULL) |
---|
1436 | n/a | return NULL; |
---|
1437 | n/a | wname = Py_DecodeLocale(fname, &len); |
---|
1438 | n/a | if (wname == NULL) |
---|
1439 | n/a | return NULL; |
---|
1440 | n/a | if (size <= len) { |
---|
1441 | n/a | PyMem_RawFree(wname); |
---|
1442 | n/a | return NULL; |
---|
1443 | n/a | } |
---|
1444 | n/a | wcsncpy(buf, wname, size); |
---|
1445 | n/a | PyMem_RawFree(wname); |
---|
1446 | n/a | return buf; |
---|
1447 | n/a | #endif |
---|
1448 | n/a | } |
---|
1449 | n/a | |
---|
1450 | n/a | /* Duplicate a file descriptor. The new file descriptor is created as |
---|
1451 | n/a | non-inheritable. Return a new file descriptor on success, raise an OSError |
---|
1452 | n/a | exception and return -1 on error. |
---|
1453 | n/a | |
---|
1454 | n/a | The GIL is released to call dup(). The caller must hold the GIL. */ |
---|
1455 | n/a | int |
---|
1456 | n/a | _Py_dup(int fd) |
---|
1457 | n/a | { |
---|
1458 | n/a | #ifdef MS_WINDOWS |
---|
1459 | n/a | HANDLE handle; |
---|
1460 | n/a | DWORD ftype; |
---|
1461 | n/a | #endif |
---|
1462 | n/a | |
---|
1463 | n/a | #ifdef WITH_THREAD |
---|
1464 | n/a | assert(PyGILState_Check()); |
---|
1465 | n/a | #endif |
---|
1466 | n/a | |
---|
1467 | n/a | #ifdef MS_WINDOWS |
---|
1468 | n/a | _Py_BEGIN_SUPPRESS_IPH |
---|
1469 | n/a | handle = (HANDLE)_get_osfhandle(fd); |
---|
1470 | n/a | _Py_END_SUPPRESS_IPH |
---|
1471 | n/a | if (handle == INVALID_HANDLE_VALUE) { |
---|
1472 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
1473 | n/a | return -1; |
---|
1474 | n/a | } |
---|
1475 | n/a | |
---|
1476 | n/a | /* get the file type, ignore the error if it failed */ |
---|
1477 | n/a | ftype = GetFileType(handle); |
---|
1478 | n/a | |
---|
1479 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
1480 | n/a | _Py_BEGIN_SUPPRESS_IPH |
---|
1481 | n/a | fd = dup(fd); |
---|
1482 | n/a | _Py_END_SUPPRESS_IPH |
---|
1483 | n/a | Py_END_ALLOW_THREADS |
---|
1484 | n/a | if (fd < 0) { |
---|
1485 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
1486 | n/a | return -1; |
---|
1487 | n/a | } |
---|
1488 | n/a | |
---|
1489 | n/a | /* Character files like console cannot be make non-inheritable */ |
---|
1490 | n/a | if (ftype != FILE_TYPE_CHAR) { |
---|
1491 | n/a | if (_Py_set_inheritable(fd, 0, NULL) < 0) { |
---|
1492 | n/a | _Py_BEGIN_SUPPRESS_IPH |
---|
1493 | n/a | close(fd); |
---|
1494 | n/a | _Py_END_SUPPRESS_IPH |
---|
1495 | n/a | return -1; |
---|
1496 | n/a | } |
---|
1497 | n/a | } |
---|
1498 | n/a | #elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC) |
---|
1499 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
1500 | n/a | _Py_BEGIN_SUPPRESS_IPH |
---|
1501 | n/a | fd = fcntl(fd, F_DUPFD_CLOEXEC, 0); |
---|
1502 | n/a | _Py_END_SUPPRESS_IPH |
---|
1503 | n/a | Py_END_ALLOW_THREADS |
---|
1504 | n/a | if (fd < 0) { |
---|
1505 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
1506 | n/a | return -1; |
---|
1507 | n/a | } |
---|
1508 | n/a | |
---|
1509 | n/a | #else |
---|
1510 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
1511 | n/a | _Py_BEGIN_SUPPRESS_IPH |
---|
1512 | n/a | fd = dup(fd); |
---|
1513 | n/a | _Py_END_SUPPRESS_IPH |
---|
1514 | n/a | Py_END_ALLOW_THREADS |
---|
1515 | n/a | if (fd < 0) { |
---|
1516 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
1517 | n/a | return -1; |
---|
1518 | n/a | } |
---|
1519 | n/a | |
---|
1520 | n/a | if (_Py_set_inheritable(fd, 0, NULL) < 0) { |
---|
1521 | n/a | _Py_BEGIN_SUPPRESS_IPH |
---|
1522 | n/a | close(fd); |
---|
1523 | n/a | _Py_END_SUPPRESS_IPH |
---|
1524 | n/a | return -1; |
---|
1525 | n/a | } |
---|
1526 | n/a | #endif |
---|
1527 | n/a | return fd; |
---|
1528 | n/a | } |
---|
1529 | n/a | |
---|
1530 | n/a | #ifndef MS_WINDOWS |
---|
1531 | n/a | /* Get the blocking mode of the file descriptor. |
---|
1532 | n/a | Return 0 if the O_NONBLOCK flag is set, 1 if the flag is cleared, |
---|
1533 | n/a | raise an exception and return -1 on error. */ |
---|
1534 | n/a | int |
---|
1535 | n/a | _Py_get_blocking(int fd) |
---|
1536 | n/a | { |
---|
1537 | n/a | int flags; |
---|
1538 | n/a | _Py_BEGIN_SUPPRESS_IPH |
---|
1539 | n/a | flags = fcntl(fd, F_GETFL, 0); |
---|
1540 | n/a | _Py_END_SUPPRESS_IPH |
---|
1541 | n/a | if (flags < 0) { |
---|
1542 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
1543 | n/a | return -1; |
---|
1544 | n/a | } |
---|
1545 | n/a | |
---|
1546 | n/a | return !(flags & O_NONBLOCK); |
---|
1547 | n/a | } |
---|
1548 | n/a | |
---|
1549 | n/a | /* Set the blocking mode of the specified file descriptor. |
---|
1550 | n/a | |
---|
1551 | n/a | Set the O_NONBLOCK flag if blocking is False, clear the O_NONBLOCK flag |
---|
1552 | n/a | otherwise. |
---|
1553 | n/a | |
---|
1554 | n/a | Return 0 on success, raise an exception and return -1 on error. */ |
---|
1555 | n/a | int |
---|
1556 | n/a | _Py_set_blocking(int fd, int blocking) |
---|
1557 | n/a | { |
---|
1558 | n/a | #if defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO) |
---|
1559 | n/a | int arg = !blocking; |
---|
1560 | n/a | if (ioctl(fd, FIONBIO, &arg) < 0) |
---|
1561 | n/a | goto error; |
---|
1562 | n/a | #else |
---|
1563 | n/a | int flags, res; |
---|
1564 | n/a | |
---|
1565 | n/a | _Py_BEGIN_SUPPRESS_IPH |
---|
1566 | n/a | flags = fcntl(fd, F_GETFL, 0); |
---|
1567 | n/a | if (flags >= 0) { |
---|
1568 | n/a | if (blocking) |
---|
1569 | n/a | flags = flags & (~O_NONBLOCK); |
---|
1570 | n/a | else |
---|
1571 | n/a | flags = flags | O_NONBLOCK; |
---|
1572 | n/a | |
---|
1573 | n/a | res = fcntl(fd, F_SETFL, flags); |
---|
1574 | n/a | } else { |
---|
1575 | n/a | res = -1; |
---|
1576 | n/a | } |
---|
1577 | n/a | _Py_END_SUPPRESS_IPH |
---|
1578 | n/a | |
---|
1579 | n/a | if (res < 0) |
---|
1580 | n/a | goto error; |
---|
1581 | n/a | #endif |
---|
1582 | n/a | return 0; |
---|
1583 | n/a | |
---|
1584 | n/a | error: |
---|
1585 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
1586 | n/a | return -1; |
---|
1587 | n/a | } |
---|
1588 | n/a | #endif |
---|